blob: 22989c8283c5d066b16917552cd3fe16423abf4f [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);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001797 DCHECK(instruction->IsAdd() || instruction->IsSub());
1798 bool single_use = right->GetUses().HasExactlyOneElement();
1799 if (instruction->IsSub()) {
1800 if (!(type == DataType::Type::kInt32 && imm == INT32_MIN)) {
1801 imm = -imm;
1802 }
1803 }
1804 if (type == DataType::Type::kInt32) {
1805 can_use_imm = IsInt<16>(imm) || (Low16Bits(imm) == 0) || single_use;
1806 } else {
1807 can_use_imm = IsInt<16>(imm) || (IsInt<32>(imm) && (Low16Bits(imm) == 0)) || single_use;
1808 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 }
1810 }
1811 if (can_use_imm)
1812 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1813 else
1814 locations->SetInAt(1, Location::RequiresRegister());
1815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1816 }
1817 break;
1818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 case DataType::Type::kFloat32:
1820 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821 locations->SetInAt(0, Location::RequiresFpuRegister());
1822 locations->SetInAt(1, Location::RequiresFpuRegister());
1823 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1824 break;
1825
1826 default:
1827 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1828 }
1829}
1830
1831void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001832 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 LocationSummary* locations = instruction->GetLocations();
1834
1835 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 case DataType::Type::kInt32:
1837 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001838 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1839 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1840 Location rhs_location = locations->InAt(1);
1841
1842 GpuRegister rhs_reg = ZERO;
1843 int64_t rhs_imm = 0;
1844 bool use_imm = rhs_location.IsConstant();
1845 if (use_imm) {
1846 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1847 } else {
1848 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1849 }
1850
1851 if (instruction->IsAnd()) {
1852 if (use_imm)
1853 __ Andi(dst, lhs, rhs_imm);
1854 else
1855 __ And(dst, lhs, rhs_reg);
1856 } else if (instruction->IsOr()) {
1857 if (use_imm)
1858 __ Ori(dst, lhs, rhs_imm);
1859 else
1860 __ Or(dst, lhs, rhs_reg);
1861 } else if (instruction->IsXor()) {
1862 if (use_imm)
1863 __ Xori(dst, lhs, rhs_imm);
1864 else
1865 __ Xor(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001866 } else if (instruction->IsAdd() || instruction->IsSub()) {
1867 if (instruction->IsSub()) {
1868 rhs_imm = -rhs_imm;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001869 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 if (type == DataType::Type::kInt32) {
Lena Djokic38530172017-11-16 11:11:50 +01001871 if (use_imm) {
1872 if (IsInt<16>(rhs_imm)) {
1873 __ Addiu(dst, lhs, rhs_imm);
1874 } else {
1875 int16_t rhs_imm_high = High16Bits(rhs_imm);
1876 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1877 if (rhs_imm_low < 0) {
1878 rhs_imm_high += 1;
1879 }
1880 __ Aui(dst, lhs, rhs_imm_high);
1881 if (rhs_imm_low != 0) {
1882 __ Addiu(dst, dst, rhs_imm_low);
1883 }
1884 }
1885 } else {
1886 if (instruction->IsAdd()) {
1887 __ Addu(dst, lhs, rhs_reg);
1888 } else {
1889 DCHECK(instruction->IsSub());
1890 __ Subu(dst, lhs, rhs_reg);
1891 }
1892 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001894 if (use_imm) {
1895 if (IsInt<16>(rhs_imm)) {
1896 __ Daddiu(dst, lhs, rhs_imm);
1897 } else if (IsInt<32>(rhs_imm)) {
1898 int16_t rhs_imm_high = High16Bits(rhs_imm);
1899 int16_t rhs_imm_low = Low16Bits(rhs_imm);
1900 bool overflow_hi16 = false;
1901 if (rhs_imm_low < 0) {
1902 rhs_imm_high += 1;
1903 overflow_hi16 = (rhs_imm_high == -32768);
1904 }
1905 __ Daui(dst, lhs, rhs_imm_high);
1906 if (rhs_imm_low != 0) {
1907 __ Daddiu(dst, dst, rhs_imm_low);
1908 }
1909 if (overflow_hi16) {
1910 __ Dahi(dst, 1);
1911 }
1912 } else {
1913 int16_t rhs_imm_low = Low16Bits(Low32Bits(rhs_imm));
1914 if (rhs_imm_low < 0) {
1915 rhs_imm += (INT64_C(1) << 16);
1916 }
1917 int16_t rhs_imm_upper = High16Bits(Low32Bits(rhs_imm));
1918 if (rhs_imm_upper < 0) {
1919 rhs_imm += (INT64_C(1) << 32);
1920 }
1921 int16_t rhs_imm_high = Low16Bits(High32Bits(rhs_imm));
1922 if (rhs_imm_high < 0) {
1923 rhs_imm += (INT64_C(1) << 48);
1924 }
1925 int16_t rhs_imm_top = High16Bits(High32Bits(rhs_imm));
1926 GpuRegister tmp = lhs;
1927 if (rhs_imm_low != 0) {
1928 __ Daddiu(dst, tmp, rhs_imm_low);
1929 tmp = dst;
1930 }
1931 // Dahi and Dati must use the same input and output register, so we have to initialize
1932 // the dst register using Daddiu or Daui, even when the intermediate value is zero:
1933 // Daui(dst, lhs, 0).
1934 if ((rhs_imm_upper != 0) || (rhs_imm_low == 0)) {
1935 __ Daui(dst, tmp, rhs_imm_upper);
1936 }
1937 if (rhs_imm_high != 0) {
1938 __ Dahi(dst, rhs_imm_high);
1939 }
1940 if (rhs_imm_top != 0) {
1941 __ Dati(dst, rhs_imm_top);
1942 }
1943 }
1944 } else if (instruction->IsAdd()) {
1945 __ Daddu(dst, lhs, rhs_reg);
1946 } else {
1947 DCHECK(instruction->IsSub());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001948 __ Dsubu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01001949 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001950 }
1951 }
1952 break;
1953 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001954 case DataType::Type::kFloat32:
1955 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001956 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1957 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1958 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1959 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001961 __ AddS(dst, lhs, rhs);
1962 else
1963 __ AddD(dst, lhs, rhs);
1964 } else if (instruction->IsSub()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07001966 __ SubS(dst, lhs, rhs);
1967 else
1968 __ SubD(dst, lhs, rhs);
1969 } else {
1970 LOG(FATAL) << "Unexpected floating-point binary operation";
1971 }
1972 break;
1973 }
1974 default:
1975 LOG(FATAL) << "Unexpected binary operation type " << type;
1976 }
1977}
1978
1979void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001980 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001981
Vladimir Markoca6fff82017-10-03 14:49:14 +01001982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 DataType::Type type = instr->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001984 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kInt32:
1986 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001987 locations->SetInAt(0, Location::RequiresRegister());
1988 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001989 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001990 break;
1991 }
1992 default:
1993 LOG(FATAL) << "Unexpected shift type " << type;
1994 }
1995}
1996
1997void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001998 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001999 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 DataType::Type type = instr->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002001
2002 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 case DataType::Type::kInt32:
2004 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002005 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2006 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2007 Location rhs_location = locations->InAt(1);
2008
2009 GpuRegister rhs_reg = ZERO;
2010 int64_t rhs_imm = 0;
2011 bool use_imm = rhs_location.IsConstant();
2012 if (use_imm) {
2013 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2014 } else {
2015 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2016 }
2017
2018 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00002019 uint32_t shift_value = rhs_imm &
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 (type == DataType::Type::kInt32 ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021
Alexey Frunze92d90602015-12-18 18:16:36 -08002022 if (shift_value == 0) {
2023 if (dst != lhs) {
2024 __ Move(dst, lhs);
2025 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 } else if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002027 if (instr->IsShl()) {
2028 __ Sll(dst, lhs, shift_value);
2029 } else if (instr->IsShr()) {
2030 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002031 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002033 } else {
2034 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 }
2036 } else {
2037 if (shift_value < 32) {
2038 if (instr->IsShl()) {
2039 __ Dsll(dst, lhs, shift_value);
2040 } else if (instr->IsShr()) {
2041 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002042 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002044 } else {
2045 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002046 }
2047 } else {
2048 shift_value -= 32;
2049 if (instr->IsShl()) {
2050 __ Dsll32(dst, lhs, shift_value);
2051 } else if (instr->IsShr()) {
2052 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002053 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002054 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002055 } else {
2056 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002057 }
2058 }
2059 }
2060 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 if (type == DataType::Type::kInt32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002062 if (instr->IsShl()) {
2063 __ Sllv(dst, lhs, rhs_reg);
2064 } else if (instr->IsShr()) {
2065 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002066 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002067 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002068 } else {
2069 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002070 }
2071 } else {
2072 if (instr->IsShl()) {
2073 __ Dsllv(dst, lhs, rhs_reg);
2074 } else if (instr->IsShr()) {
2075 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002076 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002078 } else {
2079 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002080 }
2081 }
2082 }
2083 break;
2084 }
2085 default:
2086 LOG(FATAL) << "Unexpected shift operation type " << type;
2087 }
2088}
2089
2090void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
2091 HandleBinaryOp(instruction);
2092}
2093
2094void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
2095 HandleBinaryOp(instruction);
2096}
2097
2098void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
2099 HandleBinaryOp(instruction);
2100}
2101
2102void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
2103 HandleBinaryOp(instruction);
2104}
2105
2106void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002108 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002111 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2112 object_array_get_with_read_barrier
2113 ? LocationSummary::kCallOnSlowPath
2114 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002115 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2116 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2117 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 locations->SetInAt(0, Location::RequiresRegister());
2119 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 if (DataType::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2122 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002123 // The output overlaps in the case of an object array get with
2124 // read barriers enabled: we do not want the move to overwrite the
2125 // array's location, as we need it to emit the read barrier.
2126 locations->SetOut(Location::RequiresRegister(),
2127 object_array_get_with_read_barrier
2128 ? Location::kOutputOverlap
2129 : Location::kNoOutputOverlap);
2130 }
2131 // We need a temporary register for the read barrier marking slow
2132 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2133 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002134 bool temp_needed = instruction->GetIndex()->IsConstant()
2135 ? !kBakerReadBarrierThunksEnableForFields
2136 : !kBakerReadBarrierThunksEnableForArrays;
2137 if (temp_needed) {
2138 locations->AddTemp(Location::RequiresRegister());
2139 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 }
2141}
2142
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002143static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2144 auto null_checker = [codegen, instruction]() {
2145 codegen->MaybeRecordImplicitNullCheck(instruction);
2146 };
2147 return null_checker;
2148}
2149
Alexey Frunze4dda3372015-06-01 18:31:49 -07002150void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2151 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002152 Location obj_loc = locations->InAt(0);
2153 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2154 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002156 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002157 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002159 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002160 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2161 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002163 case DataType::Type::kBool:
2164 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002165 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 if (index.IsConstant()) {
2167 size_t offset =
2168 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002169 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 } else {
2171 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002172 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002173 }
2174 break;
2175 }
2176
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002178 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 if (index.IsConstant()) {
2180 size_t offset =
2181 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002182 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002183 } else {
2184 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002185 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 }
2187 break;
2188 }
2189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002190 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002191 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002192 if (maybe_compressed_char_at) {
2193 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002194 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002195 __ Dext(TMP, TMP, 0, 1);
2196 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2197 "Expecting 0=compressed, 1=uncompressed");
2198 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002200 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2201 if (maybe_compressed_char_at) {
2202 Mips64Label uncompressed_load, done;
2203 __ Bnezc(TMP, &uncompressed_load);
2204 __ LoadFromOffset(kLoadUnsignedByte,
2205 out,
2206 obj,
2207 data_offset + (const_index << TIMES_1));
2208 __ Bc(&done);
2209 __ Bind(&uncompressed_load);
2210 __ LoadFromOffset(kLoadUnsignedHalfword,
2211 out,
2212 obj,
2213 data_offset + (const_index << TIMES_2));
2214 __ Bind(&done);
2215 } else {
2216 __ LoadFromOffset(kLoadUnsignedHalfword,
2217 out,
2218 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002219 data_offset + (const_index << TIMES_2),
2220 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002221 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002222 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002223 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2224 if (maybe_compressed_char_at) {
2225 Mips64Label uncompressed_load, done;
2226 __ Bnezc(TMP, &uncompressed_load);
2227 __ Daddu(TMP, obj, index_reg);
2228 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2229 __ Bc(&done);
2230 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002231 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002232 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2233 __ Bind(&done);
2234 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002235 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002236 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002237 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002238 }
2239 break;
2240 }
2241
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002242 case DataType::Type::kInt16: {
2243 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2244 if (index.IsConstant()) {
2245 size_t offset =
2246 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2247 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
2248 } else {
2249 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
2250 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2251 }
2252 break;
2253 }
2254
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 case DataType::Type::kInt32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002256 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002257 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002258 LoadOperandType load_type =
2259 (type == DataType::Type::kReference) ? kLoadUnsignedWord : kLoadWord;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002260 if (index.IsConstant()) {
2261 size_t offset =
2262 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002263 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002265 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002266 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 }
2268 break;
2269 }
2270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002272 static_assert(
2273 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2274 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2275 // /* HeapReference<Object> */ out =
2276 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2277 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002278 bool temp_needed = index.IsConstant()
2279 ? !kBakerReadBarrierThunksEnableForFields
2280 : !kBakerReadBarrierThunksEnableForArrays;
2281 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002282 // Note that a potential implicit null check is handled in this
2283 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002284 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2285 if (index.IsConstant()) {
2286 // Array load with a constant index can be treated as a field load.
2287 size_t offset =
2288 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2289 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2290 out_loc,
2291 obj,
2292 offset,
2293 temp,
2294 /* needs_null_check */ false);
2295 } else {
2296 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2297 out_loc,
2298 obj,
2299 data_offset,
2300 index,
2301 temp,
2302 /* needs_null_check */ false);
2303 }
Alexey Frunze15958152017-02-09 19:08:30 -08002304 } else {
2305 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2306 if (index.IsConstant()) {
2307 size_t offset =
2308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2309 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2310 // If read barriers are enabled, emit read barriers other than
2311 // Baker's using a slow path (and also unpoison the loaded
2312 // reference, if heap poisoning is enabled).
2313 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2314 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002315 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002316 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2317 // If read barriers are enabled, emit read barriers other than
2318 // Baker's using a slow path (and also unpoison the loaded
2319 // reference, if heap poisoning is enabled).
2320 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2321 out_loc,
2322 out_loc,
2323 obj_loc,
2324 data_offset,
2325 index);
2326 }
2327 }
2328 break;
2329 }
2330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002332 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002333 if (index.IsConstant()) {
2334 size_t offset =
2335 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002336 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002337 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002338 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002339 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 }
2341 break;
2342 }
2343
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002345 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346 if (index.IsConstant()) {
2347 size_t offset =
2348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002349 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002350 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002351 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002352 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002353 }
2354 break;
2355 }
2356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002358 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002359 if (index.IsConstant()) {
2360 size_t offset =
2361 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002362 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002363 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002364 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002365 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 }
2367 break;
2368 }
2369
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002371 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2372 UNREACHABLE();
2373 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002374}
2375
2376void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002377 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 locations->SetInAt(0, Location::RequiresRegister());
2379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2380}
2381
2382void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2383 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002384 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2386 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2387 __ LoadFromOffset(kLoadWord, out, obj, offset);
2388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002389 // Mask out compression flag from String's array length.
2390 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2391 __ Srl(out, out, 1u);
2392 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002393}
2394
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002395Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2396 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2397 ? Location::ConstantLocation(instruction->AsConstant())
2398 : Location::RequiresRegister();
2399}
2400
2401Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2402 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2403 // We can store a non-zero float or double constant without first loading it into the FPU,
2404 // but we should only prefer this if the constant has a single use.
2405 if (instruction->IsConstant() &&
2406 (instruction->AsConstant()->IsZeroBitPattern() ||
2407 instruction->GetUses().HasExactlyOneElement())) {
2408 return Location::ConstantLocation(instruction->AsConstant());
2409 // Otherwise fall through and require an FPU register for the constant.
2410 }
2411 return Location::RequiresFpuRegister();
2412}
2413
Alexey Frunze4dda3372015-06-01 18:31:49 -07002414void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002416
2417 bool needs_write_barrier =
2418 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2419 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2420
Vladimir Markoca6fff82017-10-03 14:49:14 +01002421 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze4dda3372015-06-01 18:31:49 -07002422 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002423 may_need_runtime_call_for_type_check ?
2424 LocationSummary::kCallOnSlowPath :
2425 LocationSummary::kNoCall);
2426
2427 locations->SetInAt(0, Location::RequiresRegister());
2428 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002429 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002430 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002431 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002432 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2433 }
2434 if (needs_write_barrier) {
2435 // Temporary register for the write barrier.
2436 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002437 }
2438}
2439
2440void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2441 LocationSummary* locations = instruction->GetLocations();
2442 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2443 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002444 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002445 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002446 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002447 bool needs_write_barrier =
2448 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002449 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002450 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451
2452 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002454 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002455 case DataType::Type::kInt8: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002457 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002458 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002460 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2461 }
2462 if (value_location.IsConstant()) {
2463 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2464 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2465 } else {
2466 GpuRegister value = value_location.AsRegister<GpuRegister>();
2467 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 }
2469 break;
2470 }
2471
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002472 case DataType::Type::kUint16:
2473 case DataType::Type::kInt16: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002474 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002476 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002477 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002478 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002479 }
2480 if (value_location.IsConstant()) {
2481 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2482 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2483 } else {
2484 GpuRegister value = value_location.AsRegister<GpuRegister>();
2485 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002486 }
2487 break;
2488 }
2489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002491 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2492 if (index.IsConstant()) {
2493 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2494 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002495 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002496 }
2497 if (value_location.IsConstant()) {
2498 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2499 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2500 } else {
2501 GpuRegister value = value_location.AsRegister<GpuRegister>();
2502 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2503 }
2504 break;
2505 }
2506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002508 if (value_location.IsConstant()) {
2509 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002510 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002511 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002512 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002513 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002514 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002515 }
Alexey Frunze15958152017-02-09 19:08:30 -08002516 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2517 DCHECK_EQ(value, 0);
2518 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2519 DCHECK(!needs_write_barrier);
2520 DCHECK(!may_need_runtime_call_for_type_check);
2521 break;
2522 }
2523
2524 DCHECK(needs_write_barrier);
2525 GpuRegister value = value_location.AsRegister<GpuRegister>();
2526 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2527 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2528 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2529 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2530 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2531 Mips64Label done;
2532 SlowPathCodeMIPS64* slow_path = nullptr;
2533
2534 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002535 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS64(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002536 codegen_->AddSlowPath(slow_path);
2537 if (instruction->GetValueCanBeNull()) {
2538 Mips64Label non_zero;
2539 __ Bnezc(value, &non_zero);
2540 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2541 if (index.IsConstant()) {
2542 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002543 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002544 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002545 }
Alexey Frunze15958152017-02-09 19:08:30 -08002546 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2547 __ Bc(&done);
2548 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002549 }
Alexey Frunze15958152017-02-09 19:08:30 -08002550
2551 // Note that when read barriers are enabled, the type checks
2552 // are performed without read barriers. This is fine, even in
2553 // the case where a class object is in the from-space after
2554 // the flip, as a comparison involving such a type would not
2555 // produce a false positive; it may of course produce a false
2556 // negative, in which case we would take the ArraySet slow
2557 // path.
2558
2559 // /* HeapReference<Class> */ temp1 = obj->klass_
2560 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2561 __ MaybeUnpoisonHeapReference(temp1);
2562
2563 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2564 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2565 // /* HeapReference<Class> */ temp2 = value->klass_
2566 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2567 // If heap poisoning is enabled, no need to unpoison `temp1`
2568 // nor `temp2`, as we are comparing two poisoned references.
2569
2570 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2571 Mips64Label do_put;
2572 __ Beqc(temp1, temp2, &do_put);
2573 // If heap poisoning is enabled, the `temp1` reference has
2574 // not been unpoisoned yet; unpoison it now.
2575 __ MaybeUnpoisonHeapReference(temp1);
2576
2577 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2578 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2579 // If heap poisoning is enabled, no need to unpoison
2580 // `temp1`, as we are comparing against null below.
2581 __ Bnezc(temp1, slow_path->GetEntryLabel());
2582 __ Bind(&do_put);
2583 } else {
2584 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2585 }
2586 }
2587
2588 GpuRegister source = value;
2589 if (kPoisonHeapReferences) {
2590 // Note that in the case where `value` is a null reference,
2591 // we do not enter this block, as a null reference does not
2592 // need poisoning.
2593 __ Move(temp1, value);
2594 __ PoisonHeapReference(temp1);
2595 source = temp1;
2596 }
2597
2598 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2599 if (index.IsConstant()) {
2600 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002602 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002603 }
2604 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2605
2606 if (!may_need_runtime_call_for_type_check) {
2607 codegen_->MaybeRecordImplicitNullCheck(instruction);
2608 }
2609
2610 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2611
2612 if (done.IsLinked()) {
2613 __ Bind(&done);
2614 }
2615
2616 if (slow_path != nullptr) {
2617 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002618 }
2619 break;
2620 }
2621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002623 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002624 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002625 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002626 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002627 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002628 }
2629 if (value_location.IsConstant()) {
2630 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2631 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2632 } else {
2633 GpuRegister value = value_location.AsRegister<GpuRegister>();
2634 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002635 }
2636 break;
2637 }
2638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002639 case DataType::Type::kFloat32: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002640 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002641 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002642 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002643 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002644 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002645 }
2646 if (value_location.IsConstant()) {
2647 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2648 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2649 } else {
2650 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2651 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002652 }
2653 break;
2654 }
2655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002657 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002658 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002659 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002661 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002662 }
2663 if (value_location.IsConstant()) {
2664 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2665 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2666 } else {
2667 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2668 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002669 }
2670 break;
2671 }
2672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002674 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2675 UNREACHABLE();
2676 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677}
2678
2679void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002680 RegisterSet caller_saves = RegisterSet::Empty();
2681 InvokeRuntimeCallingConvention calling_convention;
2682 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2683 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2684 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002685
2686 HInstruction* index = instruction->InputAt(0);
2687 HInstruction* length = instruction->InputAt(1);
2688
2689 bool const_index = false;
2690 bool const_length = false;
2691
2692 if (index->IsConstant()) {
2693 if (length->IsConstant()) {
2694 const_index = true;
2695 const_length = true;
2696 } else {
2697 int32_t index_value = index->AsIntConstant()->GetValue();
2698 if (index_value < 0 || IsInt<16>(index_value + 1)) {
2699 const_index = true;
2700 }
2701 }
2702 } else if (length->IsConstant()) {
2703 int32_t length_value = length->AsIntConstant()->GetValue();
2704 if (IsUint<15>(length_value)) {
2705 const_length = true;
2706 }
2707 }
2708
2709 locations->SetInAt(0, const_index
2710 ? Location::ConstantLocation(index->AsConstant())
2711 : Location::RequiresRegister());
2712 locations->SetInAt(1, const_length
2713 ? Location::ConstantLocation(length->AsConstant())
2714 : Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715}
2716
2717void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2718 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002719 Location index_loc = locations->InAt(0);
2720 Location length_loc = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002721
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002722 if (length_loc.IsConstant()) {
2723 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
2724 if (index_loc.IsConstant()) {
2725 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2726 if (index < 0 || index >= length) {
2727 BoundsCheckSlowPathMIPS64* slow_path =
2728 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2729 codegen_->AddSlowPath(slow_path);
2730 __ Bc(slow_path->GetEntryLabel());
2731 } else {
2732 // Nothing to be done.
2733 }
2734 return;
2735 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002736
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01002737 BoundsCheckSlowPathMIPS64* slow_path =
2738 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2739 codegen_->AddSlowPath(slow_path);
2740 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2741 if (length == 0) {
2742 __ Bc(slow_path->GetEntryLabel());
2743 } else if (length == 1) {
2744 __ Bnezc(index, slow_path->GetEntryLabel());
2745 } else {
2746 DCHECK(IsUint<15>(length)) << length;
2747 __ Sltiu(TMP, index, length);
2748 __ Beqzc(TMP, slow_path->GetEntryLabel());
2749 }
2750 } else {
2751 GpuRegister length = length_loc.AsRegister<GpuRegister>();
2752 BoundsCheckSlowPathMIPS64* slow_path =
2753 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS64(instruction);
2754 codegen_->AddSlowPath(slow_path);
2755 if (index_loc.IsConstant()) {
2756 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
2757 if (index < 0) {
2758 __ Bc(slow_path->GetEntryLabel());
2759 } else if (index == 0) {
2760 __ Blezc(length, slow_path->GetEntryLabel());
2761 } else {
2762 DCHECK(IsInt<16>(index + 1)) << index;
2763 __ Sltiu(TMP, length, index + 1);
2764 __ Bnezc(TMP, slow_path->GetEntryLabel());
2765 }
2766 } else {
2767 GpuRegister index = index_loc.AsRegister<GpuRegister>();
2768 __ Bgeuc(index, length, slow_path->GetEntryLabel());
2769 }
2770 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002771}
2772
Alexey Frunze15958152017-02-09 19:08:30 -08002773// Temp is used for read barrier.
2774static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2775 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002776 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08002777 (kUseBakerReadBarrier ||
2778 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2779 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2780 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2781 return 1;
2782 }
2783 return 0;
2784}
2785
2786// Extra temp is used for read barrier.
2787static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2788 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2789}
2790
Alexey Frunze4dda3372015-06-01 18:31:49 -07002791void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002792 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2793 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2794
2795 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2796 switch (type_check_kind) {
2797 case TypeCheckKind::kExactCheck:
2798 case TypeCheckKind::kAbstractClassCheck:
2799 case TypeCheckKind::kClassHierarchyCheck:
2800 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002801 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002802 ? LocationSummary::kCallOnSlowPath
2803 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2804 break;
2805 case TypeCheckKind::kArrayCheck:
2806 case TypeCheckKind::kUnresolvedCheck:
2807 case TypeCheckKind::kInterfaceCheck:
2808 call_kind = LocationSummary::kCallOnSlowPath;
2809 break;
2810 }
2811
Vladimir Markoca6fff82017-10-03 14:49:14 +01002812 LocationSummary* locations =
2813 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002814 locations->SetInAt(0, Location::RequiresRegister());
2815 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002816 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817}
2818
2819void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002820 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002821 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002822 Location obj_loc = locations->InAt(0);
2823 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002824 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002825 Location temp_loc = locations->GetTemp(0);
2826 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2827 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2828 DCHECK_LE(num_temps, 2u);
2829 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002830 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2831 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2832 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2833 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2834 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2835 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2836 const uint32_t object_array_data_offset =
2837 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2838 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002839
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002840 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2841 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2842 // read barriers is done for performance and code size reasons.
2843 bool is_type_check_slow_path_fatal = false;
2844 if (!kEmitCompilerReadBarrier) {
2845 is_type_check_slow_path_fatal =
2846 (type_check_kind == TypeCheckKind::kExactCheck ||
2847 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2848 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2849 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2850 !instruction->CanThrowIntoCatchBlock();
2851 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002852 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002853 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
2854 instruction, is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002855 codegen_->AddSlowPath(slow_path);
2856
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002857 // Avoid this check if we know `obj` is not null.
2858 if (instruction->MustDoNullCheck()) {
2859 __ Beqzc(obj, &done);
2860 }
2861
2862 switch (type_check_kind) {
2863 case TypeCheckKind::kExactCheck:
2864 case TypeCheckKind::kArrayCheck: {
2865 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002866 GenerateReferenceLoadTwoRegisters(instruction,
2867 temp_loc,
2868 obj_loc,
2869 class_offset,
2870 maybe_temp2_loc,
2871 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002872 // Jump to slow path for throwing the exception or doing a
2873 // more involved array check.
2874 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2875 break;
2876 }
2877
2878 case TypeCheckKind::kAbstractClassCheck: {
2879 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002880 GenerateReferenceLoadTwoRegisters(instruction,
2881 temp_loc,
2882 obj_loc,
2883 class_offset,
2884 maybe_temp2_loc,
2885 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002886 // If the class is abstract, we eagerly fetch the super class of the
2887 // object to avoid doing a comparison we know will fail.
2888 Mips64Label loop;
2889 __ Bind(&loop);
2890 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002891 GenerateReferenceLoadOneRegister(instruction,
2892 temp_loc,
2893 super_offset,
2894 maybe_temp2_loc,
2895 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002896 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2897 // exception.
2898 __ Beqzc(temp, slow_path->GetEntryLabel());
2899 // Otherwise, compare the classes.
2900 __ Bnec(temp, cls, &loop);
2901 break;
2902 }
2903
2904 case TypeCheckKind::kClassHierarchyCheck: {
2905 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002906 GenerateReferenceLoadTwoRegisters(instruction,
2907 temp_loc,
2908 obj_loc,
2909 class_offset,
2910 maybe_temp2_loc,
2911 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002912 // Walk over the class hierarchy to find a match.
2913 Mips64Label loop;
2914 __ Bind(&loop);
2915 __ Beqc(temp, cls, &done);
2916 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002917 GenerateReferenceLoadOneRegister(instruction,
2918 temp_loc,
2919 super_offset,
2920 maybe_temp2_loc,
2921 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002922 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2923 // exception. Otherwise, jump to the beginning of the loop.
2924 __ Bnezc(temp, &loop);
2925 __ Bc(slow_path->GetEntryLabel());
2926 break;
2927 }
2928
2929 case TypeCheckKind::kArrayObjectCheck: {
2930 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002931 GenerateReferenceLoadTwoRegisters(instruction,
2932 temp_loc,
2933 obj_loc,
2934 class_offset,
2935 maybe_temp2_loc,
2936 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002937 // Do an exact check.
2938 __ Beqc(temp, cls, &done);
2939 // Otherwise, we need to check that the object's class is a non-primitive array.
2940 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002941 GenerateReferenceLoadOneRegister(instruction,
2942 temp_loc,
2943 component_offset,
2944 maybe_temp2_loc,
2945 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002946 // If the component type is null, jump to the slow path to throw the exception.
2947 __ Beqzc(temp, slow_path->GetEntryLabel());
2948 // Otherwise, the object is indeed an array, further check that this component
2949 // type is not a primitive type.
2950 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2951 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2952 __ Bnezc(temp, slow_path->GetEntryLabel());
2953 break;
2954 }
2955
2956 case TypeCheckKind::kUnresolvedCheck:
2957 // We always go into the type check slow path for the unresolved check case.
2958 // We cannot directly call the CheckCast runtime entry point
2959 // without resorting to a type checking slow path here (i.e. by
2960 // calling InvokeRuntime directly), as it would require to
2961 // assign fixed registers for the inputs of this HInstanceOf
2962 // instruction (following the runtime calling convention), which
2963 // might be cluttered by the potential first read barrier
2964 // emission at the beginning of this method.
2965 __ Bc(slow_path->GetEntryLabel());
2966 break;
2967
2968 case TypeCheckKind::kInterfaceCheck: {
2969 // Avoid read barriers to improve performance of the fast path. We can not get false
2970 // positives by doing this.
2971 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002972 GenerateReferenceLoadTwoRegisters(instruction,
2973 temp_loc,
2974 obj_loc,
2975 class_offset,
2976 maybe_temp2_loc,
2977 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002978 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002979 GenerateReferenceLoadTwoRegisters(instruction,
2980 temp_loc,
2981 temp_loc,
2982 iftable_offset,
2983 maybe_temp2_loc,
2984 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002985 // Iftable is never null.
2986 __ Lw(TMP, temp, array_length_offset);
2987 // Loop through the iftable and check if any class matches.
2988 Mips64Label loop;
2989 __ Bind(&loop);
2990 __ Beqzc(TMP, slow_path->GetEntryLabel());
2991 __ Lwu(AT, temp, object_array_data_offset);
2992 __ MaybeUnpoisonHeapReference(AT);
2993 // Go to next interface.
2994 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2995 __ Addiu(TMP, TMP, -2);
2996 // Compare the classes and continue the loop if they do not match.
2997 __ Bnec(AT, cls, &loop);
2998 break;
2999 }
3000 }
3001
3002 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003 __ Bind(slow_path->GetExitLabel());
3004}
3005
3006void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
3007 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003008 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003009 locations->SetInAt(0, Location::RequiresRegister());
3010 if (check->HasUses()) {
3011 locations->SetOut(Location::SameAsFirstInput());
3012 }
3013}
3014
3015void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
3016 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003017 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Alexey Frunze4dda3372015-06-01 18:31:49 -07003018 check->GetLoadClass(),
3019 check,
3020 check->GetDexPc(),
3021 true);
3022 codegen_->AddSlowPath(slow_path);
3023 GenerateClassInitializationCheck(slow_path,
3024 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
3025}
3026
3027void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 DataType::Type in_type = compare->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029
Vladimir Markoca6fff82017-10-03 14:49:14 +01003030 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003031
3032 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003033 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003034 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003035 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003037 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 case DataType::Type::kInt32:
3039 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003040 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003041 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3043 break;
3044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kFloat32:
3046 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003047 locations->SetInAt(0, Location::RequiresFpuRegister());
3048 locations->SetInAt(1, Location::RequiresFpuRegister());
3049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003050 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003051
3052 default:
3053 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3054 }
3055}
3056
3057void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
3058 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003059 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003061
3062 // 0 if: left == right
3063 // 1 if: left > right
3064 // -1 if: left < right
3065 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003067 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003069 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003070 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003071 case DataType::Type::kInt32:
3072 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003073 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003074 Location rhs_location = locations->InAt(1);
3075 bool use_imm = rhs_location.IsConstant();
3076 GpuRegister rhs = ZERO;
3077 if (use_imm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 if (in_type == DataType::Type::kInt64) {
Aart Bika19616e2016-02-01 18:57:58 -08003079 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
3080 if (value != 0) {
3081 rhs = AT;
3082 __ LoadConst64(rhs, value);
3083 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00003084 } else {
3085 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
3086 if (value != 0) {
3087 rhs = AT;
3088 __ LoadConst32(rhs, value);
3089 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07003090 }
3091 } else {
3092 rhs = rhs_location.AsRegister<GpuRegister>();
3093 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08003095 __ Slt(res, rhs, lhs);
3096 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097 break;
3098 }
3099
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003100 case DataType::Type::kFloat32: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003101 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3102 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3103 Mips64Label done;
3104 __ CmpEqS(FTMP, lhs, rhs);
3105 __ LoadConst32(res, 0);
3106 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003107 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003108 __ CmpLtS(FTMP, lhs, rhs);
3109 __ LoadConst32(res, -1);
3110 __ Bc1nez(FTMP, &done);
3111 __ LoadConst32(res, 1);
3112 } else {
3113 __ CmpLtS(FTMP, rhs, lhs);
3114 __ LoadConst32(res, 1);
3115 __ Bc1nez(FTMP, &done);
3116 __ LoadConst32(res, -1);
3117 }
3118 __ Bind(&done);
3119 break;
3120 }
3121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kFloat64: {
Alexey Frunze299a9392015-12-08 16:08:02 -08003123 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3124 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3125 Mips64Label done;
3126 __ CmpEqD(FTMP, lhs, rhs);
3127 __ LoadConst32(res, 0);
3128 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00003129 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08003130 __ CmpLtD(FTMP, lhs, rhs);
3131 __ LoadConst32(res, -1);
3132 __ Bc1nez(FTMP, &done);
3133 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003134 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08003135 __ CmpLtD(FTMP, rhs, lhs);
3136 __ LoadConst32(res, 1);
3137 __ Bc1nez(FTMP, &done);
3138 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003139 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003140 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003141 break;
3142 }
3143
3144 default:
3145 LOG(FATAL) << "Unimplemented compare type " << in_type;
3146 }
3147}
3148
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003149void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003150 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08003151 switch (instruction->InputAt(0)->GetType()) {
3152 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003154 locations->SetInAt(0, Location::RequiresRegister());
3155 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3156 break;
3157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003158 case DataType::Type::kFloat32:
3159 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003160 locations->SetInAt(0, Location::RequiresFpuRegister());
3161 locations->SetInAt(1, Location::RequiresFpuRegister());
3162 break;
3163 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003164 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3166 }
3167}
3168
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003169void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003171 return;
3172 }
3173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 DataType::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08003176 switch (type) {
3177 default:
3178 // Integer case.
3179 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
3180 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08003182 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
3183 return;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003184 case DataType::Type::kFloat32:
3185 case DataType::Type::kFloat64:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003186 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3187 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003188 }
3189}
3190
Alexey Frunzec857c742015-09-23 15:12:39 -07003191void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3192 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003194
3195 LocationSummary* locations = instruction->GetLocations();
3196 Location second = locations->InAt(1);
3197 DCHECK(second.IsConstant());
3198
3199 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3200 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3201 int64_t imm = Int64FromConstant(second.GetConstant());
3202 DCHECK(imm == 1 || imm == -1);
3203
3204 if (instruction->IsRem()) {
3205 __ Move(out, ZERO);
3206 } else {
3207 if (imm == -1) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003208 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003209 __ Subu(out, ZERO, dividend);
3210 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003212 __ Dsubu(out, ZERO, dividend);
3213 }
3214 } else if (out != dividend) {
3215 __ Move(out, dividend);
3216 }
3217 }
3218}
3219
3220void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3221 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003222 DataType::Type type = instruction->GetResultType();
Alexey Frunzec857c742015-09-23 15:12:39 -07003223
3224 LocationSummary* locations = instruction->GetLocations();
3225 Location second = locations->InAt(1);
3226 DCHECK(second.IsConstant());
3227
3228 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3229 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3230 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003231 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003232 int ctz_imm = CTZ(abs_imm);
3233
3234 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003235 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003236 if (ctz_imm == 1) {
3237 // Fast path for division by +/-2, which is very common.
3238 __ Srl(TMP, dividend, 31);
3239 } else {
3240 __ Sra(TMP, dividend, 31);
3241 __ Srl(TMP, TMP, 32 - ctz_imm);
3242 }
3243 __ Addu(out, dividend, TMP);
3244 __ Sra(out, out, ctz_imm);
3245 if (imm < 0) {
3246 __ Subu(out, ZERO, out);
3247 }
3248 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003250 if (ctz_imm == 1) {
3251 // Fast path for division by +/-2, which is very common.
3252 __ Dsrl32(TMP, dividend, 31);
3253 } else {
3254 __ Dsra32(TMP, dividend, 31);
3255 if (ctz_imm > 32) {
3256 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3257 } else {
3258 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3259 }
3260 }
3261 __ Daddu(out, dividend, TMP);
3262 if (ctz_imm < 32) {
3263 __ Dsra(out, out, ctz_imm);
3264 } else {
3265 __ Dsra32(out, out, ctz_imm - 32);
3266 }
3267 if (imm < 0) {
3268 __ Dsubu(out, ZERO, out);
3269 }
3270 }
3271 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003272 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003273 if (ctz_imm == 1) {
3274 // Fast path for modulo +/-2, which is very common.
3275 __ Sra(TMP, dividend, 31);
3276 __ Subu(out, dividend, TMP);
3277 __ Andi(out, out, 1);
3278 __ Addu(out, out, TMP);
3279 } else {
3280 __ Sra(TMP, dividend, 31);
3281 __ Srl(TMP, TMP, 32 - ctz_imm);
3282 __ Addu(out, dividend, TMP);
3283 if (IsUint<16>(abs_imm - 1)) {
3284 __ Andi(out, out, abs_imm - 1);
3285 } else {
3286 __ Sll(out, out, 32 - ctz_imm);
3287 __ Srl(out, out, 32 - ctz_imm);
3288 }
3289 __ Subu(out, out, TMP);
3290 }
3291 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 DCHECK_EQ(type, DataType::Type::kInt64);
Alexey Frunzec857c742015-09-23 15:12:39 -07003293 if (ctz_imm == 1) {
3294 // Fast path for modulo +/-2, which is very common.
3295 __ Dsra32(TMP, dividend, 31);
3296 __ Dsubu(out, dividend, TMP);
3297 __ Andi(out, out, 1);
3298 __ Daddu(out, out, TMP);
3299 } else {
3300 __ Dsra32(TMP, dividend, 31);
3301 if (ctz_imm > 32) {
3302 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3303 } else {
3304 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3305 }
3306 __ Daddu(out, dividend, TMP);
3307 if (IsUint<16>(abs_imm - 1)) {
3308 __ Andi(out, out, abs_imm - 1);
3309 } else {
3310 if (ctz_imm > 32) {
3311 __ Dsll(out, out, 64 - ctz_imm);
3312 __ Dsrl(out, out, 64 - ctz_imm);
3313 } else {
3314 __ Dsll32(out, out, 32 - ctz_imm);
3315 __ Dsrl32(out, out, 32 - ctz_imm);
3316 }
3317 }
3318 __ Dsubu(out, out, TMP);
3319 }
3320 }
3321 }
3322}
3323
3324void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3325 DCHECK(instruction->IsDiv() || instruction->IsRem());
3326
3327 LocationSummary* locations = instruction->GetLocations();
3328 Location second = locations->InAt(1);
3329 DCHECK(second.IsConstant());
3330
3331 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3332 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3333 int64_t imm = Int64FromConstant(second.GetConstant());
3334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 DataType::Type type = instruction->GetResultType();
3336 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003337
3338 int64_t magic;
3339 int shift;
3340 CalculateMagicAndShiftForDivRem(imm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003341 (type == DataType::Type::kInt64),
Alexey Frunzec857c742015-09-23 15:12:39 -07003342 &magic,
3343 &shift);
3344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 if (type == DataType::Type::kInt32) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003346 __ LoadConst32(TMP, magic);
3347 __ MuhR6(TMP, dividend, TMP);
3348
3349 if (imm > 0 && magic < 0) {
3350 __ Addu(TMP, TMP, dividend);
3351 } else if (imm < 0 && magic > 0) {
3352 __ Subu(TMP, TMP, dividend);
3353 }
3354
3355 if (shift != 0) {
3356 __ Sra(TMP, TMP, shift);
3357 }
3358
3359 if (instruction->IsDiv()) {
3360 __ Sra(out, TMP, 31);
3361 __ Subu(out, TMP, out);
3362 } else {
3363 __ Sra(AT, TMP, 31);
3364 __ Subu(AT, TMP, AT);
3365 __ LoadConst32(TMP, imm);
3366 __ MulR6(TMP, AT, TMP);
3367 __ Subu(out, dividend, TMP);
3368 }
3369 } else {
3370 __ LoadConst64(TMP, magic);
3371 __ Dmuh(TMP, dividend, TMP);
3372
3373 if (imm > 0 && magic < 0) {
3374 __ Daddu(TMP, TMP, dividend);
3375 } else if (imm < 0 && magic > 0) {
3376 __ Dsubu(TMP, TMP, dividend);
3377 }
3378
3379 if (shift >= 32) {
3380 __ Dsra32(TMP, TMP, shift - 32);
3381 } else if (shift > 0) {
3382 __ Dsra(TMP, TMP, shift);
3383 }
3384
3385 if (instruction->IsDiv()) {
3386 __ Dsra32(out, TMP, 31);
3387 __ Dsubu(out, TMP, out);
3388 } else {
3389 __ Dsra32(AT, TMP, 31);
3390 __ Dsubu(AT, TMP, AT);
3391 __ LoadConst64(TMP, imm);
3392 __ Dmul(TMP, AT, TMP);
3393 __ Dsubu(out, dividend, TMP);
3394 }
3395 }
3396}
3397
3398void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3399 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 DataType::Type type = instruction->GetResultType();
3401 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64) << type;
Alexey Frunzec857c742015-09-23 15:12:39 -07003402
3403 LocationSummary* locations = instruction->GetLocations();
3404 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3405 Location second = locations->InAt(1);
3406
3407 if (second.IsConstant()) {
3408 int64_t imm = Int64FromConstant(second.GetConstant());
3409 if (imm == 0) {
3410 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3411 } else if (imm == 1 || imm == -1) {
3412 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003413 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003414 DivRemByPowerOfTwo(instruction);
3415 } else {
3416 DCHECK(imm <= -2 || imm >= 2);
3417 GenerateDivRemWithAnyConstant(instruction);
3418 }
3419 } else {
3420 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3421 GpuRegister divisor = second.AsRegister<GpuRegister>();
3422 if (instruction->IsDiv()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003424 __ DivR6(out, dividend, divisor);
3425 else
3426 __ Ddiv(out, dividend, divisor);
3427 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 if (type == DataType::Type::kInt32)
Alexey Frunzec857c742015-09-23 15:12:39 -07003429 __ ModR6(out, dividend, divisor);
3430 else
3431 __ Dmod(out, dividend, divisor);
3432 }
3433 }
3434}
3435
Alexey Frunze4dda3372015-06-01 18:31:49 -07003436void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3437 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003438 new (GetGraph()->GetAllocator()) LocationSummary(div, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003439 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 case DataType::Type::kInt32:
3441 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003443 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3445 break;
3446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003447 case DataType::Type::kFloat32:
3448 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003449 locations->SetInAt(0, Location::RequiresFpuRegister());
3450 locations->SetInAt(1, Location::RequiresFpuRegister());
3451 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3452 break;
3453
3454 default:
3455 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3456 }
3457}
3458
3459void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003460 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003461 LocationSummary* locations = instruction->GetLocations();
3462
3463 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003464 case DataType::Type::kInt32:
3465 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07003466 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003467 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003468 case DataType::Type::kFloat32:
3469 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3471 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3472 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003473 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07003474 __ DivS(dst, lhs, rhs);
3475 else
3476 __ DivD(dst, lhs, rhs);
3477 break;
3478 }
3479 default:
3480 LOG(FATAL) << "Unexpected div type " << type;
3481 }
3482}
3483
3484void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003485 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003486 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003487}
3488
3489void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3490 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003491 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492 codegen_->AddSlowPath(slow_path);
3493 Location value = instruction->GetLocations()->InAt(0);
3494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003497 if (!DataType::IsIntegralType(type)) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003498 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003499 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003500 }
3501
3502 if (value.IsConstant()) {
3503 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3504 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003505 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506 } else {
3507 // A division by a non-null constant is valid. We don't need to perform
3508 // any check, so simply fall through.
3509 }
3510 } else {
3511 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3512 }
3513}
3514
3515void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003517 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003518 locations->SetOut(Location::ConstantLocation(constant));
3519}
3520
3521void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3522 // Will be generated at use site.
3523}
3524
3525void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3526 exit->SetLocations(nullptr);
3527}
3528
3529void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3530}
3531
3532void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003534 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003535 locations->SetOut(Location::ConstantLocation(constant));
3536}
3537
3538void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3539 // Will be generated at use site.
3540}
3541
David Brazdilfc6a86a2015-06-26 10:33:45 +00003542void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 DCHECK(!successor->IsExitBlock());
3544 HBasicBlock* block = got->GetBlock();
3545 HInstruction* previous = got->GetPrevious();
3546 HLoopInformation* info = block->GetLoopInformation();
3547
3548 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003549 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3550 return;
3551 }
3552 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3553 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3554 }
3555 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003556 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003557 }
3558}
3559
David Brazdilfc6a86a2015-06-26 10:33:45 +00003560void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3561 got->SetLocations(nullptr);
3562}
3563
3564void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3565 HandleGoto(got, got->GetSuccessor());
3566}
3567
3568void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3569 try_boundary->SetLocations(nullptr);
3570}
3571
3572void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3573 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3574 if (!successor->IsExitBlock()) {
3575 HandleGoto(try_boundary, successor);
3576 }
3577}
3578
Alexey Frunze299a9392015-12-08 16:08:02 -08003579void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3580 bool is64bit,
3581 LocationSummary* locations) {
3582 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3583 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3584 Location rhs_location = locations->InAt(1);
3585 GpuRegister rhs_reg = ZERO;
3586 int64_t rhs_imm = 0;
3587 bool use_imm = rhs_location.IsConstant();
3588 if (use_imm) {
3589 if (is64bit) {
3590 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3591 } else {
3592 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3593 }
3594 } else {
3595 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3596 }
3597 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3598
3599 switch (cond) {
3600 case kCondEQ:
3601 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003602 if (use_imm && IsInt<16>(-rhs_imm)) {
3603 if (rhs_imm == 0) {
3604 if (cond == kCondEQ) {
3605 __ Sltiu(dst, lhs, 1);
3606 } else {
3607 __ Sltu(dst, ZERO, lhs);
3608 }
3609 } else {
3610 if (is64bit) {
3611 __ Daddiu(dst, lhs, -rhs_imm);
3612 } else {
3613 __ Addiu(dst, lhs, -rhs_imm);
3614 }
3615 if (cond == kCondEQ) {
3616 __ Sltiu(dst, dst, 1);
3617 } else {
3618 __ Sltu(dst, ZERO, dst);
3619 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003620 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003621 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003622 if (use_imm && IsUint<16>(rhs_imm)) {
3623 __ Xori(dst, lhs, rhs_imm);
3624 } else {
3625 if (use_imm) {
3626 rhs_reg = TMP;
3627 __ LoadConst64(rhs_reg, rhs_imm);
3628 }
3629 __ Xor(dst, lhs, rhs_reg);
3630 }
3631 if (cond == kCondEQ) {
3632 __ Sltiu(dst, dst, 1);
3633 } else {
3634 __ Sltu(dst, ZERO, dst);
3635 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003636 }
3637 break;
3638
3639 case kCondLT:
3640 case kCondGE:
3641 if (use_imm && IsInt<16>(rhs_imm)) {
3642 __ Slti(dst, lhs, rhs_imm);
3643 } else {
3644 if (use_imm) {
3645 rhs_reg = TMP;
3646 __ LoadConst64(rhs_reg, rhs_imm);
3647 }
3648 __ Slt(dst, lhs, rhs_reg);
3649 }
3650 if (cond == kCondGE) {
3651 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3652 // only the slt instruction but no sge.
3653 __ Xori(dst, dst, 1);
3654 }
3655 break;
3656
3657 case kCondLE:
3658 case kCondGT:
3659 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3660 // Simulate lhs <= rhs via lhs < rhs + 1.
3661 __ Slti(dst, lhs, rhs_imm_plus_one);
3662 if (cond == kCondGT) {
3663 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3664 // only the slti instruction but no sgti.
3665 __ Xori(dst, dst, 1);
3666 }
3667 } else {
3668 if (use_imm) {
3669 rhs_reg = TMP;
3670 __ LoadConst64(rhs_reg, rhs_imm);
3671 }
3672 __ Slt(dst, rhs_reg, lhs);
3673 if (cond == kCondLE) {
3674 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3675 // only the slt instruction but no sle.
3676 __ Xori(dst, dst, 1);
3677 }
3678 }
3679 break;
3680
3681 case kCondB:
3682 case kCondAE:
3683 if (use_imm && IsInt<16>(rhs_imm)) {
3684 // Sltiu sign-extends its 16-bit immediate operand before
3685 // the comparison and thus lets us compare directly with
3686 // unsigned values in the ranges [0, 0x7fff] and
3687 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3688 __ Sltiu(dst, lhs, rhs_imm);
3689 } else {
3690 if (use_imm) {
3691 rhs_reg = TMP;
3692 __ LoadConst64(rhs_reg, rhs_imm);
3693 }
3694 __ Sltu(dst, lhs, rhs_reg);
3695 }
3696 if (cond == kCondAE) {
3697 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3698 // only the sltu instruction but no sgeu.
3699 __ Xori(dst, dst, 1);
3700 }
3701 break;
3702
3703 case kCondBE:
3704 case kCondA:
3705 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3706 // Simulate lhs <= rhs via lhs < rhs + 1.
3707 // Note that this only works if rhs + 1 does not overflow
3708 // to 0, hence the check above.
3709 // Sltiu sign-extends its 16-bit immediate operand before
3710 // the comparison and thus lets us compare directly with
3711 // unsigned values in the ranges [0, 0x7fff] and
3712 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3713 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3714 if (cond == kCondA) {
3715 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3716 // only the sltiu instruction but no sgtiu.
3717 __ Xori(dst, dst, 1);
3718 }
3719 } else {
3720 if (use_imm) {
3721 rhs_reg = TMP;
3722 __ LoadConst64(rhs_reg, rhs_imm);
3723 }
3724 __ Sltu(dst, rhs_reg, lhs);
3725 if (cond == kCondBE) {
3726 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3727 // only the sltu instruction but no sleu.
3728 __ Xori(dst, dst, 1);
3729 }
3730 }
3731 break;
3732 }
3733}
3734
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02003735bool InstructionCodeGeneratorMIPS64::MaterializeIntLongCompare(IfCondition cond,
3736 bool is64bit,
3737 LocationSummary* input_locations,
3738 GpuRegister dst) {
3739 GpuRegister lhs = input_locations->InAt(0).AsRegister<GpuRegister>();
3740 Location rhs_location = input_locations->InAt(1);
3741 GpuRegister rhs_reg = ZERO;
3742 int64_t rhs_imm = 0;
3743 bool use_imm = rhs_location.IsConstant();
3744 if (use_imm) {
3745 if (is64bit) {
3746 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3747 } else {
3748 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3749 }
3750 } else {
3751 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3752 }
3753 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3754
3755 switch (cond) {
3756 case kCondEQ:
3757 case kCondNE:
3758 if (use_imm && IsInt<16>(-rhs_imm)) {
3759 if (is64bit) {
3760 __ Daddiu(dst, lhs, -rhs_imm);
3761 } else {
3762 __ Addiu(dst, lhs, -rhs_imm);
3763 }
3764 } else if (use_imm && IsUint<16>(rhs_imm)) {
3765 __ Xori(dst, lhs, rhs_imm);
3766 } else {
3767 if (use_imm) {
3768 rhs_reg = TMP;
3769 __ LoadConst64(rhs_reg, rhs_imm);
3770 }
3771 __ Xor(dst, lhs, rhs_reg);
3772 }
3773 return (cond == kCondEQ);
3774
3775 case kCondLT:
3776 case kCondGE:
3777 if (use_imm && IsInt<16>(rhs_imm)) {
3778 __ Slti(dst, lhs, rhs_imm);
3779 } else {
3780 if (use_imm) {
3781 rhs_reg = TMP;
3782 __ LoadConst64(rhs_reg, rhs_imm);
3783 }
3784 __ Slt(dst, lhs, rhs_reg);
3785 }
3786 return (cond == kCondGE);
3787
3788 case kCondLE:
3789 case kCondGT:
3790 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3791 // Simulate lhs <= rhs via lhs < rhs + 1.
3792 __ Slti(dst, lhs, rhs_imm_plus_one);
3793 return (cond == kCondGT);
3794 } else {
3795 if (use_imm) {
3796 rhs_reg = TMP;
3797 __ LoadConst64(rhs_reg, rhs_imm);
3798 }
3799 __ Slt(dst, rhs_reg, lhs);
3800 return (cond == kCondLE);
3801 }
3802
3803 case kCondB:
3804 case kCondAE:
3805 if (use_imm && IsInt<16>(rhs_imm)) {
3806 // Sltiu sign-extends its 16-bit immediate operand before
3807 // the comparison and thus lets us compare directly with
3808 // unsigned values in the ranges [0, 0x7fff] and
3809 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3810 __ Sltiu(dst, lhs, rhs_imm);
3811 } else {
3812 if (use_imm) {
3813 rhs_reg = TMP;
3814 __ LoadConst64(rhs_reg, rhs_imm);
3815 }
3816 __ Sltu(dst, lhs, rhs_reg);
3817 }
3818 return (cond == kCondAE);
3819
3820 case kCondBE:
3821 case kCondA:
3822 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3823 // Simulate lhs <= rhs via lhs < rhs + 1.
3824 // Note that this only works if rhs + 1 does not overflow
3825 // to 0, hence the check above.
3826 // Sltiu sign-extends its 16-bit immediate operand before
3827 // the comparison and thus lets us compare directly with
3828 // unsigned values in the ranges [0, 0x7fff] and
3829 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3830 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3831 return (cond == kCondA);
3832 } else {
3833 if (use_imm) {
3834 rhs_reg = TMP;
3835 __ LoadConst64(rhs_reg, rhs_imm);
3836 }
3837 __ Sltu(dst, rhs_reg, lhs);
3838 return (cond == kCondBE);
3839 }
3840 }
3841}
3842
Alexey Frunze299a9392015-12-08 16:08:02 -08003843void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3844 bool is64bit,
3845 LocationSummary* locations,
3846 Mips64Label* label) {
3847 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3848 Location rhs_location = locations->InAt(1);
3849 GpuRegister rhs_reg = ZERO;
3850 int64_t rhs_imm = 0;
3851 bool use_imm = rhs_location.IsConstant();
3852 if (use_imm) {
3853 if (is64bit) {
3854 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3855 } else {
3856 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3857 }
3858 } else {
3859 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3860 }
3861
3862 if (use_imm && rhs_imm == 0) {
3863 switch (cond) {
3864 case kCondEQ:
3865 case kCondBE: // <= 0 if zero
3866 __ Beqzc(lhs, label);
3867 break;
3868 case kCondNE:
3869 case kCondA: // > 0 if non-zero
3870 __ Bnezc(lhs, label);
3871 break;
3872 case kCondLT:
3873 __ Bltzc(lhs, label);
3874 break;
3875 case kCondGE:
3876 __ Bgezc(lhs, label);
3877 break;
3878 case kCondLE:
3879 __ Blezc(lhs, label);
3880 break;
3881 case kCondGT:
3882 __ Bgtzc(lhs, label);
3883 break;
3884 case kCondB: // always false
3885 break;
3886 case kCondAE: // always true
3887 __ Bc(label);
3888 break;
3889 }
3890 } else {
3891 if (use_imm) {
3892 rhs_reg = TMP;
3893 __ LoadConst64(rhs_reg, rhs_imm);
3894 }
3895 switch (cond) {
3896 case kCondEQ:
3897 __ Beqc(lhs, rhs_reg, label);
3898 break;
3899 case kCondNE:
3900 __ Bnec(lhs, rhs_reg, label);
3901 break;
3902 case kCondLT:
3903 __ Bltc(lhs, rhs_reg, label);
3904 break;
3905 case kCondGE:
3906 __ Bgec(lhs, rhs_reg, label);
3907 break;
3908 case kCondLE:
3909 __ Bgec(rhs_reg, lhs, label);
3910 break;
3911 case kCondGT:
3912 __ Bltc(rhs_reg, lhs, label);
3913 break;
3914 case kCondB:
3915 __ Bltuc(lhs, rhs_reg, label);
3916 break;
3917 case kCondAE:
3918 __ Bgeuc(lhs, rhs_reg, label);
3919 break;
3920 case kCondBE:
3921 __ Bgeuc(rhs_reg, lhs, label);
3922 break;
3923 case kCondA:
3924 __ Bltuc(rhs_reg, lhs, label);
3925 break;
3926 }
3927 }
3928}
3929
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003930void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3931 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003932 DataType::Type type,
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003933 LocationSummary* locations) {
3934 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3935 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3936 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003937 if (type == DataType::Type::kFloat32) {
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003938 switch (cond) {
3939 case kCondEQ:
3940 __ CmpEqS(FTMP, lhs, rhs);
3941 __ Mfc1(dst, FTMP);
3942 __ Andi(dst, dst, 1);
3943 break;
3944 case kCondNE:
3945 __ CmpEqS(FTMP, lhs, rhs);
3946 __ Mfc1(dst, FTMP);
3947 __ Addiu(dst, dst, 1);
3948 break;
3949 case kCondLT:
3950 if (gt_bias) {
3951 __ CmpLtS(FTMP, lhs, rhs);
3952 } else {
3953 __ CmpUltS(FTMP, lhs, rhs);
3954 }
3955 __ Mfc1(dst, FTMP);
3956 __ Andi(dst, dst, 1);
3957 break;
3958 case kCondLE:
3959 if (gt_bias) {
3960 __ CmpLeS(FTMP, lhs, rhs);
3961 } else {
3962 __ CmpUleS(FTMP, lhs, rhs);
3963 }
3964 __ Mfc1(dst, FTMP);
3965 __ Andi(dst, dst, 1);
3966 break;
3967 case kCondGT:
3968 if (gt_bias) {
3969 __ CmpUltS(FTMP, rhs, lhs);
3970 } else {
3971 __ CmpLtS(FTMP, rhs, lhs);
3972 }
3973 __ Mfc1(dst, FTMP);
3974 __ Andi(dst, dst, 1);
3975 break;
3976 case kCondGE:
3977 if (gt_bias) {
3978 __ CmpUleS(FTMP, rhs, lhs);
3979 } else {
3980 __ CmpLeS(FTMP, rhs, lhs);
3981 }
3982 __ Mfc1(dst, FTMP);
3983 __ Andi(dst, dst, 1);
3984 break;
3985 default:
3986 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3987 UNREACHABLE();
3988 }
3989 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003990 DCHECK_EQ(type, DataType::Type::kFloat64);
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003991 switch (cond) {
3992 case kCondEQ:
3993 __ CmpEqD(FTMP, lhs, rhs);
3994 __ Mfc1(dst, FTMP);
3995 __ Andi(dst, dst, 1);
3996 break;
3997 case kCondNE:
3998 __ CmpEqD(FTMP, lhs, rhs);
3999 __ Mfc1(dst, FTMP);
4000 __ Addiu(dst, dst, 1);
4001 break;
4002 case kCondLT:
4003 if (gt_bias) {
4004 __ CmpLtD(FTMP, lhs, rhs);
4005 } else {
4006 __ CmpUltD(FTMP, lhs, rhs);
4007 }
4008 __ Mfc1(dst, FTMP);
4009 __ Andi(dst, dst, 1);
4010 break;
4011 case kCondLE:
4012 if (gt_bias) {
4013 __ CmpLeD(FTMP, lhs, rhs);
4014 } else {
4015 __ CmpUleD(FTMP, lhs, rhs);
4016 }
4017 __ Mfc1(dst, FTMP);
4018 __ Andi(dst, dst, 1);
4019 break;
4020 case kCondGT:
4021 if (gt_bias) {
4022 __ CmpUltD(FTMP, rhs, lhs);
4023 } else {
4024 __ CmpLtD(FTMP, rhs, lhs);
4025 }
4026 __ Mfc1(dst, FTMP);
4027 __ Andi(dst, dst, 1);
4028 break;
4029 case kCondGE:
4030 if (gt_bias) {
4031 __ CmpUleD(FTMP, rhs, lhs);
4032 } else {
4033 __ CmpLeD(FTMP, rhs, lhs);
4034 }
4035 __ Mfc1(dst, FTMP);
4036 __ Andi(dst, dst, 1);
4037 break;
4038 default:
4039 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4040 UNREACHABLE();
4041 }
4042 }
4043}
4044
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004045bool InstructionCodeGeneratorMIPS64::MaterializeFpCompare(IfCondition cond,
4046 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004047 DataType::Type type,
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004048 LocationSummary* input_locations,
4049 FpuRegister dst) {
4050 FpuRegister lhs = input_locations->InAt(0).AsFpuRegister<FpuRegister>();
4051 FpuRegister rhs = input_locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004053 switch (cond) {
4054 case kCondEQ:
4055 __ CmpEqS(dst, lhs, rhs);
4056 return false;
4057 case kCondNE:
4058 __ CmpEqS(dst, lhs, rhs);
4059 return true;
4060 case kCondLT:
4061 if (gt_bias) {
4062 __ CmpLtS(dst, lhs, rhs);
4063 } else {
4064 __ CmpUltS(dst, lhs, rhs);
4065 }
4066 return false;
4067 case kCondLE:
4068 if (gt_bias) {
4069 __ CmpLeS(dst, lhs, rhs);
4070 } else {
4071 __ CmpUleS(dst, lhs, rhs);
4072 }
4073 return false;
4074 case kCondGT:
4075 if (gt_bias) {
4076 __ CmpUltS(dst, rhs, lhs);
4077 } else {
4078 __ CmpLtS(dst, rhs, lhs);
4079 }
4080 return false;
4081 case kCondGE:
4082 if (gt_bias) {
4083 __ CmpUleS(dst, rhs, lhs);
4084 } else {
4085 __ CmpLeS(dst, rhs, lhs);
4086 }
4087 return false;
4088 default:
4089 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4090 UNREACHABLE();
4091 }
4092 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004093 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004094 switch (cond) {
4095 case kCondEQ:
4096 __ CmpEqD(dst, lhs, rhs);
4097 return false;
4098 case kCondNE:
4099 __ CmpEqD(dst, lhs, rhs);
4100 return true;
4101 case kCondLT:
4102 if (gt_bias) {
4103 __ CmpLtD(dst, lhs, rhs);
4104 } else {
4105 __ CmpUltD(dst, lhs, rhs);
4106 }
4107 return false;
4108 case kCondLE:
4109 if (gt_bias) {
4110 __ CmpLeD(dst, lhs, rhs);
4111 } else {
4112 __ CmpUleD(dst, lhs, rhs);
4113 }
4114 return false;
4115 case kCondGT:
4116 if (gt_bias) {
4117 __ CmpUltD(dst, rhs, lhs);
4118 } else {
4119 __ CmpLtD(dst, rhs, lhs);
4120 }
4121 return false;
4122 case kCondGE:
4123 if (gt_bias) {
4124 __ CmpUleD(dst, rhs, lhs);
4125 } else {
4126 __ CmpLeD(dst, rhs, lhs);
4127 }
4128 return false;
4129 default:
4130 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4131 UNREACHABLE();
4132 }
4133 }
4134}
4135
Alexey Frunze299a9392015-12-08 16:08:02 -08004136void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
4137 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 DataType::Type type,
Alexey Frunze299a9392015-12-08 16:08:02 -08004139 LocationSummary* locations,
4140 Mips64Label* label) {
4141 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4142 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 if (type == DataType::Type::kFloat32) {
Alexey Frunze299a9392015-12-08 16:08:02 -08004144 switch (cond) {
4145 case kCondEQ:
4146 __ CmpEqS(FTMP, lhs, rhs);
4147 __ Bc1nez(FTMP, label);
4148 break;
4149 case kCondNE:
4150 __ CmpEqS(FTMP, lhs, rhs);
4151 __ Bc1eqz(FTMP, label);
4152 break;
4153 case kCondLT:
4154 if (gt_bias) {
4155 __ CmpLtS(FTMP, lhs, rhs);
4156 } else {
4157 __ CmpUltS(FTMP, lhs, rhs);
4158 }
4159 __ Bc1nez(FTMP, label);
4160 break;
4161 case kCondLE:
4162 if (gt_bias) {
4163 __ CmpLeS(FTMP, lhs, rhs);
4164 } else {
4165 __ CmpUleS(FTMP, lhs, rhs);
4166 }
4167 __ Bc1nez(FTMP, label);
4168 break;
4169 case kCondGT:
4170 if (gt_bias) {
4171 __ CmpUltS(FTMP, rhs, lhs);
4172 } else {
4173 __ CmpLtS(FTMP, rhs, lhs);
4174 }
4175 __ Bc1nez(FTMP, label);
4176 break;
4177 case kCondGE:
4178 if (gt_bias) {
4179 __ CmpUleS(FTMP, rhs, lhs);
4180 } else {
4181 __ CmpLeS(FTMP, rhs, lhs);
4182 }
4183 __ Bc1nez(FTMP, label);
4184 break;
4185 default:
4186 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004187 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004188 }
4189 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004190 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze299a9392015-12-08 16:08:02 -08004191 switch (cond) {
4192 case kCondEQ:
4193 __ CmpEqD(FTMP, lhs, rhs);
4194 __ Bc1nez(FTMP, label);
4195 break;
4196 case kCondNE:
4197 __ CmpEqD(FTMP, lhs, rhs);
4198 __ Bc1eqz(FTMP, label);
4199 break;
4200 case kCondLT:
4201 if (gt_bias) {
4202 __ CmpLtD(FTMP, lhs, rhs);
4203 } else {
4204 __ CmpUltD(FTMP, lhs, rhs);
4205 }
4206 __ Bc1nez(FTMP, label);
4207 break;
4208 case kCondLE:
4209 if (gt_bias) {
4210 __ CmpLeD(FTMP, lhs, rhs);
4211 } else {
4212 __ CmpUleD(FTMP, lhs, rhs);
4213 }
4214 __ Bc1nez(FTMP, label);
4215 break;
4216 case kCondGT:
4217 if (gt_bias) {
4218 __ CmpUltD(FTMP, rhs, lhs);
4219 } else {
4220 __ CmpLtD(FTMP, rhs, lhs);
4221 }
4222 __ Bc1nez(FTMP, label);
4223 break;
4224 case kCondGE:
4225 if (gt_bias) {
4226 __ CmpUleD(FTMP, rhs, lhs);
4227 } else {
4228 __ CmpLeD(FTMP, rhs, lhs);
4229 }
4230 __ Bc1nez(FTMP, label);
4231 break;
4232 default:
4233 LOG(FATAL) << "Unexpected non-floating-point condition";
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004234 UNREACHABLE();
Alexey Frunze299a9392015-12-08 16:08:02 -08004235 }
4236 }
4237}
4238
Alexey Frunze4dda3372015-06-01 18:31:49 -07004239void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004240 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004241 Mips64Label* true_target,
4242 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00004243 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004244
David Brazdil0debae72015-11-12 18:37:00 +00004245 if (true_target == nullptr && false_target == nullptr) {
4246 // Nothing to do. The code always falls through.
4247 return;
4248 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004249 // Constant condition, statically compared against "true" (integer value 1).
4250 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004251 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004252 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004253 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004254 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004255 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004256 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004257 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00004258 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004259 }
David Brazdil0debae72015-11-12 18:37:00 +00004260 return;
4261 }
4262
4263 // The following code generates these patterns:
4264 // (1) true_target == nullptr && false_target != nullptr
4265 // - opposite condition true => branch to false_target
4266 // (2) true_target != nullptr && false_target == nullptr
4267 // - condition true => branch to true_target
4268 // (3) true_target != nullptr && false_target != nullptr
4269 // - condition true => branch to true_target
4270 // - branch to false_target
4271 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004272 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004273 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004274 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00004275 if (true_target == nullptr) {
4276 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
4277 } else {
4278 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
4279 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004280 } else {
4281 // The condition instruction has not been materialized, use its inputs as
4282 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004283 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunze299a9392015-12-08 16:08:02 -08004285 LocationSummary* locations = cond->GetLocations();
4286 IfCondition if_cond = condition->GetCondition();
4287 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004288
David Brazdil0debae72015-11-12 18:37:00 +00004289 if (true_target == nullptr) {
4290 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08004291 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004292 }
4293
Alexey Frunze299a9392015-12-08 16:08:02 -08004294 switch (type) {
4295 default:
4296 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
4297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004298 case DataType::Type::kInt64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004299 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
4300 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004301 case DataType::Type::kFloat32:
4302 case DataType::Type::kFloat64:
Alexey Frunze299a9392015-12-08 16:08:02 -08004303 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4304 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004305 }
4306 }
David Brazdil0debae72015-11-12 18:37:00 +00004307
4308 // If neither branch falls through (case 3), the conditional branch to `true_target`
4309 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4310 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004311 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004312 }
4313}
4314
4315void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004316 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004317 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004318 locations->SetInAt(0, Location::RequiresRegister());
4319 }
4320}
4321
4322void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004323 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4324 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004325 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004326 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004327 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00004328 nullptr : codegen_->GetLabelOf(false_successor);
4329 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004330}
4331
4332void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004333 LocationSummary* locations = new (GetGraph()->GetAllocator())
Alexey Frunze4dda3372015-06-01 18:31:49 -07004334 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01004335 InvokeRuntimeCallingConvention calling_convention;
4336 RegisterSet caller_saves = RegisterSet::Empty();
4337 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4338 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00004339 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004340 locations->SetInAt(0, Location::RequiresRegister());
4341 }
4342}
4343
4344void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004345 SlowPathCodeMIPS64* slow_path =
4346 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004347 GenerateTestAndBranch(deoptimize,
4348 /* condition_input_index */ 0,
4349 slow_path->GetEntryLabel(),
4350 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004351}
4352
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004353// This function returns true if a conditional move can be generated for HSelect.
4354// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4355// branches and regular moves.
4356//
4357// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4358//
4359// While determining feasibility of a conditional move and setting inputs/outputs
4360// are two distinct tasks, this function does both because they share quite a bit
4361// of common logic.
4362static bool CanMoveConditionally(HSelect* select, LocationSummary* locations_to_set) {
4363 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4364 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4365 HCondition* condition = cond->AsCondition();
4366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 DataType::Type cond_type =
4368 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
4369 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004370
4371 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4372 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4373 bool is_true_value_zero_constant =
4374 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4375 bool is_false_value_zero_constant =
4376 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4377
4378 bool can_move_conditionally = false;
4379 bool use_const_for_false_in = false;
4380 bool use_const_for_true_in = false;
4381
4382 if (!cond->IsConstant()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004383 if (!DataType::IsFloatingPointType(cond_type)) {
4384 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004385 // Moving int/long on int/long condition.
4386 if (is_true_value_zero_constant) {
4387 // seleqz out_reg, false_reg, cond_reg
4388 can_move_conditionally = true;
4389 use_const_for_true_in = true;
4390 } else if (is_false_value_zero_constant) {
4391 // selnez out_reg, true_reg, cond_reg
4392 can_move_conditionally = true;
4393 use_const_for_false_in = true;
4394 } else if (materialized) {
4395 // Not materializing unmaterialized int conditions
4396 // to keep the instruction count low.
4397 // selnez AT, true_reg, cond_reg
4398 // seleqz TMP, false_reg, cond_reg
4399 // or out_reg, AT, TMP
4400 can_move_conditionally = true;
4401 }
4402 } else {
4403 // Moving float/double on int/long condition.
4404 if (materialized) {
4405 // Not materializing unmaterialized int conditions
4406 // to keep the instruction count low.
4407 can_move_conditionally = true;
4408 if (is_true_value_zero_constant) {
4409 // sltu TMP, ZERO, cond_reg
4410 // mtc1 TMP, temp_cond_reg
4411 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4412 use_const_for_true_in = true;
4413 } else if (is_false_value_zero_constant) {
4414 // sltu TMP, ZERO, cond_reg
4415 // mtc1 TMP, temp_cond_reg
4416 // selnez.fmt out_reg, true_reg, temp_cond_reg
4417 use_const_for_false_in = true;
4418 } else {
4419 // sltu TMP, ZERO, cond_reg
4420 // mtc1 TMP, temp_cond_reg
4421 // sel.fmt temp_cond_reg, false_reg, true_reg
4422 // mov.fmt out_reg, temp_cond_reg
4423 }
4424 }
4425 }
4426 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004427 if (!DataType::IsFloatingPointType(dst_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004428 // Moving int/long on float/double condition.
4429 can_move_conditionally = true;
4430 if (is_true_value_zero_constant) {
4431 // mfc1 TMP, temp_cond_reg
4432 // seleqz out_reg, false_reg, TMP
4433 use_const_for_true_in = true;
4434 } else if (is_false_value_zero_constant) {
4435 // mfc1 TMP, temp_cond_reg
4436 // selnez out_reg, true_reg, TMP
4437 use_const_for_false_in = true;
4438 } else {
4439 // mfc1 TMP, temp_cond_reg
4440 // selnez AT, true_reg, TMP
4441 // seleqz TMP, false_reg, TMP
4442 // or out_reg, AT, TMP
4443 }
4444 } else {
4445 // Moving float/double on float/double condition.
4446 can_move_conditionally = true;
4447 if (is_true_value_zero_constant) {
4448 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4449 use_const_for_true_in = true;
4450 } else if (is_false_value_zero_constant) {
4451 // selnez.fmt out_reg, true_reg, temp_cond_reg
4452 use_const_for_false_in = true;
4453 } else {
4454 // sel.fmt temp_cond_reg, false_reg, true_reg
4455 // mov.fmt out_reg, temp_cond_reg
4456 }
4457 }
4458 }
4459 }
4460
4461 if (can_move_conditionally) {
4462 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4463 } else {
4464 DCHECK(!use_const_for_false_in);
4465 DCHECK(!use_const_for_true_in);
4466 }
4467
4468 if (locations_to_set != nullptr) {
4469 if (use_const_for_false_in) {
4470 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4471 } else {
4472 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004473 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004474 ? Location::RequiresFpuRegister()
4475 : Location::RequiresRegister());
4476 }
4477 if (use_const_for_true_in) {
4478 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4479 } else {
4480 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004481 DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004482 ? Location::RequiresFpuRegister()
4483 : Location::RequiresRegister());
4484 }
4485 if (materialized) {
4486 locations_to_set->SetInAt(2, Location::RequiresRegister());
4487 }
4488
4489 if (can_move_conditionally) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004490 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004491 ? Location::RequiresFpuRegister()
4492 : Location::RequiresRegister());
4493 } else {
4494 locations_to_set->SetOut(Location::SameAsFirstInput());
4495 }
4496 }
4497
4498 return can_move_conditionally;
4499}
4500
4501
4502void InstructionCodeGeneratorMIPS64::GenConditionalMove(HSelect* select) {
4503 LocationSummary* locations = select->GetLocations();
4504 Location dst = locations->Out();
4505 Location false_src = locations->InAt(0);
4506 Location true_src = locations->InAt(1);
4507 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4508 GpuRegister cond_reg = TMP;
4509 FpuRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004510 DataType::Type cond_type = DataType::Type::kInt32;
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004511 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004512 DataType::Type dst_type = select->GetType();
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004513
4514 if (IsBooleanValueOrMaterializedCondition(cond)) {
4515 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<GpuRegister>();
4516 } else {
4517 HCondition* condition = cond->AsCondition();
4518 LocationSummary* cond_locations = cond->GetLocations();
4519 IfCondition if_cond = condition->GetCondition();
4520 cond_type = condition->InputAt(0)->GetType();
4521 switch (cond_type) {
4522 default:
4523 cond_inverted = MaterializeIntLongCompare(if_cond,
4524 /* is64bit */ false,
4525 cond_locations,
4526 cond_reg);
4527 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 case DataType::Type::kInt64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004529 cond_inverted = MaterializeIntLongCompare(if_cond,
4530 /* is64bit */ true,
4531 cond_locations,
4532 cond_reg);
4533 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004534 case DataType::Type::kFloat32:
4535 case DataType::Type::kFloat64:
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004536 cond_inverted = MaterializeFpCompare(if_cond,
4537 condition->IsGtBias(),
4538 cond_type,
4539 cond_locations,
4540 fcond_reg);
4541 break;
4542 }
4543 }
4544
4545 if (true_src.IsConstant()) {
4546 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4547 }
4548 if (false_src.IsConstant()) {
4549 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4550 }
4551
4552 switch (dst_type) {
4553 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004554 if (DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004555 __ Mfc1(cond_reg, fcond_reg);
4556 }
4557 if (true_src.IsConstant()) {
4558 if (cond_inverted) {
4559 __ Selnez(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4560 } else {
4561 __ Seleqz(dst.AsRegister<GpuRegister>(), false_src.AsRegister<GpuRegister>(), cond_reg);
4562 }
4563 } else if (false_src.IsConstant()) {
4564 if (cond_inverted) {
4565 __ Seleqz(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4566 } else {
4567 __ Selnez(dst.AsRegister<GpuRegister>(), true_src.AsRegister<GpuRegister>(), cond_reg);
4568 }
4569 } else {
4570 DCHECK_NE(cond_reg, AT);
4571 if (cond_inverted) {
4572 __ Seleqz(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4573 __ Selnez(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4574 } else {
4575 __ Selnez(AT, true_src.AsRegister<GpuRegister>(), cond_reg);
4576 __ Seleqz(TMP, false_src.AsRegister<GpuRegister>(), cond_reg);
4577 }
4578 __ Or(dst.AsRegister<GpuRegister>(), AT, TMP);
4579 }
4580 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004581 case DataType::Type::kFloat32: {
4582 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004583 // sel*.fmt tests bit 0 of the condition register, account for that.
4584 __ Sltu(TMP, ZERO, cond_reg);
4585 __ Mtc1(TMP, fcond_reg);
4586 }
4587 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4588 if (true_src.IsConstant()) {
4589 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4590 if (cond_inverted) {
4591 __ SelnezS(dst_reg, src_reg, fcond_reg);
4592 } else {
4593 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4594 }
4595 } else if (false_src.IsConstant()) {
4596 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4597 if (cond_inverted) {
4598 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4599 } else {
4600 __ SelnezS(dst_reg, src_reg, fcond_reg);
4601 }
4602 } else {
4603 if (cond_inverted) {
4604 __ SelS(fcond_reg,
4605 true_src.AsFpuRegister<FpuRegister>(),
4606 false_src.AsFpuRegister<FpuRegister>());
4607 } else {
4608 __ SelS(fcond_reg,
4609 false_src.AsFpuRegister<FpuRegister>(),
4610 true_src.AsFpuRegister<FpuRegister>());
4611 }
4612 __ MovS(dst_reg, fcond_reg);
4613 }
4614 break;
4615 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004616 case DataType::Type::kFloat64: {
4617 if (!DataType::IsFloatingPointType(cond_type)) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004618 // sel*.fmt tests bit 0 of the condition register, account for that.
4619 __ Sltu(TMP, ZERO, cond_reg);
4620 __ Mtc1(TMP, fcond_reg);
4621 }
4622 FpuRegister dst_reg = dst.AsFpuRegister<FpuRegister>();
4623 if (true_src.IsConstant()) {
4624 FpuRegister src_reg = false_src.AsFpuRegister<FpuRegister>();
4625 if (cond_inverted) {
4626 __ SelnezD(dst_reg, src_reg, fcond_reg);
4627 } else {
4628 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4629 }
4630 } else if (false_src.IsConstant()) {
4631 FpuRegister src_reg = true_src.AsFpuRegister<FpuRegister>();
4632 if (cond_inverted) {
4633 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4634 } else {
4635 __ SelnezD(dst_reg, src_reg, fcond_reg);
4636 }
4637 } else {
4638 if (cond_inverted) {
4639 __ SelD(fcond_reg,
4640 true_src.AsFpuRegister<FpuRegister>(),
4641 false_src.AsFpuRegister<FpuRegister>());
4642 } else {
4643 __ SelD(fcond_reg,
4644 false_src.AsFpuRegister<FpuRegister>(),
4645 true_src.AsFpuRegister<FpuRegister>());
4646 }
4647 __ MovD(dst_reg, fcond_reg);
4648 }
4649 break;
4650 }
4651 }
4652}
4653
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004654void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004655 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004656 LocationSummary(flag, LocationSummary::kNoCall);
4657 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004658}
4659
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004660void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4661 __ LoadFromOffset(kLoadWord,
4662 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
4663 SP,
4664 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004665}
4666
David Brazdil74eb1b22015-12-14 11:44:01 +00004667void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004668 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004669 CanMoveConditionally(select, locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004670}
4671
4672void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
Goran Jakovljevic2dec9272017-08-02 11:41:26 +02004673 if (CanMoveConditionally(select, /* locations_to_set */ nullptr)) {
4674 GenConditionalMove(select);
4675 } else {
4676 LocationSummary* locations = select->GetLocations();
4677 Mips64Label false_target;
4678 GenerateTestAndBranch(select,
4679 /* condition_input_index */ 2,
4680 /* true_target */ nullptr,
4681 &false_target);
4682 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4683 __ Bind(&false_target);
4684 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004685}
4686
David Srbecky0cf44932015-12-09 14:09:59 +00004687void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004688 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00004689}
4690
David Srbeckyd28f4a02016-03-14 17:14:24 +00004691void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
4692 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004693}
4694
4695void CodeGeneratorMIPS64::GenerateNop() {
4696 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004697}
4698
Alexey Frunze4dda3372015-06-01 18:31:49 -07004699void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08004700 const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 DataType::Type field_type = field_info.GetFieldType();
Alexey Frunze15958152017-02-09 19:08:30 -08004702 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004703 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01004704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08004705 instruction,
4706 object_field_get_with_read_barrier
4707 ? LocationSummary::kCallOnSlowPath
4708 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004709 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4710 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4711 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004712 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004713 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004714 locations->SetOut(Location::RequiresFpuRegister());
4715 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004716 // The output overlaps in the case of an object field get with
4717 // read barriers enabled: we do not want the move to overwrite the
4718 // object's location, as we need it to emit the read barrier.
4719 locations->SetOut(Location::RequiresRegister(),
4720 object_field_get_with_read_barrier
4721 ? Location::kOutputOverlap
4722 : Location::kNoOutputOverlap);
4723 }
4724 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4725 // We need a temporary register for the read barrier marking slow
4726 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004727 if (!kBakerReadBarrierThunksEnableForFields) {
4728 locations->AddTemp(Location::RequiresRegister());
4729 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004730 }
4731}
4732
4733void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4734 const FieldInfo& field_info) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004735 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4736 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004737 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004738 Location obj_loc = locations->InAt(0);
4739 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4740 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004741 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004742 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004743 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004744 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4745
Alexey Frunze4dda3372015-06-01 18:31:49 -07004746 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004748 case DataType::Type::kUint8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749 load_type = kLoadUnsignedByte;
4750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004752 load_type = kLoadSignedByte;
4753 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004754 case DataType::Type::kUint16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004755 load_type = kLoadUnsignedHalfword;
4756 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004757 case DataType::Type::kInt16:
4758 load_type = kLoadSignedHalfword;
4759 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 case DataType::Type::kInt32:
4761 case DataType::Type::kFloat32:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004762 load_type = kLoadWord;
4763 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004764 case DataType::Type::kInt64:
4765 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 load_type = kLoadDoubleword;
4767 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004769 load_type = kLoadUnsignedWord;
4770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004771 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004772 LOG(FATAL) << "Unreachable type " << type;
4773 UNREACHABLE();
4774 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 if (!DataType::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004776 DCHECK(dst_loc.IsRegister());
4777 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004778 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08004779 // /* HeapReference<Object> */ dst = *(obj + offset)
4780 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004781 Location temp_loc =
4782 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08004783 // Note that a potential implicit null check is handled in this
4784 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4785 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4786 dst_loc,
4787 obj,
4788 offset,
4789 temp_loc,
4790 /* needs_null_check */ true);
4791 if (is_volatile) {
4792 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4793 }
4794 } else {
4795 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4796 if (is_volatile) {
4797 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4798 }
4799 // If read barriers are enabled, emit read barriers other than
4800 // Baker's using a slow path (and also unpoison the loaded
4801 // reference, if heap poisoning is enabled).
4802 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4803 }
4804 } else {
4805 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4806 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004807 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004808 DCHECK(dst_loc.IsFpuRegister());
4809 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004810 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004811 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004812
Alexey Frunze15958152017-02-09 19:08:30 -08004813 // Memory barriers, in the case of references, are handled in the
4814 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 if (is_volatile && (type != DataType::Type::kReference)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004816 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004817 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004818}
4819
4820void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4821 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4822 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004823 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004824 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004825 if (DataType::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004826 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004827 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004828 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004829 }
4830}
4831
4832void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004833 const FieldInfo& field_info,
4834 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004835 DataType::Type type = field_info.GetFieldType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004836 LocationSummary* locations = instruction->GetLocations();
4837 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004838 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004839 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004840 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004841 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4842 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004843 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4844
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004846 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004847 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004848 case DataType::Type::kInt8:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004849 store_type = kStoreByte;
4850 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004851 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004852 case DataType::Type::kInt16:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004853 store_type = kStoreHalfword;
4854 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 case DataType::Type::kInt32:
4856 case DataType::Type::kFloat32:
4857 case DataType::Type::kReference:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004858 store_type = kStoreWord;
4859 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kInt64:
4861 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862 store_type = kStoreDoubleword;
4863 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 case DataType::Type::kVoid:
Alexey Frunze4dda3372015-06-01 18:31:49 -07004865 LOG(FATAL) << "Unreachable type " << type;
4866 UNREACHABLE();
4867 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004868
Alexey Frunze15958152017-02-09 19:08:30 -08004869 if (is_volatile) {
4870 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4871 }
4872
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004873 if (value_location.IsConstant()) {
4874 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4875 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4876 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 if (!DataType::IsFloatingPointType(type)) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004878 DCHECK(value_location.IsRegister());
4879 GpuRegister src = value_location.AsRegister<GpuRegister>();
4880 if (kPoisonHeapReferences && needs_write_barrier) {
4881 // Note that in the case where `value` is a null reference,
4882 // we do not enter this block, as a null reference does not
4883 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 DCHECK_EQ(type, DataType::Type::kReference);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004885 __ PoisonHeapReference(TMP, src);
4886 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4887 } else {
4888 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4889 }
4890 } else {
4891 DCHECK(value_location.IsFpuRegister());
4892 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4893 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4894 }
4895 }
Alexey Frunze15958152017-02-09 19:08:30 -08004896
Alexey Frunzec061de12017-02-14 13:27:23 -08004897 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004898 DCHECK(value_location.IsRegister());
4899 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004900 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004901 }
Alexey Frunze15958152017-02-09 19:08:30 -08004902
4903 if (is_volatile) {
4904 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4905 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004906}
4907
4908void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4909 HandleFieldGet(instruction, instruction->GetFieldInfo());
4910}
4911
4912void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4913 HandleFieldGet(instruction, instruction->GetFieldInfo());
4914}
4915
4916void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4917 HandleFieldSet(instruction, instruction->GetFieldInfo());
4918}
4919
4920void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004921 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004922}
4923
Alexey Frunze15958152017-02-09 19:08:30 -08004924void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4925 HInstruction* instruction,
4926 Location out,
4927 uint32_t offset,
4928 Location maybe_temp,
4929 ReadBarrierOption read_barrier_option) {
4930 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4931 if (read_barrier_option == kWithReadBarrier) {
4932 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004933 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
4934 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4935 }
Alexey Frunze15958152017-02-09 19:08:30 -08004936 if (kUseBakerReadBarrier) {
4937 // Load with fast path based Baker's read barrier.
4938 // /* HeapReference<Object> */ out = *(out + offset)
4939 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4940 out,
4941 out_reg,
4942 offset,
4943 maybe_temp,
4944 /* needs_null_check */ false);
4945 } else {
4946 // Load with slow path based read barrier.
4947 // Save the value of `out` into `maybe_temp` before overwriting it
4948 // in the following move operation, as we will need it for the
4949 // read barrier below.
4950 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4951 // /* HeapReference<Object> */ out = *(out + offset)
4952 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4953 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4954 }
4955 } else {
4956 // Plain load with no read barrier.
4957 // /* HeapReference<Object> */ out = *(out + offset)
4958 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4959 __ MaybeUnpoisonHeapReference(out_reg);
4960 }
4961}
4962
4963void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4964 HInstruction* instruction,
4965 Location out,
4966 Location obj,
4967 uint32_t offset,
4968 Location maybe_temp,
4969 ReadBarrierOption read_barrier_option) {
4970 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4971 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4972 if (read_barrier_option == kWithReadBarrier) {
4973 CHECK(kEmitCompilerReadBarrier);
4974 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07004975 if (!kBakerReadBarrierThunksEnableForFields) {
4976 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4977 }
Alexey Frunze15958152017-02-09 19:08:30 -08004978 // Load with fast path based Baker's read barrier.
4979 // /* HeapReference<Object> */ out = *(obj + offset)
4980 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4981 out,
4982 obj_reg,
4983 offset,
4984 maybe_temp,
4985 /* needs_null_check */ false);
4986 } else {
4987 // Load with slow path based read barrier.
4988 // /* HeapReference<Object> */ out = *(obj + offset)
4989 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4990 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4991 }
4992 } else {
4993 // Plain load with no read barrier.
4994 // /* HeapReference<Object> */ out = *(obj + offset)
4995 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4996 __ MaybeUnpoisonHeapReference(out_reg);
4997 }
4998}
4999
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005000static inline int GetBakerMarkThunkNumber(GpuRegister reg) {
5001 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 20, "Expecting equal");
5002 if (reg >= V0 && reg <= T2) { // 13 consequtive regs.
5003 return reg - V0;
5004 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
5005 return 13 + (reg - S2);
5006 } else if (reg == S8) { // One more.
5007 return 19;
5008 }
5009 LOG(FATAL) << "Unexpected register " << reg;
5010 UNREACHABLE();
5011}
5012
5013static inline int GetBakerMarkFieldArrayThunkDisplacement(GpuRegister reg, bool short_offset) {
5014 int num = GetBakerMarkThunkNumber(reg) +
5015 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
5016 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
5017}
5018
5019static inline int GetBakerMarkGcRootThunkDisplacement(GpuRegister reg) {
5020 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
5021 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
5022}
5023
5024void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(HInstruction* instruction,
5025 Location root,
5026 GpuRegister obj,
5027 uint32_t offset,
5028 ReadBarrierOption read_barrier_option,
5029 Mips64Label* label_low) {
5030 if (label_low != nullptr) {
5031 DCHECK_EQ(offset, 0x5678u);
5032 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005033 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005034 if (read_barrier_option == kWithReadBarrier) {
5035 DCHECK(kEmitCompilerReadBarrier);
5036 if (kUseBakerReadBarrier) {
5037 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5038 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005039 if (kBakerReadBarrierThunksEnableForGcRoots) {
5040 // Note that we do not actually check the value of `GetIsGcMarking()`
5041 // to decide whether to mark the loaded GC root or not. Instead, we
5042 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5043 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5044 // vice versa.
5045 //
5046 // We use thunks for the slow path. That thunk checks the reference
5047 // and jumps to the entrypoint if needed.
5048 //
5049 // temp = Thread::Current()->pReadBarrierMarkReg00
5050 // // AKA &art_quick_read_barrier_mark_introspection.
5051 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5052 // if (temp != nullptr) {
5053 // temp = &gc_root_thunk<root_reg>
5054 // root = temp(root)
5055 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005056
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005057 const int32_t entry_point_offset =
5058 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5059 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
5060 int16_t offset_low = Low16Bits(offset);
5061 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
5062 // extension in lwu.
5063 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5064 GpuRegister base = short_offset ? obj : TMP;
5065 // Loading the entrypoint does not require a load acquire since it is only changed when
5066 // threads are suspended or running a checkpoint.
5067 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5068 if (!short_offset) {
5069 DCHECK(!label_low);
5070 __ Daui(base, obj, offset_high);
5071 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07005072 Mips64Label skip_call;
5073 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005074 if (label_low != nullptr) {
5075 DCHECK(short_offset);
5076 __ Bind(label_low);
5077 }
5078 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5079 __ LoadFromOffset(kLoadUnsignedWord, root_reg, base, offset_low); // Single instruction
5080 // in delay slot.
5081 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005082 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005083 } else {
5084 // Note that we do not actually check the value of `GetIsGcMarking()`
5085 // to decide whether to mark the loaded GC root or not. Instead, we
5086 // load into `temp` (T9) the read barrier mark entry point corresponding
5087 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
5088 // is false, and vice versa.
5089 //
5090 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5091 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5092 // if (temp != null) {
5093 // root = temp(root)
5094 // }
Alexey Frunze15958152017-02-09 19:08:30 -08005095
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005096 if (label_low != nullptr) {
5097 __ Bind(label_low);
5098 }
5099 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5100 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5101 static_assert(
5102 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5103 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5104 "have different sizes.");
5105 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5106 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5107 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08005108
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005109 // Slow path marking the GC root `root`.
5110 Location temp = Location::RegisterLocation(T9);
5111 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005112 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005113 instruction,
5114 root,
5115 /*entrypoint*/ temp);
5116 codegen_->AddSlowPath(slow_path);
5117
5118 const int32_t entry_point_offset =
5119 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
5120 // Loading the entrypoint does not require a load acquire since it is only changed when
5121 // threads are suspended or running a checkpoint.
5122 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
5123 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5124 __ Bind(slow_path->GetExitLabel());
5125 }
Alexey Frunze15958152017-02-09 19:08:30 -08005126 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005127 if (label_low != nullptr) {
5128 __ Bind(label_low);
5129 }
Alexey Frunze15958152017-02-09 19:08:30 -08005130 // GC root loaded through a slow path for read barriers other
5131 // than Baker's.
5132 // /* GcRoot<mirror::Object>* */ root = obj + offset
5133 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
5134 // /* mirror::Object* */ root = root->Read()
5135 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5136 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005137 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005138 if (label_low != nullptr) {
5139 __ Bind(label_low);
5140 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005141 // Plain GC root load with no read barrier.
5142 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5143 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
5144 // Note that GC roots are not affected by heap poisoning, thus we
5145 // do not have to unpoison `root_reg` here.
5146 }
5147}
5148
Alexey Frunze15958152017-02-09 19:08:30 -08005149void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5150 Location ref,
5151 GpuRegister obj,
5152 uint32_t offset,
5153 Location temp,
5154 bool needs_null_check) {
5155 DCHECK(kEmitCompilerReadBarrier);
5156 DCHECK(kUseBakerReadBarrier);
5157
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005158 if (kBakerReadBarrierThunksEnableForFields) {
5159 // Note that we do not actually check the value of `GetIsGcMarking()`
5160 // to decide whether to mark the loaded reference or not. Instead, we
5161 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5162 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5163 // vice versa.
5164 //
5165 // We use thunks for the slow path. That thunk checks the reference
5166 // and jumps to the entrypoint if needed. If the holder is not gray,
5167 // it issues a load-load memory barrier and returns to the original
5168 // reference load.
5169 //
5170 // temp = Thread::Current()->pReadBarrierMarkReg00
5171 // // AKA &art_quick_read_barrier_mark_introspection.
5172 // if (temp != nullptr) {
5173 // temp = &field_array_thunk<holder_reg>
5174 // temp()
5175 // }
5176 // not_gray_return_address:
5177 // // If the offset is too large to fit into the lw instruction, we
5178 // // use an adjusted base register (TMP) here. This register
5179 // // receives bits 16 ... 31 of the offset before the thunk invocation
5180 // // and the thunk benefits from it.
5181 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
5182 // gray_return_address:
5183
5184 DCHECK(temp.IsInvalid());
5185 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
5186 const int32_t entry_point_offset =
5187 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5188 // There may have or may have not been a null check if the field offset is smaller than
5189 // the page size.
5190 // There must've been a null check in case it's actually a load from an array.
5191 // We will, however, perform an explicit null check in the thunk as it's easier to
5192 // do it than not.
5193 if (instruction->IsArrayGet()) {
5194 DCHECK(!needs_null_check);
5195 }
5196 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
5197 // Loading the entrypoint does not require a load acquire since it is only changed when
5198 // threads are suspended or running a checkpoint.
5199 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
5200 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07005201 Mips64Label skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005202 if (short_offset) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07005203 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005204 __ Nop(); // In forbidden slot.
5205 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005206 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005207 // /* HeapReference<Object> */ ref = *(obj + offset)
5208 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset); // Single instruction.
5209 } else {
5210 int16_t offset_low = Low16Bits(offset);
5211 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lwu.
Alexey Frunze0cab6562017-07-25 15:19:36 -07005212 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005213 __ Daui(TMP, obj, offset_high); // In delay slot.
5214 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005215 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005216 // /* HeapReference<Object> */ ref = *(obj + offset)
5217 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset_low); // Single instruction.
5218 }
5219 if (needs_null_check) {
5220 MaybeRecordImplicitNullCheck(instruction);
5221 }
5222 __ MaybeUnpoisonHeapReference(ref_reg);
5223 return;
5224 }
5225
Alexey Frunze15958152017-02-09 19:08:30 -08005226 // /* HeapReference<Object> */ ref = *(obj + offset)
5227 Location no_index = Location::NoLocation();
5228 ScaleFactor no_scale_factor = TIMES_1;
5229 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5230 ref,
5231 obj,
5232 offset,
5233 no_index,
5234 no_scale_factor,
5235 temp,
5236 needs_null_check);
5237}
5238
5239void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5240 Location ref,
5241 GpuRegister obj,
5242 uint32_t data_offset,
5243 Location index,
5244 Location temp,
5245 bool needs_null_check) {
5246 DCHECK(kEmitCompilerReadBarrier);
5247 DCHECK(kUseBakerReadBarrier);
5248
5249 static_assert(
5250 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5251 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005252 ScaleFactor scale_factor = TIMES_4;
5253
5254 if (kBakerReadBarrierThunksEnableForArrays) {
5255 // Note that we do not actually check the value of `GetIsGcMarking()`
5256 // to decide whether to mark the loaded reference or not. Instead, we
5257 // load into `temp` (T9) the read barrier mark introspection entrypoint.
5258 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5259 // vice versa.
5260 //
5261 // We use thunks for the slow path. That thunk checks the reference
5262 // and jumps to the entrypoint if needed. If the holder is not gray,
5263 // it issues a load-load memory barrier and returns to the original
5264 // reference load.
5265 //
5266 // temp = Thread::Current()->pReadBarrierMarkReg00
5267 // // AKA &art_quick_read_barrier_mark_introspection.
5268 // if (temp != nullptr) {
5269 // temp = &field_array_thunk<holder_reg>
5270 // temp()
5271 // }
5272 // not_gray_return_address:
5273 // // The element address is pre-calculated in the TMP register before the
5274 // // thunk invocation and the thunk benefits from it.
5275 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
5276 // gray_return_address:
5277
5278 DCHECK(temp.IsInvalid());
5279 DCHECK(index.IsValid());
5280 const int32_t entry_point_offset =
5281 Thread::ReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(0);
5282 // We will not do the explicit null check in the thunk as some form of a null check
5283 // must've been done earlier.
5284 DCHECK(!needs_null_check);
5285 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
5286 // Loading the entrypoint does not require a load acquire since it is only changed when
5287 // threads are suspended or running a checkpoint.
5288 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005289 Mips64Label skip_call;
5290 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005291 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5292 GpuRegister index_reg = index.AsRegister<GpuRegister>();
5293 __ Dlsa(TMP, index_reg, obj, scale_factor); // In delay slot.
5294 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07005295 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07005296 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
5297 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
5298 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, data_offset); // Single instruction.
5299 __ MaybeUnpoisonHeapReference(ref_reg);
5300 return;
5301 }
5302
Alexey Frunze15958152017-02-09 19:08:30 -08005303 // /* HeapReference<Object> */ ref =
5304 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08005305 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5306 ref,
5307 obj,
5308 data_offset,
5309 index,
5310 scale_factor,
5311 temp,
5312 needs_null_check);
5313}
5314
5315void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5316 Location ref,
5317 GpuRegister obj,
5318 uint32_t offset,
5319 Location index,
5320 ScaleFactor scale_factor,
5321 Location temp,
5322 bool needs_null_check,
5323 bool always_update_field) {
5324 DCHECK(kEmitCompilerReadBarrier);
5325 DCHECK(kUseBakerReadBarrier);
5326
5327 // In slow path based read barriers, the read barrier call is
5328 // inserted after the original load. However, in fast path based
5329 // Baker's read barriers, we need to perform the load of
5330 // mirror::Object::monitor_ *before* the original reference load.
5331 // This load-load ordering is required by the read barrier.
5332 // The fast path/slow path (for Baker's algorithm) should look like:
5333 //
5334 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5335 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5336 // HeapReference<Object> ref = *src; // Original reference load.
5337 // bool is_gray = (rb_state == ReadBarrier::GrayState());
5338 // if (is_gray) {
5339 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5340 // }
5341 //
5342 // Note: the original implementation in ReadBarrier::Barrier is
5343 // slightly more complex as it performs additional checks that we do
5344 // not do here for performance reasons.
5345
5346 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
5347 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
5348 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5349
5350 // /* int32_t */ monitor = obj->monitor_
5351 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
5352 if (needs_null_check) {
5353 MaybeRecordImplicitNullCheck(instruction);
5354 }
5355 // /* LockWord */ lock_word = LockWord(monitor)
5356 static_assert(sizeof(LockWord) == sizeof(int32_t),
5357 "art::LockWord and int32_t have different sizes.");
5358
5359 __ Sync(0); // Barrier to prevent load-load reordering.
5360
5361 // The actual reference load.
5362 if (index.IsValid()) {
5363 // Load types involving an "index": ArrayGet,
5364 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
5365 // intrinsics.
5366 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5367 if (index.IsConstant()) {
5368 size_t computed_offset =
5369 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
5370 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
5371 } else {
5372 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07005373 if (scale_factor == TIMES_1) {
5374 __ Daddu(TMP, index_reg, obj);
5375 } else {
5376 __ Dlsa(TMP, index_reg, obj, scale_factor);
5377 }
Alexey Frunze15958152017-02-09 19:08:30 -08005378 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
5379 }
5380 } else {
5381 // /* HeapReference<Object> */ ref = *(obj + offset)
5382 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
5383 }
5384
5385 // Object* ref = ref_addr->AsMirrorPtr()
5386 __ MaybeUnpoisonHeapReference(ref_reg);
5387
5388 // Slow path marking the object `ref` when it is gray.
5389 SlowPathCodeMIPS64* slow_path;
5390 if (always_update_field) {
5391 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
5392 // of the form `obj + field_offset`, where `obj` is a register and
5393 // `field_offset` is a register. Thus `offset` and `scale_factor`
5394 // above are expected to be null in this code path.
5395 DCHECK_EQ(offset, 0u);
5396 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01005397 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005398 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
5399 ref,
5400 obj,
5401 /* field_offset */ index,
5402 temp_reg);
5403 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005404 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08005405 }
5406 AddSlowPath(slow_path);
5407
5408 // if (rb_state == ReadBarrier::GrayState())
5409 // ref = ReadBarrier::Mark(ref);
5410 // Given the numeric representation, it's enough to check the low bit of the
5411 // rb_state. We do that by shifting the bit into the sign bit (31) and
5412 // performing a branch on less than zero.
5413 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
5414 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
5415 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
5416 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
5417 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
5418 __ Bind(slow_path->GetExitLabel());
5419}
5420
5421void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
5422 Location out,
5423 Location ref,
5424 Location obj,
5425 uint32_t offset,
5426 Location index) {
5427 DCHECK(kEmitCompilerReadBarrier);
5428
5429 // Insert a slow path based read barrier *after* the reference load.
5430 //
5431 // If heap poisoning is enabled, the unpoisoning of the loaded
5432 // reference will be carried out by the runtime within the slow
5433 // path.
5434 //
5435 // Note that `ref` currently does not get unpoisoned (when heap
5436 // poisoning is enabled), which is alright as the `ref` argument is
5437 // not used by the artReadBarrierSlow entry point.
5438 //
5439 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01005440 SlowPathCodeMIPS64* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08005441 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
5442 AddSlowPath(slow_path);
5443
5444 __ Bc(slow_path->GetEntryLabel());
5445 __ Bind(slow_path->GetExitLabel());
5446}
5447
5448void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5449 Location out,
5450 Location ref,
5451 Location obj,
5452 uint32_t offset,
5453 Location index) {
5454 if (kEmitCompilerReadBarrier) {
5455 // Baker's read barriers shall be handled by the fast path
5456 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
5457 DCHECK(!kUseBakerReadBarrier);
5458 // If heap poisoning is enabled, unpoisoning will be taken care of
5459 // by the runtime within the slow path.
5460 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
5461 } else if (kPoisonHeapReferences) {
5462 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
5463 }
5464}
5465
5466void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5467 Location out,
5468 Location root) {
5469 DCHECK(kEmitCompilerReadBarrier);
5470
5471 // Insert a slow path based read barrier *after* the GC root load.
5472 //
5473 // Note that GC roots are not affected by heap poisoning, so we do
5474 // not need to do anything special for this here.
5475 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005476 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08005477 AddSlowPath(slow_path);
5478
5479 __ Bc(slow_path->GetEntryLabel());
5480 __ Bind(slow_path->GetExitLabel());
5481}
5482
Alexey Frunze4dda3372015-06-01 18:31:49 -07005483void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005484 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5485 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07005486 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005487 switch (type_check_kind) {
5488 case TypeCheckKind::kExactCheck:
5489 case TypeCheckKind::kAbstractClassCheck:
5490 case TypeCheckKind::kClassHierarchyCheck:
5491 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08005492 call_kind =
5493 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005494 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005495 break;
5496 case TypeCheckKind::kArrayCheck:
5497 case TypeCheckKind::kUnresolvedCheck:
5498 case TypeCheckKind::kInterfaceCheck:
5499 call_kind = LocationSummary::kCallOnSlowPath;
5500 break;
5501 }
5502
Vladimir Markoca6fff82017-10-03 14:49:14 +01005503 LocationSummary* locations =
5504 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005505 if (baker_read_barrier_slow_path) {
5506 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5507 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005508 locations->SetInAt(0, Location::RequiresRegister());
5509 locations->SetInAt(1, Location::RequiresRegister());
5510 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005511 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005512 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08005513 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005514}
5515
5516void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005517 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005518 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08005519 Location obj_loc = locations->InAt(0);
5520 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005521 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08005522 Location out_loc = locations->Out();
5523 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5524 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
5525 DCHECK_LE(num_temps, 1u);
5526 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005527 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5528 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5529 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5530 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005531 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005532 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005533
5534 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005535 // Avoid this check if we know `obj` is not null.
5536 if (instruction->MustDoNullCheck()) {
5537 __ Move(out, ZERO);
5538 __ Beqzc(obj, &done);
5539 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005540
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005541 switch (type_check_kind) {
5542 case TypeCheckKind::kExactCheck: {
5543 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005544 GenerateReferenceLoadTwoRegisters(instruction,
5545 out_loc,
5546 obj_loc,
5547 class_offset,
5548 maybe_temp_loc,
5549 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005550 // Classes must be equal for the instanceof to succeed.
5551 __ Xor(out, out, cls);
5552 __ Sltiu(out, out, 1);
5553 break;
5554 }
5555
5556 case TypeCheckKind::kAbstractClassCheck: {
5557 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005558 GenerateReferenceLoadTwoRegisters(instruction,
5559 out_loc,
5560 obj_loc,
5561 class_offset,
5562 maybe_temp_loc,
5563 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005564 // If the class is abstract, we eagerly fetch the super class of the
5565 // object to avoid doing a comparison we know will fail.
5566 Mips64Label loop;
5567 __ Bind(&loop);
5568 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005569 GenerateReferenceLoadOneRegister(instruction,
5570 out_loc,
5571 super_offset,
5572 maybe_temp_loc,
5573 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005574 // If `out` is null, we use it for the result, and jump to `done`.
5575 __ Beqzc(out, &done);
5576 __ Bnec(out, cls, &loop);
5577 __ LoadConst32(out, 1);
5578 break;
5579 }
5580
5581 case TypeCheckKind::kClassHierarchyCheck: {
5582 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005583 GenerateReferenceLoadTwoRegisters(instruction,
5584 out_loc,
5585 obj_loc,
5586 class_offset,
5587 maybe_temp_loc,
5588 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005589 // Walk over the class hierarchy to find a match.
5590 Mips64Label loop, success;
5591 __ Bind(&loop);
5592 __ Beqc(out, cls, &success);
5593 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08005594 GenerateReferenceLoadOneRegister(instruction,
5595 out_loc,
5596 super_offset,
5597 maybe_temp_loc,
5598 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005599 __ Bnezc(out, &loop);
5600 // If `out` is null, we use it for the result, and jump to `done`.
5601 __ Bc(&done);
5602 __ Bind(&success);
5603 __ LoadConst32(out, 1);
5604 break;
5605 }
5606
5607 case TypeCheckKind::kArrayObjectCheck: {
5608 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005609 GenerateReferenceLoadTwoRegisters(instruction,
5610 out_loc,
5611 obj_loc,
5612 class_offset,
5613 maybe_temp_loc,
5614 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005615 // Do an exact check.
5616 Mips64Label success;
5617 __ Beqc(out, cls, &success);
5618 // Otherwise, we need to check that the object's class is a non-primitive array.
5619 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08005620 GenerateReferenceLoadOneRegister(instruction,
5621 out_loc,
5622 component_offset,
5623 maybe_temp_loc,
5624 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005625 // If `out` is null, we use it for the result, and jump to `done`.
5626 __ Beqzc(out, &done);
5627 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5628 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5629 __ Sltiu(out, out, 1);
5630 __ Bc(&done);
5631 __ Bind(&success);
5632 __ LoadConst32(out, 1);
5633 break;
5634 }
5635
5636 case TypeCheckKind::kArrayCheck: {
5637 // No read barrier since the slow path will retry upon failure.
5638 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08005639 GenerateReferenceLoadTwoRegisters(instruction,
5640 out_loc,
5641 obj_loc,
5642 class_offset,
5643 maybe_temp_loc,
5644 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005645 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005646 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5647 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005648 codegen_->AddSlowPath(slow_path);
5649 __ Bnec(out, cls, slow_path->GetEntryLabel());
5650 __ LoadConst32(out, 1);
5651 break;
5652 }
5653
5654 case TypeCheckKind::kUnresolvedCheck:
5655 case TypeCheckKind::kInterfaceCheck: {
5656 // Note that we indeed only call on slow path, but we always go
5657 // into the slow path for the unresolved and interface check
5658 // cases.
5659 //
5660 // We cannot directly call the InstanceofNonTrivial runtime
5661 // entry point without resorting to a type checking slow path
5662 // here (i.e. by calling InvokeRuntime directly), as it would
5663 // require to assign fixed registers for the inputs of this
5664 // HInstanceOf instruction (following the runtime calling
5665 // convention), which might be cluttered by the potential first
5666 // read barrier emission at the beginning of this method.
5667 //
5668 // TODO: Introduce a new runtime entry point taking the object
5669 // to test (instead of its class) as argument, and let it deal
5670 // with the read barrier issues. This will let us refactor this
5671 // case of the `switch` code as it was previously (with a direct
5672 // call to the runtime not using a type checking slow path).
5673 // This should also be beneficial for the other cases above.
5674 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01005675 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS64(
5676 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005677 codegen_->AddSlowPath(slow_path);
5678 __ Bc(slow_path->GetEntryLabel());
5679 break;
5680 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005681 }
5682
5683 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08005684
5685 if (slow_path != nullptr) {
5686 __ Bind(slow_path->GetExitLabel());
5687 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005688}
5689
5690void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005692 locations->SetOut(Location::ConstantLocation(constant));
5693}
5694
5695void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5696 // Will be generated at use site.
5697}
5698
5699void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005701 locations->SetOut(Location::ConstantLocation(constant));
5702}
5703
5704void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5705 // Will be generated at use site.
5706}
5707
Calin Juravle175dc732015-08-25 15:42:32 +01005708void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5709 // The trampoline uses the same calling convention as dex calling conventions,
5710 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5711 // the method_idx.
5712 HandleInvoke(invoke);
5713}
5714
5715void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5716 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5717}
5718
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
5720 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
5721 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5722}
5723
5724void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5725 HandleInvoke(invoke);
5726 // The register T0 is required to be used for the hidden argument in
5727 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5728 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5729}
5730
5731void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
5732 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5733 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005734 Location receiver = invoke->GetLocations()->InAt(0);
5735 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005736 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005737
5738 // Set the hidden argument.
5739 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
5740 invoke->GetDexMethodIndex());
5741
5742 // temp = object->GetClass();
5743 if (receiver.IsStackSlot()) {
5744 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
5745 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
5746 } else {
5747 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
5748 }
5749 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005750 // Instead of simply (possibly) unpoisoning `temp` here, we should
5751 // emit a read barrier for the previous class reference load.
5752 // However this is not required in practice, as this is an
5753 // intermediate/temporary reference and because the current
5754 // concurrent copying collector keeps the from-space memory
5755 // intact/accessible until the end of the marking phase (the
5756 // concurrent copying collector may not in the future).
5757 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005758 __ LoadFromOffset(kLoadDoubleword, temp, temp,
5759 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
5760 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005761 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005762 // temp = temp->GetImtEntryAt(method_offset);
5763 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5764 // T9 = temp->GetEntryPoint();
5765 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5766 // T9();
5767 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005768 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005769 DCHECK(!codegen_->IsLeafMethod());
5770 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5771}
5772
5773void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07005774 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5775 if (intrinsic.TryDispatch(invoke)) {
5776 return;
5777 }
5778
Alexey Frunze4dda3372015-06-01 18:31:49 -07005779 HandleInvoke(invoke);
5780}
5781
5782void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005783 // Explicit clinit checks triggered by static invokes must have been pruned by
5784 // art::PrepareForRegisterAllocation.
5785 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005786
Chris Larsen3039e382015-08-26 07:54:08 -07005787 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
5788 if (intrinsic.TryDispatch(invoke)) {
5789 return;
5790 }
5791
Alexey Frunze4dda3372015-06-01 18:31:49 -07005792 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005793}
5794
Orion Hodsonac141392017-01-13 11:53:47 +00005795void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5796 HandleInvoke(invoke);
5797}
5798
5799void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
5800 codegen_->GenerateInvokePolymorphicCall(invoke);
5801}
5802
Chris Larsen3039e382015-08-26 07:54:08 -07005803static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005804 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07005805 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
5806 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005807 return true;
5808 }
5809 return false;
5810}
5811
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005812HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005813 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005814 bool fallback_load = false;
5815 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005816 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005817 case HLoadString::LoadKind::kBootImageInternTable:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005818 case HLoadString::LoadKind::kBssEntry:
5819 DCHECK(!Runtime::Current()->UseJitCompilation());
5820 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005821 case HLoadString::LoadKind::kJitTableAddress:
5822 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005823 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005824 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005825 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01005826 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005827 }
5828 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005829 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005830 }
5831 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005832}
5833
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
5835 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005836 bool fallback_load = false;
5837 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005838 case HLoadClass::LoadKind::kInvalid:
5839 LOG(FATAL) << "UNREACHABLE";
5840 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005841 case HLoadClass::LoadKind::kReferrersClass:
5842 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005843 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01005844 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005845 case HLoadClass::LoadKind::kBssEntry:
5846 DCHECK(!Runtime::Current()->UseJitCompilation());
5847 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005848 case HLoadClass::LoadKind::kJitTableAddress:
5849 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005850 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005851 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005852 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08005853 break;
5854 }
5855 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005856 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005857 }
5858 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859}
5860
Vladimir Markodc151b22015-10-15 18:02:30 +01005861HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
5862 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005863 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08005864 // On MIPS64 we support all dispatch types.
5865 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005866}
5867
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005868void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
5869 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005870 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00005871 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08005872 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5873 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5874
Alexey Frunze19f6c692016-11-30 19:19:55 -08005875 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005876 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005877 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005878 uint32_t offset =
5879 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00005880 __ LoadFromOffset(kLoadDoubleword,
5881 temp.AsRegister<GpuRegister>(),
5882 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005883 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00005884 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005885 }
Vladimir Marko58155012015-08-19 12:49:41 +00005886 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005887 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005888 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005889 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
5890 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005891 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko65979462017-05-19 17:25:12 +01005892 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005893 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
5894 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
5895 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Vladimir Marko65979462017-05-19 17:25:12 +01005896 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5897 break;
5898 }
Vladimir Marko58155012015-08-19 12:49:41 +00005899 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005900 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
5901 kLoadDoubleword,
5902 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00005903 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005904 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005905 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005906 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07005907 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
5908 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
5909 EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunze19f6c692016-11-30 19:19:55 -08005910 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
5911 break;
5912 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005913 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5914 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5915 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005916 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005917 }
5918
Alexey Frunze19f6c692016-11-30 19:19:55 -08005919 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005920 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005921 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005922 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005923 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5924 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5925 __ LoadFromOffset(kLoadDoubleword,
5926 T9,
5927 callee_method.AsRegister<GpuRegister>(),
5928 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005929 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005930 // T9()
5931 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005932 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005933 break;
5934 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005935 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5936
Alexey Frunze4dda3372015-06-01 18:31:49 -07005937 DCHECK(!IsLeafMethod());
5938}
5939
5940void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005941 // Explicit clinit checks triggered by static invokes must have been pruned by
5942 // art::PrepareForRegisterAllocation.
5943 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005944
5945 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5946 return;
5947 }
5948
5949 LocationSummary* locations = invoke->GetLocations();
5950 codegen_->GenerateStaticOrDirectCall(invoke,
5951 locations->HasTemps()
5952 ? locations->GetTemp(0)
5953 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005954}
5955
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005956void CodeGeneratorMIPS64::GenerateVirtualCall(
5957 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005958 // Use the calling convention instead of the location of the receiver, as
5959 // intrinsics may have put the receiver in a different register. In the intrinsics
5960 // slow path, the arguments have been moved to the right place, so here we are
5961 // guaranteed that the receiver is the first register of the calling convention.
5962 InvokeDexCallingConvention calling_convention;
5963 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5964
Alexey Frunze53afca12015-11-05 16:34:23 -08005965 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005966 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5967 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5968 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005969 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005970
5971 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005972 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005973 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005974 // Instead of simply (possibly) unpoisoning `temp` here, we should
5975 // emit a read barrier for the previous class reference load.
5976 // However this is not required in practice, as this is an
5977 // intermediate/temporary reference and because the current
5978 // concurrent copying collector keeps the from-space memory
5979 // intact/accessible until the end of the marking phase (the
5980 // concurrent copying collector may not in the future).
5981 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005982 // temp = temp->GetMethodAt(method_offset);
5983 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5984 // T9 = temp->GetEntryPoint();
5985 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5986 // T9();
5987 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005988 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005989 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005990}
5991
5992void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5993 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5994 return;
5995 }
5996
5997 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005998 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005999}
6000
6001void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006002 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006003 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006004 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006005 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6006 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006007 return;
6008 }
Vladimir Marko41559982017-01-06 14:04:23 +00006009 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006010
Alexey Frunze15958152017-02-09 19:08:30 -08006011 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6012 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08006013 ? LocationSummary::kCallOnSlowPath
6014 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006015 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006016 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
6017 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6018 }
Vladimir Marko41559982017-01-06 14:04:23 +00006019 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006020 locations->SetInAt(0, Location::RequiresRegister());
6021 }
6022 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006023 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6024 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6025 // Rely on the type resolution or initialization and marking to save everything we need.
6026 RegisterSet caller_saves = RegisterSet::Empty();
6027 InvokeRuntimeCallingConvention calling_convention;
6028 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6029 locations->SetCustomSlowPathCallerSaves(caller_saves);
6030 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006031 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006032 }
6033 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006034}
6035
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006036// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6037// move.
6038void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006039 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006040 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006041 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006042 return;
6043 }
Vladimir Marko41559982017-01-06 14:04:23 +00006044 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006045
Vladimir Marko41559982017-01-06 14:04:23 +00006046 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006047 Location out_loc = locations->Out();
6048 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6049 GpuRegister current_method_reg = ZERO;
6050 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006051 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006052 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
6053 }
6054
Alexey Frunze15958152017-02-09 19:08:30 -08006055 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6056 ? kWithoutReadBarrier
6057 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006058 bool generate_null_check = false;
6059 switch (load_kind) {
6060 case HLoadClass::LoadKind::kReferrersClass:
6061 DCHECK(!cls->CanCallRuntime());
6062 DCHECK(!cls->MustGenerateClinitCheck());
6063 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6064 GenerateGcRootFieldLoad(cls,
6065 out_loc,
6066 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08006067 ArtMethod::DeclaringClassOffset().Int32Value(),
6068 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006069 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006070 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006071 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08006072 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006073 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Alexey Frunzef63f5692016-12-13 17:43:11 -08006074 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006075 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6076 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6077 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006078 __ Daddiu(out, AT, /* placeholder */ 0x5678);
6079 break;
6080 }
6081 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08006082 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006083 uint32_t address = dchecked_integral_cast<uint32_t>(
6084 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6085 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006086 __ LoadLiteral(out,
6087 kLoadUnsignedWord,
6088 codegen_->DeduplicateBootImageAddressLiteral(address));
6089 break;
6090 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006091 case HLoadClass::LoadKind::kBootImageClassTable: {
6092 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6093 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6094 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6095 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6096 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
6097 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6098 __ Lwu(out, AT, /* placeholder */ 0x5678);
6099 // Extract the reference from the slot data, i.e. clear the hash bits.
6100 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6101 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6102 if (masked_hash != 0) {
6103 __ Daddiu(out, out, -masked_hash);
6104 }
6105 break;
6106 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006107 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00006108 CodeGeneratorMIPS64::PcRelativePatchInfo* bss_info_high =
6109 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006110 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6111 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006112 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high, out);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006113 GenerateGcRootFieldLoad(cls,
6114 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006115 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006116 /* placeholder */ 0x5678,
6117 read_barrier_option,
6118 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006119 generate_null_check = true;
6120 break;
6121 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006122 case HLoadClass::LoadKind::kJitTableAddress:
6123 __ LoadLiteral(out,
6124 kLoadUnsignedWord,
6125 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6126 cls->GetTypeIndex(),
6127 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08006128 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006129 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006130 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006131 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006132 LOG(FATAL) << "UNREACHABLE";
6133 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08006134 }
6135
6136 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6137 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006138 SlowPathCodeMIPS64* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS64(
Vladimir Markof3c52b42017-11-17 17:32:12 +00006139 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08006140 codegen_->AddSlowPath(slow_path);
6141 if (generate_null_check) {
6142 __ Beqzc(out, slow_path->GetEntryLabel());
6143 }
6144 if (cls->MustGenerateClinitCheck()) {
6145 GenerateClassInitializationCheck(slow_path, out);
6146 } else {
6147 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006148 }
6149 }
6150}
6151
David Brazdilcb1c0552015-08-04 16:22:25 +01006152static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006153 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006154}
6155
Alexey Frunze4dda3372015-06-01 18:31:49 -07006156void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
6157 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006158 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006159 locations->SetOut(Location::RequiresRegister());
6160}
6161
6162void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
6163 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006164 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
6165}
6166
6167void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006168 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006169}
6170
6171void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6172 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006173}
6174
Alexey Frunze4dda3372015-06-01 18:31:49 -07006175void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006176 HLoadString::LoadKind load_kind = load->GetLoadKind();
6177 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006178 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006179 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006180 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006181 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08006182 } else {
6183 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07006184 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6185 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6186 // Rely on the pResolveString and marking to save everything we need.
6187 RegisterSet caller_saves = RegisterSet::Empty();
6188 InvokeRuntimeCallingConvention calling_convention;
6189 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6190 locations->SetCustomSlowPathCallerSaves(caller_saves);
6191 } else {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006192 // For non-Baker read barriers we have a temp-clobbering call.
Alexey Frunzec61c0762017-04-10 13:54:23 -07006193 }
6194 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08006195 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006196}
6197
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006198// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6199// move.
6200void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006201 HLoadString::LoadKind load_kind = load->GetLoadKind();
6202 LocationSummary* locations = load->GetLocations();
6203 Location out_loc = locations->Out();
6204 GpuRegister out = out_loc.AsRegister<GpuRegister>();
6205
6206 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006207 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6208 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006209 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006210 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006211 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6212 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
6213 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006214 __ Daddiu(out, AT, /* placeholder */ 0x5678);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006215 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006216 }
6217 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006218 uint32_t address = dchecked_integral_cast<uint32_t>(
6219 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6220 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006221 __ LoadLiteral(out,
6222 kLoadUnsignedWord,
6223 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006224 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006225 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006226 case HLoadString::LoadKind::kBootImageInternTable: {
Alexey Frunzef63f5692016-12-13 17:43:11 -08006227 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006228 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006229 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006230 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6231 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006232 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, AT, info_low);
6233 __ Lwu(out, AT, /* placeholder */ 0x5678);
6234 return;
6235 }
6236 case HLoadString::LoadKind::kBssEntry: {
6237 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6238 CodeGeneratorMIPS64::PcRelativePatchInfo* info_high =
6239 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
6240 CodeGeneratorMIPS64::PcRelativePatchInfo* info_low =
6241 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Markof3c52b42017-11-17 17:32:12 +00006242 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high, out);
Alexey Frunze15958152017-02-09 19:08:30 -08006243 GenerateGcRootFieldLoad(load,
6244 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00006245 out,
Alexey Frunze15958152017-02-09 19:08:30 -08006246 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006247 kCompilerReadBarrierOption,
6248 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07006249 SlowPathCodeMIPS64* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00006250 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS64(load);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006251 codegen_->AddSlowPath(slow_path);
6252 __ Beqzc(out, slow_path->GetEntryLabel());
6253 __ Bind(slow_path->GetExitLabel());
6254 return;
6255 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08006256 case HLoadString::LoadKind::kJitTableAddress:
6257 __ LoadLiteral(out,
6258 kLoadUnsignedWord,
6259 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
6260 load->GetStringIndex(),
6261 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08006262 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08006263 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08006264 default:
6265 break;
6266 }
6267
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006268 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006269 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006270 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006271 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08006272 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
6273 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6274 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006275}
6276
Alexey Frunze4dda3372015-06-01 18:31:49 -07006277void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006278 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006279 locations->SetOut(Location::ConstantLocation(constant));
6280}
6281
6282void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
6283 // Will be generated at use site.
6284}
6285
6286void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006287 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6288 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006289 InvokeRuntimeCallingConvention calling_convention;
6290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6291}
6292
6293void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006294 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07006295 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01006296 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006297 if (instruction->IsEnter()) {
6298 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6299 } else {
6300 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6301 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006302}
6303
6304void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
6305 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006306 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006307 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 case DataType::Type::kInt32:
6309 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006310 locations->SetInAt(0, Location::RequiresRegister());
6311 locations->SetInAt(1, Location::RequiresRegister());
6312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6313 break;
6314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006315 case DataType::Type::kFloat32:
6316 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006317 locations->SetInAt(0, Location::RequiresFpuRegister());
6318 locations->SetInAt(1, Location::RequiresFpuRegister());
6319 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6320 break;
6321
6322 default:
6323 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
6324 }
6325}
6326
6327void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006329 LocationSummary* locations = instruction->GetLocations();
6330
6331 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006332 case DataType::Type::kInt32:
6333 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006334 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6335 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
6336 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006338 __ MulR6(dst, lhs, rhs);
6339 else
6340 __ Dmul(dst, lhs, rhs);
6341 break;
6342 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 case DataType::Type::kFloat32:
6344 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006345 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6346 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
6347 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006349 __ MulS(dst, lhs, rhs);
6350 else
6351 __ MulD(dst, lhs, rhs);
6352 break;
6353 }
6354 default:
6355 LOG(FATAL) << "Unexpected mul type " << type;
6356 }
6357}
6358
6359void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
6360 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006361 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006362 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006363 case DataType::Type::kInt32:
6364 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006365 locations->SetInAt(0, Location::RequiresRegister());
6366 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6367 break;
6368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 case DataType::Type::kFloat32:
6370 case DataType::Type::kFloat64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006371 locations->SetInAt(0, Location::RequiresFpuRegister());
6372 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6373 break;
6374
6375 default:
6376 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
6377 }
6378}
6379
6380void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006382 LocationSummary* locations = instruction->GetLocations();
6383
6384 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006385 case DataType::Type::kInt32:
6386 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006387 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6388 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006389 if (type == DataType::Type::kInt32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006390 __ Subu(dst, ZERO, src);
6391 else
6392 __ Dsubu(dst, ZERO, src);
6393 break;
6394 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 case DataType::Type::kFloat32:
6396 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006397 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6398 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 if (type == DataType::Type::kFloat32)
Alexey Frunze4dda3372015-06-01 18:31:49 -07006400 __ NegS(dst, src);
6401 else
6402 __ NegD(dst, src);
6403 break;
6404 }
6405 default:
6406 LOG(FATAL) << "Unexpected neg type " << type;
6407 }
6408}
6409
6410void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006411 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6412 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006413 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006415 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6416 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006417}
6418
6419void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006420 // Note: if heap poisoning is enabled, the entry point takes care
6421 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02006422 QuickEntrypointEnum entrypoint =
6423 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
6424 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006425 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02006426 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006427}
6428
6429void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006430 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6431 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006432 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00006433 if (instruction->IsStringAlloc()) {
6434 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
6435 } else {
6436 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00006437 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006438 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006439}
6440
6441void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08006442 // Note: if heap poisoning is enabled, the entry point takes care
6443 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00006444 if (instruction->IsStringAlloc()) {
6445 // String is allocated through StringFactory. Call NewEmptyString entry point.
6446 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02006447 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07006448 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006449 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6450 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
6451 __ Jalr(T9);
6452 __ Nop();
6453 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6454 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01006455 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00006456 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00006457 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006458}
6459
6460void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006461 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006462 locations->SetInAt(0, Location::RequiresRegister());
6463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6464}
6465
6466void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006468 LocationSummary* locations = instruction->GetLocations();
6469
6470 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006471 case DataType::Type::kInt32:
6472 case DataType::Type::kInt64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006473 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6474 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6475 __ Nor(dst, src, ZERO);
6476 break;
6477 }
6478
6479 default:
6480 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6481 }
6482}
6483
6484void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006485 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006486 locations->SetInAt(0, Location::RequiresRegister());
6487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6488}
6489
6490void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
6491 LocationSummary* locations = instruction->GetLocations();
6492 __ Xori(locations->Out().AsRegister<GpuRegister>(),
6493 locations->InAt(0).AsRegister<GpuRegister>(),
6494 1);
6495}
6496
6497void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006498 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6499 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006500}
6501
Calin Juravle2ae48182016-03-16 14:05:09 +00006502void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
6503 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006504 return;
6505 }
6506 Location obj = instruction->GetLocations()->InAt(0);
6507
6508 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006509 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006510}
6511
Calin Juravle2ae48182016-03-16 14:05:09 +00006512void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006513 SlowPathCodeMIPS64* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006514 new (GetScopedAllocator()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006515 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006516
6517 Location obj = instruction->GetLocations()->InAt(0);
6518
6519 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
6520}
6521
6522void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006523 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006524}
6525
6526void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
6527 HandleBinaryOp(instruction);
6528}
6529
6530void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
6531 HandleBinaryOp(instruction);
6532}
6533
6534void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6535 LOG(FATAL) << "Unreachable";
6536}
6537
6538void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006539 if (instruction->GetNext()->IsSuspendCheck() &&
6540 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6541 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6542 // The back edge will generate the suspend check.
6543 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6544 }
6545
Alexey Frunze4dda3372015-06-01 18:31:49 -07006546 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6547}
6548
6549void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006551 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6552 if (location.IsStackSlot()) {
6553 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6554 } else if (location.IsDoubleStackSlot()) {
6555 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6556 }
6557 locations->SetOut(location);
6558}
6559
6560void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
6561 ATTRIBUTE_UNUSED) {
6562 // Nothing to do, the parameter is already at its location.
6563}
6564
6565void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
6566 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006567 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006568 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6569}
6570
6571void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
6572 ATTRIBUTE_UNUSED) {
6573 // Nothing to do, the method is already at its location.
6574}
6575
6576void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006577 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006578 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006579 locations->SetInAt(i, Location::Any());
6580 }
6581 locations->SetOut(Location::Any());
6582}
6583
6584void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6585 LOG(FATAL) << "Unreachable";
6586}
6587
6588void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006589 DataType::Type type = rem->GetResultType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006590 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006591 DataType::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
6592 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006594
6595 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006596 case DataType::Type::kInt32:
6597 case DataType::Type::kInt64:
Alexey Frunze4dda3372015-06-01 18:31:49 -07006598 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07006599 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07006600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6601 break;
6602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006603 case DataType::Type::kFloat32:
6604 case DataType::Type::kFloat64: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006605 InvokeRuntimeCallingConvention calling_convention;
6606 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6607 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6608 locations->SetOut(calling_convention.GetReturnLocation(type));
6609 break;
6610 }
6611
6612 default:
6613 LOG(FATAL) << "Unexpected rem type " << type;
6614 }
6615}
6616
6617void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006618 DataType::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006619
6620 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006621 case DataType::Type::kInt32:
6622 case DataType::Type::kInt64:
Alexey Frunzec857c742015-09-23 15:12:39 -07006623 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006624 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 case DataType::Type::kFloat32:
6627 case DataType::Type::kFloat64: {
6628 QuickEntrypointEnum entrypoint =
6629 (type == DataType::Type::kFloat32) ? kQuickFmodf : kQuickFmod;
Serban Constantinescufc734082016-07-19 17:18:07 +01006630 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 if (type == DataType::Type::kFloat32) {
Roland Levillain888d0672015-11-23 18:53:50 +00006632 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
6633 } else {
6634 CheckEntrypointTypes<kQuickFmod, double, double, double>();
6635 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006636 break;
6637 }
6638 default:
6639 LOG(FATAL) << "Unexpected rem type " << type;
6640 }
6641}
6642
Igor Murashkind01745e2017-04-05 16:40:31 -07006643void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
6644 constructor_fence->SetLocations(nullptr);
6645}
6646
6647void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
6648 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
6649 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
6650}
6651
Alexey Frunze4dda3372015-06-01 18:31:49 -07006652void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6653 memory_barrier->SetLocations(nullptr);
6654}
6655
6656void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6657 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6658}
6659
6660void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006661 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006662 DataType::Type return_type = ret->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006663 locations->SetInAt(0, Mips64ReturnLocation(return_type));
6664}
6665
6666void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6667 codegen_->GenerateFrameExit();
6668}
6669
6670void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
6671 ret->SetLocations(nullptr);
6672}
6673
6674void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6675 codegen_->GenerateFrameExit();
6676}
6677
Alexey Frunze92d90602015-12-18 18:16:36 -08006678void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
6679 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006680}
6681
Alexey Frunze92d90602015-12-18 18:16:36 -08006682void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
6683 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006684}
6685
Alexey Frunze4dda3372015-06-01 18:31:49 -07006686void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
6687 HandleShift(shl);
6688}
6689
6690void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
6691 HandleShift(shl);
6692}
6693
6694void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
6695 HandleShift(shr);
6696}
6697
6698void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
6699 HandleShift(shr);
6700}
6701
Alexey Frunze4dda3372015-06-01 18:31:49 -07006702void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
6703 HandleBinaryOp(instruction);
6704}
6705
6706void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
6707 HandleBinaryOp(instruction);
6708}
6709
6710void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6711 HandleFieldGet(instruction, instruction->GetFieldInfo());
6712}
6713
6714void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6715 HandleFieldGet(instruction, instruction->GetFieldInfo());
6716}
6717
6718void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6719 HandleFieldSet(instruction, instruction->GetFieldInfo());
6720}
6721
6722void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01006723 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006724}
6725
Calin Juravlee460d1d2015-09-29 04:52:17 +01006726void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
6727 HUnresolvedInstanceFieldGet* instruction) {
6728 FieldAccessCallingConventionMIPS64 calling_convention;
6729 codegen_->CreateUnresolvedFieldLocationSummary(
6730 instruction, instruction->GetFieldType(), calling_convention);
6731}
6732
6733void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
6734 HUnresolvedInstanceFieldGet* instruction) {
6735 FieldAccessCallingConventionMIPS64 calling_convention;
6736 codegen_->GenerateUnresolvedFieldAccess(instruction,
6737 instruction->GetFieldType(),
6738 instruction->GetFieldIndex(),
6739 instruction->GetDexPc(),
6740 calling_convention);
6741}
6742
6743void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
6744 HUnresolvedInstanceFieldSet* instruction) {
6745 FieldAccessCallingConventionMIPS64 calling_convention;
6746 codegen_->CreateUnresolvedFieldLocationSummary(
6747 instruction, instruction->GetFieldType(), calling_convention);
6748}
6749
6750void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
6751 HUnresolvedInstanceFieldSet* instruction) {
6752 FieldAccessCallingConventionMIPS64 calling_convention;
6753 codegen_->GenerateUnresolvedFieldAccess(instruction,
6754 instruction->GetFieldType(),
6755 instruction->GetFieldIndex(),
6756 instruction->GetDexPc(),
6757 calling_convention);
6758}
6759
6760void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
6761 HUnresolvedStaticFieldGet* instruction) {
6762 FieldAccessCallingConventionMIPS64 calling_convention;
6763 codegen_->CreateUnresolvedFieldLocationSummary(
6764 instruction, instruction->GetFieldType(), calling_convention);
6765}
6766
6767void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
6768 HUnresolvedStaticFieldGet* instruction) {
6769 FieldAccessCallingConventionMIPS64 calling_convention;
6770 codegen_->GenerateUnresolvedFieldAccess(instruction,
6771 instruction->GetFieldType(),
6772 instruction->GetFieldIndex(),
6773 instruction->GetDexPc(),
6774 calling_convention);
6775}
6776
6777void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
6778 HUnresolvedStaticFieldSet* instruction) {
6779 FieldAccessCallingConventionMIPS64 calling_convention;
6780 codegen_->CreateUnresolvedFieldLocationSummary(
6781 instruction, instruction->GetFieldType(), calling_convention);
6782}
6783
6784void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
6785 HUnresolvedStaticFieldSet* instruction) {
6786 FieldAccessCallingConventionMIPS64 calling_convention;
6787 codegen_->GenerateUnresolvedFieldAccess(instruction,
6788 instruction->GetFieldType(),
6789 instruction->GetFieldIndex(),
6790 instruction->GetDexPc(),
6791 calling_convention);
6792}
6793
Alexey Frunze4dda3372015-06-01 18:31:49 -07006794void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006795 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6796 instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02006797 // In suspend check slow path, usually there are no caller-save registers at all.
6798 // If SIMD instructions are present, however, we force spilling all live SIMD
6799 // registers in full width (since the runtime only saves/restores lower part).
6800 locations->SetCustomSlowPathCallerSaves(
6801 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006802}
6803
6804void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
6805 HBasicBlock* block = instruction->GetBlock();
6806 if (block->GetLoopInformation() != nullptr) {
6807 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6808 // The back edge will generate the suspend check.
6809 return;
6810 }
6811 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6812 // The goto will generate the suspend check.
6813 return;
6814 }
6815 GenerateSuspendCheck(instruction, nullptr);
6816}
6817
Alexey Frunze4dda3372015-06-01 18:31:49 -07006818void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006819 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6820 instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006821 InvokeRuntimeCallingConvention calling_convention;
6822 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6823}
6824
6825void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01006826 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006827 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6828}
6829
6830void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006831 DataType::Type input_type = conversion->GetInputType();
6832 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006833 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6834 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
6837 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006838 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6839 }
6840
Vladimir Markoca6fff82017-10-03 14:49:14 +01006841 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(conversion);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 if (DataType::IsFloatingPointType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006844 locations->SetInAt(0, Location::RequiresFpuRegister());
6845 } else {
6846 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07006847 }
6848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006849 if (DataType::IsFloatingPointType(result_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006850 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006851 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006853 }
6854}
6855
6856void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
6857 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006858 DataType::Type result_type = conversion->GetResultType();
6859 DataType::Type input_type = conversion->GetInputType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07006860
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006861 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
6862 << input_type << " -> " << result_type;
Alexey Frunze4dda3372015-06-01 18:31:49 -07006863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006864 if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006865 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6866 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
6867
6868 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006869 case DataType::Type::kUint8:
6870 __ Andi(dst, src, 0xFF);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006871 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006872 case DataType::Type::kInt8:
6873 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006874 // Type conversion from long to types narrower than int is a result of code
6875 // transformations. To avoid unpredictable results for SEB and SEH, we first
6876 // need to sign-extend the low 32-bit value into bits 32 through 63.
6877 __ Sll(dst, src, 0);
6878 __ Seb(dst, dst);
6879 } else {
6880 __ Seb(dst, src);
6881 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006882 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006883 case DataType::Type::kUint16:
6884 __ Andi(dst, src, 0xFFFF);
6885 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006886 case DataType::Type::kInt16:
6887 if (input_type == DataType::Type::kInt64) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00006888 // Type conversion from long to types narrower than int is a result of code
6889 // transformations. To avoid unpredictable results for SEB and SEH, we first
6890 // need to sign-extend the low 32-bit value into bits 32 through 63.
6891 __ Sll(dst, src, 0);
6892 __ Seh(dst, dst);
6893 } else {
6894 __ Seh(dst, src);
6895 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006896 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 case DataType::Type::kInt32:
6898 case DataType::Type::kInt64:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006899 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
6900 // conversions, except when the input and output registers are the same and we are not
6901 // converting longs to shorter types. In these cases, do nothing.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006902 if ((input_type == DataType::Type::kInt64) || (dst != src)) {
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01006903 __ Sll(dst, src, 0);
6904 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006905 break;
6906
6907 default:
6908 LOG(FATAL) << "Unexpected type conversion from " << input_type
6909 << " to " << result_type;
6910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006911 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006912 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6913 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006914 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006915 __ Dmtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006916 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006917 __ Cvtsl(dst, FTMP);
6918 } else {
6919 __ Cvtdl(dst, FTMP);
6920 }
6921 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006922 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006923 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006924 __ Cvtsw(dst, FTMP);
6925 } else {
6926 __ Cvtdw(dst, FTMP);
6927 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07006928 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006929 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
6930 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006931 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
6932 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006934 if (result_type == DataType::Type::kInt64) {
6935 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006936 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006937 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006938 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006939 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006940 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006941 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006942 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006943 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006944 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006945 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006946 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006947 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006948 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006949 } else if (DataType::IsFloatingPointType(result_type) &&
6950 DataType::IsFloatingPointType(input_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006951 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6952 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006953 if (result_type == DataType::Type::kFloat32) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07006954 __ Cvtsd(dst, src);
6955 } else {
6956 __ Cvtds(dst, src);
6957 }
6958 } else {
6959 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6960 << " to " << result_type;
6961 }
6962}
6963
6964void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6965 HandleShift(ushr);
6966}
6967
6968void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6969 HandleShift(ushr);
6970}
6971
6972void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6973 HandleBinaryOp(instruction);
6974}
6975
6976void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6977 HandleBinaryOp(instruction);
6978}
6979
6980void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6981 // Nothing to do, this should be removed during prepare for register allocator.
6982 LOG(FATAL) << "Unreachable";
6983}
6984
6985void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6986 // Nothing to do, this should be removed during prepare for register allocator.
6987 LOG(FATAL) << "Unreachable";
6988}
6989
6990void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006991 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006992}
6993
6994void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006995 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006996}
6997
6998void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006999 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007000}
7001
7002void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007003 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007004}
7005
7006void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007007 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007008}
7009
7010void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007011 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007012}
7013
7014void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007015 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007016}
7017
7018void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007019 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007020}
7021
7022void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007023 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007024}
7025
7026void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007027 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007028}
7029
7030void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007031 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007032}
7033
7034void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007035 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07007036}
7037
Aart Bike9f37602015-10-09 11:15:55 -07007038void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007040}
7041
7042void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007044}
7045
7046void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007048}
7049
7050void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007052}
7053
7054void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007056}
7057
7058void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007060}
7061
7062void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007064}
7065
7066void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07007068}
7069
Mark Mendellfe57faa2015-09-18 09:26:15 -04007070// Simple implementation of packed switch - generate cascaded compare/jumps.
7071void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007073 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007074 locations->SetInAt(0, Location::RequiresRegister());
7075}
7076
Alexey Frunze0960ac52016-12-20 17:24:59 -08007077void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
7078 int32_t lower_bound,
7079 uint32_t num_entries,
7080 HBasicBlock* switch_block,
7081 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007082 // Create a set of compare/jumps.
7083 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08007084 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007085 // Jump to default if index is negative
7086 // Note: We don't check the case that index is positive while value < lower_bound, because in
7087 // this case, index >= num_entries must be true. So that we can save one branch instruction.
7088 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
7089
Alexey Frunze0960ac52016-12-20 17:24:59 -08007090 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007091 // Jump to successors[0] if value == lower_bound.
7092 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
7093 int32_t last_index = 0;
7094 for (; num_entries - last_index > 2; last_index += 2) {
7095 __ Addiu(temp_reg, temp_reg, -2);
7096 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7097 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
7098 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7099 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
7100 }
7101 if (num_entries - last_index == 2) {
7102 // The last missing case_value.
7103 __ Addiu(temp_reg, temp_reg, -1);
7104 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007105 }
7106
7107 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08007108 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07007109 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007110 }
7111}
7112
Alexey Frunze0960ac52016-12-20 17:24:59 -08007113void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
7114 int32_t lower_bound,
7115 uint32_t num_entries,
7116 HBasicBlock* switch_block,
7117 HBasicBlock* default_block) {
7118 // Create a jump table.
7119 std::vector<Mips64Label*> labels(num_entries);
7120 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7121 for (uint32_t i = 0; i < num_entries; i++) {
7122 labels[i] = codegen_->GetLabelOf(successors[i]);
7123 }
7124 JumpTable* table = __ CreateJumpTable(std::move(labels));
7125
7126 // Is the value in range?
7127 __ Addiu32(TMP, value_reg, -lower_bound);
7128 __ LoadConst32(AT, num_entries);
7129 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
7130
7131 // We are in the range of the table.
7132 // Load the target address from the jump table, indexing by the value.
7133 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07007134 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08007135 __ Lw(TMP, TMP, 0);
7136 // Compute the absolute target address by adding the table start address
7137 // (the table contains offsets to targets relative to its start).
7138 __ Daddu(TMP, TMP, AT);
7139 // And jump.
7140 __ Jr(TMP);
7141 __ Nop();
7142}
7143
7144void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7145 int32_t lower_bound = switch_instr->GetStartValue();
7146 uint32_t num_entries = switch_instr->GetNumEntries();
7147 LocationSummary* locations = switch_instr->GetLocations();
7148 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
7149 HBasicBlock* switch_block = switch_instr->GetBlock();
7150 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7151
7152 if (num_entries > kPackedSwitchJumpTableThreshold) {
7153 GenTableBasedPackedSwitch(value_reg,
7154 lower_bound,
7155 num_entries,
7156 switch_block,
7157 default_block);
7158 } else {
7159 GenPackedSwitchWithCompares(value_reg,
7160 lower_bound,
7161 num_entries,
7162 switch_block,
7163 default_block);
7164 }
7165}
7166
Chris Larsenc9905a62017-03-13 17:06:18 -07007167void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7168 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007169 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Chris Larsenc9905a62017-03-13 17:06:18 -07007170 locations->SetInAt(0, Location::RequiresRegister());
7171 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007172}
7173
Chris Larsenc9905a62017-03-13 17:06:18 -07007174void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
7175 LocationSummary* locations = instruction->GetLocations();
7176 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
7177 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7178 instruction->GetIndex(), kMips64PointerSize).SizeValue();
7179 __ LoadFromOffset(kLoadDoubleword,
7180 locations->Out().AsRegister<GpuRegister>(),
7181 locations->InAt(0).AsRegister<GpuRegister>(),
7182 method_offset);
7183 } else {
7184 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
7185 instruction->GetIndex(), kMips64PointerSize));
7186 __ LoadFromOffset(kLoadDoubleword,
7187 locations->Out().AsRegister<GpuRegister>(),
7188 locations->InAt(0).AsRegister<GpuRegister>(),
7189 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
7190 __ LoadFromOffset(kLoadDoubleword,
7191 locations->Out().AsRegister<GpuRegister>(),
7192 locations->Out().AsRegister<GpuRegister>(),
7193 method_offset);
7194 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007195}
7196
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007197void LocationsBuilderMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7198 ATTRIBUTE_UNUSED) {
7199 LOG(FATAL) << "Unreachable";
7200}
7201
7202void InstructionCodeGeneratorMIPS64::VisitIntermediateAddress(HIntermediateAddress* instruction
7203 ATTRIBUTE_UNUSED) {
7204 LOG(FATAL) << "Unreachable";
7205}
7206
Alexey Frunze4dda3372015-06-01 18:31:49 -07007207} // namespace mips64
7208} // namespace art