blob: 5bb12d738912498cab6174a56f9e17753a3466d2 [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 Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 return next_location;
95}
96
97Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
98 return Mips64ReturnLocation(type);
99}
100
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100101// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
102#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700103#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
105class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100110 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
112 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000113 if (instruction_->CanThrowIntoCatchBlock()) {
114 // Live registers will be restored in the catch block if caught.
115 SaveLiveRegisters(codegen, instruction_->GetLocations());
116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
119 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
122 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
125 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100126 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
127 ? kQuickThrowStringBounds
128 : kQuickThrowArrayBounds;
129 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
132 }
133
Alexandre Rames8158f282015-08-07 10:26:17 +0100134 bool IsFatal() const OVERRIDE { return true; }
135
Roland Levillain46648892015-06-19 16:07:18 +0100136 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
137
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
140};
141
142class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
143 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
145 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146
147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
148 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
149 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100150 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700151 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Roland Levillain46648892015-06-19 16:07:18 +0100156 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
157
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
160};
161
162class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
163 public:
164 LoadClassSlowPathMIPS64(HLoadClass* cls,
165 HInstruction* at,
166 uint32_t dex_pc,
167 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000168 : SlowPathCodeMIPS64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
170 }
171
172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000173 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700174 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
175
176 __ Bind(GetEntryLabel());
177 SaveLiveRegisters(codegen, locations);
178
179 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000180 dex::TypeIndex type_index = cls_->GetTypeIndex();
181 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100182 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
183 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000184 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 if (do_clinit_) {
186 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
187 } else {
188 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
189 }
190
191 // Move the class to the desired location.
192 Location out = locations->Out();
193 if (out.IsValid()) {
194 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700196 mips64_codegen->MoveLocation(out,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
198 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700199 }
200
201 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000202 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
203 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
204 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
205 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000206 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000207 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000208 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
209 __ Sw(out.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
210 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700211 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 }
213
Roland Levillain46648892015-06-19 16:07:18 +0100214 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
215
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 private:
217 // The class this slow path will load.
218 HLoadClass* const cls_;
219
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 // The dex PC of `at_`.
221 const uint32_t dex_pc_;
222
223 // Whether to initialize the class.
224 const bool do_clinit_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227};
228
229class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000231 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 LocationSummary* locations = instruction_->GetLocations();
235 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237
238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
241 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800242 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000243 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100245 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700246 instruction_,
247 instruction_->GetDexPc(),
248 this);
249 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
250 Primitive::Type type = instruction_->GetType();
251 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700252 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700253 type);
254
255 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800256
257 // Store the resolved String to the BSS entry.
Alexey Frunzef63f5692016-12-13 17:43:11 -0800258 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunzef63f5692016-12-13 17:43:11 -0800259 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
260 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
261 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
262 __ Sw(out, AT, /* placeholder */ 0x5678);
263
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700264 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 }
266
Roland Levillain46648892015-06-19 16:07:18 +0100267 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
268
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
271};
272
273class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
274 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
279 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000280 if (instruction_->CanThrowIntoCatchBlock()) {
281 // Live registers will be restored in the catch block if caught.
282 SaveLiveRegisters(codegen, instruction_->GetLocations());
283 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100284 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700285 instruction_,
286 instruction_->GetDexPc(),
287 this);
288 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Roland Levillain46648892015-06-19 16:07:18 +0100293 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
294
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200305 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700306 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
307 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200308 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100309 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200311 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 }
317 }
318
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700319 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 DCHECK(successor_ == nullptr);
321 return &return_label_;
322 }
323
Roland Levillain46648892015-06-19 16:07:18 +0100324 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
325
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 // If not null, the block to branch to after the suspend check.
328 HBasicBlock* const successor_;
329
330 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700331 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332
333 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
334};
335
336class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
337 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800338 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
339 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
347 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
348
349 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800350 if (!is_fatal_) {
351 SaveLiveRegisters(codegen, locations);
352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100364 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800365 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 Primitive::Type ret_type = instruction_->GetType();
367 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
368 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 } else {
370 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800371 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
372 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 }
374
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800375 if (!is_fatal_) {
376 RestoreLiveRegisters(codegen, locations);
377 __ Bc(GetExitLabel());
378 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 }
380
Roland Levillain46648892015-06-19 16:07:18 +0100381 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
382
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 bool IsFatal() const OVERRIDE { return is_fatal_; }
384
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 const bool is_fatal_;
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
389};
390
391class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
392 public:
Aart Bik42249c32016-01-07 15:33:50 -0800393 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800397 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100399 LocationSummary* locations = instruction_->GetLocations();
400 SaveLiveRegisters(codegen, locations);
401 InvokeRuntimeCallingConvention calling_convention;
402 __ LoadConst32(calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100404 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 }
407
Roland Levillain46648892015-06-19 16:07:18 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
409
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
412};
413
Alexey Frunze15958152017-02-09 19:08:30 -0800414class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
415 public:
416 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
424 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
428 Primitive::kPrimNot,
429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
433 Primitive::kPrimInt,
434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
438 Primitive::kPrimNot,
439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
443 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
445 RestoreLiveRegisters(codegen, locations);
446 __ Bc(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
450
451 private:
452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
453};
454
455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
466//
467// If `entrypoint` is a valid location it is assumed to already be
468// holding the entrypoint. The case where the entrypoint is passed in
469// is for the GcRoot read barrier.
470class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
471 public:
472 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
473 Location ref,
474 Location entrypoint = Location::NoLocation())
475 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
476 DCHECK(kEmitCompilerReadBarrier);
477 }
478
479 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 LocationSummary* locations = instruction_->GetLocations();
483 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
484 DCHECK(locations->CanCall());
485 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
486 DCHECK(instruction_->IsInstanceFieldGet() ||
487 instruction_->IsStaticFieldGet() ||
488 instruction_->IsArrayGet() ||
489 instruction_->IsArraySet() ||
490 instruction_->IsLoadClass() ||
491 instruction_->IsLoadString() ||
492 instruction_->IsInstanceOf() ||
493 instruction_->IsCheckCast() ||
494 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
495 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
496 << "Unexpected instruction in read barrier marking slow path: "
497 << instruction_->DebugName();
498
499 __ Bind(GetEntryLabel());
500 // No need to save live registers; it's taken care of by the
501 // entrypoint. Also, there is no need to update the stack mask,
502 // as this runtime call will not trigger a garbage collection.
503 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
504 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
505 (S2 <= ref_reg && ref_reg <= S7) ||
506 (ref_reg == S8)) << ref_reg;
507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in A0 and V0 respectively):
511 //
512 // A0 <- ref
513 // V0 <- ReadBarrierMark(A0)
514 // ref <- V0
515 //
516 // we just use rX (the register containing `ref`) as input and output
517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
521 if (entrypoint_.IsValid()) {
522 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
523 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
524 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
525 __ Nop();
526 } else {
527 int32_t entry_point_offset =
528 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
529 // This runtime call does not require a stack map.
530 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
531 instruction_,
532 this);
533 }
534 __ Bc(GetExitLabel());
535 }
536
537 private:
538 // The location (register) of the marked object reference.
539 const Location ref_;
540
541 // The location of the entrypoint if already loaded.
542 const Location entrypoint_;
543
544 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
545};
546
547// Slow path marking an object reference `ref` during a read barrier,
548// and if needed, atomically updating the field `obj.field` in the
549// object `obj` holding this reference after marking (contrary to
550// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
551// `obj.field`).
552//
553// This means that after the execution of this slow path, both `ref`
554// and `obj.field` will be up-to-date; i.e., after the flip, both will
555// hold the same to-space reference (unless another thread installed
556// another object reference (different from `ref`) in `obj.field`).
557class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
558 public:
559 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
560 Location ref,
561 GpuRegister obj,
562 Location field_offset,
563 GpuRegister temp1)
564 : SlowPathCodeMIPS64(instruction),
565 ref_(ref),
566 obj_(obj),
567 field_offset_(field_offset),
568 temp1_(temp1) {
569 DCHECK(kEmitCompilerReadBarrier);
570 }
571
572 const char* GetDescription() const OVERRIDE {
573 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
574 }
575
576 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
577 LocationSummary* locations = instruction_->GetLocations();
578 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
579 DCHECK(locations->CanCall());
580 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
581 // This slow path is only used by the UnsafeCASObject intrinsic.
582 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
583 << "Unexpected instruction in read barrier marking and field updating slow path: "
584 << instruction_->DebugName();
585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
587 DCHECK(field_offset_.IsRegister()) << field_offset_;
588
589 __ Bind(GetEntryLabel());
590
591 // Save the old reference.
592 // Note that we cannot use AT or TMP to save the old reference, as those
593 // are used by the code that follows, but we need the old reference after
594 // the call to the ReadBarrierMarkRegX entry point.
595 DCHECK_NE(temp1_, AT);
596 DCHECK_NE(temp1_, TMP);
597 __ Move(temp1_, ref_reg);
598
599 // No need to save live registers; it's taken care of by the
600 // entrypoint. Also, there is no need to update the stack mask,
601 // as this runtime call will not trigger a garbage collection.
602 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
603 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
604 (S2 <= ref_reg && ref_reg <= S7) ||
605 (ref_reg == S8)) << ref_reg;
606 // "Compact" slow path, saving two moves.
607 //
608 // Instead of using the standard runtime calling convention (input
609 // and output in A0 and V0 respectively):
610 //
611 // A0 <- ref
612 // V0 <- ReadBarrierMark(A0)
613 // ref <- V0
614 //
615 // we just use rX (the register containing `ref`) as input and output
616 // of a dedicated entrypoint:
617 //
618 // rX <- ReadBarrierMarkRegX(rX)
619 //
620 int32_t entry_point_offset =
621 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
622 // This runtime call does not require a stack map.
623 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
624 instruction_,
625 this);
626
627 // If the new reference is different from the old reference,
628 // update the field in the holder (`*(obj_ + field_offset_)`).
629 //
630 // Note that this field could also hold a different object, if
631 // another thread had concurrently changed it. In that case, the
632 // the compare-and-set (CAS) loop below would abort, leaving the
633 // field as-is.
634 Mips64Label done;
635 __ Beqc(temp1_, ref_reg, &done);
636
637 // Update the the holder's field atomically. This may fail if
638 // mutator updates before us, but it's OK. This is achieved
639 // using a strong compare-and-set (CAS) operation with relaxed
640 // memory synchronization ordering, where the expected value is
641 // the old reference and the desired value is the new reference.
642
643 // Convenience aliases.
644 GpuRegister base = obj_;
645 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
646 GpuRegister expected = temp1_;
647 GpuRegister value = ref_reg;
648 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
649 GpuRegister tmp = AT; // Value in memory.
650
651 __ Daddu(tmp_ptr, base, offset);
652
653 if (kPoisonHeapReferences) {
654 __ PoisonHeapReference(expected);
655 // Do not poison `value` if it is the same register as
656 // `expected`, which has just been poisoned.
657 if (value != expected) {
658 __ PoisonHeapReference(value);
659 }
660 }
661
662 // do {
663 // tmp = [r_ptr] - expected;
664 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
665
666 Mips64Label loop_head, exit_loop;
667 __ Bind(&loop_head);
668 __ Ll(tmp, tmp_ptr);
669 // The LL instruction sign-extends the 32-bit value, but
670 // 32-bit references must be zero-extended. Zero-extend `tmp`.
671 __ Dext(tmp, tmp, 0, 32);
672 __ Bnec(tmp, expected, &exit_loop);
673 __ Move(tmp, value);
674 __ Sc(tmp, tmp_ptr);
675 __ Beqzc(tmp, &loop_head);
676 __ Bind(&exit_loop);
677
678 if (kPoisonHeapReferences) {
679 __ UnpoisonHeapReference(expected);
680 // Do not unpoison `value` if it is the same register as
681 // `expected`, which has just been unpoisoned.
682 if (value != expected) {
683 __ UnpoisonHeapReference(value);
684 }
685 }
686
687 __ Bind(&done);
688 __ Bc(GetExitLabel());
689 }
690
691 private:
692 // The location (register) of the marked object reference.
693 const Location ref_;
694 // The register containing the object holding the marked object reference field.
695 const GpuRegister obj_;
696 // The location of the offset of the marked reference field within `obj_`.
697 Location field_offset_;
698
699 const GpuRegister temp1_;
700
701 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
702};
703
704// Slow path generating a read barrier for a heap reference.
705class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
706 public:
707 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
708 Location out,
709 Location ref,
710 Location obj,
711 uint32_t offset,
712 Location index)
713 : SlowPathCodeMIPS64(instruction),
714 out_(out),
715 ref_(ref),
716 obj_(obj),
717 offset_(offset),
718 index_(index) {
719 DCHECK(kEmitCompilerReadBarrier);
720 // If `obj` is equal to `out` or `ref`, it means the initial object
721 // has been overwritten by (or after) the heap object reference load
722 // to be instrumented, e.g.:
723 //
724 // __ LoadFromOffset(kLoadWord, out, out, offset);
725 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
726 //
727 // In that case, we have lost the information about the original
728 // object, and the emitted read barrier cannot work properly.
729 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
730 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
731 }
732
733 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
734 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
735 LocationSummary* locations = instruction_->GetLocations();
736 Primitive::Type type = Primitive::kPrimNot;
737 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
738 DCHECK(locations->CanCall());
739 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
740 DCHECK(instruction_->IsInstanceFieldGet() ||
741 instruction_->IsStaticFieldGet() ||
742 instruction_->IsArrayGet() ||
743 instruction_->IsInstanceOf() ||
744 instruction_->IsCheckCast() ||
745 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier for heap reference slow path: "
747 << instruction_->DebugName();
748
749 __ Bind(GetEntryLabel());
750 SaveLiveRegisters(codegen, locations);
751
752 // We may have to change the index's value, but as `index_` is a
753 // constant member (like other "inputs" of this slow path),
754 // introduce a copy of it, `index`.
755 Location index = index_;
756 if (index_.IsValid()) {
757 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
758 if (instruction_->IsArrayGet()) {
759 // Compute the actual memory offset and store it in `index`.
760 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
761 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
762 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
763 // We are about to change the value of `index_reg` (see the
764 // calls to art::mips64::Mips64Assembler::Sll and
765 // art::mips64::MipsAssembler::Addiu32 below), but it has
766 // not been saved by the previous call to
767 // art::SlowPathCode::SaveLiveRegisters, as it is a
768 // callee-save register --
769 // art::SlowPathCode::SaveLiveRegisters does not consider
770 // callee-save registers, as it has been designed with the
771 // assumption that callee-save registers are supposed to be
772 // handled by the called function. So, as a callee-save
773 // register, `index_reg` _would_ eventually be saved onto
774 // the stack, but it would be too late: we would have
775 // changed its value earlier. Therefore, we manually save
776 // it here into another freely available register,
777 // `free_reg`, chosen of course among the caller-save
778 // registers (as a callee-save `free_reg` register would
779 // exhibit the same problem).
780 //
781 // Note we could have requested a temporary register from
782 // the register allocator instead; but we prefer not to, as
783 // this is a slow path, and we know we can find a
784 // caller-save register that is available.
785 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
786 __ Move(free_reg, index_reg);
787 index_reg = free_reg;
788 index = Location::RegisterLocation(index_reg);
789 } else {
790 // The initial register stored in `index_` has already been
791 // saved in the call to art::SlowPathCode::SaveLiveRegisters
792 // (as it is not a callee-save register), so we can freely
793 // use it.
794 }
795 // Shifting the index value contained in `index_reg` by the scale
796 // factor (2) cannot overflow in practice, as the runtime is
797 // unable to allocate object arrays with a size larger than
798 // 2^26 - 1 (that is, 2^28 - 4 bytes).
799 __ Sll(index_reg, index_reg, TIMES_4);
800 static_assert(
801 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
802 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
803 __ Addiu32(index_reg, index_reg, offset_);
804 } else {
805 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
806 // intrinsics, `index_` is not shifted by a scale factor of 2
807 // (as in the case of ArrayGet), as it is actually an offset
808 // to an object field within an object.
809 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
810 DCHECK(instruction_->GetLocations()->Intrinsified());
811 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
812 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
813 << instruction_->AsInvoke()->GetIntrinsic();
814 DCHECK_EQ(offset_, 0U);
815 DCHECK(index_.IsRegister());
816 }
817 }
818
819 // We're moving two or three locations to locations that could
820 // overlap, so we need a parallel move resolver.
821 InvokeRuntimeCallingConvention calling_convention;
822 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
823 parallel_move.AddMove(ref_,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
825 Primitive::kPrimNot,
826 nullptr);
827 parallel_move.AddMove(obj_,
828 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
829 Primitive::kPrimNot,
830 nullptr);
831 if (index.IsValid()) {
832 parallel_move.AddMove(index,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
834 Primitive::kPrimInt,
835 nullptr);
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 } else {
838 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
839 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
840 }
841 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
842 instruction_,
843 instruction_->GetDexPc(),
844 this);
845 CheckEntrypointTypes<
846 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
847 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ Bc(GetExitLabel());
851 }
852
853 const char* GetDescription() const OVERRIDE {
854 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
855 }
856
857 private:
858 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
859 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
860 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
861 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
862 if (i != ref &&
863 i != obj &&
864 !codegen->IsCoreCalleeSaveRegister(i) &&
865 !codegen->IsBlockedCoreRegister(i)) {
866 return static_cast<GpuRegister>(i);
867 }
868 }
869 // We shall never fail to find a free caller-save register, as
870 // there are more than two core caller-save registers on MIPS64
871 // (meaning it is possible to find one which is different from
872 // `ref` and `obj`).
873 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
874 LOG(FATAL) << "Could not find a free caller-save register";
875 UNREACHABLE();
876 }
877
878 const Location out_;
879 const Location ref_;
880 const Location obj_;
881 const uint32_t offset_;
882 // An additional location containing an index to an array.
883 // Only used for HArrayGet and the UnsafeGetObject &
884 // UnsafeGetObjectVolatile intrinsics.
885 const Location index_;
886
887 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
888};
889
890// Slow path generating a read barrier for a GC root.
891class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
892 public:
893 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
894 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
895 DCHECK(kEmitCompilerReadBarrier);
896 }
897
898 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
899 LocationSummary* locations = instruction_->GetLocations();
900 Primitive::Type type = Primitive::kPrimNot;
901 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
902 DCHECK(locations->CanCall());
903 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
904 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
905 << "Unexpected instruction in read barrier for GC root slow path: "
906 << instruction_->DebugName();
907
908 __ Bind(GetEntryLabel());
909 SaveLiveRegisters(codegen, locations);
910
911 InvokeRuntimeCallingConvention calling_convention;
912 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
913 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
914 root_,
915 Primitive::kPrimNot);
916 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
917 instruction_,
918 instruction_->GetDexPc(),
919 this);
920 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
921 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
922
923 RestoreLiveRegisters(codegen, locations);
924 __ Bc(GetExitLabel());
925 }
926
927 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
928
929 private:
930 const Location out_;
931 const Location root_;
932
933 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
934};
935
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
937 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100938 const CompilerOptions& compiler_options,
939 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940 : CodeGenerator(graph,
941 kNumberOfGpuRegisters,
942 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000943 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
945 arraysize(kCoreCalleeSaves)),
946 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
947 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100948 compiler_options,
949 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100950 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 location_builder_(graph, this),
952 instruction_visitor_(graph, this),
953 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100954 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800955 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800956 uint32_literals_(std::less<uint32_t>(),
957 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 uint64_literals_(std::less<uint64_t>(),
959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800960 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +0100961 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800962 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000963 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +0100964 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800965 jit_string_patches_(StringReferenceValueComparator(),
966 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
967 jit_class_patches_(TypeReferenceValueComparator(),
968 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969 // Save RA (containing the return address) to mimic Quick.
970 AddAllocatedRegister(Location::RegisterLocation(RA));
971}
972
973#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100974// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
975#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700976#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700977
978void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700979 // Ensure that we fix up branches.
980 __ FinalizeCode();
981
982 // Adjust native pc offsets in stack maps.
983 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800984 uint32_t old_position =
985 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700986 uint32_t new_position = __ GetAdjustedPosition(old_position);
987 DCHECK_GE(new_position, old_position);
988 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
989 }
990
991 // Adjust pc offsets for the disassembly information.
992 if (disasm_info_ != nullptr) {
993 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
994 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
995 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
996 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
997 it.second.start = __ GetAdjustedPosition(it.second.start);
998 it.second.end = __ GetAdjustedPosition(it.second.end);
999 }
1000 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1001 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1002 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1003 }
1004 }
1005
Alexey Frunze4dda3372015-06-01 18:31:49 -07001006 CodeGenerator::Finalize(allocator);
1007}
1008
1009Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1010 return codegen_->GetAssembler();
1011}
1012
1013void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001014 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001015 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1024 // Pop reg
1025 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001026 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027}
1028
1029void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1030 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 __ Sd(GpuRegister(reg), SP, 0);
1033}
1034
1035void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1036 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1037 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1038 // Allocate a scratch register other than TMP, if available.
1039 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1040 // automatically unspilled when the scratch scope object is destroyed).
1041 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1042 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001043 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001044 __ LoadFromOffset(load_type,
1045 GpuRegister(ensure_scratch.GetRegister()),
1046 SP,
1047 index1 + stack_offset);
1048 __ LoadFromOffset(load_type,
1049 TMP,
1050 SP,
1051 index2 + stack_offset);
1052 __ StoreToOffset(store_type,
1053 GpuRegister(ensure_scratch.GetRegister()),
1054 SP,
1055 index2 + stack_offset);
1056 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1057}
1058
1059static dwarf::Reg DWARFReg(GpuRegister reg) {
1060 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1061}
1062
David Srbeckyba702002016-02-01 18:15:29 +00001063static dwarf::Reg DWARFReg(FpuRegister reg) {
1064 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1065}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001066
1067void CodeGeneratorMIPS64::GenerateFrameEntry() {
1068 __ Bind(&frame_entry_label_);
1069
1070 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1071
1072 if (do_overflow_check) {
1073 __ LoadFromOffset(kLoadWord,
1074 ZERO,
1075 SP,
1076 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1077 RecordPcInfo(nullptr, 0);
1078 }
1079
Alexey Frunze4dda3372015-06-01 18:31:49 -07001080 if (HasEmptyFrame()) {
1081 return;
1082 }
1083
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001084 // Make sure the frame size isn't unreasonably large.
1085 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1086 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1087 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001088
1089 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001090
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001091 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001092 __ IncreaseFrameSize(ofs);
1093
1094 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1095 GpuRegister reg = kCoreCalleeSaves[i];
1096 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001097 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001098 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001099 __ cfi().RelOffset(DWARFReg(reg), ofs);
1100 }
1101 }
1102
1103 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1104 FpuRegister reg = kFpuCalleeSaves[i];
1105 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001106 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001107 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001108 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001109 }
1110 }
1111
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001112 // Save the current method if we need it. Note that we do not
1113 // do this in HCurrentMethod, as the instruction might have been removed
1114 // in the SSA graph.
1115 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001116 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001117 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001118
1119 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1120 // Initialize should_deoptimize flag to 0.
1121 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001123}
1124
1125void CodeGeneratorMIPS64::GenerateFrameExit() {
1126 __ cfi().RememberState();
1127
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001129 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001130
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001131 // For better instruction scheduling restore RA before other registers.
1132 uint32_t ofs = GetFrameSize();
1133 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001134 GpuRegister reg = kCoreCalleeSaves[i];
1135 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001136 ofs -= kMips64DoublewordSize;
1137 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138 __ cfi().Restore(DWARFReg(reg));
1139 }
1140 }
1141
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001142 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1143 FpuRegister reg = kFpuCalleeSaves[i];
1144 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1145 ofs -= kMips64DoublewordSize;
1146 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1147 __ cfi().Restore(DWARFReg(reg));
1148 }
1149 }
1150
1151 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152 }
1153
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001154 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001155
1156 __ cfi().RestoreState();
1157 __ cfi().DefCFAOffset(GetFrameSize());
1158}
1159
1160void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1161 __ Bind(GetLabelOf(block));
1162}
1163
1164void CodeGeneratorMIPS64::MoveLocation(Location destination,
1165 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001166 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 if (source.Equals(destination)) {
1168 return;
1169 }
1170
1171 // A valid move can always be inferred from the destination and source
1172 // locations. When moving from and to a register, the argument type can be
1173 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001174 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 DCHECK_EQ(unspecified_type, false);
1176
1177 if (destination.IsRegister() || destination.IsFpuRegister()) {
1178 if (unspecified_type) {
1179 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1180 if (source.IsStackSlot() ||
1181 (src_cst != nullptr && (src_cst->IsIntConstant()
1182 || src_cst->IsFloatConstant()
1183 || src_cst->IsNullConstant()))) {
1184 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001185 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 } else {
1187 // If the source is a double stack slot or a 64bit constant, a 64bit
1188 // type is appropriate. Else the source is a register, and since the
1189 // type has not been specified, we chose a 64bit type to force a 64bit
1190 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001191 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001192 }
1193 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001194 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1195 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001196 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1197 // Move to GPR/FPR from stack
1198 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001199 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001200 __ LoadFpuFromOffset(load_type,
1201 destination.AsFpuRegister<FpuRegister>(),
1202 SP,
1203 source.GetStackIndex());
1204 } else {
1205 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1206 __ LoadFromOffset(load_type,
1207 destination.AsRegister<GpuRegister>(),
1208 SP,
1209 source.GetStackIndex());
1210 }
1211 } else if (source.IsConstant()) {
1212 // Move to GPR/FPR from constant
1213 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 gpr = destination.AsRegister<GpuRegister>();
1216 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001218 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1219 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1220 gpr = ZERO;
1221 } else {
1222 __ LoadConst32(gpr, value);
1223 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001225 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1226 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1227 gpr = ZERO;
1228 } else {
1229 __ LoadConst64(gpr, value);
1230 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001232 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001233 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 if (destination.IsRegister()) {
1239 // Move to GPR from GPR
1240 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1241 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 DCHECK(destination.IsFpuRegister());
1243 if (Primitive::Is64BitType(dst_type)) {
1244 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1245 } else {
1246 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1247 }
1248 }
1249 } else if (source.IsFpuRegister()) {
1250 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001253 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1254 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001256 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1257 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 } else {
1259 DCHECK(destination.IsRegister());
1260 if (Primitive::Is64BitType(dst_type)) {
1261 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1262 } else {
1263 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1264 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001265 }
1266 }
1267 } else { // The destination is not a register. It must be a stack slot.
1268 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1269 if (source.IsRegister() || source.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275 }
1276 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1278 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001279 // Move to stack from GPR/FPR
1280 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1281 if (source.IsRegister()) {
1282 __ StoreToOffset(store_type,
1283 source.AsRegister<GpuRegister>(),
1284 SP,
1285 destination.GetStackIndex());
1286 } else {
1287 __ StoreFpuToOffset(store_type,
1288 source.AsFpuRegister<FpuRegister>(),
1289 SP,
1290 destination.GetStackIndex());
1291 }
1292 } else if (source.IsConstant()) {
1293 // Move to stack from constant
1294 HConstant* src_cst = source.GetConstant();
1295 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001296 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001298 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1299 if (value != 0) {
1300 gpr = TMP;
1301 __ LoadConst32(gpr, value);
1302 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001303 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001304 DCHECK(destination.IsDoubleStackSlot());
1305 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1306 if (value != 0) {
1307 gpr = TMP;
1308 __ LoadConst64(gpr, value);
1309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001310 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001311 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001312 } else {
1313 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1314 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1315 // Move to stack from stack
1316 if (destination.IsStackSlot()) {
1317 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1318 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1319 } else {
1320 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1321 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1322 }
1323 }
1324 }
1325}
1326
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001327void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 DCHECK(!loc1.IsConstant());
1329 DCHECK(!loc2.IsConstant());
1330
1331 if (loc1.Equals(loc2)) {
1332 return;
1333 }
1334
1335 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1336 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1337 bool is_fp_reg1 = loc1.IsFpuRegister();
1338 bool is_fp_reg2 = loc2.IsFpuRegister();
1339
1340 if (loc2.IsRegister() && loc1.IsRegister()) {
1341 // Swap 2 GPRs
1342 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1343 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1344 __ Move(TMP, r2);
1345 __ Move(r2, r1);
1346 __ Move(r1, TMP);
1347 } else if (is_fp_reg2 && is_fp_reg1) {
1348 // Swap 2 FPRs
1349 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1350 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001351 if (type == Primitive::kPrimFloat) {
1352 __ MovS(FTMP, r1);
1353 __ MovS(r1, r2);
1354 __ MovS(r2, FTMP);
1355 } else {
1356 DCHECK_EQ(type, Primitive::kPrimDouble);
1357 __ MovD(FTMP, r1);
1358 __ MovD(r1, r2);
1359 __ MovD(r2, FTMP);
1360 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001361 } else if (is_slot1 != is_slot2) {
1362 // Swap GPR/FPR and stack slot
1363 Location reg_loc = is_slot1 ? loc2 : loc1;
1364 Location mem_loc = is_slot1 ? loc1 : loc2;
1365 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1366 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1367 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1368 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1369 if (reg_loc.IsFpuRegister()) {
1370 __ StoreFpuToOffset(store_type,
1371 reg_loc.AsFpuRegister<FpuRegister>(),
1372 SP,
1373 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001374 if (mem_loc.IsStackSlot()) {
1375 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1376 } else {
1377 DCHECK(mem_loc.IsDoubleStackSlot());
1378 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1379 }
1380 } else {
1381 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1382 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1383 }
1384 } else if (is_slot1 && is_slot2) {
1385 move_resolver_.Exchange(loc1.GetStackIndex(),
1386 loc2.GetStackIndex(),
1387 loc1.IsDoubleStackSlot());
1388 } else {
1389 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1390 }
1391}
1392
Calin Juravle175dc732015-08-25 15:42:32 +01001393void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1394 DCHECK(location.IsRegister());
1395 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1396}
1397
Calin Juravlee460d1d2015-09-29 04:52:17 +01001398void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1399 if (location.IsRegister()) {
1400 locations->AddTemp(location);
1401 } else {
1402 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1403 }
1404}
1405
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001406void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1407 GpuRegister value,
1408 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001409 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 GpuRegister card = AT;
1411 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001412 if (value_can_be_null) {
1413 __ Beqzc(value, &done);
1414 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001415 __ LoadFromOffset(kLoadDoubleword,
1416 card,
1417 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001418 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1420 __ Daddu(temp, card, temp);
1421 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001422 if (value_can_be_null) {
1423 __ Bind(&done);
1424 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425}
1426
Alexey Frunze19f6c692016-11-30 19:19:55 -08001427template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1428inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1429 const ArenaDeque<PcRelativePatchInfo>& infos,
1430 ArenaVector<LinkerPatch>* linker_patches) {
1431 for (const PcRelativePatchInfo& info : infos) {
1432 const DexFile& dex_file = info.target_dex_file;
1433 size_t offset_or_index = info.offset_or_index;
1434 DCHECK(info.pc_rel_label.IsBound());
1435 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1436 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
1437 }
1438}
1439
1440void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1441 DCHECK(linker_patches->empty());
1442 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -08001443 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001444 pc_relative_method_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001445 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001446 type_bss_entry_patches_.size() +
1447 pc_relative_string_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001448 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001449 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1450 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001451 if (GetCompilerOptions().IsBootImage()) {
1452 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001453 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001454 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1455 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001456 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1457 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001458 } else {
1459 DCHECK(pc_relative_method_patches_.empty());
1460 DCHECK(pc_relative_type_patches_.empty());
1461 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1462 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001463 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001464 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1465 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001466 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001467}
1468
Vladimir Marko65979462017-05-19 17:25:12 +01001469CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeMethodPatch(
1470 MethodReference target_method) {
1471 return NewPcRelativePatch(*target_method.dex_file,
1472 target_method.dex_method_index,
1473 &pc_relative_method_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001474}
1475
1476CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1477 const DexFile& dex_file, dex::TypeIndex type_index) {
1478 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001479}
1480
Vladimir Marko1998cd02017-01-13 13:02:58 +00001481CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1482 const DexFile& dex_file, dex::TypeIndex type_index) {
1483 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1484}
1485
Vladimir Marko65979462017-05-19 17:25:12 +01001486CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
1487 const DexFile& dex_file, dex::StringIndex string_index) {
1488 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
1489}
1490
Alexey Frunze19f6c692016-11-30 19:19:55 -08001491CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1492 const DexFile& dex_file, uint32_t element_offset) {
1493 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1494}
1495
Alexey Frunze19f6c692016-11-30 19:19:55 -08001496CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1497 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1498 patches->emplace_back(dex_file, offset_or_index);
1499 return &patches->back();
1500}
1501
Alexey Frunzef63f5692016-12-13 17:43:11 -08001502Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1503 return map->GetOrCreate(
1504 value,
1505 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1506}
1507
Alexey Frunze19f6c692016-11-30 19:19:55 -08001508Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1509 return uint64_literals_.GetOrCreate(
1510 value,
1511 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1512}
1513
Alexey Frunzef63f5692016-12-13 17:43:11 -08001514Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001515 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001516}
1517
Alexey Frunze19f6c692016-11-30 19:19:55 -08001518void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1519 GpuRegister out) {
1520 __ Bind(&info->pc_rel_label);
1521 // Add the high half of a 32-bit offset to PC.
1522 __ Auipc(out, /* placeholder */ 0x1234);
1523 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001524 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001525}
1526
Alexey Frunze627c1a02017-01-30 19:28:14 -08001527Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1528 dex::StringIndex string_index,
1529 Handle<mirror::String> handle) {
1530 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1531 reinterpret_cast64<uint64_t>(handle.GetReference()));
1532 return jit_string_patches_.GetOrCreate(
1533 StringReference(&dex_file, string_index),
1534 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1535}
1536
1537Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1538 dex::TypeIndex type_index,
1539 Handle<mirror::Class> handle) {
1540 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1541 reinterpret_cast64<uint64_t>(handle.GetReference()));
1542 return jit_class_patches_.GetOrCreate(
1543 TypeReference(&dex_file, type_index),
1544 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1545}
1546
1547void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1548 const uint8_t* roots_data,
1549 const Literal* literal,
1550 uint64_t index_in_table) const {
1551 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1552 uintptr_t address =
1553 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1554 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1555}
1556
1557void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1558 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001559 const StringReference& string_reference = entry.first;
1560 Literal* table_entry_literal = entry.second;
1561 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001562 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001563 uint64_t index_in_table = it->second;
1564 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001565 }
1566 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001567 const TypeReference& type_reference = entry.first;
1568 Literal* table_entry_literal = entry.second;
1569 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001570 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001571 uint64_t index_in_table = it->second;
1572 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001573 }
1574}
1575
David Brazdil58282f42016-01-14 12:45:10 +00001576void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001577 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1578 blocked_core_registers_[ZERO] = true;
1579 blocked_core_registers_[K0] = true;
1580 blocked_core_registers_[K1] = true;
1581 blocked_core_registers_[GP] = true;
1582 blocked_core_registers_[SP] = true;
1583 blocked_core_registers_[RA] = true;
1584
Lazar Trsicd9672662015-09-03 17:33:01 +02001585 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1586 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001587 blocked_core_registers_[AT] = true;
1588 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001589 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001590 blocked_fpu_registers_[FTMP] = true;
1591
1592 // Reserve suspend and thread registers.
1593 blocked_core_registers_[S0] = true;
1594 blocked_core_registers_[TR] = true;
1595
1596 // Reserve T9 for function calls
1597 blocked_core_registers_[T9] = true;
1598
Goran Jakovljevic782be112016-06-21 12:39:04 +02001599 if (GetGraph()->IsDebuggable()) {
1600 // Stubs do not save callee-save floating point registers. If the graph
1601 // is debuggable, we need to deal with these registers differently. For
1602 // now, just block them.
1603 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1604 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1605 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001606 }
1607}
1608
Alexey Frunze4dda3372015-06-01 18:31:49 -07001609size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1610 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001611 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001612}
1613
1614size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1615 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001616 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001617}
1618
1619size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001620 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1621 FpuRegister(reg_id),
1622 SP,
1623 stack_index);
1624 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001625}
1626
1627size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001628 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1629 FpuRegister(reg_id),
1630 SP,
1631 stack_index);
1632 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001633}
1634
1635void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001636 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001637}
1638
1639void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001640 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641}
1642
Calin Juravle175dc732015-08-25 15:42:32 +01001643void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001644 HInstruction* instruction,
1645 uint32_t dex_pc,
1646 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001647 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001648 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001649 if (EntrypointRequiresStackMap(entrypoint)) {
1650 RecordPcInfo(instruction, dex_pc, slow_path);
1651 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001652}
1653
Alexey Frunze15958152017-02-09 19:08:30 -08001654void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1655 HInstruction* instruction,
1656 SlowPathCode* slow_path) {
1657 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1658 GenerateInvokeRuntime(entry_point_offset);
1659}
1660
1661void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1662 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1663 __ Jalr(T9);
1664 __ Nop();
1665}
1666
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1668 GpuRegister class_reg) {
1669 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1670 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1671 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001672 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1673 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 __ Bind(slow_path->GetExitLabel());
1675}
1676
1677void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1678 __ Sync(0); // only stype 0 is supported
1679}
1680
1681void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1682 HBasicBlock* successor) {
1683 SuspendCheckSlowPathMIPS64* slow_path =
1684 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1685 codegen_->AddSlowPath(slow_path);
1686
1687 __ LoadFromOffset(kLoadUnsignedHalfword,
1688 TMP,
1689 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001690 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001691 if (successor == nullptr) {
1692 __ Bnezc(TMP, slow_path->GetEntryLabel());
1693 __ Bind(slow_path->GetReturnLabel());
1694 } else {
1695 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001696 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697 // slow_path will return to GetLabelOf(successor).
1698 }
1699}
1700
1701InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1702 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001703 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704 assembler_(codegen->GetAssembler()),
1705 codegen_(codegen) {}
1706
1707void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1708 DCHECK_EQ(instruction->InputCount(), 2U);
1709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1710 Primitive::Type type = instruction->GetResultType();
1711 switch (type) {
1712 case Primitive::kPrimInt:
1713 case Primitive::kPrimLong: {
1714 locations->SetInAt(0, Location::RequiresRegister());
1715 HInstruction* right = instruction->InputAt(1);
1716 bool can_use_imm = false;
1717 if (right->IsConstant()) {
1718 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1719 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1720 can_use_imm = IsUint<16>(imm);
1721 } else if (instruction->IsAdd()) {
1722 can_use_imm = IsInt<16>(imm);
1723 } else {
1724 DCHECK(instruction->IsSub());
1725 can_use_imm = IsInt<16>(-imm);
1726 }
1727 }
1728 if (can_use_imm)
1729 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1730 else
1731 locations->SetInAt(1, Location::RequiresRegister());
1732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1733 }
1734 break;
1735
1736 case Primitive::kPrimFloat:
1737 case Primitive::kPrimDouble:
1738 locations->SetInAt(0, Location::RequiresFpuRegister());
1739 locations->SetInAt(1, Location::RequiresFpuRegister());
1740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1741 break;
1742
1743 default:
1744 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1745 }
1746}
1747
1748void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1749 Primitive::Type type = instruction->GetType();
1750 LocationSummary* locations = instruction->GetLocations();
1751
1752 switch (type) {
1753 case Primitive::kPrimInt:
1754 case Primitive::kPrimLong: {
1755 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1756 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1757 Location rhs_location = locations->InAt(1);
1758
1759 GpuRegister rhs_reg = ZERO;
1760 int64_t rhs_imm = 0;
1761 bool use_imm = rhs_location.IsConstant();
1762 if (use_imm) {
1763 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1764 } else {
1765 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1766 }
1767
1768 if (instruction->IsAnd()) {
1769 if (use_imm)
1770 __ Andi(dst, lhs, rhs_imm);
1771 else
1772 __ And(dst, lhs, rhs_reg);
1773 } else if (instruction->IsOr()) {
1774 if (use_imm)
1775 __ Ori(dst, lhs, rhs_imm);
1776 else
1777 __ Or(dst, lhs, rhs_reg);
1778 } else if (instruction->IsXor()) {
1779 if (use_imm)
1780 __ Xori(dst, lhs, rhs_imm);
1781 else
1782 __ Xor(dst, lhs, rhs_reg);
1783 } else if (instruction->IsAdd()) {
1784 if (type == Primitive::kPrimInt) {
1785 if (use_imm)
1786 __ Addiu(dst, lhs, rhs_imm);
1787 else
1788 __ Addu(dst, lhs, rhs_reg);
1789 } else {
1790 if (use_imm)
1791 __ Daddiu(dst, lhs, rhs_imm);
1792 else
1793 __ Daddu(dst, lhs, rhs_reg);
1794 }
1795 } else {
1796 DCHECK(instruction->IsSub());
1797 if (type == Primitive::kPrimInt) {
1798 if (use_imm)
1799 __ Addiu(dst, lhs, -rhs_imm);
1800 else
1801 __ Subu(dst, lhs, rhs_reg);
1802 } else {
1803 if (use_imm)
1804 __ Daddiu(dst, lhs, -rhs_imm);
1805 else
1806 __ Dsubu(dst, lhs, rhs_reg);
1807 }
1808 }
1809 break;
1810 }
1811 case Primitive::kPrimFloat:
1812 case Primitive::kPrimDouble: {
1813 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1814 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1815 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1816 if (instruction->IsAdd()) {
1817 if (type == Primitive::kPrimFloat)
1818 __ AddS(dst, lhs, rhs);
1819 else
1820 __ AddD(dst, lhs, rhs);
1821 } else if (instruction->IsSub()) {
1822 if (type == Primitive::kPrimFloat)
1823 __ SubS(dst, lhs, rhs);
1824 else
1825 __ SubD(dst, lhs, rhs);
1826 } else {
1827 LOG(FATAL) << "Unexpected floating-point binary operation";
1828 }
1829 break;
1830 }
1831 default:
1832 LOG(FATAL) << "Unexpected binary operation type " << type;
1833 }
1834}
1835
1836void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001837 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001838
1839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1840 Primitive::Type type = instr->GetResultType();
1841 switch (type) {
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimLong: {
1844 locations->SetInAt(0, Location::RequiresRegister());
1845 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001847 break;
1848 }
1849 default:
1850 LOG(FATAL) << "Unexpected shift type " << type;
1851 }
1852}
1853
1854void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001855 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001856 LocationSummary* locations = instr->GetLocations();
1857 Primitive::Type type = instr->GetType();
1858
1859 switch (type) {
1860 case Primitive::kPrimInt:
1861 case Primitive::kPrimLong: {
1862 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1863 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1864 Location rhs_location = locations->InAt(1);
1865
1866 GpuRegister rhs_reg = ZERO;
1867 int64_t rhs_imm = 0;
1868 bool use_imm = rhs_location.IsConstant();
1869 if (use_imm) {
1870 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1871 } else {
1872 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1873 }
1874
1875 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001876 uint32_t shift_value = rhs_imm &
1877 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001878
Alexey Frunze92d90602015-12-18 18:16:36 -08001879 if (shift_value == 0) {
1880 if (dst != lhs) {
1881 __ Move(dst, lhs);
1882 }
1883 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001884 if (instr->IsShl()) {
1885 __ Sll(dst, lhs, shift_value);
1886 } else if (instr->IsShr()) {
1887 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001888 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001889 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001890 } else {
1891 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001892 }
1893 } else {
1894 if (shift_value < 32) {
1895 if (instr->IsShl()) {
1896 __ Dsll(dst, lhs, shift_value);
1897 } else if (instr->IsShr()) {
1898 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001899 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001900 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001901 } else {
1902 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903 }
1904 } else {
1905 shift_value -= 32;
1906 if (instr->IsShl()) {
1907 __ Dsll32(dst, lhs, shift_value);
1908 } else if (instr->IsShr()) {
1909 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001910 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001911 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 } else {
1913 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001914 }
1915 }
1916 }
1917 } else {
1918 if (type == Primitive::kPrimInt) {
1919 if (instr->IsShl()) {
1920 __ Sllv(dst, lhs, rhs_reg);
1921 } else if (instr->IsShr()) {
1922 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001923 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001924 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001925 } else {
1926 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001927 }
1928 } else {
1929 if (instr->IsShl()) {
1930 __ Dsllv(dst, lhs, rhs_reg);
1931 } else if (instr->IsShr()) {
1932 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001933 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001934 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001935 } else {
1936 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001937 }
1938 }
1939 }
1940 break;
1941 }
1942 default:
1943 LOG(FATAL) << "Unexpected shift operation type " << type;
1944 }
1945}
1946
1947void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1948 HandleBinaryOp(instruction);
1949}
1950
1951void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1952 HandleBinaryOp(instruction);
1953}
1954
1955void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1956 HandleBinaryOp(instruction);
1957}
1958
1959void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1960 HandleBinaryOp(instruction);
1961}
1962
1963void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001964 Primitive::Type type = instruction->GetType();
1965 bool object_array_get_with_read_barrier =
1966 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08001968 new (GetGraph()->GetArena()) LocationSummary(instruction,
1969 object_array_get_with_read_barrier
1970 ? LocationSummary::kCallOnSlowPath
1971 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001972 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1973 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1974 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001975 locations->SetInAt(0, Location::RequiresRegister());
1976 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08001977 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001978 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1979 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08001980 // The output overlaps in the case of an object array get with
1981 // read barriers enabled: we do not want the move to overwrite the
1982 // array's location, as we need it to emit the read barrier.
1983 locations->SetOut(Location::RequiresRegister(),
1984 object_array_get_with_read_barrier
1985 ? Location::kOutputOverlap
1986 : Location::kNoOutputOverlap);
1987 }
1988 // We need a temporary register for the read barrier marking slow
1989 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
1990 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1991 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992 }
1993}
1994
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001995static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
1996 auto null_checker = [codegen, instruction]() {
1997 codegen->MaybeRecordImplicitNullCheck(instruction);
1998 };
1999 return null_checker;
2000}
2001
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2003 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002004 Location obj_loc = locations->InAt(0);
2005 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2006 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002007 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002008 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002009 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002010
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002011 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002012 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2013 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002014 switch (type) {
2015 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002016 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002017 if (index.IsConstant()) {
2018 size_t offset =
2019 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002020 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 } else {
2022 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002023 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002024 }
2025 break;
2026 }
2027
2028 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002029 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002030 if (index.IsConstant()) {
2031 size_t offset =
2032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002033 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002034 } else {
2035 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002036 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002037 }
2038 break;
2039 }
2040
2041 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002042 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043 if (index.IsConstant()) {
2044 size_t offset =
2045 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002046 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002047 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002048 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002049 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 }
2051 break;
2052 }
2053
2054 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002055 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002056 if (maybe_compressed_char_at) {
2057 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002058 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002059 __ Dext(TMP, TMP, 0, 1);
2060 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2061 "Expecting 0=compressed, 1=uncompressed");
2062 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002064 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2065 if (maybe_compressed_char_at) {
2066 Mips64Label uncompressed_load, done;
2067 __ Bnezc(TMP, &uncompressed_load);
2068 __ LoadFromOffset(kLoadUnsignedByte,
2069 out,
2070 obj,
2071 data_offset + (const_index << TIMES_1));
2072 __ Bc(&done);
2073 __ Bind(&uncompressed_load);
2074 __ LoadFromOffset(kLoadUnsignedHalfword,
2075 out,
2076 obj,
2077 data_offset + (const_index << TIMES_2));
2078 __ Bind(&done);
2079 } else {
2080 __ LoadFromOffset(kLoadUnsignedHalfword,
2081 out,
2082 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002083 data_offset + (const_index << TIMES_2),
2084 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002085 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002086 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002087 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2088 if (maybe_compressed_char_at) {
2089 Mips64Label uncompressed_load, done;
2090 __ Bnezc(TMP, &uncompressed_load);
2091 __ Daddu(TMP, obj, index_reg);
2092 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2093 __ Bc(&done);
2094 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002095 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002096 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2097 __ Bind(&done);
2098 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002099 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002100 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 }
2103 break;
2104 }
2105
Alexey Frunze15958152017-02-09 19:08:30 -08002106 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002108 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002109 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2110 if (index.IsConstant()) {
2111 size_t offset =
2112 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002113 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002114 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002115 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002116 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002117 }
2118 break;
2119 }
2120
Alexey Frunze15958152017-02-09 19:08:30 -08002121 case Primitive::kPrimNot: {
2122 static_assert(
2123 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2124 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2125 // /* HeapReference<Object> */ out =
2126 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2127 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2128 Location temp = locations->GetTemp(0);
2129 // Note that a potential implicit null check is handled in this
2130 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2131 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2132 out_loc,
2133 obj,
2134 data_offset,
2135 index,
2136 temp,
2137 /* needs_null_check */ true);
2138 } else {
2139 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2140 if (index.IsConstant()) {
2141 size_t offset =
2142 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2143 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2144 // If read barriers are enabled, emit read barriers other than
2145 // Baker's using a slow path (and also unpoison the loaded
2146 // reference, if heap poisoning is enabled).
2147 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2148 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002149 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002150 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2151 // If read barriers are enabled, emit read barriers other than
2152 // Baker's using a slow path (and also unpoison the loaded
2153 // reference, if heap poisoning is enabled).
2154 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2155 out_loc,
2156 out_loc,
2157 obj_loc,
2158 data_offset,
2159 index);
2160 }
2161 }
2162 break;
2163 }
2164
Alexey Frunze4dda3372015-06-01 18:31:49 -07002165 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002166 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002167 if (index.IsConstant()) {
2168 size_t offset =
2169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002170 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002171 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002172 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002173 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002174 }
2175 break;
2176 }
2177
2178 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002179 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002180 if (index.IsConstant()) {
2181 size_t offset =
2182 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002183 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002184 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002185 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002186 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187 }
2188 break;
2189 }
2190
2191 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002192 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 if (index.IsConstant()) {
2194 size_t offset =
2195 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002196 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002197 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002198 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002199 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 }
2201 break;
2202 }
2203
2204 case Primitive::kPrimVoid:
2205 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2206 UNREACHABLE();
2207 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002208}
2209
2210void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2211 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2212 locations->SetInAt(0, Location::RequiresRegister());
2213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2214}
2215
2216void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2217 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002218 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002219 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2220 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2221 __ LoadFromOffset(kLoadWord, out, obj, offset);
2222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002223 // Mask out compression flag from String's array length.
2224 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2225 __ Srl(out, out, 1u);
2226 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002227}
2228
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002229Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2230 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2231 ? Location::ConstantLocation(instruction->AsConstant())
2232 : Location::RequiresRegister();
2233}
2234
2235Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2236 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2237 // We can store a non-zero float or double constant without first loading it into the FPU,
2238 // but we should only prefer this if the constant has a single use.
2239 if (instruction->IsConstant() &&
2240 (instruction->AsConstant()->IsZeroBitPattern() ||
2241 instruction->GetUses().HasExactlyOneElement())) {
2242 return Location::ConstantLocation(instruction->AsConstant());
2243 // Otherwise fall through and require an FPU register for the constant.
2244 }
2245 return Location::RequiresFpuRegister();
2246}
2247
Alexey Frunze4dda3372015-06-01 18:31:49 -07002248void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002249 Primitive::Type value_type = instruction->GetComponentType();
2250
2251 bool needs_write_barrier =
2252 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2253 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2254
Alexey Frunze4dda3372015-06-01 18:31:49 -07002255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2256 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002257 may_need_runtime_call_for_type_check ?
2258 LocationSummary::kCallOnSlowPath :
2259 LocationSummary::kNoCall);
2260
2261 locations->SetInAt(0, Location::RequiresRegister());
2262 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2263 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2264 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002265 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002266 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2267 }
2268 if (needs_write_barrier) {
2269 // Temporary register for the write barrier.
2270 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002271 }
2272}
2273
2274void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2275 LocationSummary* locations = instruction->GetLocations();
2276 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2277 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002278 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002279 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002280 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281 bool needs_write_barrier =
2282 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002283 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002284 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285
2286 switch (value_type) {
2287 case Primitive::kPrimBoolean:
2288 case Primitive::kPrimByte: {
2289 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002290 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002291 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002292 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002293 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2294 }
2295 if (value_location.IsConstant()) {
2296 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2297 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2298 } else {
2299 GpuRegister value = value_location.AsRegister<GpuRegister>();
2300 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 }
2302 break;
2303 }
2304
2305 case Primitive::kPrimShort:
2306 case Primitive::kPrimChar: {
2307 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002308 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002309 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002310 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002311 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002312 }
2313 if (value_location.IsConstant()) {
2314 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2315 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2316 } else {
2317 GpuRegister value = value_location.AsRegister<GpuRegister>();
2318 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002319 }
2320 break;
2321 }
2322
Alexey Frunze15958152017-02-09 19:08:30 -08002323 case Primitive::kPrimInt: {
2324 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2325 if (index.IsConstant()) {
2326 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2327 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002328 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002329 }
2330 if (value_location.IsConstant()) {
2331 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2332 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2333 } else {
2334 GpuRegister value = value_location.AsRegister<GpuRegister>();
2335 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2336 }
2337 break;
2338 }
2339
Alexey Frunze4dda3372015-06-01 18:31:49 -07002340 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002341 if (value_location.IsConstant()) {
2342 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002344 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002345 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002347 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002348 }
Alexey Frunze15958152017-02-09 19:08:30 -08002349 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2350 DCHECK_EQ(value, 0);
2351 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2352 DCHECK(!needs_write_barrier);
2353 DCHECK(!may_need_runtime_call_for_type_check);
2354 break;
2355 }
2356
2357 DCHECK(needs_write_barrier);
2358 GpuRegister value = value_location.AsRegister<GpuRegister>();
2359 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2360 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2361 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2362 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2363 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2364 Mips64Label done;
2365 SlowPathCodeMIPS64* slow_path = nullptr;
2366
2367 if (may_need_runtime_call_for_type_check) {
2368 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2369 codegen_->AddSlowPath(slow_path);
2370 if (instruction->GetValueCanBeNull()) {
2371 Mips64Label non_zero;
2372 __ Bnezc(value, &non_zero);
2373 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2374 if (index.IsConstant()) {
2375 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002376 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002377 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002378 }
Alexey Frunze15958152017-02-09 19:08:30 -08002379 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2380 __ Bc(&done);
2381 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002382 }
Alexey Frunze15958152017-02-09 19:08:30 -08002383
2384 // Note that when read barriers are enabled, the type checks
2385 // are performed without read barriers. This is fine, even in
2386 // the case where a class object is in the from-space after
2387 // the flip, as a comparison involving such a type would not
2388 // produce a false positive; it may of course produce a false
2389 // negative, in which case we would take the ArraySet slow
2390 // path.
2391
2392 // /* HeapReference<Class> */ temp1 = obj->klass_
2393 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2394 __ MaybeUnpoisonHeapReference(temp1);
2395
2396 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2397 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2398 // /* HeapReference<Class> */ temp2 = value->klass_
2399 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2400 // If heap poisoning is enabled, no need to unpoison `temp1`
2401 // nor `temp2`, as we are comparing two poisoned references.
2402
2403 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2404 Mips64Label do_put;
2405 __ Beqc(temp1, temp2, &do_put);
2406 // If heap poisoning is enabled, the `temp1` reference has
2407 // not been unpoisoned yet; unpoison it now.
2408 __ MaybeUnpoisonHeapReference(temp1);
2409
2410 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2411 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2412 // If heap poisoning is enabled, no need to unpoison
2413 // `temp1`, as we are comparing against null below.
2414 __ Bnezc(temp1, slow_path->GetEntryLabel());
2415 __ Bind(&do_put);
2416 } else {
2417 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2418 }
2419 }
2420
2421 GpuRegister source = value;
2422 if (kPoisonHeapReferences) {
2423 // Note that in the case where `value` is a null reference,
2424 // we do not enter this block, as a null reference does not
2425 // need poisoning.
2426 __ Move(temp1, value);
2427 __ PoisonHeapReference(temp1);
2428 source = temp1;
2429 }
2430
2431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2432 if (index.IsConstant()) {
2433 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002434 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002435 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002436 }
2437 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2438
2439 if (!may_need_runtime_call_for_type_check) {
2440 codegen_->MaybeRecordImplicitNullCheck(instruction);
2441 }
2442
2443 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2444
2445 if (done.IsLinked()) {
2446 __ Bind(&done);
2447 }
2448
2449 if (slow_path != nullptr) {
2450 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002451 }
2452 break;
2453 }
2454
2455 case Primitive::kPrimLong: {
2456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_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_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002460 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002461 }
2462 if (value_location.IsConstant()) {
2463 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2464 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2465 } else {
2466 GpuRegister value = value_location.AsRegister<GpuRegister>();
2467 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002468 }
2469 break;
2470 }
2471
2472 case Primitive::kPrimFloat: {
2473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002474 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002475 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002476 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002477 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002478 }
2479 if (value_location.IsConstant()) {
2480 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2481 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2482 } else {
2483 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2484 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002485 }
2486 break;
2487 }
2488
2489 case Primitive::kPrimDouble: {
2490 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002491 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002492 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002493 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002494 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002495 }
2496 if (value_location.IsConstant()) {
2497 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2498 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2499 } else {
2500 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2501 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002502 }
2503 break;
2504 }
2505
2506 case Primitive::kPrimVoid:
2507 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2508 UNREACHABLE();
2509 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002510}
2511
2512void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002513 RegisterSet caller_saves = RegisterSet::Empty();
2514 InvokeRuntimeCallingConvention calling_convention;
2515 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2516 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2517 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002518 locations->SetInAt(0, Location::RequiresRegister());
2519 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002520}
2521
2522void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2523 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002524 BoundsCheckSlowPathMIPS64* slow_path =
2525 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002526 codegen_->AddSlowPath(slow_path);
2527
2528 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2529 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2530
2531 // length is limited by the maximum positive signed 32-bit integer.
2532 // Unsigned comparison of length and index checks for index < 0
2533 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002534 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002535}
2536
Alexey Frunze15958152017-02-09 19:08:30 -08002537// Temp is used for read barrier.
2538static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2539 if (kEmitCompilerReadBarrier &&
2540 (kUseBakerReadBarrier ||
2541 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2542 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2543 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2544 return 1;
2545 }
2546 return 0;
2547}
2548
2549// Extra temp is used for read barrier.
2550static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2551 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2552}
2553
Alexey Frunze4dda3372015-06-01 18:31:49 -07002554void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002555 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2556 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2557
2558 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2559 switch (type_check_kind) {
2560 case TypeCheckKind::kExactCheck:
2561 case TypeCheckKind::kAbstractClassCheck:
2562 case TypeCheckKind::kClassHierarchyCheck:
2563 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002564 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002565 ? LocationSummary::kCallOnSlowPath
2566 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2567 break;
2568 case TypeCheckKind::kArrayCheck:
2569 case TypeCheckKind::kUnresolvedCheck:
2570 case TypeCheckKind::kInterfaceCheck:
2571 call_kind = LocationSummary::kCallOnSlowPath;
2572 break;
2573 }
2574
2575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002576 locations->SetInAt(0, Location::RequiresRegister());
2577 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002578 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002579}
2580
2581void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002582 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002583 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002584 Location obj_loc = locations->InAt(0);
2585 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002587 Location temp_loc = locations->GetTemp(0);
2588 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2589 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2590 DCHECK_LE(num_temps, 2u);
2591 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002592 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2593 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2594 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2595 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2596 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2597 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2598 const uint32_t object_array_data_offset =
2599 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2600 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002602 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2603 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2604 // read barriers is done for performance and code size reasons.
2605 bool is_type_check_slow_path_fatal = false;
2606 if (!kEmitCompilerReadBarrier) {
2607 is_type_check_slow_path_fatal =
2608 (type_check_kind == TypeCheckKind::kExactCheck ||
2609 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2610 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2611 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2612 !instruction->CanThrowIntoCatchBlock();
2613 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002614 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002615 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2616 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 codegen_->AddSlowPath(slow_path);
2618
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002619 // Avoid this check if we know `obj` is not null.
2620 if (instruction->MustDoNullCheck()) {
2621 __ Beqzc(obj, &done);
2622 }
2623
2624 switch (type_check_kind) {
2625 case TypeCheckKind::kExactCheck:
2626 case TypeCheckKind::kArrayCheck: {
2627 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002628 GenerateReferenceLoadTwoRegisters(instruction,
2629 temp_loc,
2630 obj_loc,
2631 class_offset,
2632 maybe_temp2_loc,
2633 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002634 // Jump to slow path for throwing the exception or doing a
2635 // more involved array check.
2636 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2637 break;
2638 }
2639
2640 case TypeCheckKind::kAbstractClassCheck: {
2641 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002642 GenerateReferenceLoadTwoRegisters(instruction,
2643 temp_loc,
2644 obj_loc,
2645 class_offset,
2646 maybe_temp2_loc,
2647 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002648 // If the class is abstract, we eagerly fetch the super class of the
2649 // object to avoid doing a comparison we know will fail.
2650 Mips64Label loop;
2651 __ Bind(&loop);
2652 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002653 GenerateReferenceLoadOneRegister(instruction,
2654 temp_loc,
2655 super_offset,
2656 maybe_temp2_loc,
2657 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002658 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2659 // exception.
2660 __ Beqzc(temp, slow_path->GetEntryLabel());
2661 // Otherwise, compare the classes.
2662 __ Bnec(temp, cls, &loop);
2663 break;
2664 }
2665
2666 case TypeCheckKind::kClassHierarchyCheck: {
2667 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002668 GenerateReferenceLoadTwoRegisters(instruction,
2669 temp_loc,
2670 obj_loc,
2671 class_offset,
2672 maybe_temp2_loc,
2673 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002674 // Walk over the class hierarchy to find a match.
2675 Mips64Label loop;
2676 __ Bind(&loop);
2677 __ Beqc(temp, cls, &done);
2678 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002679 GenerateReferenceLoadOneRegister(instruction,
2680 temp_loc,
2681 super_offset,
2682 maybe_temp2_loc,
2683 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002684 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2685 // exception. Otherwise, jump to the beginning of the loop.
2686 __ Bnezc(temp, &loop);
2687 __ Bc(slow_path->GetEntryLabel());
2688 break;
2689 }
2690
2691 case TypeCheckKind::kArrayObjectCheck: {
2692 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002693 GenerateReferenceLoadTwoRegisters(instruction,
2694 temp_loc,
2695 obj_loc,
2696 class_offset,
2697 maybe_temp2_loc,
2698 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002699 // Do an exact check.
2700 __ Beqc(temp, cls, &done);
2701 // Otherwise, we need to check that the object's class is a non-primitive array.
2702 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002703 GenerateReferenceLoadOneRegister(instruction,
2704 temp_loc,
2705 component_offset,
2706 maybe_temp2_loc,
2707 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002708 // If the component type is null, jump to the slow path to throw the exception.
2709 __ Beqzc(temp, slow_path->GetEntryLabel());
2710 // Otherwise, the object is indeed an array, further check that this component
2711 // type is not a primitive type.
2712 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2713 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2714 __ Bnezc(temp, slow_path->GetEntryLabel());
2715 break;
2716 }
2717
2718 case TypeCheckKind::kUnresolvedCheck:
2719 // We always go into the type check slow path for the unresolved check case.
2720 // We cannot directly call the CheckCast runtime entry point
2721 // without resorting to a type checking slow path here (i.e. by
2722 // calling InvokeRuntime directly), as it would require to
2723 // assign fixed registers for the inputs of this HInstanceOf
2724 // instruction (following the runtime calling convention), which
2725 // might be cluttered by the potential first read barrier
2726 // emission at the beginning of this method.
2727 __ Bc(slow_path->GetEntryLabel());
2728 break;
2729
2730 case TypeCheckKind::kInterfaceCheck: {
2731 // Avoid read barriers to improve performance of the fast path. We can not get false
2732 // positives by doing this.
2733 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002734 GenerateReferenceLoadTwoRegisters(instruction,
2735 temp_loc,
2736 obj_loc,
2737 class_offset,
2738 maybe_temp2_loc,
2739 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002740 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002741 GenerateReferenceLoadTwoRegisters(instruction,
2742 temp_loc,
2743 temp_loc,
2744 iftable_offset,
2745 maybe_temp2_loc,
2746 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002747 // Iftable is never null.
2748 __ Lw(TMP, temp, array_length_offset);
2749 // Loop through the iftable and check if any class matches.
2750 Mips64Label loop;
2751 __ Bind(&loop);
2752 __ Beqzc(TMP, slow_path->GetEntryLabel());
2753 __ Lwu(AT, temp, object_array_data_offset);
2754 __ MaybeUnpoisonHeapReference(AT);
2755 // Go to next interface.
2756 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2757 __ Addiu(TMP, TMP, -2);
2758 // Compare the classes and continue the loop if they do not match.
2759 __ Bnec(AT, cls, &loop);
2760 break;
2761 }
2762 }
2763
2764 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002765 __ Bind(slow_path->GetExitLabel());
2766}
2767
2768void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2769 LocationSummary* locations =
2770 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2771 locations->SetInAt(0, Location::RequiresRegister());
2772 if (check->HasUses()) {
2773 locations->SetOut(Location::SameAsFirstInput());
2774 }
2775}
2776
2777void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2778 // We assume the class is not null.
2779 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2780 check->GetLoadClass(),
2781 check,
2782 check->GetDexPc(),
2783 true);
2784 codegen_->AddSlowPath(slow_path);
2785 GenerateClassInitializationCheck(slow_path,
2786 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2787}
2788
2789void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2790 Primitive::Type in_type = compare->InputAt(0)->GetType();
2791
Alexey Frunze299a9392015-12-08 16:08:02 -08002792 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002793
2794 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002795 case Primitive::kPrimBoolean:
2796 case Primitive::kPrimByte:
2797 case Primitive::kPrimShort:
2798 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002799 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002800 case Primitive::kPrimLong:
2801 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002802 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2804 break;
2805
2806 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002807 case Primitive::kPrimDouble:
2808 locations->SetInAt(0, Location::RequiresFpuRegister());
2809 locations->SetInAt(1, Location::RequiresFpuRegister());
2810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002811 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002812
2813 default:
2814 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2815 }
2816}
2817
2818void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2819 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002820 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002821 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2822
2823 // 0 if: left == right
2824 // 1 if: left > right
2825 // -1 if: left < right
2826 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002827 case Primitive::kPrimBoolean:
2828 case Primitive::kPrimByte:
2829 case Primitive::kPrimShort:
2830 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002831 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002832 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002833 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002834 Location rhs_location = locations->InAt(1);
2835 bool use_imm = rhs_location.IsConstant();
2836 GpuRegister rhs = ZERO;
2837 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002838 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002839 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2840 if (value != 0) {
2841 rhs = AT;
2842 __ LoadConst64(rhs, value);
2843 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002844 } else {
2845 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2846 if (value != 0) {
2847 rhs = AT;
2848 __ LoadConst32(rhs, value);
2849 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002850 }
2851 } else {
2852 rhs = rhs_location.AsRegister<GpuRegister>();
2853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002854 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002855 __ Slt(res, rhs, lhs);
2856 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002857 break;
2858 }
2859
Alexey Frunze299a9392015-12-08 16:08:02 -08002860 case Primitive::kPrimFloat: {
2861 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2862 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2863 Mips64Label done;
2864 __ CmpEqS(FTMP, lhs, rhs);
2865 __ LoadConst32(res, 0);
2866 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002867 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002868 __ CmpLtS(FTMP, lhs, rhs);
2869 __ LoadConst32(res, -1);
2870 __ Bc1nez(FTMP, &done);
2871 __ LoadConst32(res, 1);
2872 } else {
2873 __ CmpLtS(FTMP, rhs, lhs);
2874 __ LoadConst32(res, 1);
2875 __ Bc1nez(FTMP, &done);
2876 __ LoadConst32(res, -1);
2877 }
2878 __ Bind(&done);
2879 break;
2880 }
2881
Alexey Frunze4dda3372015-06-01 18:31:49 -07002882 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002883 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2884 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2885 Mips64Label done;
2886 __ CmpEqD(FTMP, lhs, rhs);
2887 __ LoadConst32(res, 0);
2888 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002889 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002890 __ CmpLtD(FTMP, lhs, rhs);
2891 __ LoadConst32(res, -1);
2892 __ Bc1nez(FTMP, &done);
2893 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002894 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002895 __ CmpLtD(FTMP, rhs, lhs);
2896 __ LoadConst32(res, 1);
2897 __ Bc1nez(FTMP, &done);
2898 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002899 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002900 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002901 break;
2902 }
2903
2904 default:
2905 LOG(FATAL) << "Unimplemented compare type " << in_type;
2906 }
2907}
2908
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002909void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002911 switch (instruction->InputAt(0)->GetType()) {
2912 default:
2913 case Primitive::kPrimLong:
2914 locations->SetInAt(0, Location::RequiresRegister());
2915 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2916 break;
2917
2918 case Primitive::kPrimFloat:
2919 case Primitive::kPrimDouble:
2920 locations->SetInAt(0, Location::RequiresFpuRegister());
2921 locations->SetInAt(1, Location::RequiresFpuRegister());
2922 break;
2923 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002924 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2926 }
2927}
2928
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002929void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002930 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002931 return;
2932 }
2933
Alexey Frunze299a9392015-12-08 16:08:02 -08002934 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002935 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002936 switch (type) {
2937 default:
2938 // Integer case.
2939 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2940 return;
2941 case Primitive::kPrimLong:
2942 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2943 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002944 case Primitive::kPrimFloat:
2945 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002946 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2947 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002948 }
2949}
2950
Alexey Frunzec857c742015-09-23 15:12:39 -07002951void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2952 DCHECK(instruction->IsDiv() || instruction->IsRem());
2953 Primitive::Type type = instruction->GetResultType();
2954
2955 LocationSummary* locations = instruction->GetLocations();
2956 Location second = locations->InAt(1);
2957 DCHECK(second.IsConstant());
2958
2959 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2960 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2961 int64_t imm = Int64FromConstant(second.GetConstant());
2962 DCHECK(imm == 1 || imm == -1);
2963
2964 if (instruction->IsRem()) {
2965 __ Move(out, ZERO);
2966 } else {
2967 if (imm == -1) {
2968 if (type == Primitive::kPrimInt) {
2969 __ Subu(out, ZERO, dividend);
2970 } else {
2971 DCHECK_EQ(type, Primitive::kPrimLong);
2972 __ Dsubu(out, ZERO, dividend);
2973 }
2974 } else if (out != dividend) {
2975 __ Move(out, dividend);
2976 }
2977 }
2978}
2979
2980void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2981 DCHECK(instruction->IsDiv() || instruction->IsRem());
2982 Primitive::Type type = instruction->GetResultType();
2983
2984 LocationSummary* locations = instruction->GetLocations();
2985 Location second = locations->InAt(1);
2986 DCHECK(second.IsConstant());
2987
2988 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2989 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2990 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002991 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002992 int ctz_imm = CTZ(abs_imm);
2993
2994 if (instruction->IsDiv()) {
2995 if (type == Primitive::kPrimInt) {
2996 if (ctz_imm == 1) {
2997 // Fast path for division by +/-2, which is very common.
2998 __ Srl(TMP, dividend, 31);
2999 } else {
3000 __ Sra(TMP, dividend, 31);
3001 __ Srl(TMP, TMP, 32 - ctz_imm);
3002 }
3003 __ Addu(out, dividend, TMP);
3004 __ Sra(out, out, ctz_imm);
3005 if (imm < 0) {
3006 __ Subu(out, ZERO, out);
3007 }
3008 } else {
3009 DCHECK_EQ(type, Primitive::kPrimLong);
3010 if (ctz_imm == 1) {
3011 // Fast path for division by +/-2, which is very common.
3012 __ Dsrl32(TMP, dividend, 31);
3013 } else {
3014 __ Dsra32(TMP, dividend, 31);
3015 if (ctz_imm > 32) {
3016 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3017 } else {
3018 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3019 }
3020 }
3021 __ Daddu(out, dividend, TMP);
3022 if (ctz_imm < 32) {
3023 __ Dsra(out, out, ctz_imm);
3024 } else {
3025 __ Dsra32(out, out, ctz_imm - 32);
3026 }
3027 if (imm < 0) {
3028 __ Dsubu(out, ZERO, out);
3029 }
3030 }
3031 } else {
3032 if (type == Primitive::kPrimInt) {
3033 if (ctz_imm == 1) {
3034 // Fast path for modulo +/-2, which is very common.
3035 __ Sra(TMP, dividend, 31);
3036 __ Subu(out, dividend, TMP);
3037 __ Andi(out, out, 1);
3038 __ Addu(out, out, TMP);
3039 } else {
3040 __ Sra(TMP, dividend, 31);
3041 __ Srl(TMP, TMP, 32 - ctz_imm);
3042 __ Addu(out, dividend, TMP);
3043 if (IsUint<16>(abs_imm - 1)) {
3044 __ Andi(out, out, abs_imm - 1);
3045 } else {
3046 __ Sll(out, out, 32 - ctz_imm);
3047 __ Srl(out, out, 32 - ctz_imm);
3048 }
3049 __ Subu(out, out, TMP);
3050 }
3051 } else {
3052 DCHECK_EQ(type, Primitive::kPrimLong);
3053 if (ctz_imm == 1) {
3054 // Fast path for modulo +/-2, which is very common.
3055 __ Dsra32(TMP, dividend, 31);
3056 __ Dsubu(out, dividend, TMP);
3057 __ Andi(out, out, 1);
3058 __ Daddu(out, out, TMP);
3059 } else {
3060 __ Dsra32(TMP, dividend, 31);
3061 if (ctz_imm > 32) {
3062 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3063 } else {
3064 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3065 }
3066 __ Daddu(out, dividend, TMP);
3067 if (IsUint<16>(abs_imm - 1)) {
3068 __ Andi(out, out, abs_imm - 1);
3069 } else {
3070 if (ctz_imm > 32) {
3071 __ Dsll(out, out, 64 - ctz_imm);
3072 __ Dsrl(out, out, 64 - ctz_imm);
3073 } else {
3074 __ Dsll32(out, out, 32 - ctz_imm);
3075 __ Dsrl32(out, out, 32 - ctz_imm);
3076 }
3077 }
3078 __ Dsubu(out, out, TMP);
3079 }
3080 }
3081 }
3082}
3083
3084void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3085 DCHECK(instruction->IsDiv() || instruction->IsRem());
3086
3087 LocationSummary* locations = instruction->GetLocations();
3088 Location second = locations->InAt(1);
3089 DCHECK(second.IsConstant());
3090
3091 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3092 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3093 int64_t imm = Int64FromConstant(second.GetConstant());
3094
3095 Primitive::Type type = instruction->GetResultType();
3096 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3097
3098 int64_t magic;
3099 int shift;
3100 CalculateMagicAndShiftForDivRem(imm,
3101 (type == Primitive::kPrimLong),
3102 &magic,
3103 &shift);
3104
3105 if (type == Primitive::kPrimInt) {
3106 __ LoadConst32(TMP, magic);
3107 __ MuhR6(TMP, dividend, TMP);
3108
3109 if (imm > 0 && magic < 0) {
3110 __ Addu(TMP, TMP, dividend);
3111 } else if (imm < 0 && magic > 0) {
3112 __ Subu(TMP, TMP, dividend);
3113 }
3114
3115 if (shift != 0) {
3116 __ Sra(TMP, TMP, shift);
3117 }
3118
3119 if (instruction->IsDiv()) {
3120 __ Sra(out, TMP, 31);
3121 __ Subu(out, TMP, out);
3122 } else {
3123 __ Sra(AT, TMP, 31);
3124 __ Subu(AT, TMP, AT);
3125 __ LoadConst32(TMP, imm);
3126 __ MulR6(TMP, AT, TMP);
3127 __ Subu(out, dividend, TMP);
3128 }
3129 } else {
3130 __ LoadConst64(TMP, magic);
3131 __ Dmuh(TMP, dividend, TMP);
3132
3133 if (imm > 0 && magic < 0) {
3134 __ Daddu(TMP, TMP, dividend);
3135 } else if (imm < 0 && magic > 0) {
3136 __ Dsubu(TMP, TMP, dividend);
3137 }
3138
3139 if (shift >= 32) {
3140 __ Dsra32(TMP, TMP, shift - 32);
3141 } else if (shift > 0) {
3142 __ Dsra(TMP, TMP, shift);
3143 }
3144
3145 if (instruction->IsDiv()) {
3146 __ Dsra32(out, TMP, 31);
3147 __ Dsubu(out, TMP, out);
3148 } else {
3149 __ Dsra32(AT, TMP, 31);
3150 __ Dsubu(AT, TMP, AT);
3151 __ LoadConst64(TMP, imm);
3152 __ Dmul(TMP, AT, TMP);
3153 __ Dsubu(out, dividend, TMP);
3154 }
3155 }
3156}
3157
3158void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3159 DCHECK(instruction->IsDiv() || instruction->IsRem());
3160 Primitive::Type type = instruction->GetResultType();
3161 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3162
3163 LocationSummary* locations = instruction->GetLocations();
3164 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3165 Location second = locations->InAt(1);
3166
3167 if (second.IsConstant()) {
3168 int64_t imm = Int64FromConstant(second.GetConstant());
3169 if (imm == 0) {
3170 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3171 } else if (imm == 1 || imm == -1) {
3172 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003173 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003174 DivRemByPowerOfTwo(instruction);
3175 } else {
3176 DCHECK(imm <= -2 || imm >= 2);
3177 GenerateDivRemWithAnyConstant(instruction);
3178 }
3179 } else {
3180 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3181 GpuRegister divisor = second.AsRegister<GpuRegister>();
3182 if (instruction->IsDiv()) {
3183 if (type == Primitive::kPrimInt)
3184 __ DivR6(out, dividend, divisor);
3185 else
3186 __ Ddiv(out, dividend, divisor);
3187 } else {
3188 if (type == Primitive::kPrimInt)
3189 __ ModR6(out, dividend, divisor);
3190 else
3191 __ Dmod(out, dividend, divisor);
3192 }
3193 }
3194}
3195
Alexey Frunze4dda3372015-06-01 18:31:49 -07003196void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3197 LocationSummary* locations =
3198 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3199 switch (div->GetResultType()) {
3200 case Primitive::kPrimInt:
3201 case Primitive::kPrimLong:
3202 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003203 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3205 break;
3206
3207 case Primitive::kPrimFloat:
3208 case Primitive::kPrimDouble:
3209 locations->SetInAt(0, Location::RequiresFpuRegister());
3210 locations->SetInAt(1, Location::RequiresFpuRegister());
3211 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3212 break;
3213
3214 default:
3215 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3216 }
3217}
3218
3219void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3220 Primitive::Type type = instruction->GetType();
3221 LocationSummary* locations = instruction->GetLocations();
3222
3223 switch (type) {
3224 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003225 case Primitive::kPrimLong:
3226 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003227 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003228 case Primitive::kPrimFloat:
3229 case Primitive::kPrimDouble: {
3230 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3231 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3232 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3233 if (type == Primitive::kPrimFloat)
3234 __ DivS(dst, lhs, rhs);
3235 else
3236 __ DivD(dst, lhs, rhs);
3237 break;
3238 }
3239 default:
3240 LOG(FATAL) << "Unexpected div type " << type;
3241 }
3242}
3243
3244void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003245 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003246 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003247}
3248
3249void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3250 SlowPathCodeMIPS64* slow_path =
3251 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3252 codegen_->AddSlowPath(slow_path);
3253 Location value = instruction->GetLocations()->InAt(0);
3254
3255 Primitive::Type type = instruction->GetType();
3256
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003257 if (!Primitive::IsIntegralType(type)) {
3258 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003259 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003260 }
3261
3262 if (value.IsConstant()) {
3263 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3264 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003265 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266 } else {
3267 // A division by a non-null constant is valid. We don't need to perform
3268 // any check, so simply fall through.
3269 }
3270 } else {
3271 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3272 }
3273}
3274
3275void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3278 locations->SetOut(Location::ConstantLocation(constant));
3279}
3280
3281void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3282 // Will be generated at use site.
3283}
3284
3285void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3286 exit->SetLocations(nullptr);
3287}
3288
3289void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3290}
3291
3292void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3293 LocationSummary* locations =
3294 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3295 locations->SetOut(Location::ConstantLocation(constant));
3296}
3297
3298void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3299 // Will be generated at use site.
3300}
3301
David Brazdilfc6a86a2015-06-26 10:33:45 +00003302void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003303 DCHECK(!successor->IsExitBlock());
3304 HBasicBlock* block = got->GetBlock();
3305 HInstruction* previous = got->GetPrevious();
3306 HLoopInformation* info = block->GetLoopInformation();
3307
3308 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3309 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3310 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3311 return;
3312 }
3313 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3314 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3315 }
3316 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003317 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003318 }
3319}
3320
David Brazdilfc6a86a2015-06-26 10:33:45 +00003321void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3322 got->SetLocations(nullptr);
3323}
3324
3325void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3326 HandleGoto(got, got->GetSuccessor());
3327}
3328
3329void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3330 try_boundary->SetLocations(nullptr);
3331}
3332
3333void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3334 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3335 if (!successor->IsExitBlock()) {
3336 HandleGoto(try_boundary, successor);
3337 }
3338}
3339
Alexey Frunze299a9392015-12-08 16:08:02 -08003340void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3341 bool is64bit,
3342 LocationSummary* locations) {
3343 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3344 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3345 Location rhs_location = locations->InAt(1);
3346 GpuRegister rhs_reg = ZERO;
3347 int64_t rhs_imm = 0;
3348 bool use_imm = rhs_location.IsConstant();
3349 if (use_imm) {
3350 if (is64bit) {
3351 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3352 } else {
3353 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3354 }
3355 } else {
3356 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3357 }
3358 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3359
3360 switch (cond) {
3361 case kCondEQ:
3362 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003363 if (use_imm && IsInt<16>(-rhs_imm)) {
3364 if (rhs_imm == 0) {
3365 if (cond == kCondEQ) {
3366 __ Sltiu(dst, lhs, 1);
3367 } else {
3368 __ Sltu(dst, ZERO, lhs);
3369 }
3370 } else {
3371 if (is64bit) {
3372 __ Daddiu(dst, lhs, -rhs_imm);
3373 } else {
3374 __ Addiu(dst, lhs, -rhs_imm);
3375 }
3376 if (cond == kCondEQ) {
3377 __ Sltiu(dst, dst, 1);
3378 } else {
3379 __ Sltu(dst, ZERO, dst);
3380 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003381 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003382 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003383 if (use_imm && IsUint<16>(rhs_imm)) {
3384 __ Xori(dst, lhs, rhs_imm);
3385 } else {
3386 if (use_imm) {
3387 rhs_reg = TMP;
3388 __ LoadConst64(rhs_reg, rhs_imm);
3389 }
3390 __ Xor(dst, lhs, rhs_reg);
3391 }
3392 if (cond == kCondEQ) {
3393 __ Sltiu(dst, dst, 1);
3394 } else {
3395 __ Sltu(dst, ZERO, dst);
3396 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003397 }
3398 break;
3399
3400 case kCondLT:
3401 case kCondGE:
3402 if (use_imm && IsInt<16>(rhs_imm)) {
3403 __ Slti(dst, lhs, rhs_imm);
3404 } else {
3405 if (use_imm) {
3406 rhs_reg = TMP;
3407 __ LoadConst64(rhs_reg, rhs_imm);
3408 }
3409 __ Slt(dst, lhs, rhs_reg);
3410 }
3411 if (cond == kCondGE) {
3412 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3413 // only the slt instruction but no sge.
3414 __ Xori(dst, dst, 1);
3415 }
3416 break;
3417
3418 case kCondLE:
3419 case kCondGT:
3420 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3421 // Simulate lhs <= rhs via lhs < rhs + 1.
3422 __ Slti(dst, lhs, rhs_imm_plus_one);
3423 if (cond == kCondGT) {
3424 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3425 // only the slti instruction but no sgti.
3426 __ Xori(dst, dst, 1);
3427 }
3428 } else {
3429 if (use_imm) {
3430 rhs_reg = TMP;
3431 __ LoadConst64(rhs_reg, rhs_imm);
3432 }
3433 __ Slt(dst, rhs_reg, lhs);
3434 if (cond == kCondLE) {
3435 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3436 // only the slt instruction but no sle.
3437 __ Xori(dst, dst, 1);
3438 }
3439 }
3440 break;
3441
3442 case kCondB:
3443 case kCondAE:
3444 if (use_imm && IsInt<16>(rhs_imm)) {
3445 // Sltiu sign-extends its 16-bit immediate operand before
3446 // the comparison and thus lets us compare directly with
3447 // unsigned values in the ranges [0, 0x7fff] and
3448 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3449 __ Sltiu(dst, lhs, rhs_imm);
3450 } else {
3451 if (use_imm) {
3452 rhs_reg = TMP;
3453 __ LoadConst64(rhs_reg, rhs_imm);
3454 }
3455 __ Sltu(dst, lhs, rhs_reg);
3456 }
3457 if (cond == kCondAE) {
3458 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3459 // only the sltu instruction but no sgeu.
3460 __ Xori(dst, dst, 1);
3461 }
3462 break;
3463
3464 case kCondBE:
3465 case kCondA:
3466 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3467 // Simulate lhs <= rhs via lhs < rhs + 1.
3468 // Note that this only works if rhs + 1 does not overflow
3469 // to 0, hence the check above.
3470 // Sltiu sign-extends its 16-bit immediate operand before
3471 // the comparison and thus lets us compare directly with
3472 // unsigned values in the ranges [0, 0x7fff] and
3473 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3474 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3475 if (cond == kCondA) {
3476 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3477 // only the sltiu instruction but no sgtiu.
3478 __ Xori(dst, dst, 1);
3479 }
3480 } else {
3481 if (use_imm) {
3482 rhs_reg = TMP;
3483 __ LoadConst64(rhs_reg, rhs_imm);
3484 }
3485 __ Sltu(dst, rhs_reg, lhs);
3486 if (cond == kCondBE) {
3487 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3488 // only the sltu instruction but no sleu.
3489 __ Xori(dst, dst, 1);
3490 }
3491 }
3492 break;
3493 }
3494}
3495
3496void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3497 bool is64bit,
3498 LocationSummary* locations,
3499 Mips64Label* label) {
3500 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3501 Location rhs_location = locations->InAt(1);
3502 GpuRegister rhs_reg = ZERO;
3503 int64_t rhs_imm = 0;
3504 bool use_imm = rhs_location.IsConstant();
3505 if (use_imm) {
3506 if (is64bit) {
3507 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3508 } else {
3509 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3510 }
3511 } else {
3512 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3513 }
3514
3515 if (use_imm && rhs_imm == 0) {
3516 switch (cond) {
3517 case kCondEQ:
3518 case kCondBE: // <= 0 if zero
3519 __ Beqzc(lhs, label);
3520 break;
3521 case kCondNE:
3522 case kCondA: // > 0 if non-zero
3523 __ Bnezc(lhs, label);
3524 break;
3525 case kCondLT:
3526 __ Bltzc(lhs, label);
3527 break;
3528 case kCondGE:
3529 __ Bgezc(lhs, label);
3530 break;
3531 case kCondLE:
3532 __ Blezc(lhs, label);
3533 break;
3534 case kCondGT:
3535 __ Bgtzc(lhs, label);
3536 break;
3537 case kCondB: // always false
3538 break;
3539 case kCondAE: // always true
3540 __ Bc(label);
3541 break;
3542 }
3543 } else {
3544 if (use_imm) {
3545 rhs_reg = TMP;
3546 __ LoadConst64(rhs_reg, rhs_imm);
3547 }
3548 switch (cond) {
3549 case kCondEQ:
3550 __ Beqc(lhs, rhs_reg, label);
3551 break;
3552 case kCondNE:
3553 __ Bnec(lhs, rhs_reg, label);
3554 break;
3555 case kCondLT:
3556 __ Bltc(lhs, rhs_reg, label);
3557 break;
3558 case kCondGE:
3559 __ Bgec(lhs, rhs_reg, label);
3560 break;
3561 case kCondLE:
3562 __ Bgec(rhs_reg, lhs, label);
3563 break;
3564 case kCondGT:
3565 __ Bltc(rhs_reg, lhs, label);
3566 break;
3567 case kCondB:
3568 __ Bltuc(lhs, rhs_reg, label);
3569 break;
3570 case kCondAE:
3571 __ Bgeuc(lhs, rhs_reg, label);
3572 break;
3573 case kCondBE:
3574 __ Bgeuc(rhs_reg, lhs, label);
3575 break;
3576 case kCondA:
3577 __ Bltuc(rhs_reg, lhs, label);
3578 break;
3579 }
3580 }
3581}
3582
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003583void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3584 bool gt_bias,
3585 Primitive::Type type,
3586 LocationSummary* locations) {
3587 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3588 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3589 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3590 if (type == Primitive::kPrimFloat) {
3591 switch (cond) {
3592 case kCondEQ:
3593 __ CmpEqS(FTMP, lhs, rhs);
3594 __ Mfc1(dst, FTMP);
3595 __ Andi(dst, dst, 1);
3596 break;
3597 case kCondNE:
3598 __ CmpEqS(FTMP, lhs, rhs);
3599 __ Mfc1(dst, FTMP);
3600 __ Addiu(dst, dst, 1);
3601 break;
3602 case kCondLT:
3603 if (gt_bias) {
3604 __ CmpLtS(FTMP, lhs, rhs);
3605 } else {
3606 __ CmpUltS(FTMP, lhs, rhs);
3607 }
3608 __ Mfc1(dst, FTMP);
3609 __ Andi(dst, dst, 1);
3610 break;
3611 case kCondLE:
3612 if (gt_bias) {
3613 __ CmpLeS(FTMP, lhs, rhs);
3614 } else {
3615 __ CmpUleS(FTMP, lhs, rhs);
3616 }
3617 __ Mfc1(dst, FTMP);
3618 __ Andi(dst, dst, 1);
3619 break;
3620 case kCondGT:
3621 if (gt_bias) {
3622 __ CmpUltS(FTMP, rhs, lhs);
3623 } else {
3624 __ CmpLtS(FTMP, rhs, lhs);
3625 }
3626 __ Mfc1(dst, FTMP);
3627 __ Andi(dst, dst, 1);
3628 break;
3629 case kCondGE:
3630 if (gt_bias) {
3631 __ CmpUleS(FTMP, rhs, lhs);
3632 } else {
3633 __ CmpLeS(FTMP, rhs, lhs);
3634 }
3635 __ Mfc1(dst, FTMP);
3636 __ Andi(dst, dst, 1);
3637 break;
3638 default:
3639 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3640 UNREACHABLE();
3641 }
3642 } else {
3643 DCHECK_EQ(type, Primitive::kPrimDouble);
3644 switch (cond) {
3645 case kCondEQ:
3646 __ CmpEqD(FTMP, lhs, rhs);
3647 __ Mfc1(dst, FTMP);
3648 __ Andi(dst, dst, 1);
3649 break;
3650 case kCondNE:
3651 __ CmpEqD(FTMP, lhs, rhs);
3652 __ Mfc1(dst, FTMP);
3653 __ Addiu(dst, dst, 1);
3654 break;
3655 case kCondLT:
3656 if (gt_bias) {
3657 __ CmpLtD(FTMP, lhs, rhs);
3658 } else {
3659 __ CmpUltD(FTMP, lhs, rhs);
3660 }
3661 __ Mfc1(dst, FTMP);
3662 __ Andi(dst, dst, 1);
3663 break;
3664 case kCondLE:
3665 if (gt_bias) {
3666 __ CmpLeD(FTMP, lhs, rhs);
3667 } else {
3668 __ CmpUleD(FTMP, lhs, rhs);
3669 }
3670 __ Mfc1(dst, FTMP);
3671 __ Andi(dst, dst, 1);
3672 break;
3673 case kCondGT:
3674 if (gt_bias) {
3675 __ CmpUltD(FTMP, rhs, lhs);
3676 } else {
3677 __ CmpLtD(FTMP, rhs, lhs);
3678 }
3679 __ Mfc1(dst, FTMP);
3680 __ Andi(dst, dst, 1);
3681 break;
3682 case kCondGE:
3683 if (gt_bias) {
3684 __ CmpUleD(FTMP, rhs, lhs);
3685 } else {
3686 __ CmpLeD(FTMP, rhs, lhs);
3687 }
3688 __ Mfc1(dst, FTMP);
3689 __ Andi(dst, dst, 1);
3690 break;
3691 default:
3692 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3693 UNREACHABLE();
3694 }
3695 }
3696}
3697
Alexey Frunze299a9392015-12-08 16:08:02 -08003698void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3699 bool gt_bias,
3700 Primitive::Type type,
3701 LocationSummary* locations,
3702 Mips64Label* label) {
3703 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3704 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3705 if (type == Primitive::kPrimFloat) {
3706 switch (cond) {
3707 case kCondEQ:
3708 __ CmpEqS(FTMP, lhs, rhs);
3709 __ Bc1nez(FTMP, label);
3710 break;
3711 case kCondNE:
3712 __ CmpEqS(FTMP, lhs, rhs);
3713 __ Bc1eqz(FTMP, label);
3714 break;
3715 case kCondLT:
3716 if (gt_bias) {
3717 __ CmpLtS(FTMP, lhs, rhs);
3718 } else {
3719 __ CmpUltS(FTMP, lhs, rhs);
3720 }
3721 __ Bc1nez(FTMP, label);
3722 break;
3723 case kCondLE:
3724 if (gt_bias) {
3725 __ CmpLeS(FTMP, lhs, rhs);
3726 } else {
3727 __ CmpUleS(FTMP, lhs, rhs);
3728 }
3729 __ Bc1nez(FTMP, label);
3730 break;
3731 case kCondGT:
3732 if (gt_bias) {
3733 __ CmpUltS(FTMP, rhs, lhs);
3734 } else {
3735 __ CmpLtS(FTMP, rhs, lhs);
3736 }
3737 __ Bc1nez(FTMP, label);
3738 break;
3739 case kCondGE:
3740 if (gt_bias) {
3741 __ CmpUleS(FTMP, rhs, lhs);
3742 } else {
3743 __ CmpLeS(FTMP, rhs, lhs);
3744 }
3745 __ Bc1nez(FTMP, label);
3746 break;
3747 default:
3748 LOG(FATAL) << "Unexpected non-floating-point condition";
3749 }
3750 } else {
3751 DCHECK_EQ(type, Primitive::kPrimDouble);
3752 switch (cond) {
3753 case kCondEQ:
3754 __ CmpEqD(FTMP, lhs, rhs);
3755 __ Bc1nez(FTMP, label);
3756 break;
3757 case kCondNE:
3758 __ CmpEqD(FTMP, lhs, rhs);
3759 __ Bc1eqz(FTMP, label);
3760 break;
3761 case kCondLT:
3762 if (gt_bias) {
3763 __ CmpLtD(FTMP, lhs, rhs);
3764 } else {
3765 __ CmpUltD(FTMP, lhs, rhs);
3766 }
3767 __ Bc1nez(FTMP, label);
3768 break;
3769 case kCondLE:
3770 if (gt_bias) {
3771 __ CmpLeD(FTMP, lhs, rhs);
3772 } else {
3773 __ CmpUleD(FTMP, lhs, rhs);
3774 }
3775 __ Bc1nez(FTMP, label);
3776 break;
3777 case kCondGT:
3778 if (gt_bias) {
3779 __ CmpUltD(FTMP, rhs, lhs);
3780 } else {
3781 __ CmpLtD(FTMP, rhs, lhs);
3782 }
3783 __ Bc1nez(FTMP, label);
3784 break;
3785 case kCondGE:
3786 if (gt_bias) {
3787 __ CmpUleD(FTMP, rhs, lhs);
3788 } else {
3789 __ CmpLeD(FTMP, rhs, lhs);
3790 }
3791 __ Bc1nez(FTMP, label);
3792 break;
3793 default:
3794 LOG(FATAL) << "Unexpected non-floating-point condition";
3795 }
3796 }
3797}
3798
Alexey Frunze4dda3372015-06-01 18:31:49 -07003799void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003800 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003801 Mips64Label* true_target,
3802 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003803 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003804
David Brazdil0debae72015-11-12 18:37:00 +00003805 if (true_target == nullptr && false_target == nullptr) {
3806 // Nothing to do. The code always falls through.
3807 return;
3808 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003809 // Constant condition, statically compared against "true" (integer value 1).
3810 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003811 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003812 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003813 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003814 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003815 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003816 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003817 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003818 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003819 }
David Brazdil0debae72015-11-12 18:37:00 +00003820 return;
3821 }
3822
3823 // The following code generates these patterns:
3824 // (1) true_target == nullptr && false_target != nullptr
3825 // - opposite condition true => branch to false_target
3826 // (2) true_target != nullptr && false_target == nullptr
3827 // - condition true => branch to true_target
3828 // (3) true_target != nullptr && false_target != nullptr
3829 // - condition true => branch to true_target
3830 // - branch to false_target
3831 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003832 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003833 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003834 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003835 if (true_target == nullptr) {
3836 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3837 } else {
3838 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3839 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003840 } else {
3841 // The condition instruction has not been materialized, use its inputs as
3842 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003843 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003844 Primitive::Type type = condition->InputAt(0)->GetType();
3845 LocationSummary* locations = cond->GetLocations();
3846 IfCondition if_cond = condition->GetCondition();
3847 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003848
David Brazdil0debae72015-11-12 18:37:00 +00003849 if (true_target == nullptr) {
3850 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003851 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003852 }
3853
Alexey Frunze299a9392015-12-08 16:08:02 -08003854 switch (type) {
3855 default:
3856 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3857 break;
3858 case Primitive::kPrimLong:
3859 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3860 break;
3861 case Primitive::kPrimFloat:
3862 case Primitive::kPrimDouble:
3863 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3864 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003865 }
3866 }
David Brazdil0debae72015-11-12 18:37:00 +00003867
3868 // If neither branch falls through (case 3), the conditional branch to `true_target`
3869 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3870 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003871 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003872 }
3873}
3874
3875void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003877 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003878 locations->SetInAt(0, Location::RequiresRegister());
3879 }
3880}
3881
3882void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003883 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3884 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003885 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003886 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003887 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003888 nullptr : codegen_->GetLabelOf(false_successor);
3889 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003890}
3891
3892void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3893 LocationSummary* locations = new (GetGraph()->GetArena())
3894 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003895 InvokeRuntimeCallingConvention calling_convention;
3896 RegisterSet caller_saves = RegisterSet::Empty();
3897 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3898 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003899 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003900 locations->SetInAt(0, Location::RequiresRegister());
3901 }
3902}
3903
3904void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003905 SlowPathCodeMIPS64* slow_path =
3906 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003907 GenerateTestAndBranch(deoptimize,
3908 /* condition_input_index */ 0,
3909 slow_path->GetEntryLabel(),
3910 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003911}
3912
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003913void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3914 LocationSummary* locations = new (GetGraph()->GetArena())
3915 LocationSummary(flag, LocationSummary::kNoCall);
3916 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003917}
3918
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003919void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3920 __ LoadFromOffset(kLoadWord,
3921 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3922 SP,
3923 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003924}
3925
David Brazdil74eb1b22015-12-14 11:44:01 +00003926void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3928 if (Primitive::IsFloatingPointType(select->GetType())) {
3929 locations->SetInAt(0, Location::RequiresFpuRegister());
3930 locations->SetInAt(1, Location::RequiresFpuRegister());
3931 } else {
3932 locations->SetInAt(0, Location::RequiresRegister());
3933 locations->SetInAt(1, Location::RequiresRegister());
3934 }
3935 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3936 locations->SetInAt(2, Location::RequiresRegister());
3937 }
3938 locations->SetOut(Location::SameAsFirstInput());
3939}
3940
3941void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3942 LocationSummary* locations = select->GetLocations();
3943 Mips64Label false_target;
3944 GenerateTestAndBranch(select,
3945 /* condition_input_index */ 2,
3946 /* true_target */ nullptr,
3947 &false_target);
3948 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3949 __ Bind(&false_target);
3950}
3951
David Srbecky0cf44932015-12-09 14:09:59 +00003952void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3953 new (GetGraph()->GetArena()) LocationSummary(info);
3954}
3955
David Srbeckyd28f4a02016-03-14 17:14:24 +00003956void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3957 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003958}
3959
3960void CodeGeneratorMIPS64::GenerateNop() {
3961 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003962}
3963
Alexey Frunze4dda3372015-06-01 18:31:49 -07003964void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003965 const FieldInfo& field_info) {
3966 Primitive::Type field_type = field_info.GetFieldType();
3967 bool object_field_get_with_read_barrier =
3968 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3970 instruction,
3971 object_field_get_with_read_barrier
3972 ? LocationSummary::kCallOnSlowPath
3973 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07003974 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3975 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3976 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003977 locations->SetInAt(0, Location::RequiresRegister());
3978 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3979 locations->SetOut(Location::RequiresFpuRegister());
3980 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08003981 // The output overlaps in the case of an object field get with
3982 // read barriers enabled: we do not want the move to overwrite the
3983 // object's location, as we need it to emit the read barrier.
3984 locations->SetOut(Location::RequiresRegister(),
3985 object_field_get_with_read_barrier
3986 ? Location::kOutputOverlap
3987 : Location::kNoOutputOverlap);
3988 }
3989 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3990 // We need a temporary register for the read barrier marking slow
3991 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
3992 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003993 }
3994}
3995
3996void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3997 const FieldInfo& field_info) {
3998 Primitive::Type type = field_info.GetFieldType();
3999 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004000 Location obj_loc = locations->InAt(0);
4001 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4002 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004004 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004005 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004006 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4007
Alexey Frunze4dda3372015-06-01 18:31:49 -07004008 switch (type) {
4009 case Primitive::kPrimBoolean:
4010 load_type = kLoadUnsignedByte;
4011 break;
4012 case Primitive::kPrimByte:
4013 load_type = kLoadSignedByte;
4014 break;
4015 case Primitive::kPrimShort:
4016 load_type = kLoadSignedHalfword;
4017 break;
4018 case Primitive::kPrimChar:
4019 load_type = kLoadUnsignedHalfword;
4020 break;
4021 case Primitive::kPrimInt:
4022 case Primitive::kPrimFloat:
4023 load_type = kLoadWord;
4024 break;
4025 case Primitive::kPrimLong:
4026 case Primitive::kPrimDouble:
4027 load_type = kLoadDoubleword;
4028 break;
4029 case Primitive::kPrimNot:
4030 load_type = kLoadUnsignedWord;
4031 break;
4032 case Primitive::kPrimVoid:
4033 LOG(FATAL) << "Unreachable type " << type;
4034 UNREACHABLE();
4035 }
4036 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004037 DCHECK(dst_loc.IsRegister());
4038 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4039 if (type == Primitive::kPrimNot) {
4040 // /* HeapReference<Object> */ dst = *(obj + offset)
4041 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4042 Location temp_loc = locations->GetTemp(0);
4043 // Note that a potential implicit null check is handled in this
4044 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4045 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4046 dst_loc,
4047 obj,
4048 offset,
4049 temp_loc,
4050 /* needs_null_check */ true);
4051 if (is_volatile) {
4052 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4053 }
4054 } else {
4055 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4056 if (is_volatile) {
4057 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4058 }
4059 // If read barriers are enabled, emit read barriers other than
4060 // Baker's using a slow path (and also unpoison the loaded
4061 // reference, if heap poisoning is enabled).
4062 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4063 }
4064 } else {
4065 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4066 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004068 DCHECK(dst_loc.IsFpuRegister());
4069 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004070 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004071 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004072
Alexey Frunze15958152017-02-09 19:08:30 -08004073 // Memory barriers, in the case of references, are handled in the
4074 // previous switch statement.
4075 if (is_volatile && (type != Primitive::kPrimNot)) {
4076 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004077 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004078}
4079
4080void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4081 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4082 LocationSummary* locations =
4083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4084 locations->SetInAt(0, Location::RequiresRegister());
4085 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004086 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004087 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004088 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004089 }
4090}
4091
4092void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004093 const FieldInfo& field_info,
4094 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004095 Primitive::Type type = field_info.GetFieldType();
4096 LocationSummary* locations = instruction->GetLocations();
4097 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004098 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004100 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004101 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4102 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004103 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4104
Alexey Frunze4dda3372015-06-01 18:31:49 -07004105 switch (type) {
4106 case Primitive::kPrimBoolean:
4107 case Primitive::kPrimByte:
4108 store_type = kStoreByte;
4109 break;
4110 case Primitive::kPrimShort:
4111 case Primitive::kPrimChar:
4112 store_type = kStoreHalfword;
4113 break;
4114 case Primitive::kPrimInt:
4115 case Primitive::kPrimFloat:
4116 case Primitive::kPrimNot:
4117 store_type = kStoreWord;
4118 break;
4119 case Primitive::kPrimLong:
4120 case Primitive::kPrimDouble:
4121 store_type = kStoreDoubleword;
4122 break;
4123 case Primitive::kPrimVoid:
4124 LOG(FATAL) << "Unreachable type " << type;
4125 UNREACHABLE();
4126 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127
Alexey Frunze15958152017-02-09 19:08:30 -08004128 if (is_volatile) {
4129 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4130 }
4131
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004132 if (value_location.IsConstant()) {
4133 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4134 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4135 } else {
4136 if (!Primitive::IsFloatingPointType(type)) {
4137 DCHECK(value_location.IsRegister());
4138 GpuRegister src = value_location.AsRegister<GpuRegister>();
4139 if (kPoisonHeapReferences && needs_write_barrier) {
4140 // Note that in the case where `value` is a null reference,
4141 // we do not enter this block, as a null reference does not
4142 // need poisoning.
4143 DCHECK_EQ(type, Primitive::kPrimNot);
4144 __ PoisonHeapReference(TMP, src);
4145 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4146 } else {
4147 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4148 }
4149 } else {
4150 DCHECK(value_location.IsFpuRegister());
4151 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4152 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4153 }
4154 }
Alexey Frunze15958152017-02-09 19:08:30 -08004155
Alexey Frunzec061de12017-02-14 13:27:23 -08004156 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004157 DCHECK(value_location.IsRegister());
4158 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004159 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004160 }
Alexey Frunze15958152017-02-09 19:08:30 -08004161
4162 if (is_volatile) {
4163 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4164 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004165}
4166
4167void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4168 HandleFieldGet(instruction, instruction->GetFieldInfo());
4169}
4170
4171void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4172 HandleFieldGet(instruction, instruction->GetFieldInfo());
4173}
4174
4175void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4176 HandleFieldSet(instruction, instruction->GetFieldInfo());
4177}
4178
4179void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004180 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004181}
4182
Alexey Frunze15958152017-02-09 19:08:30 -08004183void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4184 HInstruction* instruction,
4185 Location out,
4186 uint32_t offset,
4187 Location maybe_temp,
4188 ReadBarrierOption read_barrier_option) {
4189 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4190 if (read_barrier_option == kWithReadBarrier) {
4191 CHECK(kEmitCompilerReadBarrier);
4192 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4193 if (kUseBakerReadBarrier) {
4194 // Load with fast path based Baker's read barrier.
4195 // /* HeapReference<Object> */ out = *(out + offset)
4196 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4197 out,
4198 out_reg,
4199 offset,
4200 maybe_temp,
4201 /* needs_null_check */ false);
4202 } else {
4203 // Load with slow path based read barrier.
4204 // Save the value of `out` into `maybe_temp` before overwriting it
4205 // in the following move operation, as we will need it for the
4206 // read barrier below.
4207 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4208 // /* HeapReference<Object> */ out = *(out + offset)
4209 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4210 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4211 }
4212 } else {
4213 // Plain load with no read barrier.
4214 // /* HeapReference<Object> */ out = *(out + offset)
4215 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4216 __ MaybeUnpoisonHeapReference(out_reg);
4217 }
4218}
4219
4220void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4221 HInstruction* instruction,
4222 Location out,
4223 Location obj,
4224 uint32_t offset,
4225 Location maybe_temp,
4226 ReadBarrierOption read_barrier_option) {
4227 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4228 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4229 if (read_barrier_option == kWithReadBarrier) {
4230 CHECK(kEmitCompilerReadBarrier);
4231 if (kUseBakerReadBarrier) {
4232 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4233 // Load with fast path based Baker's read barrier.
4234 // /* HeapReference<Object> */ out = *(obj + offset)
4235 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4236 out,
4237 obj_reg,
4238 offset,
4239 maybe_temp,
4240 /* needs_null_check */ false);
4241 } else {
4242 // Load with slow path based read barrier.
4243 // /* HeapReference<Object> */ out = *(obj + offset)
4244 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4245 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4246 }
4247 } else {
4248 // Plain load with no read barrier.
4249 // /* HeapReference<Object> */ out = *(obj + offset)
4250 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4251 __ MaybeUnpoisonHeapReference(out_reg);
4252 }
4253}
4254
Alexey Frunzef63f5692016-12-13 17:43:11 -08004255void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004256 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004257 Location root,
4258 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004259 uint32_t offset,
4260 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004261 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004262 if (read_barrier_option == kWithReadBarrier) {
4263 DCHECK(kEmitCompilerReadBarrier);
4264 if (kUseBakerReadBarrier) {
4265 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4266 // Baker's read barrier are used:
4267 //
4268 // root = obj.field;
4269 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4270 // if (temp != null) {
4271 // root = temp(root)
4272 // }
4273
4274 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4275 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4276 static_assert(
4277 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4278 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4279 "have different sizes.");
4280 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4281 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4282 "have different sizes.");
4283
4284 // Slow path marking the GC root `root`.
4285 Location temp = Location::RegisterLocation(T9);
4286 SlowPathCodeMIPS64* slow_path =
4287 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4288 instruction,
4289 root,
4290 /*entrypoint*/ temp);
4291 codegen_->AddSlowPath(slow_path);
4292
4293 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4294 const int32_t entry_point_offset =
4295 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4296 // Loading the entrypoint does not require a load acquire since it is only changed when
4297 // threads are suspended or running a checkpoint.
4298 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4299 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4300 // checking GetIsGcMarking.
4301 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4302 __ Bind(slow_path->GetExitLabel());
4303 } else {
4304 // GC root loaded through a slow path for read barriers other
4305 // than Baker's.
4306 // /* GcRoot<mirror::Object>* */ root = obj + offset
4307 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4308 // /* mirror::Object* */ root = root->Read()
4309 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4310 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004311 } else {
4312 // Plain GC root load with no read barrier.
4313 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4314 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4315 // Note that GC roots are not affected by heap poisoning, thus we
4316 // do not have to unpoison `root_reg` here.
4317 }
4318}
4319
Alexey Frunze15958152017-02-09 19:08:30 -08004320void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4321 Location ref,
4322 GpuRegister obj,
4323 uint32_t offset,
4324 Location temp,
4325 bool needs_null_check) {
4326 DCHECK(kEmitCompilerReadBarrier);
4327 DCHECK(kUseBakerReadBarrier);
4328
4329 // /* HeapReference<Object> */ ref = *(obj + offset)
4330 Location no_index = Location::NoLocation();
4331 ScaleFactor no_scale_factor = TIMES_1;
4332 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4333 ref,
4334 obj,
4335 offset,
4336 no_index,
4337 no_scale_factor,
4338 temp,
4339 needs_null_check);
4340}
4341
4342void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4343 Location ref,
4344 GpuRegister obj,
4345 uint32_t data_offset,
4346 Location index,
4347 Location temp,
4348 bool needs_null_check) {
4349 DCHECK(kEmitCompilerReadBarrier);
4350 DCHECK(kUseBakerReadBarrier);
4351
4352 static_assert(
4353 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4354 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4355 // /* HeapReference<Object> */ ref =
4356 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4357 ScaleFactor scale_factor = TIMES_4;
4358 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4359 ref,
4360 obj,
4361 data_offset,
4362 index,
4363 scale_factor,
4364 temp,
4365 needs_null_check);
4366}
4367
4368void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4369 Location ref,
4370 GpuRegister obj,
4371 uint32_t offset,
4372 Location index,
4373 ScaleFactor scale_factor,
4374 Location temp,
4375 bool needs_null_check,
4376 bool always_update_field) {
4377 DCHECK(kEmitCompilerReadBarrier);
4378 DCHECK(kUseBakerReadBarrier);
4379
4380 // In slow path based read barriers, the read barrier call is
4381 // inserted after the original load. However, in fast path based
4382 // Baker's read barriers, we need to perform the load of
4383 // mirror::Object::monitor_ *before* the original reference load.
4384 // This load-load ordering is required by the read barrier.
4385 // The fast path/slow path (for Baker's algorithm) should look like:
4386 //
4387 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4388 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4389 // HeapReference<Object> ref = *src; // Original reference load.
4390 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4391 // if (is_gray) {
4392 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4393 // }
4394 //
4395 // Note: the original implementation in ReadBarrier::Barrier is
4396 // slightly more complex as it performs additional checks that we do
4397 // not do here for performance reasons.
4398
4399 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4400 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4401 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4402
4403 // /* int32_t */ monitor = obj->monitor_
4404 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4405 if (needs_null_check) {
4406 MaybeRecordImplicitNullCheck(instruction);
4407 }
4408 // /* LockWord */ lock_word = LockWord(monitor)
4409 static_assert(sizeof(LockWord) == sizeof(int32_t),
4410 "art::LockWord and int32_t have different sizes.");
4411
4412 __ Sync(0); // Barrier to prevent load-load reordering.
4413
4414 // The actual reference load.
4415 if (index.IsValid()) {
4416 // Load types involving an "index": ArrayGet,
4417 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4418 // intrinsics.
4419 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4420 if (index.IsConstant()) {
4421 size_t computed_offset =
4422 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4423 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4424 } else {
4425 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004426 if (scale_factor == TIMES_1) {
4427 __ Daddu(TMP, index_reg, obj);
4428 } else {
4429 __ Dlsa(TMP, index_reg, obj, scale_factor);
4430 }
Alexey Frunze15958152017-02-09 19:08:30 -08004431 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4432 }
4433 } else {
4434 // /* HeapReference<Object> */ ref = *(obj + offset)
4435 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4436 }
4437
4438 // Object* ref = ref_addr->AsMirrorPtr()
4439 __ MaybeUnpoisonHeapReference(ref_reg);
4440
4441 // Slow path marking the object `ref` when it is gray.
4442 SlowPathCodeMIPS64* slow_path;
4443 if (always_update_field) {
4444 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4445 // of the form `obj + field_offset`, where `obj` is a register and
4446 // `field_offset` is a register. Thus `offset` and `scale_factor`
4447 // above are expected to be null in this code path.
4448 DCHECK_EQ(offset, 0u);
4449 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4450 slow_path = new (GetGraph()->GetArena())
4451 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4452 ref,
4453 obj,
4454 /* field_offset */ index,
4455 temp_reg);
4456 } else {
4457 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4458 }
4459 AddSlowPath(slow_path);
4460
4461 // if (rb_state == ReadBarrier::GrayState())
4462 // ref = ReadBarrier::Mark(ref);
4463 // Given the numeric representation, it's enough to check the low bit of the
4464 // rb_state. We do that by shifting the bit into the sign bit (31) and
4465 // performing a branch on less than zero.
4466 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4467 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4468 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4469 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4470 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4471 __ Bind(slow_path->GetExitLabel());
4472}
4473
4474void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4475 Location out,
4476 Location ref,
4477 Location obj,
4478 uint32_t offset,
4479 Location index) {
4480 DCHECK(kEmitCompilerReadBarrier);
4481
4482 // Insert a slow path based read barrier *after* the reference load.
4483 //
4484 // If heap poisoning is enabled, the unpoisoning of the loaded
4485 // reference will be carried out by the runtime within the slow
4486 // path.
4487 //
4488 // Note that `ref` currently does not get unpoisoned (when heap
4489 // poisoning is enabled), which is alright as the `ref` argument is
4490 // not used by the artReadBarrierSlow entry point.
4491 //
4492 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4493 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4494 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4495 AddSlowPath(slow_path);
4496
4497 __ Bc(slow_path->GetEntryLabel());
4498 __ Bind(slow_path->GetExitLabel());
4499}
4500
4501void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4502 Location out,
4503 Location ref,
4504 Location obj,
4505 uint32_t offset,
4506 Location index) {
4507 if (kEmitCompilerReadBarrier) {
4508 // Baker's read barriers shall be handled by the fast path
4509 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4510 DCHECK(!kUseBakerReadBarrier);
4511 // If heap poisoning is enabled, unpoisoning will be taken care of
4512 // by the runtime within the slow path.
4513 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4514 } else if (kPoisonHeapReferences) {
4515 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4516 }
4517}
4518
4519void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4520 Location out,
4521 Location root) {
4522 DCHECK(kEmitCompilerReadBarrier);
4523
4524 // Insert a slow path based read barrier *after* the GC root load.
4525 //
4526 // Note that GC roots are not affected by heap poisoning, so we do
4527 // not need to do anything special for this here.
4528 SlowPathCodeMIPS64* slow_path =
4529 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4530 AddSlowPath(slow_path);
4531
4532 __ Bc(slow_path->GetEntryLabel());
4533 __ Bind(slow_path->GetExitLabel());
4534}
4535
Alexey Frunze4dda3372015-06-01 18:31:49 -07004536void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004537 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4538 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004539 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004540 switch (type_check_kind) {
4541 case TypeCheckKind::kExactCheck:
4542 case TypeCheckKind::kAbstractClassCheck:
4543 case TypeCheckKind::kClassHierarchyCheck:
4544 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004545 call_kind =
4546 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004547 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004548 break;
4549 case TypeCheckKind::kArrayCheck:
4550 case TypeCheckKind::kUnresolvedCheck:
4551 case TypeCheckKind::kInterfaceCheck:
4552 call_kind = LocationSummary::kCallOnSlowPath;
4553 break;
4554 }
4555
Alexey Frunze4dda3372015-06-01 18:31:49 -07004556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004557 if (baker_read_barrier_slow_path) {
4558 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4559 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004560 locations->SetInAt(0, Location::RequiresRegister());
4561 locations->SetInAt(1, Location::RequiresRegister());
4562 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004563 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004564 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004565 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004566}
4567
4568void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004569 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004570 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004571 Location obj_loc = locations->InAt(0);
4572 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004573 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004574 Location out_loc = locations->Out();
4575 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4576 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4577 DCHECK_LE(num_temps, 1u);
4578 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004579 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4580 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4581 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4582 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004583 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004584 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004585
4586 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004587 // Avoid this check if we know `obj` is not null.
4588 if (instruction->MustDoNullCheck()) {
4589 __ Move(out, ZERO);
4590 __ Beqzc(obj, &done);
4591 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004592
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004593 switch (type_check_kind) {
4594 case TypeCheckKind::kExactCheck: {
4595 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004596 GenerateReferenceLoadTwoRegisters(instruction,
4597 out_loc,
4598 obj_loc,
4599 class_offset,
4600 maybe_temp_loc,
4601 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004602 // Classes must be equal for the instanceof to succeed.
4603 __ Xor(out, out, cls);
4604 __ Sltiu(out, out, 1);
4605 break;
4606 }
4607
4608 case TypeCheckKind::kAbstractClassCheck: {
4609 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004610 GenerateReferenceLoadTwoRegisters(instruction,
4611 out_loc,
4612 obj_loc,
4613 class_offset,
4614 maybe_temp_loc,
4615 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004616 // If the class is abstract, we eagerly fetch the super class of the
4617 // object to avoid doing a comparison we know will fail.
4618 Mips64Label loop;
4619 __ Bind(&loop);
4620 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004621 GenerateReferenceLoadOneRegister(instruction,
4622 out_loc,
4623 super_offset,
4624 maybe_temp_loc,
4625 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004626 // If `out` is null, we use it for the result, and jump to `done`.
4627 __ Beqzc(out, &done);
4628 __ Bnec(out, cls, &loop);
4629 __ LoadConst32(out, 1);
4630 break;
4631 }
4632
4633 case TypeCheckKind::kClassHierarchyCheck: {
4634 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004635 GenerateReferenceLoadTwoRegisters(instruction,
4636 out_loc,
4637 obj_loc,
4638 class_offset,
4639 maybe_temp_loc,
4640 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004641 // Walk over the class hierarchy to find a match.
4642 Mips64Label loop, success;
4643 __ Bind(&loop);
4644 __ Beqc(out, cls, &success);
4645 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004646 GenerateReferenceLoadOneRegister(instruction,
4647 out_loc,
4648 super_offset,
4649 maybe_temp_loc,
4650 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004651 __ Bnezc(out, &loop);
4652 // If `out` is null, we use it for the result, and jump to `done`.
4653 __ Bc(&done);
4654 __ Bind(&success);
4655 __ LoadConst32(out, 1);
4656 break;
4657 }
4658
4659 case TypeCheckKind::kArrayObjectCheck: {
4660 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004661 GenerateReferenceLoadTwoRegisters(instruction,
4662 out_loc,
4663 obj_loc,
4664 class_offset,
4665 maybe_temp_loc,
4666 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004667 // Do an exact check.
4668 Mips64Label success;
4669 __ Beqc(out, cls, &success);
4670 // Otherwise, we need to check that the object's class is a non-primitive array.
4671 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004672 GenerateReferenceLoadOneRegister(instruction,
4673 out_loc,
4674 component_offset,
4675 maybe_temp_loc,
4676 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004677 // If `out` is null, we use it for the result, and jump to `done`.
4678 __ Beqzc(out, &done);
4679 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4680 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4681 __ Sltiu(out, out, 1);
4682 __ Bc(&done);
4683 __ Bind(&success);
4684 __ LoadConst32(out, 1);
4685 break;
4686 }
4687
4688 case TypeCheckKind::kArrayCheck: {
4689 // No read barrier since the slow path will retry upon failure.
4690 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004691 GenerateReferenceLoadTwoRegisters(instruction,
4692 out_loc,
4693 obj_loc,
4694 class_offset,
4695 maybe_temp_loc,
4696 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004697 DCHECK(locations->OnlyCallsOnSlowPath());
4698 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4699 /* is_fatal */ false);
4700 codegen_->AddSlowPath(slow_path);
4701 __ Bnec(out, cls, slow_path->GetEntryLabel());
4702 __ LoadConst32(out, 1);
4703 break;
4704 }
4705
4706 case TypeCheckKind::kUnresolvedCheck:
4707 case TypeCheckKind::kInterfaceCheck: {
4708 // Note that we indeed only call on slow path, but we always go
4709 // into the slow path for the unresolved and interface check
4710 // cases.
4711 //
4712 // We cannot directly call the InstanceofNonTrivial runtime
4713 // entry point without resorting to a type checking slow path
4714 // here (i.e. by calling InvokeRuntime directly), as it would
4715 // require to assign fixed registers for the inputs of this
4716 // HInstanceOf instruction (following the runtime calling
4717 // convention), which might be cluttered by the potential first
4718 // read barrier emission at the beginning of this method.
4719 //
4720 // TODO: Introduce a new runtime entry point taking the object
4721 // to test (instead of its class) as argument, and let it deal
4722 // with the read barrier issues. This will let us refactor this
4723 // case of the `switch` code as it was previously (with a direct
4724 // call to the runtime not using a type checking slow path).
4725 // This should also be beneficial for the other cases above.
4726 DCHECK(locations->OnlyCallsOnSlowPath());
4727 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4728 /* is_fatal */ false);
4729 codegen_->AddSlowPath(slow_path);
4730 __ Bc(slow_path->GetEntryLabel());
4731 break;
4732 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004733 }
4734
4735 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004736
4737 if (slow_path != nullptr) {
4738 __ Bind(slow_path->GetExitLabel());
4739 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004740}
4741
4742void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4744 locations->SetOut(Location::ConstantLocation(constant));
4745}
4746
4747void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4748 // Will be generated at use site.
4749}
4750
4751void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4753 locations->SetOut(Location::ConstantLocation(constant));
4754}
4755
4756void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4757 // Will be generated at use site.
4758}
4759
Calin Juravle175dc732015-08-25 15:42:32 +01004760void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4761 // The trampoline uses the same calling convention as dex calling conventions,
4762 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4763 // the method_idx.
4764 HandleInvoke(invoke);
4765}
4766
4767void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4768 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4769}
4770
Alexey Frunze4dda3372015-06-01 18:31:49 -07004771void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4772 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4773 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4774}
4775
4776void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4777 HandleInvoke(invoke);
4778 // The register T0 is required to be used for the hidden argument in
4779 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4780 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4781}
4782
4783void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4784 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4785 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 Location receiver = invoke->GetLocations()->InAt(0);
4787 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004788 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004789
4790 // Set the hidden argument.
4791 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4792 invoke->GetDexMethodIndex());
4793
4794 // temp = object->GetClass();
4795 if (receiver.IsStackSlot()) {
4796 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4797 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4798 } else {
4799 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4800 }
4801 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004802 // Instead of simply (possibly) unpoisoning `temp` here, we should
4803 // emit a read barrier for the previous class reference load.
4804 // However this is not required in practice, as this is an
4805 // intermediate/temporary reference and because the current
4806 // concurrent copying collector keeps the from-space memory
4807 // intact/accessible until the end of the marking phase (the
4808 // concurrent copying collector may not in the future).
4809 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004810 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4811 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4812 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004813 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004814 // temp = temp->GetImtEntryAt(method_offset);
4815 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4816 // T9 = temp->GetEntryPoint();
4817 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4818 // T9();
4819 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004820 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004821 DCHECK(!codegen_->IsLeafMethod());
4822 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4823}
4824
4825void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004826 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4827 if (intrinsic.TryDispatch(invoke)) {
4828 return;
4829 }
4830
Alexey Frunze4dda3372015-06-01 18:31:49 -07004831 HandleInvoke(invoke);
4832}
4833
4834void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004835 // Explicit clinit checks triggered by static invokes must have been pruned by
4836 // art::PrepareForRegisterAllocation.
4837 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838
Chris Larsen3039e382015-08-26 07:54:08 -07004839 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4840 if (intrinsic.TryDispatch(invoke)) {
4841 return;
4842 }
4843
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845}
4846
Orion Hodsonac141392017-01-13 11:53:47 +00004847void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4848 HandleInvoke(invoke);
4849}
4850
4851void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4852 codegen_->GenerateInvokePolymorphicCall(invoke);
4853}
4854
Chris Larsen3039e382015-08-26 07:54:08 -07004855static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004856 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004857 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4858 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859 return true;
4860 }
4861 return false;
4862}
4863
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004864HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004865 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004866 bool fallback_load = false;
4867 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004868 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004869 case HLoadString::LoadKind::kBssEntry:
4870 DCHECK(!Runtime::Current()->UseJitCompilation());
4871 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004872 case HLoadString::LoadKind::kJitTableAddress:
4873 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004874 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004875 case HLoadString::LoadKind::kBootImageAddress:
4876 case HLoadString::LoadKind::kDexCacheViaMethod:
4877 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004878 }
4879 if (fallback_load) {
4880 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4881 }
4882 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004883}
4884
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004885HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4886 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004887 bool fallback_load = false;
4888 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004889 case HLoadClass::LoadKind::kInvalid:
4890 LOG(FATAL) << "UNREACHABLE";
4891 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004892 case HLoadClass::LoadKind::kReferrersClass:
4893 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004894 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004895 case HLoadClass::LoadKind::kBssEntry:
4896 DCHECK(!Runtime::Current()->UseJitCompilation());
4897 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004898 case HLoadClass::LoadKind::kJitTableAddress:
4899 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004900 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004901 case HLoadClass::LoadKind::kBootImageAddress:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004902 case HLoadClass::LoadKind::kDexCacheViaMethod:
4903 break;
4904 }
4905 if (fallback_load) {
4906 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4907 }
4908 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004909}
4910
Vladimir Markodc151b22015-10-15 18:02:30 +01004911HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4912 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004913 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004914 // On MIPS64 we support all dispatch types.
4915 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004916}
4917
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004918void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
4919 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004920 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004921 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004922 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4923 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4924
Alexey Frunze19f6c692016-11-30 19:19:55 -08004925 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004926 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004927 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004928 uint32_t offset =
4929 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004930 __ LoadFromOffset(kLoadDoubleword,
4931 temp.AsRegister<GpuRegister>(),
4932 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004933 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004934 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004935 }
Vladimir Marko58155012015-08-19 12:49:41 +00004936 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004937 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004938 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004939 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4940 DCHECK(GetCompilerOptions().IsBootImage());
4941 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
4942 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4943 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4944 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4945 break;
4946 }
Vladimir Marko58155012015-08-19 12:49:41 +00004947 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004948 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4949 kLoadDoubleword,
4950 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004951 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004952 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4953 uint32_t offset = invoke->GetDexCacheArrayOffset();
4954 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004955 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004956 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4957 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4958 break;
4959 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004960 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4961 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4962 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004963 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004964 }
4965
Alexey Frunze19f6c692016-11-30 19:19:55 -08004966 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00004967 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004968 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00004969 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004970 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4971 // T9 = callee_method->entry_point_from_quick_compiled_code_;
4972 __ LoadFromOffset(kLoadDoubleword,
4973 T9,
4974 callee_method.AsRegister<GpuRegister>(),
4975 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004976 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004977 // T9()
4978 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004979 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00004980 break;
4981 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004982 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4983
Alexey Frunze4dda3372015-06-01 18:31:49 -07004984 DCHECK(!IsLeafMethod());
4985}
4986
4987void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004988 // Explicit clinit checks triggered by static invokes must have been pruned by
4989 // art::PrepareForRegisterAllocation.
4990 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004991
4992 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4993 return;
4994 }
4995
4996 LocationSummary* locations = invoke->GetLocations();
4997 codegen_->GenerateStaticOrDirectCall(invoke,
4998 locations->HasTemps()
4999 ? locations->GetTemp(0)
5000 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005001}
5002
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005003void CodeGeneratorMIPS64::GenerateVirtualCall(
5004 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005005 // Use the calling convention instead of the location of the receiver, as
5006 // intrinsics may have put the receiver in a different register. In the intrinsics
5007 // slow path, the arguments have been moved to the right place, so here we are
5008 // guaranteed that the receiver is the first register of the calling convention.
5009 InvokeDexCallingConvention calling_convention;
5010 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5011
Alexey Frunze53afca12015-11-05 16:34:23 -08005012 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005013 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5014 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5015 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005016 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005017
5018 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005019 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005020 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005021 // Instead of simply (possibly) unpoisoning `temp` here, we should
5022 // emit a read barrier for the previous class reference load.
5023 // However this is not required in practice, as this is an
5024 // intermediate/temporary reference and because the current
5025 // concurrent copying collector keeps the from-space memory
5026 // intact/accessible until the end of the marking phase (the
5027 // concurrent copying collector may not in the future).
5028 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005029 // temp = temp->GetMethodAt(method_offset);
5030 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5031 // T9 = temp->GetEntryPoint();
5032 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5033 // T9();
5034 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005035 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005036 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005037}
5038
5039void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5040 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5041 return;
5042 }
5043
5044 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005045 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005046}
5047
5048void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005049 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5050 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005051 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005052 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5053 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005054 return;
5055 }
Vladimir Marko41559982017-01-06 14:04:23 +00005056 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005057
Alexey Frunze15958152017-02-09 19:08:30 -08005058 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5059 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005060 ? LocationSummary::kCallOnSlowPath
5061 : LocationSummary::kNoCall;
5062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005063 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5064 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5065 }
Vladimir Marko41559982017-01-06 14:04:23 +00005066 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005067 locations->SetInAt(0, Location::RequiresRegister());
5068 }
5069 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005070 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5071 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5072 // Rely on the type resolution or initialization and marking to save everything we need.
5073 RegisterSet caller_saves = RegisterSet::Empty();
5074 InvokeRuntimeCallingConvention calling_convention;
5075 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5076 locations->SetCustomSlowPathCallerSaves(caller_saves);
5077 } else {
5078 // For non-Baker read barrier we have a temp-clobbering call.
5079 }
5080 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005081}
5082
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005083// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5084// move.
5085void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005086 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5087 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5088 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005089 return;
5090 }
Vladimir Marko41559982017-01-06 14:04:23 +00005091 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005092
Vladimir Marko41559982017-01-06 14:04:23 +00005093 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005094 Location out_loc = locations->Out();
5095 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5096 GpuRegister current_method_reg = ZERO;
5097 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5098 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5099 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5100 }
5101
Alexey Frunze15958152017-02-09 19:08:30 -08005102 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5103 ? kWithoutReadBarrier
5104 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005105 bool generate_null_check = false;
5106 switch (load_kind) {
5107 case HLoadClass::LoadKind::kReferrersClass:
5108 DCHECK(!cls->CanCallRuntime());
5109 DCHECK(!cls->MustGenerateClinitCheck());
5110 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5111 GenerateGcRootFieldLoad(cls,
5112 out_loc,
5113 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005114 ArtMethod::DeclaringClassOffset().Int32Value(),
5115 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005116 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005117 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005118 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005119 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005120 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5121 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5122 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5123 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5124 break;
5125 }
5126 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005127 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005128 uint32_t address = dchecked_integral_cast<uint32_t>(
5129 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5130 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005131 __ LoadLiteral(out,
5132 kLoadUnsignedWord,
5133 codegen_->DeduplicateBootImageAddressLiteral(address));
5134 break;
5135 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005137 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005138 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005139 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005140 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005141 generate_null_check = true;
5142 break;
5143 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005144 case HLoadClass::LoadKind::kJitTableAddress:
5145 __ LoadLiteral(out,
5146 kLoadUnsignedWord,
5147 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5148 cls->GetTypeIndex(),
5149 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005150 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005151 break;
Vladimir Marko41559982017-01-06 14:04:23 +00005152 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005153 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005154 LOG(FATAL) << "UNREACHABLE";
5155 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005156 }
5157
5158 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5159 DCHECK(cls->CanCallRuntime());
5160 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5161 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5162 codegen_->AddSlowPath(slow_path);
5163 if (generate_null_check) {
5164 __ Beqzc(out, slow_path->GetEntryLabel());
5165 }
5166 if (cls->MustGenerateClinitCheck()) {
5167 GenerateClassInitializationCheck(slow_path, out);
5168 } else {
5169 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005170 }
5171 }
5172}
5173
David Brazdilcb1c0552015-08-04 16:22:25 +01005174static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005175 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005176}
5177
Alexey Frunze4dda3372015-06-01 18:31:49 -07005178void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5179 LocationSummary* locations =
5180 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5181 locations->SetOut(Location::RequiresRegister());
5182}
5183
5184void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5185 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005186 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5187}
5188
5189void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5190 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5191}
5192
5193void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5194 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005195}
5196
Alexey Frunze4dda3372015-06-01 18:31:49 -07005197void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005198 HLoadString::LoadKind load_kind = load->GetLoadKind();
5199 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005201 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5202 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005203 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005204 } else {
5205 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005206 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5207 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5208 // Rely on the pResolveString and marking to save everything we need.
5209 RegisterSet caller_saves = RegisterSet::Empty();
5210 InvokeRuntimeCallingConvention calling_convention;
5211 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5212 locations->SetCustomSlowPathCallerSaves(caller_saves);
5213 } else {
5214 // For non-Baker read barrier we have a temp-clobbering call.
5215 }
5216 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005217 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005218}
5219
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005220// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5221// move.
5222void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005223 HLoadString::LoadKind load_kind = load->GetLoadKind();
5224 LocationSummary* locations = load->GetLocations();
5225 Location out_loc = locations->Out();
5226 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5227
5228 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005229 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5230 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5231 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005232 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005233 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5234 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5235 return; // No dex cache slow path.
5236 }
5237 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005238 uint32_t address = dchecked_integral_cast<uint32_t>(
5239 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5240 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005241 __ LoadLiteral(out,
5242 kLoadUnsignedWord,
5243 codegen_->DeduplicateBootImageAddressLiteral(address));
5244 return; // No dex cache slow path.
5245 }
5246 case HLoadString::LoadKind::kBssEntry: {
5247 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5248 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005249 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005250 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005251 GenerateGcRootFieldLoad(load,
5252 out_loc,
5253 out,
5254 /* placeholder */ 0x5678,
5255 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005256 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5257 codegen_->AddSlowPath(slow_path);
5258 __ Beqzc(out, slow_path->GetEntryLabel());
5259 __ Bind(slow_path->GetExitLabel());
5260 return;
5261 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005262 case HLoadString::LoadKind::kJitTableAddress:
5263 __ LoadLiteral(out,
5264 kLoadUnsignedWord,
5265 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5266 load->GetStringIndex(),
5267 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005268 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005269 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005270 default:
5271 break;
5272 }
5273
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005274 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005275 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5276 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005277 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005278 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5279 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5280 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005281}
5282
Alexey Frunze4dda3372015-06-01 18:31:49 -07005283void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5285 locations->SetOut(Location::ConstantLocation(constant));
5286}
5287
5288void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5289 // Will be generated at use site.
5290}
5291
5292void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5293 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005294 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005295 InvokeRuntimeCallingConvention calling_convention;
5296 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5297}
5298
5299void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005300 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005301 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005302 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005303 if (instruction->IsEnter()) {
5304 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5305 } else {
5306 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5307 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005308}
5309
5310void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5311 LocationSummary* locations =
5312 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5313 switch (mul->GetResultType()) {
5314 case Primitive::kPrimInt:
5315 case Primitive::kPrimLong:
5316 locations->SetInAt(0, Location::RequiresRegister());
5317 locations->SetInAt(1, Location::RequiresRegister());
5318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5319 break;
5320
5321 case Primitive::kPrimFloat:
5322 case Primitive::kPrimDouble:
5323 locations->SetInAt(0, Location::RequiresFpuRegister());
5324 locations->SetInAt(1, Location::RequiresFpuRegister());
5325 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5326 break;
5327
5328 default:
5329 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5330 }
5331}
5332
5333void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5334 Primitive::Type type = instruction->GetType();
5335 LocationSummary* locations = instruction->GetLocations();
5336
5337 switch (type) {
5338 case Primitive::kPrimInt:
5339 case Primitive::kPrimLong: {
5340 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5341 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5342 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5343 if (type == Primitive::kPrimInt)
5344 __ MulR6(dst, lhs, rhs);
5345 else
5346 __ Dmul(dst, lhs, rhs);
5347 break;
5348 }
5349 case Primitive::kPrimFloat:
5350 case Primitive::kPrimDouble: {
5351 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5352 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5353 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5354 if (type == Primitive::kPrimFloat)
5355 __ MulS(dst, lhs, rhs);
5356 else
5357 __ MulD(dst, lhs, rhs);
5358 break;
5359 }
5360 default:
5361 LOG(FATAL) << "Unexpected mul type " << type;
5362 }
5363}
5364
5365void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5366 LocationSummary* locations =
5367 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5368 switch (neg->GetResultType()) {
5369 case Primitive::kPrimInt:
5370 case Primitive::kPrimLong:
5371 locations->SetInAt(0, Location::RequiresRegister());
5372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5373 break;
5374
5375 case Primitive::kPrimFloat:
5376 case Primitive::kPrimDouble:
5377 locations->SetInAt(0, Location::RequiresFpuRegister());
5378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5379 break;
5380
5381 default:
5382 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5383 }
5384}
5385
5386void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5387 Primitive::Type type = instruction->GetType();
5388 LocationSummary* locations = instruction->GetLocations();
5389
5390 switch (type) {
5391 case Primitive::kPrimInt:
5392 case Primitive::kPrimLong: {
5393 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5394 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5395 if (type == Primitive::kPrimInt)
5396 __ Subu(dst, ZERO, src);
5397 else
5398 __ Dsubu(dst, ZERO, src);
5399 break;
5400 }
5401 case Primitive::kPrimFloat:
5402 case Primitive::kPrimDouble: {
5403 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5404 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5405 if (type == Primitive::kPrimFloat)
5406 __ NegS(dst, src);
5407 else
5408 __ NegD(dst, src);
5409 break;
5410 }
5411 default:
5412 LOG(FATAL) << "Unexpected neg type " << type;
5413 }
5414}
5415
5416void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5417 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005418 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005419 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005420 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005421 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5422 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005423}
5424
5425void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005426 // Note: if heap poisoning is enabled, the entry point takes care
5427 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005428 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5429 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005430}
5431
5432void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5433 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005435 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005436 if (instruction->IsStringAlloc()) {
5437 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5438 } else {
5439 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005440 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005441 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5442}
5443
5444void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005445 // Note: if heap poisoning is enabled, the entry point takes care
5446 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005447 if (instruction->IsStringAlloc()) {
5448 // String is allocated through StringFactory. Call NewEmptyString entry point.
5449 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005450 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005451 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005452 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5453 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5454 __ Jalr(T9);
5455 __ Nop();
5456 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5457 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005458 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005459 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005460 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005461}
5462
5463void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5464 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5465 locations->SetInAt(0, Location::RequiresRegister());
5466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5467}
5468
5469void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5470 Primitive::Type type = instruction->GetType();
5471 LocationSummary* locations = instruction->GetLocations();
5472
5473 switch (type) {
5474 case Primitive::kPrimInt:
5475 case Primitive::kPrimLong: {
5476 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5477 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5478 __ Nor(dst, src, ZERO);
5479 break;
5480 }
5481
5482 default:
5483 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5484 }
5485}
5486
5487void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5489 locations->SetInAt(0, Location::RequiresRegister());
5490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5491}
5492
5493void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5494 LocationSummary* locations = instruction->GetLocations();
5495 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5496 locations->InAt(0).AsRegister<GpuRegister>(),
5497 1);
5498}
5499
5500void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005501 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5502 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005503}
5504
Calin Juravle2ae48182016-03-16 14:05:09 +00005505void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5506 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005507 return;
5508 }
5509 Location obj = instruction->GetLocations()->InAt(0);
5510
5511 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005512 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005513}
5514
Calin Juravle2ae48182016-03-16 14:05:09 +00005515void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005516 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005517 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005518
5519 Location obj = instruction->GetLocations()->InAt(0);
5520
5521 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5522}
5523
5524void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005525 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005526}
5527
5528void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5529 HandleBinaryOp(instruction);
5530}
5531
5532void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5533 HandleBinaryOp(instruction);
5534}
5535
5536void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5537 LOG(FATAL) << "Unreachable";
5538}
5539
5540void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5541 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5542}
5543
5544void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5545 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5546 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5547 if (location.IsStackSlot()) {
5548 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5549 } else if (location.IsDoubleStackSlot()) {
5550 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5551 }
5552 locations->SetOut(location);
5553}
5554
5555void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5556 ATTRIBUTE_UNUSED) {
5557 // Nothing to do, the parameter is already at its location.
5558}
5559
5560void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5561 LocationSummary* locations =
5562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5563 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5564}
5565
5566void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5567 ATTRIBUTE_UNUSED) {
5568 // Nothing to do, the method is already at its location.
5569}
5570
5571void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5572 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005573 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005574 locations->SetInAt(i, Location::Any());
5575 }
5576 locations->SetOut(Location::Any());
5577}
5578
5579void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5580 LOG(FATAL) << "Unreachable";
5581}
5582
5583void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5584 Primitive::Type type = rem->GetResultType();
5585 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005586 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5587 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5589
5590 switch (type) {
5591 case Primitive::kPrimInt:
5592 case Primitive::kPrimLong:
5593 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005594 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5596 break;
5597
5598 case Primitive::kPrimFloat:
5599 case Primitive::kPrimDouble: {
5600 InvokeRuntimeCallingConvention calling_convention;
5601 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5602 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5603 locations->SetOut(calling_convention.GetReturnLocation(type));
5604 break;
5605 }
5606
5607 default:
5608 LOG(FATAL) << "Unexpected rem type " << type;
5609 }
5610}
5611
5612void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5613 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005614
5615 switch (type) {
5616 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005617 case Primitive::kPrimLong:
5618 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005619 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620
5621 case Primitive::kPrimFloat:
5622 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005623 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5624 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005625 if (type == Primitive::kPrimFloat) {
5626 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5627 } else {
5628 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5629 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005630 break;
5631 }
5632 default:
5633 LOG(FATAL) << "Unexpected rem type " << type;
5634 }
5635}
5636
Igor Murashkind01745e2017-04-05 16:40:31 -07005637void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5638 constructor_fence->SetLocations(nullptr);
5639}
5640
5641void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5642 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5643 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5644}
5645
Alexey Frunze4dda3372015-06-01 18:31:49 -07005646void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5647 memory_barrier->SetLocations(nullptr);
5648}
5649
5650void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5651 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5652}
5653
5654void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5655 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5656 Primitive::Type return_type = ret->InputAt(0)->GetType();
5657 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5658}
5659
5660void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5661 codegen_->GenerateFrameExit();
5662}
5663
5664void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5665 ret->SetLocations(nullptr);
5666}
5667
5668void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5669 codegen_->GenerateFrameExit();
5670}
5671
Alexey Frunze92d90602015-12-18 18:16:36 -08005672void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5673 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005674}
5675
Alexey Frunze92d90602015-12-18 18:16:36 -08005676void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5677 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005678}
5679
Alexey Frunze4dda3372015-06-01 18:31:49 -07005680void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5681 HandleShift(shl);
5682}
5683
5684void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5685 HandleShift(shl);
5686}
5687
5688void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5689 HandleShift(shr);
5690}
5691
5692void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5693 HandleShift(shr);
5694}
5695
Alexey Frunze4dda3372015-06-01 18:31:49 -07005696void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5697 HandleBinaryOp(instruction);
5698}
5699
5700void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5701 HandleBinaryOp(instruction);
5702}
5703
5704void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5705 HandleFieldGet(instruction, instruction->GetFieldInfo());
5706}
5707
5708void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5709 HandleFieldGet(instruction, instruction->GetFieldInfo());
5710}
5711
5712void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5713 HandleFieldSet(instruction, instruction->GetFieldInfo());
5714}
5715
5716void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005717 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005718}
5719
Calin Juravlee460d1d2015-09-29 04:52:17 +01005720void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5721 HUnresolvedInstanceFieldGet* instruction) {
5722 FieldAccessCallingConventionMIPS64 calling_convention;
5723 codegen_->CreateUnresolvedFieldLocationSummary(
5724 instruction, instruction->GetFieldType(), calling_convention);
5725}
5726
5727void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5728 HUnresolvedInstanceFieldGet* instruction) {
5729 FieldAccessCallingConventionMIPS64 calling_convention;
5730 codegen_->GenerateUnresolvedFieldAccess(instruction,
5731 instruction->GetFieldType(),
5732 instruction->GetFieldIndex(),
5733 instruction->GetDexPc(),
5734 calling_convention);
5735}
5736
5737void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5738 HUnresolvedInstanceFieldSet* instruction) {
5739 FieldAccessCallingConventionMIPS64 calling_convention;
5740 codegen_->CreateUnresolvedFieldLocationSummary(
5741 instruction, instruction->GetFieldType(), calling_convention);
5742}
5743
5744void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5745 HUnresolvedInstanceFieldSet* instruction) {
5746 FieldAccessCallingConventionMIPS64 calling_convention;
5747 codegen_->GenerateUnresolvedFieldAccess(instruction,
5748 instruction->GetFieldType(),
5749 instruction->GetFieldIndex(),
5750 instruction->GetDexPc(),
5751 calling_convention);
5752}
5753
5754void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5755 HUnresolvedStaticFieldGet* instruction) {
5756 FieldAccessCallingConventionMIPS64 calling_convention;
5757 codegen_->CreateUnresolvedFieldLocationSummary(
5758 instruction, instruction->GetFieldType(), calling_convention);
5759}
5760
5761void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5762 HUnresolvedStaticFieldGet* instruction) {
5763 FieldAccessCallingConventionMIPS64 calling_convention;
5764 codegen_->GenerateUnresolvedFieldAccess(instruction,
5765 instruction->GetFieldType(),
5766 instruction->GetFieldIndex(),
5767 instruction->GetDexPc(),
5768 calling_convention);
5769}
5770
5771void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5772 HUnresolvedStaticFieldSet* instruction) {
5773 FieldAccessCallingConventionMIPS64 calling_convention;
5774 codegen_->CreateUnresolvedFieldLocationSummary(
5775 instruction, instruction->GetFieldType(), calling_convention);
5776}
5777
5778void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5779 HUnresolvedStaticFieldSet* instruction) {
5780 FieldAccessCallingConventionMIPS64 calling_convention;
5781 codegen_->GenerateUnresolvedFieldAccess(instruction,
5782 instruction->GetFieldType(),
5783 instruction->GetFieldIndex(),
5784 instruction->GetDexPc(),
5785 calling_convention);
5786}
5787
Alexey Frunze4dda3372015-06-01 18:31:49 -07005788void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005789 LocationSummary* locations =
5790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02005791 // In suspend check slow path, usually there are no caller-save registers at all.
5792 // If SIMD instructions are present, however, we force spilling all live SIMD
5793 // registers in full width (since the runtime only saves/restores lower part).
5794 locations->SetCustomSlowPathCallerSaves(
5795 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005796}
5797
5798void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5799 HBasicBlock* block = instruction->GetBlock();
5800 if (block->GetLoopInformation() != nullptr) {
5801 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5802 // The back edge will generate the suspend check.
5803 return;
5804 }
5805 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5806 // The goto will generate the suspend check.
5807 return;
5808 }
5809 GenerateSuspendCheck(instruction, nullptr);
5810}
5811
Alexey Frunze4dda3372015-06-01 18:31:49 -07005812void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5813 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005815 InvokeRuntimeCallingConvention calling_convention;
5816 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5817}
5818
5819void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005820 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005821 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5822}
5823
5824void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5825 Primitive::Type input_type = conversion->GetInputType();
5826 Primitive::Type result_type = conversion->GetResultType();
5827 DCHECK_NE(input_type, result_type);
5828
5829 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5830 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5831 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5832 }
5833
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005834 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5835
5836 if (Primitive::IsFloatingPointType(input_type)) {
5837 locations->SetInAt(0, Location::RequiresFpuRegister());
5838 } else {
5839 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005840 }
5841
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005842 if (Primitive::IsFloatingPointType(result_type)) {
5843 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005844 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005846 }
5847}
5848
5849void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5850 LocationSummary* locations = conversion->GetLocations();
5851 Primitive::Type result_type = conversion->GetResultType();
5852 Primitive::Type input_type = conversion->GetInputType();
5853
5854 DCHECK_NE(input_type, result_type);
5855
5856 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5857 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5858 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5859
5860 switch (result_type) {
5861 case Primitive::kPrimChar:
5862 __ Andi(dst, src, 0xFFFF);
5863 break;
5864 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005865 if (input_type == Primitive::kPrimLong) {
5866 // Type conversion from long to types narrower than int is a result of code
5867 // transformations. To avoid unpredictable results for SEB and SEH, we first
5868 // need to sign-extend the low 32-bit value into bits 32 through 63.
5869 __ Sll(dst, src, 0);
5870 __ Seb(dst, dst);
5871 } else {
5872 __ Seb(dst, src);
5873 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005874 break;
5875 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005876 if (input_type == Primitive::kPrimLong) {
5877 // Type conversion from long to types narrower than int is a result of code
5878 // transformations. To avoid unpredictable results for SEB and SEH, we first
5879 // need to sign-extend the low 32-bit value into bits 32 through 63.
5880 __ Sll(dst, src, 0);
5881 __ Seh(dst, dst);
5882 } else {
5883 __ Seh(dst, src);
5884 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005885 break;
5886 case Primitive::kPrimInt:
5887 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005888 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5889 // conversions, except when the input and output registers are the same and we are not
5890 // converting longs to shorter types. In these cases, do nothing.
5891 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5892 __ Sll(dst, src, 0);
5893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005894 break;
5895
5896 default:
5897 LOG(FATAL) << "Unexpected type conversion from " << input_type
5898 << " to " << result_type;
5899 }
5900 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005901 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5902 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5903 if (input_type == Primitive::kPrimLong) {
5904 __ Dmtc1(src, FTMP);
5905 if (result_type == Primitive::kPrimFloat) {
5906 __ Cvtsl(dst, FTMP);
5907 } else {
5908 __ Cvtdl(dst, FTMP);
5909 }
5910 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005911 __ Mtc1(src, FTMP);
5912 if (result_type == Primitive::kPrimFloat) {
5913 __ Cvtsw(dst, FTMP);
5914 } else {
5915 __ Cvtdw(dst, FTMP);
5916 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005917 }
5918 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5919 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005920 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5921 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005922
5923 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00005924 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005925 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005926 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005927 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005928 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005929 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00005930 } else {
5931 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005932 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005933 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005934 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005935 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005936 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00005937 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005938 } else if (Primitive::IsFloatingPointType(result_type) &&
5939 Primitive::IsFloatingPointType(input_type)) {
5940 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5941 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5942 if (result_type == Primitive::kPrimFloat) {
5943 __ Cvtsd(dst, src);
5944 } else {
5945 __ Cvtds(dst, src);
5946 }
5947 } else {
5948 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5949 << " to " << result_type;
5950 }
5951}
5952
5953void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
5954 HandleShift(ushr);
5955}
5956
5957void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
5958 HandleShift(ushr);
5959}
5960
5961void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
5962 HandleBinaryOp(instruction);
5963}
5964
5965void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
5966 HandleBinaryOp(instruction);
5967}
5968
5969void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5970 // Nothing to do, this should be removed during prepare for register allocator.
5971 LOG(FATAL) << "Unreachable";
5972}
5973
5974void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5975 // Nothing to do, this should be removed during prepare for register allocator.
5976 LOG(FATAL) << "Unreachable";
5977}
5978
5979void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005980 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005981}
5982
5983void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005984 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005985}
5986
5987void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005988 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005989}
5990
5991void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005992 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005993}
5994
5995void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005996 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005997}
5998
5999void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006000 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006001}
6002
6003void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006004 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006005}
6006
6007void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006008 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006009}
6010
6011void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006012 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006013}
6014
6015void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006016 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006017}
6018
6019void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006020 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006021}
6022
6023void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006024 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006025}
6026
Aart Bike9f37602015-10-09 11:15:55 -07006027void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006029}
6030
6031void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006033}
6034
6035void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006036 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006037}
6038
6039void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006040 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006041}
6042
6043void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006044 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006045}
6046
6047void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006048 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006049}
6050
6051void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006052 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006053}
6054
6055void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006056 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006057}
6058
Mark Mendellfe57faa2015-09-18 09:26:15 -04006059// Simple implementation of packed switch - generate cascaded compare/jumps.
6060void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6061 LocationSummary* locations =
6062 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6063 locations->SetInAt(0, Location::RequiresRegister());
6064}
6065
Alexey Frunze0960ac52016-12-20 17:24:59 -08006066void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6067 int32_t lower_bound,
6068 uint32_t num_entries,
6069 HBasicBlock* switch_block,
6070 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006071 // Create a set of compare/jumps.
6072 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006073 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006074 // Jump to default if index is negative
6075 // Note: We don't check the case that index is positive while value < lower_bound, because in
6076 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6077 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6078
Alexey Frunze0960ac52016-12-20 17:24:59 -08006079 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006080 // Jump to successors[0] if value == lower_bound.
6081 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6082 int32_t last_index = 0;
6083 for (; num_entries - last_index > 2; last_index += 2) {
6084 __ Addiu(temp_reg, temp_reg, -2);
6085 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6086 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6087 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6088 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6089 }
6090 if (num_entries - last_index == 2) {
6091 // The last missing case_value.
6092 __ Addiu(temp_reg, temp_reg, -1);
6093 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006094 }
6095
6096 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006097 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006098 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006099 }
6100}
6101
Alexey Frunze0960ac52016-12-20 17:24:59 -08006102void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6103 int32_t lower_bound,
6104 uint32_t num_entries,
6105 HBasicBlock* switch_block,
6106 HBasicBlock* default_block) {
6107 // Create a jump table.
6108 std::vector<Mips64Label*> labels(num_entries);
6109 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6110 for (uint32_t i = 0; i < num_entries; i++) {
6111 labels[i] = codegen_->GetLabelOf(successors[i]);
6112 }
6113 JumpTable* table = __ CreateJumpTable(std::move(labels));
6114
6115 // Is the value in range?
6116 __ Addiu32(TMP, value_reg, -lower_bound);
6117 __ LoadConst32(AT, num_entries);
6118 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6119
6120 // We are in the range of the table.
6121 // Load the target address from the jump table, indexing by the value.
6122 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006123 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006124 __ Lw(TMP, TMP, 0);
6125 // Compute the absolute target address by adding the table start address
6126 // (the table contains offsets to targets relative to its start).
6127 __ Daddu(TMP, TMP, AT);
6128 // And jump.
6129 __ Jr(TMP);
6130 __ Nop();
6131}
6132
6133void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6134 int32_t lower_bound = switch_instr->GetStartValue();
6135 uint32_t num_entries = switch_instr->GetNumEntries();
6136 LocationSummary* locations = switch_instr->GetLocations();
6137 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6138 HBasicBlock* switch_block = switch_instr->GetBlock();
6139 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6140
6141 if (num_entries > kPackedSwitchJumpTableThreshold) {
6142 GenTableBasedPackedSwitch(value_reg,
6143 lower_bound,
6144 num_entries,
6145 switch_block,
6146 default_block);
6147 } else {
6148 GenPackedSwitchWithCompares(value_reg,
6149 lower_bound,
6150 num_entries,
6151 switch_block,
6152 default_block);
6153 }
6154}
6155
Chris Larsenc9905a62017-03-13 17:06:18 -07006156void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6157 LocationSummary* locations =
6158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6159 locations->SetInAt(0, Location::RequiresRegister());
6160 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006161}
6162
Chris Larsenc9905a62017-03-13 17:06:18 -07006163void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6164 LocationSummary* locations = instruction->GetLocations();
6165 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6166 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6167 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6168 __ LoadFromOffset(kLoadDoubleword,
6169 locations->Out().AsRegister<GpuRegister>(),
6170 locations->InAt(0).AsRegister<GpuRegister>(),
6171 method_offset);
6172 } else {
6173 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6174 instruction->GetIndex(), kMips64PointerSize));
6175 __ LoadFromOffset(kLoadDoubleword,
6176 locations->Out().AsRegister<GpuRegister>(),
6177 locations->InAt(0).AsRegister<GpuRegister>(),
6178 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6179 __ LoadFromOffset(kLoadDoubleword,
6180 locations->Out().AsRegister<GpuRegister>(),
6181 locations->Out().AsRegister<GpuRegister>(),
6182 method_offset);
6183 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006184}
6185
Alexey Frunze4dda3372015-06-01 18:31:49 -07006186} // namespace mips64
6187} // namespace art