blob: d3ae3a729bdfc396dcf2339304bb0dd8f61874b7 [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 {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100307 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700310 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 }
314 }
315
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700316 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700317 DCHECK(successor_ == nullptr);
318 return &return_label_;
319 }
320
Roland Levillain46648892015-06-19 16:07:18 +0100321 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
322
Alexey Frunze4dda3372015-06-01 18:31:49 -0700323 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700324 // If not null, the block to branch to after the suspend check.
325 HBasicBlock* const successor_;
326
327 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700328 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700329
330 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
331};
332
333class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
334 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800335 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
336 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337
338 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
339 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800340
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342 DCHECK(instruction_->IsCheckCast()
343 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
344 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
345
346 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800347 if (!is_fatal_) {
348 SaveLiveRegisters(codegen, locations);
349 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700350
351 // We're moving two locations to locations that could overlap, so we need a parallel
352 // move resolver.
353 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800354 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700355 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
356 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
359 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700360 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100361 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800362 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 Primitive::Type ret_type = instruction_->GetType();
364 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
365 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 } else {
367 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800368 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
369 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 }
371
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800372 if (!is_fatal_) {
373 RestoreLiveRegisters(codegen, locations);
374 __ Bc(GetExitLabel());
375 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Roland Levillain46648892015-06-19 16:07:18 +0100378 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
379
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800380 bool IsFatal() const OVERRIDE { return is_fatal_; }
381
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 const bool is_fatal_;
384
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
386};
387
388class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
389 public:
Aart Bik42249c32016-01-07 15:33:50 -0800390 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000391 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800394 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700395 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100396 LocationSummary* locations = instruction_->GetLocations();
397 SaveLiveRegisters(codegen, locations);
398 InvokeRuntimeCallingConvention calling_convention;
399 __ LoadConst32(calling_convention.GetRegisterAt(0),
400 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufc734082016-07-19 17:18:07 +0100401 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100402 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 }
404
Roland Levillain46648892015-06-19 16:07:18 +0100405 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
406
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700408 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
409};
410
Alexey Frunze15958152017-02-09 19:08:30 -0800411class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
412 public:
413 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
414
415 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
416 LocationSummary* locations = instruction_->GetLocations();
417 __ Bind(GetEntryLabel());
418 SaveLiveRegisters(codegen, locations);
419
420 InvokeRuntimeCallingConvention calling_convention;
421 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
422 parallel_move.AddMove(
423 locations->InAt(0),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
425 Primitive::kPrimNot,
426 nullptr);
427 parallel_move.AddMove(
428 locations->InAt(1),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
430 Primitive::kPrimInt,
431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(2),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
435 Primitive::kPrimNot,
436 nullptr);
437 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
438
439 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
440 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
441 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
442 RestoreLiveRegisters(codegen, locations);
443 __ Bc(GetExitLabel());
444 }
445
446 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
447
448 private:
449 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
450};
451
452// Slow path marking an object reference `ref` during a read
453// barrier. The field `obj.field` in the object `obj` holding this
454// reference does not get updated by this slow path after marking (see
455// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
456//
457// This means that after the execution of this slow path, `ref` will
458// always be up-to-date, but `obj.field` may not; i.e., after the
459// flip, `ref` will be a to-space reference, but `obj.field` will
460// probably still be a from-space reference (unless it gets updated by
461// another thread, or if another thread installed another object
462// reference (different from `ref`) in `obj.field`).
463//
464// If `entrypoint` is a valid location it is assumed to already be
465// holding the entrypoint. The case where the entrypoint is passed in
466// is for the GcRoot read barrier.
467class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
468 public:
469 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
470 Location ref,
471 Location entrypoint = Location::NoLocation())
472 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
473 DCHECK(kEmitCompilerReadBarrier);
474 }
475
476 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
477
478 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
479 LocationSummary* locations = instruction_->GetLocations();
480 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
481 DCHECK(locations->CanCall());
482 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
483 DCHECK(instruction_->IsInstanceFieldGet() ||
484 instruction_->IsStaticFieldGet() ||
485 instruction_->IsArrayGet() ||
486 instruction_->IsArraySet() ||
487 instruction_->IsLoadClass() ||
488 instruction_->IsLoadString() ||
489 instruction_->IsInstanceOf() ||
490 instruction_->IsCheckCast() ||
491 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
492 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
497 // No need to save live registers; it's taken care of by the
498 // entrypoint. Also, there is no need to update the stack mask,
499 // as this runtime call will not trigger a garbage collection.
500 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
501 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
502 (S2 <= ref_reg && ref_reg <= S7) ||
503 (ref_reg == S8)) << ref_reg;
504 // "Compact" slow path, saving two moves.
505 //
506 // Instead of using the standard runtime calling convention (input
507 // and output in A0 and V0 respectively):
508 //
509 // A0 <- ref
510 // V0 <- ReadBarrierMark(A0)
511 // ref <- V0
512 //
513 // we just use rX (the register containing `ref`) as input and output
514 // of a dedicated entrypoint:
515 //
516 // rX <- ReadBarrierMarkRegX(rX)
517 //
518 if (entrypoint_.IsValid()) {
519 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
520 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
521 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
522 __ Nop();
523 } else {
524 int32_t entry_point_offset =
525 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
526 // This runtime call does not require a stack map.
527 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
528 instruction_,
529 this);
530 }
531 __ Bc(GetExitLabel());
532 }
533
534 private:
535 // The location (register) of the marked object reference.
536 const Location ref_;
537
538 // The location of the entrypoint if already loaded.
539 const Location entrypoint_;
540
541 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
542};
543
544// Slow path marking an object reference `ref` during a read barrier,
545// and if needed, atomically updating the field `obj.field` in the
546// object `obj` holding this reference after marking (contrary to
547// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
548// `obj.field`).
549//
550// This means that after the execution of this slow path, both `ref`
551// and `obj.field` will be up-to-date; i.e., after the flip, both will
552// hold the same to-space reference (unless another thread installed
553// another object reference (different from `ref`) in `obj.field`).
554class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
555 public:
556 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
557 Location ref,
558 GpuRegister obj,
559 Location field_offset,
560 GpuRegister temp1)
561 : SlowPathCodeMIPS64(instruction),
562 ref_(ref),
563 obj_(obj),
564 field_offset_(field_offset),
565 temp1_(temp1) {
566 DCHECK(kEmitCompilerReadBarrier);
567 }
568
569 const char* GetDescription() const OVERRIDE {
570 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
571 }
572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
576 DCHECK(locations->CanCall());
577 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
578 // This slow path is only used by the UnsafeCASObject intrinsic.
579 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
580 << "Unexpected instruction in read barrier marking and field updating slow path: "
581 << instruction_->DebugName();
582 DCHECK(instruction_->GetLocations()->Intrinsified());
583 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
584 DCHECK(field_offset_.IsRegister()) << field_offset_;
585
586 __ Bind(GetEntryLabel());
587
588 // Save the old reference.
589 // Note that we cannot use AT or TMP to save the old reference, as those
590 // are used by the code that follows, but we need the old reference after
591 // the call to the ReadBarrierMarkRegX entry point.
592 DCHECK_NE(temp1_, AT);
593 DCHECK_NE(temp1_, TMP);
594 __ Move(temp1_, ref_reg);
595
596 // No need to save live registers; it's taken care of by the
597 // entrypoint. Also, there is no need to update the stack mask,
598 // as this runtime call will not trigger a garbage collection.
599 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
600 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
601 (S2 <= ref_reg && ref_reg <= S7) ||
602 (ref_reg == S8)) << ref_reg;
603 // "Compact" slow path, saving two moves.
604 //
605 // Instead of using the standard runtime calling convention (input
606 // and output in A0 and V0 respectively):
607 //
608 // A0 <- ref
609 // V0 <- ReadBarrierMark(A0)
610 // ref <- V0
611 //
612 // we just use rX (the register containing `ref`) as input and output
613 // of a dedicated entrypoint:
614 //
615 // rX <- ReadBarrierMarkRegX(rX)
616 //
617 int32_t entry_point_offset =
618 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
619 // This runtime call does not require a stack map.
620 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
621 instruction_,
622 this);
623
624 // If the new reference is different from the old reference,
625 // update the field in the holder (`*(obj_ + field_offset_)`).
626 //
627 // Note that this field could also hold a different object, if
628 // another thread had concurrently changed it. In that case, the
629 // the compare-and-set (CAS) loop below would abort, leaving the
630 // field as-is.
631 Mips64Label done;
632 __ Beqc(temp1_, ref_reg, &done);
633
634 // Update the the holder's field atomically. This may fail if
635 // mutator updates before us, but it's OK. This is achieved
636 // using a strong compare-and-set (CAS) operation with relaxed
637 // memory synchronization ordering, where the expected value is
638 // the old reference and the desired value is the new reference.
639
640 // Convenience aliases.
641 GpuRegister base = obj_;
642 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
643 GpuRegister expected = temp1_;
644 GpuRegister value = ref_reg;
645 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
646 GpuRegister tmp = AT; // Value in memory.
647
648 __ Daddu(tmp_ptr, base, offset);
649
650 if (kPoisonHeapReferences) {
651 __ PoisonHeapReference(expected);
652 // Do not poison `value` if it is the same register as
653 // `expected`, which has just been poisoned.
654 if (value != expected) {
655 __ PoisonHeapReference(value);
656 }
657 }
658
659 // do {
660 // tmp = [r_ptr] - expected;
661 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
662
663 Mips64Label loop_head, exit_loop;
664 __ Bind(&loop_head);
665 __ Ll(tmp, tmp_ptr);
666 // The LL instruction sign-extends the 32-bit value, but
667 // 32-bit references must be zero-extended. Zero-extend `tmp`.
668 __ Dext(tmp, tmp, 0, 32);
669 __ Bnec(tmp, expected, &exit_loop);
670 __ Move(tmp, value);
671 __ Sc(tmp, tmp_ptr);
672 __ Beqzc(tmp, &loop_head);
673 __ Bind(&exit_loop);
674
675 if (kPoisonHeapReferences) {
676 __ UnpoisonHeapReference(expected);
677 // Do not unpoison `value` if it is the same register as
678 // `expected`, which has just been unpoisoned.
679 if (value != expected) {
680 __ UnpoisonHeapReference(value);
681 }
682 }
683
684 __ Bind(&done);
685 __ Bc(GetExitLabel());
686 }
687
688 private:
689 // The location (register) of the marked object reference.
690 const Location ref_;
691 // The register containing the object holding the marked object reference field.
692 const GpuRegister obj_;
693 // The location of the offset of the marked reference field within `obj_`.
694 Location field_offset_;
695
696 const GpuRegister temp1_;
697
698 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
699};
700
701// Slow path generating a read barrier for a heap reference.
702class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
703 public:
704 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
705 Location out,
706 Location ref,
707 Location obj,
708 uint32_t offset,
709 Location index)
710 : SlowPathCodeMIPS64(instruction),
711 out_(out),
712 ref_(ref),
713 obj_(obj),
714 offset_(offset),
715 index_(index) {
716 DCHECK(kEmitCompilerReadBarrier);
717 // If `obj` is equal to `out` or `ref`, it means the initial object
718 // has been overwritten by (or after) the heap object reference load
719 // to be instrumented, e.g.:
720 //
721 // __ LoadFromOffset(kLoadWord, out, out, offset);
722 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
723 //
724 // In that case, we have lost the information about the original
725 // object, and the emitted read barrier cannot work properly.
726 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
727 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
728 }
729
730 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
731 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
732 LocationSummary* locations = instruction_->GetLocations();
733 Primitive::Type type = Primitive::kPrimNot;
734 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
735 DCHECK(locations->CanCall());
736 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
737 DCHECK(instruction_->IsInstanceFieldGet() ||
738 instruction_->IsStaticFieldGet() ||
739 instruction_->IsArrayGet() ||
740 instruction_->IsInstanceOf() ||
741 instruction_->IsCheckCast() ||
742 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
743 << "Unexpected instruction in read barrier for heap reference slow path: "
744 << instruction_->DebugName();
745
746 __ Bind(GetEntryLabel());
747 SaveLiveRegisters(codegen, locations);
748
749 // We may have to change the index's value, but as `index_` is a
750 // constant member (like other "inputs" of this slow path),
751 // introduce a copy of it, `index`.
752 Location index = index_;
753 if (index_.IsValid()) {
754 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
755 if (instruction_->IsArrayGet()) {
756 // Compute the actual memory offset and store it in `index`.
757 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
758 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
759 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
760 // We are about to change the value of `index_reg` (see the
761 // calls to art::mips64::Mips64Assembler::Sll and
762 // art::mips64::MipsAssembler::Addiu32 below), but it has
763 // not been saved by the previous call to
764 // art::SlowPathCode::SaveLiveRegisters, as it is a
765 // callee-save register --
766 // art::SlowPathCode::SaveLiveRegisters does not consider
767 // callee-save registers, as it has been designed with the
768 // assumption that callee-save registers are supposed to be
769 // handled by the called function. So, as a callee-save
770 // register, `index_reg` _would_ eventually be saved onto
771 // the stack, but it would be too late: we would have
772 // changed its value earlier. Therefore, we manually save
773 // it here into another freely available register,
774 // `free_reg`, chosen of course among the caller-save
775 // registers (as a callee-save `free_reg` register would
776 // exhibit the same problem).
777 //
778 // Note we could have requested a temporary register from
779 // the register allocator instead; but we prefer not to, as
780 // this is a slow path, and we know we can find a
781 // caller-save register that is available.
782 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
783 __ Move(free_reg, index_reg);
784 index_reg = free_reg;
785 index = Location::RegisterLocation(index_reg);
786 } else {
787 // The initial register stored in `index_` has already been
788 // saved in the call to art::SlowPathCode::SaveLiveRegisters
789 // (as it is not a callee-save register), so we can freely
790 // use it.
791 }
792 // Shifting the index value contained in `index_reg` by the scale
793 // factor (2) cannot overflow in practice, as the runtime is
794 // unable to allocate object arrays with a size larger than
795 // 2^26 - 1 (that is, 2^28 - 4 bytes).
796 __ Sll(index_reg, index_reg, TIMES_4);
797 static_assert(
798 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
799 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
800 __ Addiu32(index_reg, index_reg, offset_);
801 } else {
802 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
803 // intrinsics, `index_` is not shifted by a scale factor of 2
804 // (as in the case of ArrayGet), as it is actually an offset
805 // to an object field within an object.
806 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
807 DCHECK(instruction_->GetLocations()->Intrinsified());
808 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
809 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
810 << instruction_->AsInvoke()->GetIntrinsic();
811 DCHECK_EQ(offset_, 0U);
812 DCHECK(index_.IsRegister());
813 }
814 }
815
816 // We're moving two or three locations to locations that could
817 // overlap, so we need a parallel move resolver.
818 InvokeRuntimeCallingConvention calling_convention;
819 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
820 parallel_move.AddMove(ref_,
821 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
822 Primitive::kPrimNot,
823 nullptr);
824 parallel_move.AddMove(obj_,
825 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
826 Primitive::kPrimNot,
827 nullptr);
828 if (index.IsValid()) {
829 parallel_move.AddMove(index,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
831 Primitive::kPrimInt,
832 nullptr);
833 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
834 } else {
835 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
836 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
837 }
838 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
839 instruction_,
840 instruction_->GetDexPc(),
841 this);
842 CheckEntrypointTypes<
843 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
844 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
845
846 RestoreLiveRegisters(codegen, locations);
847 __ Bc(GetExitLabel());
848 }
849
850 const char* GetDescription() const OVERRIDE {
851 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
852 }
853
854 private:
855 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
856 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
857 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
858 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
859 if (i != ref &&
860 i != obj &&
861 !codegen->IsCoreCalleeSaveRegister(i) &&
862 !codegen->IsBlockedCoreRegister(i)) {
863 return static_cast<GpuRegister>(i);
864 }
865 }
866 // We shall never fail to find a free caller-save register, as
867 // there are more than two core caller-save registers on MIPS64
868 // (meaning it is possible to find one which is different from
869 // `ref` and `obj`).
870 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
871 LOG(FATAL) << "Could not find a free caller-save register";
872 UNREACHABLE();
873 }
874
875 const Location out_;
876 const Location ref_;
877 const Location obj_;
878 const uint32_t offset_;
879 // An additional location containing an index to an array.
880 // Only used for HArrayGet and the UnsafeGetObject &
881 // UnsafeGetObjectVolatile intrinsics.
882 const Location index_;
883
884 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
885};
886
887// Slow path generating a read barrier for a GC root.
888class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
889 public:
890 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
891 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
892 DCHECK(kEmitCompilerReadBarrier);
893 }
894
895 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
896 LocationSummary* locations = instruction_->GetLocations();
897 Primitive::Type type = Primitive::kPrimNot;
898 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
899 DCHECK(locations->CanCall());
900 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
901 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
902 << "Unexpected instruction in read barrier for GC root slow path: "
903 << instruction_->DebugName();
904
905 __ Bind(GetEntryLabel());
906 SaveLiveRegisters(codegen, locations);
907
908 InvokeRuntimeCallingConvention calling_convention;
909 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
910 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
911 root_,
912 Primitive::kPrimNot);
913 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
914 instruction_,
915 instruction_->GetDexPc(),
916 this);
917 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
918 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
919
920 RestoreLiveRegisters(codegen, locations);
921 __ Bc(GetExitLabel());
922 }
923
924 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
925
926 private:
927 const Location out_;
928 const Location root_;
929
930 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
931};
932
Alexey Frunze4dda3372015-06-01 18:31:49 -0700933CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
934 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100935 const CompilerOptions& compiler_options,
936 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700937 : CodeGenerator(graph,
938 kNumberOfGpuRegisters,
939 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000940 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
942 arraysize(kCoreCalleeSaves)),
943 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
944 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100945 compiler_options,
946 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100947 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700948 location_builder_(graph, this),
949 instruction_visitor_(graph, this),
950 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100951 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800952 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800953 uint32_literals_(std::less<uint32_t>(),
954 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800955 uint64_literals_(std::less<uint64_t>(),
956 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800957 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800958 boot_image_string_patches_(StringReferenceValueComparator(),
959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
960 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
961 boot_image_type_patches_(TypeReferenceValueComparator(),
962 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
963 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000964 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800965 jit_string_patches_(StringReferenceValueComparator(),
966 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
967 jit_class_patches_(TypeReferenceValueComparator(),
968 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700969 // Save RA (containing the return address) to mimic Quick.
970 AddAllocatedRegister(Location::RegisterLocation(RA));
971}
972
973#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100974// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
975#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700976#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700977
978void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700979 // Ensure that we fix up branches.
980 __ FinalizeCode();
981
982 // Adjust native pc offsets in stack maps.
983 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800984 uint32_t old_position =
985 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700986 uint32_t new_position = __ GetAdjustedPosition(old_position);
987 DCHECK_GE(new_position, old_position);
988 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
989 }
990
991 // Adjust pc offsets for the disassembly information.
992 if (disasm_info_ != nullptr) {
993 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
994 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
995 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
996 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
997 it.second.start = __ GetAdjustedPosition(it.second.start);
998 it.second.end = __ GetAdjustedPosition(it.second.end);
999 }
1000 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1001 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1002 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1003 }
1004 }
1005
Alexey Frunze4dda3372015-06-01 18:31:49 -07001006 CodeGenerator::Finalize(allocator);
1007}
1008
1009Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1010 return codegen_->GetAssembler();
1011}
1012
1013void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001014 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001015 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1016}
1017
1018void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001019 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001020 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1021}
1022
1023void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1024 // Pop reg
1025 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001026 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001027}
1028
1029void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1030 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001031 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 __ Sd(GpuRegister(reg), SP, 0);
1033}
1034
1035void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1036 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1037 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1038 // Allocate a scratch register other than TMP, if available.
1039 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1040 // automatically unspilled when the scratch scope object is destroyed).
1041 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1042 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001043 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001044 __ LoadFromOffset(load_type,
1045 GpuRegister(ensure_scratch.GetRegister()),
1046 SP,
1047 index1 + stack_offset);
1048 __ LoadFromOffset(load_type,
1049 TMP,
1050 SP,
1051 index2 + stack_offset);
1052 __ StoreToOffset(store_type,
1053 GpuRegister(ensure_scratch.GetRegister()),
1054 SP,
1055 index2 + stack_offset);
1056 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1057}
1058
1059static dwarf::Reg DWARFReg(GpuRegister reg) {
1060 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1061}
1062
David Srbeckyba702002016-02-01 18:15:29 +00001063static dwarf::Reg DWARFReg(FpuRegister reg) {
1064 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1065}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001066
1067void CodeGeneratorMIPS64::GenerateFrameEntry() {
1068 __ Bind(&frame_entry_label_);
1069
1070 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1071
1072 if (do_overflow_check) {
1073 __ LoadFromOffset(kLoadWord,
1074 ZERO,
1075 SP,
1076 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1077 RecordPcInfo(nullptr, 0);
1078 }
1079
Alexey Frunze4dda3372015-06-01 18:31:49 -07001080 if (HasEmptyFrame()) {
1081 return;
1082 }
1083
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001084 // Make sure the frame size isn't unreasonably large.
1085 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1086 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1087 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001088
1089 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001090
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001091 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001092 __ IncreaseFrameSize(ofs);
1093
1094 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1095 GpuRegister reg = kCoreCalleeSaves[i];
1096 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001097 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001098 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001099 __ cfi().RelOffset(DWARFReg(reg), ofs);
1100 }
1101 }
1102
1103 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1104 FpuRegister reg = kFpuCalleeSaves[i];
1105 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001106 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001107 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001108 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001109 }
1110 }
1111
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001112 // Save the current method if we need it. Note that we do not
1113 // do this in HCurrentMethod, as the instruction might have been removed
1114 // in the SSA graph.
1115 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001116 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001117 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001118
1119 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1120 // Initialize should_deoptimize flag to 0.
1121 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001123}
1124
1125void CodeGeneratorMIPS64::GenerateFrameExit() {
1126 __ cfi().RememberState();
1127
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001129 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001130
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001131 // For better instruction scheduling restore RA before other registers.
1132 uint32_t ofs = GetFrameSize();
1133 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001134 GpuRegister reg = kCoreCalleeSaves[i];
1135 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001136 ofs -= kMips64DoublewordSize;
1137 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138 __ cfi().Restore(DWARFReg(reg));
1139 }
1140 }
1141
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001142 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1143 FpuRegister reg = kFpuCalleeSaves[i];
1144 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1145 ofs -= kMips64DoublewordSize;
1146 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1147 __ cfi().Restore(DWARFReg(reg));
1148 }
1149 }
1150
1151 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152 }
1153
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001154 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001155
1156 __ cfi().RestoreState();
1157 __ cfi().DefCFAOffset(GetFrameSize());
1158}
1159
1160void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1161 __ Bind(GetLabelOf(block));
1162}
1163
1164void CodeGeneratorMIPS64::MoveLocation(Location destination,
1165 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001166 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 if (source.Equals(destination)) {
1168 return;
1169 }
1170
1171 // A valid move can always be inferred from the destination and source
1172 // locations. When moving from and to a register, the argument type can be
1173 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001174 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001175 DCHECK_EQ(unspecified_type, false);
1176
1177 if (destination.IsRegister() || destination.IsFpuRegister()) {
1178 if (unspecified_type) {
1179 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1180 if (source.IsStackSlot() ||
1181 (src_cst != nullptr && (src_cst->IsIntConstant()
1182 || src_cst->IsFloatConstant()
1183 || src_cst->IsNullConstant()))) {
1184 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001185 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 } else {
1187 // If the source is a double stack slot or a 64bit constant, a 64bit
1188 // type is appropriate. Else the source is a register, and since the
1189 // type has not been specified, we chose a 64bit type to force a 64bit
1190 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001191 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001192 }
1193 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001194 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1195 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001196 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1197 // Move to GPR/FPR from stack
1198 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001199 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001200 __ LoadFpuFromOffset(load_type,
1201 destination.AsFpuRegister<FpuRegister>(),
1202 SP,
1203 source.GetStackIndex());
1204 } else {
1205 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1206 __ LoadFromOffset(load_type,
1207 destination.AsRegister<GpuRegister>(),
1208 SP,
1209 source.GetStackIndex());
1210 }
1211 } else if (source.IsConstant()) {
1212 // Move to GPR/FPR from constant
1213 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 gpr = destination.AsRegister<GpuRegister>();
1216 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001218 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1219 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1220 gpr = ZERO;
1221 } else {
1222 __ LoadConst32(gpr, value);
1223 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001225 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1226 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1227 gpr = ZERO;
1228 } else {
1229 __ LoadConst64(gpr, value);
1230 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001232 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001233 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 if (destination.IsRegister()) {
1239 // Move to GPR from GPR
1240 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1241 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 DCHECK(destination.IsFpuRegister());
1243 if (Primitive::Is64BitType(dst_type)) {
1244 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1245 } else {
1246 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1247 }
1248 }
1249 } else if (source.IsFpuRegister()) {
1250 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001253 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1254 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001256 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1257 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 } else {
1259 DCHECK(destination.IsRegister());
1260 if (Primitive::Is64BitType(dst_type)) {
1261 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1262 } else {
1263 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1264 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001265 }
1266 }
1267 } else { // The destination is not a register. It must be a stack slot.
1268 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1269 if (source.IsRegister() || source.IsFpuRegister()) {
1270 if (unspecified_type) {
1271 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001272 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275 }
1276 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001277 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1278 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001279 // Move to stack from GPR/FPR
1280 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1281 if (source.IsRegister()) {
1282 __ StoreToOffset(store_type,
1283 source.AsRegister<GpuRegister>(),
1284 SP,
1285 destination.GetStackIndex());
1286 } else {
1287 __ StoreFpuToOffset(store_type,
1288 source.AsFpuRegister<FpuRegister>(),
1289 SP,
1290 destination.GetStackIndex());
1291 }
1292 } else if (source.IsConstant()) {
1293 // Move to stack from constant
1294 HConstant* src_cst = source.GetConstant();
1295 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001296 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001298 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1299 if (value != 0) {
1300 gpr = TMP;
1301 __ LoadConst32(gpr, value);
1302 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001303 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001304 DCHECK(destination.IsDoubleStackSlot());
1305 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1306 if (value != 0) {
1307 gpr = TMP;
1308 __ LoadConst64(gpr, value);
1309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001310 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001311 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001312 } else {
1313 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1314 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1315 // Move to stack from stack
1316 if (destination.IsStackSlot()) {
1317 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1318 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1319 } else {
1320 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1321 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1322 }
1323 }
1324 }
1325}
1326
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001327void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 DCHECK(!loc1.IsConstant());
1329 DCHECK(!loc2.IsConstant());
1330
1331 if (loc1.Equals(loc2)) {
1332 return;
1333 }
1334
1335 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1336 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1337 bool is_fp_reg1 = loc1.IsFpuRegister();
1338 bool is_fp_reg2 = loc2.IsFpuRegister();
1339
1340 if (loc2.IsRegister() && loc1.IsRegister()) {
1341 // Swap 2 GPRs
1342 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1343 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1344 __ Move(TMP, r2);
1345 __ Move(r2, r1);
1346 __ Move(r1, TMP);
1347 } else if (is_fp_reg2 && is_fp_reg1) {
1348 // Swap 2 FPRs
1349 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1350 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001351 if (type == Primitive::kPrimFloat) {
1352 __ MovS(FTMP, r1);
1353 __ MovS(r1, r2);
1354 __ MovS(r2, FTMP);
1355 } else {
1356 DCHECK_EQ(type, Primitive::kPrimDouble);
1357 __ MovD(FTMP, r1);
1358 __ MovD(r1, r2);
1359 __ MovD(r2, FTMP);
1360 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001361 } else if (is_slot1 != is_slot2) {
1362 // Swap GPR/FPR and stack slot
1363 Location reg_loc = is_slot1 ? loc2 : loc1;
1364 Location mem_loc = is_slot1 ? loc1 : loc2;
1365 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1366 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1367 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1368 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1369 if (reg_loc.IsFpuRegister()) {
1370 __ StoreFpuToOffset(store_type,
1371 reg_loc.AsFpuRegister<FpuRegister>(),
1372 SP,
1373 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001374 if (mem_loc.IsStackSlot()) {
1375 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1376 } else {
1377 DCHECK(mem_loc.IsDoubleStackSlot());
1378 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1379 }
1380 } else {
1381 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1382 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1383 }
1384 } else if (is_slot1 && is_slot2) {
1385 move_resolver_.Exchange(loc1.GetStackIndex(),
1386 loc2.GetStackIndex(),
1387 loc1.IsDoubleStackSlot());
1388 } else {
1389 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1390 }
1391}
1392
Calin Juravle175dc732015-08-25 15:42:32 +01001393void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1394 DCHECK(location.IsRegister());
1395 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1396}
1397
Calin Juravlee460d1d2015-09-29 04:52:17 +01001398void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1399 if (location.IsRegister()) {
1400 locations->AddTemp(location);
1401 } else {
1402 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1403 }
1404}
1405
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001406void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1407 GpuRegister value,
1408 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001409 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 GpuRegister card = AT;
1411 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001412 if (value_can_be_null) {
1413 __ Beqzc(value, &done);
1414 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001415 __ LoadFromOffset(kLoadDoubleword,
1416 card,
1417 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001418 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1420 __ Daddu(temp, card, temp);
1421 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001422 if (value_can_be_null) {
1423 __ Bind(&done);
1424 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425}
1426
Alexey Frunze19f6c692016-11-30 19:19:55 -08001427template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1428inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1429 const ArenaDeque<PcRelativePatchInfo>& infos,
1430 ArenaVector<LinkerPatch>* linker_patches) {
1431 for (const PcRelativePatchInfo& info : infos) {
1432 const DexFile& dex_file = info.target_dex_file;
1433 size_t offset_or_index = info.offset_or_index;
1434 DCHECK(info.pc_rel_label.IsBound());
1435 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1436 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
1437 }
1438}
1439
1440void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1441 DCHECK(linker_patches->empty());
1442 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -08001443 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001444 pc_relative_string_patches_.size() +
1445 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001446 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001447 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001448 boot_image_type_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001449 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001450 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1451 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001452 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001453 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001454 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1455 linker_patches);
1456 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001457 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1458 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001459 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1460 linker_patches);
1461 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001462 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1463 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001464 for (const auto& entry : boot_image_string_patches_) {
1465 const StringReference& target_string = entry.first;
1466 Literal* literal = entry.second;
1467 DCHECK(literal->GetLabel()->IsBound());
1468 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1469 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1470 target_string.dex_file,
1471 target_string.string_index.index_));
1472 }
1473 for (const auto& entry : boot_image_type_patches_) {
1474 const TypeReference& target_type = entry.first;
1475 Literal* literal = entry.second;
1476 DCHECK(literal->GetLabel()->IsBound());
1477 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1478 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1479 target_type.dex_file,
1480 target_type.type_index.index_));
1481 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001482 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001483}
1484
1485CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001486 const DexFile& dex_file, dex::StringIndex string_index) {
1487 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001488}
1489
1490CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1491 const DexFile& dex_file, dex::TypeIndex type_index) {
1492 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001493}
1494
Vladimir Marko1998cd02017-01-13 13:02:58 +00001495CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1496 const DexFile& dex_file, dex::TypeIndex type_index) {
1497 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1498}
1499
Alexey Frunze19f6c692016-11-30 19:19:55 -08001500CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1501 const DexFile& dex_file, uint32_t element_offset) {
1502 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1503}
1504
Alexey Frunze19f6c692016-11-30 19:19:55 -08001505CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1506 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1507 patches->emplace_back(dex_file, offset_or_index);
1508 return &patches->back();
1509}
1510
Alexey Frunzef63f5692016-12-13 17:43:11 -08001511Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1512 return map->GetOrCreate(
1513 value,
1514 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1515}
1516
Alexey Frunze19f6c692016-11-30 19:19:55 -08001517Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1518 return uint64_literals_.GetOrCreate(
1519 value,
1520 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1521}
1522
1523Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1524 MethodToLiteralMap* map) {
1525 return map->GetOrCreate(
1526 target_method,
1527 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1528}
1529
Alexey Frunzef63f5692016-12-13 17:43:11 -08001530Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1531 dex::StringIndex string_index) {
1532 return boot_image_string_patches_.GetOrCreate(
1533 StringReference(&dex_file, string_index),
1534 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1535}
1536
1537Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1538 dex::TypeIndex type_index) {
1539 return boot_image_type_patches_.GetOrCreate(
1540 TypeReference(&dex_file, type_index),
1541 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1542}
1543
1544Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001545 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001546}
1547
Alexey Frunze19f6c692016-11-30 19:19:55 -08001548void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1549 GpuRegister out) {
1550 __ Bind(&info->pc_rel_label);
1551 // Add the high half of a 32-bit offset to PC.
1552 __ Auipc(out, /* placeholder */ 0x1234);
1553 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001554 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001555}
1556
Alexey Frunze627c1a02017-01-30 19:28:14 -08001557Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1558 dex::StringIndex string_index,
1559 Handle<mirror::String> handle) {
1560 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1561 reinterpret_cast64<uint64_t>(handle.GetReference()));
1562 return jit_string_patches_.GetOrCreate(
1563 StringReference(&dex_file, string_index),
1564 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1565}
1566
1567Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1568 dex::TypeIndex type_index,
1569 Handle<mirror::Class> handle) {
1570 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1571 reinterpret_cast64<uint64_t>(handle.GetReference()));
1572 return jit_class_patches_.GetOrCreate(
1573 TypeReference(&dex_file, type_index),
1574 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1575}
1576
1577void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1578 const uint8_t* roots_data,
1579 const Literal* literal,
1580 uint64_t index_in_table) const {
1581 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1582 uintptr_t address =
1583 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1584 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1585}
1586
1587void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1588 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001589 const StringReference& string_reference = entry.first;
1590 Literal* table_entry_literal = entry.second;
1591 const auto it = jit_string_roots_.find(string_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001592 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001593 uint64_t index_in_table = it->second;
1594 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001595 }
1596 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001597 const TypeReference& type_reference = entry.first;
1598 Literal* table_entry_literal = entry.second;
1599 const auto it = jit_class_roots_.find(type_reference);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001600 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001601 uint64_t index_in_table = it->second;
1602 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001603 }
1604}
1605
David Brazdil58282f42016-01-14 12:45:10 +00001606void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001607 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1608 blocked_core_registers_[ZERO] = true;
1609 blocked_core_registers_[K0] = true;
1610 blocked_core_registers_[K1] = true;
1611 blocked_core_registers_[GP] = true;
1612 blocked_core_registers_[SP] = true;
1613 blocked_core_registers_[RA] = true;
1614
Lazar Trsicd9672662015-09-03 17:33:01 +02001615 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1616 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001617 blocked_core_registers_[AT] = true;
1618 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001619 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 blocked_fpu_registers_[FTMP] = true;
1621
1622 // Reserve suspend and thread registers.
1623 blocked_core_registers_[S0] = true;
1624 blocked_core_registers_[TR] = true;
1625
1626 // Reserve T9 for function calls
1627 blocked_core_registers_[T9] = true;
1628
Goran Jakovljevic782be112016-06-21 12:39:04 +02001629 if (GetGraph()->IsDebuggable()) {
1630 // Stubs do not save callee-save floating point registers. If the graph
1631 // is debuggable, we need to deal with these registers differently. For
1632 // now, just block them.
1633 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1634 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1635 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001636 }
1637}
1638
Alexey Frunze4dda3372015-06-01 18:31:49 -07001639size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1640 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001641 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001642}
1643
1644size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1645 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001646 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001647}
1648
1649size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1650 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001651 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001652}
1653
1654size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1655 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001656 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001657}
1658
1659void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001660 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001661}
1662
1663void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001664 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001665}
1666
Calin Juravle175dc732015-08-25 15:42:32 +01001667void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001668 HInstruction* instruction,
1669 uint32_t dex_pc,
1670 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001671 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001672 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001673 if (EntrypointRequiresStackMap(entrypoint)) {
1674 RecordPcInfo(instruction, dex_pc, slow_path);
1675 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676}
1677
Alexey Frunze15958152017-02-09 19:08:30 -08001678void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1679 HInstruction* instruction,
1680 SlowPathCode* slow_path) {
1681 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1682 GenerateInvokeRuntime(entry_point_offset);
1683}
1684
1685void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1686 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1687 __ Jalr(T9);
1688 __ Nop();
1689}
1690
Alexey Frunze4dda3372015-06-01 18:31:49 -07001691void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1692 GpuRegister class_reg) {
1693 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1694 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1695 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001696 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1697 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 __ Bind(slow_path->GetExitLabel());
1699}
1700
1701void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1702 __ Sync(0); // only stype 0 is supported
1703}
1704
1705void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1706 HBasicBlock* successor) {
1707 SuspendCheckSlowPathMIPS64* slow_path =
1708 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1709 codegen_->AddSlowPath(slow_path);
1710
1711 __ LoadFromOffset(kLoadUnsignedHalfword,
1712 TMP,
1713 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001714 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001715 if (successor == nullptr) {
1716 __ Bnezc(TMP, slow_path->GetEntryLabel());
1717 __ Bind(slow_path->GetReturnLabel());
1718 } else {
1719 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001720 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001721 // slow_path will return to GetLabelOf(successor).
1722 }
1723}
1724
1725InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1726 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001727 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728 assembler_(codegen->GetAssembler()),
1729 codegen_(codegen) {}
1730
1731void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1732 DCHECK_EQ(instruction->InputCount(), 2U);
1733 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1734 Primitive::Type type = instruction->GetResultType();
1735 switch (type) {
1736 case Primitive::kPrimInt:
1737 case Primitive::kPrimLong: {
1738 locations->SetInAt(0, Location::RequiresRegister());
1739 HInstruction* right = instruction->InputAt(1);
1740 bool can_use_imm = false;
1741 if (right->IsConstant()) {
1742 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1743 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1744 can_use_imm = IsUint<16>(imm);
1745 } else if (instruction->IsAdd()) {
1746 can_use_imm = IsInt<16>(imm);
1747 } else {
1748 DCHECK(instruction->IsSub());
1749 can_use_imm = IsInt<16>(-imm);
1750 }
1751 }
1752 if (can_use_imm)
1753 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1754 else
1755 locations->SetInAt(1, Location::RequiresRegister());
1756 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1757 }
1758 break;
1759
1760 case Primitive::kPrimFloat:
1761 case Primitive::kPrimDouble:
1762 locations->SetInAt(0, Location::RequiresFpuRegister());
1763 locations->SetInAt(1, Location::RequiresFpuRegister());
1764 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1765 break;
1766
1767 default:
1768 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1769 }
1770}
1771
1772void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1773 Primitive::Type type = instruction->GetType();
1774 LocationSummary* locations = instruction->GetLocations();
1775
1776 switch (type) {
1777 case Primitive::kPrimInt:
1778 case Primitive::kPrimLong: {
1779 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1780 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1781 Location rhs_location = locations->InAt(1);
1782
1783 GpuRegister rhs_reg = ZERO;
1784 int64_t rhs_imm = 0;
1785 bool use_imm = rhs_location.IsConstant();
1786 if (use_imm) {
1787 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1788 } else {
1789 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1790 }
1791
1792 if (instruction->IsAnd()) {
1793 if (use_imm)
1794 __ Andi(dst, lhs, rhs_imm);
1795 else
1796 __ And(dst, lhs, rhs_reg);
1797 } else if (instruction->IsOr()) {
1798 if (use_imm)
1799 __ Ori(dst, lhs, rhs_imm);
1800 else
1801 __ Or(dst, lhs, rhs_reg);
1802 } else if (instruction->IsXor()) {
1803 if (use_imm)
1804 __ Xori(dst, lhs, rhs_imm);
1805 else
1806 __ Xor(dst, lhs, rhs_reg);
1807 } else if (instruction->IsAdd()) {
1808 if (type == Primitive::kPrimInt) {
1809 if (use_imm)
1810 __ Addiu(dst, lhs, rhs_imm);
1811 else
1812 __ Addu(dst, lhs, rhs_reg);
1813 } else {
1814 if (use_imm)
1815 __ Daddiu(dst, lhs, rhs_imm);
1816 else
1817 __ Daddu(dst, lhs, rhs_reg);
1818 }
1819 } else {
1820 DCHECK(instruction->IsSub());
1821 if (type == Primitive::kPrimInt) {
1822 if (use_imm)
1823 __ Addiu(dst, lhs, -rhs_imm);
1824 else
1825 __ Subu(dst, lhs, rhs_reg);
1826 } else {
1827 if (use_imm)
1828 __ Daddiu(dst, lhs, -rhs_imm);
1829 else
1830 __ Dsubu(dst, lhs, rhs_reg);
1831 }
1832 }
1833 break;
1834 }
1835 case Primitive::kPrimFloat:
1836 case Primitive::kPrimDouble: {
1837 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1838 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1839 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1840 if (instruction->IsAdd()) {
1841 if (type == Primitive::kPrimFloat)
1842 __ AddS(dst, lhs, rhs);
1843 else
1844 __ AddD(dst, lhs, rhs);
1845 } else if (instruction->IsSub()) {
1846 if (type == Primitive::kPrimFloat)
1847 __ SubS(dst, lhs, rhs);
1848 else
1849 __ SubD(dst, lhs, rhs);
1850 } else {
1851 LOG(FATAL) << "Unexpected floating-point binary operation";
1852 }
1853 break;
1854 }
1855 default:
1856 LOG(FATAL) << "Unexpected binary operation type " << type;
1857 }
1858}
1859
1860void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001861 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862
1863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1864 Primitive::Type type = instr->GetResultType();
1865 switch (type) {
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimLong: {
1868 locations->SetInAt(0, Location::RequiresRegister());
1869 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001871 break;
1872 }
1873 default:
1874 LOG(FATAL) << "Unexpected shift type " << type;
1875 }
1876}
1877
1878void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001879 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 LocationSummary* locations = instr->GetLocations();
1881 Primitive::Type type = instr->GetType();
1882
1883 switch (type) {
1884 case Primitive::kPrimInt:
1885 case Primitive::kPrimLong: {
1886 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1887 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1888 Location rhs_location = locations->InAt(1);
1889
1890 GpuRegister rhs_reg = ZERO;
1891 int64_t rhs_imm = 0;
1892 bool use_imm = rhs_location.IsConstant();
1893 if (use_imm) {
1894 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1895 } else {
1896 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1897 }
1898
1899 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001900 uint32_t shift_value = rhs_imm &
1901 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902
Alexey Frunze92d90602015-12-18 18:16:36 -08001903 if (shift_value == 0) {
1904 if (dst != lhs) {
1905 __ Move(dst, lhs);
1906 }
1907 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001908 if (instr->IsShl()) {
1909 __ Sll(dst, lhs, shift_value);
1910 } else if (instr->IsShr()) {
1911 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001912 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001914 } else {
1915 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 }
1917 } else {
1918 if (shift_value < 32) {
1919 if (instr->IsShl()) {
1920 __ Dsll(dst, lhs, shift_value);
1921 } else if (instr->IsShr()) {
1922 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001923 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001924 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001925 } else {
1926 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001927 }
1928 } else {
1929 shift_value -= 32;
1930 if (instr->IsShl()) {
1931 __ Dsll32(dst, lhs, shift_value);
1932 } else if (instr->IsShr()) {
1933 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001934 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001935 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001936 } else {
1937 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001938 }
1939 }
1940 }
1941 } else {
1942 if (type == Primitive::kPrimInt) {
1943 if (instr->IsShl()) {
1944 __ Sllv(dst, lhs, rhs_reg);
1945 } else if (instr->IsShr()) {
1946 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001947 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001948 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001949 } else {
1950 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001951 }
1952 } else {
1953 if (instr->IsShl()) {
1954 __ Dsllv(dst, lhs, rhs_reg);
1955 } else if (instr->IsShr()) {
1956 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001957 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001958 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001959 } else {
1960 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001961 }
1962 }
1963 }
1964 break;
1965 }
1966 default:
1967 LOG(FATAL) << "Unexpected shift operation type " << type;
1968 }
1969}
1970
1971void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1972 HandleBinaryOp(instruction);
1973}
1974
1975void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1976 HandleBinaryOp(instruction);
1977}
1978
1979void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1980 HandleBinaryOp(instruction);
1981}
1982
1983void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1984 HandleBinaryOp(instruction);
1985}
1986
1987void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001988 Primitive::Type type = instruction->GetType();
1989 bool object_array_get_with_read_barrier =
1990 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001991 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08001992 new (GetGraph()->GetArena()) LocationSummary(instruction,
1993 object_array_get_with_read_barrier
1994 ? LocationSummary::kCallOnSlowPath
1995 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001996 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
1997 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1998 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001999 locations->SetInAt(0, Location::RequiresRegister());
2000 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002001 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002002 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2003 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002004 // The output overlaps in the case of an object array get with
2005 // read barriers enabled: we do not want the move to overwrite the
2006 // array's location, as we need it to emit the read barrier.
2007 locations->SetOut(Location::RequiresRegister(),
2008 object_array_get_with_read_barrier
2009 ? Location::kOutputOverlap
2010 : Location::kNoOutputOverlap);
2011 }
2012 // We need a temporary register for the read barrier marking slow
2013 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2014 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2015 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002016 }
2017}
2018
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002019static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2020 auto null_checker = [codegen, instruction]() {
2021 codegen->MaybeRecordImplicitNullCheck(instruction);
2022 };
2023 return null_checker;
2024}
2025
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2027 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002028 Location obj_loc = locations->InAt(0);
2029 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2030 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002031 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002032 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002033 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002034
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002035 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002036 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2037 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 switch (type) {
2039 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002040 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002041 if (index.IsConstant()) {
2042 size_t offset =
2043 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002044 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002045 } else {
2046 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002047 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002048 }
2049 break;
2050 }
2051
2052 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002053 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002054 if (index.IsConstant()) {
2055 size_t offset =
2056 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002057 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058 } else {
2059 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002060 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 }
2062 break;
2063 }
2064
2065 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002066 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002067 if (index.IsConstant()) {
2068 size_t offset =
2069 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002070 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002071 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002072 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002073 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 }
2075 break;
2076 }
2077
2078 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002079 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002080 if (maybe_compressed_char_at) {
2081 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002082 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002083 __ Dext(TMP, TMP, 0, 1);
2084 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2085 "Expecting 0=compressed, 1=uncompressed");
2086 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002088 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2089 if (maybe_compressed_char_at) {
2090 Mips64Label uncompressed_load, done;
2091 __ Bnezc(TMP, &uncompressed_load);
2092 __ LoadFromOffset(kLoadUnsignedByte,
2093 out,
2094 obj,
2095 data_offset + (const_index << TIMES_1));
2096 __ Bc(&done);
2097 __ Bind(&uncompressed_load);
2098 __ LoadFromOffset(kLoadUnsignedHalfword,
2099 out,
2100 obj,
2101 data_offset + (const_index << TIMES_2));
2102 __ Bind(&done);
2103 } else {
2104 __ LoadFromOffset(kLoadUnsignedHalfword,
2105 out,
2106 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002107 data_offset + (const_index << TIMES_2),
2108 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002109 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002110 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002111 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2112 if (maybe_compressed_char_at) {
2113 Mips64Label uncompressed_load, done;
2114 __ Bnezc(TMP, &uncompressed_load);
2115 __ Daddu(TMP, obj, index_reg);
2116 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2117 __ Bc(&done);
2118 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002119 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002120 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2121 __ Bind(&done);
2122 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002123 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002124 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002125 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126 }
2127 break;
2128 }
2129
Alexey Frunze15958152017-02-09 19:08:30 -08002130 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002132 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002133 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2134 if (index.IsConstant()) {
2135 size_t offset =
2136 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002137 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002138 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002139 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002140 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002141 }
2142 break;
2143 }
2144
Alexey Frunze15958152017-02-09 19:08:30 -08002145 case Primitive::kPrimNot: {
2146 static_assert(
2147 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2148 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2149 // /* HeapReference<Object> */ out =
2150 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2151 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2152 Location temp = locations->GetTemp(0);
2153 // Note that a potential implicit null check is handled in this
2154 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2155 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2156 out_loc,
2157 obj,
2158 data_offset,
2159 index,
2160 temp,
2161 /* needs_null_check */ true);
2162 } else {
2163 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2164 if (index.IsConstant()) {
2165 size_t offset =
2166 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2167 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2168 // If read barriers are enabled, emit read barriers other than
2169 // Baker's using a slow path (and also unpoison the loaded
2170 // reference, if heap poisoning is enabled).
2171 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2172 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002173 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002174 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2175 // If read barriers are enabled, emit read barriers other than
2176 // Baker's using a slow path (and also unpoison the loaded
2177 // reference, if heap poisoning is enabled).
2178 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2179 out_loc,
2180 out_loc,
2181 obj_loc,
2182 data_offset,
2183 index);
2184 }
2185 }
2186 break;
2187 }
2188
Alexey Frunze4dda3372015-06-01 18:31:49 -07002189 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002190 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002191 if (index.IsConstant()) {
2192 size_t offset =
2193 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002194 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002196 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002197 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002198 }
2199 break;
2200 }
2201
2202 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002203 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 if (index.IsConstant()) {
2205 size_t offset =
2206 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002207 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002208 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002209 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002210 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 }
2212 break;
2213 }
2214
2215 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002216 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002217 if (index.IsConstant()) {
2218 size_t offset =
2219 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002220 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002222 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002223 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002224 }
2225 break;
2226 }
2227
2228 case Primitive::kPrimVoid:
2229 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2230 UNREACHABLE();
2231 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002232}
2233
2234void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2236 locations->SetInAt(0, Location::RequiresRegister());
2237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2238}
2239
2240void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2241 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002242 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2244 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2245 __ LoadFromOffset(kLoadWord, out, obj, offset);
2246 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002247 // Mask out compression flag from String's array length.
2248 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2249 __ Srl(out, out, 1u);
2250 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002251}
2252
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002253Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2254 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2255 ? Location::ConstantLocation(instruction->AsConstant())
2256 : Location::RequiresRegister();
2257}
2258
2259Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2260 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2261 // We can store a non-zero float or double constant without first loading it into the FPU,
2262 // but we should only prefer this if the constant has a single use.
2263 if (instruction->IsConstant() &&
2264 (instruction->AsConstant()->IsZeroBitPattern() ||
2265 instruction->GetUses().HasExactlyOneElement())) {
2266 return Location::ConstantLocation(instruction->AsConstant());
2267 // Otherwise fall through and require an FPU register for the constant.
2268 }
2269 return Location::RequiresFpuRegister();
2270}
2271
Alexey Frunze4dda3372015-06-01 18:31:49 -07002272void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002273 Primitive::Type value_type = instruction->GetComponentType();
2274
2275 bool needs_write_barrier =
2276 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2277 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2278
Alexey Frunze4dda3372015-06-01 18:31:49 -07002279 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2280 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002281 may_need_runtime_call_for_type_check ?
2282 LocationSummary::kCallOnSlowPath :
2283 LocationSummary::kNoCall);
2284
2285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2287 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2288 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002289 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002290 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2291 }
2292 if (needs_write_barrier) {
2293 // Temporary register for the write barrier.
2294 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002295 }
2296}
2297
2298void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2299 LocationSummary* locations = instruction->GetLocations();
2300 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2301 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002302 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002304 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002305 bool needs_write_barrier =
2306 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002307 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002308 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309
2310 switch (value_type) {
2311 case Primitive::kPrimBoolean:
2312 case Primitive::kPrimByte: {
2313 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002314 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002315 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002316 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002317 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2318 }
2319 if (value_location.IsConstant()) {
2320 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2321 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2322 } else {
2323 GpuRegister value = value_location.AsRegister<GpuRegister>();
2324 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002325 }
2326 break;
2327 }
2328
2329 case Primitive::kPrimShort:
2330 case Primitive::kPrimChar: {
2331 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002332 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002333 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
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_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002336 }
2337 if (value_location.IsConstant()) {
2338 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2339 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2340 } else {
2341 GpuRegister value = value_location.AsRegister<GpuRegister>();
2342 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 }
2344 break;
2345 }
2346
Alexey Frunze15958152017-02-09 19:08:30 -08002347 case Primitive::kPrimInt: {
2348 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2349 if (index.IsConstant()) {
2350 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2351 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002352 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002353 }
2354 if (value_location.IsConstant()) {
2355 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2356 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2357 } else {
2358 GpuRegister value = value_location.AsRegister<GpuRegister>();
2359 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2360 }
2361 break;
2362 }
2363
Alexey Frunze4dda3372015-06-01 18:31:49 -07002364 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002365 if (value_location.IsConstant()) {
2366 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002367 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002368 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002369 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002371 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002372 }
Alexey Frunze15958152017-02-09 19:08:30 -08002373 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2374 DCHECK_EQ(value, 0);
2375 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2376 DCHECK(!needs_write_barrier);
2377 DCHECK(!may_need_runtime_call_for_type_check);
2378 break;
2379 }
2380
2381 DCHECK(needs_write_barrier);
2382 GpuRegister value = value_location.AsRegister<GpuRegister>();
2383 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2384 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2385 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2386 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2387 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2388 Mips64Label done;
2389 SlowPathCodeMIPS64* slow_path = nullptr;
2390
2391 if (may_need_runtime_call_for_type_check) {
2392 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2393 codegen_->AddSlowPath(slow_path);
2394 if (instruction->GetValueCanBeNull()) {
2395 Mips64Label non_zero;
2396 __ Bnezc(value, &non_zero);
2397 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2398 if (index.IsConstant()) {
2399 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002400 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002401 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002402 }
Alexey Frunze15958152017-02-09 19:08:30 -08002403 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2404 __ Bc(&done);
2405 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002406 }
Alexey Frunze15958152017-02-09 19:08:30 -08002407
2408 // Note that when read barriers are enabled, the type checks
2409 // are performed without read barriers. This is fine, even in
2410 // the case where a class object is in the from-space after
2411 // the flip, as a comparison involving such a type would not
2412 // produce a false positive; it may of course produce a false
2413 // negative, in which case we would take the ArraySet slow
2414 // path.
2415
2416 // /* HeapReference<Class> */ temp1 = obj->klass_
2417 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2418 __ MaybeUnpoisonHeapReference(temp1);
2419
2420 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2421 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2422 // /* HeapReference<Class> */ temp2 = value->klass_
2423 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2424 // If heap poisoning is enabled, no need to unpoison `temp1`
2425 // nor `temp2`, as we are comparing two poisoned references.
2426
2427 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2428 Mips64Label do_put;
2429 __ Beqc(temp1, temp2, &do_put);
2430 // If heap poisoning is enabled, the `temp1` reference has
2431 // not been unpoisoned yet; unpoison it now.
2432 __ MaybeUnpoisonHeapReference(temp1);
2433
2434 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2435 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2436 // If heap poisoning is enabled, no need to unpoison
2437 // `temp1`, as we are comparing against null below.
2438 __ Bnezc(temp1, slow_path->GetEntryLabel());
2439 __ Bind(&do_put);
2440 } else {
2441 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2442 }
2443 }
2444
2445 GpuRegister source = value;
2446 if (kPoisonHeapReferences) {
2447 // Note that in the case where `value` is a null reference,
2448 // we do not enter this block, as a null reference does not
2449 // need poisoning.
2450 __ Move(temp1, value);
2451 __ PoisonHeapReference(temp1);
2452 source = temp1;
2453 }
2454
2455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2456 if (index.IsConstant()) {
2457 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002458 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002459 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002460 }
2461 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2462
2463 if (!may_need_runtime_call_for_type_check) {
2464 codegen_->MaybeRecordImplicitNullCheck(instruction);
2465 }
2466
2467 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2468
2469 if (done.IsLinked()) {
2470 __ Bind(&done);
2471 }
2472
2473 if (slow_path != nullptr) {
2474 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002475 }
2476 break;
2477 }
2478
2479 case Primitive::kPrimLong: {
2480 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002481 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002482 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002483 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002484 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002485 }
2486 if (value_location.IsConstant()) {
2487 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2488 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2489 } else {
2490 GpuRegister value = value_location.AsRegister<GpuRegister>();
2491 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002492 }
2493 break;
2494 }
2495
2496 case Primitive::kPrimFloat: {
2497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002498 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002499 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002501 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002502 }
2503 if (value_location.IsConstant()) {
2504 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2505 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2506 } else {
2507 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2508 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002509 }
2510 break;
2511 }
2512
2513 case Primitive::kPrimDouble: {
2514 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002515 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002516 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002518 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002519 }
2520 if (value_location.IsConstant()) {
2521 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2522 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2523 } else {
2524 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2525 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002526 }
2527 break;
2528 }
2529
2530 case Primitive::kPrimVoid:
2531 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2532 UNREACHABLE();
2533 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002534}
2535
2536void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002537 RegisterSet caller_saves = RegisterSet::Empty();
2538 InvokeRuntimeCallingConvention calling_convention;
2539 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2540 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2541 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002544}
2545
2546void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2547 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002548 BoundsCheckSlowPathMIPS64* slow_path =
2549 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550 codegen_->AddSlowPath(slow_path);
2551
2552 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2553 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2554
2555 // length is limited by the maximum positive signed 32-bit integer.
2556 // Unsigned comparison of length and index checks for index < 0
2557 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002558 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002559}
2560
Alexey Frunze15958152017-02-09 19:08:30 -08002561// Temp is used for read barrier.
2562static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2563 if (kEmitCompilerReadBarrier &&
2564 (kUseBakerReadBarrier ||
2565 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2566 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2567 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2568 return 1;
2569 }
2570 return 0;
2571}
2572
2573// Extra temp is used for read barrier.
2574static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2575 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2576}
2577
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002579 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2580 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2581
2582 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2583 switch (type_check_kind) {
2584 case TypeCheckKind::kExactCheck:
2585 case TypeCheckKind::kAbstractClassCheck:
2586 case TypeCheckKind::kClassHierarchyCheck:
2587 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002588 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002589 ? LocationSummary::kCallOnSlowPath
2590 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2591 break;
2592 case TypeCheckKind::kArrayCheck:
2593 case TypeCheckKind::kUnresolvedCheck:
2594 case TypeCheckKind::kInterfaceCheck:
2595 call_kind = LocationSummary::kCallOnSlowPath;
2596 break;
2597 }
2598
2599 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002600 locations->SetInAt(0, Location::RequiresRegister());
2601 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002602 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603}
2604
2605void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002606 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002607 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002608 Location obj_loc = locations->InAt(0);
2609 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002610 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002611 Location temp_loc = locations->GetTemp(0);
2612 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2613 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2614 DCHECK_LE(num_temps, 2u);
2615 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002616 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2617 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2618 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2619 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2620 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2621 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2622 const uint32_t object_array_data_offset =
2623 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2624 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002625
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002626 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2627 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2628 // read barriers is done for performance and code size reasons.
2629 bool is_type_check_slow_path_fatal = false;
2630 if (!kEmitCompilerReadBarrier) {
2631 is_type_check_slow_path_fatal =
2632 (type_check_kind == TypeCheckKind::kExactCheck ||
2633 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2634 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2635 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2636 !instruction->CanThrowIntoCatchBlock();
2637 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002638 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002639 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2640 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002641 codegen_->AddSlowPath(slow_path);
2642
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002643 // Avoid this check if we know `obj` is not null.
2644 if (instruction->MustDoNullCheck()) {
2645 __ Beqzc(obj, &done);
2646 }
2647
2648 switch (type_check_kind) {
2649 case TypeCheckKind::kExactCheck:
2650 case TypeCheckKind::kArrayCheck: {
2651 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002652 GenerateReferenceLoadTwoRegisters(instruction,
2653 temp_loc,
2654 obj_loc,
2655 class_offset,
2656 maybe_temp2_loc,
2657 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002658 // Jump to slow path for throwing the exception or doing a
2659 // more involved array check.
2660 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2661 break;
2662 }
2663
2664 case TypeCheckKind::kAbstractClassCheck: {
2665 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002666 GenerateReferenceLoadTwoRegisters(instruction,
2667 temp_loc,
2668 obj_loc,
2669 class_offset,
2670 maybe_temp2_loc,
2671 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002672 // If the class is abstract, we eagerly fetch the super class of the
2673 // object to avoid doing a comparison we know will fail.
2674 Mips64Label loop;
2675 __ Bind(&loop);
2676 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002677 GenerateReferenceLoadOneRegister(instruction,
2678 temp_loc,
2679 super_offset,
2680 maybe_temp2_loc,
2681 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002682 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2683 // exception.
2684 __ Beqzc(temp, slow_path->GetEntryLabel());
2685 // Otherwise, compare the classes.
2686 __ Bnec(temp, cls, &loop);
2687 break;
2688 }
2689
2690 case TypeCheckKind::kClassHierarchyCheck: {
2691 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002692 GenerateReferenceLoadTwoRegisters(instruction,
2693 temp_loc,
2694 obj_loc,
2695 class_offset,
2696 maybe_temp2_loc,
2697 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002698 // Walk over the class hierarchy to find a match.
2699 Mips64Label loop;
2700 __ Bind(&loop);
2701 __ Beqc(temp, cls, &done);
2702 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002703 GenerateReferenceLoadOneRegister(instruction,
2704 temp_loc,
2705 super_offset,
2706 maybe_temp2_loc,
2707 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002708 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2709 // exception. Otherwise, jump to the beginning of the loop.
2710 __ Bnezc(temp, &loop);
2711 __ Bc(slow_path->GetEntryLabel());
2712 break;
2713 }
2714
2715 case TypeCheckKind::kArrayObjectCheck: {
2716 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002717 GenerateReferenceLoadTwoRegisters(instruction,
2718 temp_loc,
2719 obj_loc,
2720 class_offset,
2721 maybe_temp2_loc,
2722 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002723 // Do an exact check.
2724 __ Beqc(temp, cls, &done);
2725 // Otherwise, we need to check that the object's class is a non-primitive array.
2726 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002727 GenerateReferenceLoadOneRegister(instruction,
2728 temp_loc,
2729 component_offset,
2730 maybe_temp2_loc,
2731 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002732 // If the component type is null, jump to the slow path to throw the exception.
2733 __ Beqzc(temp, slow_path->GetEntryLabel());
2734 // Otherwise, the object is indeed an array, further check that this component
2735 // type is not a primitive type.
2736 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2737 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2738 __ Bnezc(temp, slow_path->GetEntryLabel());
2739 break;
2740 }
2741
2742 case TypeCheckKind::kUnresolvedCheck:
2743 // We always go into the type check slow path for the unresolved check case.
2744 // We cannot directly call the CheckCast runtime entry point
2745 // without resorting to a type checking slow path here (i.e. by
2746 // calling InvokeRuntime directly), as it would require to
2747 // assign fixed registers for the inputs of this HInstanceOf
2748 // instruction (following the runtime calling convention), which
2749 // might be cluttered by the potential first read barrier
2750 // emission at the beginning of this method.
2751 __ Bc(slow_path->GetEntryLabel());
2752 break;
2753
2754 case TypeCheckKind::kInterfaceCheck: {
2755 // Avoid read barriers to improve performance of the fast path. We can not get false
2756 // positives by doing this.
2757 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002758 GenerateReferenceLoadTwoRegisters(instruction,
2759 temp_loc,
2760 obj_loc,
2761 class_offset,
2762 maybe_temp2_loc,
2763 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002764 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002765 GenerateReferenceLoadTwoRegisters(instruction,
2766 temp_loc,
2767 temp_loc,
2768 iftable_offset,
2769 maybe_temp2_loc,
2770 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002771 // Iftable is never null.
2772 __ Lw(TMP, temp, array_length_offset);
2773 // Loop through the iftable and check if any class matches.
2774 Mips64Label loop;
2775 __ Bind(&loop);
2776 __ Beqzc(TMP, slow_path->GetEntryLabel());
2777 __ Lwu(AT, temp, object_array_data_offset);
2778 __ MaybeUnpoisonHeapReference(AT);
2779 // Go to next interface.
2780 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2781 __ Addiu(TMP, TMP, -2);
2782 // Compare the classes and continue the loop if they do not match.
2783 __ Bnec(AT, cls, &loop);
2784 break;
2785 }
2786 }
2787
2788 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002789 __ Bind(slow_path->GetExitLabel());
2790}
2791
2792void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2793 LocationSummary* locations =
2794 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2795 locations->SetInAt(0, Location::RequiresRegister());
2796 if (check->HasUses()) {
2797 locations->SetOut(Location::SameAsFirstInput());
2798 }
2799}
2800
2801void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2802 // We assume the class is not null.
2803 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2804 check->GetLoadClass(),
2805 check,
2806 check->GetDexPc(),
2807 true);
2808 codegen_->AddSlowPath(slow_path);
2809 GenerateClassInitializationCheck(slow_path,
2810 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2811}
2812
2813void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2814 Primitive::Type in_type = compare->InputAt(0)->GetType();
2815
Alexey Frunze299a9392015-12-08 16:08:02 -08002816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817
2818 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002819 case Primitive::kPrimBoolean:
2820 case Primitive::kPrimByte:
2821 case Primitive::kPrimShort:
2822 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002823 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002824 case Primitive::kPrimLong:
2825 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002826 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2828 break;
2829
2830 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002831 case Primitive::kPrimDouble:
2832 locations->SetInAt(0, Location::RequiresFpuRegister());
2833 locations->SetInAt(1, Location::RequiresFpuRegister());
2834 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002835 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002836
2837 default:
2838 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2839 }
2840}
2841
2842void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2843 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002844 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2846
2847 // 0 if: left == right
2848 // 1 if: left > right
2849 // -1 if: left < right
2850 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002851 case Primitive::kPrimBoolean:
2852 case Primitive::kPrimByte:
2853 case Primitive::kPrimShort:
2854 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002855 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002856 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002857 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002858 Location rhs_location = locations->InAt(1);
2859 bool use_imm = rhs_location.IsConstant();
2860 GpuRegister rhs = ZERO;
2861 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002862 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002863 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2864 if (value != 0) {
2865 rhs = AT;
2866 __ LoadConst64(rhs, value);
2867 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002868 } else {
2869 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2870 if (value != 0) {
2871 rhs = AT;
2872 __ LoadConst32(rhs, value);
2873 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002874 }
2875 } else {
2876 rhs = rhs_location.AsRegister<GpuRegister>();
2877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002878 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002879 __ Slt(res, rhs, lhs);
2880 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002881 break;
2882 }
2883
Alexey Frunze299a9392015-12-08 16:08:02 -08002884 case Primitive::kPrimFloat: {
2885 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2886 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2887 Mips64Label done;
2888 __ CmpEqS(FTMP, lhs, rhs);
2889 __ LoadConst32(res, 0);
2890 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002891 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002892 __ CmpLtS(FTMP, lhs, rhs);
2893 __ LoadConst32(res, -1);
2894 __ Bc1nez(FTMP, &done);
2895 __ LoadConst32(res, 1);
2896 } else {
2897 __ CmpLtS(FTMP, rhs, lhs);
2898 __ LoadConst32(res, 1);
2899 __ Bc1nez(FTMP, &done);
2900 __ LoadConst32(res, -1);
2901 }
2902 __ Bind(&done);
2903 break;
2904 }
2905
Alexey Frunze4dda3372015-06-01 18:31:49 -07002906 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002907 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2908 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2909 Mips64Label done;
2910 __ CmpEqD(FTMP, lhs, rhs);
2911 __ LoadConst32(res, 0);
2912 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002913 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002914 __ CmpLtD(FTMP, lhs, rhs);
2915 __ LoadConst32(res, -1);
2916 __ Bc1nez(FTMP, &done);
2917 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002918 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002919 __ CmpLtD(FTMP, rhs, lhs);
2920 __ LoadConst32(res, 1);
2921 __ Bc1nez(FTMP, &done);
2922 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002924 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002925 break;
2926 }
2927
2928 default:
2929 LOG(FATAL) << "Unimplemented compare type " << in_type;
2930 }
2931}
2932
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002933void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002934 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002935 switch (instruction->InputAt(0)->GetType()) {
2936 default:
2937 case Primitive::kPrimLong:
2938 locations->SetInAt(0, Location::RequiresRegister());
2939 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2940 break;
2941
2942 case Primitive::kPrimFloat:
2943 case Primitive::kPrimDouble:
2944 locations->SetInAt(0, Location::RequiresFpuRegister());
2945 locations->SetInAt(1, Location::RequiresFpuRegister());
2946 break;
2947 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002948 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2950 }
2951}
2952
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002953void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002954 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 return;
2956 }
2957
Alexey Frunze299a9392015-12-08 16:08:02 -08002958 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002959 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002960 switch (type) {
2961 default:
2962 // Integer case.
2963 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2964 return;
2965 case Primitive::kPrimLong:
2966 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2967 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002968 case Primitive::kPrimFloat:
2969 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002970 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2971 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972 }
2973}
2974
Alexey Frunzec857c742015-09-23 15:12:39 -07002975void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2976 DCHECK(instruction->IsDiv() || instruction->IsRem());
2977 Primitive::Type type = instruction->GetResultType();
2978
2979 LocationSummary* locations = instruction->GetLocations();
2980 Location second = locations->InAt(1);
2981 DCHECK(second.IsConstant());
2982
2983 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2984 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2985 int64_t imm = Int64FromConstant(second.GetConstant());
2986 DCHECK(imm == 1 || imm == -1);
2987
2988 if (instruction->IsRem()) {
2989 __ Move(out, ZERO);
2990 } else {
2991 if (imm == -1) {
2992 if (type == Primitive::kPrimInt) {
2993 __ Subu(out, ZERO, dividend);
2994 } else {
2995 DCHECK_EQ(type, Primitive::kPrimLong);
2996 __ Dsubu(out, ZERO, dividend);
2997 }
2998 } else if (out != dividend) {
2999 __ Move(out, dividend);
3000 }
3001 }
3002}
3003
3004void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3005 DCHECK(instruction->IsDiv() || instruction->IsRem());
3006 Primitive::Type type = instruction->GetResultType();
3007
3008 LocationSummary* locations = instruction->GetLocations();
3009 Location second = locations->InAt(1);
3010 DCHECK(second.IsConstant());
3011
3012 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3013 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3014 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003015 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003016 int ctz_imm = CTZ(abs_imm);
3017
3018 if (instruction->IsDiv()) {
3019 if (type == Primitive::kPrimInt) {
3020 if (ctz_imm == 1) {
3021 // Fast path for division by +/-2, which is very common.
3022 __ Srl(TMP, dividend, 31);
3023 } else {
3024 __ Sra(TMP, dividend, 31);
3025 __ Srl(TMP, TMP, 32 - ctz_imm);
3026 }
3027 __ Addu(out, dividend, TMP);
3028 __ Sra(out, out, ctz_imm);
3029 if (imm < 0) {
3030 __ Subu(out, ZERO, out);
3031 }
3032 } else {
3033 DCHECK_EQ(type, Primitive::kPrimLong);
3034 if (ctz_imm == 1) {
3035 // Fast path for division by +/-2, which is very common.
3036 __ Dsrl32(TMP, dividend, 31);
3037 } else {
3038 __ Dsra32(TMP, dividend, 31);
3039 if (ctz_imm > 32) {
3040 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3041 } else {
3042 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3043 }
3044 }
3045 __ Daddu(out, dividend, TMP);
3046 if (ctz_imm < 32) {
3047 __ Dsra(out, out, ctz_imm);
3048 } else {
3049 __ Dsra32(out, out, ctz_imm - 32);
3050 }
3051 if (imm < 0) {
3052 __ Dsubu(out, ZERO, out);
3053 }
3054 }
3055 } else {
3056 if (type == Primitive::kPrimInt) {
3057 if (ctz_imm == 1) {
3058 // Fast path for modulo +/-2, which is very common.
3059 __ Sra(TMP, dividend, 31);
3060 __ Subu(out, dividend, TMP);
3061 __ Andi(out, out, 1);
3062 __ Addu(out, out, TMP);
3063 } else {
3064 __ Sra(TMP, dividend, 31);
3065 __ Srl(TMP, TMP, 32 - ctz_imm);
3066 __ Addu(out, dividend, TMP);
3067 if (IsUint<16>(abs_imm - 1)) {
3068 __ Andi(out, out, abs_imm - 1);
3069 } else {
3070 __ Sll(out, out, 32 - ctz_imm);
3071 __ Srl(out, out, 32 - ctz_imm);
3072 }
3073 __ Subu(out, out, TMP);
3074 }
3075 } else {
3076 DCHECK_EQ(type, Primitive::kPrimLong);
3077 if (ctz_imm == 1) {
3078 // Fast path for modulo +/-2, which is very common.
3079 __ Dsra32(TMP, dividend, 31);
3080 __ Dsubu(out, dividend, TMP);
3081 __ Andi(out, out, 1);
3082 __ Daddu(out, out, TMP);
3083 } else {
3084 __ Dsra32(TMP, dividend, 31);
3085 if (ctz_imm > 32) {
3086 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3087 } else {
3088 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3089 }
3090 __ Daddu(out, dividend, TMP);
3091 if (IsUint<16>(abs_imm - 1)) {
3092 __ Andi(out, out, abs_imm - 1);
3093 } else {
3094 if (ctz_imm > 32) {
3095 __ Dsll(out, out, 64 - ctz_imm);
3096 __ Dsrl(out, out, 64 - ctz_imm);
3097 } else {
3098 __ Dsll32(out, out, 32 - ctz_imm);
3099 __ Dsrl32(out, out, 32 - ctz_imm);
3100 }
3101 }
3102 __ Dsubu(out, out, TMP);
3103 }
3104 }
3105 }
3106}
3107
3108void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3109 DCHECK(instruction->IsDiv() || instruction->IsRem());
3110
3111 LocationSummary* locations = instruction->GetLocations();
3112 Location second = locations->InAt(1);
3113 DCHECK(second.IsConstant());
3114
3115 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3116 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3117 int64_t imm = Int64FromConstant(second.GetConstant());
3118
3119 Primitive::Type type = instruction->GetResultType();
3120 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3121
3122 int64_t magic;
3123 int shift;
3124 CalculateMagicAndShiftForDivRem(imm,
3125 (type == Primitive::kPrimLong),
3126 &magic,
3127 &shift);
3128
3129 if (type == Primitive::kPrimInt) {
3130 __ LoadConst32(TMP, magic);
3131 __ MuhR6(TMP, dividend, TMP);
3132
3133 if (imm > 0 && magic < 0) {
3134 __ Addu(TMP, TMP, dividend);
3135 } else if (imm < 0 && magic > 0) {
3136 __ Subu(TMP, TMP, dividend);
3137 }
3138
3139 if (shift != 0) {
3140 __ Sra(TMP, TMP, shift);
3141 }
3142
3143 if (instruction->IsDiv()) {
3144 __ Sra(out, TMP, 31);
3145 __ Subu(out, TMP, out);
3146 } else {
3147 __ Sra(AT, TMP, 31);
3148 __ Subu(AT, TMP, AT);
3149 __ LoadConst32(TMP, imm);
3150 __ MulR6(TMP, AT, TMP);
3151 __ Subu(out, dividend, TMP);
3152 }
3153 } else {
3154 __ LoadConst64(TMP, magic);
3155 __ Dmuh(TMP, dividend, TMP);
3156
3157 if (imm > 0 && magic < 0) {
3158 __ Daddu(TMP, TMP, dividend);
3159 } else if (imm < 0 && magic > 0) {
3160 __ Dsubu(TMP, TMP, dividend);
3161 }
3162
3163 if (shift >= 32) {
3164 __ Dsra32(TMP, TMP, shift - 32);
3165 } else if (shift > 0) {
3166 __ Dsra(TMP, TMP, shift);
3167 }
3168
3169 if (instruction->IsDiv()) {
3170 __ Dsra32(out, TMP, 31);
3171 __ Dsubu(out, TMP, out);
3172 } else {
3173 __ Dsra32(AT, TMP, 31);
3174 __ Dsubu(AT, TMP, AT);
3175 __ LoadConst64(TMP, imm);
3176 __ Dmul(TMP, AT, TMP);
3177 __ Dsubu(out, dividend, TMP);
3178 }
3179 }
3180}
3181
3182void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3183 DCHECK(instruction->IsDiv() || instruction->IsRem());
3184 Primitive::Type type = instruction->GetResultType();
3185 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3186
3187 LocationSummary* locations = instruction->GetLocations();
3188 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3189 Location second = locations->InAt(1);
3190
3191 if (second.IsConstant()) {
3192 int64_t imm = Int64FromConstant(second.GetConstant());
3193 if (imm == 0) {
3194 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3195 } else if (imm == 1 || imm == -1) {
3196 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003197 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003198 DivRemByPowerOfTwo(instruction);
3199 } else {
3200 DCHECK(imm <= -2 || imm >= 2);
3201 GenerateDivRemWithAnyConstant(instruction);
3202 }
3203 } else {
3204 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3205 GpuRegister divisor = second.AsRegister<GpuRegister>();
3206 if (instruction->IsDiv()) {
3207 if (type == Primitive::kPrimInt)
3208 __ DivR6(out, dividend, divisor);
3209 else
3210 __ Ddiv(out, dividend, divisor);
3211 } else {
3212 if (type == Primitive::kPrimInt)
3213 __ ModR6(out, dividend, divisor);
3214 else
3215 __ Dmod(out, dividend, divisor);
3216 }
3217 }
3218}
3219
Alexey Frunze4dda3372015-06-01 18:31:49 -07003220void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3221 LocationSummary* locations =
3222 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3223 switch (div->GetResultType()) {
3224 case Primitive::kPrimInt:
3225 case Primitive::kPrimLong:
3226 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003227 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003228 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3229 break;
3230
3231 case Primitive::kPrimFloat:
3232 case Primitive::kPrimDouble:
3233 locations->SetInAt(0, Location::RequiresFpuRegister());
3234 locations->SetInAt(1, Location::RequiresFpuRegister());
3235 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3236 break;
3237
3238 default:
3239 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3240 }
3241}
3242
3243void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3244 Primitive::Type type = instruction->GetType();
3245 LocationSummary* locations = instruction->GetLocations();
3246
3247 switch (type) {
3248 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003249 case Primitive::kPrimLong:
3250 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003251 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252 case Primitive::kPrimFloat:
3253 case Primitive::kPrimDouble: {
3254 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3255 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3256 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3257 if (type == Primitive::kPrimFloat)
3258 __ DivS(dst, lhs, rhs);
3259 else
3260 __ DivD(dst, lhs, rhs);
3261 break;
3262 }
3263 default:
3264 LOG(FATAL) << "Unexpected div type " << type;
3265 }
3266}
3267
3268void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003269 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003271}
3272
3273void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3274 SlowPathCodeMIPS64* slow_path =
3275 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3276 codegen_->AddSlowPath(slow_path);
3277 Location value = instruction->GetLocations()->InAt(0);
3278
3279 Primitive::Type type = instruction->GetType();
3280
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003281 if (!Primitive::IsIntegralType(type)) {
3282 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003283 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003284 }
3285
3286 if (value.IsConstant()) {
3287 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3288 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003289 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003290 } else {
3291 // A division by a non-null constant is valid. We don't need to perform
3292 // any check, so simply fall through.
3293 }
3294 } else {
3295 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3296 }
3297}
3298
3299void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3302 locations->SetOut(Location::ConstantLocation(constant));
3303}
3304
3305void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3306 // Will be generated at use site.
3307}
3308
3309void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3310 exit->SetLocations(nullptr);
3311}
3312
3313void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3314}
3315
3316void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3317 LocationSummary* locations =
3318 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3319 locations->SetOut(Location::ConstantLocation(constant));
3320}
3321
3322void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3323 // Will be generated at use site.
3324}
3325
David Brazdilfc6a86a2015-06-26 10:33:45 +00003326void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003327 DCHECK(!successor->IsExitBlock());
3328 HBasicBlock* block = got->GetBlock();
3329 HInstruction* previous = got->GetPrevious();
3330 HLoopInformation* info = block->GetLoopInformation();
3331
3332 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3333 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3334 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3335 return;
3336 }
3337 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3338 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3339 }
3340 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003341 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003342 }
3343}
3344
David Brazdilfc6a86a2015-06-26 10:33:45 +00003345void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3346 got->SetLocations(nullptr);
3347}
3348
3349void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3350 HandleGoto(got, got->GetSuccessor());
3351}
3352
3353void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3354 try_boundary->SetLocations(nullptr);
3355}
3356
3357void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3358 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3359 if (!successor->IsExitBlock()) {
3360 HandleGoto(try_boundary, successor);
3361 }
3362}
3363
Alexey Frunze299a9392015-12-08 16:08:02 -08003364void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3365 bool is64bit,
3366 LocationSummary* locations) {
3367 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3368 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3369 Location rhs_location = locations->InAt(1);
3370 GpuRegister rhs_reg = ZERO;
3371 int64_t rhs_imm = 0;
3372 bool use_imm = rhs_location.IsConstant();
3373 if (use_imm) {
3374 if (is64bit) {
3375 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3376 } else {
3377 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3378 }
3379 } else {
3380 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3381 }
3382 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3383
3384 switch (cond) {
3385 case kCondEQ:
3386 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003387 if (use_imm && IsInt<16>(-rhs_imm)) {
3388 if (rhs_imm == 0) {
3389 if (cond == kCondEQ) {
3390 __ Sltiu(dst, lhs, 1);
3391 } else {
3392 __ Sltu(dst, ZERO, lhs);
3393 }
3394 } else {
3395 if (is64bit) {
3396 __ Daddiu(dst, lhs, -rhs_imm);
3397 } else {
3398 __ Addiu(dst, lhs, -rhs_imm);
3399 }
3400 if (cond == kCondEQ) {
3401 __ Sltiu(dst, dst, 1);
3402 } else {
3403 __ Sltu(dst, ZERO, dst);
3404 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003405 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003406 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003407 if (use_imm && IsUint<16>(rhs_imm)) {
3408 __ Xori(dst, lhs, rhs_imm);
3409 } else {
3410 if (use_imm) {
3411 rhs_reg = TMP;
3412 __ LoadConst64(rhs_reg, rhs_imm);
3413 }
3414 __ Xor(dst, lhs, rhs_reg);
3415 }
3416 if (cond == kCondEQ) {
3417 __ Sltiu(dst, dst, 1);
3418 } else {
3419 __ Sltu(dst, ZERO, dst);
3420 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003421 }
3422 break;
3423
3424 case kCondLT:
3425 case kCondGE:
3426 if (use_imm && IsInt<16>(rhs_imm)) {
3427 __ Slti(dst, lhs, rhs_imm);
3428 } else {
3429 if (use_imm) {
3430 rhs_reg = TMP;
3431 __ LoadConst64(rhs_reg, rhs_imm);
3432 }
3433 __ Slt(dst, lhs, rhs_reg);
3434 }
3435 if (cond == kCondGE) {
3436 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3437 // only the slt instruction but no sge.
3438 __ Xori(dst, dst, 1);
3439 }
3440 break;
3441
3442 case kCondLE:
3443 case kCondGT:
3444 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3445 // Simulate lhs <= rhs via lhs < rhs + 1.
3446 __ Slti(dst, lhs, rhs_imm_plus_one);
3447 if (cond == kCondGT) {
3448 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3449 // only the slti instruction but no sgti.
3450 __ Xori(dst, dst, 1);
3451 }
3452 } else {
3453 if (use_imm) {
3454 rhs_reg = TMP;
3455 __ LoadConst64(rhs_reg, rhs_imm);
3456 }
3457 __ Slt(dst, rhs_reg, lhs);
3458 if (cond == kCondLE) {
3459 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3460 // only the slt instruction but no sle.
3461 __ Xori(dst, dst, 1);
3462 }
3463 }
3464 break;
3465
3466 case kCondB:
3467 case kCondAE:
3468 if (use_imm && IsInt<16>(rhs_imm)) {
3469 // Sltiu sign-extends its 16-bit immediate operand before
3470 // the comparison and thus lets us compare directly with
3471 // unsigned values in the ranges [0, 0x7fff] and
3472 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3473 __ Sltiu(dst, lhs, rhs_imm);
3474 } else {
3475 if (use_imm) {
3476 rhs_reg = TMP;
3477 __ LoadConst64(rhs_reg, rhs_imm);
3478 }
3479 __ Sltu(dst, lhs, rhs_reg);
3480 }
3481 if (cond == kCondAE) {
3482 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3483 // only the sltu instruction but no sgeu.
3484 __ Xori(dst, dst, 1);
3485 }
3486 break;
3487
3488 case kCondBE:
3489 case kCondA:
3490 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3491 // Simulate lhs <= rhs via lhs < rhs + 1.
3492 // Note that this only works if rhs + 1 does not overflow
3493 // to 0, hence the check above.
3494 // Sltiu sign-extends its 16-bit immediate operand before
3495 // the comparison and thus lets us compare directly with
3496 // unsigned values in the ranges [0, 0x7fff] and
3497 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3498 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3499 if (cond == kCondA) {
3500 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3501 // only the sltiu instruction but no sgtiu.
3502 __ Xori(dst, dst, 1);
3503 }
3504 } else {
3505 if (use_imm) {
3506 rhs_reg = TMP;
3507 __ LoadConst64(rhs_reg, rhs_imm);
3508 }
3509 __ Sltu(dst, rhs_reg, lhs);
3510 if (cond == kCondBE) {
3511 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3512 // only the sltu instruction but no sleu.
3513 __ Xori(dst, dst, 1);
3514 }
3515 }
3516 break;
3517 }
3518}
3519
3520void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3521 bool is64bit,
3522 LocationSummary* locations,
3523 Mips64Label* label) {
3524 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3525 Location rhs_location = locations->InAt(1);
3526 GpuRegister rhs_reg = ZERO;
3527 int64_t rhs_imm = 0;
3528 bool use_imm = rhs_location.IsConstant();
3529 if (use_imm) {
3530 if (is64bit) {
3531 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3532 } else {
3533 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3534 }
3535 } else {
3536 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3537 }
3538
3539 if (use_imm && rhs_imm == 0) {
3540 switch (cond) {
3541 case kCondEQ:
3542 case kCondBE: // <= 0 if zero
3543 __ Beqzc(lhs, label);
3544 break;
3545 case kCondNE:
3546 case kCondA: // > 0 if non-zero
3547 __ Bnezc(lhs, label);
3548 break;
3549 case kCondLT:
3550 __ Bltzc(lhs, label);
3551 break;
3552 case kCondGE:
3553 __ Bgezc(lhs, label);
3554 break;
3555 case kCondLE:
3556 __ Blezc(lhs, label);
3557 break;
3558 case kCondGT:
3559 __ Bgtzc(lhs, label);
3560 break;
3561 case kCondB: // always false
3562 break;
3563 case kCondAE: // always true
3564 __ Bc(label);
3565 break;
3566 }
3567 } else {
3568 if (use_imm) {
3569 rhs_reg = TMP;
3570 __ LoadConst64(rhs_reg, rhs_imm);
3571 }
3572 switch (cond) {
3573 case kCondEQ:
3574 __ Beqc(lhs, rhs_reg, label);
3575 break;
3576 case kCondNE:
3577 __ Bnec(lhs, rhs_reg, label);
3578 break;
3579 case kCondLT:
3580 __ Bltc(lhs, rhs_reg, label);
3581 break;
3582 case kCondGE:
3583 __ Bgec(lhs, rhs_reg, label);
3584 break;
3585 case kCondLE:
3586 __ Bgec(rhs_reg, lhs, label);
3587 break;
3588 case kCondGT:
3589 __ Bltc(rhs_reg, lhs, label);
3590 break;
3591 case kCondB:
3592 __ Bltuc(lhs, rhs_reg, label);
3593 break;
3594 case kCondAE:
3595 __ Bgeuc(lhs, rhs_reg, label);
3596 break;
3597 case kCondBE:
3598 __ Bgeuc(rhs_reg, lhs, label);
3599 break;
3600 case kCondA:
3601 __ Bltuc(rhs_reg, lhs, label);
3602 break;
3603 }
3604 }
3605}
3606
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003607void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3608 bool gt_bias,
3609 Primitive::Type type,
3610 LocationSummary* locations) {
3611 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3612 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3613 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3614 if (type == Primitive::kPrimFloat) {
3615 switch (cond) {
3616 case kCondEQ:
3617 __ CmpEqS(FTMP, lhs, rhs);
3618 __ Mfc1(dst, FTMP);
3619 __ Andi(dst, dst, 1);
3620 break;
3621 case kCondNE:
3622 __ CmpEqS(FTMP, lhs, rhs);
3623 __ Mfc1(dst, FTMP);
3624 __ Addiu(dst, dst, 1);
3625 break;
3626 case kCondLT:
3627 if (gt_bias) {
3628 __ CmpLtS(FTMP, lhs, rhs);
3629 } else {
3630 __ CmpUltS(FTMP, lhs, rhs);
3631 }
3632 __ Mfc1(dst, FTMP);
3633 __ Andi(dst, dst, 1);
3634 break;
3635 case kCondLE:
3636 if (gt_bias) {
3637 __ CmpLeS(FTMP, lhs, rhs);
3638 } else {
3639 __ CmpUleS(FTMP, lhs, rhs);
3640 }
3641 __ Mfc1(dst, FTMP);
3642 __ Andi(dst, dst, 1);
3643 break;
3644 case kCondGT:
3645 if (gt_bias) {
3646 __ CmpUltS(FTMP, rhs, lhs);
3647 } else {
3648 __ CmpLtS(FTMP, rhs, lhs);
3649 }
3650 __ Mfc1(dst, FTMP);
3651 __ Andi(dst, dst, 1);
3652 break;
3653 case kCondGE:
3654 if (gt_bias) {
3655 __ CmpUleS(FTMP, rhs, lhs);
3656 } else {
3657 __ CmpLeS(FTMP, rhs, lhs);
3658 }
3659 __ Mfc1(dst, FTMP);
3660 __ Andi(dst, dst, 1);
3661 break;
3662 default:
3663 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3664 UNREACHABLE();
3665 }
3666 } else {
3667 DCHECK_EQ(type, Primitive::kPrimDouble);
3668 switch (cond) {
3669 case kCondEQ:
3670 __ CmpEqD(FTMP, lhs, rhs);
3671 __ Mfc1(dst, FTMP);
3672 __ Andi(dst, dst, 1);
3673 break;
3674 case kCondNE:
3675 __ CmpEqD(FTMP, lhs, rhs);
3676 __ Mfc1(dst, FTMP);
3677 __ Addiu(dst, dst, 1);
3678 break;
3679 case kCondLT:
3680 if (gt_bias) {
3681 __ CmpLtD(FTMP, lhs, rhs);
3682 } else {
3683 __ CmpUltD(FTMP, lhs, rhs);
3684 }
3685 __ Mfc1(dst, FTMP);
3686 __ Andi(dst, dst, 1);
3687 break;
3688 case kCondLE:
3689 if (gt_bias) {
3690 __ CmpLeD(FTMP, lhs, rhs);
3691 } else {
3692 __ CmpUleD(FTMP, lhs, rhs);
3693 }
3694 __ Mfc1(dst, FTMP);
3695 __ Andi(dst, dst, 1);
3696 break;
3697 case kCondGT:
3698 if (gt_bias) {
3699 __ CmpUltD(FTMP, rhs, lhs);
3700 } else {
3701 __ CmpLtD(FTMP, rhs, lhs);
3702 }
3703 __ Mfc1(dst, FTMP);
3704 __ Andi(dst, dst, 1);
3705 break;
3706 case kCondGE:
3707 if (gt_bias) {
3708 __ CmpUleD(FTMP, rhs, lhs);
3709 } else {
3710 __ CmpLeD(FTMP, rhs, lhs);
3711 }
3712 __ Mfc1(dst, FTMP);
3713 __ Andi(dst, dst, 1);
3714 break;
3715 default:
3716 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3717 UNREACHABLE();
3718 }
3719 }
3720}
3721
Alexey Frunze299a9392015-12-08 16:08:02 -08003722void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3723 bool gt_bias,
3724 Primitive::Type type,
3725 LocationSummary* locations,
3726 Mips64Label* label) {
3727 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3728 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3729 if (type == Primitive::kPrimFloat) {
3730 switch (cond) {
3731 case kCondEQ:
3732 __ CmpEqS(FTMP, lhs, rhs);
3733 __ Bc1nez(FTMP, label);
3734 break;
3735 case kCondNE:
3736 __ CmpEqS(FTMP, lhs, rhs);
3737 __ Bc1eqz(FTMP, label);
3738 break;
3739 case kCondLT:
3740 if (gt_bias) {
3741 __ CmpLtS(FTMP, lhs, rhs);
3742 } else {
3743 __ CmpUltS(FTMP, lhs, rhs);
3744 }
3745 __ Bc1nez(FTMP, label);
3746 break;
3747 case kCondLE:
3748 if (gt_bias) {
3749 __ CmpLeS(FTMP, lhs, rhs);
3750 } else {
3751 __ CmpUleS(FTMP, lhs, rhs);
3752 }
3753 __ Bc1nez(FTMP, label);
3754 break;
3755 case kCondGT:
3756 if (gt_bias) {
3757 __ CmpUltS(FTMP, rhs, lhs);
3758 } else {
3759 __ CmpLtS(FTMP, rhs, lhs);
3760 }
3761 __ Bc1nez(FTMP, label);
3762 break;
3763 case kCondGE:
3764 if (gt_bias) {
3765 __ CmpUleS(FTMP, rhs, lhs);
3766 } else {
3767 __ CmpLeS(FTMP, rhs, lhs);
3768 }
3769 __ Bc1nez(FTMP, label);
3770 break;
3771 default:
3772 LOG(FATAL) << "Unexpected non-floating-point condition";
3773 }
3774 } else {
3775 DCHECK_EQ(type, Primitive::kPrimDouble);
3776 switch (cond) {
3777 case kCondEQ:
3778 __ CmpEqD(FTMP, lhs, rhs);
3779 __ Bc1nez(FTMP, label);
3780 break;
3781 case kCondNE:
3782 __ CmpEqD(FTMP, lhs, rhs);
3783 __ Bc1eqz(FTMP, label);
3784 break;
3785 case kCondLT:
3786 if (gt_bias) {
3787 __ CmpLtD(FTMP, lhs, rhs);
3788 } else {
3789 __ CmpUltD(FTMP, lhs, rhs);
3790 }
3791 __ Bc1nez(FTMP, label);
3792 break;
3793 case kCondLE:
3794 if (gt_bias) {
3795 __ CmpLeD(FTMP, lhs, rhs);
3796 } else {
3797 __ CmpUleD(FTMP, lhs, rhs);
3798 }
3799 __ Bc1nez(FTMP, label);
3800 break;
3801 case kCondGT:
3802 if (gt_bias) {
3803 __ CmpUltD(FTMP, rhs, lhs);
3804 } else {
3805 __ CmpLtD(FTMP, rhs, lhs);
3806 }
3807 __ Bc1nez(FTMP, label);
3808 break;
3809 case kCondGE:
3810 if (gt_bias) {
3811 __ CmpUleD(FTMP, rhs, lhs);
3812 } else {
3813 __ CmpLeD(FTMP, rhs, lhs);
3814 }
3815 __ Bc1nez(FTMP, label);
3816 break;
3817 default:
3818 LOG(FATAL) << "Unexpected non-floating-point condition";
3819 }
3820 }
3821}
3822
Alexey Frunze4dda3372015-06-01 18:31:49 -07003823void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003824 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003825 Mips64Label* true_target,
3826 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003827 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003828
David Brazdil0debae72015-11-12 18:37:00 +00003829 if (true_target == nullptr && false_target == nullptr) {
3830 // Nothing to do. The code always falls through.
3831 return;
3832 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003833 // Constant condition, statically compared against "true" (integer value 1).
3834 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003835 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003836 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003838 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003839 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003840 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003841 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003842 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003843 }
David Brazdil0debae72015-11-12 18:37:00 +00003844 return;
3845 }
3846
3847 // The following code generates these patterns:
3848 // (1) true_target == nullptr && false_target != nullptr
3849 // - opposite condition true => branch to false_target
3850 // (2) true_target != nullptr && false_target == nullptr
3851 // - condition true => branch to true_target
3852 // (3) true_target != nullptr && false_target != nullptr
3853 // - condition true => branch to true_target
3854 // - branch to false_target
3855 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003856 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003857 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003858 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003859 if (true_target == nullptr) {
3860 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3861 } else {
3862 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3863 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003864 } else {
3865 // The condition instruction has not been materialized, use its inputs as
3866 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003867 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003868 Primitive::Type type = condition->InputAt(0)->GetType();
3869 LocationSummary* locations = cond->GetLocations();
3870 IfCondition if_cond = condition->GetCondition();
3871 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003872
David Brazdil0debae72015-11-12 18:37:00 +00003873 if (true_target == nullptr) {
3874 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003875 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003876 }
3877
Alexey Frunze299a9392015-12-08 16:08:02 -08003878 switch (type) {
3879 default:
3880 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3881 break;
3882 case Primitive::kPrimLong:
3883 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3884 break;
3885 case Primitive::kPrimFloat:
3886 case Primitive::kPrimDouble:
3887 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3888 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003889 }
3890 }
David Brazdil0debae72015-11-12 18:37:00 +00003891
3892 // If neither branch falls through (case 3), the conditional branch to `true_target`
3893 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3894 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003895 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003896 }
3897}
3898
3899void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003901 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003902 locations->SetInAt(0, Location::RequiresRegister());
3903 }
3904}
3905
3906void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003907 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3908 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003909 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003910 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003911 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003912 nullptr : codegen_->GetLabelOf(false_successor);
3913 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003914}
3915
3916void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3917 LocationSummary* locations = new (GetGraph()->GetArena())
3918 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003919 InvokeRuntimeCallingConvention calling_convention;
3920 RegisterSet caller_saves = RegisterSet::Empty();
3921 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3922 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003923 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003924 locations->SetInAt(0, Location::RequiresRegister());
3925 }
3926}
3927
3928void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003929 SlowPathCodeMIPS64* slow_path =
3930 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003931 GenerateTestAndBranch(deoptimize,
3932 /* condition_input_index */ 0,
3933 slow_path->GetEntryLabel(),
3934 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003935}
3936
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003937void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3938 LocationSummary* locations = new (GetGraph()->GetArena())
3939 LocationSummary(flag, LocationSummary::kNoCall);
3940 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003941}
3942
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003943void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3944 __ LoadFromOffset(kLoadWord,
3945 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3946 SP,
3947 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003948}
3949
David Brazdil74eb1b22015-12-14 11:44:01 +00003950void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3952 if (Primitive::IsFloatingPointType(select->GetType())) {
3953 locations->SetInAt(0, Location::RequiresFpuRegister());
3954 locations->SetInAt(1, Location::RequiresFpuRegister());
3955 } else {
3956 locations->SetInAt(0, Location::RequiresRegister());
3957 locations->SetInAt(1, Location::RequiresRegister());
3958 }
3959 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3960 locations->SetInAt(2, Location::RequiresRegister());
3961 }
3962 locations->SetOut(Location::SameAsFirstInput());
3963}
3964
3965void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3966 LocationSummary* locations = select->GetLocations();
3967 Mips64Label false_target;
3968 GenerateTestAndBranch(select,
3969 /* condition_input_index */ 2,
3970 /* true_target */ nullptr,
3971 &false_target);
3972 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3973 __ Bind(&false_target);
3974}
3975
David Srbecky0cf44932015-12-09 14:09:59 +00003976void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3977 new (GetGraph()->GetArena()) LocationSummary(info);
3978}
3979
David Srbeckyd28f4a02016-03-14 17:14:24 +00003980void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3981 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003982}
3983
3984void CodeGeneratorMIPS64::GenerateNop() {
3985 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003986}
3987
Alexey Frunze4dda3372015-06-01 18:31:49 -07003988void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003989 const FieldInfo& field_info) {
3990 Primitive::Type field_type = field_info.GetFieldType();
3991 bool object_field_get_with_read_barrier =
3992 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3993 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3994 instruction,
3995 object_field_get_with_read_barrier
3996 ? LocationSummary::kCallOnSlowPath
3997 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07003998 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3999 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4000 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004001 locations->SetInAt(0, Location::RequiresRegister());
4002 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4003 locations->SetOut(Location::RequiresFpuRegister());
4004 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004005 // The output overlaps in the case of an object field get with
4006 // read barriers enabled: we do not want the move to overwrite the
4007 // object's location, as we need it to emit the read barrier.
4008 locations->SetOut(Location::RequiresRegister(),
4009 object_field_get_with_read_barrier
4010 ? Location::kOutputOverlap
4011 : Location::kNoOutputOverlap);
4012 }
4013 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4014 // We need a temporary register for the read barrier marking slow
4015 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
4016 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004017 }
4018}
4019
4020void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4021 const FieldInfo& field_info) {
4022 Primitive::Type type = field_info.GetFieldType();
4023 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004024 Location obj_loc = locations->InAt(0);
4025 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4026 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004027 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004028 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004029 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004030 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4031
Alexey Frunze4dda3372015-06-01 18:31:49 -07004032 switch (type) {
4033 case Primitive::kPrimBoolean:
4034 load_type = kLoadUnsignedByte;
4035 break;
4036 case Primitive::kPrimByte:
4037 load_type = kLoadSignedByte;
4038 break;
4039 case Primitive::kPrimShort:
4040 load_type = kLoadSignedHalfword;
4041 break;
4042 case Primitive::kPrimChar:
4043 load_type = kLoadUnsignedHalfword;
4044 break;
4045 case Primitive::kPrimInt:
4046 case Primitive::kPrimFloat:
4047 load_type = kLoadWord;
4048 break;
4049 case Primitive::kPrimLong:
4050 case Primitive::kPrimDouble:
4051 load_type = kLoadDoubleword;
4052 break;
4053 case Primitive::kPrimNot:
4054 load_type = kLoadUnsignedWord;
4055 break;
4056 case Primitive::kPrimVoid:
4057 LOG(FATAL) << "Unreachable type " << type;
4058 UNREACHABLE();
4059 }
4060 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004061 DCHECK(dst_loc.IsRegister());
4062 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4063 if (type == Primitive::kPrimNot) {
4064 // /* HeapReference<Object> */ dst = *(obj + offset)
4065 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4066 Location temp_loc = locations->GetTemp(0);
4067 // Note that a potential implicit null check is handled in this
4068 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4069 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4070 dst_loc,
4071 obj,
4072 offset,
4073 temp_loc,
4074 /* needs_null_check */ true);
4075 if (is_volatile) {
4076 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4077 }
4078 } else {
4079 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4080 if (is_volatile) {
4081 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4082 }
4083 // If read barriers are enabled, emit read barriers other than
4084 // Baker's using a slow path (and also unpoison the loaded
4085 // reference, if heap poisoning is enabled).
4086 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4087 }
4088 } else {
4089 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4090 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004092 DCHECK(dst_loc.IsFpuRegister());
4093 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004094 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004095 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004096
Alexey Frunze15958152017-02-09 19:08:30 -08004097 // Memory barriers, in the case of references, are handled in the
4098 // previous switch statement.
4099 if (is_volatile && (type != Primitive::kPrimNot)) {
4100 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004102}
4103
4104void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4105 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4106 LocationSummary* locations =
4107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4108 locations->SetInAt(0, Location::RequiresRegister());
4109 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004110 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004112 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004113 }
4114}
4115
4116void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004117 const FieldInfo& field_info,
4118 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004119 Primitive::Type type = field_info.GetFieldType();
4120 LocationSummary* locations = instruction->GetLocations();
4121 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004122 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004124 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004125 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4126 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004127 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4128
Alexey Frunze4dda3372015-06-01 18:31:49 -07004129 switch (type) {
4130 case Primitive::kPrimBoolean:
4131 case Primitive::kPrimByte:
4132 store_type = kStoreByte;
4133 break;
4134 case Primitive::kPrimShort:
4135 case Primitive::kPrimChar:
4136 store_type = kStoreHalfword;
4137 break;
4138 case Primitive::kPrimInt:
4139 case Primitive::kPrimFloat:
4140 case Primitive::kPrimNot:
4141 store_type = kStoreWord;
4142 break;
4143 case Primitive::kPrimLong:
4144 case Primitive::kPrimDouble:
4145 store_type = kStoreDoubleword;
4146 break;
4147 case Primitive::kPrimVoid:
4148 LOG(FATAL) << "Unreachable type " << type;
4149 UNREACHABLE();
4150 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004151
Alexey Frunze15958152017-02-09 19:08:30 -08004152 if (is_volatile) {
4153 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4154 }
4155
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004156 if (value_location.IsConstant()) {
4157 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4158 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4159 } else {
4160 if (!Primitive::IsFloatingPointType(type)) {
4161 DCHECK(value_location.IsRegister());
4162 GpuRegister src = value_location.AsRegister<GpuRegister>();
4163 if (kPoisonHeapReferences && needs_write_barrier) {
4164 // Note that in the case where `value` is a null reference,
4165 // we do not enter this block, as a null reference does not
4166 // need poisoning.
4167 DCHECK_EQ(type, Primitive::kPrimNot);
4168 __ PoisonHeapReference(TMP, src);
4169 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4170 } else {
4171 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4172 }
4173 } else {
4174 DCHECK(value_location.IsFpuRegister());
4175 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4176 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4177 }
4178 }
Alexey Frunze15958152017-02-09 19:08:30 -08004179
Alexey Frunzec061de12017-02-14 13:27:23 -08004180 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004181 DCHECK(value_location.IsRegister());
4182 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004183 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004184 }
Alexey Frunze15958152017-02-09 19:08:30 -08004185
4186 if (is_volatile) {
4187 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4188 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004189}
4190
4191void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4192 HandleFieldGet(instruction, instruction->GetFieldInfo());
4193}
4194
4195void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4196 HandleFieldGet(instruction, instruction->GetFieldInfo());
4197}
4198
4199void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4200 HandleFieldSet(instruction, instruction->GetFieldInfo());
4201}
4202
4203void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004204 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004205}
4206
Alexey Frunze15958152017-02-09 19:08:30 -08004207void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4208 HInstruction* instruction,
4209 Location out,
4210 uint32_t offset,
4211 Location maybe_temp,
4212 ReadBarrierOption read_barrier_option) {
4213 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4214 if (read_barrier_option == kWithReadBarrier) {
4215 CHECK(kEmitCompilerReadBarrier);
4216 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4217 if (kUseBakerReadBarrier) {
4218 // Load with fast path based Baker's read barrier.
4219 // /* HeapReference<Object> */ out = *(out + offset)
4220 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4221 out,
4222 out_reg,
4223 offset,
4224 maybe_temp,
4225 /* needs_null_check */ false);
4226 } else {
4227 // Load with slow path based read barrier.
4228 // Save the value of `out` into `maybe_temp` before overwriting it
4229 // in the following move operation, as we will need it for the
4230 // read barrier below.
4231 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4232 // /* HeapReference<Object> */ out = *(out + offset)
4233 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4234 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4235 }
4236 } else {
4237 // Plain load with no read barrier.
4238 // /* HeapReference<Object> */ out = *(out + offset)
4239 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4240 __ MaybeUnpoisonHeapReference(out_reg);
4241 }
4242}
4243
4244void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4245 HInstruction* instruction,
4246 Location out,
4247 Location obj,
4248 uint32_t offset,
4249 Location maybe_temp,
4250 ReadBarrierOption read_barrier_option) {
4251 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4252 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4253 if (read_barrier_option == kWithReadBarrier) {
4254 CHECK(kEmitCompilerReadBarrier);
4255 if (kUseBakerReadBarrier) {
4256 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4257 // Load with fast path based Baker's read barrier.
4258 // /* HeapReference<Object> */ out = *(obj + offset)
4259 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4260 out,
4261 obj_reg,
4262 offset,
4263 maybe_temp,
4264 /* needs_null_check */ false);
4265 } else {
4266 // Load with slow path based read barrier.
4267 // /* HeapReference<Object> */ out = *(obj + offset)
4268 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4269 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4270 }
4271 } else {
4272 // Plain load with no read barrier.
4273 // /* HeapReference<Object> */ out = *(obj + offset)
4274 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4275 __ MaybeUnpoisonHeapReference(out_reg);
4276 }
4277}
4278
Alexey Frunzef63f5692016-12-13 17:43:11 -08004279void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004280 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004281 Location root,
4282 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004283 uint32_t offset,
4284 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004285 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004286 if (read_barrier_option == kWithReadBarrier) {
4287 DCHECK(kEmitCompilerReadBarrier);
4288 if (kUseBakerReadBarrier) {
4289 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4290 // Baker's read barrier are used:
4291 //
4292 // root = obj.field;
4293 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4294 // if (temp != null) {
4295 // root = temp(root)
4296 // }
4297
4298 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4299 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4300 static_assert(
4301 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4302 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4303 "have different sizes.");
4304 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4305 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4306 "have different sizes.");
4307
4308 // Slow path marking the GC root `root`.
4309 Location temp = Location::RegisterLocation(T9);
4310 SlowPathCodeMIPS64* slow_path =
4311 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4312 instruction,
4313 root,
4314 /*entrypoint*/ temp);
4315 codegen_->AddSlowPath(slow_path);
4316
4317 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4318 const int32_t entry_point_offset =
4319 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4320 // Loading the entrypoint does not require a load acquire since it is only changed when
4321 // threads are suspended or running a checkpoint.
4322 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4323 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4324 // checking GetIsGcMarking.
4325 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4326 __ Bind(slow_path->GetExitLabel());
4327 } else {
4328 // GC root loaded through a slow path for read barriers other
4329 // than Baker's.
4330 // /* GcRoot<mirror::Object>* */ root = obj + offset
4331 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4332 // /* mirror::Object* */ root = root->Read()
4333 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4334 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004335 } else {
4336 // Plain GC root load with no read barrier.
4337 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4338 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4339 // Note that GC roots are not affected by heap poisoning, thus we
4340 // do not have to unpoison `root_reg` here.
4341 }
4342}
4343
Alexey Frunze15958152017-02-09 19:08:30 -08004344void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4345 Location ref,
4346 GpuRegister obj,
4347 uint32_t offset,
4348 Location temp,
4349 bool needs_null_check) {
4350 DCHECK(kEmitCompilerReadBarrier);
4351 DCHECK(kUseBakerReadBarrier);
4352
4353 // /* HeapReference<Object> */ ref = *(obj + offset)
4354 Location no_index = Location::NoLocation();
4355 ScaleFactor no_scale_factor = TIMES_1;
4356 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4357 ref,
4358 obj,
4359 offset,
4360 no_index,
4361 no_scale_factor,
4362 temp,
4363 needs_null_check);
4364}
4365
4366void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4367 Location ref,
4368 GpuRegister obj,
4369 uint32_t data_offset,
4370 Location index,
4371 Location temp,
4372 bool needs_null_check) {
4373 DCHECK(kEmitCompilerReadBarrier);
4374 DCHECK(kUseBakerReadBarrier);
4375
4376 static_assert(
4377 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4378 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4379 // /* HeapReference<Object> */ ref =
4380 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4381 ScaleFactor scale_factor = TIMES_4;
4382 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4383 ref,
4384 obj,
4385 data_offset,
4386 index,
4387 scale_factor,
4388 temp,
4389 needs_null_check);
4390}
4391
4392void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4393 Location ref,
4394 GpuRegister obj,
4395 uint32_t offset,
4396 Location index,
4397 ScaleFactor scale_factor,
4398 Location temp,
4399 bool needs_null_check,
4400 bool always_update_field) {
4401 DCHECK(kEmitCompilerReadBarrier);
4402 DCHECK(kUseBakerReadBarrier);
4403
4404 // In slow path based read barriers, the read barrier call is
4405 // inserted after the original load. However, in fast path based
4406 // Baker's read barriers, we need to perform the load of
4407 // mirror::Object::monitor_ *before* the original reference load.
4408 // This load-load ordering is required by the read barrier.
4409 // The fast path/slow path (for Baker's algorithm) should look like:
4410 //
4411 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4412 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4413 // HeapReference<Object> ref = *src; // Original reference load.
4414 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4415 // if (is_gray) {
4416 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4417 // }
4418 //
4419 // Note: the original implementation in ReadBarrier::Barrier is
4420 // slightly more complex as it performs additional checks that we do
4421 // not do here for performance reasons.
4422
4423 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4424 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4425 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4426
4427 // /* int32_t */ monitor = obj->monitor_
4428 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4429 if (needs_null_check) {
4430 MaybeRecordImplicitNullCheck(instruction);
4431 }
4432 // /* LockWord */ lock_word = LockWord(monitor)
4433 static_assert(sizeof(LockWord) == sizeof(int32_t),
4434 "art::LockWord and int32_t have different sizes.");
4435
4436 __ Sync(0); // Barrier to prevent load-load reordering.
4437
4438 // The actual reference load.
4439 if (index.IsValid()) {
4440 // Load types involving an "index": ArrayGet,
4441 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4442 // intrinsics.
4443 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4444 if (index.IsConstant()) {
4445 size_t computed_offset =
4446 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4447 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4448 } else {
4449 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004450 if (scale_factor == TIMES_1) {
4451 __ Daddu(TMP, index_reg, obj);
4452 } else {
4453 __ Dlsa(TMP, index_reg, obj, scale_factor);
4454 }
Alexey Frunze15958152017-02-09 19:08:30 -08004455 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4456 }
4457 } else {
4458 // /* HeapReference<Object> */ ref = *(obj + offset)
4459 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4460 }
4461
4462 // Object* ref = ref_addr->AsMirrorPtr()
4463 __ MaybeUnpoisonHeapReference(ref_reg);
4464
4465 // Slow path marking the object `ref` when it is gray.
4466 SlowPathCodeMIPS64* slow_path;
4467 if (always_update_field) {
4468 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4469 // of the form `obj + field_offset`, where `obj` is a register and
4470 // `field_offset` is a register. Thus `offset` and `scale_factor`
4471 // above are expected to be null in this code path.
4472 DCHECK_EQ(offset, 0u);
4473 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4474 slow_path = new (GetGraph()->GetArena())
4475 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4476 ref,
4477 obj,
4478 /* field_offset */ index,
4479 temp_reg);
4480 } else {
4481 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4482 }
4483 AddSlowPath(slow_path);
4484
4485 // if (rb_state == ReadBarrier::GrayState())
4486 // ref = ReadBarrier::Mark(ref);
4487 // Given the numeric representation, it's enough to check the low bit of the
4488 // rb_state. We do that by shifting the bit into the sign bit (31) and
4489 // performing a branch on less than zero.
4490 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4491 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4492 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4493 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4494 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4495 __ Bind(slow_path->GetExitLabel());
4496}
4497
4498void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4499 Location out,
4500 Location ref,
4501 Location obj,
4502 uint32_t offset,
4503 Location index) {
4504 DCHECK(kEmitCompilerReadBarrier);
4505
4506 // Insert a slow path based read barrier *after* the reference load.
4507 //
4508 // If heap poisoning is enabled, the unpoisoning of the loaded
4509 // reference will be carried out by the runtime within the slow
4510 // path.
4511 //
4512 // Note that `ref` currently does not get unpoisoned (when heap
4513 // poisoning is enabled), which is alright as the `ref` argument is
4514 // not used by the artReadBarrierSlow entry point.
4515 //
4516 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4517 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4518 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4519 AddSlowPath(slow_path);
4520
4521 __ Bc(slow_path->GetEntryLabel());
4522 __ Bind(slow_path->GetExitLabel());
4523}
4524
4525void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4526 Location out,
4527 Location ref,
4528 Location obj,
4529 uint32_t offset,
4530 Location index) {
4531 if (kEmitCompilerReadBarrier) {
4532 // Baker's read barriers shall be handled by the fast path
4533 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4534 DCHECK(!kUseBakerReadBarrier);
4535 // If heap poisoning is enabled, unpoisoning will be taken care of
4536 // by the runtime within the slow path.
4537 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4538 } else if (kPoisonHeapReferences) {
4539 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4540 }
4541}
4542
4543void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4544 Location out,
4545 Location root) {
4546 DCHECK(kEmitCompilerReadBarrier);
4547
4548 // Insert a slow path based read barrier *after* the GC root load.
4549 //
4550 // Note that GC roots are not affected by heap poisoning, so we do
4551 // not need to do anything special for this here.
4552 SlowPathCodeMIPS64* slow_path =
4553 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4554 AddSlowPath(slow_path);
4555
4556 __ Bc(slow_path->GetEntryLabel());
4557 __ Bind(slow_path->GetExitLabel());
4558}
4559
Alexey Frunze4dda3372015-06-01 18:31:49 -07004560void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004561 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4562 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07004563 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004564 switch (type_check_kind) {
4565 case TypeCheckKind::kExactCheck:
4566 case TypeCheckKind::kAbstractClassCheck:
4567 case TypeCheckKind::kClassHierarchyCheck:
4568 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004569 call_kind =
4570 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07004571 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004572 break;
4573 case TypeCheckKind::kArrayCheck:
4574 case TypeCheckKind::kUnresolvedCheck:
4575 case TypeCheckKind::kInterfaceCheck:
4576 call_kind = LocationSummary::kCallOnSlowPath;
4577 break;
4578 }
4579
Alexey Frunze4dda3372015-06-01 18:31:49 -07004580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07004581 if (baker_read_barrier_slow_path) {
4582 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
4583 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004584 locations->SetInAt(0, Location::RequiresRegister());
4585 locations->SetInAt(1, Location::RequiresRegister());
4586 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004587 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004588 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004589 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004590}
4591
4592void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004593 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004594 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004595 Location obj_loc = locations->InAt(0);
4596 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004597 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004598 Location out_loc = locations->Out();
4599 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4600 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4601 DCHECK_LE(num_temps, 1u);
4602 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004603 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4604 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4605 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4606 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004607 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004608 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004609
4610 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004611 // Avoid this check if we know `obj` is not null.
4612 if (instruction->MustDoNullCheck()) {
4613 __ Move(out, ZERO);
4614 __ Beqzc(obj, &done);
4615 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004616
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004617 switch (type_check_kind) {
4618 case TypeCheckKind::kExactCheck: {
4619 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004620 GenerateReferenceLoadTwoRegisters(instruction,
4621 out_loc,
4622 obj_loc,
4623 class_offset,
4624 maybe_temp_loc,
4625 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004626 // Classes must be equal for the instanceof to succeed.
4627 __ Xor(out, out, cls);
4628 __ Sltiu(out, out, 1);
4629 break;
4630 }
4631
4632 case TypeCheckKind::kAbstractClassCheck: {
4633 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004634 GenerateReferenceLoadTwoRegisters(instruction,
4635 out_loc,
4636 obj_loc,
4637 class_offset,
4638 maybe_temp_loc,
4639 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004640 // If the class is abstract, we eagerly fetch the super class of the
4641 // object to avoid doing a comparison we know will fail.
4642 Mips64Label loop;
4643 __ Bind(&loop);
4644 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004645 GenerateReferenceLoadOneRegister(instruction,
4646 out_loc,
4647 super_offset,
4648 maybe_temp_loc,
4649 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004650 // If `out` is null, we use it for the result, and jump to `done`.
4651 __ Beqzc(out, &done);
4652 __ Bnec(out, cls, &loop);
4653 __ LoadConst32(out, 1);
4654 break;
4655 }
4656
4657 case TypeCheckKind::kClassHierarchyCheck: {
4658 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004659 GenerateReferenceLoadTwoRegisters(instruction,
4660 out_loc,
4661 obj_loc,
4662 class_offset,
4663 maybe_temp_loc,
4664 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004665 // Walk over the class hierarchy to find a match.
4666 Mips64Label loop, success;
4667 __ Bind(&loop);
4668 __ Beqc(out, cls, &success);
4669 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004670 GenerateReferenceLoadOneRegister(instruction,
4671 out_loc,
4672 super_offset,
4673 maybe_temp_loc,
4674 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004675 __ Bnezc(out, &loop);
4676 // If `out` is null, we use it for the result, and jump to `done`.
4677 __ Bc(&done);
4678 __ Bind(&success);
4679 __ LoadConst32(out, 1);
4680 break;
4681 }
4682
4683 case TypeCheckKind::kArrayObjectCheck: {
4684 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004685 GenerateReferenceLoadTwoRegisters(instruction,
4686 out_loc,
4687 obj_loc,
4688 class_offset,
4689 maybe_temp_loc,
4690 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004691 // Do an exact check.
4692 Mips64Label success;
4693 __ Beqc(out, cls, &success);
4694 // Otherwise, we need to check that the object's class is a non-primitive array.
4695 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004696 GenerateReferenceLoadOneRegister(instruction,
4697 out_loc,
4698 component_offset,
4699 maybe_temp_loc,
4700 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004701 // If `out` is null, we use it for the result, and jump to `done`.
4702 __ Beqzc(out, &done);
4703 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4704 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4705 __ Sltiu(out, out, 1);
4706 __ Bc(&done);
4707 __ Bind(&success);
4708 __ LoadConst32(out, 1);
4709 break;
4710 }
4711
4712 case TypeCheckKind::kArrayCheck: {
4713 // No read barrier since the slow path will retry upon failure.
4714 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004715 GenerateReferenceLoadTwoRegisters(instruction,
4716 out_loc,
4717 obj_loc,
4718 class_offset,
4719 maybe_temp_loc,
4720 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004721 DCHECK(locations->OnlyCallsOnSlowPath());
4722 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4723 /* is_fatal */ false);
4724 codegen_->AddSlowPath(slow_path);
4725 __ Bnec(out, cls, slow_path->GetEntryLabel());
4726 __ LoadConst32(out, 1);
4727 break;
4728 }
4729
4730 case TypeCheckKind::kUnresolvedCheck:
4731 case TypeCheckKind::kInterfaceCheck: {
4732 // Note that we indeed only call on slow path, but we always go
4733 // into the slow path for the unresolved and interface check
4734 // cases.
4735 //
4736 // We cannot directly call the InstanceofNonTrivial runtime
4737 // entry point without resorting to a type checking slow path
4738 // here (i.e. by calling InvokeRuntime directly), as it would
4739 // require to assign fixed registers for the inputs of this
4740 // HInstanceOf instruction (following the runtime calling
4741 // convention), which might be cluttered by the potential first
4742 // read barrier emission at the beginning of this method.
4743 //
4744 // TODO: Introduce a new runtime entry point taking the object
4745 // to test (instead of its class) as argument, and let it deal
4746 // with the read barrier issues. This will let us refactor this
4747 // case of the `switch` code as it was previously (with a direct
4748 // call to the runtime not using a type checking slow path).
4749 // This should also be beneficial for the other cases above.
4750 DCHECK(locations->OnlyCallsOnSlowPath());
4751 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4752 /* is_fatal */ false);
4753 codegen_->AddSlowPath(slow_path);
4754 __ Bc(slow_path->GetEntryLabel());
4755 break;
4756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004757 }
4758
4759 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004760
4761 if (slow_path != nullptr) {
4762 __ Bind(slow_path->GetExitLabel());
4763 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004764}
4765
4766void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4768 locations->SetOut(Location::ConstantLocation(constant));
4769}
4770
4771void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4772 // Will be generated at use site.
4773}
4774
4775void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4777 locations->SetOut(Location::ConstantLocation(constant));
4778}
4779
4780void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4781 // Will be generated at use site.
4782}
4783
Calin Juravle175dc732015-08-25 15:42:32 +01004784void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4785 // The trampoline uses the same calling convention as dex calling conventions,
4786 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4787 // the method_idx.
4788 HandleInvoke(invoke);
4789}
4790
4791void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4792 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4793}
4794
Alexey Frunze4dda3372015-06-01 18:31:49 -07004795void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4796 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4797 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4798}
4799
4800void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4801 HandleInvoke(invoke);
4802 // The register T0 is required to be used for the hidden argument in
4803 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4804 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4805}
4806
4807void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4808 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4809 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004810 Location receiver = invoke->GetLocations()->InAt(0);
4811 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004812 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004813
4814 // Set the hidden argument.
4815 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4816 invoke->GetDexMethodIndex());
4817
4818 // temp = object->GetClass();
4819 if (receiver.IsStackSlot()) {
4820 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4821 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4822 } else {
4823 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4824 }
4825 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004826 // Instead of simply (possibly) unpoisoning `temp` here, we should
4827 // emit a read barrier for the previous class reference load.
4828 // However this is not required in practice, as this is an
4829 // intermediate/temporary reference and because the current
4830 // concurrent copying collector keeps the from-space memory
4831 // intact/accessible until the end of the marking phase (the
4832 // concurrent copying collector may not in the future).
4833 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004834 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4835 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4836 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004837 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004838 // temp = temp->GetImtEntryAt(method_offset);
4839 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4840 // T9 = temp->GetEntryPoint();
4841 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4842 // T9();
4843 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004844 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004845 DCHECK(!codegen_->IsLeafMethod());
4846 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4847}
4848
4849void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004850 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4851 if (intrinsic.TryDispatch(invoke)) {
4852 return;
4853 }
4854
Alexey Frunze4dda3372015-06-01 18:31:49 -07004855 HandleInvoke(invoke);
4856}
4857
4858void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004859 // Explicit clinit checks triggered by static invokes must have been pruned by
4860 // art::PrepareForRegisterAllocation.
4861 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862
Chris Larsen3039e382015-08-26 07:54:08 -07004863 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4864 if (intrinsic.TryDispatch(invoke)) {
4865 return;
4866 }
4867
Alexey Frunze4dda3372015-06-01 18:31:49 -07004868 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004869}
4870
Orion Hodsonac141392017-01-13 11:53:47 +00004871void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4872 HandleInvoke(invoke);
4873}
4874
4875void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4876 codegen_->GenerateInvokePolymorphicCall(invoke);
4877}
4878
Chris Larsen3039e382015-08-26 07:54:08 -07004879static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004880 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004881 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4882 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004883 return true;
4884 }
4885 return false;
4886}
4887
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004888HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004889 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004890 bool fallback_load = false;
4891 switch (desired_string_load_kind) {
4892 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4893 DCHECK(!GetCompilerOptions().GetCompilePic());
4894 break;
4895 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4896 DCHECK(GetCompilerOptions().GetCompilePic());
4897 break;
4898 case HLoadString::LoadKind::kBootImageAddress:
4899 break;
4900 case HLoadString::LoadKind::kBssEntry:
4901 DCHECK(!Runtime::Current()->UseJitCompilation());
4902 break;
4903 case HLoadString::LoadKind::kDexCacheViaMethod:
4904 break;
4905 case HLoadString::LoadKind::kJitTableAddress:
4906 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004907 break;
4908 }
4909 if (fallback_load) {
4910 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4911 }
4912 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004913}
4914
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004915HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4916 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004917 bool fallback_load = false;
4918 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004919 case HLoadClass::LoadKind::kInvalid:
4920 LOG(FATAL) << "UNREACHABLE";
4921 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004922 case HLoadClass::LoadKind::kReferrersClass:
4923 break;
4924 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4925 DCHECK(!GetCompilerOptions().GetCompilePic());
4926 break;
4927 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4928 DCHECK(GetCompilerOptions().GetCompilePic());
4929 break;
4930 case HLoadClass::LoadKind::kBootImageAddress:
4931 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004932 case HLoadClass::LoadKind::kBssEntry:
4933 DCHECK(!Runtime::Current()->UseJitCompilation());
4934 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004935 case HLoadClass::LoadKind::kJitTableAddress:
4936 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004937 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004938 case HLoadClass::LoadKind::kDexCacheViaMethod:
4939 break;
4940 }
4941 if (fallback_load) {
4942 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4943 }
4944 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945}
4946
Vladimir Markodc151b22015-10-15 18:02:30 +01004947HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4948 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004949 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004950 // On MIPS64 we support all dispatch types.
4951 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004952}
4953
Alexey Frunze4dda3372015-06-01 18:31:49 -07004954void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4955 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004956 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004957 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4958 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4959
Alexey Frunze19f6c692016-11-30 19:19:55 -08004960 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004961 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004962 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004963 uint32_t offset =
4964 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004965 __ LoadFromOffset(kLoadDoubleword,
4966 temp.AsRegister<GpuRegister>(),
4967 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004968 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004969 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004970 }
Vladimir Marko58155012015-08-19 12:49:41 +00004971 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004972 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004973 break;
4974 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004975 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4976 kLoadDoubleword,
4977 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004978 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004979 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4980 uint32_t offset = invoke->GetDexCacheArrayOffset();
4981 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004982 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004983 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4984 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4985 break;
4986 }
Vladimir Marko58155012015-08-19 12:49:41 +00004987 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004988 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004989 GpuRegister reg = temp.AsRegister<GpuRegister>();
4990 GpuRegister method_reg;
4991 if (current_method.IsRegister()) {
4992 method_reg = current_method.AsRegister<GpuRegister>();
4993 } else {
4994 // TODO: use the appropriate DCHECK() here if possible.
4995 // DCHECK(invoke->GetLocations()->Intrinsified());
4996 DCHECK(!current_method.IsValid());
4997 method_reg = reg;
4998 __ Ld(reg, SP, kCurrentMethodStackOffset);
4999 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005000
Vladimir Marko58155012015-08-19 12:49:41 +00005001 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01005002 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00005003 reg,
5004 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01005005 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005006 // temp = temp[index_in_cache];
5007 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5008 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00005009 __ LoadFromOffset(kLoadDoubleword,
5010 reg,
5011 reg,
5012 CodeGenerator::GetCachePointerOffset(index_in_cache));
5013 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005014 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005015 }
5016
Alexey Frunze19f6c692016-11-30 19:19:55 -08005017 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005018 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005019 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005020 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005021 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5022 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5023 __ LoadFromOffset(kLoadDoubleword,
5024 T9,
5025 callee_method.AsRegister<GpuRegister>(),
5026 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005027 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005028 // T9()
5029 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005030 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005031 break;
5032 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005033 DCHECK(!IsLeafMethod());
5034}
5035
5036void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005037 // Explicit clinit checks triggered by static invokes must have been pruned by
5038 // art::PrepareForRegisterAllocation.
5039 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005040
5041 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5042 return;
5043 }
5044
5045 LocationSummary* locations = invoke->GetLocations();
5046 codegen_->GenerateStaticOrDirectCall(invoke,
5047 locations->HasTemps()
5048 ? locations->GetTemp(0)
5049 : Location::NoLocation());
5050 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5051}
5052
Alexey Frunze53afca12015-11-05 16:34:23 -08005053void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005054 // Use the calling convention instead of the location of the receiver, as
5055 // intrinsics may have put the receiver in a different register. In the intrinsics
5056 // slow path, the arguments have been moved to the right place, so here we are
5057 // guaranteed that the receiver is the first register of the calling convention.
5058 InvokeDexCallingConvention calling_convention;
5059 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5060
Alexey Frunze53afca12015-11-05 16:34:23 -08005061 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005062 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5063 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5064 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005065 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005066
5067 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005068 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005069 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005070 // Instead of simply (possibly) unpoisoning `temp` here, we should
5071 // emit a read barrier for the previous class reference load.
5072 // However this is not required in practice, as this is an
5073 // intermediate/temporary reference and because the current
5074 // concurrent copying collector keeps the from-space memory
5075 // intact/accessible until the end of the marking phase (the
5076 // concurrent copying collector may not in the future).
5077 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005078 // temp = temp->GetMethodAt(method_offset);
5079 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5080 // T9 = temp->GetEntryPoint();
5081 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5082 // T9();
5083 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005084 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08005085}
5086
5087void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5088 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5089 return;
5090 }
5091
5092 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005093 DCHECK(!codegen_->IsLeafMethod());
5094 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5095}
5096
5097void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005098 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5099 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005100 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005101 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
5102 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005103 return;
5104 }
Vladimir Marko41559982017-01-06 14:04:23 +00005105 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005106
Alexey Frunze15958152017-02-09 19:08:30 -08005107 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5108 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005109 ? LocationSummary::kCallOnSlowPath
5110 : LocationSummary::kNoCall;
5111 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07005112 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
5113 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5114 }
Vladimir Marko41559982017-01-06 14:04:23 +00005115 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005116 locations->SetInAt(0, Location::RequiresRegister());
5117 }
5118 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005119 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5120 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5121 // Rely on the type resolution or initialization and marking to save everything we need.
5122 RegisterSet caller_saves = RegisterSet::Empty();
5123 InvokeRuntimeCallingConvention calling_convention;
5124 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5125 locations->SetCustomSlowPathCallerSaves(caller_saves);
5126 } else {
5127 // For non-Baker read barrier we have a temp-clobbering call.
5128 }
5129 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005130}
5131
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005132// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5133// move.
5134void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005135 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5136 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5137 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005138 return;
5139 }
Vladimir Marko41559982017-01-06 14:04:23 +00005140 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005141
Vladimir Marko41559982017-01-06 14:04:23 +00005142 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005143 Location out_loc = locations->Out();
5144 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5145 GpuRegister current_method_reg = ZERO;
5146 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5147 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5148 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5149 }
5150
Alexey Frunze15958152017-02-09 19:08:30 -08005151 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5152 ? kWithoutReadBarrier
5153 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005154 bool generate_null_check = false;
5155 switch (load_kind) {
5156 case HLoadClass::LoadKind::kReferrersClass:
5157 DCHECK(!cls->CanCallRuntime());
5158 DCHECK(!cls->MustGenerateClinitCheck());
5159 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5160 GenerateGcRootFieldLoad(cls,
5161 out_loc,
5162 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005163 ArtMethod::DeclaringClassOffset().Int32Value(),
5164 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005165 break;
5166 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005167 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005168 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005169 __ LoadLiteral(out,
5170 kLoadUnsignedWord,
5171 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5172 cls->GetTypeIndex()));
5173 break;
5174 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005175 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005176 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005177 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5178 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5179 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5180 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5181 break;
5182 }
5183 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005184 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005185 uint32_t address = dchecked_integral_cast<uint32_t>(
5186 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5187 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005188 __ LoadLiteral(out,
5189 kLoadUnsignedWord,
5190 codegen_->DeduplicateBootImageAddressLiteral(address));
5191 break;
5192 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005193 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005194 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005195 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005196 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005197 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005198 generate_null_check = true;
5199 break;
5200 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005201 case HLoadClass::LoadKind::kJitTableAddress:
5202 __ LoadLiteral(out,
5203 kLoadUnsignedWord,
5204 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5205 cls->GetTypeIndex(),
5206 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005207 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005208 break;
Vladimir Marko41559982017-01-06 14:04:23 +00005209 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005210 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005211 LOG(FATAL) << "UNREACHABLE";
5212 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005213 }
5214
5215 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5216 DCHECK(cls->CanCallRuntime());
5217 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5218 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5219 codegen_->AddSlowPath(slow_path);
5220 if (generate_null_check) {
5221 __ Beqzc(out, slow_path->GetEntryLabel());
5222 }
5223 if (cls->MustGenerateClinitCheck()) {
5224 GenerateClassInitializationCheck(slow_path, out);
5225 } else {
5226 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005227 }
5228 }
5229}
5230
David Brazdilcb1c0552015-08-04 16:22:25 +01005231static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005232 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005233}
5234
Alexey Frunze4dda3372015-06-01 18:31:49 -07005235void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5236 LocationSummary* locations =
5237 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5238 locations->SetOut(Location::RequiresRegister());
5239}
5240
5241void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5242 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005243 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5244}
5245
5246void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5247 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5248}
5249
5250void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5251 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005252}
5253
Alexey Frunze4dda3372015-06-01 18:31:49 -07005254void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005255 HLoadString::LoadKind load_kind = load->GetLoadKind();
5256 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005258 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5259 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005260 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005261 } else {
5262 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07005263 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5264 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5265 // Rely on the pResolveString and marking to save everything we need.
5266 RegisterSet caller_saves = RegisterSet::Empty();
5267 InvokeRuntimeCallingConvention calling_convention;
5268 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5269 locations->SetCustomSlowPathCallerSaves(caller_saves);
5270 } else {
5271 // For non-Baker read barrier we have a temp-clobbering call.
5272 }
5273 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08005274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005275}
5276
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005277// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5278// move.
5279void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005280 HLoadString::LoadKind load_kind = load->GetLoadKind();
5281 LocationSummary* locations = load->GetLocations();
5282 Location out_loc = locations->Out();
5283 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5284
5285 switch (load_kind) {
5286 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005287 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005288 __ LoadLiteral(out,
5289 kLoadUnsignedWord,
5290 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5291 load->GetStringIndex()));
5292 return; // No dex cache slow path.
5293 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5294 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5295 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005296 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005297 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5298 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5299 return; // No dex cache slow path.
5300 }
5301 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005302 uint32_t address = dchecked_integral_cast<uint32_t>(
5303 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5304 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005305 __ LoadLiteral(out,
5306 kLoadUnsignedWord,
5307 codegen_->DeduplicateBootImageAddressLiteral(address));
5308 return; // No dex cache slow path.
5309 }
5310 case HLoadString::LoadKind::kBssEntry: {
5311 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5312 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005313 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005314 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005315 GenerateGcRootFieldLoad(load,
5316 out_loc,
5317 out,
5318 /* placeholder */ 0x5678,
5319 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005320 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5321 codegen_->AddSlowPath(slow_path);
5322 __ Beqzc(out, slow_path->GetEntryLabel());
5323 __ Bind(slow_path->GetExitLabel());
5324 return;
5325 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005326 case HLoadString::LoadKind::kJitTableAddress:
5327 __ LoadLiteral(out,
5328 kLoadUnsignedWord,
5329 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5330 load->GetStringIndex(),
5331 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005332 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005333 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005334 default:
5335 break;
5336 }
5337
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005338 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005339 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5340 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07005341 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005342 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5343 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5344 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005345}
5346
Alexey Frunze4dda3372015-06-01 18:31:49 -07005347void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5349 locations->SetOut(Location::ConstantLocation(constant));
5350}
5351
5352void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5353 // Will be generated at use site.
5354}
5355
5356void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5357 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005359 InvokeRuntimeCallingConvention calling_convention;
5360 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5361}
5362
5363void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005364 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005365 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005366 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005367 if (instruction->IsEnter()) {
5368 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5369 } else {
5370 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5371 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005372}
5373
5374void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5375 LocationSummary* locations =
5376 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5377 switch (mul->GetResultType()) {
5378 case Primitive::kPrimInt:
5379 case Primitive::kPrimLong:
5380 locations->SetInAt(0, Location::RequiresRegister());
5381 locations->SetInAt(1, Location::RequiresRegister());
5382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5383 break;
5384
5385 case Primitive::kPrimFloat:
5386 case Primitive::kPrimDouble:
5387 locations->SetInAt(0, Location::RequiresFpuRegister());
5388 locations->SetInAt(1, Location::RequiresFpuRegister());
5389 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5390 break;
5391
5392 default:
5393 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5394 }
5395}
5396
5397void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5398 Primitive::Type type = instruction->GetType();
5399 LocationSummary* locations = instruction->GetLocations();
5400
5401 switch (type) {
5402 case Primitive::kPrimInt:
5403 case Primitive::kPrimLong: {
5404 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5405 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5406 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5407 if (type == Primitive::kPrimInt)
5408 __ MulR6(dst, lhs, rhs);
5409 else
5410 __ Dmul(dst, lhs, rhs);
5411 break;
5412 }
5413 case Primitive::kPrimFloat:
5414 case Primitive::kPrimDouble: {
5415 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5416 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5417 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5418 if (type == Primitive::kPrimFloat)
5419 __ MulS(dst, lhs, rhs);
5420 else
5421 __ MulD(dst, lhs, rhs);
5422 break;
5423 }
5424 default:
5425 LOG(FATAL) << "Unexpected mul type " << type;
5426 }
5427}
5428
5429void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5430 LocationSummary* locations =
5431 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5432 switch (neg->GetResultType()) {
5433 case Primitive::kPrimInt:
5434 case Primitive::kPrimLong:
5435 locations->SetInAt(0, Location::RequiresRegister());
5436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5437 break;
5438
5439 case Primitive::kPrimFloat:
5440 case Primitive::kPrimDouble:
5441 locations->SetInAt(0, Location::RequiresFpuRegister());
5442 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5443 break;
5444
5445 default:
5446 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5447 }
5448}
5449
5450void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5451 Primitive::Type type = instruction->GetType();
5452 LocationSummary* locations = instruction->GetLocations();
5453
5454 switch (type) {
5455 case Primitive::kPrimInt:
5456 case Primitive::kPrimLong: {
5457 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5458 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5459 if (type == Primitive::kPrimInt)
5460 __ Subu(dst, ZERO, src);
5461 else
5462 __ Dsubu(dst, ZERO, src);
5463 break;
5464 }
5465 case Primitive::kPrimFloat:
5466 case Primitive::kPrimDouble: {
5467 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5468 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5469 if (type == Primitive::kPrimFloat)
5470 __ NegS(dst, src);
5471 else
5472 __ NegD(dst, src);
5473 break;
5474 }
5475 default:
5476 LOG(FATAL) << "Unexpected neg type " << type;
5477 }
5478}
5479
5480void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5481 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005483 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005484 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005485 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5486 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005487}
5488
5489void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005490 // Note: if heap poisoning is enabled, the entry point takes care
5491 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005492 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5493 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005494}
5495
5496void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5497 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005499 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005500 if (instruction->IsStringAlloc()) {
5501 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5502 } else {
5503 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005504 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005505 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5506}
5507
5508void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005509 // Note: if heap poisoning is enabled, the entry point takes care
5510 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005511 if (instruction->IsStringAlloc()) {
5512 // String is allocated through StringFactory. Call NewEmptyString entry point.
5513 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005514 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005515 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005516 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5517 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5518 __ Jalr(T9);
5519 __ Nop();
5520 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5521 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005522 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005523 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005524 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005525}
5526
5527void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5529 locations->SetInAt(0, Location::RequiresRegister());
5530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5531}
5532
5533void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5534 Primitive::Type type = instruction->GetType();
5535 LocationSummary* locations = instruction->GetLocations();
5536
5537 switch (type) {
5538 case Primitive::kPrimInt:
5539 case Primitive::kPrimLong: {
5540 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5541 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5542 __ Nor(dst, src, ZERO);
5543 break;
5544 }
5545
5546 default:
5547 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5548 }
5549}
5550
5551void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5553 locations->SetInAt(0, Location::RequiresRegister());
5554 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5555}
5556
5557void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5558 LocationSummary* locations = instruction->GetLocations();
5559 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5560 locations->InAt(0).AsRegister<GpuRegister>(),
5561 1);
5562}
5563
5564void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005565 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5566 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005567}
5568
Calin Juravle2ae48182016-03-16 14:05:09 +00005569void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5570 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005571 return;
5572 }
5573 Location obj = instruction->GetLocations()->InAt(0);
5574
5575 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005576 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005577}
5578
Calin Juravle2ae48182016-03-16 14:05:09 +00005579void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005580 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005581 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005582
5583 Location obj = instruction->GetLocations()->InAt(0);
5584
5585 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5586}
5587
5588void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005589 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005590}
5591
5592void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5593 HandleBinaryOp(instruction);
5594}
5595
5596void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5597 HandleBinaryOp(instruction);
5598}
5599
5600void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5601 LOG(FATAL) << "Unreachable";
5602}
5603
5604void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5605 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5606}
5607
5608void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5610 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5611 if (location.IsStackSlot()) {
5612 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5613 } else if (location.IsDoubleStackSlot()) {
5614 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5615 }
5616 locations->SetOut(location);
5617}
5618
5619void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5620 ATTRIBUTE_UNUSED) {
5621 // Nothing to do, the parameter is already at its location.
5622}
5623
5624void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5625 LocationSummary* locations =
5626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5627 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5628}
5629
5630void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5631 ATTRIBUTE_UNUSED) {
5632 // Nothing to do, the method is already at its location.
5633}
5634
5635void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005637 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005638 locations->SetInAt(i, Location::Any());
5639 }
5640 locations->SetOut(Location::Any());
5641}
5642
5643void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5644 LOG(FATAL) << "Unreachable";
5645}
5646
5647void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5648 Primitive::Type type = rem->GetResultType();
5649 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005650 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5651 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5653
5654 switch (type) {
5655 case Primitive::kPrimInt:
5656 case Primitive::kPrimLong:
5657 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005658 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5660 break;
5661
5662 case Primitive::kPrimFloat:
5663 case Primitive::kPrimDouble: {
5664 InvokeRuntimeCallingConvention calling_convention;
5665 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5666 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5667 locations->SetOut(calling_convention.GetReturnLocation(type));
5668 break;
5669 }
5670
5671 default:
5672 LOG(FATAL) << "Unexpected rem type " << type;
5673 }
5674}
5675
5676void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5677 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005678
5679 switch (type) {
5680 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005681 case Primitive::kPrimLong:
5682 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005683 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005684
5685 case Primitive::kPrimFloat:
5686 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005687 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5688 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005689 if (type == Primitive::kPrimFloat) {
5690 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5691 } else {
5692 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5693 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005694 break;
5695 }
5696 default:
5697 LOG(FATAL) << "Unexpected rem type " << type;
5698 }
5699}
5700
Igor Murashkind01745e2017-04-05 16:40:31 -07005701void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5702 constructor_fence->SetLocations(nullptr);
5703}
5704
5705void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5706 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5707 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5708}
5709
Alexey Frunze4dda3372015-06-01 18:31:49 -07005710void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5711 memory_barrier->SetLocations(nullptr);
5712}
5713
5714void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5715 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5716}
5717
5718void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5720 Primitive::Type return_type = ret->InputAt(0)->GetType();
5721 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5722}
5723
5724void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5725 codegen_->GenerateFrameExit();
5726}
5727
5728void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5729 ret->SetLocations(nullptr);
5730}
5731
5732void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5733 codegen_->GenerateFrameExit();
5734}
5735
Alexey Frunze92d90602015-12-18 18:16:36 -08005736void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5737 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005738}
5739
Alexey Frunze92d90602015-12-18 18:16:36 -08005740void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5741 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005742}
5743
Alexey Frunze4dda3372015-06-01 18:31:49 -07005744void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5745 HandleShift(shl);
5746}
5747
5748void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5749 HandleShift(shl);
5750}
5751
5752void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5753 HandleShift(shr);
5754}
5755
5756void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5757 HandleShift(shr);
5758}
5759
Alexey Frunze4dda3372015-06-01 18:31:49 -07005760void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5761 HandleBinaryOp(instruction);
5762}
5763
5764void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5765 HandleBinaryOp(instruction);
5766}
5767
5768void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5769 HandleFieldGet(instruction, instruction->GetFieldInfo());
5770}
5771
5772void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5773 HandleFieldGet(instruction, instruction->GetFieldInfo());
5774}
5775
5776void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5777 HandleFieldSet(instruction, instruction->GetFieldInfo());
5778}
5779
5780void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005781 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005782}
5783
Calin Juravlee460d1d2015-09-29 04:52:17 +01005784void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5785 HUnresolvedInstanceFieldGet* instruction) {
5786 FieldAccessCallingConventionMIPS64 calling_convention;
5787 codegen_->CreateUnresolvedFieldLocationSummary(
5788 instruction, instruction->GetFieldType(), calling_convention);
5789}
5790
5791void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5792 HUnresolvedInstanceFieldGet* instruction) {
5793 FieldAccessCallingConventionMIPS64 calling_convention;
5794 codegen_->GenerateUnresolvedFieldAccess(instruction,
5795 instruction->GetFieldType(),
5796 instruction->GetFieldIndex(),
5797 instruction->GetDexPc(),
5798 calling_convention);
5799}
5800
5801void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5802 HUnresolvedInstanceFieldSet* instruction) {
5803 FieldAccessCallingConventionMIPS64 calling_convention;
5804 codegen_->CreateUnresolvedFieldLocationSummary(
5805 instruction, instruction->GetFieldType(), calling_convention);
5806}
5807
5808void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5809 HUnresolvedInstanceFieldSet* instruction) {
5810 FieldAccessCallingConventionMIPS64 calling_convention;
5811 codegen_->GenerateUnresolvedFieldAccess(instruction,
5812 instruction->GetFieldType(),
5813 instruction->GetFieldIndex(),
5814 instruction->GetDexPc(),
5815 calling_convention);
5816}
5817
5818void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5819 HUnresolvedStaticFieldGet* instruction) {
5820 FieldAccessCallingConventionMIPS64 calling_convention;
5821 codegen_->CreateUnresolvedFieldLocationSummary(
5822 instruction, instruction->GetFieldType(), calling_convention);
5823}
5824
5825void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5826 HUnresolvedStaticFieldGet* instruction) {
5827 FieldAccessCallingConventionMIPS64 calling_convention;
5828 codegen_->GenerateUnresolvedFieldAccess(instruction,
5829 instruction->GetFieldType(),
5830 instruction->GetFieldIndex(),
5831 instruction->GetDexPc(),
5832 calling_convention);
5833}
5834
5835void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5836 HUnresolvedStaticFieldSet* instruction) {
5837 FieldAccessCallingConventionMIPS64 calling_convention;
5838 codegen_->CreateUnresolvedFieldLocationSummary(
5839 instruction, instruction->GetFieldType(), calling_convention);
5840}
5841
5842void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5843 HUnresolvedStaticFieldSet* instruction) {
5844 FieldAccessCallingConventionMIPS64 calling_convention;
5845 codegen_->GenerateUnresolvedFieldAccess(instruction,
5846 instruction->GetFieldType(),
5847 instruction->GetFieldIndex(),
5848 instruction->GetDexPc(),
5849 calling_convention);
5850}
5851
Alexey Frunze4dda3372015-06-01 18:31:49 -07005852void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005853 LocationSummary* locations =
5854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005855 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005856}
5857
5858void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5859 HBasicBlock* block = instruction->GetBlock();
5860 if (block->GetLoopInformation() != nullptr) {
5861 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5862 // The back edge will generate the suspend check.
5863 return;
5864 }
5865 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5866 // The goto will generate the suspend check.
5867 return;
5868 }
5869 GenerateSuspendCheck(instruction, nullptr);
5870}
5871
Alexey Frunze4dda3372015-06-01 18:31:49 -07005872void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5873 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005875 InvokeRuntimeCallingConvention calling_convention;
5876 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5877}
5878
5879void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005880 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005881 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5882}
5883
5884void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5885 Primitive::Type input_type = conversion->GetInputType();
5886 Primitive::Type result_type = conversion->GetResultType();
5887 DCHECK_NE(input_type, result_type);
5888
5889 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5890 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5891 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5892 }
5893
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5895
5896 if (Primitive::IsFloatingPointType(input_type)) {
5897 locations->SetInAt(0, Location::RequiresFpuRegister());
5898 } else {
5899 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005900 }
5901
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005902 if (Primitive::IsFloatingPointType(result_type)) {
5903 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005904 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005905 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005906 }
5907}
5908
5909void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5910 LocationSummary* locations = conversion->GetLocations();
5911 Primitive::Type result_type = conversion->GetResultType();
5912 Primitive::Type input_type = conversion->GetInputType();
5913
5914 DCHECK_NE(input_type, result_type);
5915
5916 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5917 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5918 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5919
5920 switch (result_type) {
5921 case Primitive::kPrimChar:
5922 __ Andi(dst, src, 0xFFFF);
5923 break;
5924 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005925 if (input_type == Primitive::kPrimLong) {
5926 // Type conversion from long to types narrower than int is a result of code
5927 // transformations. To avoid unpredictable results for SEB and SEH, we first
5928 // need to sign-extend the low 32-bit value into bits 32 through 63.
5929 __ Sll(dst, src, 0);
5930 __ Seb(dst, dst);
5931 } else {
5932 __ Seb(dst, src);
5933 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005934 break;
5935 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005936 if (input_type == Primitive::kPrimLong) {
5937 // Type conversion from long to types narrower than int is a result of code
5938 // transformations. To avoid unpredictable results for SEB and SEH, we first
5939 // need to sign-extend the low 32-bit value into bits 32 through 63.
5940 __ Sll(dst, src, 0);
5941 __ Seh(dst, dst);
5942 } else {
5943 __ Seh(dst, src);
5944 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005945 break;
5946 case Primitive::kPrimInt:
5947 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005948 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5949 // conversions, except when the input and output registers are the same and we are not
5950 // converting longs to shorter types. In these cases, do nothing.
5951 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5952 __ Sll(dst, src, 0);
5953 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005954 break;
5955
5956 default:
5957 LOG(FATAL) << "Unexpected type conversion from " << input_type
5958 << " to " << result_type;
5959 }
5960 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005961 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5962 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5963 if (input_type == Primitive::kPrimLong) {
5964 __ Dmtc1(src, FTMP);
5965 if (result_type == Primitive::kPrimFloat) {
5966 __ Cvtsl(dst, FTMP);
5967 } else {
5968 __ Cvtdl(dst, FTMP);
5969 }
5970 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005971 __ Mtc1(src, FTMP);
5972 if (result_type == Primitive::kPrimFloat) {
5973 __ Cvtsw(dst, FTMP);
5974 } else {
5975 __ Cvtdw(dst, FTMP);
5976 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005977 }
5978 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5979 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005980 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5981 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5982 Mips64Label truncate;
5983 Mips64Label done;
5984
5985 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5986 // value when the input is either a NaN or is outside of the range of the output type
5987 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5988 // the same result.
5989 //
5990 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5991 // value of the output type if the input is outside of the range after the truncation or
5992 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5993 // results. This matches the desired float/double-to-int/long conversion exactly.
5994 //
5995 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5996 //
5997 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5998 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5999 // even though it must be NAN2008=1 on R6.
6000 //
6001 // The code takes care of the different behaviors by first comparing the input to the
6002 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6003 // If the input is greater than or equal to the minimum, it procedes to the truncate
6004 // instruction, which will handle such an input the same way irrespective of NAN2008.
6005 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6006 // in order to return either zero or the minimum value.
6007 //
6008 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6009 // truncate instruction for MIPS64R6.
6010 if (input_type == Primitive::kPrimFloat) {
6011 uint32_t min_val = (result_type == Primitive::kPrimLong)
6012 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
6013 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6014 __ LoadConst32(TMP, min_val);
6015 __ Mtc1(TMP, FTMP);
6016 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006017 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006018 uint64_t min_val = (result_type == Primitive::kPrimLong)
6019 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
6020 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6021 __ LoadConst64(TMP, min_val);
6022 __ Dmtc1(TMP, FTMP);
6023 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006024 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006025
6026 __ Bc1nez(FTMP, &truncate);
6027
6028 if (input_type == Primitive::kPrimFloat) {
6029 __ CmpEqS(FTMP, src, src);
6030 } else {
6031 __ CmpEqD(FTMP, src, src);
6032 }
6033 if (result_type == Primitive::kPrimLong) {
6034 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
6035 } else {
6036 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6037 }
6038 __ Mfc1(TMP, FTMP);
6039 __ And(dst, dst, TMP);
6040
6041 __ Bc(&done);
6042
6043 __ Bind(&truncate);
6044
6045 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006046 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006047 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006048 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006049 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006050 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006051 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006052 } else {
6053 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006054 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006055 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006056 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006057 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006058 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006059 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006060
6061 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006062 } else if (Primitive::IsFloatingPointType(result_type) &&
6063 Primitive::IsFloatingPointType(input_type)) {
6064 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6065 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6066 if (result_type == Primitive::kPrimFloat) {
6067 __ Cvtsd(dst, src);
6068 } else {
6069 __ Cvtds(dst, src);
6070 }
6071 } else {
6072 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6073 << " to " << result_type;
6074 }
6075}
6076
6077void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6078 HandleShift(ushr);
6079}
6080
6081void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6082 HandleShift(ushr);
6083}
6084
6085void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6086 HandleBinaryOp(instruction);
6087}
6088
6089void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6090 HandleBinaryOp(instruction);
6091}
6092
6093void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6094 // Nothing to do, this should be removed during prepare for register allocator.
6095 LOG(FATAL) << "Unreachable";
6096}
6097
6098void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6099 // Nothing to do, this should be removed during prepare for register allocator.
6100 LOG(FATAL) << "Unreachable";
6101}
6102
6103void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006104 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006105}
6106
6107void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006108 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006109}
6110
6111void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006112 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006113}
6114
6115void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006116 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006117}
6118
6119void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006120 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006121}
6122
6123void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006124 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006125}
6126
6127void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006128 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006129}
6130
6131void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006132 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006133}
6134
6135void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006136 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006137}
6138
6139void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006140 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006141}
6142
6143void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006144 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006145}
6146
6147void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006148 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006149}
6150
Aart Bike9f37602015-10-09 11:15:55 -07006151void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006152 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006153}
6154
6155void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006156 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006157}
6158
6159void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006160 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006161}
6162
6163void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006164 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006165}
6166
6167void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006168 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006169}
6170
6171void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006172 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006173}
6174
6175void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006176 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006177}
6178
6179void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006180 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006181}
6182
Mark Mendellfe57faa2015-09-18 09:26:15 -04006183// Simple implementation of packed switch - generate cascaded compare/jumps.
6184void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6185 LocationSummary* locations =
6186 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6187 locations->SetInAt(0, Location::RequiresRegister());
6188}
6189
Alexey Frunze0960ac52016-12-20 17:24:59 -08006190void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6191 int32_t lower_bound,
6192 uint32_t num_entries,
6193 HBasicBlock* switch_block,
6194 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006195 // Create a set of compare/jumps.
6196 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006197 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006198 // Jump to default if index is negative
6199 // Note: We don't check the case that index is positive while value < lower_bound, because in
6200 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6201 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6202
Alexey Frunze0960ac52016-12-20 17:24:59 -08006203 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006204 // Jump to successors[0] if value == lower_bound.
6205 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6206 int32_t last_index = 0;
6207 for (; num_entries - last_index > 2; last_index += 2) {
6208 __ Addiu(temp_reg, temp_reg, -2);
6209 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6210 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6211 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6212 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6213 }
6214 if (num_entries - last_index == 2) {
6215 // The last missing case_value.
6216 __ Addiu(temp_reg, temp_reg, -1);
6217 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006218 }
6219
6220 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006221 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006222 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006223 }
6224}
6225
Alexey Frunze0960ac52016-12-20 17:24:59 -08006226void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6227 int32_t lower_bound,
6228 uint32_t num_entries,
6229 HBasicBlock* switch_block,
6230 HBasicBlock* default_block) {
6231 // Create a jump table.
6232 std::vector<Mips64Label*> labels(num_entries);
6233 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6234 for (uint32_t i = 0; i < num_entries; i++) {
6235 labels[i] = codegen_->GetLabelOf(successors[i]);
6236 }
6237 JumpTable* table = __ CreateJumpTable(std::move(labels));
6238
6239 // Is the value in range?
6240 __ Addiu32(TMP, value_reg, -lower_bound);
6241 __ LoadConst32(AT, num_entries);
6242 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6243
6244 // We are in the range of the table.
6245 // Load the target address from the jump table, indexing by the value.
6246 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006247 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006248 __ Lw(TMP, TMP, 0);
6249 // Compute the absolute target address by adding the table start address
6250 // (the table contains offsets to targets relative to its start).
6251 __ Daddu(TMP, TMP, AT);
6252 // And jump.
6253 __ Jr(TMP);
6254 __ Nop();
6255}
6256
6257void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6258 int32_t lower_bound = switch_instr->GetStartValue();
6259 uint32_t num_entries = switch_instr->GetNumEntries();
6260 LocationSummary* locations = switch_instr->GetLocations();
6261 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6262 HBasicBlock* switch_block = switch_instr->GetBlock();
6263 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6264
6265 if (num_entries > kPackedSwitchJumpTableThreshold) {
6266 GenTableBasedPackedSwitch(value_reg,
6267 lower_bound,
6268 num_entries,
6269 switch_block,
6270 default_block);
6271 } else {
6272 GenPackedSwitchWithCompares(value_reg,
6273 lower_bound,
6274 num_entries,
6275 switch_block,
6276 default_block);
6277 }
6278}
6279
Chris Larsenc9905a62017-03-13 17:06:18 -07006280void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6281 LocationSummary* locations =
6282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6283 locations->SetInAt(0, Location::RequiresRegister());
6284 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006285}
6286
Chris Larsenc9905a62017-03-13 17:06:18 -07006287void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6288 LocationSummary* locations = instruction->GetLocations();
6289 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6290 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6291 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6292 __ LoadFromOffset(kLoadDoubleword,
6293 locations->Out().AsRegister<GpuRegister>(),
6294 locations->InAt(0).AsRegister<GpuRegister>(),
6295 method_offset);
6296 } else {
6297 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6298 instruction->GetIndex(), kMips64PointerSize));
6299 __ LoadFromOffset(kLoadDoubleword,
6300 locations->Out().AsRegister<GpuRegister>(),
6301 locations->InAt(0).AsRegister<GpuRegister>(),
6302 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6303 __ LoadFromOffset(kLoadDoubleword,
6304 locations->Out().AsRegister<GpuRegister>(),
6305 locations->Out().AsRegister<GpuRegister>(),
6306 method_offset);
6307 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006308}
6309
Alexey Frunze4dda3372015-06-01 18:31:49 -07006310} // namespace mips64
6311} // namespace art