blob: 5cdff5a7bc73156d72a267c5c42d10b741ce1194 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 return next_location;
95}
96
97Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
98 return Mips64ReturnLocation(type);
99}
100
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100101// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
102#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700103#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
105class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100110 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
112 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000113 if (instruction_->CanThrowIntoCatchBlock()) {
114 // Live registers will be restored in the catch block if caught.
115 SaveLiveRegisters(codegen, instruction_->GetLocations());
116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
119 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
122 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
125 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100126 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
127 ? kQuickThrowStringBounds
128 : kQuickThrowArrayBounds;
129 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
132 }
133
Alexandre Rames8158f282015-08-07 10:26:17 +0100134 bool IsFatal() const OVERRIDE { return true; }
135
Roland Levillain46648892015-06-19 16:07:18 +0100136 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
137
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
140};
141
142class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
143 public:
Alexey Frunzec61c0762017-04-10 13:54:23 -0700144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction)
145 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146
147 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
148 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
149 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100150 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700151 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Roland Levillain46648892015-06-19 16:07:18 +0100156 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
157
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700159 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
160};
161
162class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
163 public:
164 LoadClassSlowPathMIPS64(HLoadClass* cls,
165 HInstruction* at,
166 uint32_t dex_pc,
167 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000168 : SlowPathCodeMIPS64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700169 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
170 }
171
172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000173 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700174 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
175
176 __ Bind(GetEntryLabel());
177 SaveLiveRegisters(codegen, locations);
178
179 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000180 dex::TypeIndex type_index = cls_->GetTypeIndex();
181 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100182 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
183 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000184 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700185 if (do_clinit_) {
186 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
187 } else {
188 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
189 }
190
191 // Move the class to the desired location.
192 Location out = locations->Out();
193 if (out.IsValid()) {
194 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000195 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700196 mips64_codegen->MoveLocation(out,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
198 type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700199 }
200
201 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000202 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
203 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
204 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
205 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000206 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000207 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000208 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
209 __ Sw(out.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
210 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700211 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 }
213
Roland Levillain46648892015-06-19 16:07:18 +0100214 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
215
Alexey Frunze4dda3372015-06-01 18:31:49 -0700216 private:
217 // The class this slow path will load.
218 HLoadClass* const cls_;
219
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 // The dex PC of `at_`.
221 const uint32_t dex_pc_;
222
223 // Whether to initialize the class.
224 const bool do_clinit_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227};
228
229class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000231 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 LocationSummary* locations = instruction_->GetLocations();
235 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237
238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
241 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800242 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000243 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100245 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700246 instruction_,
247 instruction_->GetDexPc(),
248 this);
249 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
250 Primitive::Type type = instruction_->GetType();
251 mips64_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700252 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700253 type);
254
255 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800256
257 // Store the resolved String to the BSS entry.
Alexey Frunzef63f5692016-12-13 17:43:11 -0800258 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunzef63f5692016-12-13 17:43:11 -0800259 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
260 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
261 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
262 __ Sw(out, AT, /* placeholder */ 0x5678);
263
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700264 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 }
266
Roland Levillain46648892015-06-19 16:07:18 +0100267 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
268
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700270 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
271};
272
273class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
274 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000275 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
279 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000280 if (instruction_->CanThrowIntoCatchBlock()) {
281 // Live registers will be restored in the catch block if caught.
282 SaveLiveRegisters(codegen, instruction_->GetLocations());
283 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100284 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700285 instruction_,
286 instruction_->GetDexPc(),
287 this);
288 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Roland Levillain46648892015-06-19 16:07:18 +0100293 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
294
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200305 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700306 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
307 __ Bind(GetEntryLabel());
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200308 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufc734082016-07-19 17:18:07 +0100309 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +0200311 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 }
317 }
318
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700319 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 DCHECK(successor_ == nullptr);
321 return &return_label_;
322 }
323
Roland Levillain46648892015-06-19 16:07:18 +0100324 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
325
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 // If not null, the block to branch to after the suspend check.
328 HBasicBlock* const successor_;
329
330 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700331 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332
333 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
334};
335
336class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
337 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800338 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
339 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
347 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
348
349 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800350 if (!is_fatal_) {
351 SaveLiveRegisters(codegen, locations);
352 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
359 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
362 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100364 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800365 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 Primitive::Type ret_type = instruction_->GetType();
367 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
368 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 } else {
370 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800371 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
372 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 }
374
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800375 if (!is_fatal_) {
376 RestoreLiveRegisters(codegen, locations);
377 __ Bc(GetExitLabel());
378 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 }
380
Roland Levillain46648892015-06-19 16:07:18 +0100381 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
382
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 bool IsFatal() const OVERRIDE { return is_fatal_; }
384
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800386 const bool is_fatal_;
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
389};
390
391class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
392 public:
Aart Bik42249c32016-01-07 15:33:50 -0800393 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395
396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800397 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100399 LocationSummary* locations = instruction_->GetLocations();
400 SaveLiveRegisters(codegen, locations);
401 InvokeRuntimeCallingConvention calling_convention;
402 __ LoadConst32(calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100404 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 }
407
Roland Levillain46648892015-06-19 16:07:18 +0100408 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
409
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
412};
413
Alexey Frunze15958152017-02-09 19:08:30 -0800414class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
415 public:
416 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
417
418 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
424 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
428 Primitive::kPrimNot,
429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
433 Primitive::kPrimInt,
434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
438 Primitive::kPrimNot,
439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
443 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
445 RestoreLiveRegisters(codegen, locations);
446 __ Bc(GetExitLabel());
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
450
451 private:
452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
453};
454
455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
466//
467// If `entrypoint` is a valid location it is assumed to already be
468// holding the entrypoint. The case where the entrypoint is passed in
469// is for the GcRoot read barrier.
470class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
471 public:
472 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
473 Location ref,
474 Location entrypoint = Location::NoLocation())
475 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
476 DCHECK(kEmitCompilerReadBarrier);
477 }
478
479 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
480
481 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
482 LocationSummary* locations = instruction_->GetLocations();
483 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
484 DCHECK(locations->CanCall());
485 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
486 DCHECK(instruction_->IsInstanceFieldGet() ||
487 instruction_->IsStaticFieldGet() ||
488 instruction_->IsArrayGet() ||
489 instruction_->IsArraySet() ||
490 instruction_->IsLoadClass() ||
491 instruction_->IsLoadString() ||
492 instruction_->IsInstanceOf() ||
493 instruction_->IsCheckCast() ||
494 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
495 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
496 << "Unexpected instruction in read barrier marking slow path: "
497 << instruction_->DebugName();
498
499 __ Bind(GetEntryLabel());
500 // No need to save live registers; it's taken care of by the
501 // entrypoint. Also, there is no need to update the stack mask,
502 // as this runtime call will not trigger a garbage collection.
503 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
504 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
505 (S2 <= ref_reg && ref_reg <= S7) ||
506 (ref_reg == S8)) << ref_reg;
507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in A0 and V0 respectively):
511 //
512 // A0 <- ref
513 // V0 <- ReadBarrierMark(A0)
514 // ref <- V0
515 //
516 // we just use rX (the register containing `ref`) as input and output
517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
521 if (entrypoint_.IsValid()) {
522 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
523 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
524 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
525 __ Nop();
526 } else {
527 int32_t entry_point_offset =
528 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
529 // This runtime call does not require a stack map.
530 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
531 instruction_,
532 this);
533 }
534 __ Bc(GetExitLabel());
535 }
536
537 private:
538 // The location (register) of the marked object reference.
539 const Location ref_;
540
541 // The location of the entrypoint if already loaded.
542 const Location entrypoint_;
543
544 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
545};
546
547// Slow path marking an object reference `ref` during a read barrier,
548// and if needed, atomically updating the field `obj.field` in the
549// object `obj` holding this reference after marking (contrary to
550// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
551// `obj.field`).
552//
553// This means that after the execution of this slow path, both `ref`
554// and `obj.field` will be up-to-date; i.e., after the flip, both will
555// hold the same to-space reference (unless another thread installed
556// another object reference (different from `ref`) in `obj.field`).
557class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
558 public:
559 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
560 Location ref,
561 GpuRegister obj,
562 Location field_offset,
563 GpuRegister temp1)
564 : SlowPathCodeMIPS64(instruction),
565 ref_(ref),
566 obj_(obj),
567 field_offset_(field_offset),
568 temp1_(temp1) {
569 DCHECK(kEmitCompilerReadBarrier);
570 }
571
572 const char* GetDescription() const OVERRIDE {
573 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
574 }
575
576 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
577 LocationSummary* locations = instruction_->GetLocations();
578 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
579 DCHECK(locations->CanCall());
580 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
581 // This slow path is only used by the UnsafeCASObject intrinsic.
582 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
583 << "Unexpected instruction in read barrier marking and field updating slow path: "
584 << instruction_->DebugName();
585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
587 DCHECK(field_offset_.IsRegister()) << field_offset_;
588
589 __ Bind(GetEntryLabel());
590
591 // Save the old reference.
592 // Note that we cannot use AT or TMP to save the old reference, as those
593 // are used by the code that follows, but we need the old reference after
594 // the call to the ReadBarrierMarkRegX entry point.
595 DCHECK_NE(temp1_, AT);
596 DCHECK_NE(temp1_, TMP);
597 __ Move(temp1_, ref_reg);
598
599 // No need to save live registers; it's taken care of by the
600 // entrypoint. Also, there is no need to update the stack mask,
601 // as this runtime call will not trigger a garbage collection.
602 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
603 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
604 (S2 <= ref_reg && ref_reg <= S7) ||
605 (ref_reg == S8)) << ref_reg;
606 // "Compact" slow path, saving two moves.
607 //
608 // Instead of using the standard runtime calling convention (input
609 // and output in A0 and V0 respectively):
610 //
611 // A0 <- ref
612 // V0 <- ReadBarrierMark(A0)
613 // ref <- V0
614 //
615 // we just use rX (the register containing `ref`) as input and output
616 // of a dedicated entrypoint:
617 //
618 // rX <- ReadBarrierMarkRegX(rX)
619 //
620 int32_t entry_point_offset =
621 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
622 // This runtime call does not require a stack map.
623 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
624 instruction_,
625 this);
626
627 // If the new reference is different from the old reference,
628 // update the field in the holder (`*(obj_ + field_offset_)`).
629 //
630 // Note that this field could also hold a different object, if
631 // another thread had concurrently changed it. In that case, the
632 // the compare-and-set (CAS) loop below would abort, leaving the
633 // field as-is.
634 Mips64Label done;
635 __ Beqc(temp1_, ref_reg, &done);
636
637 // Update the the holder's field atomically. This may fail if
638 // mutator updates before us, but it's OK. This is achieved
639 // using a strong compare-and-set (CAS) operation with relaxed
640 // memory synchronization ordering, where the expected value is
641 // the old reference and the desired value is the new reference.
642
643 // Convenience aliases.
644 GpuRegister base = obj_;
645 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
646 GpuRegister expected = temp1_;
647 GpuRegister value = ref_reg;
648 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
649 GpuRegister tmp = AT; // Value in memory.
650
651 __ Daddu(tmp_ptr, base, offset);
652
653 if (kPoisonHeapReferences) {
654 __ PoisonHeapReference(expected);
655 // Do not poison `value` if it is the same register as
656 // `expected`, which has just been poisoned.
657 if (value != expected) {
658 __ PoisonHeapReference(value);
659 }
660 }
661
662 // do {
663 // tmp = [r_ptr] - expected;
664 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
665
666 Mips64Label loop_head, exit_loop;
667 __ Bind(&loop_head);
668 __ Ll(tmp, tmp_ptr);
669 // The LL instruction sign-extends the 32-bit value, but
670 // 32-bit references must be zero-extended. Zero-extend `tmp`.
671 __ Dext(tmp, tmp, 0, 32);
672 __ Bnec(tmp, expected, &exit_loop);
673 __ Move(tmp, value);
674 __ Sc(tmp, tmp_ptr);
675 __ Beqzc(tmp, &loop_head);
676 __ Bind(&exit_loop);
677
678 if (kPoisonHeapReferences) {
679 __ UnpoisonHeapReference(expected);
680 // Do not unpoison `value` if it is the same register as
681 // `expected`, which has just been unpoisoned.
682 if (value != expected) {
683 __ UnpoisonHeapReference(value);
684 }
685 }
686
687 __ Bind(&done);
688 __ Bc(GetExitLabel());
689 }
690
691 private:
692 // The location (register) of the marked object reference.
693 const Location ref_;
694 // The register containing the object holding the marked object reference field.
695 const GpuRegister obj_;
696 // The location of the offset of the marked reference field within `obj_`.
697 Location field_offset_;
698
699 const GpuRegister temp1_;
700
701 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
702};
703
704// Slow path generating a read barrier for a heap reference.
705class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
706 public:
707 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
708 Location out,
709 Location ref,
710 Location obj,
711 uint32_t offset,
712 Location index)
713 : SlowPathCodeMIPS64(instruction),
714 out_(out),
715 ref_(ref),
716 obj_(obj),
717 offset_(offset),
718 index_(index) {
719 DCHECK(kEmitCompilerReadBarrier);
720 // If `obj` is equal to `out` or `ref`, it means the initial object
721 // has been overwritten by (or after) the heap object reference load
722 // to be instrumented, e.g.:
723 //
724 // __ LoadFromOffset(kLoadWord, out, out, offset);
725 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
726 //
727 // In that case, we have lost the information about the original
728 // object, and the emitted read barrier cannot work properly.
729 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
730 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
731 }
732
733 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
734 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
735 LocationSummary* locations = instruction_->GetLocations();
736 Primitive::Type type = Primitive::kPrimNot;
737 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
738 DCHECK(locations->CanCall());
739 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
740 DCHECK(instruction_->IsInstanceFieldGet() ||
741 instruction_->IsStaticFieldGet() ||
742 instruction_->IsArrayGet() ||
743 instruction_->IsInstanceOf() ||
744 instruction_->IsCheckCast() ||
745 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
746 << "Unexpected instruction in read barrier for heap reference slow path: "
747 << instruction_->DebugName();
748
749 __ Bind(GetEntryLabel());
750 SaveLiveRegisters(codegen, locations);
751
752 // We may have to change the index's value, but as `index_` is a
753 // constant member (like other "inputs" of this slow path),
754 // introduce a copy of it, `index`.
755 Location index = index_;
756 if (index_.IsValid()) {
757 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
758 if (instruction_->IsArrayGet()) {
759 // Compute the actual memory offset and store it in `index`.
760 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
761 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
762 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
763 // We are about to change the value of `index_reg` (see the
764 // calls to art::mips64::Mips64Assembler::Sll and
765 // art::mips64::MipsAssembler::Addiu32 below), but it has
766 // not been saved by the previous call to
767 // art::SlowPathCode::SaveLiveRegisters, as it is a
768 // callee-save register --
769 // art::SlowPathCode::SaveLiveRegisters does not consider
770 // callee-save registers, as it has been designed with the
771 // assumption that callee-save registers are supposed to be
772 // handled by the called function. So, as a callee-save
773 // register, `index_reg` _would_ eventually be saved onto
774 // the stack, but it would be too late: we would have
775 // changed its value earlier. Therefore, we manually save
776 // it here into another freely available register,
777 // `free_reg`, chosen of course among the caller-save
778 // registers (as a callee-save `free_reg` register would
779 // exhibit the same problem).
780 //
781 // Note we could have requested a temporary register from
782 // the register allocator instead; but we prefer not to, as
783 // this is a slow path, and we know we can find a
784 // caller-save register that is available.
785 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
786 __ Move(free_reg, index_reg);
787 index_reg = free_reg;
788 index = Location::RegisterLocation(index_reg);
789 } else {
790 // The initial register stored in `index_` has already been
791 // saved in the call to art::SlowPathCode::SaveLiveRegisters
792 // (as it is not a callee-save register), so we can freely
793 // use it.
794 }
795 // Shifting the index value contained in `index_reg` by the scale
796 // factor (2) cannot overflow in practice, as the runtime is
797 // unable to allocate object arrays with a size larger than
798 // 2^26 - 1 (that is, 2^28 - 4 bytes).
799 __ Sll(index_reg, index_reg, TIMES_4);
800 static_assert(
801 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
802 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
803 __ Addiu32(index_reg, index_reg, offset_);
804 } else {
805 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
806 // intrinsics, `index_` is not shifted by a scale factor of 2
807 // (as in the case of ArrayGet), as it is actually an offset
808 // to an object field within an object.
809 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
810 DCHECK(instruction_->GetLocations()->Intrinsified());
811 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
812 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
813 << instruction_->AsInvoke()->GetIntrinsic();
814 DCHECK_EQ(offset_, 0U);
815 DCHECK(index_.IsRegister());
816 }
817 }
818
819 // We're moving two or three locations to locations that could
820 // overlap, so we need a parallel move resolver.
821 InvokeRuntimeCallingConvention calling_convention;
822 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
823 parallel_move.AddMove(ref_,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
825 Primitive::kPrimNot,
826 nullptr);
827 parallel_move.AddMove(obj_,
828 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
829 Primitive::kPrimNot,
830 nullptr);
831 if (index.IsValid()) {
832 parallel_move.AddMove(index,
833 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
834 Primitive::kPrimInt,
835 nullptr);
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 } else {
838 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
839 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
840 }
841 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
842 instruction_,
843 instruction_->GetDexPc(),
844 this);
845 CheckEntrypointTypes<
846 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
847 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ Bc(GetExitLabel());
851 }
852
853 const char* GetDescription() const OVERRIDE {
854 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
855 }
856
857 private:
858 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
859 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
860 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
861 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
862 if (i != ref &&
863 i != obj &&
864 !codegen->IsCoreCalleeSaveRegister(i) &&
865 !codegen->IsBlockedCoreRegister(i)) {
866 return static_cast<GpuRegister>(i);
867 }
868 }
869 // We shall never fail to find a free caller-save register, as
870 // there are more than two core caller-save registers on MIPS64
871 // (meaning it is possible to find one which is different from
872 // `ref` and `obj`).
873 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
874 LOG(FATAL) << "Could not find a free caller-save register";
875 UNREACHABLE();
876 }
877
878 const Location out_;
879 const Location ref_;
880 const Location obj_;
881 const uint32_t offset_;
882 // An additional location containing an index to an array.
883 // Only used for HArrayGet and the UnsafeGetObject &
884 // UnsafeGetObjectVolatile intrinsics.
885 const Location index_;
886
887 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
888};
889
890// Slow path generating a read barrier for a GC root.
891class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
892 public:
893 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
894 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
895 DCHECK(kEmitCompilerReadBarrier);
896 }
897
898 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
899 LocationSummary* locations = instruction_->GetLocations();
900 Primitive::Type type = Primitive::kPrimNot;
901 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
902 DCHECK(locations->CanCall());
903 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
904 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
905 << "Unexpected instruction in read barrier for GC root slow path: "
906 << instruction_->DebugName();
907
908 __ Bind(GetEntryLabel());
909 SaveLiveRegisters(codegen, locations);
910
911 InvokeRuntimeCallingConvention calling_convention;
912 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
913 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
914 root_,
915 Primitive::kPrimNot);
916 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
917 instruction_,
918 instruction_->GetDexPc(),
919 this);
920 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
921 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
922
923 RestoreLiveRegisters(codegen, locations);
924 __ Bc(GetExitLabel());
925 }
926
927 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
928
929 private:
930 const Location out_;
931 const Location root_;
932
933 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
934};
935
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
937 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100938 const CompilerOptions& compiler_options,
939 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940 : CodeGenerator(graph,
941 kNumberOfGpuRegisters,
942 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000943 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
945 arraysize(kCoreCalleeSaves)),
946 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
947 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100948 compiler_options,
949 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100950 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 location_builder_(graph, this),
952 instruction_visitor_(graph, this),
953 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100954 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800955 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800956 uint32_literals_(std::less<uint32_t>(),
957 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800958 uint64_literals_(std::less<uint64_t>(),
959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800960 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800961 pc_relative_string_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)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800964 jit_string_patches_(StringReferenceValueComparator(),
965 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
966 jit_class_patches_(TypeReferenceValueComparator(),
967 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700968 // Save RA (containing the return address) to mimic Quick.
969 AddAllocatedRegister(Location::RegisterLocation(RA));
970}
971
972#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100973// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
974#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700975#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976
977void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700978 // Ensure that we fix up branches.
979 __ FinalizeCode();
980
981 // Adjust native pc offsets in stack maps.
982 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800983 uint32_t old_position =
984 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700985 uint32_t new_position = __ GetAdjustedPosition(old_position);
986 DCHECK_GE(new_position, old_position);
987 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
988 }
989
990 // Adjust pc offsets for the disassembly information.
991 if (disasm_info_ != nullptr) {
992 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
993 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
994 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
995 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
996 it.second.start = __ GetAdjustedPosition(it.second.start);
997 it.second.end = __ GetAdjustedPosition(it.second.end);
998 }
999 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1000 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1001 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1002 }
1003 }
1004
Alexey Frunze4dda3372015-06-01 18:31:49 -07001005 CodeGenerator::Finalize(allocator);
1006}
1007
1008Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1009 return codegen_->GetAssembler();
1010}
1011
1012void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001013 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1015}
1016
1017void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001018 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001019 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1020}
1021
1022void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1023 // Pop reg
1024 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026}
1027
1028void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1029 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001030 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031 __ Sd(GpuRegister(reg), SP, 0);
1032}
1033
1034void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1035 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1036 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1037 // Allocate a scratch register other than TMP, if available.
1038 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1039 // automatically unspilled when the scratch scope object is destroyed).
1040 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1041 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001042 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001043 __ LoadFromOffset(load_type,
1044 GpuRegister(ensure_scratch.GetRegister()),
1045 SP,
1046 index1 + stack_offset);
1047 __ LoadFromOffset(load_type,
1048 TMP,
1049 SP,
1050 index2 + stack_offset);
1051 __ StoreToOffset(store_type,
1052 GpuRegister(ensure_scratch.GetRegister()),
1053 SP,
1054 index2 + stack_offset);
1055 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1056}
1057
1058static dwarf::Reg DWARFReg(GpuRegister reg) {
1059 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1060}
1061
David Srbeckyba702002016-02-01 18:15:29 +00001062static dwarf::Reg DWARFReg(FpuRegister reg) {
1063 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1064}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001065
1066void CodeGeneratorMIPS64::GenerateFrameEntry() {
1067 __ Bind(&frame_entry_label_);
1068
1069 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1070
1071 if (do_overflow_check) {
1072 __ LoadFromOffset(kLoadWord,
1073 ZERO,
1074 SP,
1075 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1076 RecordPcInfo(nullptr, 0);
1077 }
1078
Alexey Frunze4dda3372015-06-01 18:31:49 -07001079 if (HasEmptyFrame()) {
1080 return;
1081 }
1082
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001083 // Make sure the frame size isn't unreasonably large.
1084 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1085 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1086 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087
1088 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001089
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001090 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 __ IncreaseFrameSize(ofs);
1092
1093 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1094 GpuRegister reg = kCoreCalleeSaves[i];
1095 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001096 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001097 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 __ cfi().RelOffset(DWARFReg(reg), ofs);
1099 }
1100 }
1101
1102 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1103 FpuRegister reg = kFpuCalleeSaves[i];
1104 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001105 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001107 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001108 }
1109 }
1110
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001111 // Save the current method if we need it. Note that we do not
1112 // do this in HCurrentMethod, as the instruction might have been removed
1113 // in the SSA graph.
1114 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001115 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001116 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001117
1118 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1119 // Initialize should_deoptimize flag to 0.
1120 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001122}
1123
1124void CodeGeneratorMIPS64::GenerateFrameExit() {
1125 __ cfi().RememberState();
1126
Alexey Frunze4dda3372015-06-01 18:31:49 -07001127 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001129
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001130 // For better instruction scheduling restore RA before other registers.
1131 uint32_t ofs = GetFrameSize();
1132 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001133 GpuRegister reg = kCoreCalleeSaves[i];
1134 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001135 ofs -= kMips64DoublewordSize;
1136 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 __ cfi().Restore(DWARFReg(reg));
1138 }
1139 }
1140
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001141 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1142 FpuRegister reg = kFpuCalleeSaves[i];
1143 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1144 ofs -= kMips64DoublewordSize;
1145 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1146 __ cfi().Restore(DWARFReg(reg));
1147 }
1148 }
1149
1150 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 }
1152
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001153 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001154
1155 __ cfi().RestoreState();
1156 __ cfi().DefCFAOffset(GetFrameSize());
1157}
1158
1159void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1160 __ Bind(GetLabelOf(block));
1161}
1162
1163void CodeGeneratorMIPS64::MoveLocation(Location destination,
1164 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001166 if (source.Equals(destination)) {
1167 return;
1168 }
1169
1170 // A valid move can always be inferred from the destination and source
1171 // locations. When moving from and to a register, the argument type can be
1172 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001173 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174 DCHECK_EQ(unspecified_type, false);
1175
1176 if (destination.IsRegister() || destination.IsFpuRegister()) {
1177 if (unspecified_type) {
1178 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1179 if (source.IsStackSlot() ||
1180 (src_cst != nullptr && (src_cst->IsIntConstant()
1181 || src_cst->IsFloatConstant()
1182 || src_cst->IsNullConstant()))) {
1183 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001184 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001185 } else {
1186 // If the source is a double stack slot or a 64bit constant, a 64bit
1187 // type is appropriate. Else the source is a register, and since the
1188 // type has not been specified, we chose a 64bit type to force a 64bit
1189 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001190 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 }
1192 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001193 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1194 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1196 // Move to GPR/FPR from stack
1197 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001198 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 __ LoadFpuFromOffset(load_type,
1200 destination.AsFpuRegister<FpuRegister>(),
1201 SP,
1202 source.GetStackIndex());
1203 } else {
1204 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1205 __ LoadFromOffset(load_type,
1206 destination.AsRegister<GpuRegister>(),
1207 SP,
1208 source.GetStackIndex());
1209 }
1210 } else if (source.IsConstant()) {
1211 // Move to GPR/FPR from constant
1212 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001213 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 gpr = destination.AsRegister<GpuRegister>();
1215 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001216 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001217 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1218 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1219 gpr = ZERO;
1220 } else {
1221 __ LoadConst32(gpr, value);
1222 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001224 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1225 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1226 gpr = ZERO;
1227 } else {
1228 __ LoadConst64(gpr, value);
1229 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001230 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001234 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1235 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 if (destination.IsRegister()) {
1238 // Move to GPR from GPR
1239 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1240 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 DCHECK(destination.IsFpuRegister());
1242 if (Primitive::Is64BitType(dst_type)) {
1243 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1244 } else {
1245 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1246 }
1247 }
1248 } else if (source.IsFpuRegister()) {
1249 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1253 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1256 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else {
1258 DCHECK(destination.IsRegister());
1259 if (Primitive::Is64BitType(dst_type)) {
1260 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1261 } else {
1262 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001264 }
1265 }
1266 } else { // The destination is not a register. It must be a stack slot.
1267 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1268 if (source.IsRegister() || source.IsFpuRegister()) {
1269 if (unspecified_type) {
1270 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001272 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 }
1275 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1277 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 // Move to stack from GPR/FPR
1279 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1280 if (source.IsRegister()) {
1281 __ StoreToOffset(store_type,
1282 source.AsRegister<GpuRegister>(),
1283 SP,
1284 destination.GetStackIndex());
1285 } else {
1286 __ StoreFpuToOffset(store_type,
1287 source.AsFpuRegister<FpuRegister>(),
1288 SP,
1289 destination.GetStackIndex());
1290 }
1291 } else if (source.IsConstant()) {
1292 // Move to stack from constant
1293 HConstant* src_cst = source.GetConstant();
1294 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001295 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001296 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001297 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1298 if (value != 0) {
1299 gpr = TMP;
1300 __ LoadConst32(gpr, value);
1301 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001302 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001303 DCHECK(destination.IsDoubleStackSlot());
1304 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1305 if (value != 0) {
1306 gpr = TMP;
1307 __ LoadConst64(gpr, value);
1308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001310 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 } else {
1312 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1313 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1314 // Move to stack from stack
1315 if (destination.IsStackSlot()) {
1316 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1317 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1318 } else {
1319 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1320 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1321 }
1322 }
1323 }
1324}
1325
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001326void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001327 DCHECK(!loc1.IsConstant());
1328 DCHECK(!loc2.IsConstant());
1329
1330 if (loc1.Equals(loc2)) {
1331 return;
1332 }
1333
1334 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1335 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1336 bool is_fp_reg1 = loc1.IsFpuRegister();
1337 bool is_fp_reg2 = loc2.IsFpuRegister();
1338
1339 if (loc2.IsRegister() && loc1.IsRegister()) {
1340 // Swap 2 GPRs
1341 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1342 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1343 __ Move(TMP, r2);
1344 __ Move(r2, r1);
1345 __ Move(r1, TMP);
1346 } else if (is_fp_reg2 && is_fp_reg1) {
1347 // Swap 2 FPRs
1348 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1349 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001350 if (type == Primitive::kPrimFloat) {
1351 __ MovS(FTMP, r1);
1352 __ MovS(r1, r2);
1353 __ MovS(r2, FTMP);
1354 } else {
1355 DCHECK_EQ(type, Primitive::kPrimDouble);
1356 __ MovD(FTMP, r1);
1357 __ MovD(r1, r2);
1358 __ MovD(r2, FTMP);
1359 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001360 } else if (is_slot1 != is_slot2) {
1361 // Swap GPR/FPR and stack slot
1362 Location reg_loc = is_slot1 ? loc2 : loc1;
1363 Location mem_loc = is_slot1 ? loc1 : loc2;
1364 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1365 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1366 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1367 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1368 if (reg_loc.IsFpuRegister()) {
1369 __ StoreFpuToOffset(store_type,
1370 reg_loc.AsFpuRegister<FpuRegister>(),
1371 SP,
1372 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001373 if (mem_loc.IsStackSlot()) {
1374 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1375 } else {
1376 DCHECK(mem_loc.IsDoubleStackSlot());
1377 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1378 }
1379 } else {
1380 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1381 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1382 }
1383 } else if (is_slot1 && is_slot2) {
1384 move_resolver_.Exchange(loc1.GetStackIndex(),
1385 loc2.GetStackIndex(),
1386 loc1.IsDoubleStackSlot());
1387 } else {
1388 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1389 }
1390}
1391
Calin Juravle175dc732015-08-25 15:42:32 +01001392void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1393 DCHECK(location.IsRegister());
1394 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1395}
1396
Calin Juravlee460d1d2015-09-29 04:52:17 +01001397void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1398 if (location.IsRegister()) {
1399 locations->AddTemp(location);
1400 } else {
1401 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1402 }
1403}
1404
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001405void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1406 GpuRegister value,
1407 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001408 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001409 GpuRegister card = AT;
1410 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001411 if (value_can_be_null) {
1412 __ Beqzc(value, &done);
1413 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 __ LoadFromOffset(kLoadDoubleword,
1415 card,
1416 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001417 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1419 __ Daddu(temp, card, temp);
1420 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001421 if (value_can_be_null) {
1422 __ Bind(&done);
1423 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001424}
1425
Alexey Frunze19f6c692016-11-30 19:19:55 -08001426template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1427inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1428 const ArenaDeque<PcRelativePatchInfo>& infos,
1429 ArenaVector<LinkerPatch>* linker_patches) {
1430 for (const PcRelativePatchInfo& info : infos) {
1431 const DexFile& dex_file = info.target_dex_file;
1432 size_t offset_or_index = info.offset_or_index;
1433 DCHECK(info.pc_rel_label.IsBound());
1434 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1435 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
1436 }
1437}
1438
1439void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1440 DCHECK(linker_patches->empty());
1441 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -08001442 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001443 pc_relative_string_patches_.size() +
1444 pc_relative_type_patches_.size() +
Vladimir Marko764d4542017-05-16 10:31:41 +01001445 type_bss_entry_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001446 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001447 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1448 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001449 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001450 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001451 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1452 linker_patches);
1453 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001454 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1455 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001456 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1457 linker_patches);
1458 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001459 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1460 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001461 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001462}
1463
1464CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001465 const DexFile& dex_file, dex::StringIndex string_index) {
1466 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001467}
1468
1469CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1470 const DexFile& dex_file, dex::TypeIndex type_index) {
1471 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001472}
1473
Vladimir Marko1998cd02017-01-13 13:02:58 +00001474CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1475 const DexFile& dex_file, dex::TypeIndex type_index) {
1476 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1477}
1478
Alexey Frunze19f6c692016-11-30 19:19:55 -08001479CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1480 const DexFile& dex_file, uint32_t element_offset) {
1481 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1482}
1483
Alexey Frunze19f6c692016-11-30 19:19:55 -08001484CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1485 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1486 patches->emplace_back(dex_file, offset_or_index);
1487 return &patches->back();
1488}
1489
Alexey Frunzef63f5692016-12-13 17:43:11 -08001490Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1491 return map->GetOrCreate(
1492 value,
1493 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1494}
1495
Alexey Frunze19f6c692016-11-30 19:19:55 -08001496Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1497 return uint64_literals_.GetOrCreate(
1498 value,
1499 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1500}
1501
Alexey Frunzef63f5692016-12-13 17:43:11 -08001502Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001503 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001504}
1505
Alexey Frunze19f6c692016-11-30 19:19:55 -08001506void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1507 GpuRegister out) {
1508 __ Bind(&info->pc_rel_label);
1509 // Add the high half of a 32-bit offset to PC.
1510 __ Auipc(out, /* placeholder */ 0x1234);
1511 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001512 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001513}
1514
Alexey Frunze627c1a02017-01-30 19:28:14 -08001515Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1516 dex::StringIndex string_index,
1517 Handle<mirror::String> handle) {
1518 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1519 reinterpret_cast64<uint64_t>(handle.GetReference()));
1520 return jit_string_patches_.GetOrCreate(
1521 StringReference(&dex_file, string_index),
1522 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1523}
1524
1525Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1526 dex::TypeIndex type_index,
1527 Handle<mirror::Class> handle) {
1528 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1529 reinterpret_cast64<uint64_t>(handle.GetReference()));
1530 return jit_class_patches_.GetOrCreate(
1531 TypeReference(&dex_file, type_index),
1532 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1533}
1534
1535void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1536 const uint8_t* roots_data,
1537 const Literal* literal,
1538 uint64_t index_in_table) const {
1539 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1540 uintptr_t address =
1541 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1542 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1543}
1544
1545void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1546 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001547 const StringReference& string_reference = entry.first;
1548 Literal* table_entry_literal = entry.second;
1549 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001550 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001551 uint64_t index_in_table = it->second;
1552 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001553 }
1554 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001555 const TypeReference& type_reference = entry.first;
1556 Literal* table_entry_literal = entry.second;
1557 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001558 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001559 uint64_t index_in_table = it->second;
1560 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001561 }
1562}
1563
David Brazdil58282f42016-01-14 12:45:10 +00001564void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001565 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1566 blocked_core_registers_[ZERO] = true;
1567 blocked_core_registers_[K0] = true;
1568 blocked_core_registers_[K1] = true;
1569 blocked_core_registers_[GP] = true;
1570 blocked_core_registers_[SP] = true;
1571 blocked_core_registers_[RA] = true;
1572
Lazar Trsicd9672662015-09-03 17:33:01 +02001573 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1574 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001575 blocked_core_registers_[AT] = true;
1576 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001577 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001578 blocked_fpu_registers_[FTMP] = true;
1579
1580 // Reserve suspend and thread registers.
1581 blocked_core_registers_[S0] = true;
1582 blocked_core_registers_[TR] = true;
1583
1584 // Reserve T9 for function calls
1585 blocked_core_registers_[T9] = true;
1586
Goran Jakovljevic782be112016-06-21 12:39:04 +02001587 if (GetGraph()->IsDebuggable()) {
1588 // Stubs do not save callee-save floating point registers. If the graph
1589 // is debuggable, we need to deal with these registers differently. For
1590 // now, just block them.
1591 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1592 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001594 }
1595}
1596
Alexey Frunze4dda3372015-06-01 18:31:49 -07001597size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1598 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001599 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001600}
1601
1602size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1603 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001604 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001605}
1606
1607size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001608 __ StoreFpuToOffset(GetGraph()->HasSIMD() ? kStoreQuadword : kStoreDoubleword,
1609 FpuRegister(reg_id),
1610 SP,
1611 stack_index);
1612 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001613}
1614
1615size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Goran Jakovljevicd8b6a532017-04-20 11:42:30 +02001616 __ LoadFpuFromOffset(GetGraph()->HasSIMD() ? kLoadQuadword : kLoadDoubleword,
1617 FpuRegister(reg_id),
1618 SP,
1619 stack_index);
1620 return GetFloatingPointSpillSlotSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001621}
1622
1623void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001624 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001625}
1626
1627void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001628 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001629}
1630
Calin Juravle175dc732015-08-25 15:42:32 +01001631void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001632 HInstruction* instruction,
1633 uint32_t dex_pc,
1634 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001635 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001636 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001637 if (EntrypointRequiresStackMap(entrypoint)) {
1638 RecordPcInfo(instruction, dex_pc, slow_path);
1639 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001640}
1641
Alexey Frunze15958152017-02-09 19:08:30 -08001642void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1643 HInstruction* instruction,
1644 SlowPathCode* slow_path) {
1645 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1646 GenerateInvokeRuntime(entry_point_offset);
1647}
1648
1649void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1650 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1651 __ Jalr(T9);
1652 __ Nop();
1653}
1654
Alexey Frunze4dda3372015-06-01 18:31:49 -07001655void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1656 GpuRegister class_reg) {
1657 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1658 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1659 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001660 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1661 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001662 __ Bind(slow_path->GetExitLabel());
1663}
1664
1665void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1666 __ Sync(0); // only stype 0 is supported
1667}
1668
1669void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1670 HBasicBlock* successor) {
1671 SuspendCheckSlowPathMIPS64* slow_path =
1672 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1673 codegen_->AddSlowPath(slow_path);
1674
1675 __ LoadFromOffset(kLoadUnsignedHalfword,
1676 TMP,
1677 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001678 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001679 if (successor == nullptr) {
1680 __ Bnezc(TMP, slow_path->GetEntryLabel());
1681 __ Bind(slow_path->GetReturnLabel());
1682 } else {
1683 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001684 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685 // slow_path will return to GetLabelOf(successor).
1686 }
1687}
1688
1689InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1690 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001691 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 assembler_(codegen->GetAssembler()),
1693 codegen_(codegen) {}
1694
1695void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1696 DCHECK_EQ(instruction->InputCount(), 2U);
1697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1698 Primitive::Type type = instruction->GetResultType();
1699 switch (type) {
1700 case Primitive::kPrimInt:
1701 case Primitive::kPrimLong: {
1702 locations->SetInAt(0, Location::RequiresRegister());
1703 HInstruction* right = instruction->InputAt(1);
1704 bool can_use_imm = false;
1705 if (right->IsConstant()) {
1706 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1707 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1708 can_use_imm = IsUint<16>(imm);
1709 } else if (instruction->IsAdd()) {
1710 can_use_imm = IsInt<16>(imm);
1711 } else {
1712 DCHECK(instruction->IsSub());
1713 can_use_imm = IsInt<16>(-imm);
1714 }
1715 }
1716 if (can_use_imm)
1717 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1718 else
1719 locations->SetInAt(1, Location::RequiresRegister());
1720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1721 }
1722 break;
1723
1724 case Primitive::kPrimFloat:
1725 case Primitive::kPrimDouble:
1726 locations->SetInAt(0, Location::RequiresFpuRegister());
1727 locations->SetInAt(1, Location::RequiresFpuRegister());
1728 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1729 break;
1730
1731 default:
1732 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1733 }
1734}
1735
1736void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1737 Primitive::Type type = instruction->GetType();
1738 LocationSummary* locations = instruction->GetLocations();
1739
1740 switch (type) {
1741 case Primitive::kPrimInt:
1742 case Primitive::kPrimLong: {
1743 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1744 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1745 Location rhs_location = locations->InAt(1);
1746
1747 GpuRegister rhs_reg = ZERO;
1748 int64_t rhs_imm = 0;
1749 bool use_imm = rhs_location.IsConstant();
1750 if (use_imm) {
1751 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1752 } else {
1753 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1754 }
1755
1756 if (instruction->IsAnd()) {
1757 if (use_imm)
1758 __ Andi(dst, lhs, rhs_imm);
1759 else
1760 __ And(dst, lhs, rhs_reg);
1761 } else if (instruction->IsOr()) {
1762 if (use_imm)
1763 __ Ori(dst, lhs, rhs_imm);
1764 else
1765 __ Or(dst, lhs, rhs_reg);
1766 } else if (instruction->IsXor()) {
1767 if (use_imm)
1768 __ Xori(dst, lhs, rhs_imm);
1769 else
1770 __ Xor(dst, lhs, rhs_reg);
1771 } else if (instruction->IsAdd()) {
1772 if (type == Primitive::kPrimInt) {
1773 if (use_imm)
1774 __ Addiu(dst, lhs, rhs_imm);
1775 else
1776 __ Addu(dst, lhs, rhs_reg);
1777 } else {
1778 if (use_imm)
1779 __ Daddiu(dst, lhs, rhs_imm);
1780 else
1781 __ Daddu(dst, lhs, rhs_reg);
1782 }
1783 } else {
1784 DCHECK(instruction->IsSub());
1785 if (type == Primitive::kPrimInt) {
1786 if (use_imm)
1787 __ Addiu(dst, lhs, -rhs_imm);
1788 else
1789 __ Subu(dst, lhs, rhs_reg);
1790 } else {
1791 if (use_imm)
1792 __ Daddiu(dst, lhs, -rhs_imm);
1793 else
1794 __ Dsubu(dst, lhs, rhs_reg);
1795 }
1796 }
1797 break;
1798 }
1799 case Primitive::kPrimFloat:
1800 case Primitive::kPrimDouble: {
1801 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1802 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1803 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1804 if (instruction->IsAdd()) {
1805 if (type == Primitive::kPrimFloat)
1806 __ AddS(dst, lhs, rhs);
1807 else
1808 __ AddD(dst, lhs, rhs);
1809 } else if (instruction->IsSub()) {
1810 if (type == Primitive::kPrimFloat)
1811 __ SubS(dst, lhs, rhs);
1812 else
1813 __ SubD(dst, lhs, rhs);
1814 } else {
1815 LOG(FATAL) << "Unexpected floating-point binary operation";
1816 }
1817 break;
1818 }
1819 default:
1820 LOG(FATAL) << "Unexpected binary operation type " << type;
1821 }
1822}
1823
1824void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001825 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826
1827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1828 Primitive::Type type = instr->GetResultType();
1829 switch (type) {
1830 case Primitive::kPrimInt:
1831 case Primitive::kPrimLong: {
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001834 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001835 break;
1836 }
1837 default:
1838 LOG(FATAL) << "Unexpected shift type " << type;
1839 }
1840}
1841
1842void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001843 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844 LocationSummary* locations = instr->GetLocations();
1845 Primitive::Type type = instr->GetType();
1846
1847 switch (type) {
1848 case Primitive::kPrimInt:
1849 case Primitive::kPrimLong: {
1850 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1851 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1852 Location rhs_location = locations->InAt(1);
1853
1854 GpuRegister rhs_reg = ZERO;
1855 int64_t rhs_imm = 0;
1856 bool use_imm = rhs_location.IsConstant();
1857 if (use_imm) {
1858 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1859 } else {
1860 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1861 }
1862
1863 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001864 uint32_t shift_value = rhs_imm &
1865 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001866
Alexey Frunze92d90602015-12-18 18:16:36 -08001867 if (shift_value == 0) {
1868 if (dst != lhs) {
1869 __ Move(dst, lhs);
1870 }
1871 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001872 if (instr->IsShl()) {
1873 __ Sll(dst, lhs, shift_value);
1874 } else if (instr->IsShr()) {
1875 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001876 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001877 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001878 } else {
1879 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 }
1881 } else {
1882 if (shift_value < 32) {
1883 if (instr->IsShl()) {
1884 __ Dsll(dst, lhs, shift_value);
1885 } else if (instr->IsShr()) {
1886 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001887 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001889 } else {
1890 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001891 }
1892 } else {
1893 shift_value -= 32;
1894 if (instr->IsShl()) {
1895 __ Dsll32(dst, lhs, shift_value);
1896 } else if (instr->IsShr()) {
1897 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001898 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001899 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001900 } else {
1901 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902 }
1903 }
1904 }
1905 } else {
1906 if (type == Primitive::kPrimInt) {
1907 if (instr->IsShl()) {
1908 __ Sllv(dst, lhs, rhs_reg);
1909 } else if (instr->IsShr()) {
1910 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001911 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001912 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001913 } else {
1914 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001915 }
1916 } else {
1917 if (instr->IsShl()) {
1918 __ Dsllv(dst, lhs, rhs_reg);
1919 } else if (instr->IsShr()) {
1920 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001921 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001923 } else {
1924 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001925 }
1926 }
1927 }
1928 break;
1929 }
1930 default:
1931 LOG(FATAL) << "Unexpected shift operation type " << type;
1932 }
1933}
1934
1935void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1936 HandleBinaryOp(instruction);
1937}
1938
1939void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1940 HandleBinaryOp(instruction);
1941}
1942
1943void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1944 HandleBinaryOp(instruction);
1945}
1946
1947void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1948 HandleBinaryOp(instruction);
1949}
1950
1951void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001952 Primitive::Type type = instruction->GetType();
1953 bool object_array_get_with_read_barrier =
1954 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001955 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08001956 new (GetGraph()->GetArena()) LocationSummary(instruction,
1957 object_array_get_with_read_barrier
1958 ? LocationSummary::kCallOnSlowPath
1959 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001960 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1961 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1962 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08001965 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001966 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1967 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08001968 // The output overlaps in the case of an object array get with
1969 // read barriers enabled: we do not want the move to overwrite the
1970 // array's location, as we need it to emit the read barrier.
1971 locations->SetOut(Location::RequiresRegister(),
1972 object_array_get_with_read_barrier
1973 ? Location::kOutputOverlap
1974 : Location::kNoOutputOverlap);
1975 }
1976 // We need a temporary register for the read barrier marking slow
1977 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
1978 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1979 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001980 }
1981}
1982
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001983static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
1984 auto null_checker = [codegen, instruction]() {
1985 codegen->MaybeRecordImplicitNullCheck(instruction);
1986 };
1987 return null_checker;
1988}
1989
Alexey Frunze4dda3372015-06-01 18:31:49 -07001990void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1991 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08001992 Location obj_loc = locations->InAt(0);
1993 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
1994 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001995 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001996 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001997 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001998
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001999 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002000 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2001 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002 switch (type) {
2003 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002004 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002005 if (index.IsConstant()) {
2006 size_t offset =
2007 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002008 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002009 } else {
2010 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002011 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002012 }
2013 break;
2014 }
2015
2016 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002017 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002018 if (index.IsConstant()) {
2019 size_t offset =
2020 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002021 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002022 } else {
2023 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002024 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002025 }
2026 break;
2027 }
2028
2029 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002030 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002031 if (index.IsConstant()) {
2032 size_t offset =
2033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002034 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002036 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002037 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 }
2039 break;
2040 }
2041
2042 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002043 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002044 if (maybe_compressed_char_at) {
2045 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002046 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002047 __ Dext(TMP, TMP, 0, 1);
2048 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2049 "Expecting 0=compressed, 1=uncompressed");
2050 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002051 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002052 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2053 if (maybe_compressed_char_at) {
2054 Mips64Label uncompressed_load, done;
2055 __ Bnezc(TMP, &uncompressed_load);
2056 __ LoadFromOffset(kLoadUnsignedByte,
2057 out,
2058 obj,
2059 data_offset + (const_index << TIMES_1));
2060 __ Bc(&done);
2061 __ Bind(&uncompressed_load);
2062 __ LoadFromOffset(kLoadUnsignedHalfword,
2063 out,
2064 obj,
2065 data_offset + (const_index << TIMES_2));
2066 __ Bind(&done);
2067 } else {
2068 __ LoadFromOffset(kLoadUnsignedHalfword,
2069 out,
2070 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002071 data_offset + (const_index << TIMES_2),
2072 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002073 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002075 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2076 if (maybe_compressed_char_at) {
2077 Mips64Label uncompressed_load, done;
2078 __ Bnezc(TMP, &uncompressed_load);
2079 __ Daddu(TMP, obj, index_reg);
2080 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2081 __ Bc(&done);
2082 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002083 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002084 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2085 __ Bind(&done);
2086 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002087 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002088 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002089 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090 }
2091 break;
2092 }
2093
Alexey Frunze15958152017-02-09 19:08:30 -08002094 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002095 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002096 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002097 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2098 if (index.IsConstant()) {
2099 size_t offset =
2100 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002101 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002103 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002104 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002105 }
2106 break;
2107 }
2108
Alexey Frunze15958152017-02-09 19:08:30 -08002109 case Primitive::kPrimNot: {
2110 static_assert(
2111 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2112 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2113 // /* HeapReference<Object> */ out =
2114 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2115 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2116 Location temp = locations->GetTemp(0);
2117 // Note that a potential implicit null check is handled in this
2118 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2119 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2120 out_loc,
2121 obj,
2122 data_offset,
2123 index,
2124 temp,
2125 /* needs_null_check */ true);
2126 } else {
2127 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2128 if (index.IsConstant()) {
2129 size_t offset =
2130 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2131 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2132 // If read barriers are enabled, emit read barriers other than
2133 // Baker's using a slow path (and also unpoison the loaded
2134 // reference, if heap poisoning is enabled).
2135 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2136 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002137 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002138 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2139 // If read barriers are enabled, emit read barriers other than
2140 // Baker's using a slow path (and also unpoison the loaded
2141 // reference, if heap poisoning is enabled).
2142 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2143 out_loc,
2144 out_loc,
2145 obj_loc,
2146 data_offset,
2147 index);
2148 }
2149 }
2150 break;
2151 }
2152
Alexey Frunze4dda3372015-06-01 18:31:49 -07002153 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002154 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155 if (index.IsConstant()) {
2156 size_t offset =
2157 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002158 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002159 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002160 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002161 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002167 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002168 if (index.IsConstant()) {
2169 size_t offset =
2170 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002171 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002172 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002173 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002174 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002175 }
2176 break;
2177 }
2178
2179 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002180 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181 if (index.IsConstant()) {
2182 size_t offset =
2183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002184 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002186 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002187 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002188 }
2189 break;
2190 }
2191
2192 case Primitive::kPrimVoid:
2193 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2194 UNREACHABLE();
2195 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002196}
2197
2198void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2199 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2202}
2203
2204void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2205 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002206 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2208 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2209 __ LoadFromOffset(kLoadWord, out, obj, offset);
2210 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002211 // Mask out compression flag from String's array length.
2212 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2213 __ Srl(out, out, 1u);
2214 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215}
2216
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002217Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2218 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2219 ? Location::ConstantLocation(instruction->AsConstant())
2220 : Location::RequiresRegister();
2221}
2222
2223Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2224 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2225 // We can store a non-zero float or double constant without first loading it into the FPU,
2226 // but we should only prefer this if the constant has a single use.
2227 if (instruction->IsConstant() &&
2228 (instruction->AsConstant()->IsZeroBitPattern() ||
2229 instruction->GetUses().HasExactlyOneElement())) {
2230 return Location::ConstantLocation(instruction->AsConstant());
2231 // Otherwise fall through and require an FPU register for the constant.
2232 }
2233 return Location::RequiresFpuRegister();
2234}
2235
Alexey Frunze4dda3372015-06-01 18:31:49 -07002236void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002237 Primitive::Type value_type = instruction->GetComponentType();
2238
2239 bool needs_write_barrier =
2240 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2241 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2242
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2244 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002245 may_need_runtime_call_for_type_check ?
2246 LocationSummary::kCallOnSlowPath :
2247 LocationSummary::kNoCall);
2248
2249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2251 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2252 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002253 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002254 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2255 }
2256 if (needs_write_barrier) {
2257 // Temporary register for the write barrier.
2258 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002259 }
2260}
2261
2262void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2263 LocationSummary* locations = instruction->GetLocations();
2264 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2265 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002266 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002267 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002268 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 bool needs_write_barrier =
2270 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002271 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002272 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002273
2274 switch (value_type) {
2275 case Primitive::kPrimBoolean:
2276 case Primitive::kPrimByte: {
2277 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002278 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002279 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002280 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002281 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2282 }
2283 if (value_location.IsConstant()) {
2284 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2285 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2286 } else {
2287 GpuRegister value = value_location.AsRegister<GpuRegister>();
2288 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002289 }
2290 break;
2291 }
2292
2293 case Primitive::kPrimShort:
2294 case Primitive::kPrimChar: {
2295 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002296 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002297 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002298 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002299 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002300 }
2301 if (value_location.IsConstant()) {
2302 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2303 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2304 } else {
2305 GpuRegister value = value_location.AsRegister<GpuRegister>();
2306 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002307 }
2308 break;
2309 }
2310
Alexey Frunze15958152017-02-09 19:08:30 -08002311 case Primitive::kPrimInt: {
2312 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2313 if (index.IsConstant()) {
2314 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2315 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002316 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002317 }
2318 if (value_location.IsConstant()) {
2319 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2320 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2321 } else {
2322 GpuRegister value = value_location.AsRegister<GpuRegister>();
2323 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2324 }
2325 break;
2326 }
2327
Alexey Frunze4dda3372015-06-01 18:31:49 -07002328 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002329 if (value_location.IsConstant()) {
2330 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002331 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002332 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002333 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002334 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002335 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002336 }
Alexey Frunze15958152017-02-09 19:08:30 -08002337 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2338 DCHECK_EQ(value, 0);
2339 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2340 DCHECK(!needs_write_barrier);
2341 DCHECK(!may_need_runtime_call_for_type_check);
2342 break;
2343 }
2344
2345 DCHECK(needs_write_barrier);
2346 GpuRegister value = value_location.AsRegister<GpuRegister>();
2347 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2348 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2350 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2351 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2352 Mips64Label done;
2353 SlowPathCodeMIPS64* slow_path = nullptr;
2354
2355 if (may_need_runtime_call_for_type_check) {
2356 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2357 codegen_->AddSlowPath(slow_path);
2358 if (instruction->GetValueCanBeNull()) {
2359 Mips64Label non_zero;
2360 __ Bnezc(value, &non_zero);
2361 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2362 if (index.IsConstant()) {
2363 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002364 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002365 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002366 }
Alexey Frunze15958152017-02-09 19:08:30 -08002367 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2368 __ Bc(&done);
2369 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 }
Alexey Frunze15958152017-02-09 19:08:30 -08002371
2372 // Note that when read barriers are enabled, the type checks
2373 // are performed without read barriers. This is fine, even in
2374 // the case where a class object is in the from-space after
2375 // the flip, as a comparison involving such a type would not
2376 // produce a false positive; it may of course produce a false
2377 // negative, in which case we would take the ArraySet slow
2378 // path.
2379
2380 // /* HeapReference<Class> */ temp1 = obj->klass_
2381 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2382 __ MaybeUnpoisonHeapReference(temp1);
2383
2384 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2385 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2386 // /* HeapReference<Class> */ temp2 = value->klass_
2387 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2388 // If heap poisoning is enabled, no need to unpoison `temp1`
2389 // nor `temp2`, as we are comparing two poisoned references.
2390
2391 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2392 Mips64Label do_put;
2393 __ Beqc(temp1, temp2, &do_put);
2394 // If heap poisoning is enabled, the `temp1` reference has
2395 // not been unpoisoned yet; unpoison it now.
2396 __ MaybeUnpoisonHeapReference(temp1);
2397
2398 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2399 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2400 // If heap poisoning is enabled, no need to unpoison
2401 // `temp1`, as we are comparing against null below.
2402 __ Bnezc(temp1, slow_path->GetEntryLabel());
2403 __ Bind(&do_put);
2404 } else {
2405 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2406 }
2407 }
2408
2409 GpuRegister source = value;
2410 if (kPoisonHeapReferences) {
2411 // Note that in the case where `value` is a null reference,
2412 // we do not enter this block, as a null reference does not
2413 // need poisoning.
2414 __ Move(temp1, value);
2415 __ PoisonHeapReference(temp1);
2416 source = temp1;
2417 }
2418
2419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2420 if (index.IsConstant()) {
2421 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002422 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002423 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002424 }
2425 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2426
2427 if (!may_need_runtime_call_for_type_check) {
2428 codegen_->MaybeRecordImplicitNullCheck(instruction);
2429 }
2430
2431 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2432
2433 if (done.IsLinked()) {
2434 __ Bind(&done);
2435 }
2436
2437 if (slow_path != nullptr) {
2438 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002439 }
2440 break;
2441 }
2442
2443 case Primitive::kPrimLong: {
2444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002445 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002446 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002447 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002448 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002449 }
2450 if (value_location.IsConstant()) {
2451 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2452 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2453 } else {
2454 GpuRegister value = value_location.AsRegister<GpuRegister>();
2455 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002456 }
2457 break;
2458 }
2459
2460 case Primitive::kPrimFloat: {
2461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002462 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002463 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002464 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002465 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002466 }
2467 if (value_location.IsConstant()) {
2468 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2469 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2470 } else {
2471 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2472 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002473 }
2474 break;
2475 }
2476
2477 case Primitive::kPrimDouble: {
2478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002479 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002480 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002482 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002483 }
2484 if (value_location.IsConstant()) {
2485 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2486 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2487 } else {
2488 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2489 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002490 }
2491 break;
2492 }
2493
2494 case Primitive::kPrimVoid:
2495 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2496 UNREACHABLE();
2497 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498}
2499
2500void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002501 RegisterSet caller_saves = RegisterSet::Empty();
2502 InvokeRuntimeCallingConvention calling_convention;
2503 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2504 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2505 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002506 locations->SetInAt(0, Location::RequiresRegister());
2507 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002508}
2509
2510void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2511 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002512 BoundsCheckSlowPathMIPS64* slow_path =
2513 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002514 codegen_->AddSlowPath(slow_path);
2515
2516 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2517 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2518
2519 // length is limited by the maximum positive signed 32-bit integer.
2520 // Unsigned comparison of length and index checks for index < 0
2521 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002522 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002523}
2524
Alexey Frunze15958152017-02-09 19:08:30 -08002525// Temp is used for read barrier.
2526static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2527 if (kEmitCompilerReadBarrier &&
2528 (kUseBakerReadBarrier ||
2529 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2530 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2531 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2532 return 1;
2533 }
2534 return 0;
2535}
2536
2537// Extra temp is used for read barrier.
2538static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2539 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2540}
2541
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002543 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2544 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2545
2546 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2547 switch (type_check_kind) {
2548 case TypeCheckKind::kExactCheck:
2549 case TypeCheckKind::kAbstractClassCheck:
2550 case TypeCheckKind::kClassHierarchyCheck:
2551 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002552 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002553 ? LocationSummary::kCallOnSlowPath
2554 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2555 break;
2556 case TypeCheckKind::kArrayCheck:
2557 case TypeCheckKind::kUnresolvedCheck:
2558 case TypeCheckKind::kInterfaceCheck:
2559 call_kind = LocationSummary::kCallOnSlowPath;
2560 break;
2561 }
2562
2563 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002564 locations->SetInAt(0, Location::RequiresRegister());
2565 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002566 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002567}
2568
2569void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002570 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002571 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002572 Location obj_loc = locations->InAt(0);
2573 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002574 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002575 Location temp_loc = locations->GetTemp(0);
2576 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2577 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2578 DCHECK_LE(num_temps, 2u);
2579 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002580 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2581 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2582 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2583 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2584 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2585 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2586 const uint32_t object_array_data_offset =
2587 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2588 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002589
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002590 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2591 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2592 // read barriers is done for performance and code size reasons.
2593 bool is_type_check_slow_path_fatal = false;
2594 if (!kEmitCompilerReadBarrier) {
2595 is_type_check_slow_path_fatal =
2596 (type_check_kind == TypeCheckKind::kExactCheck ||
2597 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2598 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2599 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2600 !instruction->CanThrowIntoCatchBlock();
2601 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002602 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002603 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2604 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002605 codegen_->AddSlowPath(slow_path);
2606
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002607 // Avoid this check if we know `obj` is not null.
2608 if (instruction->MustDoNullCheck()) {
2609 __ Beqzc(obj, &done);
2610 }
2611
2612 switch (type_check_kind) {
2613 case TypeCheckKind::kExactCheck:
2614 case TypeCheckKind::kArrayCheck: {
2615 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002616 GenerateReferenceLoadTwoRegisters(instruction,
2617 temp_loc,
2618 obj_loc,
2619 class_offset,
2620 maybe_temp2_loc,
2621 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002622 // Jump to slow path for throwing the exception or doing a
2623 // more involved array check.
2624 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2625 break;
2626 }
2627
2628 case TypeCheckKind::kAbstractClassCheck: {
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 // If the class is abstract, we eagerly fetch the super class of the
2637 // object to avoid doing a comparison we know will fail.
2638 Mips64Label loop;
2639 __ Bind(&loop);
2640 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002641 GenerateReferenceLoadOneRegister(instruction,
2642 temp_loc,
2643 super_offset,
2644 maybe_temp2_loc,
2645 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002646 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2647 // exception.
2648 __ Beqzc(temp, slow_path->GetEntryLabel());
2649 // Otherwise, compare the classes.
2650 __ Bnec(temp, cls, &loop);
2651 break;
2652 }
2653
2654 case TypeCheckKind::kClassHierarchyCheck: {
2655 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002656 GenerateReferenceLoadTwoRegisters(instruction,
2657 temp_loc,
2658 obj_loc,
2659 class_offset,
2660 maybe_temp2_loc,
2661 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002662 // Walk over the class hierarchy to find a match.
2663 Mips64Label loop;
2664 __ Bind(&loop);
2665 __ Beqc(temp, cls, &done);
2666 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002667 GenerateReferenceLoadOneRegister(instruction,
2668 temp_loc,
2669 super_offset,
2670 maybe_temp2_loc,
2671 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002672 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2673 // exception. Otherwise, jump to the beginning of the loop.
2674 __ Bnezc(temp, &loop);
2675 __ Bc(slow_path->GetEntryLabel());
2676 break;
2677 }
2678
2679 case TypeCheckKind::kArrayObjectCheck: {
2680 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002681 GenerateReferenceLoadTwoRegisters(instruction,
2682 temp_loc,
2683 obj_loc,
2684 class_offset,
2685 maybe_temp2_loc,
2686 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002687 // Do an exact check.
2688 __ Beqc(temp, cls, &done);
2689 // Otherwise, we need to check that the object's class is a non-primitive array.
2690 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002691 GenerateReferenceLoadOneRegister(instruction,
2692 temp_loc,
2693 component_offset,
2694 maybe_temp2_loc,
2695 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002696 // If the component type is null, jump to the slow path to throw the exception.
2697 __ Beqzc(temp, slow_path->GetEntryLabel());
2698 // Otherwise, the object is indeed an array, further check that this component
2699 // type is not a primitive type.
2700 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2701 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2702 __ Bnezc(temp, slow_path->GetEntryLabel());
2703 break;
2704 }
2705
2706 case TypeCheckKind::kUnresolvedCheck:
2707 // We always go into the type check slow path for the unresolved check case.
2708 // We cannot directly call the CheckCast runtime entry point
2709 // without resorting to a type checking slow path here (i.e. by
2710 // calling InvokeRuntime directly), as it would require to
2711 // assign fixed registers for the inputs of this HInstanceOf
2712 // instruction (following the runtime calling convention), which
2713 // might be cluttered by the potential first read barrier
2714 // emission at the beginning of this method.
2715 __ Bc(slow_path->GetEntryLabel());
2716 break;
2717
2718 case TypeCheckKind::kInterfaceCheck: {
2719 // Avoid read barriers to improve performance of the fast path. We can not get false
2720 // positives by doing this.
2721 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002722 GenerateReferenceLoadTwoRegisters(instruction,
2723 temp_loc,
2724 obj_loc,
2725 class_offset,
2726 maybe_temp2_loc,
2727 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002728 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002729 GenerateReferenceLoadTwoRegisters(instruction,
2730 temp_loc,
2731 temp_loc,
2732 iftable_offset,
2733 maybe_temp2_loc,
2734 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002735 // Iftable is never null.
2736 __ Lw(TMP, temp, array_length_offset);
2737 // Loop through the iftable and check if any class matches.
2738 Mips64Label loop;
2739 __ Bind(&loop);
2740 __ Beqzc(TMP, slow_path->GetEntryLabel());
2741 __ Lwu(AT, temp, object_array_data_offset);
2742 __ MaybeUnpoisonHeapReference(AT);
2743 // Go to next interface.
2744 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2745 __ Addiu(TMP, TMP, -2);
2746 // Compare the classes and continue the loop if they do not match.
2747 __ Bnec(AT, cls, &loop);
2748 break;
2749 }
2750 }
2751
2752 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002753 __ Bind(slow_path->GetExitLabel());
2754}
2755
2756void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2757 LocationSummary* locations =
2758 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2759 locations->SetInAt(0, Location::RequiresRegister());
2760 if (check->HasUses()) {
2761 locations->SetOut(Location::SameAsFirstInput());
2762 }
2763}
2764
2765void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2766 // We assume the class is not null.
2767 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2768 check->GetLoadClass(),
2769 check,
2770 check->GetDexPc(),
2771 true);
2772 codegen_->AddSlowPath(slow_path);
2773 GenerateClassInitializationCheck(slow_path,
2774 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2775}
2776
2777void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2778 Primitive::Type in_type = compare->InputAt(0)->GetType();
2779
Alexey Frunze299a9392015-12-08 16:08:02 -08002780 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002781
2782 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002783 case Primitive::kPrimBoolean:
2784 case Primitive::kPrimByte:
2785 case Primitive::kPrimShort:
2786 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002787 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002788 case Primitive::kPrimLong:
2789 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002790 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2792 break;
2793
2794 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002795 case Primitive::kPrimDouble:
2796 locations->SetInAt(0, Location::RequiresFpuRegister());
2797 locations->SetInAt(1, Location::RequiresFpuRegister());
2798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002799 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002800
2801 default:
2802 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2803 }
2804}
2805
2806void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2807 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002808 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002809 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2810
2811 // 0 if: left == right
2812 // 1 if: left > right
2813 // -1 if: left < right
2814 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002815 case Primitive::kPrimBoolean:
2816 case Primitive::kPrimByte:
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002819 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002820 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002821 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002822 Location rhs_location = locations->InAt(1);
2823 bool use_imm = rhs_location.IsConstant();
2824 GpuRegister rhs = ZERO;
2825 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002826 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002827 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2828 if (value != 0) {
2829 rhs = AT;
2830 __ LoadConst64(rhs, value);
2831 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002832 } else {
2833 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2834 if (value != 0) {
2835 rhs = AT;
2836 __ LoadConst32(rhs, value);
2837 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002838 }
2839 } else {
2840 rhs = rhs_location.AsRegister<GpuRegister>();
2841 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002843 __ Slt(res, rhs, lhs);
2844 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845 break;
2846 }
2847
Alexey Frunze299a9392015-12-08 16:08:02 -08002848 case Primitive::kPrimFloat: {
2849 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2850 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2851 Mips64Label done;
2852 __ CmpEqS(FTMP, lhs, rhs);
2853 __ LoadConst32(res, 0);
2854 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002855 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002856 __ CmpLtS(FTMP, lhs, rhs);
2857 __ LoadConst32(res, -1);
2858 __ Bc1nez(FTMP, &done);
2859 __ LoadConst32(res, 1);
2860 } else {
2861 __ CmpLtS(FTMP, rhs, lhs);
2862 __ LoadConst32(res, 1);
2863 __ Bc1nez(FTMP, &done);
2864 __ LoadConst32(res, -1);
2865 }
2866 __ Bind(&done);
2867 break;
2868 }
2869
Alexey Frunze4dda3372015-06-01 18:31:49 -07002870 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002871 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2872 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2873 Mips64Label done;
2874 __ CmpEqD(FTMP, lhs, rhs);
2875 __ LoadConst32(res, 0);
2876 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002877 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002878 __ CmpLtD(FTMP, lhs, rhs);
2879 __ LoadConst32(res, -1);
2880 __ Bc1nez(FTMP, &done);
2881 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002882 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002883 __ CmpLtD(FTMP, rhs, lhs);
2884 __ LoadConst32(res, 1);
2885 __ Bc1nez(FTMP, &done);
2886 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002887 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002888 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002889 break;
2890 }
2891
2892 default:
2893 LOG(FATAL) << "Unimplemented compare type " << in_type;
2894 }
2895}
2896
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002897void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002898 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002899 switch (instruction->InputAt(0)->GetType()) {
2900 default:
2901 case Primitive::kPrimLong:
2902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2904 break;
2905
2906 case Primitive::kPrimFloat:
2907 case Primitive::kPrimDouble:
2908 locations->SetInAt(0, Location::RequiresFpuRegister());
2909 locations->SetInAt(1, Location::RequiresFpuRegister());
2910 break;
2911 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002912 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2914 }
2915}
2916
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002917void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002918 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002919 return;
2920 }
2921
Alexey Frunze299a9392015-12-08 16:08:02 -08002922 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002924 switch (type) {
2925 default:
2926 // Integer case.
2927 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2928 return;
2929 case Primitive::kPrimLong:
2930 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2931 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002932 case Primitive::kPrimFloat:
2933 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002934 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2935 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936 }
2937}
2938
Alexey Frunzec857c742015-09-23 15:12:39 -07002939void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2940 DCHECK(instruction->IsDiv() || instruction->IsRem());
2941 Primitive::Type type = instruction->GetResultType();
2942
2943 LocationSummary* locations = instruction->GetLocations();
2944 Location second = locations->InAt(1);
2945 DCHECK(second.IsConstant());
2946
2947 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2948 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2949 int64_t imm = Int64FromConstant(second.GetConstant());
2950 DCHECK(imm == 1 || imm == -1);
2951
2952 if (instruction->IsRem()) {
2953 __ Move(out, ZERO);
2954 } else {
2955 if (imm == -1) {
2956 if (type == Primitive::kPrimInt) {
2957 __ Subu(out, ZERO, dividend);
2958 } else {
2959 DCHECK_EQ(type, Primitive::kPrimLong);
2960 __ Dsubu(out, ZERO, dividend);
2961 }
2962 } else if (out != dividend) {
2963 __ Move(out, dividend);
2964 }
2965 }
2966}
2967
2968void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2969 DCHECK(instruction->IsDiv() || instruction->IsRem());
2970 Primitive::Type type = instruction->GetResultType();
2971
2972 LocationSummary* locations = instruction->GetLocations();
2973 Location second = locations->InAt(1);
2974 DCHECK(second.IsConstant());
2975
2976 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2977 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2978 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002979 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002980 int ctz_imm = CTZ(abs_imm);
2981
2982 if (instruction->IsDiv()) {
2983 if (type == Primitive::kPrimInt) {
2984 if (ctz_imm == 1) {
2985 // Fast path for division by +/-2, which is very common.
2986 __ Srl(TMP, dividend, 31);
2987 } else {
2988 __ Sra(TMP, dividend, 31);
2989 __ Srl(TMP, TMP, 32 - ctz_imm);
2990 }
2991 __ Addu(out, dividend, TMP);
2992 __ Sra(out, out, ctz_imm);
2993 if (imm < 0) {
2994 __ Subu(out, ZERO, out);
2995 }
2996 } else {
2997 DCHECK_EQ(type, Primitive::kPrimLong);
2998 if (ctz_imm == 1) {
2999 // Fast path for division by +/-2, which is very common.
3000 __ Dsrl32(TMP, dividend, 31);
3001 } else {
3002 __ Dsra32(TMP, dividend, 31);
3003 if (ctz_imm > 32) {
3004 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3005 } else {
3006 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3007 }
3008 }
3009 __ Daddu(out, dividend, TMP);
3010 if (ctz_imm < 32) {
3011 __ Dsra(out, out, ctz_imm);
3012 } else {
3013 __ Dsra32(out, out, ctz_imm - 32);
3014 }
3015 if (imm < 0) {
3016 __ Dsubu(out, ZERO, out);
3017 }
3018 }
3019 } else {
3020 if (type == Primitive::kPrimInt) {
3021 if (ctz_imm == 1) {
3022 // Fast path for modulo +/-2, which is very common.
3023 __ Sra(TMP, dividend, 31);
3024 __ Subu(out, dividend, TMP);
3025 __ Andi(out, out, 1);
3026 __ Addu(out, out, TMP);
3027 } else {
3028 __ Sra(TMP, dividend, 31);
3029 __ Srl(TMP, TMP, 32 - ctz_imm);
3030 __ Addu(out, dividend, TMP);
3031 if (IsUint<16>(abs_imm - 1)) {
3032 __ Andi(out, out, abs_imm - 1);
3033 } else {
3034 __ Sll(out, out, 32 - ctz_imm);
3035 __ Srl(out, out, 32 - ctz_imm);
3036 }
3037 __ Subu(out, out, TMP);
3038 }
3039 } else {
3040 DCHECK_EQ(type, Primitive::kPrimLong);
3041 if (ctz_imm == 1) {
3042 // Fast path for modulo +/-2, which is very common.
3043 __ Dsra32(TMP, dividend, 31);
3044 __ Dsubu(out, dividend, TMP);
3045 __ Andi(out, out, 1);
3046 __ Daddu(out, out, TMP);
3047 } else {
3048 __ Dsra32(TMP, dividend, 31);
3049 if (ctz_imm > 32) {
3050 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3051 } else {
3052 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3053 }
3054 __ Daddu(out, dividend, TMP);
3055 if (IsUint<16>(abs_imm - 1)) {
3056 __ Andi(out, out, abs_imm - 1);
3057 } else {
3058 if (ctz_imm > 32) {
3059 __ Dsll(out, out, 64 - ctz_imm);
3060 __ Dsrl(out, out, 64 - ctz_imm);
3061 } else {
3062 __ Dsll32(out, out, 32 - ctz_imm);
3063 __ Dsrl32(out, out, 32 - ctz_imm);
3064 }
3065 }
3066 __ Dsubu(out, out, TMP);
3067 }
3068 }
3069 }
3070}
3071
3072void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3073 DCHECK(instruction->IsDiv() || instruction->IsRem());
3074
3075 LocationSummary* locations = instruction->GetLocations();
3076 Location second = locations->InAt(1);
3077 DCHECK(second.IsConstant());
3078
3079 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3080 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3081 int64_t imm = Int64FromConstant(second.GetConstant());
3082
3083 Primitive::Type type = instruction->GetResultType();
3084 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3085
3086 int64_t magic;
3087 int shift;
3088 CalculateMagicAndShiftForDivRem(imm,
3089 (type == Primitive::kPrimLong),
3090 &magic,
3091 &shift);
3092
3093 if (type == Primitive::kPrimInt) {
3094 __ LoadConst32(TMP, magic);
3095 __ MuhR6(TMP, dividend, TMP);
3096
3097 if (imm > 0 && magic < 0) {
3098 __ Addu(TMP, TMP, dividend);
3099 } else if (imm < 0 && magic > 0) {
3100 __ Subu(TMP, TMP, dividend);
3101 }
3102
3103 if (shift != 0) {
3104 __ Sra(TMP, TMP, shift);
3105 }
3106
3107 if (instruction->IsDiv()) {
3108 __ Sra(out, TMP, 31);
3109 __ Subu(out, TMP, out);
3110 } else {
3111 __ Sra(AT, TMP, 31);
3112 __ Subu(AT, TMP, AT);
3113 __ LoadConst32(TMP, imm);
3114 __ MulR6(TMP, AT, TMP);
3115 __ Subu(out, dividend, TMP);
3116 }
3117 } else {
3118 __ LoadConst64(TMP, magic);
3119 __ Dmuh(TMP, dividend, TMP);
3120
3121 if (imm > 0 && magic < 0) {
3122 __ Daddu(TMP, TMP, dividend);
3123 } else if (imm < 0 && magic > 0) {
3124 __ Dsubu(TMP, TMP, dividend);
3125 }
3126
3127 if (shift >= 32) {
3128 __ Dsra32(TMP, TMP, shift - 32);
3129 } else if (shift > 0) {
3130 __ Dsra(TMP, TMP, shift);
3131 }
3132
3133 if (instruction->IsDiv()) {
3134 __ Dsra32(out, TMP, 31);
3135 __ Dsubu(out, TMP, out);
3136 } else {
3137 __ Dsra32(AT, TMP, 31);
3138 __ Dsubu(AT, TMP, AT);
3139 __ LoadConst64(TMP, imm);
3140 __ Dmul(TMP, AT, TMP);
3141 __ Dsubu(out, dividend, TMP);
3142 }
3143 }
3144}
3145
3146void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3147 DCHECK(instruction->IsDiv() || instruction->IsRem());
3148 Primitive::Type type = instruction->GetResultType();
3149 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3150
3151 LocationSummary* locations = instruction->GetLocations();
3152 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3153 Location second = locations->InAt(1);
3154
3155 if (second.IsConstant()) {
3156 int64_t imm = Int64FromConstant(second.GetConstant());
3157 if (imm == 0) {
3158 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3159 } else if (imm == 1 || imm == -1) {
3160 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003161 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003162 DivRemByPowerOfTwo(instruction);
3163 } else {
3164 DCHECK(imm <= -2 || imm >= 2);
3165 GenerateDivRemWithAnyConstant(instruction);
3166 }
3167 } else {
3168 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3169 GpuRegister divisor = second.AsRegister<GpuRegister>();
3170 if (instruction->IsDiv()) {
3171 if (type == Primitive::kPrimInt)
3172 __ DivR6(out, dividend, divisor);
3173 else
3174 __ Ddiv(out, dividend, divisor);
3175 } else {
3176 if (type == Primitive::kPrimInt)
3177 __ ModR6(out, dividend, divisor);
3178 else
3179 __ Dmod(out, dividend, divisor);
3180 }
3181 }
3182}
3183
Alexey Frunze4dda3372015-06-01 18:31:49 -07003184void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3185 LocationSummary* locations =
3186 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3187 switch (div->GetResultType()) {
3188 case Primitive::kPrimInt:
3189 case Primitive::kPrimLong:
3190 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003191 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003192 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3193 break;
3194
3195 case Primitive::kPrimFloat:
3196 case Primitive::kPrimDouble:
3197 locations->SetInAt(0, Location::RequiresFpuRegister());
3198 locations->SetInAt(1, Location::RequiresFpuRegister());
3199 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3200 break;
3201
3202 default:
3203 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3204 }
3205}
3206
3207void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3208 Primitive::Type type = instruction->GetType();
3209 LocationSummary* locations = instruction->GetLocations();
3210
3211 switch (type) {
3212 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003213 case Primitive::kPrimLong:
3214 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003215 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003216 case Primitive::kPrimFloat:
3217 case Primitive::kPrimDouble: {
3218 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3219 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3220 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3221 if (type == Primitive::kPrimFloat)
3222 __ DivS(dst, lhs, rhs);
3223 else
3224 __ DivD(dst, lhs, rhs);
3225 break;
3226 }
3227 default:
3228 LOG(FATAL) << "Unexpected div type " << type;
3229 }
3230}
3231
3232void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003233 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003235}
3236
3237void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3238 SlowPathCodeMIPS64* slow_path =
3239 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3240 codegen_->AddSlowPath(slow_path);
3241 Location value = instruction->GetLocations()->InAt(0);
3242
3243 Primitive::Type type = instruction->GetType();
3244
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003245 if (!Primitive::IsIntegralType(type)) {
3246 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003247 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003248 }
3249
3250 if (value.IsConstant()) {
3251 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3252 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003253 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254 } else {
3255 // A division by a non-null constant is valid. We don't need to perform
3256 // any check, so simply fall through.
3257 }
3258 } else {
3259 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3260 }
3261}
3262
3263void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3264 LocationSummary* locations =
3265 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3266 locations->SetOut(Location::ConstantLocation(constant));
3267}
3268
3269void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3270 // Will be generated at use site.
3271}
3272
3273void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3274 exit->SetLocations(nullptr);
3275}
3276
3277void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3278}
3279
3280void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3281 LocationSummary* locations =
3282 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3283 locations->SetOut(Location::ConstantLocation(constant));
3284}
3285
3286void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3287 // Will be generated at use site.
3288}
3289
David Brazdilfc6a86a2015-06-26 10:33:45 +00003290void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003291 DCHECK(!successor->IsExitBlock());
3292 HBasicBlock* block = got->GetBlock();
3293 HInstruction* previous = got->GetPrevious();
3294 HLoopInformation* info = block->GetLoopInformation();
3295
3296 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3297 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3298 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3299 return;
3300 }
3301 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3302 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3303 }
3304 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003305 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003306 }
3307}
3308
David Brazdilfc6a86a2015-06-26 10:33:45 +00003309void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3310 got->SetLocations(nullptr);
3311}
3312
3313void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3314 HandleGoto(got, got->GetSuccessor());
3315}
3316
3317void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3318 try_boundary->SetLocations(nullptr);
3319}
3320
3321void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3322 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3323 if (!successor->IsExitBlock()) {
3324 HandleGoto(try_boundary, successor);
3325 }
3326}
3327
Alexey Frunze299a9392015-12-08 16:08:02 -08003328void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3329 bool is64bit,
3330 LocationSummary* locations) {
3331 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3332 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3333 Location rhs_location = locations->InAt(1);
3334 GpuRegister rhs_reg = ZERO;
3335 int64_t rhs_imm = 0;
3336 bool use_imm = rhs_location.IsConstant();
3337 if (use_imm) {
3338 if (is64bit) {
3339 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3340 } else {
3341 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3342 }
3343 } else {
3344 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3345 }
3346 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3347
3348 switch (cond) {
3349 case kCondEQ:
3350 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003351 if (use_imm && IsInt<16>(-rhs_imm)) {
3352 if (rhs_imm == 0) {
3353 if (cond == kCondEQ) {
3354 __ Sltiu(dst, lhs, 1);
3355 } else {
3356 __ Sltu(dst, ZERO, lhs);
3357 }
3358 } else {
3359 if (is64bit) {
3360 __ Daddiu(dst, lhs, -rhs_imm);
3361 } else {
3362 __ Addiu(dst, lhs, -rhs_imm);
3363 }
3364 if (cond == kCondEQ) {
3365 __ Sltiu(dst, dst, 1);
3366 } else {
3367 __ Sltu(dst, ZERO, dst);
3368 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003369 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003370 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003371 if (use_imm && IsUint<16>(rhs_imm)) {
3372 __ Xori(dst, lhs, rhs_imm);
3373 } else {
3374 if (use_imm) {
3375 rhs_reg = TMP;
3376 __ LoadConst64(rhs_reg, rhs_imm);
3377 }
3378 __ Xor(dst, lhs, rhs_reg);
3379 }
3380 if (cond == kCondEQ) {
3381 __ Sltiu(dst, dst, 1);
3382 } else {
3383 __ Sltu(dst, ZERO, dst);
3384 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003385 }
3386 break;
3387
3388 case kCondLT:
3389 case kCondGE:
3390 if (use_imm && IsInt<16>(rhs_imm)) {
3391 __ Slti(dst, lhs, rhs_imm);
3392 } else {
3393 if (use_imm) {
3394 rhs_reg = TMP;
3395 __ LoadConst64(rhs_reg, rhs_imm);
3396 }
3397 __ Slt(dst, lhs, rhs_reg);
3398 }
3399 if (cond == kCondGE) {
3400 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3401 // only the slt instruction but no sge.
3402 __ Xori(dst, dst, 1);
3403 }
3404 break;
3405
3406 case kCondLE:
3407 case kCondGT:
3408 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3409 // Simulate lhs <= rhs via lhs < rhs + 1.
3410 __ Slti(dst, lhs, rhs_imm_plus_one);
3411 if (cond == kCondGT) {
3412 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3413 // only the slti instruction but no sgti.
3414 __ Xori(dst, dst, 1);
3415 }
3416 } else {
3417 if (use_imm) {
3418 rhs_reg = TMP;
3419 __ LoadConst64(rhs_reg, rhs_imm);
3420 }
3421 __ Slt(dst, rhs_reg, lhs);
3422 if (cond == kCondLE) {
3423 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3424 // only the slt instruction but no sle.
3425 __ Xori(dst, dst, 1);
3426 }
3427 }
3428 break;
3429
3430 case kCondB:
3431 case kCondAE:
3432 if (use_imm && IsInt<16>(rhs_imm)) {
3433 // Sltiu sign-extends its 16-bit immediate operand before
3434 // the comparison and thus lets us compare directly with
3435 // unsigned values in the ranges [0, 0x7fff] and
3436 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3437 __ Sltiu(dst, lhs, rhs_imm);
3438 } else {
3439 if (use_imm) {
3440 rhs_reg = TMP;
3441 __ LoadConst64(rhs_reg, rhs_imm);
3442 }
3443 __ Sltu(dst, lhs, rhs_reg);
3444 }
3445 if (cond == kCondAE) {
3446 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3447 // only the sltu instruction but no sgeu.
3448 __ Xori(dst, dst, 1);
3449 }
3450 break;
3451
3452 case kCondBE:
3453 case kCondA:
3454 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3455 // Simulate lhs <= rhs via lhs < rhs + 1.
3456 // Note that this only works if rhs + 1 does not overflow
3457 // to 0, hence the check above.
3458 // Sltiu sign-extends its 16-bit immediate operand before
3459 // the comparison and thus lets us compare directly with
3460 // unsigned values in the ranges [0, 0x7fff] and
3461 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3462 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3463 if (cond == kCondA) {
3464 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3465 // only the sltiu instruction but no sgtiu.
3466 __ Xori(dst, dst, 1);
3467 }
3468 } else {
3469 if (use_imm) {
3470 rhs_reg = TMP;
3471 __ LoadConst64(rhs_reg, rhs_imm);
3472 }
3473 __ Sltu(dst, rhs_reg, lhs);
3474 if (cond == kCondBE) {
3475 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3476 // only the sltu instruction but no sleu.
3477 __ Xori(dst, dst, 1);
3478 }
3479 }
3480 break;
3481 }
3482}
3483
3484void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3485 bool is64bit,
3486 LocationSummary* locations,
3487 Mips64Label* label) {
3488 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3489 Location rhs_location = locations->InAt(1);
3490 GpuRegister rhs_reg = ZERO;
3491 int64_t rhs_imm = 0;
3492 bool use_imm = rhs_location.IsConstant();
3493 if (use_imm) {
3494 if (is64bit) {
3495 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3496 } else {
3497 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3498 }
3499 } else {
3500 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3501 }
3502
3503 if (use_imm && rhs_imm == 0) {
3504 switch (cond) {
3505 case kCondEQ:
3506 case kCondBE: // <= 0 if zero
3507 __ Beqzc(lhs, label);
3508 break;
3509 case kCondNE:
3510 case kCondA: // > 0 if non-zero
3511 __ Bnezc(lhs, label);
3512 break;
3513 case kCondLT:
3514 __ Bltzc(lhs, label);
3515 break;
3516 case kCondGE:
3517 __ Bgezc(lhs, label);
3518 break;
3519 case kCondLE:
3520 __ Blezc(lhs, label);
3521 break;
3522 case kCondGT:
3523 __ Bgtzc(lhs, label);
3524 break;
3525 case kCondB: // always false
3526 break;
3527 case kCondAE: // always true
3528 __ Bc(label);
3529 break;
3530 }
3531 } else {
3532 if (use_imm) {
3533 rhs_reg = TMP;
3534 __ LoadConst64(rhs_reg, rhs_imm);
3535 }
3536 switch (cond) {
3537 case kCondEQ:
3538 __ Beqc(lhs, rhs_reg, label);
3539 break;
3540 case kCondNE:
3541 __ Bnec(lhs, rhs_reg, label);
3542 break;
3543 case kCondLT:
3544 __ Bltc(lhs, rhs_reg, label);
3545 break;
3546 case kCondGE:
3547 __ Bgec(lhs, rhs_reg, label);
3548 break;
3549 case kCondLE:
3550 __ Bgec(rhs_reg, lhs, label);
3551 break;
3552 case kCondGT:
3553 __ Bltc(rhs_reg, lhs, label);
3554 break;
3555 case kCondB:
3556 __ Bltuc(lhs, rhs_reg, label);
3557 break;
3558 case kCondAE:
3559 __ Bgeuc(lhs, rhs_reg, label);
3560 break;
3561 case kCondBE:
3562 __ Bgeuc(rhs_reg, lhs, label);
3563 break;
3564 case kCondA:
3565 __ Bltuc(rhs_reg, lhs, label);
3566 break;
3567 }
3568 }
3569}
3570
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003571void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3572 bool gt_bias,
3573 Primitive::Type type,
3574 LocationSummary* locations) {
3575 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3576 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3577 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3578 if (type == Primitive::kPrimFloat) {
3579 switch (cond) {
3580 case kCondEQ:
3581 __ CmpEqS(FTMP, lhs, rhs);
3582 __ Mfc1(dst, FTMP);
3583 __ Andi(dst, dst, 1);
3584 break;
3585 case kCondNE:
3586 __ CmpEqS(FTMP, lhs, rhs);
3587 __ Mfc1(dst, FTMP);
3588 __ Addiu(dst, dst, 1);
3589 break;
3590 case kCondLT:
3591 if (gt_bias) {
3592 __ CmpLtS(FTMP, lhs, rhs);
3593 } else {
3594 __ CmpUltS(FTMP, lhs, rhs);
3595 }
3596 __ Mfc1(dst, FTMP);
3597 __ Andi(dst, dst, 1);
3598 break;
3599 case kCondLE:
3600 if (gt_bias) {
3601 __ CmpLeS(FTMP, lhs, rhs);
3602 } else {
3603 __ CmpUleS(FTMP, lhs, rhs);
3604 }
3605 __ Mfc1(dst, FTMP);
3606 __ Andi(dst, dst, 1);
3607 break;
3608 case kCondGT:
3609 if (gt_bias) {
3610 __ CmpUltS(FTMP, rhs, lhs);
3611 } else {
3612 __ CmpLtS(FTMP, rhs, lhs);
3613 }
3614 __ Mfc1(dst, FTMP);
3615 __ Andi(dst, dst, 1);
3616 break;
3617 case kCondGE:
3618 if (gt_bias) {
3619 __ CmpUleS(FTMP, rhs, lhs);
3620 } else {
3621 __ CmpLeS(FTMP, rhs, lhs);
3622 }
3623 __ Mfc1(dst, FTMP);
3624 __ Andi(dst, dst, 1);
3625 break;
3626 default:
3627 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3628 UNREACHABLE();
3629 }
3630 } else {
3631 DCHECK_EQ(type, Primitive::kPrimDouble);
3632 switch (cond) {
3633 case kCondEQ:
3634 __ CmpEqD(FTMP, lhs, rhs);
3635 __ Mfc1(dst, FTMP);
3636 __ Andi(dst, dst, 1);
3637 break;
3638 case kCondNE:
3639 __ CmpEqD(FTMP, lhs, rhs);
3640 __ Mfc1(dst, FTMP);
3641 __ Addiu(dst, dst, 1);
3642 break;
3643 case kCondLT:
3644 if (gt_bias) {
3645 __ CmpLtD(FTMP, lhs, rhs);
3646 } else {
3647 __ CmpUltD(FTMP, lhs, rhs);
3648 }
3649 __ Mfc1(dst, FTMP);
3650 __ Andi(dst, dst, 1);
3651 break;
3652 case kCondLE:
3653 if (gt_bias) {
3654 __ CmpLeD(FTMP, lhs, rhs);
3655 } else {
3656 __ CmpUleD(FTMP, lhs, rhs);
3657 }
3658 __ Mfc1(dst, FTMP);
3659 __ Andi(dst, dst, 1);
3660 break;
3661 case kCondGT:
3662 if (gt_bias) {
3663 __ CmpUltD(FTMP, rhs, lhs);
3664 } else {
3665 __ CmpLtD(FTMP, rhs, lhs);
3666 }
3667 __ Mfc1(dst, FTMP);
3668 __ Andi(dst, dst, 1);
3669 break;
3670 case kCondGE:
3671 if (gt_bias) {
3672 __ CmpUleD(FTMP, rhs, lhs);
3673 } else {
3674 __ CmpLeD(FTMP, rhs, lhs);
3675 }
3676 __ Mfc1(dst, FTMP);
3677 __ Andi(dst, dst, 1);
3678 break;
3679 default:
3680 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3681 UNREACHABLE();
3682 }
3683 }
3684}
3685
Alexey Frunze299a9392015-12-08 16:08:02 -08003686void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3687 bool gt_bias,
3688 Primitive::Type type,
3689 LocationSummary* locations,
3690 Mips64Label* label) {
3691 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3692 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3693 if (type == Primitive::kPrimFloat) {
3694 switch (cond) {
3695 case kCondEQ:
3696 __ CmpEqS(FTMP, lhs, rhs);
3697 __ Bc1nez(FTMP, label);
3698 break;
3699 case kCondNE:
3700 __ CmpEqS(FTMP, lhs, rhs);
3701 __ Bc1eqz(FTMP, label);
3702 break;
3703 case kCondLT:
3704 if (gt_bias) {
3705 __ CmpLtS(FTMP, lhs, rhs);
3706 } else {
3707 __ CmpUltS(FTMP, lhs, rhs);
3708 }
3709 __ Bc1nez(FTMP, label);
3710 break;
3711 case kCondLE:
3712 if (gt_bias) {
3713 __ CmpLeS(FTMP, lhs, rhs);
3714 } else {
3715 __ CmpUleS(FTMP, lhs, rhs);
3716 }
3717 __ Bc1nez(FTMP, label);
3718 break;
3719 case kCondGT:
3720 if (gt_bias) {
3721 __ CmpUltS(FTMP, rhs, lhs);
3722 } else {
3723 __ CmpLtS(FTMP, rhs, lhs);
3724 }
3725 __ Bc1nez(FTMP, label);
3726 break;
3727 case kCondGE:
3728 if (gt_bias) {
3729 __ CmpUleS(FTMP, rhs, lhs);
3730 } else {
3731 __ CmpLeS(FTMP, rhs, lhs);
3732 }
3733 __ Bc1nez(FTMP, label);
3734 break;
3735 default:
3736 LOG(FATAL) << "Unexpected non-floating-point condition";
3737 }
3738 } else {
3739 DCHECK_EQ(type, Primitive::kPrimDouble);
3740 switch (cond) {
3741 case kCondEQ:
3742 __ CmpEqD(FTMP, lhs, rhs);
3743 __ Bc1nez(FTMP, label);
3744 break;
3745 case kCondNE:
3746 __ CmpEqD(FTMP, lhs, rhs);
3747 __ Bc1eqz(FTMP, label);
3748 break;
3749 case kCondLT:
3750 if (gt_bias) {
3751 __ CmpLtD(FTMP, lhs, rhs);
3752 } else {
3753 __ CmpUltD(FTMP, lhs, rhs);
3754 }
3755 __ Bc1nez(FTMP, label);
3756 break;
3757 case kCondLE:
3758 if (gt_bias) {
3759 __ CmpLeD(FTMP, lhs, rhs);
3760 } else {
3761 __ CmpUleD(FTMP, lhs, rhs);
3762 }
3763 __ Bc1nez(FTMP, label);
3764 break;
3765 case kCondGT:
3766 if (gt_bias) {
3767 __ CmpUltD(FTMP, rhs, lhs);
3768 } else {
3769 __ CmpLtD(FTMP, rhs, lhs);
3770 }
3771 __ Bc1nez(FTMP, label);
3772 break;
3773 case kCondGE:
3774 if (gt_bias) {
3775 __ CmpUleD(FTMP, rhs, lhs);
3776 } else {
3777 __ CmpLeD(FTMP, rhs, lhs);
3778 }
3779 __ Bc1nez(FTMP, label);
3780 break;
3781 default:
3782 LOG(FATAL) << "Unexpected non-floating-point condition";
3783 }
3784 }
3785}
3786
Alexey Frunze4dda3372015-06-01 18:31:49 -07003787void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003788 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003789 Mips64Label* true_target,
3790 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003791 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003792
David Brazdil0debae72015-11-12 18:37:00 +00003793 if (true_target == nullptr && false_target == nullptr) {
3794 // Nothing to do. The code always falls through.
3795 return;
3796 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003797 // Constant condition, statically compared against "true" (integer value 1).
3798 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003799 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003800 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003803 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003804 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003805 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003806 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003807 }
David Brazdil0debae72015-11-12 18:37:00 +00003808 return;
3809 }
3810
3811 // The following code generates these patterns:
3812 // (1) true_target == nullptr && false_target != nullptr
3813 // - opposite condition true => branch to false_target
3814 // (2) true_target != nullptr && false_target == nullptr
3815 // - condition true => branch to true_target
3816 // (3) true_target != nullptr && false_target != nullptr
3817 // - condition true => branch to true_target
3818 // - branch to false_target
3819 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003820 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003821 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003822 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003823 if (true_target == nullptr) {
3824 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3825 } else {
3826 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3827 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003828 } else {
3829 // The condition instruction has not been materialized, use its inputs as
3830 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003831 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003832 Primitive::Type type = condition->InputAt(0)->GetType();
3833 LocationSummary* locations = cond->GetLocations();
3834 IfCondition if_cond = condition->GetCondition();
3835 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003836
David Brazdil0debae72015-11-12 18:37:00 +00003837 if (true_target == nullptr) {
3838 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003839 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003840 }
3841
Alexey Frunze299a9392015-12-08 16:08:02 -08003842 switch (type) {
3843 default:
3844 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3845 break;
3846 case Primitive::kPrimLong:
3847 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3848 break;
3849 case Primitive::kPrimFloat:
3850 case Primitive::kPrimDouble:
3851 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3852 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003853 }
3854 }
David Brazdil0debae72015-11-12 18:37:00 +00003855
3856 // If neither branch falls through (case 3), the conditional branch to `true_target`
3857 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3858 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003859 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003860 }
3861}
3862
3863void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3864 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003865 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003866 locations->SetInAt(0, Location::RequiresRegister());
3867 }
3868}
3869
3870void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003871 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3872 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003873 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003874 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003875 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003876 nullptr : codegen_->GetLabelOf(false_successor);
3877 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003878}
3879
3880void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3881 LocationSummary* locations = new (GetGraph()->GetArena())
3882 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003883 InvokeRuntimeCallingConvention calling_convention;
3884 RegisterSet caller_saves = RegisterSet::Empty();
3885 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3886 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003887 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003888 locations->SetInAt(0, Location::RequiresRegister());
3889 }
3890}
3891
3892void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003893 SlowPathCodeMIPS64* slow_path =
3894 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003895 GenerateTestAndBranch(deoptimize,
3896 /* condition_input_index */ 0,
3897 slow_path->GetEntryLabel(),
3898 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003899}
3900
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003901void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3902 LocationSummary* locations = new (GetGraph()->GetArena())
3903 LocationSummary(flag, LocationSummary::kNoCall);
3904 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003905}
3906
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003907void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3908 __ LoadFromOffset(kLoadWord,
3909 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3910 SP,
3911 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003912}
3913
David Brazdil74eb1b22015-12-14 11:44:01 +00003914void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3916 if (Primitive::IsFloatingPointType(select->GetType())) {
3917 locations->SetInAt(0, Location::RequiresFpuRegister());
3918 locations->SetInAt(1, Location::RequiresFpuRegister());
3919 } else {
3920 locations->SetInAt(0, Location::RequiresRegister());
3921 locations->SetInAt(1, Location::RequiresRegister());
3922 }
3923 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3924 locations->SetInAt(2, Location::RequiresRegister());
3925 }
3926 locations->SetOut(Location::SameAsFirstInput());
3927}
3928
3929void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3930 LocationSummary* locations = select->GetLocations();
3931 Mips64Label false_target;
3932 GenerateTestAndBranch(select,
3933 /* condition_input_index */ 2,
3934 /* true_target */ nullptr,
3935 &false_target);
3936 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3937 __ Bind(&false_target);
3938}
3939
David Srbecky0cf44932015-12-09 14:09:59 +00003940void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3941 new (GetGraph()->GetArena()) LocationSummary(info);
3942}
3943
David Srbeckyd28f4a02016-03-14 17:14:24 +00003944void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3945 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003946}
3947
3948void CodeGeneratorMIPS64::GenerateNop() {
3949 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003950}
3951
Alexey Frunze4dda3372015-06-01 18:31:49 -07003952void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003953 const FieldInfo& field_info) {
3954 Primitive::Type field_type = field_info.GetFieldType();
3955 bool object_field_get_with_read_barrier =
3956 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3958 instruction,
3959 object_field_get_with_read_barrier
3960 ? LocationSummary::kCallOnSlowPath
3961 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07003962 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3963 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
3964 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003965 locations->SetInAt(0, Location::RequiresRegister());
3966 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3967 locations->SetOut(Location::RequiresFpuRegister());
3968 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08003969 // The output overlaps in the case of an object field get with
3970 // read barriers enabled: we do not want the move to overwrite the
3971 // object's location, as we need it to emit the read barrier.
3972 locations->SetOut(Location::RequiresRegister(),
3973 object_field_get_with_read_barrier
3974 ? Location::kOutputOverlap
3975 : Location::kNoOutputOverlap);
3976 }
3977 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3978 // We need a temporary register for the read barrier marking slow
3979 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
3980 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003981 }
3982}
3983
3984void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3985 const FieldInfo& field_info) {
3986 Primitive::Type type = field_info.GetFieldType();
3987 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003988 Location obj_loc = locations->InAt(0);
3989 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
3990 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08003992 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08003993 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003994 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3995
Alexey Frunze4dda3372015-06-01 18:31:49 -07003996 switch (type) {
3997 case Primitive::kPrimBoolean:
3998 load_type = kLoadUnsignedByte;
3999 break;
4000 case Primitive::kPrimByte:
4001 load_type = kLoadSignedByte;
4002 break;
4003 case Primitive::kPrimShort:
4004 load_type = kLoadSignedHalfword;
4005 break;
4006 case Primitive::kPrimChar:
4007 load_type = kLoadUnsignedHalfword;
4008 break;
4009 case Primitive::kPrimInt:
4010 case Primitive::kPrimFloat:
4011 load_type = kLoadWord;
4012 break;
4013 case Primitive::kPrimLong:
4014 case Primitive::kPrimDouble:
4015 load_type = kLoadDoubleword;
4016 break;
4017 case Primitive::kPrimNot:
4018 load_type = kLoadUnsignedWord;
4019 break;
4020 case Primitive::kPrimVoid:
4021 LOG(FATAL) << "Unreachable type " << type;
4022 UNREACHABLE();
4023 }
4024 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004025 DCHECK(dst_loc.IsRegister());
4026 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4027 if (type == Primitive::kPrimNot) {
4028 // /* HeapReference<Object> */ dst = *(obj + offset)
4029 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4030 Location temp_loc = locations->GetTemp(0);
4031 // Note that a potential implicit null check is handled in this
4032 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4033 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4034 dst_loc,
4035 obj,
4036 offset,
4037 temp_loc,
4038 /* needs_null_check */ true);
4039 if (is_volatile) {
4040 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4041 }
4042 } else {
4043 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4044 if (is_volatile) {
4045 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4046 }
4047 // If read barriers are enabled, emit read barriers other than
4048 // Baker's using a slow path (and also unpoison the loaded
4049 // reference, if heap poisoning is enabled).
4050 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4051 }
4052 } else {
4053 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4054 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004055 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004056 DCHECK(dst_loc.IsFpuRegister());
4057 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004058 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004059 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004060
Alexey Frunze15958152017-02-09 19:08:30 -08004061 // Memory barriers, in the case of references, are handled in the
4062 // previous switch statement.
4063 if (is_volatile && (type != Primitive::kPrimNot)) {
4064 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004065 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066}
4067
4068void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4069 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4070 LocationSummary* locations =
4071 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4072 locations->SetInAt(0, Location::RequiresRegister());
4073 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004074 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004075 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004076 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004077 }
4078}
4079
4080void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004081 const FieldInfo& field_info,
4082 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004083 Primitive::Type type = field_info.GetFieldType();
4084 LocationSummary* locations = instruction->GetLocations();
4085 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004086 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004087 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004088 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004089 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4090 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004091 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4092
Alexey Frunze4dda3372015-06-01 18:31:49 -07004093 switch (type) {
4094 case Primitive::kPrimBoolean:
4095 case Primitive::kPrimByte:
4096 store_type = kStoreByte;
4097 break;
4098 case Primitive::kPrimShort:
4099 case Primitive::kPrimChar:
4100 store_type = kStoreHalfword;
4101 break;
4102 case Primitive::kPrimInt:
4103 case Primitive::kPrimFloat:
4104 case Primitive::kPrimNot:
4105 store_type = kStoreWord;
4106 break;
4107 case Primitive::kPrimLong:
4108 case Primitive::kPrimDouble:
4109 store_type = kStoreDoubleword;
4110 break;
4111 case Primitive::kPrimVoid:
4112 LOG(FATAL) << "Unreachable type " << type;
4113 UNREACHABLE();
4114 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115
Alexey Frunze15958152017-02-09 19:08:30 -08004116 if (is_volatile) {
4117 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4118 }
4119
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004120 if (value_location.IsConstant()) {
4121 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4122 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4123 } else {
4124 if (!Primitive::IsFloatingPointType(type)) {
4125 DCHECK(value_location.IsRegister());
4126 GpuRegister src = value_location.AsRegister<GpuRegister>();
4127 if (kPoisonHeapReferences && needs_write_barrier) {
4128 // Note that in the case where `value` is a null reference,
4129 // we do not enter this block, as a null reference does not
4130 // need poisoning.
4131 DCHECK_EQ(type, Primitive::kPrimNot);
4132 __ PoisonHeapReference(TMP, src);
4133 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4134 } else {
4135 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4136 }
4137 } else {
4138 DCHECK(value_location.IsFpuRegister());
4139 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4140 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4141 }
4142 }
Alexey Frunze15958152017-02-09 19:08:30 -08004143
Alexey Frunzec061de12017-02-14 13:27:23 -08004144 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004145 DCHECK(value_location.IsRegister());
4146 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004147 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004148 }
Alexey Frunze15958152017-02-09 19:08:30 -08004149
4150 if (is_volatile) {
4151 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4152 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004153}
4154
4155void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4156 HandleFieldGet(instruction, instruction->GetFieldInfo());
4157}
4158
4159void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4160 HandleFieldGet(instruction, instruction->GetFieldInfo());
4161}
4162
4163void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4164 HandleFieldSet(instruction, instruction->GetFieldInfo());
4165}
4166
4167void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004168 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004169}
4170
Alexey Frunze15958152017-02-09 19:08:30 -08004171void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4172 HInstruction* instruction,
4173 Location out,
4174 uint32_t offset,
4175 Location maybe_temp,
4176 ReadBarrierOption read_barrier_option) {
4177 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4178 if (read_barrier_option == kWithReadBarrier) {
4179 CHECK(kEmitCompilerReadBarrier);
4180 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4181 if (kUseBakerReadBarrier) {
4182 // Load with fast path based Baker's read barrier.
4183 // /* HeapReference<Object> */ out = *(out + offset)
4184 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4185 out,
4186 out_reg,
4187 offset,
4188 maybe_temp,
4189 /* needs_null_check */ false);
4190 } else {
4191 // Load with slow path based read barrier.
4192 // Save the value of `out` into `maybe_temp` before overwriting it
4193 // in the following move operation, as we will need it for the
4194 // read barrier below.
4195 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4196 // /* HeapReference<Object> */ out = *(out + offset)
4197 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4198 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4199 }
4200 } else {
4201 // Plain load with no read barrier.
4202 // /* HeapReference<Object> */ out = *(out + offset)
4203 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4204 __ MaybeUnpoisonHeapReference(out_reg);
4205 }
4206}
4207
4208void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4209 HInstruction* instruction,
4210 Location out,
4211 Location obj,
4212 uint32_t offset,
4213 Location maybe_temp,
4214 ReadBarrierOption read_barrier_option) {
4215 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4216 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4217 if (read_barrier_option == kWithReadBarrier) {
4218 CHECK(kEmitCompilerReadBarrier);
4219 if (kUseBakerReadBarrier) {
4220 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4221 // Load with fast path based Baker's read barrier.
4222 // /* HeapReference<Object> */ out = *(obj + offset)
4223 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4224 out,
4225 obj_reg,
4226 offset,
4227 maybe_temp,
4228 /* needs_null_check */ false);
4229 } else {
4230 // Load with slow path based read barrier.
4231 // /* HeapReference<Object> */ out = *(obj + offset)
4232 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4233 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4234 }
4235 } else {
4236 // Plain load with no read barrier.
4237 // /* HeapReference<Object> */ out = *(obj + offset)
4238 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4239 __ MaybeUnpoisonHeapReference(out_reg);
4240 }
4241}
4242
Alexey Frunzef63f5692016-12-13 17:43:11 -08004243void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004244 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004245 Location root,
4246 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004247 uint32_t offset,
4248 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004249 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004250 if (read_barrier_option == kWithReadBarrier) {
4251 DCHECK(kEmitCompilerReadBarrier);
4252 if (kUseBakerReadBarrier) {
4253 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4254 // Baker's read barrier are used:
4255 //
4256 // root = obj.field;
4257 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4258 // if (temp != null) {
4259 // root = temp(root)
4260 // }
4261
4262 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4263 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4264 static_assert(
4265 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4266 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4267 "have different sizes.");
4268 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4269 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4270 "have different sizes.");
4271
4272 // Slow path marking the GC root `root`.
4273 Location temp = Location::RegisterLocation(T9);
4274 SlowPathCodeMIPS64* slow_path =
4275 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4276 instruction,
4277 root,
4278 /*entrypoint*/ temp);
4279 codegen_->AddSlowPath(slow_path);
4280
4281 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4282 const int32_t entry_point_offset =
4283 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4284 // Loading the entrypoint does not require a load acquire since it is only changed when
4285 // threads are suspended or running a checkpoint.
4286 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4287 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4288 // checking GetIsGcMarking.
4289 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4290 __ Bind(slow_path->GetExitLabel());
4291 } else {
4292 // GC root loaded through a slow path for read barriers other
4293 // than Baker's.
4294 // /* GcRoot<mirror::Object>* */ root = obj + offset
4295 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4296 // /* mirror::Object* */ root = root->Read()
4297 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4298 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004299 } else {
4300 // Plain GC root load with no read barrier.
4301 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4302 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4303 // Note that GC roots are not affected by heap poisoning, thus we
4304 // do not have to unpoison `root_reg` here.
4305 }
4306}
4307
Alexey Frunze15958152017-02-09 19:08:30 -08004308void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4309 Location ref,
4310 GpuRegister obj,
4311 uint32_t offset,
4312 Location temp,
4313 bool needs_null_check) {
4314 DCHECK(kEmitCompilerReadBarrier);
4315 DCHECK(kUseBakerReadBarrier);
4316
4317 // /* HeapReference<Object> */ ref = *(obj + offset)
4318 Location no_index = Location::NoLocation();
4319 ScaleFactor no_scale_factor = TIMES_1;
4320 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4321 ref,
4322 obj,
4323 offset,
4324 no_index,
4325 no_scale_factor,
4326 temp,
4327 needs_null_check);
4328}
4329
4330void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4331 Location ref,
4332 GpuRegister obj,
4333 uint32_t data_offset,
4334 Location index,
4335 Location temp,
4336 bool needs_null_check) {
4337 DCHECK(kEmitCompilerReadBarrier);
4338 DCHECK(kUseBakerReadBarrier);
4339
4340 static_assert(
4341 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4342 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4343 // /* HeapReference<Object> */ ref =
4344 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4345 ScaleFactor scale_factor = TIMES_4;
4346 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4347 ref,
4348 obj,
4349 data_offset,
4350 index,
4351 scale_factor,
4352 temp,
4353 needs_null_check);
4354}
4355
4356void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4357 Location ref,
4358 GpuRegister obj,
4359 uint32_t offset,
4360 Location index,
4361 ScaleFactor scale_factor,
4362 Location temp,
4363 bool needs_null_check,
4364 bool always_update_field) {
4365 DCHECK(kEmitCompilerReadBarrier);
4366 DCHECK(kUseBakerReadBarrier);
4367
4368 // In slow path based read barriers, the read barrier call is
4369 // inserted after the original load. However, in fast path based
4370 // Baker's read barriers, we need to perform the load of
4371 // mirror::Object::monitor_ *before* the original reference load.
4372 // This load-load ordering is required by the read barrier.
4373 // The fast path/slow path (for Baker's algorithm) should look like:
4374 //
4375 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4376 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4377 // HeapReference<Object> ref = *src; // Original reference load.
4378 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4379 // if (is_gray) {
4380 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4381 // }
4382 //
4383 // Note: the original implementation in ReadBarrier::Barrier is
4384 // slightly more complex as it performs additional checks that we do
4385 // not do here for performance reasons.
4386
4387 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4388 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4389 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4390
4391 // /* int32_t */ monitor = obj->monitor_
4392 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4393 if (needs_null_check) {
4394 MaybeRecordImplicitNullCheck(instruction);
4395 }
4396 // /* LockWord */ lock_word = LockWord(monitor)
4397 static_assert(sizeof(LockWord) == sizeof(int32_t),
4398 "art::LockWord and int32_t have different sizes.");
4399
4400 __ Sync(0); // Barrier to prevent load-load reordering.
4401
4402 // The actual reference load.
4403 if (index.IsValid()) {
4404 // Load types involving an "index": ArrayGet,
4405 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4406 // intrinsics.
4407 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4408 if (index.IsConstant()) {
4409 size_t computed_offset =
4410 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4411 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4412 } else {
4413 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004414 if (scale_factor == TIMES_1) {
4415 __ Daddu(TMP, index_reg, obj);
4416 } else {
4417 __ Dlsa(TMP, index_reg, obj, scale_factor);
4418 }
Alexey Frunze15958152017-02-09 19:08:30 -08004419 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4420 }
4421 } else {
4422 // /* HeapReference<Object> */ ref = *(obj + offset)
4423 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4424 }
4425
4426 // Object* ref = ref_addr->AsMirrorPtr()
4427 __ MaybeUnpoisonHeapReference(ref_reg);
4428
4429 // Slow path marking the object `ref` when it is gray.
4430 SlowPathCodeMIPS64* slow_path;
4431 if (always_update_field) {
4432 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4433 // of the form `obj + field_offset`, where `obj` is a register and
4434 // `field_offset` is a register. Thus `offset` and `scale_factor`
4435 // above are expected to be null in this code path.
4436 DCHECK_EQ(offset, 0u);
4437 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4438 slow_path = new (GetGraph()->GetArena())
4439 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4440 ref,
4441 obj,
4442 /* field_offset */ index,
4443 temp_reg);
4444 } else {
4445 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4446 }
4447 AddSlowPath(slow_path);
4448
4449 // if (rb_state == ReadBarrier::GrayState())
4450 // ref = ReadBarrier::Mark(ref);
4451 // Given the numeric representation, it's enough to check the low bit of the
4452 // rb_state. We do that by shifting the bit into the sign bit (31) and
4453 // performing a branch on less than zero.
4454 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4455 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4456 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4457 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4458 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4459 __ Bind(slow_path->GetExitLabel());
4460}
4461
4462void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4463 Location out,
4464 Location ref,
4465 Location obj,
4466 uint32_t offset,
4467 Location index) {
4468 DCHECK(kEmitCompilerReadBarrier);
4469
4470 // Insert a slow path based read barrier *after* the reference load.
4471 //
4472 // If heap poisoning is enabled, the unpoisoning of the loaded
4473 // reference will be carried out by the runtime within the slow
4474 // path.
4475 //
4476 // Note that `ref` currently does not get unpoisoned (when heap
4477 // poisoning is enabled), which is alright as the `ref` argument is
4478 // not used by the artReadBarrierSlow entry point.
4479 //
4480 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4481 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4482 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4483 AddSlowPath(slow_path);
4484
4485 __ Bc(slow_path->GetEntryLabel());
4486 __ Bind(slow_path->GetExitLabel());
4487}
4488
4489void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4490 Location out,
4491 Location ref,
4492 Location obj,
4493 uint32_t offset,
4494 Location index) {
4495 if (kEmitCompilerReadBarrier) {
4496 // Baker's read barriers shall be handled by the fast path
4497 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4498 DCHECK(!kUseBakerReadBarrier);
4499 // If heap poisoning is enabled, unpoisoning will be taken care of
4500 // by the runtime within the slow path.
4501 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4502 } else if (kPoisonHeapReferences) {
4503 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4504 }
4505}
4506
4507void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4508 Location out,
4509 Location root) {
4510 DCHECK(kEmitCompilerReadBarrier);
4511
4512 // Insert a slow path based read barrier *after* the GC root load.
4513 //
4514 // Note that GC roots are not affected by heap poisoning, so we do
4515 // not need to do anything special for this here.
4516 SlowPathCodeMIPS64* slow_path =
4517 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4518 AddSlowPath(slow_path);
4519
4520 __ Bc(slow_path->GetEntryLabel());
4521 __ Bind(slow_path->GetExitLabel());
4522}
4523
Alexey Frunze4dda3372015-06-01 18:31:49 -07004524void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004525 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4526 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004527 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004528 switch (type_check_kind) {
4529 case TypeCheckKind::kExactCheck:
4530 case TypeCheckKind::kAbstractClassCheck:
4531 case TypeCheckKind::kClassHierarchyCheck:
4532 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004533 call_kind =
4534 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004535 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004536 break;
4537 case TypeCheckKind::kArrayCheck:
4538 case TypeCheckKind::kUnresolvedCheck:
4539 case TypeCheckKind::kInterfaceCheck:
4540 call_kind = LocationSummary::kCallOnSlowPath;
4541 break;
4542 }
4543
Alexey Frunze4dda3372015-06-01 18:31:49 -07004544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004545 if (baker_read_barrier_slow_path) {
4546 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4547 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004548 locations->SetInAt(0, Location::RequiresRegister());
4549 locations->SetInAt(1, Location::RequiresRegister());
4550 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004551 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004552 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004553 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004554}
4555
4556void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004557 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004558 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004559 Location obj_loc = locations->InAt(0);
4560 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004561 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004562 Location out_loc = locations->Out();
4563 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4564 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4565 DCHECK_LE(num_temps, 1u);
4566 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4568 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4569 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4570 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004571 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004572 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004573
4574 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004575 // Avoid this check if we know `obj` is not null.
4576 if (instruction->MustDoNullCheck()) {
4577 __ Move(out, ZERO);
4578 __ Beqzc(obj, &done);
4579 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004580
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004581 switch (type_check_kind) {
4582 case TypeCheckKind::kExactCheck: {
4583 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004584 GenerateReferenceLoadTwoRegisters(instruction,
4585 out_loc,
4586 obj_loc,
4587 class_offset,
4588 maybe_temp_loc,
4589 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004590 // Classes must be equal for the instanceof to succeed.
4591 __ Xor(out, out, cls);
4592 __ Sltiu(out, out, 1);
4593 break;
4594 }
4595
4596 case TypeCheckKind::kAbstractClassCheck: {
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 // If the class is abstract, we eagerly fetch the super class of the
4605 // object to avoid doing a comparison we know will fail.
4606 Mips64Label loop;
4607 __ Bind(&loop);
4608 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004609 GenerateReferenceLoadOneRegister(instruction,
4610 out_loc,
4611 super_offset,
4612 maybe_temp_loc,
4613 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004614 // If `out` is null, we use it for the result, and jump to `done`.
4615 __ Beqzc(out, &done);
4616 __ Bnec(out, cls, &loop);
4617 __ LoadConst32(out, 1);
4618 break;
4619 }
4620
4621 case TypeCheckKind::kClassHierarchyCheck: {
4622 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004623 GenerateReferenceLoadTwoRegisters(instruction,
4624 out_loc,
4625 obj_loc,
4626 class_offset,
4627 maybe_temp_loc,
4628 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004629 // Walk over the class hierarchy to find a match.
4630 Mips64Label loop, success;
4631 __ Bind(&loop);
4632 __ Beqc(out, cls, &success);
4633 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004634 GenerateReferenceLoadOneRegister(instruction,
4635 out_loc,
4636 super_offset,
4637 maybe_temp_loc,
4638 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004639 __ Bnezc(out, &loop);
4640 // If `out` is null, we use it for the result, and jump to `done`.
4641 __ Bc(&done);
4642 __ Bind(&success);
4643 __ LoadConst32(out, 1);
4644 break;
4645 }
4646
4647 case TypeCheckKind::kArrayObjectCheck: {
4648 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004649 GenerateReferenceLoadTwoRegisters(instruction,
4650 out_loc,
4651 obj_loc,
4652 class_offset,
4653 maybe_temp_loc,
4654 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004655 // Do an exact check.
4656 Mips64Label success;
4657 __ Beqc(out, cls, &success);
4658 // Otherwise, we need to check that the object's class is a non-primitive array.
4659 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004660 GenerateReferenceLoadOneRegister(instruction,
4661 out_loc,
4662 component_offset,
4663 maybe_temp_loc,
4664 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004665 // If `out` is null, we use it for the result, and jump to `done`.
4666 __ Beqzc(out, &done);
4667 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4668 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4669 __ Sltiu(out, out, 1);
4670 __ Bc(&done);
4671 __ Bind(&success);
4672 __ LoadConst32(out, 1);
4673 break;
4674 }
4675
4676 case TypeCheckKind::kArrayCheck: {
4677 // No read barrier since the slow path will retry upon failure.
4678 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004679 GenerateReferenceLoadTwoRegisters(instruction,
4680 out_loc,
4681 obj_loc,
4682 class_offset,
4683 maybe_temp_loc,
4684 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004685 DCHECK(locations->OnlyCallsOnSlowPath());
4686 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4687 /* is_fatal */ false);
4688 codegen_->AddSlowPath(slow_path);
4689 __ Bnec(out, cls, slow_path->GetEntryLabel());
4690 __ LoadConst32(out, 1);
4691 break;
4692 }
4693
4694 case TypeCheckKind::kUnresolvedCheck:
4695 case TypeCheckKind::kInterfaceCheck: {
4696 // Note that we indeed only call on slow path, but we always go
4697 // into the slow path for the unresolved and interface check
4698 // cases.
4699 //
4700 // We cannot directly call the InstanceofNonTrivial runtime
4701 // entry point without resorting to a type checking slow path
4702 // here (i.e. by calling InvokeRuntime directly), as it would
4703 // require to assign fixed registers for the inputs of this
4704 // HInstanceOf instruction (following the runtime calling
4705 // convention), which might be cluttered by the potential first
4706 // read barrier emission at the beginning of this method.
4707 //
4708 // TODO: Introduce a new runtime entry point taking the object
4709 // to test (instead of its class) as argument, and let it deal
4710 // with the read barrier issues. This will let us refactor this
4711 // case of the `switch` code as it was previously (with a direct
4712 // call to the runtime not using a type checking slow path).
4713 // This should also be beneficial for the other cases above.
4714 DCHECK(locations->OnlyCallsOnSlowPath());
4715 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4716 /* is_fatal */ false);
4717 codegen_->AddSlowPath(slow_path);
4718 __ Bc(slow_path->GetEntryLabel());
4719 break;
4720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004721 }
4722
4723 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004724
4725 if (slow_path != nullptr) {
4726 __ Bind(slow_path->GetExitLabel());
4727 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004728}
4729
4730void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4732 locations->SetOut(Location::ConstantLocation(constant));
4733}
4734
4735void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4736 // Will be generated at use site.
4737}
4738
4739void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4741 locations->SetOut(Location::ConstantLocation(constant));
4742}
4743
4744void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4745 // Will be generated at use site.
4746}
4747
Calin Juravle175dc732015-08-25 15:42:32 +01004748void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4749 // The trampoline uses the same calling convention as dex calling conventions,
4750 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4751 // the method_idx.
4752 HandleInvoke(invoke);
4753}
4754
4755void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4756 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4757}
4758
Alexey Frunze4dda3372015-06-01 18:31:49 -07004759void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4760 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4761 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4762}
4763
4764void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4765 HandleInvoke(invoke);
4766 // The register T0 is required to be used for the hidden argument in
4767 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4768 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4769}
4770
4771void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4772 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4773 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004774 Location receiver = invoke->GetLocations()->InAt(0);
4775 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004776 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777
4778 // Set the hidden argument.
4779 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4780 invoke->GetDexMethodIndex());
4781
4782 // temp = object->GetClass();
4783 if (receiver.IsStackSlot()) {
4784 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4785 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4786 } else {
4787 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4788 }
4789 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004790 // Instead of simply (possibly) unpoisoning `temp` here, we should
4791 // emit a read barrier for the previous class reference load.
4792 // However this is not required in practice, as this is an
4793 // intermediate/temporary reference and because the current
4794 // concurrent copying collector keeps the from-space memory
4795 // intact/accessible until the end of the marking phase (the
4796 // concurrent copying collector may not in the future).
4797 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004798 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4799 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4800 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004801 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004802 // temp = temp->GetImtEntryAt(method_offset);
4803 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4804 // T9 = temp->GetEntryPoint();
4805 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4806 // T9();
4807 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004808 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004809 DCHECK(!codegen_->IsLeafMethod());
4810 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4811}
4812
4813void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004814 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4815 if (intrinsic.TryDispatch(invoke)) {
4816 return;
4817 }
4818
Alexey Frunze4dda3372015-06-01 18:31:49 -07004819 HandleInvoke(invoke);
4820}
4821
4822void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004823 // Explicit clinit checks triggered by static invokes must have been pruned by
4824 // art::PrepareForRegisterAllocation.
4825 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004826
Chris Larsen3039e382015-08-26 07:54:08 -07004827 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4828 if (intrinsic.TryDispatch(invoke)) {
4829 return;
4830 }
4831
Alexey Frunze4dda3372015-06-01 18:31:49 -07004832 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004833}
4834
Orion Hodsonac141392017-01-13 11:53:47 +00004835void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4836 HandleInvoke(invoke);
4837}
4838
4839void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4840 codegen_->GenerateInvokePolymorphicCall(invoke);
4841}
4842
Chris Larsen3039e382015-08-26 07:54:08 -07004843static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004844 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004845 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4846 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847 return true;
4848 }
4849 return false;
4850}
4851
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004852HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004853 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004854 bool fallback_load = false;
4855 switch (desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004856 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004857 case HLoadString::LoadKind::kBssEntry:
4858 DCHECK(!Runtime::Current()->UseJitCompilation());
4859 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004860 case HLoadString::LoadKind::kJitTableAddress:
4861 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004862 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004863 case HLoadString::LoadKind::kBootImageAddress:
4864 case HLoadString::LoadKind::kDexCacheViaMethod:
4865 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004866 }
4867 if (fallback_load) {
4868 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4869 }
4870 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004871}
4872
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004873HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4874 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004875 bool fallback_load = false;
4876 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004877 case HLoadClass::LoadKind::kInvalid:
4878 LOG(FATAL) << "UNREACHABLE";
4879 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004880 case HLoadClass::LoadKind::kReferrersClass:
4881 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004882 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004883 case HLoadClass::LoadKind::kBssEntry:
4884 DCHECK(!Runtime::Current()->UseJitCompilation());
4885 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004886 case HLoadClass::LoadKind::kJitTableAddress:
4887 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004888 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004889 case HLoadClass::LoadKind::kBootImageAddress:
Alexey Frunzef63f5692016-12-13 17:43:11 -08004890 case HLoadClass::LoadKind::kDexCacheViaMethod:
4891 break;
4892 }
4893 if (fallback_load) {
4894 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4895 }
4896 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004897}
4898
Vladimir Markodc151b22015-10-15 18:02:30 +01004899HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4900 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004901 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004902 // On MIPS64 we support all dispatch types.
4903 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004904}
4905
Alexey Frunze4dda3372015-06-01 18:31:49 -07004906void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4907 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004908 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004909 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4910 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4911
Alexey Frunze19f6c692016-11-30 19:19:55 -08004912 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004913 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004914 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004915 uint32_t offset =
4916 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004917 __ LoadFromOffset(kLoadDoubleword,
4918 temp.AsRegister<GpuRegister>(),
4919 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004920 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004921 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004922 }
Vladimir Marko58155012015-08-19 12:49:41 +00004923 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004924 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004925 break;
4926 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004927 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4928 kLoadDoubleword,
4929 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004930 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004931 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4932 uint32_t offset = invoke->GetDexCacheArrayOffset();
4933 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004934 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004935 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4936 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4937 break;
4938 }
Vladimir Marko58155012015-08-19 12:49:41 +00004939 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004940 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004941 GpuRegister reg = temp.AsRegister<GpuRegister>();
4942 GpuRegister method_reg;
4943 if (current_method.IsRegister()) {
4944 method_reg = current_method.AsRegister<GpuRegister>();
4945 } else {
4946 // TODO: use the appropriate DCHECK() here if possible.
4947 // DCHECK(invoke->GetLocations()->Intrinsified());
4948 DCHECK(!current_method.IsValid());
4949 method_reg = reg;
4950 __ Ld(reg, SP, kCurrentMethodStackOffset);
4951 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004952
Vladimir Marko58155012015-08-19 12:49:41 +00004953 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004954 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00004955 reg,
4956 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01004957 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004958 // temp = temp[index_in_cache];
4959 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4960 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004961 __ LoadFromOffset(kLoadDoubleword,
4962 reg,
4963 reg,
4964 CodeGenerator::GetCachePointerOffset(index_in_cache));
4965 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004966 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004967 }
4968
Alexey Frunze19f6c692016-11-30 19:19:55 -08004969 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00004970 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004971 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00004972 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004973 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4974 // T9 = callee_method->entry_point_from_quick_compiled_code_;
4975 __ LoadFromOffset(kLoadDoubleword,
4976 T9,
4977 callee_method.AsRegister<GpuRegister>(),
4978 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004979 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00004980 // T9()
4981 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004982 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00004983 break;
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());
5002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5003}
5004
Alexey Frunze53afca12015-11-05 16:34:23 -08005005void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
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();
Alexey Frunze53afca12015-11-05 16:34:23 -08005037}
5038
5039void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5040 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5041 return;
5042 }
5043
5044 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005045 DCHECK(!codegen_->IsLeafMethod());
5046 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5047}
5048
5049void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005050 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5051 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
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();
5088 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5089 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 ||
5099 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5100 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 Marko41559982017-01-06 14:04:23 +00005153 case HLoadClass::LoadKind::kDexCacheViaMethod:
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);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005202 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5203 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.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005276 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5277 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