blob: e4f1cbd600189ecd49c405d39e127d710daf3387 [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),
Goran Jakovljevic19680d32017-05-11 10:38:36 +0200954 assembler_(graph->GetArena(), &isa_features),
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)),
Vladimir Marko65979462017-05-19 17:25:12 +0100960 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100961 method_bss_entry_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 =
Vladimir Marko65979462017-05-19 17:25:12 +01001443 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001444 method_bss_entry_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);
Vladimir Marko65979462017-05-19 17:25:12 +01001449 if (GetCompilerOptions().IsBootImage()) {
1450 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Alexey Frunzef63f5692016-12-13 17:43:11 -08001451 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001452 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1453 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001454 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1455 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001456 } else {
1457 DCHECK(pc_relative_method_patches_.empty());
1458 DCHECK(pc_relative_type_patches_.empty());
1459 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1460 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001461 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001462 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1463 linker_patches);
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
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001476CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewMethodBssEntryPatch(
1477 MethodReference target_method) {
1478 return NewPcRelativePatch(*target_method.dex_file,
1479 target_method.dex_method_index,
1480 &method_bss_entry_patches_);
1481}
1482
Alexey Frunzef63f5692016-12-13 17:43:11 -08001483CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1484 const DexFile& dex_file, dex::TypeIndex type_index) {
1485 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001486}
1487
Vladimir Marko1998cd02017-01-13 13:02:58 +00001488CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1489 const DexFile& dex_file, dex::TypeIndex type_index) {
1490 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1491}
1492
Vladimir Marko65979462017-05-19 17:25:12 +01001493CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
1494 const DexFile& dex_file, dex::StringIndex string_index) {
1495 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
1496}
1497
Alexey Frunze19f6c692016-11-30 19:19:55 -08001498CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1499 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1500 patches->emplace_back(dex_file, offset_or_index);
1501 return &patches->back();
1502}
1503
Alexey Frunzef63f5692016-12-13 17:43:11 -08001504Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1505 return map->GetOrCreate(
1506 value,
1507 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1508}
1509
Alexey Frunze19f6c692016-11-30 19:19:55 -08001510Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1511 return uint64_literals_.GetOrCreate(
1512 value,
1513 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1514}
1515
Alexey Frunzef63f5692016-12-13 17:43:11 -08001516Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001517 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001518}
1519
Alexey Frunze19f6c692016-11-30 19:19:55 -08001520void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1521 GpuRegister out) {
1522 __ Bind(&info->pc_rel_label);
1523 // Add the high half of a 32-bit offset to PC.
1524 __ Auipc(out, /* placeholder */ 0x1234);
1525 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001526 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001527}
1528
Alexey Frunze627c1a02017-01-30 19:28:14 -08001529Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1530 dex::StringIndex string_index,
1531 Handle<mirror::String> handle) {
1532 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1533 reinterpret_cast64<uint64_t>(handle.GetReference()));
1534 return jit_string_patches_.GetOrCreate(
1535 StringReference(&dex_file, string_index),
1536 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1537}
1538
1539Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1540 dex::TypeIndex type_index,
1541 Handle<mirror::Class> handle) {
1542 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1543 reinterpret_cast64<uint64_t>(handle.GetReference()));
1544 return jit_class_patches_.GetOrCreate(
1545 TypeReference(&dex_file, type_index),
1546 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1547}
1548
1549void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1550 const uint8_t* roots_data,
1551 const Literal* literal,
1552 uint64_t index_in_table) const {
1553 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1554 uintptr_t address =
1555 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1556 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1557}
1558
1559void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1560 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001561 const StringReference& string_reference = entry.first;
1562 Literal* table_entry_literal = entry.second;
1563 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001564 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001565 uint64_t index_in_table = it->second;
1566 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001567 }
1568 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001569 const TypeReference& type_reference = entry.first;
1570 Literal* table_entry_literal = entry.second;
1571 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001572 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001573 uint64_t index_in_table = it->second;
1574 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001575 }
1576}
1577
David Brazdil58282f42016-01-14 12:45:10 +00001578void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001579 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1580 blocked_core_registers_[ZERO] = true;
1581 blocked_core_registers_[K0] = true;
1582 blocked_core_registers_[K1] = true;
1583 blocked_core_registers_[GP] = true;
1584 blocked_core_registers_[SP] = true;
1585 blocked_core_registers_[RA] = true;
1586
Lazar Trsicd9672662015-09-03 17:33:01 +02001587 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1588 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001589 blocked_core_registers_[AT] = true;
1590 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001591 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001592 blocked_fpu_registers_[FTMP] = true;
1593
1594 // Reserve suspend and thread registers.
1595 blocked_core_registers_[S0] = true;
1596 blocked_core_registers_[TR] = true;
1597
1598 // Reserve T9 for function calls
1599 blocked_core_registers_[T9] = true;
1600
Goran Jakovljevic782be112016-06-21 12:39:04 +02001601 if (GetGraph()->IsDebuggable()) {
1602 // Stubs do not save callee-save floating point registers. If the graph
1603 // is debuggable, we need to deal with these registers differently. For
1604 // now, just block them.
1605 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1606 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1607 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001608 }
1609}
1610
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1612 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001613 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001614}
1615
1616size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1617 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001618 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001619}
1620
1621size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001622 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1623 FpuRegister(reg_id),
1624 SP,
1625 stack_index);
1626 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001627}
1628
1629size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001630 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1631 FpuRegister(reg_id),
1632 SP,
1633 stack_index);
1634 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001635}
1636
1637void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001638 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001639}
1640
1641void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001642 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001643}
1644
Calin Juravle175dc732015-08-25 15:42:32 +01001645void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646 HInstruction* instruction,
1647 uint32_t dex_pc,
1648 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001649 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001650 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001651 if (EntrypointRequiresStackMap(entrypoint)) {
1652 RecordPcInfo(instruction, dex_pc, slow_path);
1653 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001654}
1655
Alexey Frunze15958152017-02-09 19:08:30 -08001656void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1657 HInstruction* instruction,
1658 SlowPathCode* slow_path) {
1659 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1660 GenerateInvokeRuntime(entry_point_offset);
1661}
1662
1663void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1664 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1665 __ Jalr(T9);
1666 __ Nop();
1667}
1668
Alexey Frunze4dda3372015-06-01 18:31:49 -07001669void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1670 GpuRegister class_reg) {
1671 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1672 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1673 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001674 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1675 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 __ Bind(slow_path->GetExitLabel());
1677}
1678
1679void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1680 __ Sync(0); // only stype 0 is supported
1681}
1682
1683void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1684 HBasicBlock* successor) {
1685 SuspendCheckSlowPathMIPS64* slow_path =
1686 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1687 codegen_->AddSlowPath(slow_path);
1688
1689 __ LoadFromOffset(kLoadUnsignedHalfword,
1690 TMP,
1691 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001692 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001693 if (successor == nullptr) {
1694 __ Bnezc(TMP, slow_path->GetEntryLabel());
1695 __ Bind(slow_path->GetReturnLabel());
1696 } else {
1697 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001698 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001699 // slow_path will return to GetLabelOf(successor).
1700 }
1701}
1702
1703InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1704 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001705 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 assembler_(codegen->GetAssembler()),
1707 codegen_(codegen) {}
1708
1709void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1710 DCHECK_EQ(instruction->InputCount(), 2U);
1711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1712 Primitive::Type type = instruction->GetResultType();
1713 switch (type) {
1714 case Primitive::kPrimInt:
1715 case Primitive::kPrimLong: {
1716 locations->SetInAt(0, Location::RequiresRegister());
1717 HInstruction* right = instruction->InputAt(1);
1718 bool can_use_imm = false;
1719 if (right->IsConstant()) {
1720 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1721 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1722 can_use_imm = IsUint<16>(imm);
1723 } else if (instruction->IsAdd()) {
1724 can_use_imm = IsInt<16>(imm);
1725 } else {
1726 DCHECK(instruction->IsSub());
1727 can_use_imm = IsInt<16>(-imm);
1728 }
1729 }
1730 if (can_use_imm)
1731 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1732 else
1733 locations->SetInAt(1, Location::RequiresRegister());
1734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1735 }
1736 break;
1737
1738 case Primitive::kPrimFloat:
1739 case Primitive::kPrimDouble:
1740 locations->SetInAt(0, Location::RequiresFpuRegister());
1741 locations->SetInAt(1, Location::RequiresFpuRegister());
1742 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1743 break;
1744
1745 default:
1746 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1747 }
1748}
1749
1750void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1751 Primitive::Type type = instruction->GetType();
1752 LocationSummary* locations = instruction->GetLocations();
1753
1754 switch (type) {
1755 case Primitive::kPrimInt:
1756 case Primitive::kPrimLong: {
1757 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1758 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1759 Location rhs_location = locations->InAt(1);
1760
1761 GpuRegister rhs_reg = ZERO;
1762 int64_t rhs_imm = 0;
1763 bool use_imm = rhs_location.IsConstant();
1764 if (use_imm) {
1765 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1766 } else {
1767 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1768 }
1769
1770 if (instruction->IsAnd()) {
1771 if (use_imm)
1772 __ Andi(dst, lhs, rhs_imm);
1773 else
1774 __ And(dst, lhs, rhs_reg);
1775 } else if (instruction->IsOr()) {
1776 if (use_imm)
1777 __ Ori(dst, lhs, rhs_imm);
1778 else
1779 __ Or(dst, lhs, rhs_reg);
1780 } else if (instruction->IsXor()) {
1781 if (use_imm)
1782 __ Xori(dst, lhs, rhs_imm);
1783 else
1784 __ Xor(dst, lhs, rhs_reg);
1785 } else if (instruction->IsAdd()) {
1786 if (type == Primitive::kPrimInt) {
1787 if (use_imm)
1788 __ Addiu(dst, lhs, rhs_imm);
1789 else
1790 __ Addu(dst, lhs, rhs_reg);
1791 } else {
1792 if (use_imm)
1793 __ Daddiu(dst, lhs, rhs_imm);
1794 else
1795 __ Daddu(dst, lhs, rhs_reg);
1796 }
1797 } else {
1798 DCHECK(instruction->IsSub());
1799 if (type == Primitive::kPrimInt) {
1800 if (use_imm)
1801 __ Addiu(dst, lhs, -rhs_imm);
1802 else
1803 __ Subu(dst, lhs, rhs_reg);
1804 } else {
1805 if (use_imm)
1806 __ Daddiu(dst, lhs, -rhs_imm);
1807 else
1808 __ Dsubu(dst, lhs, rhs_reg);
1809 }
1810 }
1811 break;
1812 }
1813 case Primitive::kPrimFloat:
1814 case Primitive::kPrimDouble: {
1815 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1816 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1817 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1818 if (instruction->IsAdd()) {
1819 if (type == Primitive::kPrimFloat)
1820 __ AddS(dst, lhs, rhs);
1821 else
1822 __ AddD(dst, lhs, rhs);
1823 } else if (instruction->IsSub()) {
1824 if (type == Primitive::kPrimFloat)
1825 __ SubS(dst, lhs, rhs);
1826 else
1827 __ SubD(dst, lhs, rhs);
1828 } else {
1829 LOG(FATAL) << "Unexpected floating-point binary operation";
1830 }
1831 break;
1832 }
1833 default:
1834 LOG(FATAL) << "Unexpected binary operation type " << type;
1835 }
1836}
1837
1838void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001839 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001840
1841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1842 Primitive::Type type = instr->GetResultType();
1843 switch (type) {
1844 case Primitive::kPrimInt:
1845 case Primitive::kPrimLong: {
1846 locations->SetInAt(0, Location::RequiresRegister());
1847 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001848 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001849 break;
1850 }
1851 default:
1852 LOG(FATAL) << "Unexpected shift type " << type;
1853 }
1854}
1855
1856void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001857 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001858 LocationSummary* locations = instr->GetLocations();
1859 Primitive::Type type = instr->GetType();
1860
1861 switch (type) {
1862 case Primitive::kPrimInt:
1863 case Primitive::kPrimLong: {
1864 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1865 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1866 Location rhs_location = locations->InAt(1);
1867
1868 GpuRegister rhs_reg = ZERO;
1869 int64_t rhs_imm = 0;
1870 bool use_imm = rhs_location.IsConstant();
1871 if (use_imm) {
1872 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1873 } else {
1874 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1875 }
1876
1877 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001878 uint32_t shift_value = rhs_imm &
1879 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880
Alexey Frunze92d90602015-12-18 18:16:36 -08001881 if (shift_value == 0) {
1882 if (dst != lhs) {
1883 __ Move(dst, lhs);
1884 }
1885 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001886 if (instr->IsShl()) {
1887 __ Sll(dst, lhs, shift_value);
1888 } else if (instr->IsShr()) {
1889 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001890 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001891 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001892 } else {
1893 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001894 }
1895 } else {
1896 if (shift_value < 32) {
1897 if (instr->IsShl()) {
1898 __ Dsll(dst, lhs, shift_value);
1899 } else if (instr->IsShr()) {
1900 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001901 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001903 } else {
1904 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001905 }
1906 } else {
1907 shift_value -= 32;
1908 if (instr->IsShl()) {
1909 __ Dsll32(dst, lhs, shift_value);
1910 } else if (instr->IsShr()) {
1911 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001914 } else {
1915 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 }
1917 }
1918 }
1919 } else {
1920 if (type == Primitive::kPrimInt) {
1921 if (instr->IsShl()) {
1922 __ Sllv(dst, lhs, rhs_reg);
1923 } else if (instr->IsShr()) {
1924 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001925 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001926 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001927 } else {
1928 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001929 }
1930 } else {
1931 if (instr->IsShl()) {
1932 __ Dsllv(dst, lhs, rhs_reg);
1933 } else if (instr->IsShr()) {
1934 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001935 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001936 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001937 } else {
1938 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001939 }
1940 }
1941 }
1942 break;
1943 }
1944 default:
1945 LOG(FATAL) << "Unexpected shift operation type " << type;
1946 }
1947}
1948
1949void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1950 HandleBinaryOp(instruction);
1951}
1952
1953void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1954 HandleBinaryOp(instruction);
1955}
1956
1957void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1958 HandleBinaryOp(instruction);
1959}
1960
1961void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1962 HandleBinaryOp(instruction);
1963}
1964
1965void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001966 Primitive::Type type = instruction->GetType();
1967 bool object_array_get_with_read_barrier =
1968 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001969 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08001970 new (GetGraph()->GetArena()) LocationSummary(instruction,
1971 object_array_get_with_read_barrier
1972 ? LocationSummary::kCallOnSlowPath
1973 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001974 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1975 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1976 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001977 locations->SetInAt(0, Location::RequiresRegister());
1978 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08001979 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001980 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1981 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08001982 // The output overlaps in the case of an object array get with
1983 // read barriers enabled: we do not want the move to overwrite the
1984 // array's location, as we need it to emit the read barrier.
1985 locations->SetOut(Location::RequiresRegister(),
1986 object_array_get_with_read_barrier
1987 ? Location::kOutputOverlap
1988 : Location::kNoOutputOverlap);
1989 }
1990 // We need a temporary register for the read barrier marking slow
1991 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
1992 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1993 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001994 }
1995}
1996
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001997static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
1998 auto null_checker = [codegen, instruction]() {
1999 codegen->MaybeRecordImplicitNullCheck(instruction);
2000 };
2001 return null_checker;
2002}
2003
Alexey Frunze4dda3372015-06-01 18:31:49 -07002004void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2005 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002006 Location obj_loc = locations->InAt(0);
2007 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2008 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002009 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002010 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002011 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002013 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002014 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2015 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002016 switch (type) {
2017 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002018 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002019 if (index.IsConstant()) {
2020 size_t offset =
2021 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002022 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002023 } else {
2024 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002025 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026 }
2027 break;
2028 }
2029
2030 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002031 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002032 if (index.IsConstant()) {
2033 size_t offset =
2034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002035 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002036 } else {
2037 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002038 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002039 }
2040 break;
2041 }
2042
2043 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002044 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002045 if (index.IsConstant()) {
2046 size_t offset =
2047 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002048 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002049 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002050 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002051 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002052 }
2053 break;
2054 }
2055
2056 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002057 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002058 if (maybe_compressed_char_at) {
2059 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002060 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002061 __ Dext(TMP, TMP, 0, 1);
2062 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2063 "Expecting 0=compressed, 1=uncompressed");
2064 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002066 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2067 if (maybe_compressed_char_at) {
2068 Mips64Label uncompressed_load, done;
2069 __ Bnezc(TMP, &uncompressed_load);
2070 __ LoadFromOffset(kLoadUnsignedByte,
2071 out,
2072 obj,
2073 data_offset + (const_index << TIMES_1));
2074 __ Bc(&done);
2075 __ Bind(&uncompressed_load);
2076 __ LoadFromOffset(kLoadUnsignedHalfword,
2077 out,
2078 obj,
2079 data_offset + (const_index << TIMES_2));
2080 __ Bind(&done);
2081 } else {
2082 __ LoadFromOffset(kLoadUnsignedHalfword,
2083 out,
2084 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002085 data_offset + (const_index << TIMES_2),
2086 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002087 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002089 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2090 if (maybe_compressed_char_at) {
2091 Mips64Label uncompressed_load, done;
2092 __ Bnezc(TMP, &uncompressed_load);
2093 __ Daddu(TMP, obj, index_reg);
2094 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2095 __ Bc(&done);
2096 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002097 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002098 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2099 __ Bind(&done);
2100 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002101 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002102 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002103 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 }
2105 break;
2106 }
2107
Alexey Frunze15958152017-02-09 19:08:30 -08002108 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002109 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002110 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2112 if (index.IsConstant()) {
2113 size_t offset =
2114 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002115 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002116 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002117 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002118 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 }
2120 break;
2121 }
2122
Alexey Frunze15958152017-02-09 19:08:30 -08002123 case Primitive::kPrimNot: {
2124 static_assert(
2125 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2126 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2127 // /* HeapReference<Object> */ out =
2128 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2129 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2130 Location temp = locations->GetTemp(0);
2131 // Note that a potential implicit null check is handled in this
2132 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2133 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2134 out_loc,
2135 obj,
2136 data_offset,
2137 index,
2138 temp,
2139 /* needs_null_check */ true);
2140 } else {
2141 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2142 if (index.IsConstant()) {
2143 size_t offset =
2144 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2145 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2146 // If read barriers are enabled, emit read barriers other than
2147 // Baker's using a slow path (and also unpoison the loaded
2148 // reference, if heap poisoning is enabled).
2149 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2150 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002151 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002152 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2153 // If read barriers are enabled, emit read barriers other than
2154 // Baker's using a slow path (and also unpoison the loaded
2155 // reference, if heap poisoning is enabled).
2156 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2157 out_loc,
2158 out_loc,
2159 obj_loc,
2160 data_offset,
2161 index);
2162 }
2163 }
2164 break;
2165 }
2166
Alexey Frunze4dda3372015-06-01 18:31:49 -07002167 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002168 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002169 if (index.IsConstant()) {
2170 size_t offset =
2171 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002172 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002173 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002174 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002175 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002176 }
2177 break;
2178 }
2179
2180 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002181 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002182 if (index.IsConstant()) {
2183 size_t offset =
2184 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002185 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002187 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002188 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002189 }
2190 break;
2191 }
2192
2193 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002194 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195 if (index.IsConstant()) {
2196 size_t offset =
2197 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002198 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002199 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002200 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002201 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002202 }
2203 break;
2204 }
2205
2206 case Primitive::kPrimVoid:
2207 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2208 UNREACHABLE();
2209 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002210}
2211
2212void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2214 locations->SetInAt(0, Location::RequiresRegister());
2215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2216}
2217
2218void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2219 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002220 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2222 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2223 __ LoadFromOffset(kLoadWord, out, obj, offset);
2224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002225 // Mask out compression flag from String's array length.
2226 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2227 __ Srl(out, out, 1u);
2228 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002229}
2230
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002231Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2232 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2233 ? Location::ConstantLocation(instruction->AsConstant())
2234 : Location::RequiresRegister();
2235}
2236
2237Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2238 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2239 // We can store a non-zero float or double constant without first loading it into the FPU,
2240 // but we should only prefer this if the constant has a single use.
2241 if (instruction->IsConstant() &&
2242 (instruction->AsConstant()->IsZeroBitPattern() ||
2243 instruction->GetUses().HasExactlyOneElement())) {
2244 return Location::ConstantLocation(instruction->AsConstant());
2245 // Otherwise fall through and require an FPU register for the constant.
2246 }
2247 return Location::RequiresFpuRegister();
2248}
2249
Alexey Frunze4dda3372015-06-01 18:31:49 -07002250void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002251 Primitive::Type value_type = instruction->GetComponentType();
2252
2253 bool needs_write_barrier =
2254 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2255 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2256
Alexey Frunze4dda3372015-06-01 18:31:49 -07002257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2258 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002259 may_need_runtime_call_for_type_check ?
2260 LocationSummary::kCallOnSlowPath :
2261 LocationSummary::kNoCall);
2262
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2265 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2266 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002268 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2269 }
2270 if (needs_write_barrier) {
2271 // Temporary register for the write barrier.
2272 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002273 }
2274}
2275
2276void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2277 LocationSummary* locations = instruction->GetLocations();
2278 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2279 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002280 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002282 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002283 bool needs_write_barrier =
2284 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002285 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002286 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002287
2288 switch (value_type) {
2289 case Primitive::kPrimBoolean:
2290 case Primitive::kPrimByte: {
2291 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002292 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002293 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002294 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002295 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2296 }
2297 if (value_location.IsConstant()) {
2298 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2299 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2300 } else {
2301 GpuRegister value = value_location.AsRegister<GpuRegister>();
2302 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 }
2304 break;
2305 }
2306
2307 case Primitive::kPrimShort:
2308 case Primitive::kPrimChar: {
2309 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002310 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002311 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002312 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002313 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002314 }
2315 if (value_location.IsConstant()) {
2316 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2317 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2318 } else {
2319 GpuRegister value = value_location.AsRegister<GpuRegister>();
2320 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002321 }
2322 break;
2323 }
2324
Alexey Frunze15958152017-02-09 19:08:30 -08002325 case Primitive::kPrimInt: {
2326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2327 if (index.IsConstant()) {
2328 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2329 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002330 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002331 }
2332 if (value_location.IsConstant()) {
2333 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2334 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2335 } else {
2336 GpuRegister value = value_location.AsRegister<GpuRegister>();
2337 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2338 }
2339 break;
2340 }
2341
Alexey Frunze4dda3372015-06-01 18:31:49 -07002342 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002343 if (value_location.IsConstant()) {
2344 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002345 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002346 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002347 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002348 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002349 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002350 }
Alexey Frunze15958152017-02-09 19:08:30 -08002351 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2352 DCHECK_EQ(value, 0);
2353 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2354 DCHECK(!needs_write_barrier);
2355 DCHECK(!may_need_runtime_call_for_type_check);
2356 break;
2357 }
2358
2359 DCHECK(needs_write_barrier);
2360 GpuRegister value = value_location.AsRegister<GpuRegister>();
2361 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2362 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2363 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2364 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2365 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2366 Mips64Label done;
2367 SlowPathCodeMIPS64* slow_path = nullptr;
2368
2369 if (may_need_runtime_call_for_type_check) {
2370 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2371 codegen_->AddSlowPath(slow_path);
2372 if (instruction->GetValueCanBeNull()) {
2373 Mips64Label non_zero;
2374 __ Bnezc(value, &non_zero);
2375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2376 if (index.IsConstant()) {
2377 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002378 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002379 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002380 }
Alexey Frunze15958152017-02-09 19:08:30 -08002381 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2382 __ Bc(&done);
2383 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002384 }
Alexey Frunze15958152017-02-09 19:08:30 -08002385
2386 // Note that when read barriers are enabled, the type checks
2387 // are performed without read barriers. This is fine, even in
2388 // the case where a class object is in the from-space after
2389 // the flip, as a comparison involving such a type would not
2390 // produce a false positive; it may of course produce a false
2391 // negative, in which case we would take the ArraySet slow
2392 // path.
2393
2394 // /* HeapReference<Class> */ temp1 = obj->klass_
2395 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2396 __ MaybeUnpoisonHeapReference(temp1);
2397
2398 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2399 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2400 // /* HeapReference<Class> */ temp2 = value->klass_
2401 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2402 // If heap poisoning is enabled, no need to unpoison `temp1`
2403 // nor `temp2`, as we are comparing two poisoned references.
2404
2405 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2406 Mips64Label do_put;
2407 __ Beqc(temp1, temp2, &do_put);
2408 // If heap poisoning is enabled, the `temp1` reference has
2409 // not been unpoisoned yet; unpoison it now.
2410 __ MaybeUnpoisonHeapReference(temp1);
2411
2412 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2413 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2414 // If heap poisoning is enabled, no need to unpoison
2415 // `temp1`, as we are comparing against null below.
2416 __ Bnezc(temp1, slow_path->GetEntryLabel());
2417 __ Bind(&do_put);
2418 } else {
2419 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2420 }
2421 }
2422
2423 GpuRegister source = value;
2424 if (kPoisonHeapReferences) {
2425 // Note that in the case where `value` is a null reference,
2426 // we do not enter this block, as a null reference does not
2427 // need poisoning.
2428 __ Move(temp1, value);
2429 __ PoisonHeapReference(temp1);
2430 source = temp1;
2431 }
2432
2433 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2434 if (index.IsConstant()) {
2435 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002436 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002437 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002438 }
2439 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2440
2441 if (!may_need_runtime_call_for_type_check) {
2442 codegen_->MaybeRecordImplicitNullCheck(instruction);
2443 }
2444
2445 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2446
2447 if (done.IsLinked()) {
2448 __ Bind(&done);
2449 }
2450
2451 if (slow_path != nullptr) {
2452 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002453 }
2454 break;
2455 }
2456
2457 case Primitive::kPrimLong: {
2458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002460 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002461 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002462 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002463 }
2464 if (value_location.IsConstant()) {
2465 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2466 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2467 } else {
2468 GpuRegister value = value_location.AsRegister<GpuRegister>();
2469 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002470 }
2471 break;
2472 }
2473
2474 case Primitive::kPrimFloat: {
2475 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002476 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002477 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002479 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002480 }
2481 if (value_location.IsConstant()) {
2482 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2483 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2484 } else {
2485 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2486 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002487 }
2488 break;
2489 }
2490
2491 case Primitive::kPrimDouble: {
2492 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002493 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002494 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002495 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002496 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002497 }
2498 if (value_location.IsConstant()) {
2499 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2500 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2501 } else {
2502 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2503 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002504 }
2505 break;
2506 }
2507
2508 case Primitive::kPrimVoid:
2509 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2510 UNREACHABLE();
2511 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002512}
2513
2514void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002515 RegisterSet caller_saves = RegisterSet::Empty();
2516 InvokeRuntimeCallingConvention calling_convention;
2517 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2518 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2519 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002520 locations->SetInAt(0, Location::RequiresRegister());
2521 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002522}
2523
2524void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2525 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002526 BoundsCheckSlowPathMIPS64* slow_path =
2527 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002528 codegen_->AddSlowPath(slow_path);
2529
2530 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2531 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2532
2533 // length is limited by the maximum positive signed 32-bit integer.
2534 // Unsigned comparison of length and index checks for index < 0
2535 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002536 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002537}
2538
Alexey Frunze15958152017-02-09 19:08:30 -08002539// Temp is used for read barrier.
2540static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2541 if (kEmitCompilerReadBarrier &&
2542 (kUseBakerReadBarrier ||
2543 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2544 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2545 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2546 return 1;
2547 }
2548 return 0;
2549}
2550
2551// Extra temp is used for read barrier.
2552static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2553 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2554}
2555
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002557 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2558 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2559
2560 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2561 switch (type_check_kind) {
2562 case TypeCheckKind::kExactCheck:
2563 case TypeCheckKind::kAbstractClassCheck:
2564 case TypeCheckKind::kClassHierarchyCheck:
2565 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002566 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002567 ? LocationSummary::kCallOnSlowPath
2568 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2569 break;
2570 case TypeCheckKind::kArrayCheck:
2571 case TypeCheckKind::kUnresolvedCheck:
2572 case TypeCheckKind::kInterfaceCheck:
2573 call_kind = LocationSummary::kCallOnSlowPath;
2574 break;
2575 }
2576
2577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 locations->SetInAt(0, Location::RequiresRegister());
2579 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002580 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581}
2582
2583void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002584 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002585 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002586 Location obj_loc = locations->InAt(0);
2587 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002588 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002589 Location temp_loc = locations->GetTemp(0);
2590 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2591 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2592 DCHECK_LE(num_temps, 2u);
2593 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002594 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2595 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2596 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2597 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2598 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2599 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2600 const uint32_t object_array_data_offset =
2601 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2602 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002604 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2605 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2606 // read barriers is done for performance and code size reasons.
2607 bool is_type_check_slow_path_fatal = false;
2608 if (!kEmitCompilerReadBarrier) {
2609 is_type_check_slow_path_fatal =
2610 (type_check_kind == TypeCheckKind::kExactCheck ||
2611 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2612 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2613 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2614 !instruction->CanThrowIntoCatchBlock();
2615 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002616 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002617 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2618 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002619 codegen_->AddSlowPath(slow_path);
2620
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002621 // Avoid this check if we know `obj` is not null.
2622 if (instruction->MustDoNullCheck()) {
2623 __ Beqzc(obj, &done);
2624 }
2625
2626 switch (type_check_kind) {
2627 case TypeCheckKind::kExactCheck:
2628 case TypeCheckKind::kArrayCheck: {
2629 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002630 GenerateReferenceLoadTwoRegisters(instruction,
2631 temp_loc,
2632 obj_loc,
2633 class_offset,
2634 maybe_temp2_loc,
2635 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002636 // Jump to slow path for throwing the exception or doing a
2637 // more involved array check.
2638 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2639 break;
2640 }
2641
2642 case TypeCheckKind::kAbstractClassCheck: {
2643 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002644 GenerateReferenceLoadTwoRegisters(instruction,
2645 temp_loc,
2646 obj_loc,
2647 class_offset,
2648 maybe_temp2_loc,
2649 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002650 // If the class is abstract, we eagerly fetch the super class of the
2651 // object to avoid doing a comparison we know will fail.
2652 Mips64Label loop;
2653 __ Bind(&loop);
2654 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002655 GenerateReferenceLoadOneRegister(instruction,
2656 temp_loc,
2657 super_offset,
2658 maybe_temp2_loc,
2659 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002660 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2661 // exception.
2662 __ Beqzc(temp, slow_path->GetEntryLabel());
2663 // Otherwise, compare the classes.
2664 __ Bnec(temp, cls, &loop);
2665 break;
2666 }
2667
2668 case TypeCheckKind::kClassHierarchyCheck: {
2669 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002670 GenerateReferenceLoadTwoRegisters(instruction,
2671 temp_loc,
2672 obj_loc,
2673 class_offset,
2674 maybe_temp2_loc,
2675 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002676 // Walk over the class hierarchy to find a match.
2677 Mips64Label loop;
2678 __ Bind(&loop);
2679 __ Beqc(temp, cls, &done);
2680 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002681 GenerateReferenceLoadOneRegister(instruction,
2682 temp_loc,
2683 super_offset,
2684 maybe_temp2_loc,
2685 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002686 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2687 // exception. Otherwise, jump to the beginning of the loop.
2688 __ Bnezc(temp, &loop);
2689 __ Bc(slow_path->GetEntryLabel());
2690 break;
2691 }
2692
2693 case TypeCheckKind::kArrayObjectCheck: {
2694 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002695 GenerateReferenceLoadTwoRegisters(instruction,
2696 temp_loc,
2697 obj_loc,
2698 class_offset,
2699 maybe_temp2_loc,
2700 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002701 // Do an exact check.
2702 __ Beqc(temp, cls, &done);
2703 // Otherwise, we need to check that the object's class is a non-primitive array.
2704 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002705 GenerateReferenceLoadOneRegister(instruction,
2706 temp_loc,
2707 component_offset,
2708 maybe_temp2_loc,
2709 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002710 // If the component type is null, jump to the slow path to throw the exception.
2711 __ Beqzc(temp, slow_path->GetEntryLabel());
2712 // Otherwise, the object is indeed an array, further check that this component
2713 // type is not a primitive type.
2714 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2715 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2716 __ Bnezc(temp, slow_path->GetEntryLabel());
2717 break;
2718 }
2719
2720 case TypeCheckKind::kUnresolvedCheck:
2721 // We always go into the type check slow path for the unresolved check case.
2722 // We cannot directly call the CheckCast runtime entry point
2723 // without resorting to a type checking slow path here (i.e. by
2724 // calling InvokeRuntime directly), as it would require to
2725 // assign fixed registers for the inputs of this HInstanceOf
2726 // instruction (following the runtime calling convention), which
2727 // might be cluttered by the potential first read barrier
2728 // emission at the beginning of this method.
2729 __ Bc(slow_path->GetEntryLabel());
2730 break;
2731
2732 case TypeCheckKind::kInterfaceCheck: {
2733 // Avoid read barriers to improve performance of the fast path. We can not get false
2734 // positives by doing this.
2735 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002736 GenerateReferenceLoadTwoRegisters(instruction,
2737 temp_loc,
2738 obj_loc,
2739 class_offset,
2740 maybe_temp2_loc,
2741 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002742 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002743 GenerateReferenceLoadTwoRegisters(instruction,
2744 temp_loc,
2745 temp_loc,
2746 iftable_offset,
2747 maybe_temp2_loc,
2748 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002749 // Iftable is never null.
2750 __ Lw(TMP, temp, array_length_offset);
2751 // Loop through the iftable and check if any class matches.
2752 Mips64Label loop;
2753 __ Bind(&loop);
2754 __ Beqzc(TMP, slow_path->GetEntryLabel());
2755 __ Lwu(AT, temp, object_array_data_offset);
2756 __ MaybeUnpoisonHeapReference(AT);
2757 // Go to next interface.
2758 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2759 __ Addiu(TMP, TMP, -2);
2760 // Compare the classes and continue the loop if they do not match.
2761 __ Bnec(AT, cls, &loop);
2762 break;
2763 }
2764 }
2765
2766 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002767 __ Bind(slow_path->GetExitLabel());
2768}
2769
2770void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2771 LocationSummary* locations =
2772 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2773 locations->SetInAt(0, Location::RequiresRegister());
2774 if (check->HasUses()) {
2775 locations->SetOut(Location::SameAsFirstInput());
2776 }
2777}
2778
2779void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2780 // We assume the class is not null.
2781 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2782 check->GetLoadClass(),
2783 check,
2784 check->GetDexPc(),
2785 true);
2786 codegen_->AddSlowPath(slow_path);
2787 GenerateClassInitializationCheck(slow_path,
2788 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2789}
2790
2791void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2792 Primitive::Type in_type = compare->InputAt(0)->GetType();
2793
Alexey Frunze299a9392015-12-08 16:08:02 -08002794 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002795
2796 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002797 case Primitive::kPrimBoolean:
2798 case Primitive::kPrimByte:
2799 case Primitive::kPrimShort:
2800 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002801 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002802 case Primitive::kPrimLong:
2803 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002804 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2806 break;
2807
2808 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002809 case Primitive::kPrimDouble:
2810 locations->SetInAt(0, Location::RequiresFpuRegister());
2811 locations->SetInAt(1, Location::RequiresFpuRegister());
2812 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002813 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002814
2815 default:
2816 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2817 }
2818}
2819
2820void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2821 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002822 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002823 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2824
2825 // 0 if: left == right
2826 // 1 if: left > right
2827 // -1 if: left < right
2828 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002829 case Primitive::kPrimBoolean:
2830 case Primitive::kPrimByte:
2831 case Primitive::kPrimShort:
2832 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002833 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002834 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002835 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002836 Location rhs_location = locations->InAt(1);
2837 bool use_imm = rhs_location.IsConstant();
2838 GpuRegister rhs = ZERO;
2839 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002840 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002841 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2842 if (value != 0) {
2843 rhs = AT;
2844 __ LoadConst64(rhs, value);
2845 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002846 } else {
2847 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2848 if (value != 0) {
2849 rhs = AT;
2850 __ LoadConst32(rhs, value);
2851 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002852 }
2853 } else {
2854 rhs = rhs_location.AsRegister<GpuRegister>();
2855 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002856 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002857 __ Slt(res, rhs, lhs);
2858 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002859 break;
2860 }
2861
Alexey Frunze299a9392015-12-08 16:08:02 -08002862 case Primitive::kPrimFloat: {
2863 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2864 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2865 Mips64Label done;
2866 __ CmpEqS(FTMP, lhs, rhs);
2867 __ LoadConst32(res, 0);
2868 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002869 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002870 __ CmpLtS(FTMP, lhs, rhs);
2871 __ LoadConst32(res, -1);
2872 __ Bc1nez(FTMP, &done);
2873 __ LoadConst32(res, 1);
2874 } else {
2875 __ CmpLtS(FTMP, rhs, lhs);
2876 __ LoadConst32(res, 1);
2877 __ Bc1nez(FTMP, &done);
2878 __ LoadConst32(res, -1);
2879 }
2880 __ Bind(&done);
2881 break;
2882 }
2883
Alexey Frunze4dda3372015-06-01 18:31:49 -07002884 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002885 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2886 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2887 Mips64Label done;
2888 __ CmpEqD(FTMP, lhs, rhs);
2889 __ LoadConst32(res, 0);
2890 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002891 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002892 __ CmpLtD(FTMP, lhs, rhs);
2893 __ LoadConst32(res, -1);
2894 __ Bc1nez(FTMP, &done);
2895 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002896 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002897 __ CmpLtD(FTMP, rhs, lhs);
2898 __ LoadConst32(res, 1);
2899 __ Bc1nez(FTMP, &done);
2900 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002901 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002902 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002903 break;
2904 }
2905
2906 default:
2907 LOG(FATAL) << "Unimplemented compare type " << in_type;
2908 }
2909}
2910
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002911void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002913 switch (instruction->InputAt(0)->GetType()) {
2914 default:
2915 case Primitive::kPrimLong:
2916 locations->SetInAt(0, Location::RequiresRegister());
2917 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2918 break;
2919
2920 case Primitive::kPrimFloat:
2921 case Primitive::kPrimDouble:
2922 locations->SetInAt(0, Location::RequiresFpuRegister());
2923 locations->SetInAt(1, Location::RequiresFpuRegister());
2924 break;
2925 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002926 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2928 }
2929}
2930
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002931void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002932 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002933 return;
2934 }
2935
Alexey Frunze299a9392015-12-08 16:08:02 -08002936 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002937 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002938 switch (type) {
2939 default:
2940 // Integer case.
2941 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2942 return;
2943 case Primitive::kPrimLong:
2944 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2945 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002946 case Primitive::kPrimFloat:
2947 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002948 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2949 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002950 }
2951}
2952
Alexey Frunzec857c742015-09-23 15:12:39 -07002953void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2954 DCHECK(instruction->IsDiv() || instruction->IsRem());
2955 Primitive::Type type = instruction->GetResultType();
2956
2957 LocationSummary* locations = instruction->GetLocations();
2958 Location second = locations->InAt(1);
2959 DCHECK(second.IsConstant());
2960
2961 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2962 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2963 int64_t imm = Int64FromConstant(second.GetConstant());
2964 DCHECK(imm == 1 || imm == -1);
2965
2966 if (instruction->IsRem()) {
2967 __ Move(out, ZERO);
2968 } else {
2969 if (imm == -1) {
2970 if (type == Primitive::kPrimInt) {
2971 __ Subu(out, ZERO, dividend);
2972 } else {
2973 DCHECK_EQ(type, Primitive::kPrimLong);
2974 __ Dsubu(out, ZERO, dividend);
2975 }
2976 } else if (out != dividend) {
2977 __ Move(out, dividend);
2978 }
2979 }
2980}
2981
2982void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2983 DCHECK(instruction->IsDiv() || instruction->IsRem());
2984 Primitive::Type type = instruction->GetResultType();
2985
2986 LocationSummary* locations = instruction->GetLocations();
2987 Location second = locations->InAt(1);
2988 DCHECK(second.IsConstant());
2989
2990 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2991 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2992 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002993 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002994 int ctz_imm = CTZ(abs_imm);
2995
2996 if (instruction->IsDiv()) {
2997 if (type == Primitive::kPrimInt) {
2998 if (ctz_imm == 1) {
2999 // Fast path for division by +/-2, which is very common.
3000 __ Srl(TMP, dividend, 31);
3001 } else {
3002 __ Sra(TMP, dividend, 31);
3003 __ Srl(TMP, TMP, 32 - ctz_imm);
3004 }
3005 __ Addu(out, dividend, TMP);
3006 __ Sra(out, out, ctz_imm);
3007 if (imm < 0) {
3008 __ Subu(out, ZERO, out);
3009 }
3010 } else {
3011 DCHECK_EQ(type, Primitive::kPrimLong);
3012 if (ctz_imm == 1) {
3013 // Fast path for division by +/-2, which is very common.
3014 __ Dsrl32(TMP, dividend, 31);
3015 } else {
3016 __ Dsra32(TMP, dividend, 31);
3017 if (ctz_imm > 32) {
3018 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3019 } else {
3020 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3021 }
3022 }
3023 __ Daddu(out, dividend, TMP);
3024 if (ctz_imm < 32) {
3025 __ Dsra(out, out, ctz_imm);
3026 } else {
3027 __ Dsra32(out, out, ctz_imm - 32);
3028 }
3029 if (imm < 0) {
3030 __ Dsubu(out, ZERO, out);
3031 }
3032 }
3033 } else {
3034 if (type == Primitive::kPrimInt) {
3035 if (ctz_imm == 1) {
3036 // Fast path for modulo +/-2, which is very common.
3037 __ Sra(TMP, dividend, 31);
3038 __ Subu(out, dividend, TMP);
3039 __ Andi(out, out, 1);
3040 __ Addu(out, out, TMP);
3041 } else {
3042 __ Sra(TMP, dividend, 31);
3043 __ Srl(TMP, TMP, 32 - ctz_imm);
3044 __ Addu(out, dividend, TMP);
3045 if (IsUint<16>(abs_imm - 1)) {
3046 __ Andi(out, out, abs_imm - 1);
3047 } else {
3048 __ Sll(out, out, 32 - ctz_imm);
3049 __ Srl(out, out, 32 - ctz_imm);
3050 }
3051 __ Subu(out, out, TMP);
3052 }
3053 } else {
3054 DCHECK_EQ(type, Primitive::kPrimLong);
3055 if (ctz_imm == 1) {
3056 // Fast path for modulo +/-2, which is very common.
3057 __ Dsra32(TMP, dividend, 31);
3058 __ Dsubu(out, dividend, TMP);
3059 __ Andi(out, out, 1);
3060 __ Daddu(out, out, TMP);
3061 } else {
3062 __ Dsra32(TMP, dividend, 31);
3063 if (ctz_imm > 32) {
3064 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3065 } else {
3066 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3067 }
3068 __ Daddu(out, dividend, TMP);
3069 if (IsUint<16>(abs_imm - 1)) {
3070 __ Andi(out, out, abs_imm - 1);
3071 } else {
3072 if (ctz_imm > 32) {
3073 __ Dsll(out, out, 64 - ctz_imm);
3074 __ Dsrl(out, out, 64 - ctz_imm);
3075 } else {
3076 __ Dsll32(out, out, 32 - ctz_imm);
3077 __ Dsrl32(out, out, 32 - ctz_imm);
3078 }
3079 }
3080 __ Dsubu(out, out, TMP);
3081 }
3082 }
3083 }
3084}
3085
3086void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3087 DCHECK(instruction->IsDiv() || instruction->IsRem());
3088
3089 LocationSummary* locations = instruction->GetLocations();
3090 Location second = locations->InAt(1);
3091 DCHECK(second.IsConstant());
3092
3093 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3094 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3095 int64_t imm = Int64FromConstant(second.GetConstant());
3096
3097 Primitive::Type type = instruction->GetResultType();
3098 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3099
3100 int64_t magic;
3101 int shift;
3102 CalculateMagicAndShiftForDivRem(imm,
3103 (type == Primitive::kPrimLong),
3104 &magic,
3105 &shift);
3106
3107 if (type == Primitive::kPrimInt) {
3108 __ LoadConst32(TMP, magic);
3109 __ MuhR6(TMP, dividend, TMP);
3110
3111 if (imm > 0 && magic < 0) {
3112 __ Addu(TMP, TMP, dividend);
3113 } else if (imm < 0 && magic > 0) {
3114 __ Subu(TMP, TMP, dividend);
3115 }
3116
3117 if (shift != 0) {
3118 __ Sra(TMP, TMP, shift);
3119 }
3120
3121 if (instruction->IsDiv()) {
3122 __ Sra(out, TMP, 31);
3123 __ Subu(out, TMP, out);
3124 } else {
3125 __ Sra(AT, TMP, 31);
3126 __ Subu(AT, TMP, AT);
3127 __ LoadConst32(TMP, imm);
3128 __ MulR6(TMP, AT, TMP);
3129 __ Subu(out, dividend, TMP);
3130 }
3131 } else {
3132 __ LoadConst64(TMP, magic);
3133 __ Dmuh(TMP, dividend, TMP);
3134
3135 if (imm > 0 && magic < 0) {
3136 __ Daddu(TMP, TMP, dividend);
3137 } else if (imm < 0 && magic > 0) {
3138 __ Dsubu(TMP, TMP, dividend);
3139 }
3140
3141 if (shift >= 32) {
3142 __ Dsra32(TMP, TMP, shift - 32);
3143 } else if (shift > 0) {
3144 __ Dsra(TMP, TMP, shift);
3145 }
3146
3147 if (instruction->IsDiv()) {
3148 __ Dsra32(out, TMP, 31);
3149 __ Dsubu(out, TMP, out);
3150 } else {
3151 __ Dsra32(AT, TMP, 31);
3152 __ Dsubu(AT, TMP, AT);
3153 __ LoadConst64(TMP, imm);
3154 __ Dmul(TMP, AT, TMP);
3155 __ Dsubu(out, dividend, TMP);
3156 }
3157 }
3158}
3159
3160void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3161 DCHECK(instruction->IsDiv() || instruction->IsRem());
3162 Primitive::Type type = instruction->GetResultType();
3163 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3164
3165 LocationSummary* locations = instruction->GetLocations();
3166 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3167 Location second = locations->InAt(1);
3168
3169 if (second.IsConstant()) {
3170 int64_t imm = Int64FromConstant(second.GetConstant());
3171 if (imm == 0) {
3172 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3173 } else if (imm == 1 || imm == -1) {
3174 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003175 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003176 DivRemByPowerOfTwo(instruction);
3177 } else {
3178 DCHECK(imm <= -2 || imm >= 2);
3179 GenerateDivRemWithAnyConstant(instruction);
3180 }
3181 } else {
3182 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3183 GpuRegister divisor = second.AsRegister<GpuRegister>();
3184 if (instruction->IsDiv()) {
3185 if (type == Primitive::kPrimInt)
3186 __ DivR6(out, dividend, divisor);
3187 else
3188 __ Ddiv(out, dividend, divisor);
3189 } else {
3190 if (type == Primitive::kPrimInt)
3191 __ ModR6(out, dividend, divisor);
3192 else
3193 __ Dmod(out, dividend, divisor);
3194 }
3195 }
3196}
3197
Alexey Frunze4dda3372015-06-01 18:31:49 -07003198void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3199 LocationSummary* locations =
3200 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3201 switch (div->GetResultType()) {
3202 case Primitive::kPrimInt:
3203 case Primitive::kPrimLong:
3204 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003205 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3207 break;
3208
3209 case Primitive::kPrimFloat:
3210 case Primitive::kPrimDouble:
3211 locations->SetInAt(0, Location::RequiresFpuRegister());
3212 locations->SetInAt(1, Location::RequiresFpuRegister());
3213 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3214 break;
3215
3216 default:
3217 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3218 }
3219}
3220
3221void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3222 Primitive::Type type = instruction->GetType();
3223 LocationSummary* locations = instruction->GetLocations();
3224
3225 switch (type) {
3226 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003227 case Primitive::kPrimLong:
3228 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003230 case Primitive::kPrimFloat:
3231 case Primitive::kPrimDouble: {
3232 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3233 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3234 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3235 if (type == Primitive::kPrimFloat)
3236 __ DivS(dst, lhs, rhs);
3237 else
3238 __ DivD(dst, lhs, rhs);
3239 break;
3240 }
3241 default:
3242 LOG(FATAL) << "Unexpected div type " << type;
3243 }
3244}
3245
3246void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003247 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003248 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003249}
3250
3251void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3252 SlowPathCodeMIPS64* slow_path =
3253 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3254 codegen_->AddSlowPath(slow_path);
3255 Location value = instruction->GetLocations()->InAt(0);
3256
3257 Primitive::Type type = instruction->GetType();
3258
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003259 if (!Primitive::IsIntegralType(type)) {
3260 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003261 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003262 }
3263
3264 if (value.IsConstant()) {
3265 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3266 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003267 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003268 } else {
3269 // A division by a non-null constant is valid. We don't need to perform
3270 // any check, so simply fall through.
3271 }
3272 } else {
3273 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3274 }
3275}
3276
3277void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3278 LocationSummary* locations =
3279 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3280 locations->SetOut(Location::ConstantLocation(constant));
3281}
3282
3283void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3284 // Will be generated at use site.
3285}
3286
3287void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3288 exit->SetLocations(nullptr);
3289}
3290
3291void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3292}
3293
3294void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3295 LocationSummary* locations =
3296 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3297 locations->SetOut(Location::ConstantLocation(constant));
3298}
3299
3300void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3301 // Will be generated at use site.
3302}
3303
David Brazdilfc6a86a2015-06-26 10:33:45 +00003304void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003305 DCHECK(!successor->IsExitBlock());
3306 HBasicBlock* block = got->GetBlock();
3307 HInstruction* previous = got->GetPrevious();
3308 HLoopInformation* info = block->GetLoopInformation();
3309
3310 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3311 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3312 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3313 return;
3314 }
3315 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3316 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3317 }
3318 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003319 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003320 }
3321}
3322
David Brazdilfc6a86a2015-06-26 10:33:45 +00003323void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3324 got->SetLocations(nullptr);
3325}
3326
3327void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3328 HandleGoto(got, got->GetSuccessor());
3329}
3330
3331void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3332 try_boundary->SetLocations(nullptr);
3333}
3334
3335void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3336 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3337 if (!successor->IsExitBlock()) {
3338 HandleGoto(try_boundary, successor);
3339 }
3340}
3341
Alexey Frunze299a9392015-12-08 16:08:02 -08003342void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3343 bool is64bit,
3344 LocationSummary* locations) {
3345 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3346 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3347 Location rhs_location = locations->InAt(1);
3348 GpuRegister rhs_reg = ZERO;
3349 int64_t rhs_imm = 0;
3350 bool use_imm = rhs_location.IsConstant();
3351 if (use_imm) {
3352 if (is64bit) {
3353 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3354 } else {
3355 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3356 }
3357 } else {
3358 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3359 }
3360 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3361
3362 switch (cond) {
3363 case kCondEQ:
3364 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003365 if (use_imm && IsInt<16>(-rhs_imm)) {
3366 if (rhs_imm == 0) {
3367 if (cond == kCondEQ) {
3368 __ Sltiu(dst, lhs, 1);
3369 } else {
3370 __ Sltu(dst, ZERO, lhs);
3371 }
3372 } else {
3373 if (is64bit) {
3374 __ Daddiu(dst, lhs, -rhs_imm);
3375 } else {
3376 __ Addiu(dst, lhs, -rhs_imm);
3377 }
3378 if (cond == kCondEQ) {
3379 __ Sltiu(dst, dst, 1);
3380 } else {
3381 __ Sltu(dst, ZERO, dst);
3382 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003383 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003384 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003385 if (use_imm && IsUint<16>(rhs_imm)) {
3386 __ Xori(dst, lhs, rhs_imm);
3387 } else {
3388 if (use_imm) {
3389 rhs_reg = TMP;
3390 __ LoadConst64(rhs_reg, rhs_imm);
3391 }
3392 __ Xor(dst, lhs, rhs_reg);
3393 }
3394 if (cond == kCondEQ) {
3395 __ Sltiu(dst, dst, 1);
3396 } else {
3397 __ Sltu(dst, ZERO, dst);
3398 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003399 }
3400 break;
3401
3402 case kCondLT:
3403 case kCondGE:
3404 if (use_imm && IsInt<16>(rhs_imm)) {
3405 __ Slti(dst, lhs, rhs_imm);
3406 } else {
3407 if (use_imm) {
3408 rhs_reg = TMP;
3409 __ LoadConst64(rhs_reg, rhs_imm);
3410 }
3411 __ Slt(dst, lhs, rhs_reg);
3412 }
3413 if (cond == kCondGE) {
3414 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3415 // only the slt instruction but no sge.
3416 __ Xori(dst, dst, 1);
3417 }
3418 break;
3419
3420 case kCondLE:
3421 case kCondGT:
3422 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3423 // Simulate lhs <= rhs via lhs < rhs + 1.
3424 __ Slti(dst, lhs, rhs_imm_plus_one);
3425 if (cond == kCondGT) {
3426 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3427 // only the slti instruction but no sgti.
3428 __ Xori(dst, dst, 1);
3429 }
3430 } else {
3431 if (use_imm) {
3432 rhs_reg = TMP;
3433 __ LoadConst64(rhs_reg, rhs_imm);
3434 }
3435 __ Slt(dst, rhs_reg, lhs);
3436 if (cond == kCondLE) {
3437 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3438 // only the slt instruction but no sle.
3439 __ Xori(dst, dst, 1);
3440 }
3441 }
3442 break;
3443
3444 case kCondB:
3445 case kCondAE:
3446 if (use_imm && IsInt<16>(rhs_imm)) {
3447 // Sltiu sign-extends its 16-bit immediate operand before
3448 // the comparison and thus lets us compare directly with
3449 // unsigned values in the ranges [0, 0x7fff] and
3450 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3451 __ Sltiu(dst, lhs, rhs_imm);
3452 } else {
3453 if (use_imm) {
3454 rhs_reg = TMP;
3455 __ LoadConst64(rhs_reg, rhs_imm);
3456 }
3457 __ Sltu(dst, lhs, rhs_reg);
3458 }
3459 if (cond == kCondAE) {
3460 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3461 // only the sltu instruction but no sgeu.
3462 __ Xori(dst, dst, 1);
3463 }
3464 break;
3465
3466 case kCondBE:
3467 case kCondA:
3468 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3469 // Simulate lhs <= rhs via lhs < rhs + 1.
3470 // Note that this only works if rhs + 1 does not overflow
3471 // to 0, hence the check above.
3472 // Sltiu sign-extends its 16-bit immediate operand before
3473 // the comparison and thus lets us compare directly with
3474 // unsigned values in the ranges [0, 0x7fff] and
3475 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3476 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3477 if (cond == kCondA) {
3478 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3479 // only the sltiu instruction but no sgtiu.
3480 __ Xori(dst, dst, 1);
3481 }
3482 } else {
3483 if (use_imm) {
3484 rhs_reg = TMP;
3485 __ LoadConst64(rhs_reg, rhs_imm);
3486 }
3487 __ Sltu(dst, rhs_reg, lhs);
3488 if (cond == kCondBE) {
3489 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3490 // only the sltu instruction but no sleu.
3491 __ Xori(dst, dst, 1);
3492 }
3493 }
3494 break;
3495 }
3496}
3497
3498void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3499 bool is64bit,
3500 LocationSummary* locations,
3501 Mips64Label* label) {
3502 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3503 Location rhs_location = locations->InAt(1);
3504 GpuRegister rhs_reg = ZERO;
3505 int64_t rhs_imm = 0;
3506 bool use_imm = rhs_location.IsConstant();
3507 if (use_imm) {
3508 if (is64bit) {
3509 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3510 } else {
3511 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3512 }
3513 } else {
3514 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3515 }
3516
3517 if (use_imm && rhs_imm == 0) {
3518 switch (cond) {
3519 case kCondEQ:
3520 case kCondBE: // <= 0 if zero
3521 __ Beqzc(lhs, label);
3522 break;
3523 case kCondNE:
3524 case kCondA: // > 0 if non-zero
3525 __ Bnezc(lhs, label);
3526 break;
3527 case kCondLT:
3528 __ Bltzc(lhs, label);
3529 break;
3530 case kCondGE:
3531 __ Bgezc(lhs, label);
3532 break;
3533 case kCondLE:
3534 __ Blezc(lhs, label);
3535 break;
3536 case kCondGT:
3537 __ Bgtzc(lhs, label);
3538 break;
3539 case kCondB: // always false
3540 break;
3541 case kCondAE: // always true
3542 __ Bc(label);
3543 break;
3544 }
3545 } else {
3546 if (use_imm) {
3547 rhs_reg = TMP;
3548 __ LoadConst64(rhs_reg, rhs_imm);
3549 }
3550 switch (cond) {
3551 case kCondEQ:
3552 __ Beqc(lhs, rhs_reg, label);
3553 break;
3554 case kCondNE:
3555 __ Bnec(lhs, rhs_reg, label);
3556 break;
3557 case kCondLT:
3558 __ Bltc(lhs, rhs_reg, label);
3559 break;
3560 case kCondGE:
3561 __ Bgec(lhs, rhs_reg, label);
3562 break;
3563 case kCondLE:
3564 __ Bgec(rhs_reg, lhs, label);
3565 break;
3566 case kCondGT:
3567 __ Bltc(rhs_reg, lhs, label);
3568 break;
3569 case kCondB:
3570 __ Bltuc(lhs, rhs_reg, label);
3571 break;
3572 case kCondAE:
3573 __ Bgeuc(lhs, rhs_reg, label);
3574 break;
3575 case kCondBE:
3576 __ Bgeuc(rhs_reg, lhs, label);
3577 break;
3578 case kCondA:
3579 __ Bltuc(rhs_reg, lhs, label);
3580 break;
3581 }
3582 }
3583}
3584
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003585void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3586 bool gt_bias,
3587 Primitive::Type type,
3588 LocationSummary* locations) {
3589 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3590 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3591 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3592 if (type == Primitive::kPrimFloat) {
3593 switch (cond) {
3594 case kCondEQ:
3595 __ CmpEqS(FTMP, lhs, rhs);
3596 __ Mfc1(dst, FTMP);
3597 __ Andi(dst, dst, 1);
3598 break;
3599 case kCondNE:
3600 __ CmpEqS(FTMP, lhs, rhs);
3601 __ Mfc1(dst, FTMP);
3602 __ Addiu(dst, dst, 1);
3603 break;
3604 case kCondLT:
3605 if (gt_bias) {
3606 __ CmpLtS(FTMP, lhs, rhs);
3607 } else {
3608 __ CmpUltS(FTMP, lhs, rhs);
3609 }
3610 __ Mfc1(dst, FTMP);
3611 __ Andi(dst, dst, 1);
3612 break;
3613 case kCondLE:
3614 if (gt_bias) {
3615 __ CmpLeS(FTMP, lhs, rhs);
3616 } else {
3617 __ CmpUleS(FTMP, lhs, rhs);
3618 }
3619 __ Mfc1(dst, FTMP);
3620 __ Andi(dst, dst, 1);
3621 break;
3622 case kCondGT:
3623 if (gt_bias) {
3624 __ CmpUltS(FTMP, rhs, lhs);
3625 } else {
3626 __ CmpLtS(FTMP, rhs, lhs);
3627 }
3628 __ Mfc1(dst, FTMP);
3629 __ Andi(dst, dst, 1);
3630 break;
3631 case kCondGE:
3632 if (gt_bias) {
3633 __ CmpUleS(FTMP, rhs, lhs);
3634 } else {
3635 __ CmpLeS(FTMP, rhs, lhs);
3636 }
3637 __ Mfc1(dst, FTMP);
3638 __ Andi(dst, dst, 1);
3639 break;
3640 default:
3641 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3642 UNREACHABLE();
3643 }
3644 } else {
3645 DCHECK_EQ(type, Primitive::kPrimDouble);
3646 switch (cond) {
3647 case kCondEQ:
3648 __ CmpEqD(FTMP, lhs, rhs);
3649 __ Mfc1(dst, FTMP);
3650 __ Andi(dst, dst, 1);
3651 break;
3652 case kCondNE:
3653 __ CmpEqD(FTMP, lhs, rhs);
3654 __ Mfc1(dst, FTMP);
3655 __ Addiu(dst, dst, 1);
3656 break;
3657 case kCondLT:
3658 if (gt_bias) {
3659 __ CmpLtD(FTMP, lhs, rhs);
3660 } else {
3661 __ CmpUltD(FTMP, lhs, rhs);
3662 }
3663 __ Mfc1(dst, FTMP);
3664 __ Andi(dst, dst, 1);
3665 break;
3666 case kCondLE:
3667 if (gt_bias) {
3668 __ CmpLeD(FTMP, lhs, rhs);
3669 } else {
3670 __ CmpUleD(FTMP, lhs, rhs);
3671 }
3672 __ Mfc1(dst, FTMP);
3673 __ Andi(dst, dst, 1);
3674 break;
3675 case kCondGT:
3676 if (gt_bias) {
3677 __ CmpUltD(FTMP, rhs, lhs);
3678 } else {
3679 __ CmpLtD(FTMP, rhs, lhs);
3680 }
3681 __ Mfc1(dst, FTMP);
3682 __ Andi(dst, dst, 1);
3683 break;
3684 case kCondGE:
3685 if (gt_bias) {
3686 __ CmpUleD(FTMP, rhs, lhs);
3687 } else {
3688 __ CmpLeD(FTMP, rhs, lhs);
3689 }
3690 __ Mfc1(dst, FTMP);
3691 __ Andi(dst, dst, 1);
3692 break;
3693 default:
3694 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3695 UNREACHABLE();
3696 }
3697 }
3698}
3699
Alexey Frunze299a9392015-12-08 16:08:02 -08003700void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3701 bool gt_bias,
3702 Primitive::Type type,
3703 LocationSummary* locations,
3704 Mips64Label* label) {
3705 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3706 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3707 if (type == Primitive::kPrimFloat) {
3708 switch (cond) {
3709 case kCondEQ:
3710 __ CmpEqS(FTMP, lhs, rhs);
3711 __ Bc1nez(FTMP, label);
3712 break;
3713 case kCondNE:
3714 __ CmpEqS(FTMP, lhs, rhs);
3715 __ Bc1eqz(FTMP, label);
3716 break;
3717 case kCondLT:
3718 if (gt_bias) {
3719 __ CmpLtS(FTMP, lhs, rhs);
3720 } else {
3721 __ CmpUltS(FTMP, lhs, rhs);
3722 }
3723 __ Bc1nez(FTMP, label);
3724 break;
3725 case kCondLE:
3726 if (gt_bias) {
3727 __ CmpLeS(FTMP, lhs, rhs);
3728 } else {
3729 __ CmpUleS(FTMP, lhs, rhs);
3730 }
3731 __ Bc1nez(FTMP, label);
3732 break;
3733 case kCondGT:
3734 if (gt_bias) {
3735 __ CmpUltS(FTMP, rhs, lhs);
3736 } else {
3737 __ CmpLtS(FTMP, rhs, lhs);
3738 }
3739 __ Bc1nez(FTMP, label);
3740 break;
3741 case kCondGE:
3742 if (gt_bias) {
3743 __ CmpUleS(FTMP, rhs, lhs);
3744 } else {
3745 __ CmpLeS(FTMP, rhs, lhs);
3746 }
3747 __ Bc1nez(FTMP, label);
3748 break;
3749 default:
3750 LOG(FATAL) << "Unexpected non-floating-point condition";
3751 }
3752 } else {
3753 DCHECK_EQ(type, Primitive::kPrimDouble);
3754 switch (cond) {
3755 case kCondEQ:
3756 __ CmpEqD(FTMP, lhs, rhs);
3757 __ Bc1nez(FTMP, label);
3758 break;
3759 case kCondNE:
3760 __ CmpEqD(FTMP, lhs, rhs);
3761 __ Bc1eqz(FTMP, label);
3762 break;
3763 case kCondLT:
3764 if (gt_bias) {
3765 __ CmpLtD(FTMP, lhs, rhs);
3766 } else {
3767 __ CmpUltD(FTMP, lhs, rhs);
3768 }
3769 __ Bc1nez(FTMP, label);
3770 break;
3771 case kCondLE:
3772 if (gt_bias) {
3773 __ CmpLeD(FTMP, lhs, rhs);
3774 } else {
3775 __ CmpUleD(FTMP, lhs, rhs);
3776 }
3777 __ Bc1nez(FTMP, label);
3778 break;
3779 case kCondGT:
3780 if (gt_bias) {
3781 __ CmpUltD(FTMP, rhs, lhs);
3782 } else {
3783 __ CmpLtD(FTMP, rhs, lhs);
3784 }
3785 __ Bc1nez(FTMP, label);
3786 break;
3787 case kCondGE:
3788 if (gt_bias) {
3789 __ CmpUleD(FTMP, rhs, lhs);
3790 } else {
3791 __ CmpLeD(FTMP, rhs, lhs);
3792 }
3793 __ Bc1nez(FTMP, label);
3794 break;
3795 default:
3796 LOG(FATAL) << "Unexpected non-floating-point condition";
3797 }
3798 }
3799}
3800
Alexey Frunze4dda3372015-06-01 18:31:49 -07003801void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003802 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003803 Mips64Label* true_target,
3804 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003805 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003806
David Brazdil0debae72015-11-12 18:37:00 +00003807 if (true_target == nullptr && false_target == nullptr) {
3808 // Nothing to do. The code always falls through.
3809 return;
3810 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003811 // Constant condition, statically compared against "true" (integer value 1).
3812 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003813 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003814 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003815 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003817 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003818 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003819 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003820 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003821 }
David Brazdil0debae72015-11-12 18:37:00 +00003822 return;
3823 }
3824
3825 // The following code generates these patterns:
3826 // (1) true_target == nullptr && false_target != nullptr
3827 // - opposite condition true => branch to false_target
3828 // (2) true_target != nullptr && false_target == nullptr
3829 // - condition true => branch to true_target
3830 // (3) true_target != nullptr && false_target != nullptr
3831 // - condition true => branch to true_target
3832 // - branch to false_target
3833 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003834 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003835 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003836 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003837 if (true_target == nullptr) {
3838 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3839 } else {
3840 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3841 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003842 } else {
3843 // The condition instruction has not been materialized, use its inputs as
3844 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003845 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003846 Primitive::Type type = condition->InputAt(0)->GetType();
3847 LocationSummary* locations = cond->GetLocations();
3848 IfCondition if_cond = condition->GetCondition();
3849 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003850
David Brazdil0debae72015-11-12 18:37:00 +00003851 if (true_target == nullptr) {
3852 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003853 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003854 }
3855
Alexey Frunze299a9392015-12-08 16:08:02 -08003856 switch (type) {
3857 default:
3858 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3859 break;
3860 case Primitive::kPrimLong:
3861 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3862 break;
3863 case Primitive::kPrimFloat:
3864 case Primitive::kPrimDouble:
3865 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3866 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867 }
3868 }
David Brazdil0debae72015-11-12 18:37:00 +00003869
3870 // If neither branch falls through (case 3), the conditional branch to `true_target`
3871 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3872 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003873 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003874 }
3875}
3876
3877void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003879 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003880 locations->SetInAt(0, Location::RequiresRegister());
3881 }
3882}
3883
3884void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003885 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3886 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003887 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003888 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003889 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003890 nullptr : codegen_->GetLabelOf(false_successor);
3891 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003892}
3893
3894void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3895 LocationSummary* locations = new (GetGraph()->GetArena())
3896 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003897 InvokeRuntimeCallingConvention calling_convention;
3898 RegisterSet caller_saves = RegisterSet::Empty();
3899 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3900 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003901 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003902 locations->SetInAt(0, Location::RequiresRegister());
3903 }
3904}
3905
3906void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003907 SlowPathCodeMIPS64* slow_path =
3908 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003909 GenerateTestAndBranch(deoptimize,
3910 /* condition_input_index */ 0,
3911 slow_path->GetEntryLabel(),
3912 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003913}
3914
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003915void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3916 LocationSummary* locations = new (GetGraph()->GetArena())
3917 LocationSummary(flag, LocationSummary::kNoCall);
3918 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003919}
3920
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003921void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3922 __ LoadFromOffset(kLoadWord,
3923 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3924 SP,
3925 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003926}
3927
David Brazdil74eb1b22015-12-14 11:44:01 +00003928void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3930 if (Primitive::IsFloatingPointType(select->GetType())) {
3931 locations->SetInAt(0, Location::RequiresFpuRegister());
3932 locations->SetInAt(1, Location::RequiresFpuRegister());
3933 } else {
3934 locations->SetInAt(0, Location::RequiresRegister());
3935 locations->SetInAt(1, Location::RequiresRegister());
3936 }
3937 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3938 locations->SetInAt(2, Location::RequiresRegister());
3939 }
3940 locations->SetOut(Location::SameAsFirstInput());
3941}
3942
3943void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3944 LocationSummary* locations = select->GetLocations();
3945 Mips64Label false_target;
3946 GenerateTestAndBranch(select,
3947 /* condition_input_index */ 2,
3948 /* true_target */ nullptr,
3949 &false_target);
3950 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3951 __ Bind(&false_target);
3952}
3953
David Srbecky0cf44932015-12-09 14:09:59 +00003954void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3955 new (GetGraph()->GetArena()) LocationSummary(info);
3956}
3957
David Srbeckyd28f4a02016-03-14 17:14:24 +00003958void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3959 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003960}
3961
3962void CodeGeneratorMIPS64::GenerateNop() {
3963 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003964}
3965
Alexey Frunze4dda3372015-06-01 18:31:49 -07003966void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003967 const FieldInfo& field_info) {
3968 Primitive::Type field_type = field_info.GetFieldType();
3969 bool object_field_get_with_read_barrier =
3970 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3972 instruction,
3973 object_field_get_with_read_barrier
3974 ? LocationSummary::kCallOnSlowPath
3975 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07003976 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3977 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3978 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003979 locations->SetInAt(0, Location::RequiresRegister());
3980 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3981 locations->SetOut(Location::RequiresFpuRegister());
3982 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08003983 // The output overlaps in the case of an object field get with
3984 // read barriers enabled: we do not want the move to overwrite the
3985 // object's location, as we need it to emit the read barrier.
3986 locations->SetOut(Location::RequiresRegister(),
3987 object_field_get_with_read_barrier
3988 ? Location::kOutputOverlap
3989 : Location::kNoOutputOverlap);
3990 }
3991 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3992 // We need a temporary register for the read barrier marking slow
3993 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
3994 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995 }
3996}
3997
3998void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3999 const FieldInfo& field_info) {
4000 Primitive::Type type = field_info.GetFieldType();
4001 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004002 Location obj_loc = locations->InAt(0);
4003 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4004 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004005 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004006 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004007 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004008 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4009
Alexey Frunze4dda3372015-06-01 18:31:49 -07004010 switch (type) {
4011 case Primitive::kPrimBoolean:
4012 load_type = kLoadUnsignedByte;
4013 break;
4014 case Primitive::kPrimByte:
4015 load_type = kLoadSignedByte;
4016 break;
4017 case Primitive::kPrimShort:
4018 load_type = kLoadSignedHalfword;
4019 break;
4020 case Primitive::kPrimChar:
4021 load_type = kLoadUnsignedHalfword;
4022 break;
4023 case Primitive::kPrimInt:
4024 case Primitive::kPrimFloat:
4025 load_type = kLoadWord;
4026 break;
4027 case Primitive::kPrimLong:
4028 case Primitive::kPrimDouble:
4029 load_type = kLoadDoubleword;
4030 break;
4031 case Primitive::kPrimNot:
4032 load_type = kLoadUnsignedWord;
4033 break;
4034 case Primitive::kPrimVoid:
4035 LOG(FATAL) << "Unreachable type " << type;
4036 UNREACHABLE();
4037 }
4038 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004039 DCHECK(dst_loc.IsRegister());
4040 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4041 if (type == Primitive::kPrimNot) {
4042 // /* HeapReference<Object> */ dst = *(obj + offset)
4043 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4044 Location temp_loc = locations->GetTemp(0);
4045 // Note that a potential implicit null check is handled in this
4046 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4047 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4048 dst_loc,
4049 obj,
4050 offset,
4051 temp_loc,
4052 /* needs_null_check */ true);
4053 if (is_volatile) {
4054 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4055 }
4056 } else {
4057 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4058 if (is_volatile) {
4059 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4060 }
4061 // If read barriers are enabled, emit read barriers other than
4062 // Baker's using a slow path (and also unpoison the loaded
4063 // reference, if heap poisoning is enabled).
4064 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4065 }
4066 } else {
4067 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4068 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004069 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004070 DCHECK(dst_loc.IsFpuRegister());
4071 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004072 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004073 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004074
Alexey Frunze15958152017-02-09 19:08:30 -08004075 // Memory barriers, in the case of references, are handled in the
4076 // previous switch statement.
4077 if (is_volatile && (type != Primitive::kPrimNot)) {
4078 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004079 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004080}
4081
4082void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4083 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4084 LocationSummary* locations =
4085 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4086 locations->SetInAt(0, Location::RequiresRegister());
4087 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004088 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004089 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004090 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091 }
4092}
4093
4094void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004095 const FieldInfo& field_info,
4096 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004097 Primitive::Type type = field_info.GetFieldType();
4098 LocationSummary* locations = instruction->GetLocations();
4099 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004100 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004101 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004102 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004103 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4104 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004105 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4106
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107 switch (type) {
4108 case Primitive::kPrimBoolean:
4109 case Primitive::kPrimByte:
4110 store_type = kStoreByte;
4111 break;
4112 case Primitive::kPrimShort:
4113 case Primitive::kPrimChar:
4114 store_type = kStoreHalfword;
4115 break;
4116 case Primitive::kPrimInt:
4117 case Primitive::kPrimFloat:
4118 case Primitive::kPrimNot:
4119 store_type = kStoreWord;
4120 break;
4121 case Primitive::kPrimLong:
4122 case Primitive::kPrimDouble:
4123 store_type = kStoreDoubleword;
4124 break;
4125 case Primitive::kPrimVoid:
4126 LOG(FATAL) << "Unreachable type " << type;
4127 UNREACHABLE();
4128 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004129
Alexey Frunze15958152017-02-09 19:08:30 -08004130 if (is_volatile) {
4131 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4132 }
4133
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004134 if (value_location.IsConstant()) {
4135 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4136 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4137 } else {
4138 if (!Primitive::IsFloatingPointType(type)) {
4139 DCHECK(value_location.IsRegister());
4140 GpuRegister src = value_location.AsRegister<GpuRegister>();
4141 if (kPoisonHeapReferences && needs_write_barrier) {
4142 // Note that in the case where `value` is a null reference,
4143 // we do not enter this block, as a null reference does not
4144 // need poisoning.
4145 DCHECK_EQ(type, Primitive::kPrimNot);
4146 __ PoisonHeapReference(TMP, src);
4147 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4148 } else {
4149 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4150 }
4151 } else {
4152 DCHECK(value_location.IsFpuRegister());
4153 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4154 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4155 }
4156 }
Alexey Frunze15958152017-02-09 19:08:30 -08004157
Alexey Frunzec061de12017-02-14 13:27:23 -08004158 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004159 DCHECK(value_location.IsRegister());
4160 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004161 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004162 }
Alexey Frunze15958152017-02-09 19:08:30 -08004163
4164 if (is_volatile) {
4165 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4166 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004167}
4168
4169void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4170 HandleFieldGet(instruction, instruction->GetFieldInfo());
4171}
4172
4173void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4174 HandleFieldGet(instruction, instruction->GetFieldInfo());
4175}
4176
4177void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4178 HandleFieldSet(instruction, instruction->GetFieldInfo());
4179}
4180
4181void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004182 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004183}
4184
Alexey Frunze15958152017-02-09 19:08:30 -08004185void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4186 HInstruction* instruction,
4187 Location out,
4188 uint32_t offset,
4189 Location maybe_temp,
4190 ReadBarrierOption read_barrier_option) {
4191 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4192 if (read_barrier_option == kWithReadBarrier) {
4193 CHECK(kEmitCompilerReadBarrier);
4194 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4195 if (kUseBakerReadBarrier) {
4196 // Load with fast path based Baker's read barrier.
4197 // /* HeapReference<Object> */ out = *(out + offset)
4198 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4199 out,
4200 out_reg,
4201 offset,
4202 maybe_temp,
4203 /* needs_null_check */ false);
4204 } else {
4205 // Load with slow path based read barrier.
4206 // Save the value of `out` into `maybe_temp` before overwriting it
4207 // in the following move operation, as we will need it for the
4208 // read barrier below.
4209 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4210 // /* HeapReference<Object> */ out = *(out + offset)
4211 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4212 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4213 }
4214 } else {
4215 // Plain load with no read barrier.
4216 // /* HeapReference<Object> */ out = *(out + offset)
4217 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4218 __ MaybeUnpoisonHeapReference(out_reg);
4219 }
4220}
4221
4222void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4223 HInstruction* instruction,
4224 Location out,
4225 Location obj,
4226 uint32_t offset,
4227 Location maybe_temp,
4228 ReadBarrierOption read_barrier_option) {
4229 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4230 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4231 if (read_barrier_option == kWithReadBarrier) {
4232 CHECK(kEmitCompilerReadBarrier);
4233 if (kUseBakerReadBarrier) {
4234 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4235 // Load with fast path based Baker's read barrier.
4236 // /* HeapReference<Object> */ out = *(obj + offset)
4237 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4238 out,
4239 obj_reg,
4240 offset,
4241 maybe_temp,
4242 /* needs_null_check */ false);
4243 } else {
4244 // Load with slow path based read barrier.
4245 // /* HeapReference<Object> */ out = *(obj + offset)
4246 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4247 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4248 }
4249 } else {
4250 // Plain load with no read barrier.
4251 // /* HeapReference<Object> */ out = *(obj + offset)
4252 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4253 __ MaybeUnpoisonHeapReference(out_reg);
4254 }
4255}
4256
Alexey Frunzef63f5692016-12-13 17:43:11 -08004257void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004258 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004259 Location root,
4260 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004261 uint32_t offset,
4262 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004263 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004264 if (read_barrier_option == kWithReadBarrier) {
4265 DCHECK(kEmitCompilerReadBarrier);
4266 if (kUseBakerReadBarrier) {
4267 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4268 // Baker's read barrier are used:
4269 //
4270 // root = obj.field;
4271 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4272 // if (temp != null) {
4273 // root = temp(root)
4274 // }
4275
4276 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4277 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4278 static_assert(
4279 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4280 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4281 "have different sizes.");
4282 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4283 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4284 "have different sizes.");
4285
4286 // Slow path marking the GC root `root`.
4287 Location temp = Location::RegisterLocation(T9);
4288 SlowPathCodeMIPS64* slow_path =
4289 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4290 instruction,
4291 root,
4292 /*entrypoint*/ temp);
4293 codegen_->AddSlowPath(slow_path);
4294
4295 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4296 const int32_t entry_point_offset =
4297 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4298 // Loading the entrypoint does not require a load acquire since it is only changed when
4299 // threads are suspended or running a checkpoint.
4300 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4301 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4302 // checking GetIsGcMarking.
4303 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4304 __ Bind(slow_path->GetExitLabel());
4305 } else {
4306 // GC root loaded through a slow path for read barriers other
4307 // than Baker's.
4308 // /* GcRoot<mirror::Object>* */ root = obj + offset
4309 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4310 // /* mirror::Object* */ root = root->Read()
4311 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4312 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004313 } else {
4314 // Plain GC root load with no read barrier.
4315 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4316 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4317 // Note that GC roots are not affected by heap poisoning, thus we
4318 // do not have to unpoison `root_reg` here.
4319 }
4320}
4321
Alexey Frunze15958152017-02-09 19:08:30 -08004322void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4323 Location ref,
4324 GpuRegister obj,
4325 uint32_t offset,
4326 Location temp,
4327 bool needs_null_check) {
4328 DCHECK(kEmitCompilerReadBarrier);
4329 DCHECK(kUseBakerReadBarrier);
4330
4331 // /* HeapReference<Object> */ ref = *(obj + offset)
4332 Location no_index = Location::NoLocation();
4333 ScaleFactor no_scale_factor = TIMES_1;
4334 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4335 ref,
4336 obj,
4337 offset,
4338 no_index,
4339 no_scale_factor,
4340 temp,
4341 needs_null_check);
4342}
4343
4344void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4345 Location ref,
4346 GpuRegister obj,
4347 uint32_t data_offset,
4348 Location index,
4349 Location temp,
4350 bool needs_null_check) {
4351 DCHECK(kEmitCompilerReadBarrier);
4352 DCHECK(kUseBakerReadBarrier);
4353
4354 static_assert(
4355 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4356 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4357 // /* HeapReference<Object> */ ref =
4358 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4359 ScaleFactor scale_factor = TIMES_4;
4360 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4361 ref,
4362 obj,
4363 data_offset,
4364 index,
4365 scale_factor,
4366 temp,
4367 needs_null_check);
4368}
4369
4370void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4371 Location ref,
4372 GpuRegister obj,
4373 uint32_t offset,
4374 Location index,
4375 ScaleFactor scale_factor,
4376 Location temp,
4377 bool needs_null_check,
4378 bool always_update_field) {
4379 DCHECK(kEmitCompilerReadBarrier);
4380 DCHECK(kUseBakerReadBarrier);
4381
4382 // In slow path based read barriers, the read barrier call is
4383 // inserted after the original load. However, in fast path based
4384 // Baker's read barriers, we need to perform the load of
4385 // mirror::Object::monitor_ *before* the original reference load.
4386 // This load-load ordering is required by the read barrier.
4387 // The fast path/slow path (for Baker's algorithm) should look like:
4388 //
4389 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4390 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4391 // HeapReference<Object> ref = *src; // Original reference load.
4392 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4393 // if (is_gray) {
4394 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4395 // }
4396 //
4397 // Note: the original implementation in ReadBarrier::Barrier is
4398 // slightly more complex as it performs additional checks that we do
4399 // not do here for performance reasons.
4400
4401 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4402 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4403 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4404
4405 // /* int32_t */ monitor = obj->monitor_
4406 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4407 if (needs_null_check) {
4408 MaybeRecordImplicitNullCheck(instruction);
4409 }
4410 // /* LockWord */ lock_word = LockWord(monitor)
4411 static_assert(sizeof(LockWord) == sizeof(int32_t),
4412 "art::LockWord and int32_t have different sizes.");
4413
4414 __ Sync(0); // Barrier to prevent load-load reordering.
4415
4416 // The actual reference load.
4417 if (index.IsValid()) {
4418 // Load types involving an "index": ArrayGet,
4419 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4420 // intrinsics.
4421 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4422 if (index.IsConstant()) {
4423 size_t computed_offset =
4424 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4425 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4426 } else {
4427 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004428 if (scale_factor == TIMES_1) {
4429 __ Daddu(TMP, index_reg, obj);
4430 } else {
4431 __ Dlsa(TMP, index_reg, obj, scale_factor);
4432 }
Alexey Frunze15958152017-02-09 19:08:30 -08004433 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4434 }
4435 } else {
4436 // /* HeapReference<Object> */ ref = *(obj + offset)
4437 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4438 }
4439
4440 // Object* ref = ref_addr->AsMirrorPtr()
4441 __ MaybeUnpoisonHeapReference(ref_reg);
4442
4443 // Slow path marking the object `ref` when it is gray.
4444 SlowPathCodeMIPS64* slow_path;
4445 if (always_update_field) {
4446 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4447 // of the form `obj + field_offset`, where `obj` is a register and
4448 // `field_offset` is a register. Thus `offset` and `scale_factor`
4449 // above are expected to be null in this code path.
4450 DCHECK_EQ(offset, 0u);
4451 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4452 slow_path = new (GetGraph()->GetArena())
4453 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4454 ref,
4455 obj,
4456 /* field_offset */ index,
4457 temp_reg);
4458 } else {
4459 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4460 }
4461 AddSlowPath(slow_path);
4462
4463 // if (rb_state == ReadBarrier::GrayState())
4464 // ref = ReadBarrier::Mark(ref);
4465 // Given the numeric representation, it's enough to check the low bit of the
4466 // rb_state. We do that by shifting the bit into the sign bit (31) and
4467 // performing a branch on less than zero.
4468 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4469 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4470 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4471 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4472 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4473 __ Bind(slow_path->GetExitLabel());
4474}
4475
4476void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4477 Location out,
4478 Location ref,
4479 Location obj,
4480 uint32_t offset,
4481 Location index) {
4482 DCHECK(kEmitCompilerReadBarrier);
4483
4484 // Insert a slow path based read barrier *after* the reference load.
4485 //
4486 // If heap poisoning is enabled, the unpoisoning of the loaded
4487 // reference will be carried out by the runtime within the slow
4488 // path.
4489 //
4490 // Note that `ref` currently does not get unpoisoned (when heap
4491 // poisoning is enabled), which is alright as the `ref` argument is
4492 // not used by the artReadBarrierSlow entry point.
4493 //
4494 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4495 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4496 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4497 AddSlowPath(slow_path);
4498
4499 __ Bc(slow_path->GetEntryLabel());
4500 __ Bind(slow_path->GetExitLabel());
4501}
4502
4503void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4504 Location out,
4505 Location ref,
4506 Location obj,
4507 uint32_t offset,
4508 Location index) {
4509 if (kEmitCompilerReadBarrier) {
4510 // Baker's read barriers shall be handled by the fast path
4511 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4512 DCHECK(!kUseBakerReadBarrier);
4513 // If heap poisoning is enabled, unpoisoning will be taken care of
4514 // by the runtime within the slow path.
4515 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4516 } else if (kPoisonHeapReferences) {
4517 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4518 }
4519}
4520
4521void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4522 Location out,
4523 Location root) {
4524 DCHECK(kEmitCompilerReadBarrier);
4525
4526 // Insert a slow path based read barrier *after* the GC root load.
4527 //
4528 // Note that GC roots are not affected by heap poisoning, so we do
4529 // not need to do anything special for this here.
4530 SlowPathCodeMIPS64* slow_path =
4531 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4532 AddSlowPath(slow_path);
4533
4534 __ Bc(slow_path->GetEntryLabel());
4535 __ Bind(slow_path->GetExitLabel());
4536}
4537
Alexey Frunze4dda3372015-06-01 18:31:49 -07004538void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004539 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4540 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004541 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004542 switch (type_check_kind) {
4543 case TypeCheckKind::kExactCheck:
4544 case TypeCheckKind::kAbstractClassCheck:
4545 case TypeCheckKind::kClassHierarchyCheck:
4546 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004547 call_kind =
4548 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004549 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004550 break;
4551 case TypeCheckKind::kArrayCheck:
4552 case TypeCheckKind::kUnresolvedCheck:
4553 case TypeCheckKind::kInterfaceCheck:
4554 call_kind = LocationSummary::kCallOnSlowPath;
4555 break;
4556 }
4557
Alexey Frunze4dda3372015-06-01 18:31:49 -07004558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004559 if (baker_read_barrier_slow_path) {
4560 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4561 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004562 locations->SetInAt(0, Location::RequiresRegister());
4563 locations->SetInAt(1, Location::RequiresRegister());
4564 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004565 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004566 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004567 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004568}
4569
4570void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004571 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004572 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004573 Location obj_loc = locations->InAt(0);
4574 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004575 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004576 Location out_loc = locations->Out();
4577 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4578 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4579 DCHECK_LE(num_temps, 1u);
4580 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004581 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4582 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4583 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4584 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004585 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004586 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004587
4588 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004589 // Avoid this check if we know `obj` is not null.
4590 if (instruction->MustDoNullCheck()) {
4591 __ Move(out, ZERO);
4592 __ Beqzc(obj, &done);
4593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004594
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004595 switch (type_check_kind) {
4596 case TypeCheckKind::kExactCheck: {
4597 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004598 GenerateReferenceLoadTwoRegisters(instruction,
4599 out_loc,
4600 obj_loc,
4601 class_offset,
4602 maybe_temp_loc,
4603 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004604 // Classes must be equal for the instanceof to succeed.
4605 __ Xor(out, out, cls);
4606 __ Sltiu(out, out, 1);
4607 break;
4608 }
4609
4610 case TypeCheckKind::kAbstractClassCheck: {
4611 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004612 GenerateReferenceLoadTwoRegisters(instruction,
4613 out_loc,
4614 obj_loc,
4615 class_offset,
4616 maybe_temp_loc,
4617 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004618 // If the class is abstract, we eagerly fetch the super class of the
4619 // object to avoid doing a comparison we know will fail.
4620 Mips64Label loop;
4621 __ Bind(&loop);
4622 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004623 GenerateReferenceLoadOneRegister(instruction,
4624 out_loc,
4625 super_offset,
4626 maybe_temp_loc,
4627 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004628 // If `out` is null, we use it for the result, and jump to `done`.
4629 __ Beqzc(out, &done);
4630 __ Bnec(out, cls, &loop);
4631 __ LoadConst32(out, 1);
4632 break;
4633 }
4634
4635 case TypeCheckKind::kClassHierarchyCheck: {
4636 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004637 GenerateReferenceLoadTwoRegisters(instruction,
4638 out_loc,
4639 obj_loc,
4640 class_offset,
4641 maybe_temp_loc,
4642 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004643 // Walk over the class hierarchy to find a match.
4644 Mips64Label loop, success;
4645 __ Bind(&loop);
4646 __ Beqc(out, cls, &success);
4647 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004648 GenerateReferenceLoadOneRegister(instruction,
4649 out_loc,
4650 super_offset,
4651 maybe_temp_loc,
4652 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004653 __ Bnezc(out, &loop);
4654 // If `out` is null, we use it for the result, and jump to `done`.
4655 __ Bc(&done);
4656 __ Bind(&success);
4657 __ LoadConst32(out, 1);
4658 break;
4659 }
4660
4661 case TypeCheckKind::kArrayObjectCheck: {
4662 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004663 GenerateReferenceLoadTwoRegisters(instruction,
4664 out_loc,
4665 obj_loc,
4666 class_offset,
4667 maybe_temp_loc,
4668 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004669 // Do an exact check.
4670 Mips64Label success;
4671 __ Beqc(out, cls, &success);
4672 // Otherwise, we need to check that the object's class is a non-primitive array.
4673 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004674 GenerateReferenceLoadOneRegister(instruction,
4675 out_loc,
4676 component_offset,
4677 maybe_temp_loc,
4678 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004679 // If `out` is null, we use it for the result, and jump to `done`.
4680 __ Beqzc(out, &done);
4681 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4682 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4683 __ Sltiu(out, out, 1);
4684 __ Bc(&done);
4685 __ Bind(&success);
4686 __ LoadConst32(out, 1);
4687 break;
4688 }
4689
4690 case TypeCheckKind::kArrayCheck: {
4691 // No read barrier since the slow path will retry upon failure.
4692 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004693 GenerateReferenceLoadTwoRegisters(instruction,
4694 out_loc,
4695 obj_loc,
4696 class_offset,
4697 maybe_temp_loc,
4698 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004699 DCHECK(locations->OnlyCallsOnSlowPath());
4700 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4701 /* is_fatal */ false);
4702 codegen_->AddSlowPath(slow_path);
4703 __ Bnec(out, cls, slow_path->GetEntryLabel());
4704 __ LoadConst32(out, 1);
4705 break;
4706 }
4707
4708 case TypeCheckKind::kUnresolvedCheck:
4709 case TypeCheckKind::kInterfaceCheck: {
4710 // Note that we indeed only call on slow path, but we always go
4711 // into the slow path for the unresolved and interface check
4712 // cases.
4713 //
4714 // We cannot directly call the InstanceofNonTrivial runtime
4715 // entry point without resorting to a type checking slow path
4716 // here (i.e. by calling InvokeRuntime directly), as it would
4717 // require to assign fixed registers for the inputs of this
4718 // HInstanceOf instruction (following the runtime calling
4719 // convention), which might be cluttered by the potential first
4720 // read barrier emission at the beginning of this method.
4721 //
4722 // TODO: Introduce a new runtime entry point taking the object
4723 // to test (instead of its class) as argument, and let it deal
4724 // with the read barrier issues. This will let us refactor this
4725 // case of the `switch` code as it was previously (with a direct
4726 // call to the runtime not using a type checking slow path).
4727 // This should also be beneficial for the other cases above.
4728 DCHECK(locations->OnlyCallsOnSlowPath());
4729 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4730 /* is_fatal */ false);
4731 codegen_->AddSlowPath(slow_path);
4732 __ Bc(slow_path->GetEntryLabel());
4733 break;
4734 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004735 }
4736
4737 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004738
4739 if (slow_path != nullptr) {
4740 __ Bind(slow_path->GetExitLabel());
4741 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004742}
4743
4744void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4745 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4746 locations->SetOut(Location::ConstantLocation(constant));
4747}
4748
4749void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4750 // Will be generated at use site.
4751}
4752
4753void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4755 locations->SetOut(Location::ConstantLocation(constant));
4756}
4757
4758void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4759 // Will be generated at use site.
4760}
4761
Calin Juravle175dc732015-08-25 15:42:32 +01004762void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4763 // The trampoline uses the same calling convention as dex calling conventions,
4764 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4765 // the method_idx.
4766 HandleInvoke(invoke);
4767}
4768
4769void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4770 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4771}
4772
Alexey Frunze4dda3372015-06-01 18:31:49 -07004773void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4774 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4775 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4776}
4777
4778void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4779 HandleInvoke(invoke);
4780 // The register T0 is required to be used for the hidden argument in
4781 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4782 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4783}
4784
4785void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4786 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4787 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004788 Location receiver = invoke->GetLocations()->InAt(0);
4789 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004790 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004791
4792 // Set the hidden argument.
4793 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4794 invoke->GetDexMethodIndex());
4795
4796 // temp = object->GetClass();
4797 if (receiver.IsStackSlot()) {
4798 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4799 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4800 } else {
4801 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4802 }
4803 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004804 // Instead of simply (possibly) unpoisoning `temp` here, we should
4805 // emit a read barrier for the previous class reference load.
4806 // However this is not required in practice, as this is an
4807 // intermediate/temporary reference and because the current
4808 // concurrent copying collector keeps the from-space memory
4809 // intact/accessible until the end of the marking phase (the
4810 // concurrent copying collector may not in the future).
4811 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004812 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4813 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4814 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004815 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004816 // temp = temp->GetImtEntryAt(method_offset);
4817 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4818 // T9 = temp->GetEntryPoint();
4819 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4820 // T9();
4821 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004822 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004823 DCHECK(!codegen_->IsLeafMethod());
4824 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4825}
4826
4827void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004828 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4829 if (intrinsic.TryDispatch(invoke)) {
4830 return;
4831 }
4832
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833 HandleInvoke(invoke);
4834}
4835
4836void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004837 // Explicit clinit checks triggered by static invokes must have been pruned by
4838 // art::PrepareForRegisterAllocation.
4839 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004840
Chris Larsen3039e382015-08-26 07:54:08 -07004841 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4842 if (intrinsic.TryDispatch(invoke)) {
4843 return;
4844 }
4845
Alexey Frunze4dda3372015-06-01 18:31:49 -07004846 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847}
4848
Orion Hodsonac141392017-01-13 11:53:47 +00004849void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4850 HandleInvoke(invoke);
4851}
4852
4853void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4854 codegen_->GenerateInvokePolymorphicCall(invoke);
4855}
4856
Chris Larsen3039e382015-08-26 07:54:08 -07004857static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004858 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004859 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4860 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861 return true;
4862 }
4863 return false;
4864}
4865
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004866HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004867 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004868 bool fallback_load = false;
4869 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004870 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004871 case HLoadString::LoadKind::kBssEntry:
4872 DCHECK(!Runtime::Current()->UseJitCompilation());
4873 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004874 case HLoadString::LoadKind::kJitTableAddress:
4875 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004876 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004877 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004878 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko764d4542017-05-16 10:31:41 +01004879 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004880 }
4881 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004882 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004883 }
4884 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004885}
4886
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004887HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4888 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004889 bool fallback_load = false;
4890 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004891 case HLoadClass::LoadKind::kInvalid:
4892 LOG(FATAL) << "UNREACHABLE";
4893 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004894 case HLoadClass::LoadKind::kReferrersClass:
4895 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004896 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004897 case HLoadClass::LoadKind::kBssEntry:
4898 DCHECK(!Runtime::Current()->UseJitCompilation());
4899 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004900 case HLoadClass::LoadKind::kJitTableAddress:
4901 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004902 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004903 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004904 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004905 break;
4906 }
4907 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01004908 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004909 }
4910 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004911}
4912
Vladimir Markodc151b22015-10-15 18:02:30 +01004913HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4914 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004915 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004916 // On MIPS64 we support all dispatch types.
4917 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004918}
4919
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004920void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(
4921 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004922 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004923 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004924 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4925 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4926
Alexey Frunze19f6c692016-11-30 19:19:55 -08004927 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004928 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004929 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004930 uint32_t offset =
4931 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004932 __ LoadFromOffset(kLoadDoubleword,
4933 temp.AsRegister<GpuRegister>(),
4934 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004935 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004936 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004937 }
Vladimir Marko58155012015-08-19 12:49:41 +00004938 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004939 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004940 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004941 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4942 DCHECK(GetCompilerOptions().IsBootImage());
4943 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
4944 NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4945 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4946 __ Daddiu(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4947 break;
4948 }
Vladimir Marko58155012015-08-19 12:49:41 +00004949 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004950 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4951 kLoadDoubleword,
4952 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004953 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004954 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
4955 PcRelativePatchInfo* info = NewMethodBssEntryPatch(
4956 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze19f6c692016-11-30 19:19:55 -08004957 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4958 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4959 break;
4960 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004961 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4962 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4963 return; // No code pointer retrieval; the runtime performs the call directly.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004964 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004965 }
4966
Alexey Frunze19f6c692016-11-30 19:19:55 -08004967 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00004968 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004969 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00004970 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004971 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4972 // T9 = callee_method->entry_point_from_quick_compiled_code_;
4973 __ LoadFromOffset(kLoadDoubleword,
4974 T9,
4975 callee_method.AsRegister<GpuRegister>(),
4976 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004977 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004978 // T9()
4979 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004980 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00004981 break;
4982 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004983 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4984
Alexey Frunze4dda3372015-06-01 18:31:49 -07004985 DCHECK(!IsLeafMethod());
4986}
4987
4988void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004989 // Explicit clinit checks triggered by static invokes must have been pruned by
4990 // art::PrepareForRegisterAllocation.
4991 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004992
4993 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4994 return;
4995 }
4996
4997 LocationSummary* locations = invoke->GetLocations();
4998 codegen_->GenerateStaticOrDirectCall(invoke,
4999 locations->HasTemps()
5000 ? locations->GetTemp(0)
5001 : Location::NoLocation());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005002}
5003
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005004void CodeGeneratorMIPS64::GenerateVirtualCall(
5005 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005006 // Use the calling convention instead of the location of the receiver, as
5007 // intrinsics may have put the receiver in a different register. In the intrinsics
5008 // slow path, the arguments have been moved to the right place, so here we are
5009 // guaranteed that the receiver is the first register of the calling convention.
5010 InvokeDexCallingConvention calling_convention;
5011 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5012
Alexey Frunze53afca12015-11-05 16:34:23 -08005013 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005014 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5015 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5016 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005017 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005018
5019 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005020 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005021 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005022 // Instead of simply (possibly) unpoisoning `temp` here, we should
5023 // emit a read barrier for the previous class reference load.
5024 // However this is not required in practice, as this is an
5025 // intermediate/temporary reference and because the current
5026 // concurrent copying collector keeps the from-space memory
5027 // intact/accessible until the end of the marking phase (the
5028 // concurrent copying collector may not in the future).
5029 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005030 // temp = temp->GetMethodAt(method_offset);
5031 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5032 // T9 = temp->GetEntryPoint();
5033 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5034 // T9();
5035 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005036 __ Nop();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005037 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Alexey Frunze53afca12015-11-05 16:34:23 -08005038}
5039
5040void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5041 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5042 return;
5043 }
5044
5045 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005046 DCHECK(!codegen_->IsLeafMethod());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005047}
5048
5049void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005050 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005051 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005052 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005053 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5054 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005055 return;
5056 }
Vladimir Marko41559982017-01-06 14:04:23 +00005057 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005058
Alexey Frunze15958152017-02-09 19:08:30 -08005059 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5060 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005061 ? LocationSummary::kCallOnSlowPath
5062 : LocationSummary::kNoCall;
5063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005064 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5065 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5066 }
Vladimir Marko41559982017-01-06 14:04:23 +00005067 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005068 locations->SetInAt(0, Location::RequiresRegister());
5069 }
5070 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005071 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5072 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5073 // Rely on the type resolution or initialization and marking to save everything we need.
5074 RegisterSet caller_saves = RegisterSet::Empty();
5075 InvokeRuntimeCallingConvention calling_convention;
5076 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5077 locations->SetCustomSlowPathCallerSaves(caller_saves);
5078 } else {
5079 // For non-Baker read barrier we have a temp-clobbering call.
5080 }
5081 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005082}
5083
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005084// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5085// move.
5086void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005087 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005088 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00005089 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005090 return;
5091 }
Vladimir Marko41559982017-01-06 14:04:23 +00005092 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005093
Vladimir Marko41559982017-01-06 14:04:23 +00005094 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005095 Location out_loc = locations->Out();
5096 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5097 GpuRegister current_method_reg = ZERO;
5098 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005099 load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005100 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5101 }
5102
Alexey Frunze15958152017-02-09 19:08:30 -08005103 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5104 ? kWithoutReadBarrier
5105 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005106 bool generate_null_check = false;
5107 switch (load_kind) {
5108 case HLoadClass::LoadKind::kReferrersClass:
5109 DCHECK(!cls->CanCallRuntime());
5110 DCHECK(!cls->MustGenerateClinitCheck());
5111 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5112 GenerateGcRootFieldLoad(cls,
5113 out_loc,
5114 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005115 ArtMethod::DeclaringClassOffset().Int32Value(),
5116 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005117 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005118 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005119 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005120 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005121 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5122 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5123 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5124 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5125 break;
5126 }
5127 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005128 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005129 uint32_t address = dchecked_integral_cast<uint32_t>(
5130 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5131 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005132 __ LoadLiteral(out,
5133 kLoadUnsignedWord,
5134 codegen_->DeduplicateBootImageAddressLiteral(address));
5135 break;
5136 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005137 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005138 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005139 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005140 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005141 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005142 generate_null_check = true;
5143 break;
5144 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005145 case HLoadClass::LoadKind::kJitTableAddress:
5146 __ LoadLiteral(out,
5147 kLoadUnsignedWord,
5148 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5149 cls->GetTypeIndex(),
5150 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005151 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005152 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005153 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005154 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005155 LOG(FATAL) << "UNREACHABLE";
5156 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005157 }
5158
5159 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5160 DCHECK(cls->CanCallRuntime());
5161 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5162 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5163 codegen_->AddSlowPath(slow_path);
5164 if (generate_null_check) {
5165 __ Beqzc(out, slow_path->GetEntryLabel());
5166 }
5167 if (cls->MustGenerateClinitCheck()) {
5168 GenerateClassInitializationCheck(slow_path, out);
5169 } else {
5170 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005171 }
5172 }
5173}
5174
David Brazdilcb1c0552015-08-04 16:22:25 +01005175static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005176 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005177}
5178
Alexey Frunze4dda3372015-06-01 18:31:49 -07005179void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5180 LocationSummary* locations =
5181 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5182 locations->SetOut(Location::RequiresRegister());
5183}
5184
5185void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5186 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005187 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5188}
5189
5190void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5191 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5192}
5193
5194void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5195 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005196}
5197
Alexey Frunze4dda3372015-06-01 18:31:49 -07005198void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005199 HLoadString::LoadKind load_kind = load->GetLoadKind();
5200 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005202 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005203 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005204 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005205 } else {
5206 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005207 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5208 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5209 // Rely on the pResolveString and marking to save everything we need.
5210 RegisterSet caller_saves = RegisterSet::Empty();
5211 InvokeRuntimeCallingConvention calling_convention;
5212 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5213 locations->SetCustomSlowPathCallerSaves(caller_saves);
5214 } else {
5215 // For non-Baker read barrier we have a temp-clobbering call.
5216 }
5217 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005218 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005219}
5220
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005221// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5222// move.
5223void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005224 HLoadString::LoadKind load_kind = load->GetLoadKind();
5225 LocationSummary* locations = load->GetLocations();
5226 Location out_loc = locations->Out();
5227 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5228
5229 switch (load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005230 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5231 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5232 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005233 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005234 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5235 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5236 return; // No dex cache slow path.
5237 }
5238 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005239 uint32_t address = dchecked_integral_cast<uint32_t>(
5240 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5241 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005242 __ LoadLiteral(out,
5243 kLoadUnsignedWord,
5244 codegen_->DeduplicateBootImageAddressLiteral(address));
5245 return; // No dex cache slow path.
5246 }
5247 case HLoadString::LoadKind::kBssEntry: {
5248 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5249 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005250 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005251 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005252 GenerateGcRootFieldLoad(load,
5253 out_loc,
5254 out,
5255 /* placeholder */ 0x5678,
5256 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005257 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5258 codegen_->AddSlowPath(slow_path);
5259 __ Beqzc(out, slow_path->GetEntryLabel());
5260 __ Bind(slow_path->GetExitLabel());
5261 return;
5262 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005263 case HLoadString::LoadKind::kJitTableAddress:
5264 __ LoadLiteral(out,
5265 kLoadUnsignedWord,
5266 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5267 load->GetStringIndex(),
5268 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005269 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005270 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005271 default:
5272 break;
5273 }
5274
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005275 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005276 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005277 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005278 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005279 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5280 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5281 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005282}
5283
Alexey Frunze4dda3372015-06-01 18:31:49 -07005284void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5286 locations->SetOut(Location::ConstantLocation(constant));
5287}
5288
5289void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5290 // Will be generated at use site.
5291}
5292
5293void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5294 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005296 InvokeRuntimeCallingConvention calling_convention;
5297 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5298}
5299
5300void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005301 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005302 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005303 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005304 if (instruction->IsEnter()) {
5305 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5306 } else {
5307 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005309}
5310
5311void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5312 LocationSummary* locations =
5313 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5314 switch (mul->GetResultType()) {
5315 case Primitive::kPrimInt:
5316 case Primitive::kPrimLong:
5317 locations->SetInAt(0, Location::RequiresRegister());
5318 locations->SetInAt(1, Location::RequiresRegister());
5319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5320 break;
5321
5322 case Primitive::kPrimFloat:
5323 case Primitive::kPrimDouble:
5324 locations->SetInAt(0, Location::RequiresFpuRegister());
5325 locations->SetInAt(1, Location::RequiresFpuRegister());
5326 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5327 break;
5328
5329 default:
5330 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5331 }
5332}
5333
5334void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5335 Primitive::Type type = instruction->GetType();
5336 LocationSummary* locations = instruction->GetLocations();
5337
5338 switch (type) {
5339 case Primitive::kPrimInt:
5340 case Primitive::kPrimLong: {
5341 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5342 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5343 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5344 if (type == Primitive::kPrimInt)
5345 __ MulR6(dst, lhs, rhs);
5346 else
5347 __ Dmul(dst, lhs, rhs);
5348 break;
5349 }
5350 case Primitive::kPrimFloat:
5351 case Primitive::kPrimDouble: {
5352 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5353 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5354 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5355 if (type == Primitive::kPrimFloat)
5356 __ MulS(dst, lhs, rhs);
5357 else
5358 __ MulD(dst, lhs, rhs);
5359 break;
5360 }
5361 default:
5362 LOG(FATAL) << "Unexpected mul type " << type;
5363 }
5364}
5365
5366void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5367 LocationSummary* locations =
5368 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5369 switch (neg->GetResultType()) {
5370 case Primitive::kPrimInt:
5371 case Primitive::kPrimLong:
5372 locations->SetInAt(0, Location::RequiresRegister());
5373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5374 break;
5375
5376 case Primitive::kPrimFloat:
5377 case Primitive::kPrimDouble:
5378 locations->SetInAt(0, Location::RequiresFpuRegister());
5379 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5380 break;
5381
5382 default:
5383 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5384 }
5385}
5386
5387void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5388 Primitive::Type type = instruction->GetType();
5389 LocationSummary* locations = instruction->GetLocations();
5390
5391 switch (type) {
5392 case Primitive::kPrimInt:
5393 case Primitive::kPrimLong: {
5394 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5395 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5396 if (type == Primitive::kPrimInt)
5397 __ Subu(dst, ZERO, src);
5398 else
5399 __ Dsubu(dst, ZERO, src);
5400 break;
5401 }
5402 case Primitive::kPrimFloat:
5403 case Primitive::kPrimDouble: {
5404 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5405 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5406 if (type == Primitive::kPrimFloat)
5407 __ NegS(dst, src);
5408 else
5409 __ NegD(dst, src);
5410 break;
5411 }
5412 default:
5413 LOG(FATAL) << "Unexpected neg type " << type;
5414 }
5415}
5416
5417void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5418 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005420 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005421 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005422 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5423 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005424}
5425
5426void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005427 // Note: if heap poisoning is enabled, the entry point takes care
5428 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005429 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5430 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005431}
5432
5433void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5434 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005436 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005437 if (instruction->IsStringAlloc()) {
5438 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5439 } else {
5440 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005441 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005442 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5443}
5444
5445void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005446 // Note: if heap poisoning is enabled, the entry point takes care
5447 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005448 if (instruction->IsStringAlloc()) {
5449 // String is allocated through StringFactory. Call NewEmptyString entry point.
5450 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005451 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005452 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005453 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5454 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5455 __ Jalr(T9);
5456 __ Nop();
5457 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5458 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005459 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005460 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005461 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005462}
5463
5464void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5465 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5466 locations->SetInAt(0, Location::RequiresRegister());
5467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5468}
5469
5470void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5471 Primitive::Type type = instruction->GetType();
5472 LocationSummary* locations = instruction->GetLocations();
5473
5474 switch (type) {
5475 case Primitive::kPrimInt:
5476 case Primitive::kPrimLong: {
5477 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5478 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5479 __ Nor(dst, src, ZERO);
5480 break;
5481 }
5482
5483 default:
5484 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5485 }
5486}
5487
5488void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5490 locations->SetInAt(0, Location::RequiresRegister());
5491 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5492}
5493
5494void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5495 LocationSummary* locations = instruction->GetLocations();
5496 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5497 locations->InAt(0).AsRegister<GpuRegister>(),
5498 1);
5499}
5500
5501void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005502 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5503 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005504}
5505
Calin Juravle2ae48182016-03-16 14:05:09 +00005506void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5507 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005508 return;
5509 }
5510 Location obj = instruction->GetLocations()->InAt(0);
5511
5512 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005513 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005514}
5515
Calin Juravle2ae48182016-03-16 14:05:09 +00005516void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005517 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005518 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005519
5520 Location obj = instruction->GetLocations()->InAt(0);
5521
5522 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5523}
5524
5525void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005526 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005527}
5528
5529void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5530 HandleBinaryOp(instruction);
5531}
5532
5533void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5534 HandleBinaryOp(instruction);
5535}
5536
5537void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5538 LOG(FATAL) << "Unreachable";
5539}
5540
5541void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5542 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5543}
5544
5545void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5547 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5548 if (location.IsStackSlot()) {
5549 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5550 } else if (location.IsDoubleStackSlot()) {
5551 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5552 }
5553 locations->SetOut(location);
5554}
5555
5556void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5557 ATTRIBUTE_UNUSED) {
5558 // Nothing to do, the parameter is already at its location.
5559}
5560
5561void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5562 LocationSummary* locations =
5563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5564 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5565}
5566
5567void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5568 ATTRIBUTE_UNUSED) {
5569 // Nothing to do, the method is already at its location.
5570}
5571
5572void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005574 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005575 locations->SetInAt(i, Location::Any());
5576 }
5577 locations->SetOut(Location::Any());
5578}
5579
5580void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5581 LOG(FATAL) << "Unreachable";
5582}
5583
5584void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5585 Primitive::Type type = rem->GetResultType();
5586 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005587 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5588 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5590
5591 switch (type) {
5592 case Primitive::kPrimInt:
5593 case Primitive::kPrimLong:
5594 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005595 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5597 break;
5598
5599 case Primitive::kPrimFloat:
5600 case Primitive::kPrimDouble: {
5601 InvokeRuntimeCallingConvention calling_convention;
5602 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5603 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5604 locations->SetOut(calling_convention.GetReturnLocation(type));
5605 break;
5606 }
5607
5608 default:
5609 LOG(FATAL) << "Unexpected rem type " << type;
5610 }
5611}
5612
5613void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5614 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005615
5616 switch (type) {
5617 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005618 case Primitive::kPrimLong:
5619 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005621
5622 case Primitive::kPrimFloat:
5623 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005624 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5625 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005626 if (type == Primitive::kPrimFloat) {
5627 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5628 } else {
5629 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5630 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005631 break;
5632 }
5633 default:
5634 LOG(FATAL) << "Unexpected rem type " << type;
5635 }
5636}
5637
Igor Murashkind01745e2017-04-05 16:40:31 -07005638void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5639 constructor_fence->SetLocations(nullptr);
5640}
5641
5642void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5643 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5644 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5645}
5646
Alexey Frunze4dda3372015-06-01 18:31:49 -07005647void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5648 memory_barrier->SetLocations(nullptr);
5649}
5650
5651void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5652 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5653}
5654
5655void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5656 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5657 Primitive::Type return_type = ret->InputAt(0)->GetType();
5658 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5659}
5660
5661void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5662 codegen_->GenerateFrameExit();
5663}
5664
5665void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5666 ret->SetLocations(nullptr);
5667}
5668
5669void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5670 codegen_->GenerateFrameExit();
5671}
5672
Alexey Frunze92d90602015-12-18 18:16:36 -08005673void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5674 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005675}
5676
Alexey Frunze92d90602015-12-18 18:16:36 -08005677void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5678 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005679}
5680
Alexey Frunze4dda3372015-06-01 18:31:49 -07005681void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5682 HandleShift(shl);
5683}
5684
5685void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5686 HandleShift(shl);
5687}
5688
5689void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5690 HandleShift(shr);
5691}
5692
5693void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5694 HandleShift(shr);
5695}
5696
Alexey Frunze4dda3372015-06-01 18:31:49 -07005697void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5698 HandleBinaryOp(instruction);
5699}
5700
5701void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5702 HandleBinaryOp(instruction);
5703}
5704
5705void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5706 HandleFieldGet(instruction, instruction->GetFieldInfo());
5707}
5708
5709void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5710 HandleFieldGet(instruction, instruction->GetFieldInfo());
5711}
5712
5713void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5714 HandleFieldSet(instruction, instruction->GetFieldInfo());
5715}
5716
5717void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005718 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719}
5720
Calin Juravlee460d1d2015-09-29 04:52:17 +01005721void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5722 HUnresolvedInstanceFieldGet* instruction) {
5723 FieldAccessCallingConventionMIPS64 calling_convention;
5724 codegen_->CreateUnresolvedFieldLocationSummary(
5725 instruction, instruction->GetFieldType(), calling_convention);
5726}
5727
5728void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5729 HUnresolvedInstanceFieldGet* instruction) {
5730 FieldAccessCallingConventionMIPS64 calling_convention;
5731 codegen_->GenerateUnresolvedFieldAccess(instruction,
5732 instruction->GetFieldType(),
5733 instruction->GetFieldIndex(),
5734 instruction->GetDexPc(),
5735 calling_convention);
5736}
5737
5738void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5739 HUnresolvedInstanceFieldSet* instruction) {
5740 FieldAccessCallingConventionMIPS64 calling_convention;
5741 codegen_->CreateUnresolvedFieldLocationSummary(
5742 instruction, instruction->GetFieldType(), calling_convention);
5743}
5744
5745void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5746 HUnresolvedInstanceFieldSet* instruction) {
5747 FieldAccessCallingConventionMIPS64 calling_convention;
5748 codegen_->GenerateUnresolvedFieldAccess(instruction,
5749 instruction->GetFieldType(),
5750 instruction->GetFieldIndex(),
5751 instruction->GetDexPc(),
5752 calling_convention);
5753}
5754
5755void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5756 HUnresolvedStaticFieldGet* instruction) {
5757 FieldAccessCallingConventionMIPS64 calling_convention;
5758 codegen_->CreateUnresolvedFieldLocationSummary(
5759 instruction, instruction->GetFieldType(), calling_convention);
5760}
5761
5762void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5763 HUnresolvedStaticFieldGet* instruction) {
5764 FieldAccessCallingConventionMIPS64 calling_convention;
5765 codegen_->GenerateUnresolvedFieldAccess(instruction,
5766 instruction->GetFieldType(),
5767 instruction->GetFieldIndex(),
5768 instruction->GetDexPc(),
5769 calling_convention);
5770}
5771
5772void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5773 HUnresolvedStaticFieldSet* instruction) {
5774 FieldAccessCallingConventionMIPS64 calling_convention;
5775 codegen_->CreateUnresolvedFieldLocationSummary(
5776 instruction, instruction->GetFieldType(), calling_convention);
5777}
5778
5779void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5780 HUnresolvedStaticFieldSet* instruction) {
5781 FieldAccessCallingConventionMIPS64 calling_convention;
5782 codegen_->GenerateUnresolvedFieldAccess(instruction,
5783 instruction->GetFieldType(),
5784 instruction->GetFieldIndex(),
5785 instruction->GetDexPc(),
5786 calling_convention);
5787}
5788
Alexey Frunze4dda3372015-06-01 18:31:49 -07005789void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005790 LocationSummary* locations =
5791 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02005792 // In suspend check slow path, usually there are no caller-save registers at all.
5793 // If SIMD instructions are present, however, we force spilling all live SIMD
5794 // registers in full width (since the runtime only saves/restores lower part).
5795 locations->SetCustomSlowPathCallerSaves(
5796 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005797}
5798
5799void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5800 HBasicBlock* block = instruction->GetBlock();
5801 if (block->GetLoopInformation() != nullptr) {
5802 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5803 // The back edge will generate the suspend check.
5804 return;
5805 }
5806 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5807 // The goto will generate the suspend check.
5808 return;
5809 }
5810 GenerateSuspendCheck(instruction, nullptr);
5811}
5812
Alexey Frunze4dda3372015-06-01 18:31:49 -07005813void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5814 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005816 InvokeRuntimeCallingConvention calling_convention;
5817 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5818}
5819
5820void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005821 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005822 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5823}
5824
5825void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5826 Primitive::Type input_type = conversion->GetInputType();
5827 Primitive::Type result_type = conversion->GetResultType();
5828 DCHECK_NE(input_type, result_type);
5829
5830 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5831 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5832 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5833 }
5834
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005835 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5836
5837 if (Primitive::IsFloatingPointType(input_type)) {
5838 locations->SetInAt(0, Location::RequiresFpuRegister());
5839 } else {
5840 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005841 }
5842
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005843 if (Primitive::IsFloatingPointType(result_type)) {
5844 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005845 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005847 }
5848}
5849
5850void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5851 LocationSummary* locations = conversion->GetLocations();
5852 Primitive::Type result_type = conversion->GetResultType();
5853 Primitive::Type input_type = conversion->GetInputType();
5854
5855 DCHECK_NE(input_type, result_type);
5856
5857 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5858 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5859 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5860
5861 switch (result_type) {
5862 case Primitive::kPrimChar:
5863 __ Andi(dst, src, 0xFFFF);
5864 break;
5865 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005866 if (input_type == Primitive::kPrimLong) {
5867 // Type conversion from long to types narrower than int is a result of code
5868 // transformations. To avoid unpredictable results for SEB and SEH, we first
5869 // need to sign-extend the low 32-bit value into bits 32 through 63.
5870 __ Sll(dst, src, 0);
5871 __ Seb(dst, dst);
5872 } else {
5873 __ Seb(dst, src);
5874 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005875 break;
5876 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005877 if (input_type == Primitive::kPrimLong) {
5878 // Type conversion from long to types narrower than int is a result of code
5879 // transformations. To avoid unpredictable results for SEB and SEH, we first
5880 // need to sign-extend the low 32-bit value into bits 32 through 63.
5881 __ Sll(dst, src, 0);
5882 __ Seh(dst, dst);
5883 } else {
5884 __ Seh(dst, src);
5885 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005886 break;
5887 case Primitive::kPrimInt:
5888 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005889 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5890 // conversions, except when the input and output registers are the same and we are not
5891 // converting longs to shorter types. In these cases, do nothing.
5892 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5893 __ Sll(dst, src, 0);
5894 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005895 break;
5896
5897 default:
5898 LOG(FATAL) << "Unexpected type conversion from " << input_type
5899 << " to " << result_type;
5900 }
5901 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005902 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5903 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5904 if (input_type == Primitive::kPrimLong) {
5905 __ Dmtc1(src, FTMP);
5906 if (result_type == Primitive::kPrimFloat) {
5907 __ Cvtsl(dst, FTMP);
5908 } else {
5909 __ Cvtdl(dst, FTMP);
5910 }
5911 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005912 __ Mtc1(src, FTMP);
5913 if (result_type == Primitive::kPrimFloat) {
5914 __ Cvtsw(dst, FTMP);
5915 } else {
5916 __ Cvtdw(dst, FTMP);
5917 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005918 }
5919 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5920 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005921 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5922 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005923
5924 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00005925 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005926 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005927 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005928 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005929 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005930 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00005931 } else {
5932 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005933 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005934 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005935 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00005936 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005937 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00005938 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005939 } else if (Primitive::IsFloatingPointType(result_type) &&
5940 Primitive::IsFloatingPointType(input_type)) {
5941 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5942 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5943 if (result_type == Primitive::kPrimFloat) {
5944 __ Cvtsd(dst, src);
5945 } else {
5946 __ Cvtds(dst, src);
5947 }
5948 } else {
5949 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5950 << " to " << result_type;
5951 }
5952}
5953
5954void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
5955 HandleShift(ushr);
5956}
5957
5958void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
5959 HandleShift(ushr);
5960}
5961
5962void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
5963 HandleBinaryOp(instruction);
5964}
5965
5966void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
5967 HandleBinaryOp(instruction);
5968}
5969
5970void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5971 // Nothing to do, this should be removed during prepare for register allocator.
5972 LOG(FATAL) << "Unreachable";
5973}
5974
5975void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5976 // Nothing to do, this should be removed during prepare for register allocator.
5977 LOG(FATAL) << "Unreachable";
5978}
5979
5980void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005981 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005982}
5983
5984void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005985 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005986}
5987
5988void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005989 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005990}
5991
5992void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005993 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005994}
5995
5996void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005997 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005998}
5999
6000void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006001 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006002}
6003
6004void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006005 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006006}
6007
6008void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006009 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006010}
6011
6012void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006013 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006014}
6015
6016void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006017 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006018}
6019
6020void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006021 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006022}
6023
6024void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006025 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006026}
6027
Aart Bike9f37602015-10-09 11:15:55 -07006028void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006029 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006030}
6031
6032void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006033 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006034}
6035
6036void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006037 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006038}
6039
6040void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006041 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006042}
6043
6044void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006045 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006046}
6047
6048void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006049 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006050}
6051
6052void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006053 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006054}
6055
6056void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006057 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006058}
6059
Mark Mendellfe57faa2015-09-18 09:26:15 -04006060// Simple implementation of packed switch - generate cascaded compare/jumps.
6061void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6062 LocationSummary* locations =
6063 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6064 locations->SetInAt(0, Location::RequiresRegister());
6065}
6066
Alexey Frunze0960ac52016-12-20 17:24:59 -08006067void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6068 int32_t lower_bound,
6069 uint32_t num_entries,
6070 HBasicBlock* switch_block,
6071 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006072 // Create a set of compare/jumps.
6073 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006074 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006075 // Jump to default if index is negative
6076 // Note: We don't check the case that index is positive while value < lower_bound, because in
6077 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6078 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6079
Alexey Frunze0960ac52016-12-20 17:24:59 -08006080 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006081 // Jump to successors[0] if value == lower_bound.
6082 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6083 int32_t last_index = 0;
6084 for (; num_entries - last_index > 2; last_index += 2) {
6085 __ Addiu(temp_reg, temp_reg, -2);
6086 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6087 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6088 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6089 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6090 }
6091 if (num_entries - last_index == 2) {
6092 // The last missing case_value.
6093 __ Addiu(temp_reg, temp_reg, -1);
6094 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006095 }
6096
6097 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006098 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006099 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006100 }
6101}
6102
Alexey Frunze0960ac52016-12-20 17:24:59 -08006103void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6104 int32_t lower_bound,
6105 uint32_t num_entries,
6106 HBasicBlock* switch_block,
6107 HBasicBlock* default_block) {
6108 // Create a jump table.
6109 std::vector<Mips64Label*> labels(num_entries);
6110 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6111 for (uint32_t i = 0; i < num_entries; i++) {
6112 labels[i] = codegen_->GetLabelOf(successors[i]);
6113 }
6114 JumpTable* table = __ CreateJumpTable(std::move(labels));
6115
6116 // Is the value in range?
6117 __ Addiu32(TMP, value_reg, -lower_bound);
6118 __ LoadConst32(AT, num_entries);
6119 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6120
6121 // We are in the range of the table.
6122 // Load the target address from the jump table, indexing by the value.
6123 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006124 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006125 __ Lw(TMP, TMP, 0);
6126 // Compute the absolute target address by adding the table start address
6127 // (the table contains offsets to targets relative to its start).
6128 __ Daddu(TMP, TMP, AT);
6129 // And jump.
6130 __ Jr(TMP);
6131 __ Nop();
6132}
6133
6134void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6135 int32_t lower_bound = switch_instr->GetStartValue();
6136 uint32_t num_entries = switch_instr->GetNumEntries();
6137 LocationSummary* locations = switch_instr->GetLocations();
6138 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6139 HBasicBlock* switch_block = switch_instr->GetBlock();
6140 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6141
6142 if (num_entries > kPackedSwitchJumpTableThreshold) {
6143 GenTableBasedPackedSwitch(value_reg,
6144 lower_bound,
6145 num_entries,
6146 switch_block,
6147 default_block);
6148 } else {
6149 GenPackedSwitchWithCompares(value_reg,
6150 lower_bound,
6151 num_entries,
6152 switch_block,
6153 default_block);
6154 }
6155}
6156
Chris Larsenc9905a62017-03-13 17:06:18 -07006157void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6158 LocationSummary* locations =
6159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6160 locations->SetInAt(0, Location::RequiresRegister());
6161 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006162}
6163
Chris Larsenc9905a62017-03-13 17:06:18 -07006164void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6165 LocationSummary* locations = instruction->GetLocations();
6166 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6167 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6168 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6169 __ LoadFromOffset(kLoadDoubleword,
6170 locations->Out().AsRegister<GpuRegister>(),
6171 locations->InAt(0).AsRegister<GpuRegister>(),
6172 method_offset);
6173 } else {
6174 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6175 instruction->GetIndex(), kMips64PointerSize));
6176 __ LoadFromOffset(kLoadDoubleword,
6177 locations->Out().AsRegister<GpuRegister>(),
6178 locations->InAt(0).AsRegister<GpuRegister>(),
6179 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6180 __ LoadFromOffset(kLoadDoubleword,
6181 locations->Out().AsRegister<GpuRegister>(),
6182 locations->Out().AsRegister<GpuRegister>(),
6183 method_offset);
6184 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006185}
6186
Alexey Frunze4dda3372015-06-01 18:31:49 -07006187} // namespace mips64
6188} // namespace art