blob: 03a719f445598c1644f713ac04adf491ff52c754 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips64/asm_support_mips64.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070020#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Alexey Frunzec857c742015-09-23 15:12:39 -070022#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080023#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070028#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070029#include "intrinsics_mips64.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030#include "linker/linker_patch.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "mirror/array-inl.h"
32#include "mirror/class-inl.h"
33#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010034#include "stack_map_stream.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070035#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070037#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070038#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips64 {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr GpuRegister kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010051Location Mips64ReturnLocation(DataType::Type return_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070052 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010054 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010055 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
60 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070061 return Location::RegisterLocation(V0);
62
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kFloat32:
64 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -070065 return Location::FpuRegisterLocation(F0);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -070068 return Location();
69 }
70 UNREACHABLE();
71}
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(DataType::Type type) const {
Alexey Frunze4dda3372015-06-01 18:31:49 -070074 return Mips64ReturnLocation(type);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
78 return Location::RegisterLocation(kMethodRegisterArgument);
79}
80
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070082 Location next_location;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083 if (type == DataType::Type::kVoid) {
Alexey Frunze4dda3372015-06-01 18:31:49 -070084 LOG(FATAL) << "Unexpected parameter type " << type;
85 }
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 if (DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070088 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
89 next_location = Location::FpuRegisterLocation(
90 calling_convention.GetFpuRegisterAt(float_index_++));
91 gp_index_++;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 } else if (!DataType::IsFloatingPointType(type) &&
Alexey Frunze4dda3372015-06-01 18:31:49 -070093 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
95 float_index_++;
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010098 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
99 : Location::StackSlot(stack_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700100 }
101
102 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
Alexey Frunze4dda3372015-06-01 18:31:49 -0700105 return next_location;
106}
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109 return Mips64ReturnLocation(type);
110}
111
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100112// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
113#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700114#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115
116class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
117 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000118 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119
120 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100121 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
123 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000124 if (instruction_->CanThrowIntoCatchBlock()) {
125 // Live registers will be restored in the catch block if caught.
126 SaveLiveRegisters(codegen, instruction_->GetLocations());
127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 // We're moving two locations to locations that could overlap, so we need a parallel
129 // move resolver.
130 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 DataType::Type::kInt32,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DataType::Type::kInt32);
Serban Constantinescufc734082016-07-19 17:18:07 +0100137 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
138 ? kQuickThrowStringBounds
139 : kQuickThrowArrayBounds;
140 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100141 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
143 }
144
Alexandre Rames8158f282015-08-07 10:26:17 +0100145 bool IsFatal() const OVERRIDE { return true; }
146
Roland Levillain46648892015-06-19 16:07:18 +0100147 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
148
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
151};
152
153class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
154 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700155 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
156 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
160 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100161 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
163 }
164
Alexandre Rames8158f282015-08-07 10:26:17 +0100165 bool IsFatal() const OVERRIDE { return true; }
166
Roland Levillain46648892015-06-19 16:07:18 +0100167 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
168
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700170 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
171};
172
173class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
174 public:
175 LoadClassSlowPathMIPS64(HLoadClass* cls,
176 HInstruction* at,
177 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000178 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700179 : SlowPathCodeMIPS64(at),
180 cls_(cls),
181 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000182 do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000187 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700188 Location out = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700189 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700190 InvokeRuntimeCallingConvention calling_convention;
191 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 dex::TypeIndex type_index = cls_->GetTypeIndex();
196 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100197 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
198 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700200 if (do_clinit_) {
201 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
202 } else {
203 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
204 }
205
206 // Move the class to the desired location.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 if (out.IsValid()) {
208 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100209 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700210 mips64_codegen->MoveLocation(out,
211 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
212 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700214 RestoreLiveRegisters(codegen, locations);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700215
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700216 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 }
218
Roland Levillain46648892015-06-19 16:07:18 +0100219 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 private:
222 // The class this slow path will load.
223 HLoadClass* const cls_;
224
Alexey Frunze4dda3372015-06-01 18:31:49 -0700225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
232};
233
234class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
235 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000236 explicit LoadStringSlowPathMIPS64(HLoadString* instruction)
237 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700240 DCHECK(instruction_->IsLoadString());
241 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700245 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700246 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000250 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100251 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700252 instruction_,
253 instruction_->GetDexPc(),
254 this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100257 DataType::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700258 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800262
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700263 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700264 }
265
Roland Levillain46648892015-06-19 16:07:18 +0100266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
267
Alexey Frunze4dda3372015-06-01 18:31:49 -0700268 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
270};
271
272class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
273 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000274 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275
276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
277 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
278 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000279 if (instruction_->CanThrowIntoCatchBlock()) {
280 // Live registers will be restored in the catch block if caught.
281 SaveLiveRegisters(codegen, instruction_->GetLocations());
282 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100283 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 instruction_,
285 instruction_->GetDexPc(),
286 this);
287 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
288 }
289
Alexandre Rames8158f282015-08-07 10:26:17 +0100290 bool IsFatal() const OVERRIDE { return true; }
291
Roland Levillain46648892015-06-19 16:07:18 +0100292 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
293
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
296};
297
298class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
299 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100300 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000301 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302
303 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200304 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200307 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100308 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200310 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 }
316 }
317
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 DCHECK(successor_ == nullptr);
320 return &return_label_;
321 }
322
Roland Levillain46648892015-06-19 16:07:18 +0100323 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
324
Chris Larsena2045912017-11-02 12:39:54 -0700325 HBasicBlock* GetSuccessor() const {
326 return successor_;
327 }
328
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700334 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
342 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343
344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
345 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800346
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
350 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
351
352 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800353 if (!is_fatal_) {
354 SaveLiveRegisters(codegen, locations);
355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356
357 // We're moving two locations to locations that could overlap, so we need a parallel
358 // move resolver.
359 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100365 DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100367 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800368 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100369 DataType::Type ret_type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
371 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 } else {
373 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800374 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800378 if (!is_fatal_) {
379 RestoreLiveRegisters(codegen, locations);
380 __ Bc(GetExitLabel());
381 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
385
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 bool IsFatal() const OVERRIDE { return is_fatal_; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800389 const bool is_fatal_;
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
392};
393
394class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
395 public:
Aart Bik42249c32016-01-07 15:33:50 -0800396 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000397 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800400 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 LocationSummary* locations = instruction_->GetLocations();
403 SaveLiveRegisters(codegen, locations);
404 InvokeRuntimeCallingConvention calling_convention;
405 __ LoadConst32(calling_convention.GetRegisterAt(0),
406 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100407 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100408 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 }
410
Roland Levillain46648892015-06-19 16:07:18 +0100411 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
412
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
415};
416
Alexey Frunze15958152017-02-09 19:08:30 -0800417class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
418 public:
419 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
420
421 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
422 LocationSummary* locations = instruction_->GetLocations();
423 __ Bind(GetEntryLabel());
424 SaveLiveRegisters(codegen, locations);
425
426 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800428 parallel_move.AddMove(
429 locations->InAt(0),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100431 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800432 nullptr);
433 parallel_move.AddMove(
434 locations->InAt(1),
435 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100436 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800437 nullptr);
438 parallel_move.AddMove(
439 locations->InAt(2),
440 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100441 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800442 nullptr);
443 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
444
445 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
446 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
447 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
448 RestoreLiveRegisters(codegen, locations);
449 __ Bc(GetExitLabel());
450 }
451
452 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
453
454 private:
455 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
456};
457
458// Slow path marking an object reference `ref` during a read
459// barrier. The field `obj.field` in the object `obj` holding this
460// reference does not get updated by this slow path after marking (see
461// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
462//
463// This means that after the execution of this slow path, `ref` will
464// always be up-to-date, but `obj.field` may not; i.e., after the
465// flip, `ref` will be a to-space reference, but `obj.field` will
466// probably still be a from-space reference (unless it gets updated by
467// another thread, or if another thread installed another object
468// reference (different from `ref`) in `obj.field`).
469//
470// If `entrypoint` is a valid location it is assumed to already be
471// holding the entrypoint. The case where the entrypoint is passed in
472// is for the GcRoot read barrier.
473class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
474 public:
475 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
476 Location ref,
477 Location entrypoint = Location::NoLocation())
478 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
479 DCHECK(kEmitCompilerReadBarrier);
480 }
481
482 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
483
484 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
485 LocationSummary* locations = instruction_->GetLocations();
486 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
487 DCHECK(locations->CanCall());
488 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
489 DCHECK(instruction_->IsInstanceFieldGet() ||
490 instruction_->IsStaticFieldGet() ||
491 instruction_->IsArrayGet() ||
492 instruction_->IsArraySet() ||
493 instruction_->IsLoadClass() ||
494 instruction_->IsLoadString() ||
495 instruction_->IsInstanceOf() ||
496 instruction_->IsCheckCast() ||
497 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
498 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
499 << "Unexpected instruction in read barrier marking slow path: "
500 << instruction_->DebugName();
501
502 __ Bind(GetEntryLabel());
503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
506 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
507 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
508 (S2 <= ref_reg && ref_reg <= S7) ||
509 (ref_reg == S8)) << ref_reg;
510 // "Compact" slow path, saving two moves.
511 //
512 // Instead of using the standard runtime calling convention (input
513 // and output in A0 and V0 respectively):
514 //
515 // A0 <- ref
516 // V0 <- ReadBarrierMark(A0)
517 // ref <- V0
518 //
519 // we just use rX (the register containing `ref`) as input and output
520 // of a dedicated entrypoint:
521 //
522 // rX <- ReadBarrierMarkRegX(rX)
523 //
524 if (entrypoint_.IsValid()) {
525 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
526 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
527 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
528 __ Nop();
529 } else {
530 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100531 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800532 // This runtime call does not require a stack map.
533 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
534 instruction_,
535 this);
536 }
537 __ Bc(GetExitLabel());
538 }
539
540 private:
541 // The location (register) of the marked object reference.
542 const Location ref_;
543
544 // The location of the entrypoint if already loaded.
545 const Location entrypoint_;
546
547 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
548};
549
550// Slow path marking an object reference `ref` during a read barrier,
551// and if needed, atomically updating the field `obj.field` in the
552// object `obj` holding this reference after marking (contrary to
553// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
554// `obj.field`).
555//
556// This means that after the execution of this slow path, both `ref`
557// and `obj.field` will be up-to-date; i.e., after the flip, both will
558// hold the same to-space reference (unless another thread installed
559// another object reference (different from `ref`) in `obj.field`).
560class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
561 public:
562 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
563 Location ref,
564 GpuRegister obj,
565 Location field_offset,
566 GpuRegister temp1)
567 : SlowPathCodeMIPS64(instruction),
568 ref_(ref),
569 obj_(obj),
570 field_offset_(field_offset),
571 temp1_(temp1) {
572 DCHECK(kEmitCompilerReadBarrier);
573 }
574
575 const char* GetDescription() const OVERRIDE {
576 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
577 }
578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
582 DCHECK(locations->CanCall());
583 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
584 // This slow path is only used by the UnsafeCASObject intrinsic.
585 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
586 << "Unexpected instruction in read barrier marking and field updating slow path: "
587 << instruction_->DebugName();
588 DCHECK(instruction_->GetLocations()->Intrinsified());
589 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
590 DCHECK(field_offset_.IsRegister()) << field_offset_;
591
592 __ Bind(GetEntryLabel());
593
594 // Save the old reference.
595 // Note that we cannot use AT or TMP to save the old reference, as those
596 // are used by the code that follows, but we need the old reference after
597 // the call to the ReadBarrierMarkRegX entry point.
598 DCHECK_NE(temp1_, AT);
599 DCHECK_NE(temp1_, TMP);
600 __ Move(temp1_, ref_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
606 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
607 (S2 <= ref_reg && ref_reg <= S7) ||
608 (ref_reg == S8)) << ref_reg;
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in A0 and V0 respectively):
613 //
614 // A0 <- ref
615 // V0 <- ReadBarrierMark(A0)
616 // ref <- V0
617 //
618 // we just use rX (the register containing `ref`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
623 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100624 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800625 // This runtime call does not require a stack map.
626 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
627 instruction_,
628 this);
629
630 // If the new reference is different from the old reference,
631 // update the field in the holder (`*(obj_ + field_offset_)`).
632 //
633 // Note that this field could also hold a different object, if
634 // another thread had concurrently changed it. In that case, the
635 // the compare-and-set (CAS) loop below would abort, leaving the
636 // field as-is.
637 Mips64Label done;
638 __ Beqc(temp1_, ref_reg, &done);
639
640 // Update the the holder's field atomically. This may fail if
641 // mutator updates before us, but it's OK. This is achieved
642 // using a strong compare-and-set (CAS) operation with relaxed
643 // memory synchronization ordering, where the expected value is
644 // the old reference and the desired value is the new reference.
645
646 // Convenience aliases.
647 GpuRegister base = obj_;
648 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
649 GpuRegister expected = temp1_;
650 GpuRegister value = ref_reg;
651 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
652 GpuRegister tmp = AT; // Value in memory.
653
654 __ Daddu(tmp_ptr, base, offset);
655
656 if (kPoisonHeapReferences) {
657 __ PoisonHeapReference(expected);
658 // Do not poison `value` if it is the same register as
659 // `expected`, which has just been poisoned.
660 if (value != expected) {
661 __ PoisonHeapReference(value);
662 }
663 }
664
665 // do {
666 // tmp = [r_ptr] - expected;
667 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
668
669 Mips64Label loop_head, exit_loop;
670 __ Bind(&loop_head);
671 __ Ll(tmp, tmp_ptr);
672 // The LL instruction sign-extends the 32-bit value, but
673 // 32-bit references must be zero-extended. Zero-extend `tmp`.
674 __ Dext(tmp, tmp, 0, 32);
675 __ Bnec(tmp, expected, &exit_loop);
676 __ Move(tmp, value);
677 __ Sc(tmp, tmp_ptr);
678 __ Beqzc(tmp, &loop_head);
679 __ Bind(&exit_loop);
680
681 if (kPoisonHeapReferences) {
682 __ UnpoisonHeapReference(expected);
683 // Do not unpoison `value` if it is the same register as
684 // `expected`, which has just been unpoisoned.
685 if (value != expected) {
686 __ UnpoisonHeapReference(value);
687 }
688 }
689
690 __ Bind(&done);
691 __ Bc(GetExitLabel());
692 }
693
694 private:
695 // The location (register) of the marked object reference.
696 const Location ref_;
697 // The register containing the object holding the marked object reference field.
698 const GpuRegister obj_;
699 // The location of the offset of the marked reference field within `obj_`.
700 Location field_offset_;
701
702 const GpuRegister temp1_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
705};
706
707// Slow path generating a read barrier for a heap reference.
708class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
709 public:
710 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
711 Location out,
712 Location ref,
713 Location obj,
714 uint32_t offset,
715 Location index)
716 : SlowPathCodeMIPS64(instruction),
717 out_(out),
718 ref_(ref),
719 obj_(obj),
720 offset_(offset),
721 index_(index) {
722 DCHECK(kEmitCompilerReadBarrier);
723 // If `obj` is equal to `out` or `ref`, it means the initial object
724 // has been overwritten by (or after) the heap object reference load
725 // to be instrumented, e.g.:
726 //
727 // __ LoadFromOffset(kLoadWord, out, out, offset);
728 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
729 //
730 // In that case, we have lost the information about the original
731 // object, and the emitted read barrier cannot work properly.
732 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
733 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
734 }
735
736 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
737 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
738 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800740 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
743 DCHECK(instruction_->IsInstanceFieldGet() ||
744 instruction_->IsStaticFieldGet() ||
745 instruction_->IsArrayGet() ||
746 instruction_->IsInstanceOf() ||
747 instruction_->IsCheckCast() ||
748 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
749 << "Unexpected instruction in read barrier for heap reference slow path: "
750 << instruction_->DebugName();
751
752 __ Bind(GetEntryLabel());
753 SaveLiveRegisters(codegen, locations);
754
755 // We may have to change the index's value, but as `index_` is a
756 // constant member (like other "inputs" of this slow path),
757 // introduce a copy of it, `index`.
758 Location index = index_;
759 if (index_.IsValid()) {
760 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
761 if (instruction_->IsArrayGet()) {
762 // Compute the actual memory offset and store it in `index`.
763 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
764 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
765 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
766 // We are about to change the value of `index_reg` (see the
767 // calls to art::mips64::Mips64Assembler::Sll and
768 // art::mips64::MipsAssembler::Addiu32 below), but it has
769 // not been saved by the previous call to
770 // art::SlowPathCode::SaveLiveRegisters, as it is a
771 // callee-save register --
772 // art::SlowPathCode::SaveLiveRegisters does not consider
773 // callee-save registers, as it has been designed with the
774 // assumption that callee-save registers are supposed to be
775 // handled by the called function. So, as a callee-save
776 // register, `index_reg` _would_ eventually be saved onto
777 // the stack, but it would be too late: we would have
778 // changed its value earlier. Therefore, we manually save
779 // it here into another freely available register,
780 // `free_reg`, chosen of course among the caller-save
781 // registers (as a callee-save `free_reg` register would
782 // exhibit the same problem).
783 //
784 // Note we could have requested a temporary register from
785 // the register allocator instead; but we prefer not to, as
786 // this is a slow path, and we know we can find a
787 // caller-save register that is available.
788 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
789 __ Move(free_reg, index_reg);
790 index_reg = free_reg;
791 index = Location::RegisterLocation(index_reg);
792 } else {
793 // The initial register stored in `index_` has already been
794 // saved in the call to art::SlowPathCode::SaveLiveRegisters
795 // (as it is not a callee-save register), so we can freely
796 // use it.
797 }
798 // Shifting the index value contained in `index_reg` by the scale
799 // factor (2) cannot overflow in practice, as the runtime is
800 // unable to allocate object arrays with a size larger than
801 // 2^26 - 1 (that is, 2^28 - 4 bytes).
802 __ Sll(index_reg, index_reg, TIMES_4);
803 static_assert(
804 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
805 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
806 __ Addiu32(index_reg, index_reg, offset_);
807 } else {
808 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
809 // intrinsics, `index_` is not shifted by a scale factor of 2
810 // (as in the case of ArrayGet), as it is actually an offset
811 // to an object field within an object.
812 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
813 DCHECK(instruction_->GetLocations()->Intrinsified());
814 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
815 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
816 << instruction_->AsInvoke()->GetIntrinsic();
817 DCHECK_EQ(offset_, 0U);
818 DCHECK(index_.IsRegister());
819 }
820 }
821
822 // We're moving two or three locations to locations that could
823 // overlap, so we need a parallel move resolver.
824 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800826 parallel_move.AddMove(ref_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800829 nullptr);
830 parallel_move.AddMove(obj_,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800833 nullptr);
834 if (index.IsValid()) {
835 parallel_move.AddMove(index,
836 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100837 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800838 nullptr);
839 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
840 } else {
841 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
842 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
843 }
844 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
845 instruction_,
846 instruction_->GetDexPc(),
847 this);
848 CheckEntrypointTypes<
849 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
850 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
851
852 RestoreLiveRegisters(codegen, locations);
853 __ Bc(GetExitLabel());
854 }
855
856 const char* GetDescription() const OVERRIDE {
857 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
858 }
859
860 private:
861 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
862 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
863 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
864 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
865 if (i != ref &&
866 i != obj &&
867 !codegen->IsCoreCalleeSaveRegister(i) &&
868 !codegen->IsBlockedCoreRegister(i)) {
869 return static_cast<GpuRegister>(i);
870 }
871 }
872 // We shall never fail to find a free caller-save register, as
873 // there are more than two core caller-save registers on MIPS64
874 // (meaning it is possible to find one which is different from
875 // `ref` and `obj`).
876 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
877 LOG(FATAL) << "Could not find a free caller-save register";
878 UNREACHABLE();
879 }
880
881 const Location out_;
882 const Location ref_;
883 const Location obj_;
884 const uint32_t offset_;
885 // An additional location containing an index to an array.
886 // Only used for HArrayGet and the UnsafeGetObject &
887 // UnsafeGetObjectVolatile intrinsics.
888 const Location index_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
891};
892
893// Slow path generating a read barrier for a GC root.
894class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
895 public:
896 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
897 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
898 DCHECK(kEmitCompilerReadBarrier);
899 }
900
901 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
902 LocationSummary* locations = instruction_->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100903 DataType::Type type = DataType::Type::kReference;
Alexey Frunze15958152017-02-09 19:08:30 -0800904 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
905 DCHECK(locations->CanCall());
906 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
907 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
908 << "Unexpected instruction in read barrier for GC root slow path: "
909 << instruction_->DebugName();
910
911 __ Bind(GetEntryLabel());
912 SaveLiveRegisters(codegen, locations);
913
914 InvokeRuntimeCallingConvention calling_convention;
915 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
916 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
917 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100918 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800919 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
920 instruction_,
921 instruction_->GetDexPc(),
922 this);
923 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
924 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
925
926 RestoreLiveRegisters(codegen, locations);
927 __ Bc(GetExitLabel());
928 }
929
930 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
931
932 private:
933 const Location out_;
934 const Location root_;
935
936 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
937};
938
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
940 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100941 const CompilerOptions& compiler_options,
942 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943 : CodeGenerator(graph,
944 kNumberOfGpuRegisters,
945 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000946 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
948 arraysize(kCoreCalleeSaves)),
949 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
950 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100951 compiler_options,
952 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100953 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 location_builder_(graph, this),
955 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 move_resolver_(graph->GetAllocator(), this),
957 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800959 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100960 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800961 uint64_literals_(std::less<uint64_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
964 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
965 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
966 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
967 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
968 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800969 jit_string_patches_(StringReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100970 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800971 jit_class_patches_(TypeReferenceValueComparator(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100972 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973 // Save RA (containing the return address) to mimic Quick.
974 AddAllocatedRegister(Location::RegisterLocation(RA));
975}
976
977#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100978// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
979#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700980#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981
982void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700983 // Ensure that we fix up branches.
984 __ FinalizeCode();
985
986 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +0100987 StackMapStream* stack_map_stream = GetStackMapStream();
988 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800989 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +0000990 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 uint32_t new_position = __ GetAdjustedPosition(old_position);
992 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100993 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700994 }
995
996 // Adjust pc offsets for the disassembly information.
997 if (disasm_info_ != nullptr) {
998 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
999 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1000 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1001 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1002 it.second.start = __ GetAdjustedPosition(it.second.start);
1003 it.second.end = __ GetAdjustedPosition(it.second.end);
1004 }
1005 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1006 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1007 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1008 }
1009 }
1010
Alexey Frunze4dda3372015-06-01 18:31:49 -07001011 CodeGenerator::Finalize(allocator);
1012}
1013
1014Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1015 return codegen_->GetAssembler();
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001024 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001025 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1026}
1027
1028void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1029 // Pop reg
1030 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032}
1033
1034void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1035 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001036 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037 __ Sd(GpuRegister(reg), SP, 0);
1038}
1039
1040void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1041 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1042 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1043 // Allocate a scratch register other than TMP, if available.
1044 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1045 // automatically unspilled when the scratch scope object is destroyed).
1046 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1047 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001048 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001049 __ LoadFromOffset(load_type,
1050 GpuRegister(ensure_scratch.GetRegister()),
1051 SP,
1052 index1 + stack_offset);
1053 __ LoadFromOffset(load_type,
1054 TMP,
1055 SP,
1056 index2 + stack_offset);
1057 __ StoreToOffset(store_type,
1058 GpuRegister(ensure_scratch.GetRegister()),
1059 SP,
1060 index2 + stack_offset);
1061 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1062}
1063
1064static dwarf::Reg DWARFReg(GpuRegister reg) {
1065 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1066}
1067
David Srbeckyba702002016-02-01 18:15:29 +00001068static dwarf::Reg DWARFReg(FpuRegister reg) {
1069 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1070}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001071
1072void CodeGeneratorMIPS64::GenerateFrameEntry() {
1073 __ Bind(&frame_entry_label_);
1074
Vladimir Marko33bff252017-11-01 14:35:42 +00001075 bool do_overflow_check =
1076 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips64) || !IsLeafMethod();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001077
1078 if (do_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001079 __ LoadFromOffset(
1080 kLoadWord,
1081 ZERO,
1082 SP,
1083 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips64)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001084 RecordPcInfo(nullptr, 0);
1085 }
1086
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087 if (HasEmptyFrame()) {
1088 return;
1089 }
1090
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001091 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001092 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips64)) {
1093 LOG(FATAL) << "Stack frame larger than "
1094 << GetStackOverflowReservedBytes(InstructionSet::kMips64) << " bytes";
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001095 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001096
1097 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001099 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001100 __ IncreaseFrameSize(ofs);
1101
1102 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1103 GpuRegister reg = kCoreCalleeSaves[i];
1104 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001105 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001107 __ cfi().RelOffset(DWARFReg(reg), ofs);
1108 }
1109 }
1110
1111 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1112 FpuRegister reg = kFpuCalleeSaves[i];
1113 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001114 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001115 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001116 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001117 }
1118 }
1119
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001120 // Save the current method if we need it. Note that we do not
1121 // do this in HCurrentMethod, as the instruction might have been removed
1122 // in the SSA graph.
1123 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001124 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001125 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001126
1127 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1128 // Initialize should_deoptimize flag to 0.
1129 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131}
1132
1133void CodeGeneratorMIPS64::GenerateFrameExit() {
1134 __ cfi().RememberState();
1135
Alexey Frunze4dda3372015-06-01 18:31:49 -07001136 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001139 // For better instruction scheduling restore RA before other registers.
1140 uint32_t ofs = GetFrameSize();
1141 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142 GpuRegister reg = kCoreCalleeSaves[i];
1143 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001144 ofs -= kMips64DoublewordSize;
1145 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001146 __ cfi().Restore(DWARFReg(reg));
1147 }
1148 }
1149
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001150 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1151 FpuRegister reg = kFpuCalleeSaves[i];
1152 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1153 ofs -= kMips64DoublewordSize;
1154 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1155 __ cfi().Restore(DWARFReg(reg));
1156 }
1157 }
1158
1159 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160 }
1161
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001162 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001163
1164 __ cfi().RestoreState();
1165 __ cfi().DefCFAOffset(GetFrameSize());
1166}
1167
1168void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1169 __ Bind(GetLabelOf(block));
1170}
1171
1172void CodeGeneratorMIPS64::MoveLocation(Location destination,
1173 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001174 DataType::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 if (source.Equals(destination)) {
1176 return;
1177 }
1178
1179 // A valid move can always be inferred from the destination and source
1180 // locations. When moving from and to a register, the argument type can be
1181 // used to generate 32bit instead of 64bit moves.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001182 bool unspecified_type = (dst_type == DataType::Type::kVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001183 DCHECK_EQ(unspecified_type, false);
1184
1185 if (destination.IsRegister() || destination.IsFpuRegister()) {
1186 if (unspecified_type) {
1187 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1188 if (source.IsStackSlot() ||
1189 (src_cst != nullptr && (src_cst->IsIntConstant()
1190 || src_cst->IsFloatConstant()
1191 || src_cst->IsNullConstant()))) {
1192 // For stack slots and 32bit constants, a 64bit type is appropriate.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 dst_type = destination.IsRegister() ? DataType::Type::kInt32 : DataType::Type::kFloat32;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 } else {
1195 // If the source is a double stack slot or a 64bit constant, a 64bit
1196 // type is appropriate. Else the source is a register, and since the
1197 // type has not been specified, we chose a 64bit type to force a 64bit
1198 // move.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001199 dst_type = destination.IsRegister() ? DataType::Type::kInt64 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001200 }
1201 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 DCHECK((destination.IsFpuRegister() && DataType::IsFloatingPointType(dst_type)) ||
1203 (destination.IsRegister() && !DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1205 // Move to GPR/FPR from stack
1206 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001207 if (DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 __ LoadFpuFromOffset(load_type,
1209 destination.AsFpuRegister<FpuRegister>(),
1210 SP,
1211 source.GetStackIndex());
1212 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 __ LoadFromOffset(load_type,
1215 destination.AsRegister<GpuRegister>(),
1216 SP,
1217 source.GetStackIndex());
1218 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001219 } else if (source.IsSIMDStackSlot()) {
1220 __ LoadFpuFromOffset(kLoadQuadword,
1221 destination.AsFpuRegister<FpuRegister>(),
1222 SP,
1223 source.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 } else if (source.IsConstant()) {
1225 // Move to GPR/FPR from constant
1226 GpuRegister gpr = AT;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 if (!DataType::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 gpr = destination.AsRegister<GpuRegister>();
1229 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 if (dst_type == DataType::Type::kInt32 || dst_type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001231 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001233 gpr = ZERO;
1234 } else {
1235 __ LoadConst32(gpr, value);
1236 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001238 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 if (DataType::IsFloatingPointType(dst_type) && value == 0) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001240 gpr = ZERO;
1241 } else {
1242 __ LoadConst64(gpr, value);
1243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 if (dst_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001246 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 } else if (dst_type == DataType::Type::kFloat64) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1249 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 if (destination.IsRegister()) {
1252 // Move to GPR from GPR
1253 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1254 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK(destination.IsFpuRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001256 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1258 } else {
1259 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1260 }
1261 }
1262 } else if (source.IsFpuRegister()) {
1263 if (destination.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001264 if (GetGraph()->HasSIMD()) {
1265 __ MoveV(VectorRegisterFrom(destination),
1266 VectorRegisterFrom(source));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001267 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001268 // Move to FPR from FPR
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 if (dst_type == DataType::Type::kFloat32) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001270 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1271 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DCHECK_EQ(dst_type, DataType::Type::kFloat64);
Lena Djokicca8c2952017-05-29 11:31:46 +02001273 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 } else {
1277 DCHECK(destination.IsRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001278 if (DataType::Is64BitType(dst_type)) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001279 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1280 } else {
1281 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1282 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283 }
1284 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001285 } else if (destination.IsSIMDStackSlot()) {
1286 if (source.IsFpuRegister()) {
1287 __ StoreFpuToOffset(kStoreQuadword,
1288 source.AsFpuRegister<FpuRegister>(),
1289 SP,
1290 destination.GetStackIndex());
1291 } else {
1292 DCHECK(source.IsSIMDStackSlot());
1293 __ LoadFpuFromOffset(kLoadQuadword,
1294 FTMP,
1295 SP,
1296 source.GetStackIndex());
1297 __ StoreFpuToOffset(kStoreQuadword,
1298 FTMP,
1299 SP,
1300 destination.GetStackIndex());
1301 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001302 } else { // The destination is not a register. It must be a stack slot.
1303 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1304 if (source.IsRegister() || source.IsFpuRegister()) {
1305 if (unspecified_type) {
1306 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001307 dst_type = destination.IsStackSlot() ? DataType::Type::kInt32 : DataType::Type::kInt64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001309 dst_type =
1310 destination.IsStackSlot() ? DataType::Type::kFloat32 : DataType::Type::kFloat64;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 }
1312 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001313 DCHECK((destination.IsDoubleStackSlot() == DataType::Is64BitType(dst_type)) &&
1314 (source.IsFpuRegister() == DataType::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001315 // Move to stack from GPR/FPR
1316 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1317 if (source.IsRegister()) {
1318 __ StoreToOffset(store_type,
1319 source.AsRegister<GpuRegister>(),
1320 SP,
1321 destination.GetStackIndex());
1322 } else {
1323 __ StoreFpuToOffset(store_type,
1324 source.AsFpuRegister<FpuRegister>(),
1325 SP,
1326 destination.GetStackIndex());
1327 }
1328 } else if (source.IsConstant()) {
1329 // Move to stack from constant
1330 HConstant* src_cst = source.GetConstant();
1331 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001332 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001333 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001334 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1335 if (value != 0) {
1336 gpr = TMP;
1337 __ LoadConst32(gpr, value);
1338 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001339 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001340 DCHECK(destination.IsDoubleStackSlot());
1341 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1342 if (value != 0) {
1343 gpr = TMP;
1344 __ LoadConst64(gpr, value);
1345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001347 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 } else {
1349 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1350 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1351 // Move to stack from stack
1352 if (destination.IsStackSlot()) {
1353 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1354 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1355 } else {
1356 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1357 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1358 }
1359 }
1360 }
1361}
1362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001363void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, DataType::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 DCHECK(!loc1.IsConstant());
1365 DCHECK(!loc2.IsConstant());
1366
1367 if (loc1.Equals(loc2)) {
1368 return;
1369 }
1370
1371 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1372 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1373 bool is_fp_reg1 = loc1.IsFpuRegister();
1374 bool is_fp_reg2 = loc2.IsFpuRegister();
1375
1376 if (loc2.IsRegister() && loc1.IsRegister()) {
1377 // Swap 2 GPRs
1378 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1379 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1380 __ Move(TMP, r2);
1381 __ Move(r2, r1);
1382 __ Move(r1, TMP);
1383 } else if (is_fp_reg2 && is_fp_reg1) {
1384 // Swap 2 FPRs
1385 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1386 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001387 if (type == DataType::Type::kFloat32) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001388 __ MovS(FTMP, r1);
1389 __ MovS(r1, r2);
1390 __ MovS(r2, FTMP);
1391 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001392 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001393 __ MovD(FTMP, r1);
1394 __ MovD(r1, r2);
1395 __ MovD(r2, FTMP);
1396 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001397 } else if (is_slot1 != is_slot2) {
1398 // Swap GPR/FPR and stack slot
1399 Location reg_loc = is_slot1 ? loc2 : loc1;
1400 Location mem_loc = is_slot1 ? loc1 : loc2;
1401 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1402 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 // TODO: use load_type = kLoadUnsignedWord when type == DataType::Type::kReference.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001404 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1405 if (reg_loc.IsFpuRegister()) {
1406 __ StoreFpuToOffset(store_type,
1407 reg_loc.AsFpuRegister<FpuRegister>(),
1408 SP,
1409 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 if (mem_loc.IsStackSlot()) {
1411 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1412 } else {
1413 DCHECK(mem_loc.IsDoubleStackSlot());
1414 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1415 }
1416 } else {
1417 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1418 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1419 }
1420 } else if (is_slot1 && is_slot2) {
1421 move_resolver_.Exchange(loc1.GetStackIndex(),
1422 loc2.GetStackIndex(),
1423 loc1.IsDoubleStackSlot());
1424 } else {
1425 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1426 }
1427}
1428
Calin Juravle175dc732015-08-25 15:42:32 +01001429void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1430 DCHECK(location.IsRegister());
1431 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1432}
1433
Calin Juravlee460d1d2015-09-29 04:52:17 +01001434void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1435 if (location.IsRegister()) {
1436 locations->AddTemp(location);
1437 } else {
1438 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1439 }
1440}
1441
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001442void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1443 GpuRegister value,
1444 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001445 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001446 GpuRegister card = AT;
1447 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001448 if (value_can_be_null) {
1449 __ Beqzc(value, &done);
1450 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001451 __ LoadFromOffset(kLoadDoubleword,
1452 card,
1453 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001454 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001455 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1456 __ Daddu(temp, card, temp);
1457 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001458 if (value_can_be_null) {
1459 __ Bind(&done);
1460 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001461}
1462
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001463template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Alexey Frunze19f6c692016-11-30 19:19:55 -08001464inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1465 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001466 ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001467 for (const PcRelativePatchInfo& info : infos) {
1468 const DexFile& dex_file = info.target_dex_file;
1469 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001470 DCHECK(info.label.IsBound());
1471 uint32_t literal_offset = __ GetLabelLocation(&info.label);
1472 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1473 uint32_t pc_rel_offset = __ GetLabelLocation(&info_high.label);
1474 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Alexey Frunze19f6c692016-11-30 19:19:55 -08001475 }
1476}
1477
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001478void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08001479 DCHECK(linker_patches->empty());
1480 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001481 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001482 method_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001483 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001484 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001485 pc_relative_string_patches_.size() +
1486 string_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001487 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001488 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001489 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1490 pc_relative_method_patches_, linker_patches);
1491 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1492 pc_relative_type_patches_, linker_patches);
1493 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1494 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001495 } else {
1496 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001497 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1498 pc_relative_type_patches_, linker_patches);
1499 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1500 pc_relative_string_patches_, linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001501 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001502 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1503 method_bss_entry_patches_, linker_patches);
1504 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1505 type_bss_entry_patches_, linker_patches);
1506 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1507 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001508 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001509}
1510
Vladimir Marko65979462017-05-19 17:25:12 +01001511CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001512 MethodReference target_method,
1513 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001514 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001515 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001516 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001517 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001518}
1519
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001520CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001521 MethodReference target_method,
1522 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001523 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001524 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001525 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001526 &method_bss_entry_patches_);
1527}
1528
Alexey Frunzef63f5692016-12-13 17:43:11 -08001529CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001530 const DexFile& dex_file,
1531 dex::TypeIndex type_index,
1532 const PcRelativePatchInfo* info_high) {
1533 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001534}
1535
Vladimir Marko1998cd02017-01-13 13:02:58 +00001536CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001537 const DexFile& dex_file,
1538 dex::TypeIndex type_index,
1539 const PcRelativePatchInfo* info_high) {
1540 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001541}
1542
Vladimir Marko65979462017-05-19 17:25:12 +01001543CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001544 const DexFile& dex_file,
1545 dex::StringIndex string_index,
1546 const PcRelativePatchInfo* info_high) {
1547 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001548}
1549
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001550CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewStringBssEntryPatch(
1551 const DexFile& dex_file,
1552 dex::StringIndex string_index,
1553 const PcRelativePatchInfo* info_high) {
1554 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1555}
1556
Alexey Frunze19f6c692016-11-30 19:19:55 -08001557CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001558 const DexFile& dex_file,
1559 uint32_t offset_or_index,
1560 const PcRelativePatchInfo* info_high,
1561 ArenaDeque<PcRelativePatchInfo>* patches) {
1562 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001563 return &patches->back();
1564}
1565
Alexey Frunzef63f5692016-12-13 17:43:11 -08001566Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1567 return map->GetOrCreate(
1568 value,
1569 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1570}
1571
Alexey Frunze19f6c692016-11-30 19:19:55 -08001572Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1573 return uint64_literals_.GetOrCreate(
1574 value,
1575 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1576}
1577
Alexey Frunzef63f5692016-12-13 17:43:11 -08001578Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001579 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001580}
1581
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
1583 GpuRegister out,
1584 PcRelativePatchInfo* info_low) {
1585 DCHECK(!info_high->patch_info_high);
1586 __ Bind(&info_high->label);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001587 // Add the high half of a 32-bit offset to PC.
1588 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001589 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001590 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001591 if (info_low != nullptr) {
1592 DCHECK_EQ(info_low->patch_info_high, info_high);
1593 __ Bind(&info_low->label);
1594 }
Alexey Frunze19f6c692016-11-30 19:19:55 -08001595}
1596
Alexey Frunze627c1a02017-01-30 19:28:14 -08001597Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1598 dex::StringIndex string_index,
1599 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001600 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001601 return jit_string_patches_.GetOrCreate(
1602 StringReference(&dex_file, string_index),
1603 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1604}
1605
1606Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1607 dex::TypeIndex type_index,
1608 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001609 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001610 return jit_class_patches_.GetOrCreate(
1611 TypeReference(&dex_file, type_index),
1612 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1613}
1614
1615void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1616 const uint8_t* roots_data,
1617 const Literal* literal,
1618 uint64_t index_in_table) const {
1619 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1620 uintptr_t address =
1621 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1622 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1623}
1624
1625void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1626 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001627 const StringReference& string_reference = entry.first;
1628 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001629 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001630 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001631 }
1632 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001633 const TypeReference& type_reference = entry.first;
1634 Literal* table_entry_literal = entry.second;
Vladimir Marko174b2e22017-10-12 13:34:49 +01001635 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001636 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001637 }
1638}
1639
David Brazdil58282f42016-01-14 12:45:10 +00001640void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1642 blocked_core_registers_[ZERO] = true;
1643 blocked_core_registers_[K0] = true;
1644 blocked_core_registers_[K1] = true;
1645 blocked_core_registers_[GP] = true;
1646 blocked_core_registers_[SP] = true;
1647 blocked_core_registers_[RA] = true;
1648
Lazar Trsicd9672662015-09-03 17:33:01 +02001649 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1650 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001651 blocked_core_registers_[AT] = true;
1652 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001653 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001654 blocked_fpu_registers_[FTMP] = true;
1655
1656 // Reserve suspend and thread registers.
1657 blocked_core_registers_[S0] = true;
1658 blocked_core_registers_[TR] = true;
1659
1660 // Reserve T9 for function calls
1661 blocked_core_registers_[T9] = true;
1662
Goran Jakovljevic782be112016-06-21 12:39:04 +02001663 if (GetGraph()->IsDebuggable()) {
1664 // Stubs do not save callee-save floating point registers. If the graph
1665 // is debuggable, we need to deal with these registers differently. For
1666 // now, just block them.
1667 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1668 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1669 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001670 }
1671}
1672
Alexey Frunze4dda3372015-06-01 18:31:49 -07001673size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1674 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001675 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676}
1677
1678size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1679 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001680 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681}
1682
1683size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001684 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1685 FpuRegister(reg_id),
1686 SP,
1687 stack_index);
1688 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001689}
1690
1691size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001692 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1693 FpuRegister(reg_id),
1694 SP,
1695 stack_index);
1696 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697}
1698
1699void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001700 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701}
1702
1703void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001704 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705}
1706
Calin Juravle175dc732015-08-25 15:42:32 +01001707void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708 HInstruction* instruction,
1709 uint32_t dex_pc,
1710 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001711 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001712 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001713 if (EntrypointRequiresStackMap(entrypoint)) {
1714 RecordPcInfo(instruction, dex_pc, slow_path);
1715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716}
1717
Alexey Frunze15958152017-02-09 19:08:30 -08001718void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1719 HInstruction* instruction,
1720 SlowPathCode* slow_path) {
1721 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1722 GenerateInvokeRuntime(entry_point_offset);
1723}
1724
1725void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1726 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1727 __ Jalr(T9);
1728 __ Nop();
1729}
1730
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1732 GpuRegister class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001733 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001734 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1735 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001736 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1737 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 __ Bind(slow_path->GetExitLabel());
1739}
1740
1741void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1742 __ Sync(0); // only stype 0 is supported
1743}
1744
1745void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1746 HBasicBlock* successor) {
1747 SuspendCheckSlowPathMIPS64* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001748 down_cast<SuspendCheckSlowPathMIPS64*>(instruction->GetSlowPath());
1749
1750 if (slow_path == nullptr) {
1751 slow_path =
1752 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS64(instruction, successor);
1753 instruction->SetSlowPath(slow_path);
1754 codegen_->AddSlowPath(slow_path);
1755 if (successor != nullptr) {
1756 DCHECK(successor->IsLoopHeader());
1757 }
1758 } else {
1759 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1760 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001761
1762 __ LoadFromOffset(kLoadUnsignedHalfword,
1763 TMP,
1764 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001765 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766 if (successor == nullptr) {
1767 __ Bnezc(TMP, slow_path->GetEntryLabel());
1768 __ Bind(slow_path->GetReturnLabel());
1769 } else {
1770 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001771 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 // slow_path will return to GetLabelOf(successor).
1773 }
1774}
1775
1776InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1777 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001778 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 assembler_(codegen->GetAssembler()),
1780 codegen_(codegen) {}
1781
1782void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1783 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001784 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 DataType::Type type = instruction->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001786 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 case DataType::Type::kInt32:
1788 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789 locations->SetInAt(0, Location::RequiresRegister());
1790 HInstruction* right = instruction->InputAt(1);
1791 bool can_use_imm = false;
1792 if (right->IsConstant()) {
1793 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1794 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1795 can_use_imm = IsUint<16>(imm);
1796 } else if (instruction->IsAdd()) {
1797 can_use_imm = IsInt<16>(imm);
1798 } else {
1799 DCHECK(instruction->IsSub());
1800 can_use_imm = IsInt<16>(-imm);
1801 }
1802 }
1803 if (can_use_imm)
1804 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1805 else
1806 locations->SetInAt(1, Location::RequiresRegister());
1807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1808 }
1809 break;
1810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001811 case DataType::Type::kFloat32:
1812 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001813 locations->SetInAt(0, Location::RequiresFpuRegister());
1814 locations->SetInAt(1, Location::RequiresFpuRegister());
1815 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1816 break;
1817
1818 default:
1819 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1820 }
1821}
1822
1823void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001825 LocationSummary* locations = instruction->GetLocations();
1826
1827 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001828 case DataType::Type::kInt32:
1829 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1831 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1832 Location rhs_location = locations->InAt(1);
1833
1834 GpuRegister rhs_reg = ZERO;
1835 int64_t rhs_imm = 0;
1836 bool use_imm = rhs_location.IsConstant();
1837 if (use_imm) {
1838 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1839 } else {
1840 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1841 }
1842
1843 if (instruction->IsAnd()) {
1844 if (use_imm)
1845 __ Andi(dst, lhs, rhs_imm);
1846 else
1847 __ And(dst, lhs, rhs_reg);
1848 } else if (instruction->IsOr()) {
1849 if (use_imm)
1850 __ Ori(dst, lhs, rhs_imm);
1851 else
1852 __ Or(dst, lhs, rhs_reg);
1853 } else if (instruction->IsXor()) {
1854 if (use_imm)
1855 __ Xori(dst, lhs, rhs_imm);
1856 else
1857 __ Xor(dst, lhs, rhs_reg);
1858 } else if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001860 if (use_imm)
1861 __ Addiu(dst, lhs, rhs_imm);
1862 else
1863 __ Addu(dst, lhs, rhs_reg);
1864 } else {
1865 if (use_imm)
1866 __ Daddiu(dst, lhs, rhs_imm);
1867 else
1868 __ Daddu(dst, lhs, rhs_reg);
1869 }
1870 } else {
1871 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873 if (use_imm)
1874 __ Addiu(dst, lhs, -rhs_imm);
1875 else
1876 __ Subu(dst, lhs, rhs_reg);
1877 } else {
1878 if (use_imm)
1879 __ Daddiu(dst, lhs, -rhs_imm);
1880 else
1881 __ Dsubu(dst, lhs, rhs_reg);
1882 }
1883 }
1884 break;
1885 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001886 case DataType::Type::kFloat32:
1887 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1889 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1890 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1891 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893 __ AddS(dst, lhs, rhs);
1894 else
1895 __ AddD(dst, lhs, rhs);
1896 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001897 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898 __ SubS(dst, lhs, rhs);
1899 else
1900 __ SubD(dst, lhs, rhs);
1901 } else {
1902 LOG(FATAL) << "Unexpected floating-point binary operation";
1903 }
1904 break;
1905 }
1906 default:
1907 LOG(FATAL) << "Unexpected binary operation type " << type;
1908 }
1909}
1910
1911void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913
Vladimir Markoca6fff82017-10-03 14:49:14 +01001914 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001917 case DataType::Type::kInt32:
1918 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001919 locations->SetInAt(0, Location::RequiresRegister());
1920 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922 break;
1923 }
1924 default:
1925 LOG(FATAL) << "Unexpected shift type " << type;
1926 }
1927}
1928
1929void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001930 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001931 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001933
1934 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt32:
1936 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001937 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1938 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1939 Location rhs_location = locations->InAt(1);
1940
1941 GpuRegister rhs_reg = ZERO;
1942 int64_t rhs_imm = 0;
1943 bool use_imm = rhs_location.IsConstant();
1944 if (use_imm) {
1945 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1946 } else {
1947 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1948 }
1949
1950 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001951 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001953
Alexey Frunze92d90602015-12-18 18:16:36 -08001954 if (shift_value == 0) {
1955 if (dst != lhs) {
1956 __ Move(dst, lhs);
1957 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001958 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001959 if (instr->IsShl()) {
1960 __ Sll(dst, lhs, shift_value);
1961 } else if (instr->IsShr()) {
1962 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001963 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001964 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001965 } else {
1966 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 }
1968 } else {
1969 if (shift_value < 32) {
1970 if (instr->IsShl()) {
1971 __ Dsll(dst, lhs, shift_value);
1972 } else if (instr->IsShr()) {
1973 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001974 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001975 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001976 } else {
1977 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001978 }
1979 } else {
1980 shift_value -= 32;
1981 if (instr->IsShl()) {
1982 __ Dsll32(dst, lhs, shift_value);
1983 } else if (instr->IsShr()) {
1984 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001985 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001986 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001987 } else {
1988 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989 }
1990 }
1991 }
1992 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001994 if (instr->IsShl()) {
1995 __ Sllv(dst, lhs, rhs_reg);
1996 } else if (instr->IsShr()) {
1997 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001998 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001999 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002000 } else {
2001 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002 }
2003 } else {
2004 if (instr->IsShl()) {
2005 __ Dsllv(dst, lhs, rhs_reg);
2006 } else if (instr->IsShr()) {
2007 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002008 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002009 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002010 } else {
2011 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012 }
2013 }
2014 }
2015 break;
2016 }
2017 default:
2018 LOG(FATAL) << "Unexpected shift operation type " << type;
2019 }
2020}
2021
2022void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2023 HandleBinaryOp(instruction);
2024}
2025
2026void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2027 HandleBinaryOp(instruction);
2028}
2029
2030void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2031 HandleBinaryOp(instruction);
2032}
2033
2034void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2035 HandleBinaryOp(instruction);
2036}
2037
2038void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002040 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2044 object_array_get_with_read_barrier
2045 ? LocationSummary::kCallOnSlowPath
2046 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002047 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2048 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2049 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2054 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002055 // The output overlaps in the case of an object array get with
2056 // read barriers enabled: we do not want the move to overwrite the
2057 // array's location, as we need it to emit the read barrier.
2058 locations->SetOut(Location::RequiresRegister(),
2059 object_array_get_with_read_barrier
2060 ? Location::kOutputOverlap
2061 : Location::kNoOutputOverlap);
2062 }
2063 // We need a temporary register for the read barrier marking slow
2064 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2065 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002066 bool temp_needed = instruction->GetIndex()->IsConstant()
2067 ? !kBakerReadBarrierThunksEnableForFields
2068 : !kBakerReadBarrierThunksEnableForArrays;
2069 if (temp_needed) {
2070 locations->AddTemp(Location::RequiresRegister());
2071 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 }
2073}
2074
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002075static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2076 auto null_checker = [codegen, instruction]() {
2077 codegen->MaybeRecordImplicitNullCheck(instruction);
2078 };
2079 return null_checker;
2080}
2081
Alexey Frunze4dda3372015-06-01 18:31:49 -07002082void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2083 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002084 Location obj_loc = locations->InAt(0);
2085 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2086 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002088 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002089 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002092 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2093 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002094 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002095 case DataType::Type::kBool:
2096 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002097 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 if (index.IsConstant()) {
2099 size_t offset =
2100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002101 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 } else {
2103 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002104 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002105 }
2106 break;
2107 }
2108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002110 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 if (index.IsConstant()) {
2112 size_t offset =
2113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002114 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115 } else {
2116 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002117 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 }
2119 break;
2120 }
2121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002122 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002123 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002124 if (maybe_compressed_char_at) {
2125 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002126 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002127 __ Dext(TMP, TMP, 0, 1);
2128 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2129 "Expecting 0=compressed, 1=uncompressed");
2130 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002132 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2133 if (maybe_compressed_char_at) {
2134 Mips64Label uncompressed_load, done;
2135 __ Bnezc(TMP, &uncompressed_load);
2136 __ LoadFromOffset(kLoadUnsignedByte,
2137 out,
2138 obj,
2139 data_offset + (const_index << TIMES_1));
2140 __ Bc(&done);
2141 __ Bind(&uncompressed_load);
2142 __ LoadFromOffset(kLoadUnsignedHalfword,
2143 out,
2144 obj,
2145 data_offset + (const_index << TIMES_2));
2146 __ Bind(&done);
2147 } else {
2148 __ LoadFromOffset(kLoadUnsignedHalfword,
2149 out,
2150 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002151 data_offset + (const_index << TIMES_2),
2152 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002153 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002154 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002155 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2156 if (maybe_compressed_char_at) {
2157 Mips64Label uncompressed_load, done;
2158 __ Bnezc(TMP, &uncompressed_load);
2159 __ Daddu(TMP, obj, index_reg);
2160 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2161 __ Bc(&done);
2162 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002163 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002164 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2165 __ Bind(&done);
2166 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002167 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002168 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002169 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 }
2171 break;
2172 }
2173
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002174 case DataType::Type::kInt16: {
2175 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2176 if (index.IsConstant()) {
2177 size_t offset =
2178 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2179 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2180 } else {
2181 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2182 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2183 }
2184 break;
2185 }
2186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002188 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002189 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002190 LoadOperandType load_type =
2191 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 if (index.IsConstant()) {
2193 size_t offset =
2194 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002195 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002197 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002198 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 }
2200 break;
2201 }
2202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002204 static_assert(
2205 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2206 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2207 // /* HeapReference<Object> */ out =
2208 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2209 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002210 bool temp_needed = index.IsConstant()
2211 ? !kBakerReadBarrierThunksEnableForFields
2212 : !kBakerReadBarrierThunksEnableForArrays;
2213 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002214 // Note that a potential implicit null check is handled in this
2215 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002216 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2217 if (index.IsConstant()) {
2218 // Array load with a constant index can be treated as a field load.
2219 size_t offset =
2220 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2221 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2222 out_loc,
2223 obj,
2224 offset,
2225 temp,
2226 /* needs_null_check */ false);
2227 } else {
2228 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2229 out_loc,
2230 obj,
2231 data_offset,
2232 index,
2233 temp,
2234 /* needs_null_check */ false);
2235 }
Alexey Frunze15958152017-02-09 19:08:30 -08002236 } else {
2237 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2238 if (index.IsConstant()) {
2239 size_t offset =
2240 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2241 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2242 // If read barriers are enabled, emit read barriers other than
2243 // Baker's using a slow path (and also unpoison the loaded
2244 // reference, if heap poisoning is enabled).
2245 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2246 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002247 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002248 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2249 // If read barriers are enabled, emit read barriers other than
2250 // Baker's using a slow path (and also unpoison the loaded
2251 // reference, if heap poisoning is enabled).
2252 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2253 out_loc,
2254 out_loc,
2255 obj_loc,
2256 data_offset,
2257 index);
2258 }
2259 }
2260 break;
2261 }
2262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002264 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002265 if (index.IsConstant()) {
2266 size_t offset =
2267 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002268 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002270 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002271 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002272 }
2273 break;
2274 }
2275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002276 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002277 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002278 if (index.IsConstant()) {
2279 size_t offset =
2280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002281 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002282 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002283 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002284 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285 }
2286 break;
2287 }
2288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002290 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002291 if (index.IsConstant()) {
2292 size_t offset =
2293 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002294 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002295 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002296 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002297 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 }
2299 break;
2300 }
2301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2304 UNREACHABLE();
2305 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306}
2307
2308void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002309 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002310 locations->SetInAt(0, Location::RequiresRegister());
2311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2312}
2313
2314void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2315 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002316 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002317 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2318 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2319 __ LoadFromOffset(kLoadWord, out, obj, offset);
2320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002321 // Mask out compression flag from String's array length.
2322 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2323 __ Srl(out, out, 1u);
2324 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325}
2326
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002327Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2328 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2329 ? Location::ConstantLocation(instruction->AsConstant())
2330 : Location::RequiresRegister();
2331}
2332
2333Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2334 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2335 // We can store a non-zero float or double constant without first loading it into the FPU,
2336 // but we should only prefer this if the constant has a single use.
2337 if (instruction->IsConstant() &&
2338 (instruction->AsConstant()->IsZeroBitPattern() ||
2339 instruction->GetUses().HasExactlyOneElement())) {
2340 return Location::ConstantLocation(instruction->AsConstant());
2341 // Otherwise fall through and require an FPU register for the constant.
2342 }
2343 return Location::RequiresFpuRegister();
2344}
2345
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002348
2349 bool needs_write_barrier =
2350 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2351 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2352
Vladimir Markoca6fff82017-10-03 14:49:14 +01002353 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002354 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002355 may_need_runtime_call_for_type_check ?
2356 LocationSummary::kCallOnSlowPath :
2357 LocationSummary::kNoCall);
2358
2359 locations->SetInAt(0, Location::RequiresRegister());
2360 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002362 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002363 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002364 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2365 }
2366 if (needs_write_barrier) {
2367 // Temporary register for the write barrier.
2368 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002369 }
2370}
2371
2372void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2373 LocationSummary* locations = instruction->GetLocations();
2374 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2375 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002376 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002378 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 bool needs_write_barrier =
2380 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002381 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002382 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002383
2384 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002386 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002389 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002390 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002391 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002392 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2393 }
2394 if (value_location.IsConstant()) {
2395 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2396 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2397 } else {
2398 GpuRegister value = value_location.AsRegister<GpuRegister>();
2399 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002400 }
2401 break;
2402 }
2403
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002404 case DataType::Type::kUint16:
2405 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002407 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002408 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002409 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002410 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002411 }
2412 if (value_location.IsConstant()) {
2413 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2414 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2415 } else {
2416 GpuRegister value = value_location.AsRegister<GpuRegister>();
2417 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002418 }
2419 break;
2420 }
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002423 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2424 if (index.IsConstant()) {
2425 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2426 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002427 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002428 }
2429 if (value_location.IsConstant()) {
2430 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2431 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2432 } else {
2433 GpuRegister value = value_location.AsRegister<GpuRegister>();
2434 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2435 }
2436 break;
2437 }
2438
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002439 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002440 if (value_location.IsConstant()) {
2441 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002443 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002444 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002446 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002447 }
Alexey Frunze15958152017-02-09 19:08:30 -08002448 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2449 DCHECK_EQ(value, 0);
2450 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2451 DCHECK(!needs_write_barrier);
2452 DCHECK(!may_need_runtime_call_for_type_check);
2453 break;
2454 }
2455
2456 DCHECK(needs_write_barrier);
2457 GpuRegister value = value_location.AsRegister<GpuRegister>();
2458 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2459 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2460 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2461 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2462 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2463 Mips64Label done;
2464 SlowPathCodeMIPS64* slow_path = nullptr;
2465
2466 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002467 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002468 codegen_->AddSlowPath(slow_path);
2469 if (instruction->GetValueCanBeNull()) {
2470 Mips64Label non_zero;
2471 __ Bnezc(value, &non_zero);
2472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2473 if (index.IsConstant()) {
2474 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002475 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002476 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002477 }
Alexey Frunze15958152017-02-09 19:08:30 -08002478 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2479 __ Bc(&done);
2480 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 }
Alexey Frunze15958152017-02-09 19:08:30 -08002482
2483 // Note that when read barriers are enabled, the type checks
2484 // are performed without read barriers. This is fine, even in
2485 // the case where a class object is in the from-space after
2486 // the flip, as a comparison involving such a type would not
2487 // produce a false positive; it may of course produce a false
2488 // negative, in which case we would take the ArraySet slow
2489 // path.
2490
2491 // /* HeapReference<Class> */ temp1 = obj->klass_
2492 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2493 __ MaybeUnpoisonHeapReference(temp1);
2494
2495 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2496 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2497 // /* HeapReference<Class> */ temp2 = value->klass_
2498 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2499 // If heap poisoning is enabled, no need to unpoison `temp1`
2500 // nor `temp2`, as we are comparing two poisoned references.
2501
2502 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2503 Mips64Label do_put;
2504 __ Beqc(temp1, temp2, &do_put);
2505 // If heap poisoning is enabled, the `temp1` reference has
2506 // not been unpoisoned yet; unpoison it now.
2507 __ MaybeUnpoisonHeapReference(temp1);
2508
2509 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2510 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2511 // If heap poisoning is enabled, no need to unpoison
2512 // `temp1`, as we are comparing against null below.
2513 __ Bnezc(temp1, slow_path->GetEntryLabel());
2514 __ Bind(&do_put);
2515 } else {
2516 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2517 }
2518 }
2519
2520 GpuRegister source = value;
2521 if (kPoisonHeapReferences) {
2522 // Note that in the case where `value` is a null reference,
2523 // we do not enter this block, as a null reference does not
2524 // need poisoning.
2525 __ Move(temp1, value);
2526 __ PoisonHeapReference(temp1);
2527 source = temp1;
2528 }
2529
2530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2531 if (index.IsConstant()) {
2532 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002534 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002535 }
2536 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2537
2538 if (!may_need_runtime_call_for_type_check) {
2539 codegen_->MaybeRecordImplicitNullCheck(instruction);
2540 }
2541
2542 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2543
2544 if (done.IsLinked()) {
2545 __ Bind(&done);
2546 }
2547
2548 if (slow_path != nullptr) {
2549 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550 }
2551 break;
2552 }
2553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002555 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002557 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002559 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002560 }
2561 if (value_location.IsConstant()) {
2562 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2563 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2564 } else {
2565 GpuRegister value = value_location.AsRegister<GpuRegister>();
2566 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002567 }
2568 break;
2569 }
2570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002573 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002574 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002575 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002576 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002577 }
2578 if (value_location.IsConstant()) {
2579 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2580 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2581 } else {
2582 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2583 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584 }
2585 break;
2586 }
2587
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002589 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002590 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002591 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002592 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002593 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002594 }
2595 if (value_location.IsConstant()) {
2596 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2597 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2598 } else {
2599 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2600 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 }
2602 break;
2603 }
2604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002606 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2607 UNREACHABLE();
2608 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609}
2610
2611void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002612 RegisterSet caller_saves = RegisterSet::Empty();
2613 InvokeRuntimeCallingConvention calling_convention;
2614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2615 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2616 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002617
2618 HInstruction* index = instruction->InputAt(0);
2619 HInstruction* length = instruction->InputAt(1);
2620
2621 bool const_index = false;
2622 bool const_length = false;
2623
2624 if (index->IsConstant()) {
2625 if (length->IsConstant()) {
2626 const_index = true;
2627 const_length = true;
2628 } else {
2629 int32_t index_value = index->AsIntConstant()->GetValue();
2630 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2631 const_index = true;
2632 }
2633 }
2634 } else if (length->IsConstant()) {
2635 int32_t length_value = length->AsIntConstant()->GetValue();
2636 if (IsUint<15>(length_value)) {
2637 const_length = true;
2638 }
2639 }
2640
2641 locations->SetInAt(0, const_index
2642 ? Location::ConstantLocation(index->AsConstant())
2643 : Location::RequiresRegister());
2644 locations->SetInAt(1, const_length
2645 ? Location::ConstantLocation(length->AsConstant())
2646 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647}
2648
2649void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2650 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002651 Location index_loc = locations->InAt(0);
2652 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002653
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002654 if (length_loc.IsConstant()) {
2655 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2656 if (index_loc.IsConstant()) {
2657 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2658 if (index < 0 || index >= length) {
2659 BoundsCheckSlowPathMIPS64* slow_path =
2660 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2661 codegen_->AddSlowPath(slow_path);
2662 __ Bc(slow_path->GetEntryLabel());
2663 } else {
2664 // Nothing to be done.
2665 }
2666 return;
2667 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002669 BoundsCheckSlowPathMIPS64* slow_path =
2670 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2671 codegen_->AddSlowPath(slow_path);
2672 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2673 if (length == 0) {
2674 __ Bc(slow_path->GetEntryLabel());
2675 } else if (length == 1) {
2676 __ Bnezc(index, slow_path->GetEntryLabel());
2677 } else {
2678 DCHECK(IsUint<15>(length)) << length;
2679 __ Sltiu(TMP, index, length);
2680 __ Beqzc(TMP, slow_path->GetEntryLabel());
2681 }
2682 } else {
2683 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2684 BoundsCheckSlowPathMIPS64* slow_path =
2685 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2686 codegen_->AddSlowPath(slow_path);
2687 if (index_loc.IsConstant()) {
2688 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2689 if (index < 0) {
2690 __ Bc(slow_path->GetEntryLabel());
2691 } else if (index == 0) {
2692 __ Blezc(length, slow_path->GetEntryLabel());
2693 } else {
2694 DCHECK(IsInt<16>(index + 1)) << index;
2695 __ Sltiu(TMP, length, index + 1);
2696 __ Bnezc(TMP, slow_path->GetEntryLabel());
2697 }
2698 } else {
2699 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2700 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2701 }
2702 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002703}
2704
Alexey Frunze15958152017-02-09 19:08:30 -08002705// Temp is used for read barrier.
2706static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2707 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002708 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002709 (kUseBakerReadBarrier ||
2710 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2711 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2712 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2713 return 1;
2714 }
2715 return 0;
2716}
2717
2718// Extra temp is used for read barrier.
2719static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2720 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2721}
2722
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002724 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2725 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2726
2727 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2728 switch (type_check_kind) {
2729 case TypeCheckKind::kExactCheck:
2730 case TypeCheckKind::kAbstractClassCheck:
2731 case TypeCheckKind::kClassHierarchyCheck:
2732 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002733 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002734 ? LocationSummary::kCallOnSlowPath
2735 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2736 break;
2737 case TypeCheckKind::kArrayCheck:
2738 case TypeCheckKind::kUnresolvedCheck:
2739 case TypeCheckKind::kInterfaceCheck:
2740 call_kind = LocationSummary::kCallOnSlowPath;
2741 break;
2742 }
2743
Vladimir Markoca6fff82017-10-03 14:49:14 +01002744 LocationSummary* locations =
2745 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002746 locations->SetInAt(0, Location::RequiresRegister());
2747 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002748 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002749}
2750
2751void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002752 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002754 Location obj_loc = locations->InAt(0);
2755 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002756 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002757 Location temp_loc = locations->GetTemp(0);
2758 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2759 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2760 DCHECK_LE(num_temps, 2u);
2761 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002762 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2763 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2764 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2765 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2766 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2767 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2768 const uint32_t object_array_data_offset =
2769 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2770 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002771
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002772 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2773 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2774 // read barriers is done for performance and code size reasons.
2775 bool is_type_check_slow_path_fatal = false;
2776 if (!kEmitCompilerReadBarrier) {
2777 is_type_check_slow_path_fatal =
2778 (type_check_kind == TypeCheckKind::kExactCheck ||
2779 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2780 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2781 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2782 !instruction->CanThrowIntoCatchBlock();
2783 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002784 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002785 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2786 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002787 codegen_->AddSlowPath(slow_path);
2788
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002789 // Avoid this check if we know `obj` is not null.
2790 if (instruction->MustDoNullCheck()) {
2791 __ Beqzc(obj, &done);
2792 }
2793
2794 switch (type_check_kind) {
2795 case TypeCheckKind::kExactCheck:
2796 case TypeCheckKind::kArrayCheck: {
2797 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002798 GenerateReferenceLoadTwoRegisters(instruction,
2799 temp_loc,
2800 obj_loc,
2801 class_offset,
2802 maybe_temp2_loc,
2803 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002804 // Jump to slow path for throwing the exception or doing a
2805 // more involved array check.
2806 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2807 break;
2808 }
2809
2810 case TypeCheckKind::kAbstractClassCheck: {
2811 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002812 GenerateReferenceLoadTwoRegisters(instruction,
2813 temp_loc,
2814 obj_loc,
2815 class_offset,
2816 maybe_temp2_loc,
2817 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002818 // If the class is abstract, we eagerly fetch the super class of the
2819 // object to avoid doing a comparison we know will fail.
2820 Mips64Label loop;
2821 __ Bind(&loop);
2822 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002823 GenerateReferenceLoadOneRegister(instruction,
2824 temp_loc,
2825 super_offset,
2826 maybe_temp2_loc,
2827 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002828 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2829 // exception.
2830 __ Beqzc(temp, slow_path->GetEntryLabel());
2831 // Otherwise, compare the classes.
2832 __ Bnec(temp, cls, &loop);
2833 break;
2834 }
2835
2836 case TypeCheckKind::kClassHierarchyCheck: {
2837 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002838 GenerateReferenceLoadTwoRegisters(instruction,
2839 temp_loc,
2840 obj_loc,
2841 class_offset,
2842 maybe_temp2_loc,
2843 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002844 // Walk over the class hierarchy to find a match.
2845 Mips64Label loop;
2846 __ Bind(&loop);
2847 __ Beqc(temp, cls, &done);
2848 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002849 GenerateReferenceLoadOneRegister(instruction,
2850 temp_loc,
2851 super_offset,
2852 maybe_temp2_loc,
2853 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002854 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2855 // exception. Otherwise, jump to the beginning of the loop.
2856 __ Bnezc(temp, &loop);
2857 __ Bc(slow_path->GetEntryLabel());
2858 break;
2859 }
2860
2861 case TypeCheckKind::kArrayObjectCheck: {
2862 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002863 GenerateReferenceLoadTwoRegisters(instruction,
2864 temp_loc,
2865 obj_loc,
2866 class_offset,
2867 maybe_temp2_loc,
2868 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002869 // Do an exact check.
2870 __ Beqc(temp, cls, &done);
2871 // Otherwise, we need to check that the object's class is a non-primitive array.
2872 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002873 GenerateReferenceLoadOneRegister(instruction,
2874 temp_loc,
2875 component_offset,
2876 maybe_temp2_loc,
2877 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002878 // If the component type is null, jump to the slow path to throw the exception.
2879 __ Beqzc(temp, slow_path->GetEntryLabel());
2880 // Otherwise, the object is indeed an array, further check that this component
2881 // type is not a primitive type.
2882 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2883 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2884 __ Bnezc(temp, slow_path->GetEntryLabel());
2885 break;
2886 }
2887
2888 case TypeCheckKind::kUnresolvedCheck:
2889 // We always go into the type check slow path for the unresolved check case.
2890 // We cannot directly call the CheckCast runtime entry point
2891 // without resorting to a type checking slow path here (i.e. by
2892 // calling InvokeRuntime directly), as it would require to
2893 // assign fixed registers for the inputs of this HInstanceOf
2894 // instruction (following the runtime calling convention), which
2895 // might be cluttered by the potential first read barrier
2896 // emission at the beginning of this method.
2897 __ Bc(slow_path->GetEntryLabel());
2898 break;
2899
2900 case TypeCheckKind::kInterfaceCheck: {
2901 // Avoid read barriers to improve performance of the fast path. We can not get false
2902 // positives by doing this.
2903 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002904 GenerateReferenceLoadTwoRegisters(instruction,
2905 temp_loc,
2906 obj_loc,
2907 class_offset,
2908 maybe_temp2_loc,
2909 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002910 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002911 GenerateReferenceLoadTwoRegisters(instruction,
2912 temp_loc,
2913 temp_loc,
2914 iftable_offset,
2915 maybe_temp2_loc,
2916 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002917 // Iftable is never null.
2918 __ Lw(TMP, temp, array_length_offset);
2919 // Loop through the iftable and check if any class matches.
2920 Mips64Label loop;
2921 __ Bind(&loop);
2922 __ Beqzc(TMP, slow_path->GetEntryLabel());
2923 __ Lwu(AT, temp, object_array_data_offset);
2924 __ MaybeUnpoisonHeapReference(AT);
2925 // Go to next interface.
2926 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2927 __ Addiu(TMP, TMP, -2);
2928 // Compare the classes and continue the loop if they do not match.
2929 __ Bnec(AT, cls, &loop);
2930 break;
2931 }
2932 }
2933
2934 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002935 __ Bind(slow_path->GetExitLabel());
2936}
2937
2938void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2939 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002940 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941 locations->SetInAt(0, Location::RequiresRegister());
2942 if (check->HasUses()) {
2943 locations->SetOut(Location::SameAsFirstInput());
2944 }
2945}
2946
2947void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2948 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01002949 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002950 check->GetLoadClass(),
2951 check,
2952 check->GetDexPc(),
2953 true);
2954 codegen_->AddSlowPath(slow_path);
2955 GenerateClassInitializationCheck(slow_path,
2956 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2957}
2958
2959void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961
Vladimir Markoca6fff82017-10-03 14:49:14 +01002962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002963
2964 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002966 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002969 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt32:
2971 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002973 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2975 break;
2976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 case DataType::Type::kFloat32:
2978 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08002979 locations->SetInAt(0, Location::RequiresFpuRegister());
2980 locations->SetInAt(1, Location::RequiresFpuRegister());
2981 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002982 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983
2984 default:
2985 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2986 }
2987}
2988
2989void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2990 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002991 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002993
2994 // 0 if: left == right
2995 // 1 if: left > right
2996 // -1 if: left < right
2997 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002999 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003002 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kInt32:
3004 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003005 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003006 Location rhs_location = locations->InAt(1);
3007 bool use_imm = rhs_location.IsConstant();
3008 GpuRegister rhs = ZERO;
3009 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003011 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3012 if (value != 0) {
3013 rhs = AT;
3014 __ LoadConst64(rhs, value);
3015 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003016 } else {
3017 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3018 if (value != 0) {
3019 rhs = AT;
3020 __ LoadConst32(rhs, value);
3021 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003022 }
3023 } else {
3024 rhs = rhs_location.AsRegister<GpuRegister>();
3025 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003026 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003027 __ Slt(res, rhs, lhs);
3028 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029 break;
3030 }
3031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003033 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3034 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3035 Mips64Label done;
3036 __ CmpEqS(FTMP, lhs, rhs);
3037 __ LoadConst32(res, 0);
3038 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003039 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003040 __ CmpLtS(FTMP, lhs, rhs);
3041 __ LoadConst32(res, -1);
3042 __ Bc1nez(FTMP, &done);
3043 __ LoadConst32(res, 1);
3044 } else {
3045 __ CmpLtS(FTMP, rhs, lhs);
3046 __ LoadConst32(res, 1);
3047 __ Bc1nez(FTMP, &done);
3048 __ LoadConst32(res, -1);
3049 }
3050 __ Bind(&done);
3051 break;
3052 }
3053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003054 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003055 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3056 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3057 Mips64Label done;
3058 __ CmpEqD(FTMP, lhs, rhs);
3059 __ LoadConst32(res, 0);
3060 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003061 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003062 __ CmpLtD(FTMP, lhs, rhs);
3063 __ LoadConst32(res, -1);
3064 __ Bc1nez(FTMP, &done);
3065 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003066 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003067 __ CmpLtD(FTMP, rhs, lhs);
3068 __ LoadConst32(res, 1);
3069 __ Bc1nez(FTMP, &done);
3070 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003072 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003073 break;
3074 }
3075
3076 default:
3077 LOG(FATAL) << "Unimplemented compare type " << in_type;
3078 }
3079}
3080
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003081void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003082 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003083 switch (instruction->InputAt(0)->GetType()) {
3084 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003086 locations->SetInAt(0, Location::RequiresRegister());
3087 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3088 break;
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kFloat32:
3091 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003092 locations->SetInAt(0, Location::RequiresFpuRegister());
3093 locations->SetInAt(1, Location::RequiresFpuRegister());
3094 break;
3095 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003096 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3098 }
3099}
3100
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003101void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003103 return;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003108 switch (type) {
3109 default:
3110 // Integer case.
3111 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3112 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003113 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003114 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3115 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 case DataType::Type::kFloat32:
3117 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003118 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3119 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003120 }
3121}
3122
Alexey Frunzec857c742015-09-23 15:12:39 -07003123void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3124 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003126
3127 LocationSummary* locations = instruction->GetLocations();
3128 Location second = locations->InAt(1);
3129 DCHECK(second.IsConstant());
3130
3131 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3132 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3133 int64_t imm = Int64FromConstant(second.GetConstant());
3134 DCHECK(imm == 1 || imm == -1);
3135
3136 if (instruction->IsRem()) {
3137 __ Move(out, ZERO);
3138 } else {
3139 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003140 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003141 __ Subu(out, ZERO, dividend);
3142 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003143 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003144 __ Dsubu(out, ZERO, dividend);
3145 }
3146 } else if (out != dividend) {
3147 __ Move(out, dividend);
3148 }
3149 }
3150}
3151
3152void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3153 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003154 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003155
3156 LocationSummary* locations = instruction->GetLocations();
3157 Location second = locations->InAt(1);
3158 DCHECK(second.IsConstant());
3159
3160 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3161 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3162 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003163 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003164 int ctz_imm = CTZ(abs_imm);
3165
3166 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003168 if (ctz_imm == 1) {
3169 // Fast path for division by +/-2, which is very common.
3170 __ Srl(TMP, dividend, 31);
3171 } else {
3172 __ Sra(TMP, dividend, 31);
3173 __ Srl(TMP, TMP, 32 - ctz_imm);
3174 }
3175 __ Addu(out, dividend, TMP);
3176 __ Sra(out, out, ctz_imm);
3177 if (imm < 0) {
3178 __ Subu(out, ZERO, out);
3179 }
3180 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003182 if (ctz_imm == 1) {
3183 // Fast path for division by +/-2, which is very common.
3184 __ Dsrl32(TMP, dividend, 31);
3185 } else {
3186 __ Dsra32(TMP, dividend, 31);
3187 if (ctz_imm > 32) {
3188 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3189 } else {
3190 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3191 }
3192 }
3193 __ Daddu(out, dividend, TMP);
3194 if (ctz_imm < 32) {
3195 __ Dsra(out, out, ctz_imm);
3196 } else {
3197 __ Dsra32(out, out, ctz_imm - 32);
3198 }
3199 if (imm < 0) {
3200 __ Dsubu(out, ZERO, out);
3201 }
3202 }
3203 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003205 if (ctz_imm == 1) {
3206 // Fast path for modulo +/-2, which is very common.
3207 __ Sra(TMP, dividend, 31);
3208 __ Subu(out, dividend, TMP);
3209 __ Andi(out, out, 1);
3210 __ Addu(out, out, TMP);
3211 } else {
3212 __ Sra(TMP, dividend, 31);
3213 __ Srl(TMP, TMP, 32 - ctz_imm);
3214 __ Addu(out, dividend, TMP);
3215 if (IsUint<16>(abs_imm - 1)) {
3216 __ Andi(out, out, abs_imm - 1);
3217 } else {
3218 __ Sll(out, out, 32 - ctz_imm);
3219 __ Srl(out, out, 32 - ctz_imm);
3220 }
3221 __ Subu(out, out, TMP);
3222 }
3223 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003224 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003225 if (ctz_imm == 1) {
3226 // Fast path for modulo +/-2, which is very common.
3227 __ Dsra32(TMP, dividend, 31);
3228 __ Dsubu(out, dividend, TMP);
3229 __ Andi(out, out, 1);
3230 __ Daddu(out, out, TMP);
3231 } else {
3232 __ Dsra32(TMP, dividend, 31);
3233 if (ctz_imm > 32) {
3234 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3235 } else {
3236 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3237 }
3238 __ Daddu(out, dividend, TMP);
3239 if (IsUint<16>(abs_imm - 1)) {
3240 __ Andi(out, out, abs_imm - 1);
3241 } else {
3242 if (ctz_imm > 32) {
3243 __ Dsll(out, out, 64 - ctz_imm);
3244 __ Dsrl(out, out, 64 - ctz_imm);
3245 } else {
3246 __ Dsll32(out, out, 32 - ctz_imm);
3247 __ Dsrl32(out, out, 32 - ctz_imm);
3248 }
3249 }
3250 __ Dsubu(out, out, TMP);
3251 }
3252 }
3253 }
3254}
3255
3256void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3257 DCHECK(instruction->IsDiv() || instruction->IsRem());
3258
3259 LocationSummary* locations = instruction->GetLocations();
3260 Location second = locations->InAt(1);
3261 DCHECK(second.IsConstant());
3262
3263 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3264 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3265 int64_t imm = Int64FromConstant(second.GetConstant());
3266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003267 DataType::Type type = instruction->GetResultType();
3268 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003269
3270 int64_t magic;
3271 int shift;
3272 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003274 &magic,
3275 &shift);
3276
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003278 __ LoadConst32(TMP, magic);
3279 __ MuhR6(TMP, dividend, TMP);
3280
3281 if (imm > 0 && magic < 0) {
3282 __ Addu(TMP, TMP, dividend);
3283 } else if (imm < 0 && magic > 0) {
3284 __ Subu(TMP, TMP, dividend);
3285 }
3286
3287 if (shift != 0) {
3288 __ Sra(TMP, TMP, shift);
3289 }
3290
3291 if (instruction->IsDiv()) {
3292 __ Sra(out, TMP, 31);
3293 __ Subu(out, TMP, out);
3294 } else {
3295 __ Sra(AT, TMP, 31);
3296 __ Subu(AT, TMP, AT);
3297 __ LoadConst32(TMP, imm);
3298 __ MulR6(TMP, AT, TMP);
3299 __ Subu(out, dividend, TMP);
3300 }
3301 } else {
3302 __ LoadConst64(TMP, magic);
3303 __ Dmuh(TMP, dividend, TMP);
3304
3305 if (imm > 0 && magic < 0) {
3306 __ Daddu(TMP, TMP, dividend);
3307 } else if (imm < 0 && magic > 0) {
3308 __ Dsubu(TMP, TMP, dividend);
3309 }
3310
3311 if (shift >= 32) {
3312 __ Dsra32(TMP, TMP, shift - 32);
3313 } else if (shift > 0) {
3314 __ Dsra(TMP, TMP, shift);
3315 }
3316
3317 if (instruction->IsDiv()) {
3318 __ Dsra32(out, TMP, 31);
3319 __ Dsubu(out, TMP, out);
3320 } else {
3321 __ Dsra32(AT, TMP, 31);
3322 __ Dsubu(AT, TMP, AT);
3323 __ LoadConst64(TMP, imm);
3324 __ Dmul(TMP, AT, TMP);
3325 __ Dsubu(out, dividend, TMP);
3326 }
3327 }
3328}
3329
3330void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3331 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 DataType::Type type = instruction->GetResultType();
3333 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003334
3335 LocationSummary* locations = instruction->GetLocations();
3336 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3337 Location second = locations->InAt(1);
3338
3339 if (second.IsConstant()) {
3340 int64_t imm = Int64FromConstant(second.GetConstant());
3341 if (imm == 0) {
3342 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3343 } else if (imm == 1 || imm == -1) {
3344 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003345 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003346 DivRemByPowerOfTwo(instruction);
3347 } else {
3348 DCHECK(imm <= -2 || imm >= 2);
3349 GenerateDivRemWithAnyConstant(instruction);
3350 }
3351 } else {
3352 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3353 GpuRegister divisor = second.AsRegister<GpuRegister>();
3354 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003356 __ DivR6(out, dividend, divisor);
3357 else
3358 __ Ddiv(out, dividend, divisor);
3359 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003360 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003361 __ ModR6(out, dividend, divisor);
3362 else
3363 __ Dmod(out, dividend, divisor);
3364 }
3365 }
3366}
3367
Alexey Frunze4dda3372015-06-01 18:31:49 -07003368void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3369 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003370 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003371 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003372 case DataType::Type::kInt32:
3373 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003374 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003375 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3377 break;
3378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003379 case DataType::Type::kFloat32:
3380 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003381 locations->SetInAt(0, Location::RequiresFpuRegister());
3382 locations->SetInAt(1, Location::RequiresFpuRegister());
3383 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3384 break;
3385
3386 default:
3387 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3388 }
3389}
3390
3391void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003393 LocationSummary* locations = instruction->GetLocations();
3394
3395 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 case DataType::Type::kInt32:
3397 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003398 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003399 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 case DataType::Type::kFloat32:
3401 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003402 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3403 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3404 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003405 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406 __ DivS(dst, lhs, rhs);
3407 else
3408 __ DivD(dst, lhs, rhs);
3409 break;
3410 }
3411 default:
3412 LOG(FATAL) << "Unexpected div type " << type;
3413 }
3414}
3415
3416void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003417 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003418 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003419}
3420
3421void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3422 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003423 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003424 codegen_->AddSlowPath(slow_path);
3425 Location value = instruction->GetLocations()->InAt(0);
3426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003427 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003429 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003430 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003431 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003432 }
3433
3434 if (value.IsConstant()) {
3435 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3436 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003437 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438 } else {
3439 // A division by a non-null constant is valid. We don't need to perform
3440 // any check, so simply fall through.
3441 }
3442 } else {
3443 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3444 }
3445}
3446
3447void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003449 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003450 locations->SetOut(Location::ConstantLocation(constant));
3451}
3452
3453void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3454 // Will be generated at use site.
3455}
3456
3457void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3458 exit->SetLocations(nullptr);
3459}
3460
3461void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3462}
3463
3464void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3465 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003466 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003467 locations->SetOut(Location::ConstantLocation(constant));
3468}
3469
3470void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3471 // Will be generated at use site.
3472}
3473
David Brazdilfc6a86a2015-06-26 10:33:45 +00003474void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003475 DCHECK(!successor->IsExitBlock());
3476 HBasicBlock* block = got->GetBlock();
3477 HInstruction* previous = got->GetPrevious();
3478 HLoopInformation* info = block->GetLoopInformation();
3479
3480 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003481 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3482 return;
3483 }
3484 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3485 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3486 }
3487 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003488 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003489 }
3490}
3491
David Brazdilfc6a86a2015-06-26 10:33:45 +00003492void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3493 got->SetLocations(nullptr);
3494}
3495
3496void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3497 HandleGoto(got, got->GetSuccessor());
3498}
3499
3500void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3501 try_boundary->SetLocations(nullptr);
3502}
3503
3504void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3505 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3506 if (!successor->IsExitBlock()) {
3507 HandleGoto(try_boundary, successor);
3508 }
3509}
3510
Alexey Frunze299a9392015-12-08 16:08:02 -08003511void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3512 bool is64bit,
3513 LocationSummary* locations) {
3514 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3515 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3516 Location rhs_location = locations->InAt(1);
3517 GpuRegister rhs_reg = ZERO;
3518 int64_t rhs_imm = 0;
3519 bool use_imm = rhs_location.IsConstant();
3520 if (use_imm) {
3521 if (is64bit) {
3522 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3523 } else {
3524 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3525 }
3526 } else {
3527 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3528 }
3529 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3530
3531 switch (cond) {
3532 case kCondEQ:
3533 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003534 if (use_imm && IsInt<16>(-rhs_imm)) {
3535 if (rhs_imm == 0) {
3536 if (cond == kCondEQ) {
3537 __ Sltiu(dst, lhs, 1);
3538 } else {
3539 __ Sltu(dst, ZERO, lhs);
3540 }
3541 } else {
3542 if (is64bit) {
3543 __ Daddiu(dst, lhs, -rhs_imm);
3544 } else {
3545 __ Addiu(dst, lhs, -rhs_imm);
3546 }
3547 if (cond == kCondEQ) {
3548 __ Sltiu(dst, dst, 1);
3549 } else {
3550 __ Sltu(dst, ZERO, dst);
3551 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003552 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003553 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003554 if (use_imm && IsUint<16>(rhs_imm)) {
3555 __ Xori(dst, lhs, rhs_imm);
3556 } else {
3557 if (use_imm) {
3558 rhs_reg = TMP;
3559 __ LoadConst64(rhs_reg, rhs_imm);
3560 }
3561 __ Xor(dst, lhs, rhs_reg);
3562 }
3563 if (cond == kCondEQ) {
3564 __ Sltiu(dst, dst, 1);
3565 } else {
3566 __ Sltu(dst, ZERO, dst);
3567 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003568 }
3569 break;
3570
3571 case kCondLT:
3572 case kCondGE:
3573 if (use_imm && IsInt<16>(rhs_imm)) {
3574 __ Slti(dst, lhs, rhs_imm);
3575 } else {
3576 if (use_imm) {
3577 rhs_reg = TMP;
3578 __ LoadConst64(rhs_reg, rhs_imm);
3579 }
3580 __ Slt(dst, lhs, rhs_reg);
3581 }
3582 if (cond == kCondGE) {
3583 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3584 // only the slt instruction but no sge.
3585 __ Xori(dst, dst, 1);
3586 }
3587 break;
3588
3589 case kCondLE:
3590 case kCondGT:
3591 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3592 // Simulate lhs <= rhs via lhs < rhs + 1.
3593 __ Slti(dst, lhs, rhs_imm_plus_one);
3594 if (cond == kCondGT) {
3595 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3596 // only the slti instruction but no sgti.
3597 __ Xori(dst, dst, 1);
3598 }
3599 } else {
3600 if (use_imm) {
3601 rhs_reg = TMP;
3602 __ LoadConst64(rhs_reg, rhs_imm);
3603 }
3604 __ Slt(dst, rhs_reg, lhs);
3605 if (cond == kCondLE) {
3606 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3607 // only the slt instruction but no sle.
3608 __ Xori(dst, dst, 1);
3609 }
3610 }
3611 break;
3612
3613 case kCondB:
3614 case kCondAE:
3615 if (use_imm && IsInt<16>(rhs_imm)) {
3616 // Sltiu sign-extends its 16-bit immediate operand before
3617 // the comparison and thus lets us compare directly with
3618 // unsigned values in the ranges [0, 0x7fff] and
3619 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3620 __ Sltiu(dst, lhs, rhs_imm);
3621 } else {
3622 if (use_imm) {
3623 rhs_reg = TMP;
3624 __ LoadConst64(rhs_reg, rhs_imm);
3625 }
3626 __ Sltu(dst, lhs, rhs_reg);
3627 }
3628 if (cond == kCondAE) {
3629 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3630 // only the sltu instruction but no sgeu.
3631 __ Xori(dst, dst, 1);
3632 }
3633 break;
3634
3635 case kCondBE:
3636 case kCondA:
3637 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3638 // Simulate lhs <= rhs via lhs < rhs + 1.
3639 // Note that this only works if rhs + 1 does not overflow
3640 // to 0, hence the check above.
3641 // Sltiu sign-extends its 16-bit immediate operand before
3642 // the comparison and thus lets us compare directly with
3643 // unsigned values in the ranges [0, 0x7fff] and
3644 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3645 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3646 if (cond == kCondA) {
3647 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3648 // only the sltiu instruction but no sgtiu.
3649 __ Xori(dst, dst, 1);
3650 }
3651 } else {
3652 if (use_imm) {
3653 rhs_reg = TMP;
3654 __ LoadConst64(rhs_reg, rhs_imm);
3655 }
3656 __ Sltu(dst, rhs_reg, lhs);
3657 if (cond == kCondBE) {
3658 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3659 // only the sltu instruction but no sleu.
3660 __ Xori(dst, dst, 1);
3661 }
3662 }
3663 break;
3664 }
3665}
3666
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003667bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3668 bool is64bit,
3669 LocationSummary* input_locations,
3670 GpuRegister dst) {
3671 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3672 Location rhs_location = input_locations->InAt(1);
3673 GpuRegister rhs_reg = ZERO;
3674 int64_t rhs_imm = 0;
3675 bool use_imm = rhs_location.IsConstant();
3676 if (use_imm) {
3677 if (is64bit) {
3678 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3679 } else {
3680 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3681 }
3682 } else {
3683 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3684 }
3685 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3686
3687 switch (cond) {
3688 case kCondEQ:
3689 case kCondNE:
3690 if (use_imm && IsInt<16>(-rhs_imm)) {
3691 if (is64bit) {
3692 __ Daddiu(dst, lhs, -rhs_imm);
3693 } else {
3694 __ Addiu(dst, lhs, -rhs_imm);
3695 }
3696 } else if (use_imm && IsUint<16>(rhs_imm)) {
3697 __ Xori(dst, lhs, rhs_imm);
3698 } else {
3699 if (use_imm) {
3700 rhs_reg = TMP;
3701 __ LoadConst64(rhs_reg, rhs_imm);
3702 }
3703 __ Xor(dst, lhs, rhs_reg);
3704 }
3705 return (cond == kCondEQ);
3706
3707 case kCondLT:
3708 case kCondGE:
3709 if (use_imm && IsInt<16>(rhs_imm)) {
3710 __ Slti(dst, lhs, rhs_imm);
3711 } else {
3712 if (use_imm) {
3713 rhs_reg = TMP;
3714 __ LoadConst64(rhs_reg, rhs_imm);
3715 }
3716 __ Slt(dst, lhs, rhs_reg);
3717 }
3718 return (cond == kCondGE);
3719
3720 case kCondLE:
3721 case kCondGT:
3722 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3723 // Simulate lhs <= rhs via lhs < rhs + 1.
3724 __ Slti(dst, lhs, rhs_imm_plus_one);
3725 return (cond == kCondGT);
3726 } else {
3727 if (use_imm) {
3728 rhs_reg = TMP;
3729 __ LoadConst64(rhs_reg, rhs_imm);
3730 }
3731 __ Slt(dst, rhs_reg, lhs);
3732 return (cond == kCondLE);
3733 }
3734
3735 case kCondB:
3736 case kCondAE:
3737 if (use_imm && IsInt<16>(rhs_imm)) {
3738 // Sltiu sign-extends its 16-bit immediate operand before
3739 // the comparison and thus lets us compare directly with
3740 // unsigned values in the ranges [0, 0x7fff] and
3741 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3742 __ Sltiu(dst, lhs, rhs_imm);
3743 } else {
3744 if (use_imm) {
3745 rhs_reg = TMP;
3746 __ LoadConst64(rhs_reg, rhs_imm);
3747 }
3748 __ Sltu(dst, lhs, rhs_reg);
3749 }
3750 return (cond == kCondAE);
3751
3752 case kCondBE:
3753 case kCondA:
3754 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3755 // Simulate lhs <= rhs via lhs < rhs + 1.
3756 // Note that this only works if rhs + 1 does not overflow
3757 // to 0, hence the check above.
3758 // Sltiu sign-extends its 16-bit immediate operand before
3759 // the comparison and thus lets us compare directly with
3760 // unsigned values in the ranges [0, 0x7fff] and
3761 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3762 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3763 return (cond == kCondA);
3764 } else {
3765 if (use_imm) {
3766 rhs_reg = TMP;
3767 __ LoadConst64(rhs_reg, rhs_imm);
3768 }
3769 __ Sltu(dst, rhs_reg, lhs);
3770 return (cond == kCondBE);
3771 }
3772 }
3773}
3774
Alexey Frunze299a9392015-12-08 16:08:02 -08003775void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3776 bool is64bit,
3777 LocationSummary* locations,
3778 Mips64Label* label) {
3779 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3780 Location rhs_location = locations->InAt(1);
3781 GpuRegister rhs_reg = ZERO;
3782 int64_t rhs_imm = 0;
3783 bool use_imm = rhs_location.IsConstant();
3784 if (use_imm) {
3785 if (is64bit) {
3786 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3787 } else {
3788 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3789 }
3790 } else {
3791 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3792 }
3793
3794 if (use_imm && rhs_imm == 0) {
3795 switch (cond) {
3796 case kCondEQ:
3797 case kCondBE: // <= 0 if zero
3798 __ Beqzc(lhs, label);
3799 break;
3800 case kCondNE:
3801 case kCondA: // > 0 if non-zero
3802 __ Bnezc(lhs, label);
3803 break;
3804 case kCondLT:
3805 __ Bltzc(lhs, label);
3806 break;
3807 case kCondGE:
3808 __ Bgezc(lhs, label);
3809 break;
3810 case kCondLE:
3811 __ Blezc(lhs, label);
3812 break;
3813 case kCondGT:
3814 __ Bgtzc(lhs, label);
3815 break;
3816 case kCondB: // always false
3817 break;
3818 case kCondAE: // always true
3819 __ Bc(label);
3820 break;
3821 }
3822 } else {
3823 if (use_imm) {
3824 rhs_reg = TMP;
3825 __ LoadConst64(rhs_reg, rhs_imm);
3826 }
3827 switch (cond) {
3828 case kCondEQ:
3829 __ Beqc(lhs, rhs_reg, label);
3830 break;
3831 case kCondNE:
3832 __ Bnec(lhs, rhs_reg, label);
3833 break;
3834 case kCondLT:
3835 __ Bltc(lhs, rhs_reg, label);
3836 break;
3837 case kCondGE:
3838 __ Bgec(lhs, rhs_reg, label);
3839 break;
3840 case kCondLE:
3841 __ Bgec(rhs_reg, lhs, label);
3842 break;
3843 case kCondGT:
3844 __ Bltc(rhs_reg, lhs, label);
3845 break;
3846 case kCondB:
3847 __ Bltuc(lhs, rhs_reg, label);
3848 break;
3849 case kCondAE:
3850 __ Bgeuc(lhs, rhs_reg, label);
3851 break;
3852 case kCondBE:
3853 __ Bgeuc(rhs_reg, lhs, label);
3854 break;
3855 case kCondA:
3856 __ Bltuc(rhs_reg, lhs, label);
3857 break;
3858 }
3859 }
3860}
3861
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003862void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3863 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003864 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003865 LocationSummary* locations) {
3866 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3867 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3868 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003870 switch (cond) {
3871 case kCondEQ:
3872 __ CmpEqS(FTMP, lhs, rhs);
3873 __ Mfc1(dst, FTMP);
3874 __ Andi(dst, dst, 1);
3875 break;
3876 case kCondNE:
3877 __ CmpEqS(FTMP, lhs, rhs);
3878 __ Mfc1(dst, FTMP);
3879 __ Addiu(dst, dst, 1);
3880 break;
3881 case kCondLT:
3882 if (gt_bias) {
3883 __ CmpLtS(FTMP, lhs, rhs);
3884 } else {
3885 __ CmpUltS(FTMP, lhs, rhs);
3886 }
3887 __ Mfc1(dst, FTMP);
3888 __ Andi(dst, dst, 1);
3889 break;
3890 case kCondLE:
3891 if (gt_bias) {
3892 __ CmpLeS(FTMP, lhs, rhs);
3893 } else {
3894 __ CmpUleS(FTMP, lhs, rhs);
3895 }
3896 __ Mfc1(dst, FTMP);
3897 __ Andi(dst, dst, 1);
3898 break;
3899 case kCondGT:
3900 if (gt_bias) {
3901 __ CmpUltS(FTMP, rhs, lhs);
3902 } else {
3903 __ CmpLtS(FTMP, rhs, lhs);
3904 }
3905 __ Mfc1(dst, FTMP);
3906 __ Andi(dst, dst, 1);
3907 break;
3908 case kCondGE:
3909 if (gt_bias) {
3910 __ CmpUleS(FTMP, rhs, lhs);
3911 } else {
3912 __ CmpLeS(FTMP, rhs, lhs);
3913 }
3914 __ Mfc1(dst, FTMP);
3915 __ Andi(dst, dst, 1);
3916 break;
3917 default:
3918 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3919 UNREACHABLE();
3920 }
3921 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003923 switch (cond) {
3924 case kCondEQ:
3925 __ CmpEqD(FTMP, lhs, rhs);
3926 __ Mfc1(dst, FTMP);
3927 __ Andi(dst, dst, 1);
3928 break;
3929 case kCondNE:
3930 __ CmpEqD(FTMP, lhs, rhs);
3931 __ Mfc1(dst, FTMP);
3932 __ Addiu(dst, dst, 1);
3933 break;
3934 case kCondLT:
3935 if (gt_bias) {
3936 __ CmpLtD(FTMP, lhs, rhs);
3937 } else {
3938 __ CmpUltD(FTMP, lhs, rhs);
3939 }
3940 __ Mfc1(dst, FTMP);
3941 __ Andi(dst, dst, 1);
3942 break;
3943 case kCondLE:
3944 if (gt_bias) {
3945 __ CmpLeD(FTMP, lhs, rhs);
3946 } else {
3947 __ CmpUleD(FTMP, lhs, rhs);
3948 }
3949 __ Mfc1(dst, FTMP);
3950 __ Andi(dst, dst, 1);
3951 break;
3952 case kCondGT:
3953 if (gt_bias) {
3954 __ CmpUltD(FTMP, rhs, lhs);
3955 } else {
3956 __ CmpLtD(FTMP, rhs, lhs);
3957 }
3958 __ Mfc1(dst, FTMP);
3959 __ Andi(dst, dst, 1);
3960 break;
3961 case kCondGE:
3962 if (gt_bias) {
3963 __ CmpUleD(FTMP, rhs, lhs);
3964 } else {
3965 __ CmpLeD(FTMP, rhs, lhs);
3966 }
3967 __ Mfc1(dst, FTMP);
3968 __ Andi(dst, dst, 1);
3969 break;
3970 default:
3971 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3972 UNREACHABLE();
3973 }
3974 }
3975}
3976
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003977bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
3978 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003980 LocationSummary* input_locations,
3981 FpuRegister dst) {
3982 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
3983 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003984 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003985 switch (cond) {
3986 case kCondEQ:
3987 __ CmpEqS(dst, lhs, rhs);
3988 return false;
3989 case kCondNE:
3990 __ CmpEqS(dst, lhs, rhs);
3991 return true;
3992 case kCondLT:
3993 if (gt_bias) {
3994 __ CmpLtS(dst, lhs, rhs);
3995 } else {
3996 __ CmpUltS(dst, lhs, rhs);
3997 }
3998 return false;
3999 case kCondLE:
4000 if (gt_bias) {
4001 __ CmpLeS(dst, lhs, rhs);
4002 } else {
4003 __ CmpUleS(dst, lhs, rhs);
4004 }
4005 return false;
4006 case kCondGT:
4007 if (gt_bias) {
4008 __ CmpUltS(dst, rhs, lhs);
4009 } else {
4010 __ CmpLtS(dst, rhs, lhs);
4011 }
4012 return false;
4013 case kCondGE:
4014 if (gt_bias) {
4015 __ CmpUleS(dst, rhs, lhs);
4016 } else {
4017 __ CmpLeS(dst, rhs, lhs);
4018 }
4019 return false;
4020 default:
4021 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4022 UNREACHABLE();
4023 }
4024 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004025 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004026 switch (cond) {
4027 case kCondEQ:
4028 __ CmpEqD(dst, lhs, rhs);
4029 return false;
4030 case kCondNE:
4031 __ CmpEqD(dst, lhs, rhs);
4032 return true;
4033 case kCondLT:
4034 if (gt_bias) {
4035 __ CmpLtD(dst, lhs, rhs);
4036 } else {
4037 __ CmpUltD(dst, lhs, rhs);
4038 }
4039 return false;
4040 case kCondLE:
4041 if (gt_bias) {
4042 __ CmpLeD(dst, lhs, rhs);
4043 } else {
4044 __ CmpUleD(dst, lhs, rhs);
4045 }
4046 return false;
4047 case kCondGT:
4048 if (gt_bias) {
4049 __ CmpUltD(dst, rhs, lhs);
4050 } else {
4051 __ CmpLtD(dst, rhs, lhs);
4052 }
4053 return false;
4054 case kCondGE:
4055 if (gt_bias) {
4056 __ CmpUleD(dst, rhs, lhs);
4057 } else {
4058 __ CmpLeD(dst, rhs, lhs);
4059 }
4060 return false;
4061 default:
4062 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4063 UNREACHABLE();
4064 }
4065 }
4066}
4067
Alexey Frunze299a9392015-12-08 16:08:02 -08004068void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4069 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004071 LocationSummary* locations,
4072 Mips64Label* label) {
4073 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4074 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004076 switch (cond) {
4077 case kCondEQ:
4078 __ CmpEqS(FTMP, lhs, rhs);
4079 __ Bc1nez(FTMP, label);
4080 break;
4081 case kCondNE:
4082 __ CmpEqS(FTMP, lhs, rhs);
4083 __ Bc1eqz(FTMP, label);
4084 break;
4085 case kCondLT:
4086 if (gt_bias) {
4087 __ CmpLtS(FTMP, lhs, rhs);
4088 } else {
4089 __ CmpUltS(FTMP, lhs, rhs);
4090 }
4091 __ Bc1nez(FTMP, label);
4092 break;
4093 case kCondLE:
4094 if (gt_bias) {
4095 __ CmpLeS(FTMP, lhs, rhs);
4096 } else {
4097 __ CmpUleS(FTMP, lhs, rhs);
4098 }
4099 __ Bc1nez(FTMP, label);
4100 break;
4101 case kCondGT:
4102 if (gt_bias) {
4103 __ CmpUltS(FTMP, rhs, lhs);
4104 } else {
4105 __ CmpLtS(FTMP, rhs, lhs);
4106 }
4107 __ Bc1nez(FTMP, label);
4108 break;
4109 case kCondGE:
4110 if (gt_bias) {
4111 __ CmpUleS(FTMP, rhs, lhs);
4112 } else {
4113 __ CmpLeS(FTMP, rhs, lhs);
4114 }
4115 __ Bc1nez(FTMP, label);
4116 break;
4117 default:
4118 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004119 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004120 }
4121 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004123 switch (cond) {
4124 case kCondEQ:
4125 __ CmpEqD(FTMP, lhs, rhs);
4126 __ Bc1nez(FTMP, label);
4127 break;
4128 case kCondNE:
4129 __ CmpEqD(FTMP, lhs, rhs);
4130 __ Bc1eqz(FTMP, label);
4131 break;
4132 case kCondLT:
4133 if (gt_bias) {
4134 __ CmpLtD(FTMP, lhs, rhs);
4135 } else {
4136 __ CmpUltD(FTMP, lhs, rhs);
4137 }
4138 __ Bc1nez(FTMP, label);
4139 break;
4140 case kCondLE:
4141 if (gt_bias) {
4142 __ CmpLeD(FTMP, lhs, rhs);
4143 } else {
4144 __ CmpUleD(FTMP, lhs, rhs);
4145 }
4146 __ Bc1nez(FTMP, label);
4147 break;
4148 case kCondGT:
4149 if (gt_bias) {
4150 __ CmpUltD(FTMP, rhs, lhs);
4151 } else {
4152 __ CmpLtD(FTMP, rhs, lhs);
4153 }
4154 __ Bc1nez(FTMP, label);
4155 break;
4156 case kCondGE:
4157 if (gt_bias) {
4158 __ CmpUleD(FTMP, rhs, lhs);
4159 } else {
4160 __ CmpLeD(FTMP, rhs, lhs);
4161 }
4162 __ Bc1nez(FTMP, label);
4163 break;
4164 default:
4165 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004166 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004167 }
4168 }
4169}
4170
Alexey Frunze4dda3372015-06-01 18:31:49 -07004171void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004172 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004173 Mips64Label* true_target,
4174 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004175 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004176
David Brazdil0debae72015-11-12 18:37:00 +00004177 if (true_target == nullptr && false_target == nullptr) {
4178 // Nothing to do. The code always falls through.
4179 return;
4180 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004181 // Constant condition, statically compared against "true" (integer value 1).
4182 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004183 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004184 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004185 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004187 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004188 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004189 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004190 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004191 }
David Brazdil0debae72015-11-12 18:37:00 +00004192 return;
4193 }
4194
4195 // The following code generates these patterns:
4196 // (1) true_target == nullptr && false_target != nullptr
4197 // - opposite condition true => branch to false_target
4198 // (2) true_target != nullptr && false_target == nullptr
4199 // - condition true => branch to true_target
4200 // (3) true_target != nullptr && false_target != nullptr
4201 // - condition true => branch to true_target
4202 // - branch to false_target
4203 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004204 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004205 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004207 if (true_target == nullptr) {
4208 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4209 } else {
4210 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4211 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004212 } else {
4213 // The condition instruction has not been materialized, use its inputs as
4214 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004215 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004216 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004217 LocationSummary* locations = cond->GetLocations();
4218 IfCondition if_cond = condition->GetCondition();
4219 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004220
David Brazdil0debae72015-11-12 18:37:00 +00004221 if (true_target == nullptr) {
4222 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004223 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004224 }
4225
Alexey Frunze299a9392015-12-08 16:08:02 -08004226 switch (type) {
4227 default:
4228 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4229 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004231 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4232 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004233 case DataType::Type::kFloat32:
4234 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004235 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4236 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004237 }
4238 }
David Brazdil0debae72015-11-12 18:37:00 +00004239
4240 // If neither branch falls through (case 3), the conditional branch to `true_target`
4241 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4242 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004243 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004244 }
4245}
4246
4247void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004248 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004249 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004250 locations->SetInAt(0, Location::RequiresRegister());
4251 }
4252}
4253
4254void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004255 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4256 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004257 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004258 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004259 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004260 nullptr : codegen_->GetLabelOf(false_successor);
4261 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004262}
4263
4264void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004265 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004266 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004267 InvokeRuntimeCallingConvention calling_convention;
4268 RegisterSet caller_saves = RegisterSet::Empty();
4269 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4270 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004271 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004272 locations->SetInAt(0, Location::RequiresRegister());
4273 }
4274}
4275
4276void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004277 SlowPathCodeMIPS64* slow_path =
4278 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004279 GenerateTestAndBranch(deoptimize,
4280 /* condition_input_index */ 0,
4281 slow_path->GetEntryLabel(),
4282 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004283}
4284
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004285// This function returns true if a conditional move can be generated for HSelect.
4286// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4287// branches and regular moves.
4288//
4289// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4290//
4291// While determining feasibility of a conditional move and setting inputs/outputs
4292// are two distinct tasks, this function does both because they share quite a bit
4293// of common logic.
4294static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4295 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4296 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4297 HCondition* condition = cond->AsCondition();
4298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004299 DataType::Type cond_type =
4300 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4301 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004302
4303 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4304 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4305 bool is_true_value_zero_constant =
4306 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4307 bool is_false_value_zero_constant =
4308 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4309
4310 bool can_move_conditionally = false;
4311 bool use_const_for_false_in = false;
4312 bool use_const_for_true_in = false;
4313
4314 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004315 if (!DataType::IsFloatingPointType(cond_type)) {
4316 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004317 // Moving int/long on int/long condition.
4318 if (is_true_value_zero_constant) {
4319 // seleqz out_reg, false_reg, cond_reg
4320 can_move_conditionally = true;
4321 use_const_for_true_in = true;
4322 } else if (is_false_value_zero_constant) {
4323 // selnez out_reg, true_reg, cond_reg
4324 can_move_conditionally = true;
4325 use_const_for_false_in = true;
4326 } else if (materialized) {
4327 // Not materializing unmaterialized int conditions
4328 // to keep the instruction count low.
4329 // selnez AT, true_reg, cond_reg
4330 // seleqz TMP, false_reg, cond_reg
4331 // or out_reg, AT, TMP
4332 can_move_conditionally = true;
4333 }
4334 } else {
4335 // Moving float/double on int/long condition.
4336 if (materialized) {
4337 // Not materializing unmaterialized int conditions
4338 // to keep the instruction count low.
4339 can_move_conditionally = true;
4340 if (is_true_value_zero_constant) {
4341 // sltu TMP, ZERO, cond_reg
4342 // mtc1 TMP, temp_cond_reg
4343 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4344 use_const_for_true_in = true;
4345 } else if (is_false_value_zero_constant) {
4346 // sltu TMP, ZERO, cond_reg
4347 // mtc1 TMP, temp_cond_reg
4348 // selnez.fmt out_reg, true_reg, temp_cond_reg
4349 use_const_for_false_in = true;
4350 } else {
4351 // sltu TMP, ZERO, cond_reg
4352 // mtc1 TMP, temp_cond_reg
4353 // sel.fmt temp_cond_reg, false_reg, true_reg
4354 // mov.fmt out_reg, temp_cond_reg
4355 }
4356 }
4357 }
4358 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004360 // Moving int/long on float/double condition.
4361 can_move_conditionally = true;
4362 if (is_true_value_zero_constant) {
4363 // mfc1 TMP, temp_cond_reg
4364 // seleqz out_reg, false_reg, TMP
4365 use_const_for_true_in = true;
4366 } else if (is_false_value_zero_constant) {
4367 // mfc1 TMP, temp_cond_reg
4368 // selnez out_reg, true_reg, TMP
4369 use_const_for_false_in = true;
4370 } else {
4371 // mfc1 TMP, temp_cond_reg
4372 // selnez AT, true_reg, TMP
4373 // seleqz TMP, false_reg, TMP
4374 // or out_reg, AT, TMP
4375 }
4376 } else {
4377 // Moving float/double on float/double condition.
4378 can_move_conditionally = true;
4379 if (is_true_value_zero_constant) {
4380 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4381 use_const_for_true_in = true;
4382 } else if (is_false_value_zero_constant) {
4383 // selnez.fmt out_reg, true_reg, temp_cond_reg
4384 use_const_for_false_in = true;
4385 } else {
4386 // sel.fmt temp_cond_reg, false_reg, true_reg
4387 // mov.fmt out_reg, temp_cond_reg
4388 }
4389 }
4390 }
4391 }
4392
4393 if (can_move_conditionally) {
4394 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4395 } else {
4396 DCHECK(!use_const_for_false_in);
4397 DCHECK(!use_const_for_true_in);
4398 }
4399
4400 if (locations_to_set != nullptr) {
4401 if (use_const_for_false_in) {
4402 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4403 } else {
4404 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004405 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004406 ? Location::RequiresFpuRegister()
4407 : Location::RequiresRegister());
4408 }
4409 if (use_const_for_true_in) {
4410 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4411 } else {
4412 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004413 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004414 ? Location::RequiresFpuRegister()
4415 : Location::RequiresRegister());
4416 }
4417 if (materialized) {
4418 locations_to_set->SetInAt(2, Location::RequiresRegister());
4419 }
4420
4421 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004422 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004423 ? Location::RequiresFpuRegister()
4424 : Location::RequiresRegister());
4425 } else {
4426 locations_to_set->SetOut(Location::SameAsFirstInput());
4427 }
4428 }
4429
4430 return can_move_conditionally;
4431}
4432
4433
4434void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4435 LocationSummary* locations = select->GetLocations();
4436 Location dst = locations->Out();
4437 Location false_src = locations->InAt(0);
4438 Location true_src = locations->InAt(1);
4439 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4440 GpuRegister cond_reg = TMP;
4441 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004443 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004444 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004445
4446 if (IsBooleanValueOrMaterializedCondition(cond)) {
4447 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4448 } else {
4449 HCondition* condition = cond->AsCondition();
4450 LocationSummary* cond_locations = cond->GetLocations();
4451 IfCondition if_cond = condition->GetCondition();
4452 cond_type = condition->InputAt(0)->GetType();
4453 switch (cond_type) {
4454 default:
4455 cond_inverted = MaterializeIntLongCompare(if_cond,
4456 /* is64bit */ false,
4457 cond_locations,
4458 cond_reg);
4459 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004460 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004461 cond_inverted = MaterializeIntLongCompare(if_cond,
4462 /* is64bit */ true,
4463 cond_locations,
4464 cond_reg);
4465 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004466 case DataType::Type::kFloat32:
4467 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004468 cond_inverted = MaterializeFpCompare(if_cond,
4469 condition->IsGtBias(),
4470 cond_type,
4471 cond_locations,
4472 fcond_reg);
4473 break;
4474 }
4475 }
4476
4477 if (true_src.IsConstant()) {
4478 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4479 }
4480 if (false_src.IsConstant()) {
4481 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4482 }
4483
4484 switch (dst_type) {
4485 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004486 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004487 __ Mfc1(cond_reg, fcond_reg);
4488 }
4489 if (true_src.IsConstant()) {
4490 if (cond_inverted) {
4491 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4492 } else {
4493 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4494 }
4495 } else if (false_src.IsConstant()) {
4496 if (cond_inverted) {
4497 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4498 } else {
4499 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4500 }
4501 } else {
4502 DCHECK_NE(cond_reg, AT);
4503 if (cond_inverted) {
4504 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4505 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4506 } else {
4507 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4508 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4509 }
4510 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4511 }
4512 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004513 case DataType::Type::kFloat32: {
4514 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004515 // sel*.fmt tests bit 0 of the condition register, account for that.
4516 __ Sltu(TMP, ZERO, cond_reg);
4517 __ Mtc1(TMP, fcond_reg);
4518 }
4519 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4520 if (true_src.IsConstant()) {
4521 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4522 if (cond_inverted) {
4523 __ SelnezS(dst_reg, src_reg, fcond_reg);
4524 } else {
4525 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4526 }
4527 } else if (false_src.IsConstant()) {
4528 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4529 if (cond_inverted) {
4530 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4531 } else {
4532 __ SelnezS(dst_reg, src_reg, fcond_reg);
4533 }
4534 } else {
4535 if (cond_inverted) {
4536 __ SelS(fcond_reg,
4537 true_src.AsFpuRegister<FpuRegister>(),
4538 false_src.AsFpuRegister<FpuRegister>());
4539 } else {
4540 __ SelS(fcond_reg,
4541 false_src.AsFpuRegister<FpuRegister>(),
4542 true_src.AsFpuRegister<FpuRegister>());
4543 }
4544 __ MovS(dst_reg, fcond_reg);
4545 }
4546 break;
4547 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004548 case DataType::Type::kFloat64: {
4549 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004550 // sel*.fmt tests bit 0 of the condition register, account for that.
4551 __ Sltu(TMP, ZERO, cond_reg);
4552 __ Mtc1(TMP, fcond_reg);
4553 }
4554 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4555 if (true_src.IsConstant()) {
4556 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4557 if (cond_inverted) {
4558 __ SelnezD(dst_reg, src_reg, fcond_reg);
4559 } else {
4560 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4561 }
4562 } else if (false_src.IsConstant()) {
4563 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4564 if (cond_inverted) {
4565 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4566 } else {
4567 __ SelnezD(dst_reg, src_reg, fcond_reg);
4568 }
4569 } else {
4570 if (cond_inverted) {
4571 __ SelD(fcond_reg,
4572 true_src.AsFpuRegister<FpuRegister>(),
4573 false_src.AsFpuRegister<FpuRegister>());
4574 } else {
4575 __ SelD(fcond_reg,
4576 false_src.AsFpuRegister<FpuRegister>(),
4577 true_src.AsFpuRegister<FpuRegister>());
4578 }
4579 __ MovD(dst_reg, fcond_reg);
4580 }
4581 break;
4582 }
4583 }
4584}
4585
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004586void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004587 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004588 LocationSummary(flag, LocationSummary::kNoCall);
4589 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004590}
4591
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004592void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4593 __ LoadFromOffset(kLoadWord,
4594 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4595 SP,
4596 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004597}
4598
David Brazdil74eb1b22015-12-14 11:44:01 +00004599void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004600 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004601 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004602}
4603
4604void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004605 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4606 GenConditionalMove(select);
4607 } else {
4608 LocationSummary* locations = select->GetLocations();
4609 Mips64Label false_target;
4610 GenerateTestAndBranch(select,
4611 /* condition_input_index */ 2,
4612 /* true_target */ nullptr,
4613 &false_target);
4614 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4615 __ Bind(&false_target);
4616 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004617}
4618
David Srbecky0cf44932015-12-09 14:09:59 +00004619void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004620 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004621}
4622
David Srbeckyd28f4a02016-03-14 17:14:24 +00004623void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4624 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004625}
4626
4627void CodeGeneratorMIPS64::GenerateNop() {
4628 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004629}
4630
Alexey Frunze4dda3372015-06-01 18:31:49 -07004631void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004632 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004634 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004635 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004636 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004637 instruction,
4638 object_field_get_with_read_barrier
4639 ? LocationSummary::kCallOnSlowPath
4640 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004641 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4642 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4643 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004644 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004645 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004646 locations->SetOut(Location::RequiresFpuRegister());
4647 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004648 // The output overlaps in the case of an object field get with
4649 // read barriers enabled: we do not want the move to overwrite the
4650 // object's location, as we need it to emit the read barrier.
4651 locations->SetOut(Location::RequiresRegister(),
4652 object_field_get_with_read_barrier
4653 ? Location::kOutputOverlap
4654 : Location::kNoOutputOverlap);
4655 }
4656 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4657 // We need a temporary register for the read barrier marking slow
4658 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004659 if (!kBakerReadBarrierThunksEnableForFields) {
4660 locations->AddTemp(Location::RequiresRegister());
4661 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004662 }
4663}
4664
4665void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4666 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004667 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4668 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004669 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004670 Location obj_loc = locations->InAt(0);
4671 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4672 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004673 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004674 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004675 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004676 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4677
Alexey Frunze4dda3372015-06-01 18:31:49 -07004678 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004679 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004680 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004681 load_type = kLoadUnsignedByte;
4682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004684 load_type = kLoadSignedByte;
4685 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004686 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004687 load_type = kLoadUnsignedHalfword;
4688 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004689 case DataType::Type::kInt16:
4690 load_type = kLoadSignedHalfword;
4691 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kInt32:
4693 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004694 load_type = kLoadWord;
4695 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004696 case DataType::Type::kInt64:
4697 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004698 load_type = kLoadDoubleword;
4699 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004700 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004701 load_type = kLoadUnsignedWord;
4702 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004703 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004704 LOG(FATAL) << "Unreachable type " << type;
4705 UNREACHABLE();
4706 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004707 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004708 DCHECK(dst_loc.IsRegister());
4709 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004710 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004711 // /* HeapReference<Object> */ dst = *(obj + offset)
4712 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004713 Location temp_loc =
4714 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004715 // Note that a potential implicit null check is handled in this
4716 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4717 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4718 dst_loc,
4719 obj,
4720 offset,
4721 temp_loc,
4722 /* needs_null_check */ true);
4723 if (is_volatile) {
4724 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4725 }
4726 } else {
4727 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4728 if (is_volatile) {
4729 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4730 }
4731 // If read barriers are enabled, emit read barriers other than
4732 // Baker's using a slow path (and also unpoison the loaded
4733 // reference, if heap poisoning is enabled).
4734 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4735 }
4736 } else {
4737 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4738 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004739 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004740 DCHECK(dst_loc.IsFpuRegister());
4741 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004742 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004743 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004744
Alexey Frunze15958152017-02-09 19:08:30 -08004745 // Memory barriers, in the case of references, are handled in the
4746 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004748 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004749 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004750}
4751
4752void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4753 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4754 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004755 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004756 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004758 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004759 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004760 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004761 }
4762}
4763
4764void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004765 const FieldInfo& field_info,
4766 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004767 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004768 LocationSummary* locations = instruction->GetLocations();
4769 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004770 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004771 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004772 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004773 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4774 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004775 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4776
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004778 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004779 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004781 store_type = kStoreByte;
4782 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004784 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004785 store_type = kStoreHalfword;
4786 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 case DataType::Type::kInt32:
4788 case DataType::Type::kFloat32:
4789 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004790 store_type = kStoreWord;
4791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt64:
4793 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004794 store_type = kStoreDoubleword;
4795 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004797 LOG(FATAL) << "Unreachable type " << type;
4798 UNREACHABLE();
4799 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004800
Alexey Frunze15958152017-02-09 19:08:30 -08004801 if (is_volatile) {
4802 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4803 }
4804
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004805 if (value_location.IsConstant()) {
4806 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4807 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4808 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004809 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004810 DCHECK(value_location.IsRegister());
4811 GpuRegister src = value_location.AsRegister<GpuRegister>();
4812 if (kPoisonHeapReferences && needs_write_barrier) {
4813 // Note that in the case where `value` is a null reference,
4814 // we do not enter this block, as a null reference does not
4815 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004816 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004817 __ PoisonHeapReference(TMP, src);
4818 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4819 } else {
4820 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4821 }
4822 } else {
4823 DCHECK(value_location.IsFpuRegister());
4824 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4825 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4826 }
4827 }
Alexey Frunze15958152017-02-09 19:08:30 -08004828
Alexey Frunzec061de12017-02-14 13:27:23 -08004829 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004830 DCHECK(value_location.IsRegister());
4831 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004832 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833 }
Alexey Frunze15958152017-02-09 19:08:30 -08004834
4835 if (is_volatile) {
4836 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838}
4839
4840void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4841 HandleFieldGet(instruction, instruction->GetFieldInfo());
4842}
4843
4844void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4845 HandleFieldGet(instruction, instruction->GetFieldInfo());
4846}
4847
4848void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4849 HandleFieldSet(instruction, instruction->GetFieldInfo());
4850}
4851
4852void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004853 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004854}
4855
Alexey Frunze15958152017-02-09 19:08:30 -08004856void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4857 HInstruction* instruction,
4858 Location out,
4859 uint32_t offset,
4860 Location maybe_temp,
4861 ReadBarrierOption read_barrier_option) {
4862 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4863 if (read_barrier_option == kWithReadBarrier) {
4864 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004865 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4866 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4867 }
Alexey Frunze15958152017-02-09 19:08:30 -08004868 if (kUseBakerReadBarrier) {
4869 // Load with fast path based Baker's read barrier.
4870 // /* HeapReference<Object> */ out = *(out + offset)
4871 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4872 out,
4873 out_reg,
4874 offset,
4875 maybe_temp,
4876 /* needs_null_check */ false);
4877 } else {
4878 // Load with slow path based read barrier.
4879 // Save the value of `out` into `maybe_temp` before overwriting it
4880 // in the following move operation, as we will need it for the
4881 // read barrier below.
4882 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4883 // /* HeapReference<Object> */ out = *(out + offset)
4884 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4885 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4886 }
4887 } else {
4888 // Plain load with no read barrier.
4889 // /* HeapReference<Object> */ out = *(out + offset)
4890 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4891 __ MaybeUnpoisonHeapReference(out_reg);
4892 }
4893}
4894
4895void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4896 HInstruction* instruction,
4897 Location out,
4898 Location obj,
4899 uint32_t offset,
4900 Location maybe_temp,
4901 ReadBarrierOption read_barrier_option) {
4902 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4903 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4904 if (read_barrier_option == kWithReadBarrier) {
4905 CHECK(kEmitCompilerReadBarrier);
4906 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004907 if (!kBakerReadBarrierThunksEnableForFields) {
4908 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4909 }
Alexey Frunze15958152017-02-09 19:08:30 -08004910 // Load with fast path based Baker's read barrier.
4911 // /* HeapReference<Object> */ out = *(obj + offset)
4912 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4913 out,
4914 obj_reg,
4915 offset,
4916 maybe_temp,
4917 /* needs_null_check */ false);
4918 } else {
4919 // Load with slow path based read barrier.
4920 // /* HeapReference<Object> */ out = *(obj + offset)
4921 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4922 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4923 }
4924 } else {
4925 // Plain load with no read barrier.
4926 // /* HeapReference<Object> */ out = *(obj + offset)
4927 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4928 __ MaybeUnpoisonHeapReference(out_reg);
4929 }
4930}
4931
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004932static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
4933 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
4934 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
4935 return reg - V0;
4936 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
4937 return 13 + (reg - S2);
4938 } else if (reg == S8) { // One more.
4939 return 19;
4940 }
4941 LOG(FATAL) << "Unexpected register " << reg;
4942 UNREACHABLE();
4943}
4944
4945static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
4946 int num = GetBakerMarkThunkNumber(reg) +
4947 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
4948 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
4949}
4950
4951static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
4952 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
4953 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
4954}
4955
4956void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
4957 Location root,
4958 GpuRegister obj,
4959 uint32_t offset,
4960 ReadBarrierOption read_barrier_option,
4961 Mips64Label* label_low) {
4962 if (label_low != nullptr) {
4963 DCHECK_EQ(offset, 0x5678u);
4964 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004965 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004966 if (read_barrier_option == kWithReadBarrier) {
4967 DCHECK(kEmitCompilerReadBarrier);
4968 if (kUseBakerReadBarrier) {
4969 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4970 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004971 if (kBakerReadBarrierThunksEnableForGcRoots) {
4972 // Note that we do not actually check the value of `GetIsGcMarking()`
4973 // to decide whether to mark the loaded GC root or not. Instead, we
4974 // load into `temp` (T9) the read barrier mark introspection entrypoint.
4975 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
4976 // vice versa.
4977 //
4978 // We use thunks for the slow path. That thunk checks the reference
4979 // and jumps to the entrypoint if needed.
4980 //
4981 // temp = Thread::Current()->pReadBarrierMarkReg00
4982 // // AKA &art_quick_read_barrier_mark_introspection.
4983 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
4984 // if (temp != nullptr) {
4985 // temp = &gc_root_thunk<root_reg>
4986 // root = temp(root)
4987 // }
Alexey Frunze15958152017-02-09 19:08:30 -08004988
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004989 const int32_t entry_point_offset =
4990 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
4991 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
4992 int16_t offset_low = Low16Bits(offset);
4993 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
4994 // extension in lwu.
4995 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
4996 GpuRegister base = short_offset ? obj : TMP;
4997 // Loading the entrypoint does not require a load acquire since it is only changed when
4998 // threads are suspended or running a checkpoint.
4999 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5000 if (!short_offset) {
5001 DCHECK(!label_low);
5002 __ Daui(base, obj, offset_high);
5003 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005004 Mips64Label skip_call;
5005 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005006 if (label_low != nullptr) {
5007 DCHECK(short_offset);
5008 __ Bind(label_low);
5009 }
5010 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5011 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5012 // in delay slot.
5013 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005014 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005015 } else {
5016 // Note that we do not actually check the value of `GetIsGcMarking()`
5017 // to decide whether to mark the loaded GC root or not. Instead, we
5018 // load into `temp` (T9) the read barrier mark entry point corresponding
5019 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5020 // is false, and vice versa.
5021 //
5022 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5023 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5024 // if (temp != null) {
5025 // root = temp(root)
5026 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005027
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005028 if (label_low != nullptr) {
5029 __ Bind(label_low);
5030 }
5031 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5032 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5033 static_assert(
5034 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5035 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5036 "have different sizes.");
5037 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5038 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5039 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005040
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005041 // Slow path marking the GC root `root`.
5042 Location temp = Location::RegisterLocation(T9);
5043 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005044 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005045 instruction,
5046 root,
5047 /*entrypoint*/ temp);
5048 codegen_->AddSlowPath(slow_path);
5049
5050 const int32_t entry_point_offset =
5051 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5052 // Loading the entrypoint does not require a load acquire since it is only changed when
5053 // threads are suspended or running a checkpoint.
5054 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5055 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5056 __ Bind(slow_path->GetExitLabel());
5057 }
Alexey Frunze15958152017-02-09 19:08:30 -08005058 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005059 if (label_low != nullptr) {
5060 __ Bind(label_low);
5061 }
Alexey Frunze15958152017-02-09 19:08:30 -08005062 // GC root loaded through a slow path for read barriers other
5063 // than Baker's.
5064 // /* GcRoot<mirror::Object>* */ root = obj + offset
5065 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5066 // /* mirror::Object* */ root = root->Read()
5067 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5068 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005069 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005070 if (label_low != nullptr) {
5071 __ Bind(label_low);
5072 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005073 // Plain GC root load with no read barrier.
5074 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5075 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5076 // Note that GC roots are not affected by heap poisoning, thus we
5077 // do not have to unpoison `root_reg` here.
5078 }
5079}
5080
Alexey Frunze15958152017-02-09 19:08:30 -08005081void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5082 Location ref,
5083 GpuRegister obj,
5084 uint32_t offset,
5085 Location temp,
5086 bool needs_null_check) {
5087 DCHECK(kEmitCompilerReadBarrier);
5088 DCHECK(kUseBakerReadBarrier);
5089
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005090 if (kBakerReadBarrierThunksEnableForFields) {
5091 // Note that we do not actually check the value of `GetIsGcMarking()`
5092 // to decide whether to mark the loaded reference or not. Instead, we
5093 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5094 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5095 // vice versa.
5096 //
5097 // We use thunks for the slow path. That thunk checks the reference
5098 // and jumps to the entrypoint if needed. If the holder is not gray,
5099 // it issues a load-load memory barrier and returns to the original
5100 // reference load.
5101 //
5102 // temp = Thread::Current()->pReadBarrierMarkReg00
5103 // // AKA &art_quick_read_barrier_mark_introspection.
5104 // if (temp != nullptr) {
5105 // temp = &field_array_thunk<holder_reg>
5106 // temp()
5107 // }
5108 // not_gray_return_address:
5109 // // If the offset is too large to fit into the lw instruction, we
5110 // // use an adjusted base register (TMP) here. This register
5111 // // receives bits 16 ... 31 of the offset before the thunk invocation
5112 // // and the thunk benefits from it.
5113 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5114 // gray_return_address:
5115
5116 DCHECK(temp.IsInvalid());
5117 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5118 const int32_t entry_point_offset =
5119 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5120 // There may have or may have not been a null check if the field offset is smaller than
5121 // the page size.
5122 // There must've been a null check in case it's actually a load from an array.
5123 // We will, however, perform an explicit null check in the thunk as it's easier to
5124 // do it than not.
5125 if (instruction->IsArrayGet()) {
5126 DCHECK(!needs_null_check);
5127 }
5128 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5129 // Loading the entrypoint does not require a load acquire since it is only changed when
5130 // threads are suspended or running a checkpoint.
5131 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5132 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005133 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005134 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005135 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005136 __ Nop(); // In forbidden slot.
5137 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005138 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005139 // /* HeapReference<Object> */ ref = *(obj + offset)
5140 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5141 } else {
5142 int16_t offset_low = Low16Bits(offset);
5143 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005144 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005145 __ Daui(TMP, obj, offset_high); // In delay slot.
5146 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005147 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005148 // /* HeapReference<Object> */ ref = *(obj + offset)
5149 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5150 }
5151 if (needs_null_check) {
5152 MaybeRecordImplicitNullCheck(instruction);
5153 }
5154 __ MaybeUnpoisonHeapReference(ref_reg);
5155 return;
5156 }
5157
Alexey Frunze15958152017-02-09 19:08:30 -08005158 // /* HeapReference<Object> */ ref = *(obj + offset)
5159 Location no_index = Location::NoLocation();
5160 ScaleFactor no_scale_factor = TIMES_1;
5161 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5162 ref,
5163 obj,
5164 offset,
5165 no_index,
5166 no_scale_factor,
5167 temp,
5168 needs_null_check);
5169}
5170
5171void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5172 Location ref,
5173 GpuRegister obj,
5174 uint32_t data_offset,
5175 Location index,
5176 Location temp,
5177 bool needs_null_check) {
5178 DCHECK(kEmitCompilerReadBarrier);
5179 DCHECK(kUseBakerReadBarrier);
5180
5181 static_assert(
5182 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5183 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005184 ScaleFactor scale_factor = TIMES_4;
5185
5186 if (kBakerReadBarrierThunksEnableForArrays) {
5187 // Note that we do not actually check the value of `GetIsGcMarking()`
5188 // to decide whether to mark the loaded reference or not. Instead, we
5189 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5190 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5191 // vice versa.
5192 //
5193 // We use thunks for the slow path. That thunk checks the reference
5194 // and jumps to the entrypoint if needed. If the holder is not gray,
5195 // it issues a load-load memory barrier and returns to the original
5196 // reference load.
5197 //
5198 // temp = Thread::Current()->pReadBarrierMarkReg00
5199 // // AKA &art_quick_read_barrier_mark_introspection.
5200 // if (temp != nullptr) {
5201 // temp = &field_array_thunk<holder_reg>
5202 // temp()
5203 // }
5204 // not_gray_return_address:
5205 // // The element address is pre-calculated in the TMP register before the
5206 // // thunk invocation and the thunk benefits from it.
5207 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5208 // gray_return_address:
5209
5210 DCHECK(temp.IsInvalid());
5211 DCHECK(index.IsValid());
5212 const int32_t entry_point_offset =
5213 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5214 // We will not do the explicit null check in the thunk as some form of a null check
5215 // must've been done earlier.
5216 DCHECK(!needs_null_check);
5217 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5218 // Loading the entrypoint does not require a load acquire since it is only changed when
5219 // threads are suspended or running a checkpoint.
5220 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005221 Mips64Label skip_call;
5222 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005223 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5224 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5225 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5226 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005227 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005228 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5229 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5230 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5231 __ MaybeUnpoisonHeapReference(ref_reg);
5232 return;
5233 }
5234
Alexey Frunze15958152017-02-09 19:08:30 -08005235 // /* HeapReference<Object> */ ref =
5236 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005237 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5238 ref,
5239 obj,
5240 data_offset,
5241 index,
5242 scale_factor,
5243 temp,
5244 needs_null_check);
5245}
5246
5247void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5248 Location ref,
5249 GpuRegister obj,
5250 uint32_t offset,
5251 Location index,
5252 ScaleFactor scale_factor,
5253 Location temp,
5254 bool needs_null_check,
5255 bool always_update_field) {
5256 DCHECK(kEmitCompilerReadBarrier);
5257 DCHECK(kUseBakerReadBarrier);
5258
5259 // In slow path based read barriers, the read barrier call is
5260 // inserted after the original load. However, in fast path based
5261 // Baker's read barriers, we need to perform the load of
5262 // mirror::Object::monitor_ *before* the original reference load.
5263 // This load-load ordering is required by the read barrier.
5264 // The fast path/slow path (for Baker's algorithm) should look like:
5265 //
5266 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5267 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5268 // HeapReference<Object> ref = *src; // Original reference load.
5269 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5270 // if (is_gray) {
5271 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5272 // }
5273 //
5274 // Note: the original implementation in ReadBarrier::Barrier is
5275 // slightly more complex as it performs additional checks that we do
5276 // not do here for performance reasons.
5277
5278 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5279 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5280 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5281
5282 // /* int32_t */ monitor = obj->monitor_
5283 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5284 if (needs_null_check) {
5285 MaybeRecordImplicitNullCheck(instruction);
5286 }
5287 // /* LockWord */ lock_word = LockWord(monitor)
5288 static_assert(sizeof(LockWord) == sizeof(int32_t),
5289 "art::LockWord and int32_t have different sizes.");
5290
5291 __ Sync(0); // Barrier to prevent load-load reordering.
5292
5293 // The actual reference load.
5294 if (index.IsValid()) {
5295 // Load types involving an "index": ArrayGet,
5296 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5297 // intrinsics.
5298 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5299 if (index.IsConstant()) {
5300 size_t computed_offset =
5301 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5302 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5303 } else {
5304 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005305 if (scale_factor == TIMES_1) {
5306 __ Daddu(TMP, index_reg, obj);
5307 } else {
5308 __ Dlsa(TMP, index_reg, obj, scale_factor);
5309 }
Alexey Frunze15958152017-02-09 19:08:30 -08005310 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5311 }
5312 } else {
5313 // /* HeapReference<Object> */ ref = *(obj + offset)
5314 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5315 }
5316
5317 // Object* ref = ref_addr->AsMirrorPtr()
5318 __ MaybeUnpoisonHeapReference(ref_reg);
5319
5320 // Slow path marking the object `ref` when it is gray.
5321 SlowPathCodeMIPS64* slow_path;
5322 if (always_update_field) {
5323 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5324 // of the form `obj + field_offset`, where `obj` is a register and
5325 // `field_offset` is a register. Thus `offset` and `scale_factor`
5326 // above are expected to be null in this code path.
5327 DCHECK_EQ(offset, 0u);
5328 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005329 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005330 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5331 ref,
5332 obj,
5333 /* field_offset */ index,
5334 temp_reg);
5335 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005336 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005337 }
5338 AddSlowPath(slow_path);
5339
5340 // if (rb_state == ReadBarrier::GrayState())
5341 // ref = ReadBarrier::Mark(ref);
5342 // Given the numeric representation, it's enough to check the low bit of the
5343 // rb_state. We do that by shifting the bit into the sign bit (31) and
5344 // performing a branch on less than zero.
5345 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5346 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5347 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5348 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5349 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5350 __ Bind(slow_path->GetExitLabel());
5351}
5352
5353void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5354 Location out,
5355 Location ref,
5356 Location obj,
5357 uint32_t offset,
5358 Location index) {
5359 DCHECK(kEmitCompilerReadBarrier);
5360
5361 // Insert a slow path based read barrier *after* the reference load.
5362 //
5363 // If heap poisoning is enabled, the unpoisoning of the loaded
5364 // reference will be carried out by the runtime within the slow
5365 // path.
5366 //
5367 // Note that `ref` currently does not get unpoisoned (when heap
5368 // poisoning is enabled), which is alright as the `ref` argument is
5369 // not used by the artReadBarrierSlow entry point.
5370 //
5371 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005372 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005373 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5374 AddSlowPath(slow_path);
5375
5376 __ Bc(slow_path->GetEntryLabel());
5377 __ Bind(slow_path->GetExitLabel());
5378}
5379
5380void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5381 Location out,
5382 Location ref,
5383 Location obj,
5384 uint32_t offset,
5385 Location index) {
5386 if (kEmitCompilerReadBarrier) {
5387 // Baker's read barriers shall be handled by the fast path
5388 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5389 DCHECK(!kUseBakerReadBarrier);
5390 // If heap poisoning is enabled, unpoisoning will be taken care of
5391 // by the runtime within the slow path.
5392 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5393 } else if (kPoisonHeapReferences) {
5394 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5395 }
5396}
5397
5398void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5399 Location out,
5400 Location root) {
5401 DCHECK(kEmitCompilerReadBarrier);
5402
5403 // Insert a slow path based read barrier *after* the GC root load.
5404 //
5405 // Note that GC roots are not affected by heap poisoning, so we do
5406 // not need to do anything special for this here.
5407 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005408 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005409 AddSlowPath(slow_path);
5410
5411 __ Bc(slow_path->GetEntryLabel());
5412 __ Bind(slow_path->GetExitLabel());
5413}
5414
Alexey Frunze4dda3372015-06-01 18:31:49 -07005415void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005416 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5417 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005418 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005419 switch (type_check_kind) {
5420 case TypeCheckKind::kExactCheck:
5421 case TypeCheckKind::kAbstractClassCheck:
5422 case TypeCheckKind::kClassHierarchyCheck:
5423 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005424 call_kind =
5425 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005426 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005427 break;
5428 case TypeCheckKind::kArrayCheck:
5429 case TypeCheckKind::kUnresolvedCheck:
5430 case TypeCheckKind::kInterfaceCheck:
5431 call_kind = LocationSummary::kCallOnSlowPath;
5432 break;
5433 }
5434
Vladimir Markoca6fff82017-10-03 14:49:14 +01005435 LocationSummary* locations =
5436 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005437 if (baker_read_barrier_slow_path) {
5438 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5439 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005440 locations->SetInAt(0, Location::RequiresRegister());
5441 locations->SetInAt(1, Location::RequiresRegister());
5442 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005443 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005444 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005445 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005446}
5447
5448void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005449 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005450 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005451 Location obj_loc = locations->InAt(0);
5452 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005453 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005454 Location out_loc = locations->Out();
5455 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5456 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5457 DCHECK_LE(num_temps, 1u);
5458 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005459 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5460 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5461 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5462 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005463 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005464 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005465
5466 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005467 // Avoid this check if we know `obj` is not null.
5468 if (instruction->MustDoNullCheck()) {
5469 __ Move(out, ZERO);
5470 __ Beqzc(obj, &done);
5471 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005472
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005473 switch (type_check_kind) {
5474 case TypeCheckKind::kExactCheck: {
5475 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005476 GenerateReferenceLoadTwoRegisters(instruction,
5477 out_loc,
5478 obj_loc,
5479 class_offset,
5480 maybe_temp_loc,
5481 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005482 // Classes must be equal for the instanceof to succeed.
5483 __ Xor(out, out, cls);
5484 __ Sltiu(out, out, 1);
5485 break;
5486 }
5487
5488 case TypeCheckKind::kAbstractClassCheck: {
5489 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005490 GenerateReferenceLoadTwoRegisters(instruction,
5491 out_loc,
5492 obj_loc,
5493 class_offset,
5494 maybe_temp_loc,
5495 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005496 // If the class is abstract, we eagerly fetch the super class of the
5497 // object to avoid doing a comparison we know will fail.
5498 Mips64Label loop;
5499 __ Bind(&loop);
5500 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005501 GenerateReferenceLoadOneRegister(instruction,
5502 out_loc,
5503 super_offset,
5504 maybe_temp_loc,
5505 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005506 // If `out` is null, we use it for the result, and jump to `done`.
5507 __ Beqzc(out, &done);
5508 __ Bnec(out, cls, &loop);
5509 __ LoadConst32(out, 1);
5510 break;
5511 }
5512
5513 case TypeCheckKind::kClassHierarchyCheck: {
5514 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005515 GenerateReferenceLoadTwoRegisters(instruction,
5516 out_loc,
5517 obj_loc,
5518 class_offset,
5519 maybe_temp_loc,
5520 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005521 // Walk over the class hierarchy to find a match.
5522 Mips64Label loop, success;
5523 __ Bind(&loop);
5524 __ Beqc(out, cls, &success);
5525 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005526 GenerateReferenceLoadOneRegister(instruction,
5527 out_loc,
5528 super_offset,
5529 maybe_temp_loc,
5530 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005531 __ Bnezc(out, &loop);
5532 // If `out` is null, we use it for the result, and jump to `done`.
5533 __ Bc(&done);
5534 __ Bind(&success);
5535 __ LoadConst32(out, 1);
5536 break;
5537 }
5538
5539 case TypeCheckKind::kArrayObjectCheck: {
5540 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005541 GenerateReferenceLoadTwoRegisters(instruction,
5542 out_loc,
5543 obj_loc,
5544 class_offset,
5545 maybe_temp_loc,
5546 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005547 // Do an exact check.
5548 Mips64Label success;
5549 __ Beqc(out, cls, &success);
5550 // Otherwise, we need to check that the object's class is a non-primitive array.
5551 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005552 GenerateReferenceLoadOneRegister(instruction,
5553 out_loc,
5554 component_offset,
5555 maybe_temp_loc,
5556 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005557 // If `out` is null, we use it for the result, and jump to `done`.
5558 __ Beqzc(out, &done);
5559 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5560 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5561 __ Sltiu(out, out, 1);
5562 __ Bc(&done);
5563 __ Bind(&success);
5564 __ LoadConst32(out, 1);
5565 break;
5566 }
5567
5568 case TypeCheckKind::kArrayCheck: {
5569 // No read barrier since the slow path will retry upon failure.
5570 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005571 GenerateReferenceLoadTwoRegisters(instruction,
5572 out_loc,
5573 obj_loc,
5574 class_offset,
5575 maybe_temp_loc,
5576 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005577 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005578 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5579 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005580 codegen_->AddSlowPath(slow_path);
5581 __ Bnec(out, cls, slow_path->GetEntryLabel());
5582 __ LoadConst32(out, 1);
5583 break;
5584 }
5585
5586 case TypeCheckKind::kUnresolvedCheck:
5587 case TypeCheckKind::kInterfaceCheck: {
5588 // Note that we indeed only call on slow path, but we always go
5589 // into the slow path for the unresolved and interface check
5590 // cases.
5591 //
5592 // We cannot directly call the InstanceofNonTrivial runtime
5593 // entry point without resorting to a type checking slow path
5594 // here (i.e. by calling InvokeRuntime directly), as it would
5595 // require to assign fixed registers for the inputs of this
5596 // HInstanceOf instruction (following the runtime calling
5597 // convention), which might be cluttered by the potential first
5598 // read barrier emission at the beginning of this method.
5599 //
5600 // TODO: Introduce a new runtime entry point taking the object
5601 // to test (instead of its class) as argument, and let it deal
5602 // with the read barrier issues. This will let us refactor this
5603 // case of the `switch` code as it was previously (with a direct
5604 // call to the runtime not using a type checking slow path).
5605 // This should also be beneficial for the other cases above.
5606 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005607 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5608 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005609 codegen_->AddSlowPath(slow_path);
5610 __ Bc(slow_path->GetEntryLabel());
5611 break;
5612 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005613 }
5614
5615 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005616
5617 if (slow_path != nullptr) {
5618 __ Bind(slow_path->GetExitLabel());
5619 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620}
5621
5622void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005623 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005624 locations->SetOut(Location::ConstantLocation(constant));
5625}
5626
5627void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5628 // Will be generated at use site.
5629}
5630
5631void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005632 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005633 locations->SetOut(Location::ConstantLocation(constant));
5634}
5635
5636void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5637 // Will be generated at use site.
5638}
5639
Calin Juravle175dc732015-08-25 15:42:32 +01005640void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5641 // The trampoline uses the same calling convention as dex calling conventions,
5642 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5643 // the method_idx.
5644 HandleInvoke(invoke);
5645}
5646
5647void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5648 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5649}
5650
Alexey Frunze4dda3372015-06-01 18:31:49 -07005651void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5652 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5653 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5654}
5655
5656void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5657 HandleInvoke(invoke);
5658 // The register T0 is required to be used for the hidden argument in
5659 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5660 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5661}
5662
5663void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5664 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5665 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005666 Location receiver = invoke->GetLocations()->InAt(0);
5667 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005668 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005669
5670 // Set the hidden argument.
5671 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5672 invoke->GetDexMethodIndex());
5673
5674 // temp = object->GetClass();
5675 if (receiver.IsStackSlot()) {
5676 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5677 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5678 } else {
5679 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5680 }
5681 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005682 // Instead of simply (possibly) unpoisoning `temp` here, we should
5683 // emit a read barrier for the previous class reference load.
5684 // However this is not required in practice, as this is an
5685 // intermediate/temporary reference and because the current
5686 // concurrent copying collector keeps the from-space memory
5687 // intact/accessible until the end of the marking phase (the
5688 // concurrent copying collector may not in the future).
5689 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005690 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5691 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5692 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005693 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005694 // temp = temp->GetImtEntryAt(method_offset);
5695 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5696 // T9 = temp->GetEntryPoint();
5697 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5698 // T9();
5699 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005700 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005701 DCHECK(!codegen_->IsLeafMethod());
5702 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5703}
5704
5705void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005706 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5707 if (intrinsic.TryDispatch(invoke)) {
5708 return;
5709 }
5710
Alexey Frunze4dda3372015-06-01 18:31:49 -07005711 HandleInvoke(invoke);
5712}
5713
5714void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005715 // Explicit clinit checks triggered by static invokes must have been pruned by
5716 // art::PrepareForRegisterAllocation.
5717 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005718
Chris Larsen3039e382015-08-26 07:54:08 -07005719 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5720 if (intrinsic.TryDispatch(invoke)) {
5721 return;
5722 }
5723
Alexey Frunze4dda3372015-06-01 18:31:49 -07005724 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005725}
5726
Orion Hodsonac141392017-01-13 11:53:47 +00005727void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5728 HandleInvoke(invoke);
5729}
5730
5731void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5732 codegen_->GenerateInvokePolymorphicCall(invoke);
5733}
5734
Chris Larsen3039e382015-08-26 07:54:08 -07005735static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005736 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005737 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5738 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005739 return true;
5740 }
5741 return false;
5742}
5743
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005744HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005745 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005746 bool fallback_load = false;
5747 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005748 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005749 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005750 case HLoadString::LoadKind::kBssEntry:
5751 DCHECK(!Runtime::Current()->UseJitCompilation());
5752 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005753 case HLoadString::LoadKind::kJitTableAddress:
5754 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005755 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005756 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005757 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005758 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005759 }
5760 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005761 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005762 }
5763 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005764}
5765
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005766HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5767 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005768 bool fallback_load = false;
5769 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005770 case HLoadClass::LoadKind::kInvalid:
5771 LOG(FATAL) << "UNREACHABLE";
5772 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005773 case HLoadClass::LoadKind::kReferrersClass:
5774 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005775 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005776 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005777 case HLoadClass::LoadKind::kBssEntry:
5778 DCHECK(!Runtime::Current()->UseJitCompilation());
5779 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005780 case HLoadClass::LoadKind::kJitTableAddress:
5781 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005782 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005783 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005784 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005785 break;
5786 }
5787 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005788 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005789 }
5790 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005791}
5792
Vladimir Markodc151b22015-10-15 18:02:30 +01005793HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5794 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005795 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005796 // On MIPS64 we support all dispatch types.
5797 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005798}
5799
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005800void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5801 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005802 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005803 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005804 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5805 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5806
Alexey Frunze19f6c692016-11-30 19:19:55 -08005807 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005808 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005809 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005810 uint32_t offset =
5811 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005812 __ LoadFromOffset(kLoadDoubleword,
5813 temp.AsRegister<GpuRegister>(),
5814 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005815 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005816 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005817 }
Vladimir Marko58155012015-08-19 12:49:41 +00005818 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005819 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005820 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005821 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5822 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005823 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005824 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005825 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5826 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5827 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005828 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5829 break;
5830 }
Vladimir Marko58155012015-08-19 12:49:41 +00005831 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005832 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5833 kLoadDoubleword,
5834 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005835 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005836 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005837 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005838 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005839 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5840 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5841 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005842 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5843 break;
5844 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005845 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5846 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5847 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005848 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005849 }
5850
Alexey Frunze19f6c692016-11-30 19:19:55 -08005851 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005852 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005853 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005854 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005855 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5856 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5857 __ LoadFromOffset(kLoadDoubleword,
5858 T9,
5859 callee_method.AsRegister<GpuRegister>(),
5860 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005861 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005862 // T9()
5863 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005864 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005865 break;
5866 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005867 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5868
Alexey Frunze4dda3372015-06-01 18:31:49 -07005869 DCHECK(!IsLeafMethod());
5870}
5871
5872void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005873 // Explicit clinit checks triggered by static invokes must have been pruned by
5874 // art::PrepareForRegisterAllocation.
5875 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005876
5877 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5878 return;
5879 }
5880
5881 LocationSummary* locations = invoke->GetLocations();
5882 codegen_->GenerateStaticOrDirectCall(invoke,
5883 locations->HasTemps()
5884 ? locations->GetTemp(0)
5885 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005886}
5887
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005888void CodeGeneratorMIPS64::GenerateVirtualCall(
5889 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005890 // Use the calling convention instead of the location of the receiver, as
5891 // intrinsics may have put the receiver in a different register. In the intrinsics
5892 // slow path, the arguments have been moved to the right place, so here we are
5893 // guaranteed that the receiver is the first register of the calling convention.
5894 InvokeDexCallingConvention calling_convention;
5895 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5896
Alexey Frunze53afca12015-11-05 16:34:23 -08005897 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005898 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5899 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5900 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005901 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005902
5903 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005904 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005905 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005906 // Instead of simply (possibly) unpoisoning `temp` here, we should
5907 // emit a read barrier for the previous class reference load.
5908 // However this is not required in practice, as this is an
5909 // intermediate/temporary reference and because the current
5910 // concurrent copying collector keeps the from-space memory
5911 // intact/accessible until the end of the marking phase (the
5912 // concurrent copying collector may not in the future).
5913 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005914 // temp = temp->GetMethodAt(method_offset);
5915 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5916 // T9 = temp->GetEntryPoint();
5917 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5918 // T9();
5919 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005920 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005921 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005922}
5923
5924void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5925 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5926 return;
5927 }
5928
5929 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005930 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005931}
5932
5933void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005934 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005935 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005936 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005937 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5938 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005939 return;
5940 }
Vladimir Marko41559982017-01-06 14:04:23 +00005941 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005942
Alexey Frunze15958152017-02-09 19:08:30 -08005943 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5944 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005945 ? LocationSummary::kCallOnSlowPath
5946 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01005947 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005948 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5949 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5950 }
Vladimir Marko41559982017-01-06 14:04:23 +00005951 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005952 locations->SetInAt(0, Location::RequiresRegister());
5953 }
5954 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005955 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5956 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5957 // Rely on the type resolution or initialization and marking to save everything we need.
5958 RegisterSet caller_saves = RegisterSet::Empty();
5959 InvokeRuntimeCallingConvention calling_convention;
5960 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5961 locations->SetCustomSlowPathCallerSaves(caller_saves);
5962 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005963 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07005964 }
5965 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005966}
5967
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005968// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5969// move.
5970void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005971 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005972 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005973 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005974 return;
5975 }
Vladimir Marko41559982017-01-06 14:04:23 +00005976 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005977
Vladimir Marko41559982017-01-06 14:04:23 +00005978 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005979 Location out_loc = locations->Out();
5980 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5981 GpuRegister current_method_reg = ZERO;
5982 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005983 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005984 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5985 }
5986
Alexey Frunze15958152017-02-09 19:08:30 -08005987 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5988 ? kWithoutReadBarrier
5989 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005990 bool generate_null_check = false;
5991 switch (load_kind) {
5992 case HLoadClass::LoadKind::kReferrersClass:
5993 DCHECK(!cls->CanCallRuntime());
5994 DCHECK(!cls->MustGenerateClinitCheck());
5995 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5996 GenerateGcRootFieldLoad(cls,
5997 out_loc,
5998 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005999 ArtMethod::DeclaringClassOffset().Int32Value(),
6000 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006001 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006002 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006003 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006004 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006005 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006006 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006007 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6008 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6009 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6011 break;
6012 }
6013 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006014 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006015 uint32_t address = dchecked_integral_cast<uint32_t>(
6016 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6017 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006018 __ LoadLiteral(out,
6019 kLoadUnsignedWord,
6020 codegen_->DeduplicateBootImageAddressLiteral(address));
6021 break;
6022 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006023 case HLoadClass::LoadKind::kBootImageClassTable: {
6024 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6025 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6026 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6027 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6028 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6029 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6030 __ Lwu(out, AT, /* placeholder */ 0x5678);
6031 // Extract the reference from the slot data, i.e. clear the hash bits.
6032 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6033 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6034 if (masked_hash != 0) {
6035 __ Daddiu(out, out, -masked_hash);
6036 }
6037 break;
6038 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006039 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006040 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6041 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006042 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6043 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006044 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006045 GenerateGcRootFieldLoad(cls,
6046 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006047 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006048 /* placeholder */ 0x5678,
6049 read_barrier_option,
6050 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006051 generate_null_check = true;
6052 break;
6053 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006054 case HLoadClass::LoadKind::kJitTableAddress:
6055 __ LoadLiteral(out,
6056 kLoadUnsignedWord,
6057 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6058 cls->GetTypeIndex(),
6059 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006060 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006061 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006062 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006063 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006064 LOG(FATAL) << "UNREACHABLE";
6065 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006066 }
6067
6068 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6069 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006070 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006071 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006072 codegen_->AddSlowPath(slow_path);
6073 if (generate_null_check) {
6074 __ Beqzc(out, slow_path->GetEntryLabel());
6075 }
6076 if (cls->MustGenerateClinitCheck()) {
6077 GenerateClassInitializationCheck(slow_path, out);
6078 } else {
6079 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006080 }
6081 }
6082}
6083
David Brazdilcb1c0552015-08-04 16:22:25 +01006084static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006085 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006086}
6087
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6089 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006090 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006091 locations->SetOut(Location::RequiresRegister());
6092}
6093
6094void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6095 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006096 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6097}
6098
6099void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006100 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006101}
6102
6103void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6104 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006105}
6106
Alexey Frunze4dda3372015-06-01 18:31:49 -07006107void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006108 HLoadString::LoadKind load_kind = load->GetLoadKind();
6109 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006110 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006111 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006112 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006113 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006114 } else {
6115 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006116 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6117 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6118 // Rely on the pResolveString and marking to save everything we need.
6119 RegisterSet caller_saves = RegisterSet::Empty();
6120 InvokeRuntimeCallingConvention calling_convention;
6121 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6122 locations->SetCustomSlowPathCallerSaves(caller_saves);
6123 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006124 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006125 }
6126 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006128}
6129
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006130// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6131// move.
6132void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006133 HLoadString::LoadKind load_kind = load->GetLoadKind();
6134 LocationSummary* locations = load->GetLocations();
6135 Location out_loc = locations->Out();
6136 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6137
6138 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006139 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6140 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006141 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006142 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006143 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6144 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6145 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006146 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006147 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006148 }
6149 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006150 uint32_t address = dchecked_integral_cast<uint32_t>(
6151 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6152 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006153 __ LoadLiteral(out,
6154 kLoadUnsignedWord,
6155 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006156 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006157 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006158 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006159 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006160 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006161 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006162 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6163 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006164 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6165 __ Lwu(out, AT, /* placeholder */ 0x5678);
6166 return;
6167 }
6168 case HLoadString::LoadKind::kBssEntry: {
6169 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6170 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6171 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6172 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6173 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006174 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006175 GenerateGcRootFieldLoad(load,
6176 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006177 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006178 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006179 kCompilerReadBarrierOption,
6180 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006181 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006182 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006183 codegen_->AddSlowPath(slow_path);
6184 __ Beqzc(out, slow_path->GetEntryLabel());
6185 __ Bind(slow_path->GetExitLabel());
6186 return;
6187 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006188 case HLoadString::LoadKind::kJitTableAddress:
6189 __ LoadLiteral(out,
6190 kLoadUnsignedWord,
6191 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6192 load->GetStringIndex(),
6193 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006194 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006195 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006196 default:
6197 break;
6198 }
6199
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006200 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006201 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006202 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006203 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006204 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6205 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6206 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006207}
6208
Alexey Frunze4dda3372015-06-01 18:31:49 -07006209void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006210 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006211 locations->SetOut(Location::ConstantLocation(constant));
6212}
6213
6214void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6215 // Will be generated at use site.
6216}
6217
6218void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006219 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6220 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006221 InvokeRuntimeCallingConvention calling_convention;
6222 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6223}
6224
6225void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006226 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006227 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006228 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006229 if (instruction->IsEnter()) {
6230 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6231 } else {
6232 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6233 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006234}
6235
6236void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6237 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006238 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006239 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 case DataType::Type::kInt32:
6241 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006242 locations->SetInAt(0, Location::RequiresRegister());
6243 locations->SetInAt(1, Location::RequiresRegister());
6244 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6245 break;
6246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 case DataType::Type::kFloat32:
6248 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006249 locations->SetInAt(0, Location::RequiresFpuRegister());
6250 locations->SetInAt(1, Location::RequiresFpuRegister());
6251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6252 break;
6253
6254 default:
6255 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6256 }
6257}
6258
6259void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006261 LocationSummary* locations = instruction->GetLocations();
6262
6263 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kInt32:
6265 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006266 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6267 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6268 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006270 __ MulR6(dst, lhs, rhs);
6271 else
6272 __ Dmul(dst, lhs, rhs);
6273 break;
6274 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006275 case DataType::Type::kFloat32:
6276 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006277 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6278 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6279 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006281 __ MulS(dst, lhs, rhs);
6282 else
6283 __ MulD(dst, lhs, rhs);
6284 break;
6285 }
6286 default:
6287 LOG(FATAL) << "Unexpected mul type " << type;
6288 }
6289}
6290
6291void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6292 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006293 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006294 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 case DataType::Type::kInt32:
6296 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006297 locations->SetInAt(0, Location::RequiresRegister());
6298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6299 break;
6300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kFloat32:
6302 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006303 locations->SetInAt(0, Location::RequiresFpuRegister());
6304 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6305 break;
6306
6307 default:
6308 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6309 }
6310}
6311
6312void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006313 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006314 LocationSummary* locations = instruction->GetLocations();
6315
6316 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 case DataType::Type::kInt32:
6318 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006319 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6320 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006322 __ Subu(dst, ZERO, src);
6323 else
6324 __ Dsubu(dst, ZERO, src);
6325 break;
6326 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kFloat32:
6328 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006329 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6330 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006332 __ NegS(dst, src);
6333 else
6334 __ NegD(dst, src);
6335 break;
6336 }
6337 default:
6338 LOG(FATAL) << "Unexpected neg type " << type;
6339 }
6340}
6341
6342void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006343 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6344 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006345 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006347 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6348 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006349}
6350
6351void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006352 // Note: if heap poisoning is enabled, the entry point takes care
6353 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006354 QuickEntrypointEnum entrypoint =
6355 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6356 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006357 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006358 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006359}
6360
6361void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006362 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6363 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006364 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006365 if (instruction->IsStringAlloc()) {
6366 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6367 } else {
6368 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006369 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006370 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006371}
6372
6373void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006374 // Note: if heap poisoning is enabled, the entry point takes care
6375 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006376 if (instruction->IsStringAlloc()) {
6377 // String is allocated through StringFactory. Call NewEmptyString entry point.
6378 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006379 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006380 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006381 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6382 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6383 __ Jalr(T9);
6384 __ Nop();
6385 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6386 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006387 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006388 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006389 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006390}
6391
6392void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006393 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006394 locations->SetInAt(0, Location::RequiresRegister());
6395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6396}
6397
6398void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006400 LocationSummary* locations = instruction->GetLocations();
6401
6402 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006403 case DataType::Type::kInt32:
6404 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006405 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6406 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6407 __ Nor(dst, src, ZERO);
6408 break;
6409 }
6410
6411 default:
6412 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6413 }
6414}
6415
6416void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006417 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006418 locations->SetInAt(0, Location::RequiresRegister());
6419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6420}
6421
6422void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6423 LocationSummary* locations = instruction->GetLocations();
6424 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6425 locations->InAt(0).AsRegister<GpuRegister>(),
6426 1);
6427}
6428
6429void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006430 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6431 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006432}
6433
Calin Juravle2ae48182016-03-16 14:05:09 +00006434void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6435 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006436 return;
6437 }
6438 Location obj = instruction->GetLocations()->InAt(0);
6439
6440 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006441 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006442}
6443
Calin Juravle2ae48182016-03-16 14:05:09 +00006444void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006445 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006446 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006447 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006448
6449 Location obj = instruction->GetLocations()->InAt(0);
6450
6451 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6452}
6453
6454void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006455 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006456}
6457
6458void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6459 HandleBinaryOp(instruction);
6460}
6461
6462void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6463 HandleBinaryOp(instruction);
6464}
6465
6466void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6467 LOG(FATAL) << "Unreachable";
6468}
6469
6470void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006471 if (instruction->GetNext()->IsSuspendCheck() &&
6472 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6473 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6474 // The back edge will generate the suspend check.
6475 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6476 }
6477
Alexey Frunze4dda3372015-06-01 18:31:49 -07006478 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6479}
6480
6481void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006482 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006483 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6484 if (location.IsStackSlot()) {
6485 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6486 } else if (location.IsDoubleStackSlot()) {
6487 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6488 }
6489 locations->SetOut(location);
6490}
6491
6492void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6493 ATTRIBUTE_UNUSED) {
6494 // Nothing to do, the parameter is already at its location.
6495}
6496
6497void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6498 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006499 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006500 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6501}
6502
6503void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6504 ATTRIBUTE_UNUSED) {
6505 // Nothing to do, the method is already at its location.
6506}
6507
6508void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006509 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006510 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006511 locations->SetInAt(i, Location::Any());
6512 }
6513 locations->SetOut(Location::Any());
6514}
6515
6516void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6517 LOG(FATAL) << "Unreachable";
6518}
6519
6520void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006521 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006522 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006523 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6524 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006526
6527 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006528 case DataType::Type::kInt32:
6529 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006530 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006531 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6533 break;
6534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 case DataType::Type::kFloat32:
6536 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006537 InvokeRuntimeCallingConvention calling_convention;
6538 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6539 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6540 locations->SetOut(calling_convention.GetReturnLocation(type));
6541 break;
6542 }
6543
6544 default:
6545 LOG(FATAL) << "Unexpected rem type " << type;
6546 }
6547}
6548
6549void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006550 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006551
6552 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006553 case DataType::Type::kInt32:
6554 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006555 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006556 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006557
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006558 case DataType::Type::kFloat32:
6559 case DataType::Type::kFloat64: {
6560 QuickEntrypointEnum entrypoint =
6561 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006562 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006563 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006564 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6565 } else {
6566 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6567 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568 break;
6569 }
6570 default:
6571 LOG(FATAL) << "Unexpected rem type " << type;
6572 }
6573}
6574
Igor Murashkind01745e2017-04-05 16:40:31 -07006575void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6576 constructor_fence->SetLocations(nullptr);
6577}
6578
6579void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6580 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6581 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6582}
6583
Alexey Frunze4dda3372015-06-01 18:31:49 -07006584void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6585 memory_barrier->SetLocations(nullptr);
6586}
6587
6588void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6589 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6590}
6591
6592void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006595 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6596}
6597
6598void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6599 codegen_->GenerateFrameExit();
6600}
6601
6602void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6603 ret->SetLocations(nullptr);
6604}
6605
6606void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6607 codegen_->GenerateFrameExit();
6608}
6609
Alexey Frunze92d90602015-12-18 18:16:36 -08006610void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6611 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006612}
6613
Alexey Frunze92d90602015-12-18 18:16:36 -08006614void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6615 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006616}
6617
Alexey Frunze4dda3372015-06-01 18:31:49 -07006618void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6619 HandleShift(shl);
6620}
6621
6622void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6623 HandleShift(shl);
6624}
6625
6626void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6627 HandleShift(shr);
6628}
6629
6630void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6631 HandleShift(shr);
6632}
6633
Alexey Frunze4dda3372015-06-01 18:31:49 -07006634void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6635 HandleBinaryOp(instruction);
6636}
6637
6638void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6639 HandleBinaryOp(instruction);
6640}
6641
6642void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6643 HandleFieldGet(instruction, instruction->GetFieldInfo());
6644}
6645
6646void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6647 HandleFieldGet(instruction, instruction->GetFieldInfo());
6648}
6649
6650void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6651 HandleFieldSet(instruction, instruction->GetFieldInfo());
6652}
6653
6654void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006655 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006656}
6657
Calin Juravlee460d1d2015-09-29 04:52:17 +01006658void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6659 HUnresolvedInstanceFieldGet* instruction) {
6660 FieldAccessCallingConventionMIPS64 calling_convention;
6661 codegen_->CreateUnresolvedFieldLocationSummary(
6662 instruction, instruction->GetFieldType(), calling_convention);
6663}
6664
6665void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6666 HUnresolvedInstanceFieldGet* instruction) {
6667 FieldAccessCallingConventionMIPS64 calling_convention;
6668 codegen_->GenerateUnresolvedFieldAccess(instruction,
6669 instruction->GetFieldType(),
6670 instruction->GetFieldIndex(),
6671 instruction->GetDexPc(),
6672 calling_convention);
6673}
6674
6675void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6676 HUnresolvedInstanceFieldSet* instruction) {
6677 FieldAccessCallingConventionMIPS64 calling_convention;
6678 codegen_->CreateUnresolvedFieldLocationSummary(
6679 instruction, instruction->GetFieldType(), calling_convention);
6680}
6681
6682void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6683 HUnresolvedInstanceFieldSet* instruction) {
6684 FieldAccessCallingConventionMIPS64 calling_convention;
6685 codegen_->GenerateUnresolvedFieldAccess(instruction,
6686 instruction->GetFieldType(),
6687 instruction->GetFieldIndex(),
6688 instruction->GetDexPc(),
6689 calling_convention);
6690}
6691
6692void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6693 HUnresolvedStaticFieldGet* instruction) {
6694 FieldAccessCallingConventionMIPS64 calling_convention;
6695 codegen_->CreateUnresolvedFieldLocationSummary(
6696 instruction, instruction->GetFieldType(), calling_convention);
6697}
6698
6699void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6700 HUnresolvedStaticFieldGet* instruction) {
6701 FieldAccessCallingConventionMIPS64 calling_convention;
6702 codegen_->GenerateUnresolvedFieldAccess(instruction,
6703 instruction->GetFieldType(),
6704 instruction->GetFieldIndex(),
6705 instruction->GetDexPc(),
6706 calling_convention);
6707}
6708
6709void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6710 HUnresolvedStaticFieldSet* instruction) {
6711 FieldAccessCallingConventionMIPS64 calling_convention;
6712 codegen_->CreateUnresolvedFieldLocationSummary(
6713 instruction, instruction->GetFieldType(), calling_convention);
6714}
6715
6716void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6717 HUnresolvedStaticFieldSet* instruction) {
6718 FieldAccessCallingConventionMIPS64 calling_convention;
6719 codegen_->GenerateUnresolvedFieldAccess(instruction,
6720 instruction->GetFieldType(),
6721 instruction->GetFieldIndex(),
6722 instruction->GetDexPc(),
6723 calling_convention);
6724}
6725
Alexey Frunze4dda3372015-06-01 18:31:49 -07006726void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006727 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6728 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006729 // In suspend check slow path, usually there are no caller-save registers at all.
6730 // If SIMD instructions are present, however, we force spilling all live SIMD
6731 // registers in full width (since the runtime only saves/restores lower part).
6732 locations->SetCustomSlowPathCallerSaves(
6733 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006734}
6735
6736void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6737 HBasicBlock* block = instruction->GetBlock();
6738 if (block->GetLoopInformation() != nullptr) {
6739 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6740 // The back edge will generate the suspend check.
6741 return;
6742 }
6743 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6744 // The goto will generate the suspend check.
6745 return;
6746 }
6747 GenerateSuspendCheck(instruction, nullptr);
6748}
6749
Alexey Frunze4dda3372015-06-01 18:31:49 -07006750void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006751 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6752 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006753 InvokeRuntimeCallingConvention calling_convention;
6754 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6755}
6756
6757void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006758 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006759 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6760}
6761
6762void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006763 DataType::Type input_type = conversion->GetInputType();
6764 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006765 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6766 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006768 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6769 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006770 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6771 }
6772
Vladimir Markoca6fff82017-10-03 14:49:14 +01006773 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006775 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006776 locations->SetInAt(0, Location::RequiresFpuRegister());
6777 } else {
6778 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006779 }
6780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006782 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006783 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006784 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006785 }
6786}
6787
6788void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6789 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006790 DataType::Type result_type = conversion->GetResultType();
6791 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006792
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006793 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6794 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006796 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006797 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6798 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6799
6800 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006801 case DataType::Type::kUint8:
6802 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006803 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006804 case DataType::Type::kInt8:
6805 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006806 // Type conversion from long to types narrower than int is a result of code
6807 // transformations. To avoid unpredictable results for SEB and SEH, we first
6808 // need to sign-extend the low 32-bit value into bits 32 through 63.
6809 __ Sll(dst, src, 0);
6810 __ Seb(dst, dst);
6811 } else {
6812 __ Seb(dst, src);
6813 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006814 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006815 case DataType::Type::kUint16:
6816 __ Andi(dst, src, 0xFFFF);
6817 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006818 case DataType::Type::kInt16:
6819 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006820 // Type conversion from long to types narrower than int is a result of code
6821 // transformations. To avoid unpredictable results for SEB and SEH, we first
6822 // need to sign-extend the low 32-bit value into bits 32 through 63.
6823 __ Sll(dst, src, 0);
6824 __ Seh(dst, dst);
6825 } else {
6826 __ Seh(dst, src);
6827 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006828 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006829 case DataType::Type::kInt32:
6830 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006831 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6832 // conversions, except when the input and output registers are the same and we are not
6833 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006834 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006835 __ Sll(dst, src, 0);
6836 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006837 break;
6838
6839 default:
6840 LOG(FATAL) << "Unexpected type conversion from " << input_type
6841 << " to " << result_type;
6842 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006844 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6845 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006846 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006847 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006848 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006849 __ Cvtsl(dst, FTMP);
6850 } else {
6851 __ Cvtdl(dst, FTMP);
6852 }
6853 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006854 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006855 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006856 __ Cvtsw(dst, FTMP);
6857 } else {
6858 __ Cvtdw(dst, FTMP);
6859 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006860 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6862 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006863 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6864 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006866 if (result_type == DataType::Type::kInt64) {
6867 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006868 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006869 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006870 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006871 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006872 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006873 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006874 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006875 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006876 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006877 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006878 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006879 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006880 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006881 } else if (DataType::IsFloatingPointType(result_type) &&
6882 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006883 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6884 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006885 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006886 __ Cvtsd(dst, src);
6887 } else {
6888 __ Cvtds(dst, src);
6889 }
6890 } else {
6891 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6892 << " to " << result_type;
6893 }
6894}
6895
6896void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6897 HandleShift(ushr);
6898}
6899
6900void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6901 HandleShift(ushr);
6902}
6903
6904void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6905 HandleBinaryOp(instruction);
6906}
6907
6908void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6909 HandleBinaryOp(instruction);
6910}
6911
6912void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6913 // Nothing to do, this should be removed during prepare for register allocator.
6914 LOG(FATAL) << "Unreachable";
6915}
6916
6917void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6918 // Nothing to do, this should be removed during prepare for register allocator.
6919 LOG(FATAL) << "Unreachable";
6920}
6921
6922void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006923 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006924}
6925
6926void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006927 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006928}
6929
6930void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006931 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006932}
6933
6934void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006935 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006936}
6937
6938void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006939 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006940}
6941
6942void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006943 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006944}
6945
6946void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006947 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006948}
6949
6950void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006951 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006952}
6953
6954void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006955 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006956}
6957
6958void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006959 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006960}
6961
6962void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006963 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006964}
6965
6966void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006967 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006968}
6969
Aart Bike9f37602015-10-09 11:15:55 -07006970void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006971 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006972}
6973
6974void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006975 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006976}
6977
6978void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006979 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006980}
6981
6982void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006983 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006984}
6985
6986void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006987 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006988}
6989
6990void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006991 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006992}
6993
6994void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006996}
6997
6998void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007000}
7001
Mark Mendellfe57faa2015-09-18 09:26:15 -04007002// Simple implementation of packed switch - generate cascaded compare/jumps.
7003void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7004 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007005 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007006 locations->SetInAt(0, Location::RequiresRegister());
7007}
7008
Alexey Frunze0960ac52016-12-20 17:24:59 -08007009void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7010 int32_t lower_bound,
7011 uint32_t num_entries,
7012 HBasicBlock* switch_block,
7013 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007014 // Create a set of compare/jumps.
7015 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007016 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007017 // Jump to default if index is negative
7018 // Note: We don't check the case that index is positive while value < lower_bound, because in
7019 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7020 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7021
Alexey Frunze0960ac52016-12-20 17:24:59 -08007022 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007023 // Jump to successors[0] if value == lower_bound.
7024 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7025 int32_t last_index = 0;
7026 for (; num_entries - last_index > 2; last_index += 2) {
7027 __ Addiu(temp_reg, temp_reg, -2);
7028 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7029 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7030 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7031 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7032 }
7033 if (num_entries - last_index == 2) {
7034 // The last missing case_value.
7035 __ Addiu(temp_reg, temp_reg, -1);
7036 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007037 }
7038
7039 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007040 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007041 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007042 }
7043}
7044
Alexey Frunze0960ac52016-12-20 17:24:59 -08007045void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7046 int32_t lower_bound,
7047 uint32_t num_entries,
7048 HBasicBlock* switch_block,
7049 HBasicBlock* default_block) {
7050 // Create a jump table.
7051 std::vector<Mips64Label*> labels(num_entries);
7052 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7053 for (uint32_t i = 0; i < num_entries; i++) {
7054 labels[i] = codegen_->GetLabelOf(successors[i]);
7055 }
7056 JumpTable* table = __ CreateJumpTable(std::move(labels));
7057
7058 // Is the value in range?
7059 __ Addiu32(TMP, value_reg, -lower_bound);
7060 __ LoadConst32(AT, num_entries);
7061 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7062
7063 // We are in the range of the table.
7064 // Load the target address from the jump table, indexing by the value.
7065 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007066 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007067 __ Lw(TMP, TMP, 0);
7068 // Compute the absolute target address by adding the table start address
7069 // (the table contains offsets to targets relative to its start).
7070 __ Daddu(TMP, TMP, AT);
7071 // And jump.
7072 __ Jr(TMP);
7073 __ Nop();
7074}
7075
7076void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7077 int32_t lower_bound = switch_instr->GetStartValue();
7078 uint32_t num_entries = switch_instr->GetNumEntries();
7079 LocationSummary* locations = switch_instr->GetLocations();
7080 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7081 HBasicBlock* switch_block = switch_instr->GetBlock();
7082 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7083
7084 if (num_entries > kPackedSwitchJumpTableThreshold) {
7085 GenTableBasedPackedSwitch(value_reg,
7086 lower_bound,
7087 num_entries,
7088 switch_block,
7089 default_block);
7090 } else {
7091 GenPackedSwitchWithCompares(value_reg,
7092 lower_bound,
7093 num_entries,
7094 switch_block,
7095 default_block);
7096 }
7097}
7098
Chris Larsenc9905a62017-03-13 17:06:18 -07007099void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007101 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007102 locations->SetInAt(0, Location::RequiresRegister());
7103 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007104}
7105
Chris Larsenc9905a62017-03-13 17:06:18 -07007106void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7107 LocationSummary* locations = instruction->GetLocations();
7108 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7109 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7110 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7111 __ LoadFromOffset(kLoadDoubleword,
7112 locations->Out().AsRegister<GpuRegister>(),
7113 locations->InAt(0).AsRegister<GpuRegister>(),
7114 method_offset);
7115 } else {
7116 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7117 instruction->GetIndex(), kMips64PointerSize));
7118 __ LoadFromOffset(kLoadDoubleword,
7119 locations->Out().AsRegister<GpuRegister>(),
7120 locations->InAt(0).AsRegister<GpuRegister>(),
7121 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7122 __ LoadFromOffset(kLoadDoubleword,
7123 locations->Out().AsRegister<GpuRegister>(),
7124 locations->Out().AsRegister<GpuRegister>(),
7125 method_offset);
7126 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007127}
7128
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007129void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7130 ATTRIBUTE_UNUSED) {
7131 LOG(FATAL) << "Unreachable";
7132}
7133
7134void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7135 ATTRIBUTE_UNUSED) {
7136 LOG(FATAL) << "Unreachable";
7137}
7138
Alexey Frunze4dda3372015-06-01 18:31:49 -07007139} // namespace mips64
7140} // namespace art