blob: 78b31e9e8682c60ed0cb19a8af6239972c58f51a [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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
147 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
148 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100149 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
151 }
152
Alexandre Rames8158f282015-08-07 10:26:17 +0100153 bool IsFatal() const OVERRIDE { return true; }
154
Roland Levillain46648892015-06-19 16:07:18 +0100155 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
156
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
159};
160
161class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
162 public:
163 LoadClassSlowPathMIPS64(HLoadClass* cls,
164 HInstruction* at,
165 uint32_t dex_pc,
166 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000167 : SlowPathCodeMIPS64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
169 }
170
171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000172 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700173 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
174
175 __ Bind(GetEntryLabel());
176 SaveLiveRegisters(codegen, locations);
177
178 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000179 dex::TypeIndex type_index = cls_->GetTypeIndex();
180 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100181 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
182 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000183 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700184 if (do_clinit_) {
185 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
186 } else {
187 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
188 }
189
190 // Move the class to the desired location.
191 Location out = locations->Out();
192 if (out.IsValid()) {
193 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000194 Primitive::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700195 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
196 }
197
198 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
200 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
201 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
202 DCHECK(out.IsValid());
203 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
204 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
205 // so that we can avoid another calculation here.
206 DCHECK_NE(out.AsRegister<GpuRegister>(), AT);
207 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000208 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000209 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
210 __ Sw(out.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
211 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700212 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
214
Roland Levillain46648892015-06-19 16:07:18 +0100215 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
216
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 private:
218 // The class this slow path will load.
219 HLoadClass* const cls_;
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 // The dex PC of `at_`.
222 const uint32_t dex_pc_;
223
224 // Whether to initialize the class.
225 const bool do_clinit_;
226
227 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
228};
229
230class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
231 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000232 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700233
234 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
235 LocationSummary* locations = instruction_->GetLocations();
236 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
237 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
238
239 __ Bind(GetEntryLabel());
240 SaveLiveRegisters(codegen, locations);
241
242 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800243 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100246 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 instruction_,
248 instruction_->GetDexPc(),
249 this);
250 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
251 Primitive::Type type = instruction_->GetType();
252 mips64_codegen->MoveLocation(locations->Out(),
253 calling_convention.GetReturnLocation(type),
254 type);
255
256 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800257
258 // Store the resolved String to the BSS entry.
259 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
260 // .bss entry address in the fast path, so that we can avoid another calculation here.
261 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
262 DCHECK_NE(out, AT);
263 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
264 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
265 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
266 __ Sw(out, AT, /* placeholder */ 0x5678);
267
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700268 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 }
270
Roland Levillain46648892015-06-19 16:07:18 +0100271 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
272
Alexey Frunze4dda3372015-06-01 18:31:49 -0700273 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700274 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
275};
276
277class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
278 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700280
281 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
282 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
283 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000284 if (instruction_->CanThrowIntoCatchBlock()) {
285 // Live registers will be restored in the catch block if caught.
286 SaveLiveRegisters(codegen, instruction_->GetLocations());
287 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100288 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289 instruction_,
290 instruction_->GetDexPc(),
291 this);
292 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
293 }
294
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Roland Levillain46648892015-06-19 16:07:18 +0100297 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
298
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
301};
302
303class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
304 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100305 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000306 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
309 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
310 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100311 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700316 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700317 }
318 }
319
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700320 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700321 DCHECK(successor_ == nullptr);
322 return &return_label_;
323 }
324
Roland Levillain46648892015-06-19 16:07:18 +0100325 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
326
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 // If not null, the block to branch to after the suspend check.
329 HBasicBlock* const successor_;
330
331 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700332 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333
334 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
335};
336
337class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
338 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800339 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
340 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800344
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100345 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346 DCHECK(instruction_->IsCheckCast()
347 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
348 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
349
350 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800351 if (!is_fatal_) {
352 SaveLiveRegisters(codegen, locations);
353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354
355 // We're moving two locations to locations that could overlap, so we need a parallel
356 // move resolver.
357 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
360 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
363 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100365 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800366 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 Primitive::Type ret_type = instruction_->GetType();
368 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
369 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 } else {
371 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800372 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
373 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 }
375
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800376 if (!is_fatal_) {
377 RestoreLiveRegisters(codegen, locations);
378 __ Bc(GetExitLabel());
379 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 }
381
Roland Levillain46648892015-06-19 16:07:18 +0100382 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
383
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800384 bool IsFatal() const OVERRIDE { return is_fatal_; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 const bool is_fatal_;
388
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
390};
391
392class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800398 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100400 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 }
403
Roland Levillain46648892015-06-19 16:07:18 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
405
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
408};
409
Alexey Frunze15958152017-02-09 19:08:30 -0800410class ArraySetSlowPathMIPS64 : public SlowPathCodeMIPS64 {
411 public:
412 explicit ArraySetSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
413
414 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
415 LocationSummary* locations = instruction_->GetLocations();
416 __ Bind(GetEntryLabel());
417 SaveLiveRegisters(codegen, locations);
418
419 InvokeRuntimeCallingConvention calling_convention;
420 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
421 parallel_move.AddMove(
422 locations->InAt(0),
423 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
424 Primitive::kPrimNot,
425 nullptr);
426 parallel_move.AddMove(
427 locations->InAt(1),
428 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
429 Primitive::kPrimInt,
430 nullptr);
431 parallel_move.AddMove(
432 locations->InAt(2),
433 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
434 Primitive::kPrimNot,
435 nullptr);
436 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
437
438 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
439 mips64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
440 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
441 RestoreLiveRegisters(codegen, locations);
442 __ Bc(GetExitLabel());
443 }
444
445 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS64"; }
446
447 private:
448 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS64);
449};
450
451// Slow path marking an object reference `ref` during a read
452// barrier. The field `obj.field` in the object `obj` holding this
453// reference does not get updated by this slow path after marking (see
454// ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 below for that).
455//
456// This means that after the execution of this slow path, `ref` will
457// always be up-to-date, but `obj.field` may not; i.e., after the
458// flip, `ref` will be a to-space reference, but `obj.field` will
459// probably still be a from-space reference (unless it gets updated by
460// another thread, or if another thread installed another object
461// reference (different from `ref`) in `obj.field`).
462//
463// If `entrypoint` is a valid location it is assumed to already be
464// holding the entrypoint. The case where the entrypoint is passed in
465// is for the GcRoot read barrier.
466class ReadBarrierMarkSlowPathMIPS64 : public SlowPathCodeMIPS64 {
467 public:
468 ReadBarrierMarkSlowPathMIPS64(HInstruction* instruction,
469 Location ref,
470 Location entrypoint = Location::NoLocation())
471 : SlowPathCodeMIPS64(instruction), ref_(ref), entrypoint_(entrypoint) {
472 DCHECK(kEmitCompilerReadBarrier);
473 }
474
475 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
476
477 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
478 LocationSummary* locations = instruction_->GetLocations();
479 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
480 DCHECK(locations->CanCall());
481 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
482 DCHECK(instruction_->IsInstanceFieldGet() ||
483 instruction_->IsStaticFieldGet() ||
484 instruction_->IsArrayGet() ||
485 instruction_->IsArraySet() ||
486 instruction_->IsLoadClass() ||
487 instruction_->IsLoadString() ||
488 instruction_->IsInstanceOf() ||
489 instruction_->IsCheckCast() ||
490 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
491 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
492 << "Unexpected instruction in read barrier marking slow path: "
493 << instruction_->DebugName();
494
495 __ Bind(GetEntryLabel());
496 // No need to save live registers; it's taken care of by the
497 // entrypoint. Also, there is no need to update the stack mask,
498 // as this runtime call will not trigger a garbage collection.
499 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
500 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
501 (S2 <= ref_reg && ref_reg <= S7) ||
502 (ref_reg == S8)) << ref_reg;
503 // "Compact" slow path, saving two moves.
504 //
505 // Instead of using the standard runtime calling convention (input
506 // and output in A0 and V0 respectively):
507 //
508 // A0 <- ref
509 // V0 <- ReadBarrierMark(A0)
510 // ref <- V0
511 //
512 // we just use rX (the register containing `ref`) as input and output
513 // of a dedicated entrypoint:
514 //
515 // rX <- ReadBarrierMarkRegX(rX)
516 //
517 if (entrypoint_.IsValid()) {
518 mips64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
519 DCHECK_EQ(entrypoint_.AsRegister<GpuRegister>(), T9);
520 __ Jalr(entrypoint_.AsRegister<GpuRegister>());
521 __ Nop();
522 } else {
523 int32_t entry_point_offset =
524 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
525 // This runtime call does not require a stack map.
526 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
527 instruction_,
528 this);
529 }
530 __ Bc(GetExitLabel());
531 }
532
533 private:
534 // The location (register) of the marked object reference.
535 const Location ref_;
536
537 // The location of the entrypoint if already loaded.
538 const Location entrypoint_;
539
540 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS64);
541};
542
543// Slow path marking an object reference `ref` during a read barrier,
544// and if needed, atomically updating the field `obj.field` in the
545// object `obj` holding this reference after marking (contrary to
546// ReadBarrierMarkSlowPathMIPS64 above, which never tries to update
547// `obj.field`).
548//
549// This means that after the execution of this slow path, both `ref`
550// and `obj.field` will be up-to-date; i.e., after the flip, both will
551// hold the same to-space reference (unless another thread installed
552// another object reference (different from `ref`) in `obj.field`).
553class ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 : public SlowPathCodeMIPS64 {
554 public:
555 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(HInstruction* instruction,
556 Location ref,
557 GpuRegister obj,
558 Location field_offset,
559 GpuRegister temp1)
560 : SlowPathCodeMIPS64(instruction),
561 ref_(ref),
562 obj_(obj),
563 field_offset_(field_offset),
564 temp1_(temp1) {
565 DCHECK(kEmitCompilerReadBarrier);
566 }
567
568 const char* GetDescription() const OVERRIDE {
569 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS64";
570 }
571
572 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
573 LocationSummary* locations = instruction_->GetLocations();
574 GpuRegister ref_reg = ref_.AsRegister<GpuRegister>();
575 DCHECK(locations->CanCall());
576 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
577 // This slow path is only used by the UnsafeCASObject intrinsic.
578 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
579 << "Unexpected instruction in read barrier marking and field updating slow path: "
580 << instruction_->DebugName();
581 DCHECK(instruction_->GetLocations()->Intrinsified());
582 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
583 DCHECK(field_offset_.IsRegister()) << field_offset_;
584
585 __ Bind(GetEntryLabel());
586
587 // Save the old reference.
588 // Note that we cannot use AT or TMP to save the old reference, as those
589 // are used by the code that follows, but we need the old reference after
590 // the call to the ReadBarrierMarkRegX entry point.
591 DCHECK_NE(temp1_, AT);
592 DCHECK_NE(temp1_, TMP);
593 __ Move(temp1_, ref_reg);
594
595 // No need to save live registers; it's taken care of by the
596 // entrypoint. Also, there is no need to update the stack mask,
597 // as this runtime call will not trigger a garbage collection.
598 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
599 DCHECK((V0 <= ref_reg && ref_reg <= T2) ||
600 (S2 <= ref_reg && ref_reg <= S7) ||
601 (ref_reg == S8)) << ref_reg;
602 // "Compact" slow path, saving two moves.
603 //
604 // Instead of using the standard runtime calling convention (input
605 // and output in A0 and V0 respectively):
606 //
607 // A0 <- ref
608 // V0 <- ReadBarrierMark(A0)
609 // ref <- V0
610 //
611 // we just use rX (the register containing `ref`) as input and output
612 // of a dedicated entrypoint:
613 //
614 // rX <- ReadBarrierMarkRegX(rX)
615 //
616 int32_t entry_point_offset =
617 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(ref_reg - 1);
618 // This runtime call does not require a stack map.
619 mips64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
620 instruction_,
621 this);
622
623 // If the new reference is different from the old reference,
624 // update the field in the holder (`*(obj_ + field_offset_)`).
625 //
626 // Note that this field could also hold a different object, if
627 // another thread had concurrently changed it. In that case, the
628 // the compare-and-set (CAS) loop below would abort, leaving the
629 // field as-is.
630 Mips64Label done;
631 __ Beqc(temp1_, ref_reg, &done);
632
633 // Update the the holder's field atomically. This may fail if
634 // mutator updates before us, but it's OK. This is achieved
635 // using a strong compare-and-set (CAS) operation with relaxed
636 // memory synchronization ordering, where the expected value is
637 // the old reference and the desired value is the new reference.
638
639 // Convenience aliases.
640 GpuRegister base = obj_;
641 GpuRegister offset = field_offset_.AsRegister<GpuRegister>();
642 GpuRegister expected = temp1_;
643 GpuRegister value = ref_reg;
644 GpuRegister tmp_ptr = TMP; // Pointer to actual memory.
645 GpuRegister tmp = AT; // Value in memory.
646
647 __ Daddu(tmp_ptr, base, offset);
648
649 if (kPoisonHeapReferences) {
650 __ PoisonHeapReference(expected);
651 // Do not poison `value` if it is the same register as
652 // `expected`, which has just been poisoned.
653 if (value != expected) {
654 __ PoisonHeapReference(value);
655 }
656 }
657
658 // do {
659 // tmp = [r_ptr] - expected;
660 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
661
662 Mips64Label loop_head, exit_loop;
663 __ Bind(&loop_head);
664 __ Ll(tmp, tmp_ptr);
665 // The LL instruction sign-extends the 32-bit value, but
666 // 32-bit references must be zero-extended. Zero-extend `tmp`.
667 __ Dext(tmp, tmp, 0, 32);
668 __ Bnec(tmp, expected, &exit_loop);
669 __ Move(tmp, value);
670 __ Sc(tmp, tmp_ptr);
671 __ Beqzc(tmp, &loop_head);
672 __ Bind(&exit_loop);
673
674 if (kPoisonHeapReferences) {
675 __ UnpoisonHeapReference(expected);
676 // Do not unpoison `value` if it is the same register as
677 // `expected`, which has just been unpoisoned.
678 if (value != expected) {
679 __ UnpoisonHeapReference(value);
680 }
681 }
682
683 __ Bind(&done);
684 __ Bc(GetExitLabel());
685 }
686
687 private:
688 // The location (register) of the marked object reference.
689 const Location ref_;
690 // The register containing the object holding the marked object reference field.
691 const GpuRegister obj_;
692 // The location of the offset of the marked reference field within `obj_`.
693 Location field_offset_;
694
695 const GpuRegister temp1_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS64);
698};
699
700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathMIPS64 : public SlowPathCodeMIPS64 {
702 public:
703 ReadBarrierForHeapReferenceSlowPathMIPS64(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
709 : SlowPathCodeMIPS64(instruction),
710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial object
717 // has been overwritten by (or after) the heap object reference load
718 // to be instrumented, e.g.:
719 //
720 // __ LoadFromOffset(kLoadWord, out, out, offset);
721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727 }
728
729 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
730 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 Primitive::Type type = Primitive::kPrimNot;
733 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
734 DCHECK(locations->CanCall());
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
736 DCHECK(instruction_->IsInstanceFieldGet() ||
737 instruction_->IsStaticFieldGet() ||
738 instruction_->IsArrayGet() ||
739 instruction_->IsInstanceOf() ||
740 instruction_->IsCheckCast() ||
741 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
742 << "Unexpected instruction in read barrier for heap reference slow path: "
743 << instruction_->DebugName();
744
745 __ Bind(GetEntryLabel());
746 SaveLiveRegisters(codegen, locations);
747
748 // We may have to change the index's value, but as `index_` is a
749 // constant member (like other "inputs" of this slow path),
750 // introduce a copy of it, `index`.
751 Location index = index_;
752 if (index_.IsValid()) {
753 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
754 if (instruction_->IsArrayGet()) {
755 // Compute the actual memory offset and store it in `index`.
756 GpuRegister index_reg = index_.AsRegister<GpuRegister>();
757 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
758 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
759 // We are about to change the value of `index_reg` (see the
760 // calls to art::mips64::Mips64Assembler::Sll and
761 // art::mips64::MipsAssembler::Addiu32 below), but it has
762 // not been saved by the previous call to
763 // art::SlowPathCode::SaveLiveRegisters, as it is a
764 // callee-save register --
765 // art::SlowPathCode::SaveLiveRegisters does not consider
766 // callee-save registers, as it has been designed with the
767 // assumption that callee-save registers are supposed to be
768 // handled by the called function. So, as a callee-save
769 // register, `index_reg` _would_ eventually be saved onto
770 // the stack, but it would be too late: we would have
771 // changed its value earlier. Therefore, we manually save
772 // it here into another freely available register,
773 // `free_reg`, chosen of course among the caller-save
774 // registers (as a callee-save `free_reg` register would
775 // exhibit the same problem).
776 //
777 // Note we could have requested a temporary register from
778 // the register allocator instead; but we prefer not to, as
779 // this is a slow path, and we know we can find a
780 // caller-save register that is available.
781 GpuRegister free_reg = FindAvailableCallerSaveRegister(codegen);
782 __ Move(free_reg, index_reg);
783 index_reg = free_reg;
784 index = Location::RegisterLocation(index_reg);
785 } else {
786 // The initial register stored in `index_` has already been
787 // saved in the call to art::SlowPathCode::SaveLiveRegisters
788 // (as it is not a callee-save register), so we can freely
789 // use it.
790 }
791 // Shifting the index value contained in `index_reg` by the scale
792 // factor (2) cannot overflow in practice, as the runtime is
793 // unable to allocate object arrays with a size larger than
794 // 2^26 - 1 (that is, 2^28 - 4 bytes).
795 __ Sll(index_reg, index_reg, TIMES_4);
796 static_assert(
797 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
798 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
799 __ Addiu32(index_reg, index_reg, offset_);
800 } else {
801 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
802 // intrinsics, `index_` is not shifted by a scale factor of 2
803 // (as in the case of ArrayGet), as it is actually an offset
804 // to an object field within an object.
805 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
806 DCHECK(instruction_->GetLocations()->Intrinsified());
807 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
808 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
809 << instruction_->AsInvoke()->GetIntrinsic();
810 DCHECK_EQ(offset_, 0U);
811 DCHECK(index_.IsRegister());
812 }
813 }
814
815 // We're moving two or three locations to locations that could
816 // overlap, so we need a parallel move resolver.
817 InvokeRuntimeCallingConvention calling_convention;
818 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
819 parallel_move.AddMove(ref_,
820 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
821 Primitive::kPrimNot,
822 nullptr);
823 parallel_move.AddMove(obj_,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
825 Primitive::kPrimNot,
826 nullptr);
827 if (index.IsValid()) {
828 parallel_move.AddMove(index,
829 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
830 Primitive::kPrimInt,
831 nullptr);
832 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
833 } else {
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
836 }
837 mips64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
838 instruction_,
839 instruction_->GetDexPc(),
840 this);
841 CheckEntrypointTypes<
842 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
843 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
844
845 RestoreLiveRegisters(codegen, locations);
846 __ Bc(GetExitLabel());
847 }
848
849 const char* GetDescription() const OVERRIDE {
850 return "ReadBarrierForHeapReferenceSlowPathMIPS64";
851 }
852
853 private:
854 GpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
855 size_t ref = static_cast<int>(ref_.AsRegister<GpuRegister>());
856 size_t obj = static_cast<int>(obj_.AsRegister<GpuRegister>());
857 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
858 if (i != ref &&
859 i != obj &&
860 !codegen->IsCoreCalleeSaveRegister(i) &&
861 !codegen->IsBlockedCoreRegister(i)) {
862 return static_cast<GpuRegister>(i);
863 }
864 }
865 // We shall never fail to find a free caller-save register, as
866 // there are more than two core caller-save registers on MIPS64
867 // (meaning it is possible to find one which is different from
868 // `ref` and `obj`).
869 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
870 LOG(FATAL) << "Could not find a free caller-save register";
871 UNREACHABLE();
872 }
873
874 const Location out_;
875 const Location ref_;
876 const Location obj_;
877 const uint32_t offset_;
878 // An additional location containing an index to an array.
879 // Only used for HArrayGet and the UnsafeGetObject &
880 // UnsafeGetObjectVolatile intrinsics.
881 const Location index_;
882
883 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS64);
884};
885
886// Slow path generating a read barrier for a GC root.
887class ReadBarrierForRootSlowPathMIPS64 : public SlowPathCodeMIPS64 {
888 public:
889 ReadBarrierForRootSlowPathMIPS64(HInstruction* instruction, Location out, Location root)
890 : SlowPathCodeMIPS64(instruction), out_(out), root_(root) {
891 DCHECK(kEmitCompilerReadBarrier);
892 }
893
894 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
895 LocationSummary* locations = instruction_->GetLocations();
896 Primitive::Type type = Primitive::kPrimNot;
897 GpuRegister reg_out = out_.AsRegister<GpuRegister>();
898 DCHECK(locations->CanCall());
899 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
900 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
901 << "Unexpected instruction in read barrier for GC root slow path: "
902 << instruction_->DebugName();
903
904 __ Bind(GetEntryLabel());
905 SaveLiveRegisters(codegen, locations);
906
907 InvokeRuntimeCallingConvention calling_convention;
908 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
909 mips64_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
910 root_,
911 Primitive::kPrimNot);
912 mips64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
913 instruction_,
914 instruction_->GetDexPc(),
915 this);
916 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
917 mips64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
918
919 RestoreLiveRegisters(codegen, locations);
920 __ Bc(GetExitLabel());
921 }
922
923 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS64"; }
924
925 private:
926 const Location out_;
927 const Location root_;
928
929 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS64);
930};
931
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
933 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100934 const CompilerOptions& compiler_options,
935 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936 : CodeGenerator(graph,
937 kNumberOfGpuRegisters,
938 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000939 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
941 arraysize(kCoreCalleeSaves)),
942 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
943 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100944 compiler_options,
945 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100946 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 location_builder_(graph, this),
948 instruction_visitor_(graph, this),
949 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100950 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800951 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800952 uint32_literals_(std::less<uint32_t>(),
953 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800954 uint64_literals_(std::less<uint64_t>(),
955 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800956 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800957 boot_image_string_patches_(StringReferenceValueComparator(),
958 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
959 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
960 boot_image_type_patches_(TypeReferenceValueComparator(),
961 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
962 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000963 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800964 jit_string_patches_(StringReferenceValueComparator(),
965 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
966 jit_class_patches_(TypeReferenceValueComparator(),
967 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700968 // Save RA (containing the return address) to mimic Quick.
969 AddAllocatedRegister(Location::RegisterLocation(RA));
970}
971
972#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100973// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
974#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700975#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976
977void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700978 // Ensure that we fix up branches.
979 __ FinalizeCode();
980
981 // Adjust native pc offsets in stack maps.
982 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800983 uint32_t old_position =
984 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700985 uint32_t new_position = __ GetAdjustedPosition(old_position);
986 DCHECK_GE(new_position, old_position);
987 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
988 }
989
990 // Adjust pc offsets for the disassembly information.
991 if (disasm_info_ != nullptr) {
992 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
993 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
994 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
995 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
996 it.second.start = __ GetAdjustedPosition(it.second.start);
997 it.second.end = __ GetAdjustedPosition(it.second.end);
998 }
999 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1000 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1001 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1002 }
1003 }
1004
Alexey Frunze4dda3372015-06-01 18:31:49 -07001005 CodeGenerator::Finalize(allocator);
1006}
1007
1008Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
1009 return codegen_->GetAssembler();
1010}
1011
1012void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001013 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1015}
1016
1017void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001018 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -07001019 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
1020}
1021
1022void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
1023 // Pop reg
1024 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026}
1027
1028void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
1029 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +02001030 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031 __ Sd(GpuRegister(reg), SP, 0);
1032}
1033
1034void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
1035 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
1036 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
1037 // Allocate a scratch register other than TMP, if available.
1038 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1039 // automatically unspilled when the scratch scope object is destroyed).
1040 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1041 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +02001042 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001043 __ LoadFromOffset(load_type,
1044 GpuRegister(ensure_scratch.GetRegister()),
1045 SP,
1046 index1 + stack_offset);
1047 __ LoadFromOffset(load_type,
1048 TMP,
1049 SP,
1050 index2 + stack_offset);
1051 __ StoreToOffset(store_type,
1052 GpuRegister(ensure_scratch.GetRegister()),
1053 SP,
1054 index2 + stack_offset);
1055 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
1056}
1057
1058static dwarf::Reg DWARFReg(GpuRegister reg) {
1059 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
1060}
1061
David Srbeckyba702002016-02-01 18:15:29 +00001062static dwarf::Reg DWARFReg(FpuRegister reg) {
1063 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
1064}
Alexey Frunze4dda3372015-06-01 18:31:49 -07001065
1066void CodeGeneratorMIPS64::GenerateFrameEntry() {
1067 __ Bind(&frame_entry_label_);
1068
1069 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
1070
1071 if (do_overflow_check) {
1072 __ LoadFromOffset(kLoadWord,
1073 ZERO,
1074 SP,
1075 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
1076 RecordPcInfo(nullptr, 0);
1077 }
1078
Alexey Frunze4dda3372015-06-01 18:31:49 -07001079 if (HasEmptyFrame()) {
1080 return;
1081 }
1082
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001083 // Make sure the frame size isn't unreasonably large.
1084 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips64)) {
1085 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips64) << " bytes";
1086 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001087
1088 // Spill callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001089
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001090 uint32_t ofs = GetFrameSize();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 __ IncreaseFrameSize(ofs);
1092
1093 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1094 GpuRegister reg = kCoreCalleeSaves[i];
1095 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001096 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001097 __ StoreToOffset(kStoreDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 __ cfi().RelOffset(DWARFReg(reg), ofs);
1099 }
1100 }
1101
1102 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1103 FpuRegister reg = kFpuCalleeSaves[i];
1104 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +02001105 ofs -= kMips64DoublewordSize;
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001106 __ StoreFpuToOffset(kStoreDoubleword, reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +00001107 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001108 }
1109 }
1110
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001111 // Save the current method if we need it. Note that we do not
1112 // do this in HCurrentMethod, as the instruction might have been removed
1113 // in the SSA graph.
1114 if (RequiresCurrentMethod()) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001115 __ StoreToOffset(kStoreDoubleword, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001116 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001117
1118 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1119 // Initialize should_deoptimize flag to 0.
1120 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001122}
1123
1124void CodeGeneratorMIPS64::GenerateFrameExit() {
1125 __ cfi().RememberState();
1126
Alexey Frunze4dda3372015-06-01 18:31:49 -07001127 if (!HasEmptyFrame()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001128 // Restore callee-saved registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001129
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001130 // For better instruction scheduling restore RA before other registers.
1131 uint32_t ofs = GetFrameSize();
1132 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001133 GpuRegister reg = kCoreCalleeSaves[i];
1134 if (allocated_registers_.ContainsCoreRegister(reg)) {
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001135 ofs -= kMips64DoublewordSize;
1136 __ LoadFromOffset(kLoadDoubleword, reg, SP, ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001137 __ cfi().Restore(DWARFReg(reg));
1138 }
1139 }
1140
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001141 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1142 FpuRegister reg = kFpuCalleeSaves[i];
1143 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
1144 ofs -= kMips64DoublewordSize;
1145 __ LoadFpuFromOffset(kLoadDoubleword, reg, SP, ofs);
1146 __ cfi().Restore(DWARFReg(reg));
1147 }
1148 }
1149
1150 __ DecreaseFrameSize(GetFrameSize());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 }
1152
Alexey Frunzee104d6e2017-03-21 20:16:05 -07001153 __ Jic(RA, 0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001154
1155 __ cfi().RestoreState();
1156 __ cfi().DefCFAOffset(GetFrameSize());
1157}
1158
1159void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
1160 __ Bind(GetLabelOf(block));
1161}
1162
1163void CodeGeneratorMIPS64::MoveLocation(Location destination,
1164 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +01001165 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001166 if (source.Equals(destination)) {
1167 return;
1168 }
1169
1170 // A valid move can always be inferred from the destination and source
1171 // locations. When moving from and to a register, the argument type can be
1172 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001173 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174 DCHECK_EQ(unspecified_type, false);
1175
1176 if (destination.IsRegister() || destination.IsFpuRegister()) {
1177 if (unspecified_type) {
1178 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1179 if (source.IsStackSlot() ||
1180 (src_cst != nullptr && (src_cst->IsIntConstant()
1181 || src_cst->IsFloatConstant()
1182 || src_cst->IsNullConstant()))) {
1183 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001184 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001185 } else {
1186 // If the source is a double stack slot or a 64bit constant, a 64bit
1187 // type is appropriate. Else the source is a register, and since the
1188 // type has not been specified, we chose a 64bit type to force a 64bit
1189 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001190 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 }
1192 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001193 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1194 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1196 // Move to GPR/FPR from stack
1197 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001198 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 __ LoadFpuFromOffset(load_type,
1200 destination.AsFpuRegister<FpuRegister>(),
1201 SP,
1202 source.GetStackIndex());
1203 } else {
1204 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1205 __ LoadFromOffset(load_type,
1206 destination.AsRegister<GpuRegister>(),
1207 SP,
1208 source.GetStackIndex());
1209 }
1210 } else if (source.IsConstant()) {
1211 // Move to GPR/FPR from constant
1212 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +01001213 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 gpr = destination.AsRegister<GpuRegister>();
1215 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001216 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001217 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
1218 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1219 gpr = ZERO;
1220 } else {
1221 __ LoadConst32(gpr, value);
1222 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001223 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001224 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
1225 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
1226 gpr = ZERO;
1227 } else {
1228 __ LoadConst64(gpr, value);
1229 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001230 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001231 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001233 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001234 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
1235 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 if (destination.IsRegister()) {
1238 // Move to GPR from GPR
1239 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
1240 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001241 DCHECK(destination.IsFpuRegister());
1242 if (Primitive::Is64BitType(dst_type)) {
1243 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1244 } else {
1245 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
1246 }
1247 }
1248 } else if (source.IsFpuRegister()) {
1249 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1253 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001255 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
1256 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else {
1258 DCHECK(destination.IsRegister());
1259 if (Primitive::Is64BitType(dst_type)) {
1260 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1261 } else {
1262 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
1263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001264 }
1265 }
1266 } else { // The destination is not a register. It must be a stack slot.
1267 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1268 if (source.IsRegister() || source.IsFpuRegister()) {
1269 if (unspecified_type) {
1270 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001272 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274 }
1275 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1277 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 // Move to stack from GPR/FPR
1279 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1280 if (source.IsRegister()) {
1281 __ StoreToOffset(store_type,
1282 source.AsRegister<GpuRegister>(),
1283 SP,
1284 destination.GetStackIndex());
1285 } else {
1286 __ StoreFpuToOffset(store_type,
1287 source.AsFpuRegister<FpuRegister>(),
1288 SP,
1289 destination.GetStackIndex());
1290 }
1291 } else if (source.IsConstant()) {
1292 // Move to stack from constant
1293 HConstant* src_cst = source.GetConstant();
1294 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001295 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001296 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001297 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
1298 if (value != 0) {
1299 gpr = TMP;
1300 __ LoadConst32(gpr, value);
1301 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001302 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001303 DCHECK(destination.IsDoubleStackSlot());
1304 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
1305 if (value != 0) {
1306 gpr = TMP;
1307 __ LoadConst64(gpr, value);
1308 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001309 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001310 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001311 } else {
1312 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
1313 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
1314 // Move to stack from stack
1315 if (destination.IsStackSlot()) {
1316 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1317 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1318 } else {
1319 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
1320 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
1321 }
1322 }
1323 }
1324}
1325
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001326void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001327 DCHECK(!loc1.IsConstant());
1328 DCHECK(!loc2.IsConstant());
1329
1330 if (loc1.Equals(loc2)) {
1331 return;
1332 }
1333
1334 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
1335 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
1336 bool is_fp_reg1 = loc1.IsFpuRegister();
1337 bool is_fp_reg2 = loc2.IsFpuRegister();
1338
1339 if (loc2.IsRegister() && loc1.IsRegister()) {
1340 // Swap 2 GPRs
1341 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
1342 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
1343 __ Move(TMP, r2);
1344 __ Move(r2, r1);
1345 __ Move(r1, TMP);
1346 } else if (is_fp_reg2 && is_fp_reg1) {
1347 // Swap 2 FPRs
1348 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
1349 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001350 if (type == Primitive::kPrimFloat) {
1351 __ MovS(FTMP, r1);
1352 __ MovS(r1, r2);
1353 __ MovS(r2, FTMP);
1354 } else {
1355 DCHECK_EQ(type, Primitive::kPrimDouble);
1356 __ MovD(FTMP, r1);
1357 __ MovD(r1, r2);
1358 __ MovD(r2, FTMP);
1359 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001360 } else if (is_slot1 != is_slot2) {
1361 // Swap GPR/FPR and stack slot
1362 Location reg_loc = is_slot1 ? loc2 : loc1;
1363 Location mem_loc = is_slot1 ? loc1 : loc2;
1364 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
1365 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
1366 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
1367 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
1368 if (reg_loc.IsFpuRegister()) {
1369 __ StoreFpuToOffset(store_type,
1370 reg_loc.AsFpuRegister<FpuRegister>(),
1371 SP,
1372 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001373 if (mem_loc.IsStackSlot()) {
1374 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1375 } else {
1376 DCHECK(mem_loc.IsDoubleStackSlot());
1377 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
1378 }
1379 } else {
1380 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
1381 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
1382 }
1383 } else if (is_slot1 && is_slot2) {
1384 move_resolver_.Exchange(loc1.GetStackIndex(),
1385 loc2.GetStackIndex(),
1386 loc1.IsDoubleStackSlot());
1387 } else {
1388 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
1389 }
1390}
1391
Calin Juravle175dc732015-08-25 15:42:32 +01001392void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
1393 DCHECK(location.IsRegister());
1394 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
1395}
1396
Calin Juravlee460d1d2015-09-29 04:52:17 +01001397void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1398 if (location.IsRegister()) {
1399 locations->AddTemp(location);
1400 } else {
1401 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1402 }
1403}
1404
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001405void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
1406 GpuRegister value,
1407 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001408 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001409 GpuRegister card = AT;
1410 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001411 if (value_can_be_null) {
1412 __ Beqzc(value, &done);
1413 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 __ LoadFromOffset(kLoadDoubleword,
1415 card,
1416 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001417 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
1419 __ Daddu(temp, card, temp);
1420 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001421 if (value_can_be_null) {
1422 __ Bind(&done);
1423 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001424}
1425
Alexey Frunze19f6c692016-11-30 19:19:55 -08001426template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1427inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
1428 const ArenaDeque<PcRelativePatchInfo>& infos,
1429 ArenaVector<LinkerPatch>* linker_patches) {
1430 for (const PcRelativePatchInfo& info : infos) {
1431 const DexFile& dex_file = info.target_dex_file;
1432 size_t offset_or_index = info.offset_or_index;
1433 DCHECK(info.pc_rel_label.IsBound());
1434 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
1435 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
1436 }
1437}
1438
1439void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1440 DCHECK(linker_patches->empty());
1441 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -08001442 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001443 pc_relative_string_patches_.size() +
1444 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00001445 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -08001446 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00001447 boot_image_type_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -08001448 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001449 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1450 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001451 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00001452 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001453 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1454 linker_patches);
1455 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001456 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1457 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001458 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1459 linker_patches);
1460 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001461 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1462 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001463 for (const auto& entry : boot_image_string_patches_) {
1464 const StringReference& target_string = entry.first;
1465 Literal* literal = entry.second;
1466 DCHECK(literal->GetLabel()->IsBound());
1467 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1468 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1469 target_string.dex_file,
1470 target_string.string_index.index_));
1471 }
1472 for (const auto& entry : boot_image_type_patches_) {
1473 const TypeReference& target_type = entry.first;
1474 Literal* literal = entry.second;
1475 DCHECK(literal->GetLabel()->IsBound());
1476 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1477 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1478 target_type.dex_file,
1479 target_type.type_index.index_));
1480 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00001481 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -08001482}
1483
1484CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001485 const DexFile& dex_file, dex::StringIndex string_index) {
1486 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001487}
1488
1489CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
1490 const DexFile& dex_file, dex::TypeIndex type_index) {
1491 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -08001492}
1493
Vladimir Marko1998cd02017-01-13 13:02:58 +00001494CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
1495 const DexFile& dex_file, dex::TypeIndex type_index) {
1496 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1497}
1498
Alexey Frunze19f6c692016-11-30 19:19:55 -08001499CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1500 const DexFile& dex_file, uint32_t element_offset) {
1501 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1502}
1503
Alexey Frunze19f6c692016-11-30 19:19:55 -08001504CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1505 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1506 patches->emplace_back(dex_file, offset_or_index);
1507 return &patches->back();
1508}
1509
Alexey Frunzef63f5692016-12-13 17:43:11 -08001510Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1511 return map->GetOrCreate(
1512 value,
1513 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1514}
1515
Alexey Frunze19f6c692016-11-30 19:19:55 -08001516Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1517 return uint64_literals_.GetOrCreate(
1518 value,
1519 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1520}
1521
1522Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1523 MethodToLiteralMap* map) {
1524 return map->GetOrCreate(
1525 target_method,
1526 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1527}
1528
Alexey Frunzef63f5692016-12-13 17:43:11 -08001529Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1530 dex::StringIndex string_index) {
1531 return boot_image_string_patches_.GetOrCreate(
1532 StringReference(&dex_file, string_index),
1533 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1534}
1535
1536Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1537 dex::TypeIndex type_index) {
1538 return boot_image_type_patches_.GetOrCreate(
1539 TypeReference(&dex_file, type_index),
1540 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1541}
1542
1543Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001544 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001545}
1546
Alexey Frunze19f6c692016-11-30 19:19:55 -08001547void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1548 GpuRegister out) {
1549 __ Bind(&info->pc_rel_label);
1550 // Add the high half of a 32-bit offset to PC.
1551 __ Auipc(out, /* placeholder */ 0x1234);
1552 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001553 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001554}
1555
Alexey Frunze627c1a02017-01-30 19:28:14 -08001556Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1557 dex::StringIndex string_index,
1558 Handle<mirror::String> handle) {
1559 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1560 reinterpret_cast64<uint64_t>(handle.GetReference()));
1561 return jit_string_patches_.GetOrCreate(
1562 StringReference(&dex_file, string_index),
1563 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1564}
1565
1566Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1567 dex::TypeIndex type_index,
1568 Handle<mirror::Class> handle) {
1569 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1570 reinterpret_cast64<uint64_t>(handle.GetReference()));
1571 return jit_class_patches_.GetOrCreate(
1572 TypeReference(&dex_file, type_index),
1573 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1574}
1575
1576void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1577 const uint8_t* roots_data,
1578 const Literal* literal,
1579 uint64_t index_in_table) const {
1580 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1581 uintptr_t address =
1582 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1583 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1584}
1585
1586void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1587 for (const auto& entry : jit_string_patches_) {
1588 const auto& it = jit_string_roots_.find(entry.first);
1589 DCHECK(it != jit_string_roots_.end());
1590 PatchJitRootUse(code, roots_data, entry.second, it->second);
1591 }
1592 for (const auto& entry : jit_class_patches_) {
1593 const auto& it = jit_class_roots_.find(entry.first);
1594 DCHECK(it != jit_class_roots_.end());
1595 PatchJitRootUse(code, roots_data, entry.second, it->second);
1596 }
1597}
1598
David Brazdil58282f42016-01-14 12:45:10 +00001599void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001600 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1601 blocked_core_registers_[ZERO] = true;
1602 blocked_core_registers_[K0] = true;
1603 blocked_core_registers_[K1] = true;
1604 blocked_core_registers_[GP] = true;
1605 blocked_core_registers_[SP] = true;
1606 blocked_core_registers_[RA] = true;
1607
Lazar Trsicd9672662015-09-03 17:33:01 +02001608 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1609 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001610 blocked_core_registers_[AT] = true;
1611 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001612 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001613 blocked_fpu_registers_[FTMP] = true;
1614
1615 // Reserve suspend and thread registers.
1616 blocked_core_registers_[S0] = true;
1617 blocked_core_registers_[TR] = true;
1618
1619 // Reserve T9 for function calls
1620 blocked_core_registers_[T9] = true;
1621
Goran Jakovljevic782be112016-06-21 12:39:04 +02001622 if (GetGraph()->IsDebuggable()) {
1623 // Stubs do not save callee-save floating point registers. If the graph
1624 // is debuggable, we need to deal with these registers differently. For
1625 // now, just block them.
1626 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1627 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1628 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001629 }
1630}
1631
Alexey Frunze4dda3372015-06-01 18:31:49 -07001632size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1633 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001634 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001635}
1636
1637size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1638 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001639 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001640}
1641
1642size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1643 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001644 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001645}
1646
1647size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1648 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001649 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001650}
1651
1652void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001653 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001654}
1655
1656void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001657 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001658}
1659
Calin Juravle175dc732015-08-25 15:42:32 +01001660void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001661 HInstruction* instruction,
1662 uint32_t dex_pc,
1663 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001664 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001665 GenerateInvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Serban Constantinescufc734082016-07-19 17:18:07 +01001666 if (EntrypointRequiresStackMap(entrypoint)) {
1667 RecordPcInfo(instruction, dex_pc, slow_path);
1668 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001669}
1670
Alexey Frunze15958152017-02-09 19:08:30 -08001671void CodeGeneratorMIPS64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1672 HInstruction* instruction,
1673 SlowPathCode* slow_path) {
1674 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1675 GenerateInvokeRuntime(entry_point_offset);
1676}
1677
1678void CodeGeneratorMIPS64::GenerateInvokeRuntime(int32_t entry_point_offset) {
1679 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1680 __ Jalr(T9);
1681 __ Nop();
1682}
1683
Alexey Frunze4dda3372015-06-01 18:31:49 -07001684void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1685 GpuRegister class_reg) {
1686 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1687 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1688 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
Alexey Frunze15958152017-02-09 19:08:30 -08001689 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1690 __ Sync(0);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001691 __ Bind(slow_path->GetExitLabel());
1692}
1693
1694void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1695 __ Sync(0); // only stype 0 is supported
1696}
1697
1698void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1699 HBasicBlock* successor) {
1700 SuspendCheckSlowPathMIPS64* slow_path =
1701 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1702 codegen_->AddSlowPath(slow_path);
1703
1704 __ LoadFromOffset(kLoadUnsignedHalfword,
1705 TMP,
1706 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001707 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001708 if (successor == nullptr) {
1709 __ Bnezc(TMP, slow_path->GetEntryLabel());
1710 __ Bind(slow_path->GetReturnLabel());
1711 } else {
1712 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001713 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001714 // slow_path will return to GetLabelOf(successor).
1715 }
1716}
1717
1718InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1719 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001720 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001721 assembler_(codegen->GetAssembler()),
1722 codegen_(codegen) {}
1723
1724void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1725 DCHECK_EQ(instruction->InputCount(), 2U);
1726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1727 Primitive::Type type = instruction->GetResultType();
1728 switch (type) {
1729 case Primitive::kPrimInt:
1730 case Primitive::kPrimLong: {
1731 locations->SetInAt(0, Location::RequiresRegister());
1732 HInstruction* right = instruction->InputAt(1);
1733 bool can_use_imm = false;
1734 if (right->IsConstant()) {
1735 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1736 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1737 can_use_imm = IsUint<16>(imm);
1738 } else if (instruction->IsAdd()) {
1739 can_use_imm = IsInt<16>(imm);
1740 } else {
1741 DCHECK(instruction->IsSub());
1742 can_use_imm = IsInt<16>(-imm);
1743 }
1744 }
1745 if (can_use_imm)
1746 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1747 else
1748 locations->SetInAt(1, Location::RequiresRegister());
1749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1750 }
1751 break;
1752
1753 case Primitive::kPrimFloat:
1754 case Primitive::kPrimDouble:
1755 locations->SetInAt(0, Location::RequiresFpuRegister());
1756 locations->SetInAt(1, Location::RequiresFpuRegister());
1757 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1758 break;
1759
1760 default:
1761 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1762 }
1763}
1764
1765void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1766 Primitive::Type type = instruction->GetType();
1767 LocationSummary* locations = instruction->GetLocations();
1768
1769 switch (type) {
1770 case Primitive::kPrimInt:
1771 case Primitive::kPrimLong: {
1772 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1773 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1774 Location rhs_location = locations->InAt(1);
1775
1776 GpuRegister rhs_reg = ZERO;
1777 int64_t rhs_imm = 0;
1778 bool use_imm = rhs_location.IsConstant();
1779 if (use_imm) {
1780 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1781 } else {
1782 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1783 }
1784
1785 if (instruction->IsAnd()) {
1786 if (use_imm)
1787 __ Andi(dst, lhs, rhs_imm);
1788 else
1789 __ And(dst, lhs, rhs_reg);
1790 } else if (instruction->IsOr()) {
1791 if (use_imm)
1792 __ Ori(dst, lhs, rhs_imm);
1793 else
1794 __ Or(dst, lhs, rhs_reg);
1795 } else if (instruction->IsXor()) {
1796 if (use_imm)
1797 __ Xori(dst, lhs, rhs_imm);
1798 else
1799 __ Xor(dst, lhs, rhs_reg);
1800 } else if (instruction->IsAdd()) {
1801 if (type == Primitive::kPrimInt) {
1802 if (use_imm)
1803 __ Addiu(dst, lhs, rhs_imm);
1804 else
1805 __ Addu(dst, lhs, rhs_reg);
1806 } else {
1807 if (use_imm)
1808 __ Daddiu(dst, lhs, rhs_imm);
1809 else
1810 __ Daddu(dst, lhs, rhs_reg);
1811 }
1812 } else {
1813 DCHECK(instruction->IsSub());
1814 if (type == Primitive::kPrimInt) {
1815 if (use_imm)
1816 __ Addiu(dst, lhs, -rhs_imm);
1817 else
1818 __ Subu(dst, lhs, rhs_reg);
1819 } else {
1820 if (use_imm)
1821 __ Daddiu(dst, lhs, -rhs_imm);
1822 else
1823 __ Dsubu(dst, lhs, rhs_reg);
1824 }
1825 }
1826 break;
1827 }
1828 case Primitive::kPrimFloat:
1829 case Primitive::kPrimDouble: {
1830 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1831 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1832 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1833 if (instruction->IsAdd()) {
1834 if (type == Primitive::kPrimFloat)
1835 __ AddS(dst, lhs, rhs);
1836 else
1837 __ AddD(dst, lhs, rhs);
1838 } else if (instruction->IsSub()) {
1839 if (type == Primitive::kPrimFloat)
1840 __ SubS(dst, lhs, rhs);
1841 else
1842 __ SubD(dst, lhs, rhs);
1843 } else {
1844 LOG(FATAL) << "Unexpected floating-point binary operation";
1845 }
1846 break;
1847 }
1848 default:
1849 LOG(FATAL) << "Unexpected binary operation type " << type;
1850 }
1851}
1852
1853void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001854 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855
1856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1857 Primitive::Type type = instr->GetResultType();
1858 switch (type) {
1859 case Primitive::kPrimInt:
1860 case Primitive::kPrimLong: {
1861 locations->SetInAt(0, Location::RequiresRegister());
1862 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001863 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001864 break;
1865 }
1866 default:
1867 LOG(FATAL) << "Unexpected shift type " << type;
1868 }
1869}
1870
1871void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001872 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873 LocationSummary* locations = instr->GetLocations();
1874 Primitive::Type type = instr->GetType();
1875
1876 switch (type) {
1877 case Primitive::kPrimInt:
1878 case Primitive::kPrimLong: {
1879 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1880 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1881 Location rhs_location = locations->InAt(1);
1882
1883 GpuRegister rhs_reg = ZERO;
1884 int64_t rhs_imm = 0;
1885 bool use_imm = rhs_location.IsConstant();
1886 if (use_imm) {
1887 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1888 } else {
1889 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1890 }
1891
1892 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001893 uint32_t shift_value = rhs_imm &
1894 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001895
Alexey Frunze92d90602015-12-18 18:16:36 -08001896 if (shift_value == 0) {
1897 if (dst != lhs) {
1898 __ Move(dst, lhs);
1899 }
1900 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001901 if (instr->IsShl()) {
1902 __ Sll(dst, lhs, shift_value);
1903 } else if (instr->IsShr()) {
1904 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001905 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001906 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001907 } else {
1908 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001909 }
1910 } else {
1911 if (shift_value < 32) {
1912 if (instr->IsShl()) {
1913 __ Dsll(dst, lhs, shift_value);
1914 } else if (instr->IsShr()) {
1915 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001916 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001917 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001918 } else {
1919 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001920 }
1921 } else {
1922 shift_value -= 32;
1923 if (instr->IsShl()) {
1924 __ Dsll32(dst, lhs, shift_value);
1925 } else if (instr->IsShr()) {
1926 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001927 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001928 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001929 } else {
1930 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001931 }
1932 }
1933 }
1934 } else {
1935 if (type == Primitive::kPrimInt) {
1936 if (instr->IsShl()) {
1937 __ Sllv(dst, lhs, rhs_reg);
1938 } else if (instr->IsShr()) {
1939 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001940 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001941 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001942 } else {
1943 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001944 }
1945 } else {
1946 if (instr->IsShl()) {
1947 __ Dsllv(dst, lhs, rhs_reg);
1948 } else if (instr->IsShr()) {
1949 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001950 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001951 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001952 } else {
1953 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001954 }
1955 }
1956 }
1957 break;
1958 }
1959 default:
1960 LOG(FATAL) << "Unexpected shift operation type " << type;
1961 }
1962}
1963
1964void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1965 HandleBinaryOp(instruction);
1966}
1967
1968void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1969 HandleBinaryOp(instruction);
1970}
1971
1972void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1973 HandleBinaryOp(instruction);
1974}
1975
1976void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1977 HandleBinaryOp(instruction);
1978}
1979
1980void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08001981 Primitive::Type type = instruction->GetType();
1982 bool object_array_get_with_read_barrier =
1983 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001984 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08001985 new (GetGraph()->GetArena()) LocationSummary(instruction,
1986 object_array_get_with_read_barrier
1987 ? LocationSummary::kCallOnSlowPath
1988 : LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08001991 if (Primitive::IsFloatingPointType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1993 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08001994 // The output overlaps in the case of an object array get with
1995 // read barriers enabled: we do not want the move to overwrite the
1996 // array's location, as we need it to emit the read barrier.
1997 locations->SetOut(Location::RequiresRegister(),
1998 object_array_get_with_read_barrier
1999 ? Location::kOutputOverlap
2000 : Location::kNoOutputOverlap);
2001 }
2002 // We need a temporary register for the read barrier marking slow
2003 // path in CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier.
2004 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2005 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002006 }
2007}
2008
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002009static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
2010 auto null_checker = [codegen, instruction]() {
2011 codegen->MaybeRecordImplicitNullCheck(instruction);
2012 };
2013 return null_checker;
2014}
2015
Alexey Frunze4dda3372015-06-01 18:31:49 -07002016void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
2017 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002018 Location obj_loc = locations->InAt(0);
2019 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
2020 Location out_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002022 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002023 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002024
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002025 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002026 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2027 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002028 switch (type) {
2029 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002030 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002031 if (index.IsConstant()) {
2032 size_t offset =
2033 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002034 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 } else {
2036 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002037 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 }
2039 break;
2040 }
2041
2042 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002043 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002044 if (index.IsConstant()) {
2045 size_t offset =
2046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002047 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002048 } else {
2049 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002050 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002051 }
2052 break;
2053 }
2054
2055 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002056 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002057 if (index.IsConstant()) {
2058 size_t offset =
2059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002060 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 } else {
2062 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
2063 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002064 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065 }
2066 break;
2067 }
2068
2069 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002070 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002071 if (maybe_compressed_char_at) {
2072 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002073 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002074 __ Dext(TMP, TMP, 0, 1);
2075 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2076 "Expecting 0=compressed, 1=uncompressed");
2077 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002078 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002079 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2080 if (maybe_compressed_char_at) {
2081 Mips64Label uncompressed_load, done;
2082 __ Bnezc(TMP, &uncompressed_load);
2083 __ LoadFromOffset(kLoadUnsignedByte,
2084 out,
2085 obj,
2086 data_offset + (const_index << TIMES_1));
2087 __ Bc(&done);
2088 __ Bind(&uncompressed_load);
2089 __ LoadFromOffset(kLoadUnsignedHalfword,
2090 out,
2091 obj,
2092 data_offset + (const_index << TIMES_2));
2093 __ Bind(&done);
2094 } else {
2095 __ LoadFromOffset(kLoadUnsignedHalfword,
2096 out,
2097 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002098 data_offset + (const_index << TIMES_2),
2099 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002100 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002101 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002102 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2103 if (maybe_compressed_char_at) {
2104 Mips64Label uncompressed_load, done;
2105 __ Bnezc(TMP, &uncompressed_load);
2106 __ Daddu(TMP, obj, index_reg);
2107 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2108 __ Bc(&done);
2109 __ Bind(&uncompressed_load);
2110 __ Dsll(TMP, index_reg, TIMES_2);
2111 __ Daddu(TMP, obj, TMP);
2112 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2113 __ Bind(&done);
2114 } else {
2115 __ Dsll(TMP, index_reg, TIMES_2);
2116 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002117 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002118 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 }
2120 break;
2121 }
2122
Alexey Frunze15958152017-02-09 19:08:30 -08002123 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002124 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002125 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2127 if (index.IsConstant()) {
2128 size_t offset =
2129 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002130 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 } else {
2132 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
2133 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002134 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002135 }
2136 break;
2137 }
2138
Alexey Frunze15958152017-02-09 19:08:30 -08002139 case Primitive::kPrimNot: {
2140 static_assert(
2141 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2142 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2143 // /* HeapReference<Object> */ out =
2144 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2145 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2146 Location temp = locations->GetTemp(0);
2147 // Note that a potential implicit null check is handled in this
2148 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2149 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2150 out_loc,
2151 obj,
2152 data_offset,
2153 index,
2154 temp,
2155 /* needs_null_check */ true);
2156 } else {
2157 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2158 if (index.IsConstant()) {
2159 size_t offset =
2160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2161 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2162 // If read barriers are enabled, emit read barriers other than
2163 // Baker's using a slow path (and also unpoison the loaded
2164 // reference, if heap poisoning is enabled).
2165 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2166 } else {
2167 __ Sll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
2168 __ Addu(TMP, obj, TMP);
2169 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2170 // If read barriers are enabled, emit read barriers other than
2171 // Baker's using a slow path (and also unpoison the loaded
2172 // reference, if heap poisoning is enabled).
2173 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2174 out_loc,
2175 out_loc,
2176 obj_loc,
2177 data_offset,
2178 index);
2179 }
2180 }
2181 break;
2182 }
2183
Alexey Frunze4dda3372015-06-01 18:31:49 -07002184 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002185 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002186 if (index.IsConstant()) {
2187 size_t offset =
2188 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002189 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 } else {
2191 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
2192 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002193 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002194 }
2195 break;
2196 }
2197
2198 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002199 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002200 if (index.IsConstant()) {
2201 size_t offset =
2202 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002203 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 } else {
2205 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
2206 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002207 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002208 }
2209 break;
2210 }
2211
2212 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002213 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002214 if (index.IsConstant()) {
2215 size_t offset =
2216 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002217 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002218 } else {
2219 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
2220 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002221 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002222 }
2223 break;
2224 }
2225
2226 case Primitive::kPrimVoid:
2227 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2228 UNREACHABLE();
2229 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002230}
2231
2232void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2234 locations->SetInAt(0, Location::RequiresRegister());
2235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2236}
2237
2238void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2239 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002240 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2242 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2243 __ LoadFromOffset(kLoadWord, out, obj, offset);
2244 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002245 // Mask out compression flag from String's array length.
2246 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2247 __ Srl(out, out, 1u);
2248 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002249}
2250
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002251Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2252 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2253 ? Location::ConstantLocation(instruction->AsConstant())
2254 : Location::RequiresRegister();
2255}
2256
2257Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2258 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2259 // We can store a non-zero float or double constant without first loading it into the FPU,
2260 // but we should only prefer this if the constant has a single use.
2261 if (instruction->IsConstant() &&
2262 (instruction->AsConstant()->IsZeroBitPattern() ||
2263 instruction->GetUses().HasExactlyOneElement())) {
2264 return Location::ConstantLocation(instruction->AsConstant());
2265 // Otherwise fall through and require an FPU register for the constant.
2266 }
2267 return Location::RequiresFpuRegister();
2268}
2269
Alexey Frunze4dda3372015-06-01 18:31:49 -07002270void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002271 Primitive::Type value_type = instruction->GetComponentType();
2272
2273 bool needs_write_barrier =
2274 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2275 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2276
Alexey Frunze4dda3372015-06-01 18:31:49 -07002277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2278 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002279 may_need_runtime_call_for_type_check ?
2280 LocationSummary::kCallOnSlowPath :
2281 LocationSummary::kNoCall);
2282
2283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2285 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2286 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002287 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002288 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2289 }
2290 if (needs_write_barrier) {
2291 // Temporary register for the write barrier.
2292 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002293 }
2294}
2295
2296void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2297 LocationSummary* locations = instruction->GetLocations();
2298 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2299 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002300 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002301 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002302 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002303 bool needs_write_barrier =
2304 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002305 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002306 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002307
2308 switch (value_type) {
2309 case Primitive::kPrimBoolean:
2310 case Primitive::kPrimByte: {
2311 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002312 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002313 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002314 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002315 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2316 }
2317 if (value_location.IsConstant()) {
2318 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2319 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2320 } else {
2321 GpuRegister value = value_location.AsRegister<GpuRegister>();
2322 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002323 }
2324 break;
2325 }
2326
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimChar: {
2329 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002330 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002331 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002332 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002333 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_2);
2334 __ Daddu(base_reg, obj, base_reg);
2335 }
2336 if (value_location.IsConstant()) {
2337 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2338 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2339 } else {
2340 GpuRegister value = value_location.AsRegister<GpuRegister>();
2341 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002342 }
2343 break;
2344 }
2345
Alexey Frunze15958152017-02-09 19:08:30 -08002346 case Primitive::kPrimInt: {
2347 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2348 if (index.IsConstant()) {
2349 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2350 } else {
2351 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
2352 __ Daddu(base_reg, obj, base_reg);
2353 }
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 {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002371 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
2372 __ Daddu(base_reg, obj, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002373 }
Alexey Frunze15958152017-02-09 19:08:30 -08002374 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2375 DCHECK_EQ(value, 0);
2376 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2377 DCHECK(!needs_write_barrier);
2378 DCHECK(!may_need_runtime_call_for_type_check);
2379 break;
2380 }
2381
2382 DCHECK(needs_write_barrier);
2383 GpuRegister value = value_location.AsRegister<GpuRegister>();
2384 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2385 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2386 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2387 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2388 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2389 Mips64Label done;
2390 SlowPathCodeMIPS64* slow_path = nullptr;
2391
2392 if (may_need_runtime_call_for_type_check) {
2393 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2394 codegen_->AddSlowPath(slow_path);
2395 if (instruction->GetValueCanBeNull()) {
2396 Mips64Label non_zero;
2397 __ Bnezc(value, &non_zero);
2398 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2399 if (index.IsConstant()) {
2400 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002401 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002402 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
2403 __ Daddu(base_reg, obj, base_reg);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002404 }
Alexey Frunze15958152017-02-09 19:08:30 -08002405 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2406 __ Bc(&done);
2407 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002408 }
Alexey Frunze15958152017-02-09 19:08:30 -08002409
2410 // Note that when read barriers are enabled, the type checks
2411 // are performed without read barriers. This is fine, even in
2412 // the case where a class object is in the from-space after
2413 // the flip, as a comparison involving such a type would not
2414 // produce a false positive; it may of course produce a false
2415 // negative, in which case we would take the ArraySet slow
2416 // path.
2417
2418 // /* HeapReference<Class> */ temp1 = obj->klass_
2419 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2420 __ MaybeUnpoisonHeapReference(temp1);
2421
2422 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2423 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2424 // /* HeapReference<Class> */ temp2 = value->klass_
2425 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2426 // If heap poisoning is enabled, no need to unpoison `temp1`
2427 // nor `temp2`, as we are comparing two poisoned references.
2428
2429 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2430 Mips64Label do_put;
2431 __ Beqc(temp1, temp2, &do_put);
2432 // If heap poisoning is enabled, the `temp1` reference has
2433 // not been unpoisoned yet; unpoison it now.
2434 __ MaybeUnpoisonHeapReference(temp1);
2435
2436 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2437 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2438 // If heap poisoning is enabled, no need to unpoison
2439 // `temp1`, as we are comparing against null below.
2440 __ Bnezc(temp1, slow_path->GetEntryLabel());
2441 __ Bind(&do_put);
2442 } else {
2443 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2444 }
2445 }
2446
2447 GpuRegister source = value;
2448 if (kPoisonHeapReferences) {
2449 // Note that in the case where `value` is a null reference,
2450 // we do not enter this block, as a null reference does not
2451 // need poisoning.
2452 __ Move(temp1, value);
2453 __ PoisonHeapReference(temp1);
2454 source = temp1;
2455 }
2456
2457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2458 if (index.IsConstant()) {
2459 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002460 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002461 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
2462 __ Daddu(base_reg, obj, base_reg);
2463 }
2464 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2465
2466 if (!may_need_runtime_call_for_type_check) {
2467 codegen_->MaybeRecordImplicitNullCheck(instruction);
2468 }
2469
2470 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2471
2472 if (done.IsLinked()) {
2473 __ Bind(&done);
2474 }
2475
2476 if (slow_path != nullptr) {
2477 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002478 }
2479 break;
2480 }
2481
2482 case Primitive::kPrimLong: {
2483 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002484 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002485 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002486 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002487 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_8);
2488 __ Daddu(base_reg, obj, base_reg);
2489 }
2490 if (value_location.IsConstant()) {
2491 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2492 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2493 } else {
2494 GpuRegister value = value_location.AsRegister<GpuRegister>();
2495 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002496 }
2497 break;
2498 }
2499
2500 case Primitive::kPrimFloat: {
2501 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002502 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002503 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002504 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002505 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
2506 __ Daddu(base_reg, obj, base_reg);
2507 }
2508 if (value_location.IsConstant()) {
2509 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2510 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2511 } else {
2512 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2513 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002514 }
2515 break;
2516 }
2517
2518 case Primitive::kPrimDouble: {
2519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002520 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002521 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002522 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002523 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_8);
2524 __ Daddu(base_reg, obj, base_reg);
2525 }
2526 if (value_location.IsConstant()) {
2527 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2528 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2529 } else {
2530 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2531 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002532 }
2533 break;
2534 }
2535
2536 case Primitive::kPrimVoid:
2537 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2538 UNREACHABLE();
2539 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002540}
2541
2542void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002543 RegisterSet caller_saves = RegisterSet::Empty();
2544 InvokeRuntimeCallingConvention calling_convention;
2545 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2546 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2547 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002548 locations->SetInAt(0, Location::RequiresRegister());
2549 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002550}
2551
2552void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2553 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002554 BoundsCheckSlowPathMIPS64* slow_path =
2555 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556 codegen_->AddSlowPath(slow_path);
2557
2558 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2559 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2560
2561 // length is limited by the maximum positive signed 32-bit integer.
2562 // Unsigned comparison of length and index checks for index < 0
2563 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002564 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002565}
2566
Alexey Frunze15958152017-02-09 19:08:30 -08002567// Temp is used for read barrier.
2568static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2569 if (kEmitCompilerReadBarrier &&
2570 (kUseBakerReadBarrier ||
2571 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2572 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2573 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2574 return 1;
2575 }
2576 return 0;
2577}
2578
2579// Extra temp is used for read barrier.
2580static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2581 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2582}
2583
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002585 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2586 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2587
2588 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2589 switch (type_check_kind) {
2590 case TypeCheckKind::kExactCheck:
2591 case TypeCheckKind::kAbstractClassCheck:
2592 case TypeCheckKind::kClassHierarchyCheck:
2593 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002594 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002595 ? LocationSummary::kCallOnSlowPath
2596 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2597 break;
2598 case TypeCheckKind::kArrayCheck:
2599 case TypeCheckKind::kUnresolvedCheck:
2600 case TypeCheckKind::kInterfaceCheck:
2601 call_kind = LocationSummary::kCallOnSlowPath;
2602 break;
2603 }
2604
2605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002608 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002609}
2610
2611void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002612 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002613 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002614 Location obj_loc = locations->InAt(0);
2615 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002617 Location temp_loc = locations->GetTemp(0);
2618 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2619 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2620 DCHECK_LE(num_temps, 2u);
2621 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002622 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2623 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2624 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2625 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2626 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2627 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2628 const uint32_t object_array_data_offset =
2629 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2630 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002631
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002632 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2633 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2634 // read barriers is done for performance and code size reasons.
2635 bool is_type_check_slow_path_fatal = false;
2636 if (!kEmitCompilerReadBarrier) {
2637 is_type_check_slow_path_fatal =
2638 (type_check_kind == TypeCheckKind::kExactCheck ||
2639 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2640 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2641 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2642 !instruction->CanThrowIntoCatchBlock();
2643 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002644 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002645 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2646 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647 codegen_->AddSlowPath(slow_path);
2648
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002649 // Avoid this check if we know `obj` is not null.
2650 if (instruction->MustDoNullCheck()) {
2651 __ Beqzc(obj, &done);
2652 }
2653
2654 switch (type_check_kind) {
2655 case TypeCheckKind::kExactCheck:
2656 case TypeCheckKind::kArrayCheck: {
2657 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002658 GenerateReferenceLoadTwoRegisters(instruction,
2659 temp_loc,
2660 obj_loc,
2661 class_offset,
2662 maybe_temp2_loc,
2663 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002664 // Jump to slow path for throwing the exception or doing a
2665 // more involved array check.
2666 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2667 break;
2668 }
2669
2670 case TypeCheckKind::kAbstractClassCheck: {
2671 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002672 GenerateReferenceLoadTwoRegisters(instruction,
2673 temp_loc,
2674 obj_loc,
2675 class_offset,
2676 maybe_temp2_loc,
2677 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002678 // If the class is abstract, we eagerly fetch the super class of the
2679 // object to avoid doing a comparison we know will fail.
2680 Mips64Label loop;
2681 __ Bind(&loop);
2682 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002683 GenerateReferenceLoadOneRegister(instruction,
2684 temp_loc,
2685 super_offset,
2686 maybe_temp2_loc,
2687 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002688 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2689 // exception.
2690 __ Beqzc(temp, slow_path->GetEntryLabel());
2691 // Otherwise, compare the classes.
2692 __ Bnec(temp, cls, &loop);
2693 break;
2694 }
2695
2696 case TypeCheckKind::kClassHierarchyCheck: {
2697 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002698 GenerateReferenceLoadTwoRegisters(instruction,
2699 temp_loc,
2700 obj_loc,
2701 class_offset,
2702 maybe_temp2_loc,
2703 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002704 // Walk over the class hierarchy to find a match.
2705 Mips64Label loop;
2706 __ Bind(&loop);
2707 __ Beqc(temp, cls, &done);
2708 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002709 GenerateReferenceLoadOneRegister(instruction,
2710 temp_loc,
2711 super_offset,
2712 maybe_temp2_loc,
2713 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002714 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2715 // exception. Otherwise, jump to the beginning of the loop.
2716 __ Bnezc(temp, &loop);
2717 __ Bc(slow_path->GetEntryLabel());
2718 break;
2719 }
2720
2721 case TypeCheckKind::kArrayObjectCheck: {
2722 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002723 GenerateReferenceLoadTwoRegisters(instruction,
2724 temp_loc,
2725 obj_loc,
2726 class_offset,
2727 maybe_temp2_loc,
2728 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002729 // Do an exact check.
2730 __ Beqc(temp, cls, &done);
2731 // Otherwise, we need to check that the object's class is a non-primitive array.
2732 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002733 GenerateReferenceLoadOneRegister(instruction,
2734 temp_loc,
2735 component_offset,
2736 maybe_temp2_loc,
2737 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002738 // If the component type is null, jump to the slow path to throw the exception.
2739 __ Beqzc(temp, slow_path->GetEntryLabel());
2740 // Otherwise, the object is indeed an array, further check that this component
2741 // type is not a primitive type.
2742 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2743 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2744 __ Bnezc(temp, slow_path->GetEntryLabel());
2745 break;
2746 }
2747
2748 case TypeCheckKind::kUnresolvedCheck:
2749 // We always go into the type check slow path for the unresolved check case.
2750 // We cannot directly call the CheckCast runtime entry point
2751 // without resorting to a type checking slow path here (i.e. by
2752 // calling InvokeRuntime directly), as it would require to
2753 // assign fixed registers for the inputs of this HInstanceOf
2754 // instruction (following the runtime calling convention), which
2755 // might be cluttered by the potential first read barrier
2756 // emission at the beginning of this method.
2757 __ Bc(slow_path->GetEntryLabel());
2758 break;
2759
2760 case TypeCheckKind::kInterfaceCheck: {
2761 // Avoid read barriers to improve performance of the fast path. We can not get false
2762 // positives by doing this.
2763 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002764 GenerateReferenceLoadTwoRegisters(instruction,
2765 temp_loc,
2766 obj_loc,
2767 class_offset,
2768 maybe_temp2_loc,
2769 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002770 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002771 GenerateReferenceLoadTwoRegisters(instruction,
2772 temp_loc,
2773 temp_loc,
2774 iftable_offset,
2775 maybe_temp2_loc,
2776 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002777 // Iftable is never null.
2778 __ Lw(TMP, temp, array_length_offset);
2779 // Loop through the iftable and check if any class matches.
2780 Mips64Label loop;
2781 __ Bind(&loop);
2782 __ Beqzc(TMP, slow_path->GetEntryLabel());
2783 __ Lwu(AT, temp, object_array_data_offset);
2784 __ MaybeUnpoisonHeapReference(AT);
2785 // Go to next interface.
2786 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2787 __ Addiu(TMP, TMP, -2);
2788 // Compare the classes and continue the loop if they do not match.
2789 __ Bnec(AT, cls, &loop);
2790 break;
2791 }
2792 }
2793
2794 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002795 __ Bind(slow_path->GetExitLabel());
2796}
2797
2798void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2799 LocationSummary* locations =
2800 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2801 locations->SetInAt(0, Location::RequiresRegister());
2802 if (check->HasUses()) {
2803 locations->SetOut(Location::SameAsFirstInput());
2804 }
2805}
2806
2807void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2808 // We assume the class is not null.
2809 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2810 check->GetLoadClass(),
2811 check,
2812 check->GetDexPc(),
2813 true);
2814 codegen_->AddSlowPath(slow_path);
2815 GenerateClassInitializationCheck(slow_path,
2816 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2817}
2818
2819void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2820 Primitive::Type in_type = compare->InputAt(0)->GetType();
2821
Alexey Frunze299a9392015-12-08 16:08:02 -08002822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002823
2824 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002825 case Primitive::kPrimBoolean:
2826 case Primitive::kPrimByte:
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002829 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002830 case Primitive::kPrimLong:
2831 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002832 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2834 break;
2835
2836 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002837 case Primitive::kPrimDouble:
2838 locations->SetInAt(0, Location::RequiresFpuRegister());
2839 locations->SetInAt(1, Location::RequiresFpuRegister());
2840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002841 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002842
2843 default:
2844 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2845 }
2846}
2847
2848void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2849 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002850 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002851 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2852
2853 // 0 if: left == right
2854 // 1 if: left > right
2855 // -1 if: left < right
2856 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002857 case Primitive::kPrimBoolean:
2858 case Primitive::kPrimByte:
2859 case Primitive::kPrimShort:
2860 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002861 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002862 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002864 Location rhs_location = locations->InAt(1);
2865 bool use_imm = rhs_location.IsConstant();
2866 GpuRegister rhs = ZERO;
2867 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002868 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002869 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2870 if (value != 0) {
2871 rhs = AT;
2872 __ LoadConst64(rhs, value);
2873 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002874 } else {
2875 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2876 if (value != 0) {
2877 rhs = AT;
2878 __ LoadConst32(rhs, value);
2879 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002880 }
2881 } else {
2882 rhs = rhs_location.AsRegister<GpuRegister>();
2883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002884 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002885 __ Slt(res, rhs, lhs);
2886 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002887 break;
2888 }
2889
Alexey Frunze299a9392015-12-08 16:08:02 -08002890 case Primitive::kPrimFloat: {
2891 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2892 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2893 Mips64Label done;
2894 __ CmpEqS(FTMP, lhs, rhs);
2895 __ LoadConst32(res, 0);
2896 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002897 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002898 __ CmpLtS(FTMP, lhs, rhs);
2899 __ LoadConst32(res, -1);
2900 __ Bc1nez(FTMP, &done);
2901 __ LoadConst32(res, 1);
2902 } else {
2903 __ CmpLtS(FTMP, rhs, lhs);
2904 __ LoadConst32(res, 1);
2905 __ Bc1nez(FTMP, &done);
2906 __ LoadConst32(res, -1);
2907 }
2908 __ Bind(&done);
2909 break;
2910 }
2911
Alexey Frunze4dda3372015-06-01 18:31:49 -07002912 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002913 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2914 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2915 Mips64Label done;
2916 __ CmpEqD(FTMP, lhs, rhs);
2917 __ LoadConst32(res, 0);
2918 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002919 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002920 __ CmpLtD(FTMP, lhs, rhs);
2921 __ LoadConst32(res, -1);
2922 __ Bc1nez(FTMP, &done);
2923 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002925 __ CmpLtD(FTMP, rhs, lhs);
2926 __ LoadConst32(res, 1);
2927 __ Bc1nez(FTMP, &done);
2928 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002929 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002930 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002931 break;
2932 }
2933
2934 default:
2935 LOG(FATAL) << "Unimplemented compare type " << in_type;
2936 }
2937}
2938
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002939void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002941 switch (instruction->InputAt(0)->GetType()) {
2942 default:
2943 case Primitive::kPrimLong:
2944 locations->SetInAt(0, Location::RequiresRegister());
2945 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2946 break;
2947
2948 case Primitive::kPrimFloat:
2949 case Primitive::kPrimDouble:
2950 locations->SetInAt(0, Location::RequiresFpuRegister());
2951 locations->SetInAt(1, Location::RequiresFpuRegister());
2952 break;
2953 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002954 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2956 }
2957}
2958
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002959void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002960 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961 return;
2962 }
2963
Alexey Frunze299a9392015-12-08 16:08:02 -08002964 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002965 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002966 switch (type) {
2967 default:
2968 // Integer case.
2969 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2970 return;
2971 case Primitive::kPrimLong:
2972 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2973 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002974 case Primitive::kPrimFloat:
2975 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002976 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2977 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002978 }
2979}
2980
Alexey Frunzec857c742015-09-23 15:12:39 -07002981void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2982 DCHECK(instruction->IsDiv() || instruction->IsRem());
2983 Primitive::Type type = instruction->GetResultType();
2984
2985 LocationSummary* locations = instruction->GetLocations();
2986 Location second = locations->InAt(1);
2987 DCHECK(second.IsConstant());
2988
2989 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2990 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2991 int64_t imm = Int64FromConstant(second.GetConstant());
2992 DCHECK(imm == 1 || imm == -1);
2993
2994 if (instruction->IsRem()) {
2995 __ Move(out, ZERO);
2996 } else {
2997 if (imm == -1) {
2998 if (type == Primitive::kPrimInt) {
2999 __ Subu(out, ZERO, dividend);
3000 } else {
3001 DCHECK_EQ(type, Primitive::kPrimLong);
3002 __ Dsubu(out, ZERO, dividend);
3003 }
3004 } else if (out != dividend) {
3005 __ Move(out, dividend);
3006 }
3007 }
3008}
3009
3010void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3011 DCHECK(instruction->IsDiv() || instruction->IsRem());
3012 Primitive::Type type = instruction->GetResultType();
3013
3014 LocationSummary* locations = instruction->GetLocations();
3015 Location second = locations->InAt(1);
3016 DCHECK(second.IsConstant());
3017
3018 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3019 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3020 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003021 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003022 int ctz_imm = CTZ(abs_imm);
3023
3024 if (instruction->IsDiv()) {
3025 if (type == Primitive::kPrimInt) {
3026 if (ctz_imm == 1) {
3027 // Fast path for division by +/-2, which is very common.
3028 __ Srl(TMP, dividend, 31);
3029 } else {
3030 __ Sra(TMP, dividend, 31);
3031 __ Srl(TMP, TMP, 32 - ctz_imm);
3032 }
3033 __ Addu(out, dividend, TMP);
3034 __ Sra(out, out, ctz_imm);
3035 if (imm < 0) {
3036 __ Subu(out, ZERO, out);
3037 }
3038 } else {
3039 DCHECK_EQ(type, Primitive::kPrimLong);
3040 if (ctz_imm == 1) {
3041 // Fast path for division by +/-2, which is very common.
3042 __ Dsrl32(TMP, dividend, 31);
3043 } else {
3044 __ Dsra32(TMP, dividend, 31);
3045 if (ctz_imm > 32) {
3046 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3047 } else {
3048 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3049 }
3050 }
3051 __ Daddu(out, dividend, TMP);
3052 if (ctz_imm < 32) {
3053 __ Dsra(out, out, ctz_imm);
3054 } else {
3055 __ Dsra32(out, out, ctz_imm - 32);
3056 }
3057 if (imm < 0) {
3058 __ Dsubu(out, ZERO, out);
3059 }
3060 }
3061 } else {
3062 if (type == Primitive::kPrimInt) {
3063 if (ctz_imm == 1) {
3064 // Fast path for modulo +/-2, which is very common.
3065 __ Sra(TMP, dividend, 31);
3066 __ Subu(out, dividend, TMP);
3067 __ Andi(out, out, 1);
3068 __ Addu(out, out, TMP);
3069 } else {
3070 __ Sra(TMP, dividend, 31);
3071 __ Srl(TMP, TMP, 32 - ctz_imm);
3072 __ Addu(out, dividend, TMP);
3073 if (IsUint<16>(abs_imm - 1)) {
3074 __ Andi(out, out, abs_imm - 1);
3075 } else {
3076 __ Sll(out, out, 32 - ctz_imm);
3077 __ Srl(out, out, 32 - ctz_imm);
3078 }
3079 __ Subu(out, out, TMP);
3080 }
3081 } else {
3082 DCHECK_EQ(type, Primitive::kPrimLong);
3083 if (ctz_imm == 1) {
3084 // Fast path for modulo +/-2, which is very common.
3085 __ Dsra32(TMP, dividend, 31);
3086 __ Dsubu(out, dividend, TMP);
3087 __ Andi(out, out, 1);
3088 __ Daddu(out, out, TMP);
3089 } else {
3090 __ Dsra32(TMP, dividend, 31);
3091 if (ctz_imm > 32) {
3092 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3093 } else {
3094 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3095 }
3096 __ Daddu(out, dividend, TMP);
3097 if (IsUint<16>(abs_imm - 1)) {
3098 __ Andi(out, out, abs_imm - 1);
3099 } else {
3100 if (ctz_imm > 32) {
3101 __ Dsll(out, out, 64 - ctz_imm);
3102 __ Dsrl(out, out, 64 - ctz_imm);
3103 } else {
3104 __ Dsll32(out, out, 32 - ctz_imm);
3105 __ Dsrl32(out, out, 32 - ctz_imm);
3106 }
3107 }
3108 __ Dsubu(out, out, TMP);
3109 }
3110 }
3111 }
3112}
3113
3114void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3115 DCHECK(instruction->IsDiv() || instruction->IsRem());
3116
3117 LocationSummary* locations = instruction->GetLocations();
3118 Location second = locations->InAt(1);
3119 DCHECK(second.IsConstant());
3120
3121 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3122 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3123 int64_t imm = Int64FromConstant(second.GetConstant());
3124
3125 Primitive::Type type = instruction->GetResultType();
3126 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3127
3128 int64_t magic;
3129 int shift;
3130 CalculateMagicAndShiftForDivRem(imm,
3131 (type == Primitive::kPrimLong),
3132 &magic,
3133 &shift);
3134
3135 if (type == Primitive::kPrimInt) {
3136 __ LoadConst32(TMP, magic);
3137 __ MuhR6(TMP, dividend, TMP);
3138
3139 if (imm > 0 && magic < 0) {
3140 __ Addu(TMP, TMP, dividend);
3141 } else if (imm < 0 && magic > 0) {
3142 __ Subu(TMP, TMP, dividend);
3143 }
3144
3145 if (shift != 0) {
3146 __ Sra(TMP, TMP, shift);
3147 }
3148
3149 if (instruction->IsDiv()) {
3150 __ Sra(out, TMP, 31);
3151 __ Subu(out, TMP, out);
3152 } else {
3153 __ Sra(AT, TMP, 31);
3154 __ Subu(AT, TMP, AT);
3155 __ LoadConst32(TMP, imm);
3156 __ MulR6(TMP, AT, TMP);
3157 __ Subu(out, dividend, TMP);
3158 }
3159 } else {
3160 __ LoadConst64(TMP, magic);
3161 __ Dmuh(TMP, dividend, TMP);
3162
3163 if (imm > 0 && magic < 0) {
3164 __ Daddu(TMP, TMP, dividend);
3165 } else if (imm < 0 && magic > 0) {
3166 __ Dsubu(TMP, TMP, dividend);
3167 }
3168
3169 if (shift >= 32) {
3170 __ Dsra32(TMP, TMP, shift - 32);
3171 } else if (shift > 0) {
3172 __ Dsra(TMP, TMP, shift);
3173 }
3174
3175 if (instruction->IsDiv()) {
3176 __ Dsra32(out, TMP, 31);
3177 __ Dsubu(out, TMP, out);
3178 } else {
3179 __ Dsra32(AT, TMP, 31);
3180 __ Dsubu(AT, TMP, AT);
3181 __ LoadConst64(TMP, imm);
3182 __ Dmul(TMP, AT, TMP);
3183 __ Dsubu(out, dividend, TMP);
3184 }
3185 }
3186}
3187
3188void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3189 DCHECK(instruction->IsDiv() || instruction->IsRem());
3190 Primitive::Type type = instruction->GetResultType();
3191 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3192
3193 LocationSummary* locations = instruction->GetLocations();
3194 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3195 Location second = locations->InAt(1);
3196
3197 if (second.IsConstant()) {
3198 int64_t imm = Int64FromConstant(second.GetConstant());
3199 if (imm == 0) {
3200 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3201 } else if (imm == 1 || imm == -1) {
3202 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003203 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003204 DivRemByPowerOfTwo(instruction);
3205 } else {
3206 DCHECK(imm <= -2 || imm >= 2);
3207 GenerateDivRemWithAnyConstant(instruction);
3208 }
3209 } else {
3210 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3211 GpuRegister divisor = second.AsRegister<GpuRegister>();
3212 if (instruction->IsDiv()) {
3213 if (type == Primitive::kPrimInt)
3214 __ DivR6(out, dividend, divisor);
3215 else
3216 __ Ddiv(out, dividend, divisor);
3217 } else {
3218 if (type == Primitive::kPrimInt)
3219 __ ModR6(out, dividend, divisor);
3220 else
3221 __ Dmod(out, dividend, divisor);
3222 }
3223 }
3224}
3225
Alexey Frunze4dda3372015-06-01 18:31:49 -07003226void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3227 LocationSummary* locations =
3228 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3229 switch (div->GetResultType()) {
3230 case Primitive::kPrimInt:
3231 case Primitive::kPrimLong:
3232 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003233 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3235 break;
3236
3237 case Primitive::kPrimFloat:
3238 case Primitive::kPrimDouble:
3239 locations->SetInAt(0, Location::RequiresFpuRegister());
3240 locations->SetInAt(1, Location::RequiresFpuRegister());
3241 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3242 break;
3243
3244 default:
3245 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3246 }
3247}
3248
3249void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3250 Primitive::Type type = instruction->GetType();
3251 LocationSummary* locations = instruction->GetLocations();
3252
3253 switch (type) {
3254 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003255 case Primitive::kPrimLong:
3256 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003257 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003258 case Primitive::kPrimFloat:
3259 case Primitive::kPrimDouble: {
3260 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3261 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3262 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3263 if (type == Primitive::kPrimFloat)
3264 __ DivS(dst, lhs, rhs);
3265 else
3266 __ DivD(dst, lhs, rhs);
3267 break;
3268 }
3269 default:
3270 LOG(FATAL) << "Unexpected div type " << type;
3271 }
3272}
3273
3274void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003275 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003276 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003277}
3278
3279void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3280 SlowPathCodeMIPS64* slow_path =
3281 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3282 codegen_->AddSlowPath(slow_path);
3283 Location value = instruction->GetLocations()->InAt(0);
3284
3285 Primitive::Type type = instruction->GetType();
3286
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003287 if (!Primitive::IsIntegralType(type)) {
3288 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003289 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003290 }
3291
3292 if (value.IsConstant()) {
3293 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3294 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003295 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003296 } else {
3297 // A division by a non-null constant is valid. We don't need to perform
3298 // any check, so simply fall through.
3299 }
3300 } else {
3301 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3302 }
3303}
3304
3305void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3306 LocationSummary* locations =
3307 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3308 locations->SetOut(Location::ConstantLocation(constant));
3309}
3310
3311void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3312 // Will be generated at use site.
3313}
3314
3315void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3316 exit->SetLocations(nullptr);
3317}
3318
3319void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3320}
3321
3322void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3323 LocationSummary* locations =
3324 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3325 locations->SetOut(Location::ConstantLocation(constant));
3326}
3327
3328void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3329 // Will be generated at use site.
3330}
3331
David Brazdilfc6a86a2015-06-26 10:33:45 +00003332void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003333 DCHECK(!successor->IsExitBlock());
3334 HBasicBlock* block = got->GetBlock();
3335 HInstruction* previous = got->GetPrevious();
3336 HLoopInformation* info = block->GetLoopInformation();
3337
3338 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3339 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3340 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3341 return;
3342 }
3343 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3344 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3345 }
3346 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003347 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003348 }
3349}
3350
David Brazdilfc6a86a2015-06-26 10:33:45 +00003351void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3352 got->SetLocations(nullptr);
3353}
3354
3355void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3356 HandleGoto(got, got->GetSuccessor());
3357}
3358
3359void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3360 try_boundary->SetLocations(nullptr);
3361}
3362
3363void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3364 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3365 if (!successor->IsExitBlock()) {
3366 HandleGoto(try_boundary, successor);
3367 }
3368}
3369
Alexey Frunze299a9392015-12-08 16:08:02 -08003370void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3371 bool is64bit,
3372 LocationSummary* locations) {
3373 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3374 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3375 Location rhs_location = locations->InAt(1);
3376 GpuRegister rhs_reg = ZERO;
3377 int64_t rhs_imm = 0;
3378 bool use_imm = rhs_location.IsConstant();
3379 if (use_imm) {
3380 if (is64bit) {
3381 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3382 } else {
3383 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3384 }
3385 } else {
3386 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3387 }
3388 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3389
3390 switch (cond) {
3391 case kCondEQ:
3392 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003393 if (use_imm && IsInt<16>(-rhs_imm)) {
3394 if (rhs_imm == 0) {
3395 if (cond == kCondEQ) {
3396 __ Sltiu(dst, lhs, 1);
3397 } else {
3398 __ Sltu(dst, ZERO, lhs);
3399 }
3400 } else {
3401 if (is64bit) {
3402 __ Daddiu(dst, lhs, -rhs_imm);
3403 } else {
3404 __ Addiu(dst, lhs, -rhs_imm);
3405 }
3406 if (cond == kCondEQ) {
3407 __ Sltiu(dst, dst, 1);
3408 } else {
3409 __ Sltu(dst, ZERO, dst);
3410 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003411 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003412 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003413 if (use_imm && IsUint<16>(rhs_imm)) {
3414 __ Xori(dst, lhs, rhs_imm);
3415 } else {
3416 if (use_imm) {
3417 rhs_reg = TMP;
3418 __ LoadConst64(rhs_reg, rhs_imm);
3419 }
3420 __ Xor(dst, lhs, rhs_reg);
3421 }
3422 if (cond == kCondEQ) {
3423 __ Sltiu(dst, dst, 1);
3424 } else {
3425 __ Sltu(dst, ZERO, dst);
3426 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003427 }
3428 break;
3429
3430 case kCondLT:
3431 case kCondGE:
3432 if (use_imm && IsInt<16>(rhs_imm)) {
3433 __ Slti(dst, lhs, rhs_imm);
3434 } else {
3435 if (use_imm) {
3436 rhs_reg = TMP;
3437 __ LoadConst64(rhs_reg, rhs_imm);
3438 }
3439 __ Slt(dst, lhs, rhs_reg);
3440 }
3441 if (cond == kCondGE) {
3442 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3443 // only the slt instruction but no sge.
3444 __ Xori(dst, dst, 1);
3445 }
3446 break;
3447
3448 case kCondLE:
3449 case kCondGT:
3450 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3451 // Simulate lhs <= rhs via lhs < rhs + 1.
3452 __ Slti(dst, lhs, rhs_imm_plus_one);
3453 if (cond == kCondGT) {
3454 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3455 // only the slti instruction but no sgti.
3456 __ Xori(dst, dst, 1);
3457 }
3458 } else {
3459 if (use_imm) {
3460 rhs_reg = TMP;
3461 __ LoadConst64(rhs_reg, rhs_imm);
3462 }
3463 __ Slt(dst, rhs_reg, lhs);
3464 if (cond == kCondLE) {
3465 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3466 // only the slt instruction but no sle.
3467 __ Xori(dst, dst, 1);
3468 }
3469 }
3470 break;
3471
3472 case kCondB:
3473 case kCondAE:
3474 if (use_imm && IsInt<16>(rhs_imm)) {
3475 // Sltiu sign-extends its 16-bit immediate operand before
3476 // the comparison and thus lets us compare directly with
3477 // unsigned values in the ranges [0, 0x7fff] and
3478 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3479 __ Sltiu(dst, lhs, rhs_imm);
3480 } else {
3481 if (use_imm) {
3482 rhs_reg = TMP;
3483 __ LoadConst64(rhs_reg, rhs_imm);
3484 }
3485 __ Sltu(dst, lhs, rhs_reg);
3486 }
3487 if (cond == kCondAE) {
3488 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3489 // only the sltu instruction but no sgeu.
3490 __ Xori(dst, dst, 1);
3491 }
3492 break;
3493
3494 case kCondBE:
3495 case kCondA:
3496 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3497 // Simulate lhs <= rhs via lhs < rhs + 1.
3498 // Note that this only works if rhs + 1 does not overflow
3499 // to 0, hence the check above.
3500 // Sltiu sign-extends its 16-bit immediate operand before
3501 // the comparison and thus lets us compare directly with
3502 // unsigned values in the ranges [0, 0x7fff] and
3503 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3504 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3505 if (cond == kCondA) {
3506 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3507 // only the sltiu instruction but no sgtiu.
3508 __ Xori(dst, dst, 1);
3509 }
3510 } else {
3511 if (use_imm) {
3512 rhs_reg = TMP;
3513 __ LoadConst64(rhs_reg, rhs_imm);
3514 }
3515 __ Sltu(dst, rhs_reg, lhs);
3516 if (cond == kCondBE) {
3517 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3518 // only the sltu instruction but no sleu.
3519 __ Xori(dst, dst, 1);
3520 }
3521 }
3522 break;
3523 }
3524}
3525
3526void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3527 bool is64bit,
3528 LocationSummary* locations,
3529 Mips64Label* label) {
3530 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3531 Location rhs_location = locations->InAt(1);
3532 GpuRegister rhs_reg = ZERO;
3533 int64_t rhs_imm = 0;
3534 bool use_imm = rhs_location.IsConstant();
3535 if (use_imm) {
3536 if (is64bit) {
3537 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3538 } else {
3539 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3540 }
3541 } else {
3542 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3543 }
3544
3545 if (use_imm && rhs_imm == 0) {
3546 switch (cond) {
3547 case kCondEQ:
3548 case kCondBE: // <= 0 if zero
3549 __ Beqzc(lhs, label);
3550 break;
3551 case kCondNE:
3552 case kCondA: // > 0 if non-zero
3553 __ Bnezc(lhs, label);
3554 break;
3555 case kCondLT:
3556 __ Bltzc(lhs, label);
3557 break;
3558 case kCondGE:
3559 __ Bgezc(lhs, label);
3560 break;
3561 case kCondLE:
3562 __ Blezc(lhs, label);
3563 break;
3564 case kCondGT:
3565 __ Bgtzc(lhs, label);
3566 break;
3567 case kCondB: // always false
3568 break;
3569 case kCondAE: // always true
3570 __ Bc(label);
3571 break;
3572 }
3573 } else {
3574 if (use_imm) {
3575 rhs_reg = TMP;
3576 __ LoadConst64(rhs_reg, rhs_imm);
3577 }
3578 switch (cond) {
3579 case kCondEQ:
3580 __ Beqc(lhs, rhs_reg, label);
3581 break;
3582 case kCondNE:
3583 __ Bnec(lhs, rhs_reg, label);
3584 break;
3585 case kCondLT:
3586 __ Bltc(lhs, rhs_reg, label);
3587 break;
3588 case kCondGE:
3589 __ Bgec(lhs, rhs_reg, label);
3590 break;
3591 case kCondLE:
3592 __ Bgec(rhs_reg, lhs, label);
3593 break;
3594 case kCondGT:
3595 __ Bltc(rhs_reg, lhs, label);
3596 break;
3597 case kCondB:
3598 __ Bltuc(lhs, rhs_reg, label);
3599 break;
3600 case kCondAE:
3601 __ Bgeuc(lhs, rhs_reg, label);
3602 break;
3603 case kCondBE:
3604 __ Bgeuc(rhs_reg, lhs, label);
3605 break;
3606 case kCondA:
3607 __ Bltuc(rhs_reg, lhs, label);
3608 break;
3609 }
3610 }
3611}
3612
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003613void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3614 bool gt_bias,
3615 Primitive::Type type,
3616 LocationSummary* locations) {
3617 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3618 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3619 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3620 if (type == Primitive::kPrimFloat) {
3621 switch (cond) {
3622 case kCondEQ:
3623 __ CmpEqS(FTMP, lhs, rhs);
3624 __ Mfc1(dst, FTMP);
3625 __ Andi(dst, dst, 1);
3626 break;
3627 case kCondNE:
3628 __ CmpEqS(FTMP, lhs, rhs);
3629 __ Mfc1(dst, FTMP);
3630 __ Addiu(dst, dst, 1);
3631 break;
3632 case kCondLT:
3633 if (gt_bias) {
3634 __ CmpLtS(FTMP, lhs, rhs);
3635 } else {
3636 __ CmpUltS(FTMP, lhs, rhs);
3637 }
3638 __ Mfc1(dst, FTMP);
3639 __ Andi(dst, dst, 1);
3640 break;
3641 case kCondLE:
3642 if (gt_bias) {
3643 __ CmpLeS(FTMP, lhs, rhs);
3644 } else {
3645 __ CmpUleS(FTMP, lhs, rhs);
3646 }
3647 __ Mfc1(dst, FTMP);
3648 __ Andi(dst, dst, 1);
3649 break;
3650 case kCondGT:
3651 if (gt_bias) {
3652 __ CmpUltS(FTMP, rhs, lhs);
3653 } else {
3654 __ CmpLtS(FTMP, rhs, lhs);
3655 }
3656 __ Mfc1(dst, FTMP);
3657 __ Andi(dst, dst, 1);
3658 break;
3659 case kCondGE:
3660 if (gt_bias) {
3661 __ CmpUleS(FTMP, rhs, lhs);
3662 } else {
3663 __ CmpLeS(FTMP, rhs, lhs);
3664 }
3665 __ Mfc1(dst, FTMP);
3666 __ Andi(dst, dst, 1);
3667 break;
3668 default:
3669 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3670 UNREACHABLE();
3671 }
3672 } else {
3673 DCHECK_EQ(type, Primitive::kPrimDouble);
3674 switch (cond) {
3675 case kCondEQ:
3676 __ CmpEqD(FTMP, lhs, rhs);
3677 __ Mfc1(dst, FTMP);
3678 __ Andi(dst, dst, 1);
3679 break;
3680 case kCondNE:
3681 __ CmpEqD(FTMP, lhs, rhs);
3682 __ Mfc1(dst, FTMP);
3683 __ Addiu(dst, dst, 1);
3684 break;
3685 case kCondLT:
3686 if (gt_bias) {
3687 __ CmpLtD(FTMP, lhs, rhs);
3688 } else {
3689 __ CmpUltD(FTMP, lhs, rhs);
3690 }
3691 __ Mfc1(dst, FTMP);
3692 __ Andi(dst, dst, 1);
3693 break;
3694 case kCondLE:
3695 if (gt_bias) {
3696 __ CmpLeD(FTMP, lhs, rhs);
3697 } else {
3698 __ CmpUleD(FTMP, lhs, rhs);
3699 }
3700 __ Mfc1(dst, FTMP);
3701 __ Andi(dst, dst, 1);
3702 break;
3703 case kCondGT:
3704 if (gt_bias) {
3705 __ CmpUltD(FTMP, rhs, lhs);
3706 } else {
3707 __ CmpLtD(FTMP, rhs, lhs);
3708 }
3709 __ Mfc1(dst, FTMP);
3710 __ Andi(dst, dst, 1);
3711 break;
3712 case kCondGE:
3713 if (gt_bias) {
3714 __ CmpUleD(FTMP, rhs, lhs);
3715 } else {
3716 __ CmpLeD(FTMP, rhs, lhs);
3717 }
3718 __ Mfc1(dst, FTMP);
3719 __ Andi(dst, dst, 1);
3720 break;
3721 default:
3722 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3723 UNREACHABLE();
3724 }
3725 }
3726}
3727
Alexey Frunze299a9392015-12-08 16:08:02 -08003728void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3729 bool gt_bias,
3730 Primitive::Type type,
3731 LocationSummary* locations,
3732 Mips64Label* label) {
3733 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3734 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3735 if (type == Primitive::kPrimFloat) {
3736 switch (cond) {
3737 case kCondEQ:
3738 __ CmpEqS(FTMP, lhs, rhs);
3739 __ Bc1nez(FTMP, label);
3740 break;
3741 case kCondNE:
3742 __ CmpEqS(FTMP, lhs, rhs);
3743 __ Bc1eqz(FTMP, label);
3744 break;
3745 case kCondLT:
3746 if (gt_bias) {
3747 __ CmpLtS(FTMP, lhs, rhs);
3748 } else {
3749 __ CmpUltS(FTMP, lhs, rhs);
3750 }
3751 __ Bc1nez(FTMP, label);
3752 break;
3753 case kCondLE:
3754 if (gt_bias) {
3755 __ CmpLeS(FTMP, lhs, rhs);
3756 } else {
3757 __ CmpUleS(FTMP, lhs, rhs);
3758 }
3759 __ Bc1nez(FTMP, label);
3760 break;
3761 case kCondGT:
3762 if (gt_bias) {
3763 __ CmpUltS(FTMP, rhs, lhs);
3764 } else {
3765 __ CmpLtS(FTMP, rhs, lhs);
3766 }
3767 __ Bc1nez(FTMP, label);
3768 break;
3769 case kCondGE:
3770 if (gt_bias) {
3771 __ CmpUleS(FTMP, rhs, lhs);
3772 } else {
3773 __ CmpLeS(FTMP, rhs, lhs);
3774 }
3775 __ Bc1nez(FTMP, label);
3776 break;
3777 default:
3778 LOG(FATAL) << "Unexpected non-floating-point condition";
3779 }
3780 } else {
3781 DCHECK_EQ(type, Primitive::kPrimDouble);
3782 switch (cond) {
3783 case kCondEQ:
3784 __ CmpEqD(FTMP, lhs, rhs);
3785 __ Bc1nez(FTMP, label);
3786 break;
3787 case kCondNE:
3788 __ CmpEqD(FTMP, lhs, rhs);
3789 __ Bc1eqz(FTMP, label);
3790 break;
3791 case kCondLT:
3792 if (gt_bias) {
3793 __ CmpLtD(FTMP, lhs, rhs);
3794 } else {
3795 __ CmpUltD(FTMP, lhs, rhs);
3796 }
3797 __ Bc1nez(FTMP, label);
3798 break;
3799 case kCondLE:
3800 if (gt_bias) {
3801 __ CmpLeD(FTMP, lhs, rhs);
3802 } else {
3803 __ CmpUleD(FTMP, lhs, rhs);
3804 }
3805 __ Bc1nez(FTMP, label);
3806 break;
3807 case kCondGT:
3808 if (gt_bias) {
3809 __ CmpUltD(FTMP, rhs, lhs);
3810 } else {
3811 __ CmpLtD(FTMP, rhs, lhs);
3812 }
3813 __ Bc1nez(FTMP, label);
3814 break;
3815 case kCondGE:
3816 if (gt_bias) {
3817 __ CmpUleD(FTMP, rhs, lhs);
3818 } else {
3819 __ CmpLeD(FTMP, rhs, lhs);
3820 }
3821 __ Bc1nez(FTMP, label);
3822 break;
3823 default:
3824 LOG(FATAL) << "Unexpected non-floating-point condition";
3825 }
3826 }
3827}
3828
Alexey Frunze4dda3372015-06-01 18:31:49 -07003829void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003830 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003831 Mips64Label* true_target,
3832 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003833 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003834
David Brazdil0debae72015-11-12 18:37:00 +00003835 if (true_target == nullptr && false_target == nullptr) {
3836 // Nothing to do. The code always falls through.
3837 return;
3838 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003839 // Constant condition, statically compared against "true" (integer value 1).
3840 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003841 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003842 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003843 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003844 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003845 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003846 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003847 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003848 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003849 }
David Brazdil0debae72015-11-12 18:37:00 +00003850 return;
3851 }
3852
3853 // The following code generates these patterns:
3854 // (1) true_target == nullptr && false_target != nullptr
3855 // - opposite condition true => branch to false_target
3856 // (2) true_target != nullptr && false_target == nullptr
3857 // - condition true => branch to true_target
3858 // (3) true_target != nullptr && false_target != nullptr
3859 // - condition true => branch to true_target
3860 // - branch to false_target
3861 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003862 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003863 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003864 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003865 if (true_target == nullptr) {
3866 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3867 } else {
3868 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3869 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003870 } else {
3871 // The condition instruction has not been materialized, use its inputs as
3872 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003873 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003874 Primitive::Type type = condition->InputAt(0)->GetType();
3875 LocationSummary* locations = cond->GetLocations();
3876 IfCondition if_cond = condition->GetCondition();
3877 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003878
David Brazdil0debae72015-11-12 18:37:00 +00003879 if (true_target == nullptr) {
3880 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003881 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003882 }
3883
Alexey Frunze299a9392015-12-08 16:08:02 -08003884 switch (type) {
3885 default:
3886 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3887 break;
3888 case Primitive::kPrimLong:
3889 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3890 break;
3891 case Primitive::kPrimFloat:
3892 case Primitive::kPrimDouble:
3893 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3894 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003895 }
3896 }
David Brazdil0debae72015-11-12 18:37:00 +00003897
3898 // If neither branch falls through (case 3), the conditional branch to `true_target`
3899 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3900 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003901 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003902 }
3903}
3904
3905void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3906 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003907 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003908 locations->SetInAt(0, Location::RequiresRegister());
3909 }
3910}
3911
3912void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003913 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3914 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003915 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003916 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003917 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003918 nullptr : codegen_->GetLabelOf(false_successor);
3919 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003920}
3921
3922void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3923 LocationSummary* locations = new (GetGraph()->GetArena())
3924 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003925 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003926 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003927 locations->SetInAt(0, Location::RequiresRegister());
3928 }
3929}
3930
3931void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003932 SlowPathCodeMIPS64* slow_path =
3933 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003934 GenerateTestAndBranch(deoptimize,
3935 /* condition_input_index */ 0,
3936 slow_path->GetEntryLabel(),
3937 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003938}
3939
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003940void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3941 LocationSummary* locations = new (GetGraph()->GetArena())
3942 LocationSummary(flag, LocationSummary::kNoCall);
3943 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003944}
3945
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003946void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3947 __ LoadFromOffset(kLoadWord,
3948 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3949 SP,
3950 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003951}
3952
David Brazdil74eb1b22015-12-14 11:44:01 +00003953void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3955 if (Primitive::IsFloatingPointType(select->GetType())) {
3956 locations->SetInAt(0, Location::RequiresFpuRegister());
3957 locations->SetInAt(1, Location::RequiresFpuRegister());
3958 } else {
3959 locations->SetInAt(0, Location::RequiresRegister());
3960 locations->SetInAt(1, Location::RequiresRegister());
3961 }
3962 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3963 locations->SetInAt(2, Location::RequiresRegister());
3964 }
3965 locations->SetOut(Location::SameAsFirstInput());
3966}
3967
3968void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3969 LocationSummary* locations = select->GetLocations();
3970 Mips64Label false_target;
3971 GenerateTestAndBranch(select,
3972 /* condition_input_index */ 2,
3973 /* true_target */ nullptr,
3974 &false_target);
3975 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3976 __ Bind(&false_target);
3977}
3978
David Srbecky0cf44932015-12-09 14:09:59 +00003979void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3980 new (GetGraph()->GetArena()) LocationSummary(info);
3981}
3982
David Srbeckyd28f4a02016-03-14 17:14:24 +00003983void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3984 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003985}
3986
3987void CodeGeneratorMIPS64::GenerateNop() {
3988 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003989}
3990
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003992 const FieldInfo& field_info) {
3993 Primitive::Type field_type = field_info.GetFieldType();
3994 bool object_field_get_with_read_barrier =
3995 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3997 instruction,
3998 object_field_get_with_read_barrier
3999 ? LocationSummary::kCallOnSlowPath
4000 : LocationSummary::kNoCall);
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>();
4450 __ Dsll(TMP, index_reg, scale_factor);
4451 __ Daddu(TMP, obj, TMP);
4452 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4453 }
4454 } else {
4455 // /* HeapReference<Object> */ ref = *(obj + offset)
4456 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4457 }
4458
4459 // Object* ref = ref_addr->AsMirrorPtr()
4460 __ MaybeUnpoisonHeapReference(ref_reg);
4461
4462 // Slow path marking the object `ref` when it is gray.
4463 SlowPathCodeMIPS64* slow_path;
4464 if (always_update_field) {
4465 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4466 // of the form `obj + field_offset`, where `obj` is a register and
4467 // `field_offset` is a register. Thus `offset` and `scale_factor`
4468 // above are expected to be null in this code path.
4469 DCHECK_EQ(offset, 0u);
4470 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4471 slow_path = new (GetGraph()->GetArena())
4472 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4473 ref,
4474 obj,
4475 /* field_offset */ index,
4476 temp_reg);
4477 } else {
4478 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4479 }
4480 AddSlowPath(slow_path);
4481
4482 // if (rb_state == ReadBarrier::GrayState())
4483 // ref = ReadBarrier::Mark(ref);
4484 // Given the numeric representation, it's enough to check the low bit of the
4485 // rb_state. We do that by shifting the bit into the sign bit (31) and
4486 // performing a branch on less than zero.
4487 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4488 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4489 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4490 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4491 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4492 __ Bind(slow_path->GetExitLabel());
4493}
4494
4495void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4496 Location out,
4497 Location ref,
4498 Location obj,
4499 uint32_t offset,
4500 Location index) {
4501 DCHECK(kEmitCompilerReadBarrier);
4502
4503 // Insert a slow path based read barrier *after* the reference load.
4504 //
4505 // If heap poisoning is enabled, the unpoisoning of the loaded
4506 // reference will be carried out by the runtime within the slow
4507 // path.
4508 //
4509 // Note that `ref` currently does not get unpoisoned (when heap
4510 // poisoning is enabled), which is alright as the `ref` argument is
4511 // not used by the artReadBarrierSlow entry point.
4512 //
4513 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4514 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4515 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4516 AddSlowPath(slow_path);
4517
4518 __ Bc(slow_path->GetEntryLabel());
4519 __ Bind(slow_path->GetExitLabel());
4520}
4521
4522void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4523 Location out,
4524 Location ref,
4525 Location obj,
4526 uint32_t offset,
4527 Location index) {
4528 if (kEmitCompilerReadBarrier) {
4529 // Baker's read barriers shall be handled by the fast path
4530 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4531 DCHECK(!kUseBakerReadBarrier);
4532 // If heap poisoning is enabled, unpoisoning will be taken care of
4533 // by the runtime within the slow path.
4534 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4535 } else if (kPoisonHeapReferences) {
4536 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4537 }
4538}
4539
4540void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4541 Location out,
4542 Location root) {
4543 DCHECK(kEmitCompilerReadBarrier);
4544
4545 // Insert a slow path based read barrier *after* the GC root load.
4546 //
4547 // Note that GC roots are not affected by heap poisoning, so we do
4548 // not need to do anything special for this here.
4549 SlowPathCodeMIPS64* slow_path =
4550 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4551 AddSlowPath(slow_path);
4552
4553 __ Bc(slow_path->GetEntryLabel());
4554 __ Bind(slow_path->GetExitLabel());
4555}
4556
Alexey Frunze4dda3372015-06-01 18:31:49 -07004557void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004558 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4559 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4560 switch (type_check_kind) {
4561 case TypeCheckKind::kExactCheck:
4562 case TypeCheckKind::kAbstractClassCheck:
4563 case TypeCheckKind::kClassHierarchyCheck:
4564 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004565 call_kind =
4566 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004567 break;
4568 case TypeCheckKind::kArrayCheck:
4569 case TypeCheckKind::kUnresolvedCheck:
4570 case TypeCheckKind::kInterfaceCheck:
4571 call_kind = LocationSummary::kCallOnSlowPath;
4572 break;
4573 }
4574
Alexey Frunze4dda3372015-06-01 18:31:49 -07004575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4576 locations->SetInAt(0, Location::RequiresRegister());
4577 locations->SetInAt(1, Location::RequiresRegister());
4578 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004579 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004580 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004581 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004582}
4583
4584void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004585 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004586 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004587 Location obj_loc = locations->InAt(0);
4588 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004589 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004590 Location out_loc = locations->Out();
4591 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4592 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4593 DCHECK_LE(num_temps, 1u);
4594 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004595 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4596 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4597 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4598 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004599 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004600 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004601
4602 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004603 // Avoid this check if we know `obj` is not null.
4604 if (instruction->MustDoNullCheck()) {
4605 __ Move(out, ZERO);
4606 __ Beqzc(obj, &done);
4607 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004608
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004609 switch (type_check_kind) {
4610 case TypeCheckKind::kExactCheck: {
4611 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004612 GenerateReferenceLoadTwoRegisters(instruction,
4613 out_loc,
4614 obj_loc,
4615 class_offset,
4616 maybe_temp_loc,
4617 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004618 // Classes must be equal for the instanceof to succeed.
4619 __ Xor(out, out, cls);
4620 __ Sltiu(out, out, 1);
4621 break;
4622 }
4623
4624 case TypeCheckKind::kAbstractClassCheck: {
4625 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004626 GenerateReferenceLoadTwoRegisters(instruction,
4627 out_loc,
4628 obj_loc,
4629 class_offset,
4630 maybe_temp_loc,
4631 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004632 // If the class is abstract, we eagerly fetch the super class of the
4633 // object to avoid doing a comparison we know will fail.
4634 Mips64Label loop;
4635 __ Bind(&loop);
4636 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004637 GenerateReferenceLoadOneRegister(instruction,
4638 out_loc,
4639 super_offset,
4640 maybe_temp_loc,
4641 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004642 // If `out` is null, we use it for the result, and jump to `done`.
4643 __ Beqzc(out, &done);
4644 __ Bnec(out, cls, &loop);
4645 __ LoadConst32(out, 1);
4646 break;
4647 }
4648
4649 case TypeCheckKind::kClassHierarchyCheck: {
4650 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004651 GenerateReferenceLoadTwoRegisters(instruction,
4652 out_loc,
4653 obj_loc,
4654 class_offset,
4655 maybe_temp_loc,
4656 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004657 // Walk over the class hierarchy to find a match.
4658 Mips64Label loop, success;
4659 __ Bind(&loop);
4660 __ Beqc(out, cls, &success);
4661 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004662 GenerateReferenceLoadOneRegister(instruction,
4663 out_loc,
4664 super_offset,
4665 maybe_temp_loc,
4666 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004667 __ Bnezc(out, &loop);
4668 // If `out` is null, we use it for the result, and jump to `done`.
4669 __ Bc(&done);
4670 __ Bind(&success);
4671 __ LoadConst32(out, 1);
4672 break;
4673 }
4674
4675 case TypeCheckKind::kArrayObjectCheck: {
4676 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004677 GenerateReferenceLoadTwoRegisters(instruction,
4678 out_loc,
4679 obj_loc,
4680 class_offset,
4681 maybe_temp_loc,
4682 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004683 // Do an exact check.
4684 Mips64Label success;
4685 __ Beqc(out, cls, &success);
4686 // Otherwise, we need to check that the object's class is a non-primitive array.
4687 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004688 GenerateReferenceLoadOneRegister(instruction,
4689 out_loc,
4690 component_offset,
4691 maybe_temp_loc,
4692 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004693 // If `out` is null, we use it for the result, and jump to `done`.
4694 __ Beqzc(out, &done);
4695 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4696 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4697 __ Sltiu(out, out, 1);
4698 __ Bc(&done);
4699 __ Bind(&success);
4700 __ LoadConst32(out, 1);
4701 break;
4702 }
4703
4704 case TypeCheckKind::kArrayCheck: {
4705 // No read barrier since the slow path will retry upon failure.
4706 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004707 GenerateReferenceLoadTwoRegisters(instruction,
4708 out_loc,
4709 obj_loc,
4710 class_offset,
4711 maybe_temp_loc,
4712 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004713 DCHECK(locations->OnlyCallsOnSlowPath());
4714 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4715 /* is_fatal */ false);
4716 codegen_->AddSlowPath(slow_path);
4717 __ Bnec(out, cls, slow_path->GetEntryLabel());
4718 __ LoadConst32(out, 1);
4719 break;
4720 }
4721
4722 case TypeCheckKind::kUnresolvedCheck:
4723 case TypeCheckKind::kInterfaceCheck: {
4724 // Note that we indeed only call on slow path, but we always go
4725 // into the slow path for the unresolved and interface check
4726 // cases.
4727 //
4728 // We cannot directly call the InstanceofNonTrivial runtime
4729 // entry point without resorting to a type checking slow path
4730 // here (i.e. by calling InvokeRuntime directly), as it would
4731 // require to assign fixed registers for the inputs of this
4732 // HInstanceOf instruction (following the runtime calling
4733 // convention), which might be cluttered by the potential first
4734 // read barrier emission at the beginning of this method.
4735 //
4736 // TODO: Introduce a new runtime entry point taking the object
4737 // to test (instead of its class) as argument, and let it deal
4738 // with the read barrier issues. This will let us refactor this
4739 // case of the `switch` code as it was previously (with a direct
4740 // call to the runtime not using a type checking slow path).
4741 // This should also be beneficial for the other cases above.
4742 DCHECK(locations->OnlyCallsOnSlowPath());
4743 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4744 /* is_fatal */ false);
4745 codegen_->AddSlowPath(slow_path);
4746 __ Bc(slow_path->GetEntryLabel());
4747 break;
4748 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004749 }
4750
4751 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004752
4753 if (slow_path != nullptr) {
4754 __ Bind(slow_path->GetExitLabel());
4755 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004756}
4757
4758void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4759 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4760 locations->SetOut(Location::ConstantLocation(constant));
4761}
4762
4763void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4764 // Will be generated at use site.
4765}
4766
4767void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4769 locations->SetOut(Location::ConstantLocation(constant));
4770}
4771
4772void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4773 // Will be generated at use site.
4774}
4775
Calin Juravle175dc732015-08-25 15:42:32 +01004776void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4777 // The trampoline uses the same calling convention as dex calling conventions,
4778 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4779 // the method_idx.
4780 HandleInvoke(invoke);
4781}
4782
4783void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4784 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4785}
4786
Alexey Frunze4dda3372015-06-01 18:31:49 -07004787void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4788 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4789 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4790}
4791
4792void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4793 HandleInvoke(invoke);
4794 // The register T0 is required to be used for the hidden argument in
4795 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4796 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4797}
4798
4799void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4800 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4801 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004802 Location receiver = invoke->GetLocations()->InAt(0);
4803 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004804 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004805
4806 // Set the hidden argument.
4807 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4808 invoke->GetDexMethodIndex());
4809
4810 // temp = object->GetClass();
4811 if (receiver.IsStackSlot()) {
4812 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4813 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4814 } else {
4815 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4816 }
4817 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004818 // Instead of simply (possibly) unpoisoning `temp` here, we should
4819 // emit a read barrier for the previous class reference load.
4820 // However this is not required in practice, as this is an
4821 // intermediate/temporary reference and because the current
4822 // concurrent copying collector keeps the from-space memory
4823 // intact/accessible until the end of the marking phase (the
4824 // concurrent copying collector may not in the future).
4825 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004826 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4827 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4828 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004829 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004830 // temp = temp->GetImtEntryAt(method_offset);
4831 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4832 // T9 = temp->GetEntryPoint();
4833 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4834 // T9();
4835 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004836 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004837 DCHECK(!codegen_->IsLeafMethod());
4838 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4839}
4840
4841void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004842 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4843 if (intrinsic.TryDispatch(invoke)) {
4844 return;
4845 }
4846
Alexey Frunze4dda3372015-06-01 18:31:49 -07004847 HandleInvoke(invoke);
4848}
4849
4850void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004851 // Explicit clinit checks triggered by static invokes must have been pruned by
4852 // art::PrepareForRegisterAllocation.
4853 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004854
Chris Larsen3039e382015-08-26 07:54:08 -07004855 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4856 if (intrinsic.TryDispatch(invoke)) {
4857 return;
4858 }
4859
Alexey Frunze4dda3372015-06-01 18:31:49 -07004860 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004861}
4862
Orion Hodsonac141392017-01-13 11:53:47 +00004863void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4864 HandleInvoke(invoke);
4865}
4866
4867void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4868 codegen_->GenerateInvokePolymorphicCall(invoke);
4869}
4870
Chris Larsen3039e382015-08-26 07:54:08 -07004871static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004872 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004873 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4874 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004875 return true;
4876 }
4877 return false;
4878}
4879
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004880HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004881 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004882 bool fallback_load = false;
4883 switch (desired_string_load_kind) {
4884 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4885 DCHECK(!GetCompilerOptions().GetCompilePic());
4886 break;
4887 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4888 DCHECK(GetCompilerOptions().GetCompilePic());
4889 break;
4890 case HLoadString::LoadKind::kBootImageAddress:
4891 break;
4892 case HLoadString::LoadKind::kBssEntry:
4893 DCHECK(!Runtime::Current()->UseJitCompilation());
4894 break;
4895 case HLoadString::LoadKind::kDexCacheViaMethod:
4896 break;
4897 case HLoadString::LoadKind::kJitTableAddress:
4898 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004899 break;
4900 }
4901 if (fallback_load) {
4902 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4903 }
4904 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004905}
4906
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004907HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4908 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004909 bool fallback_load = false;
4910 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004911 case HLoadClass::LoadKind::kInvalid:
4912 LOG(FATAL) << "UNREACHABLE";
4913 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004914 case HLoadClass::LoadKind::kReferrersClass:
4915 break;
4916 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4917 DCHECK(!GetCompilerOptions().GetCompilePic());
4918 break;
4919 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4920 DCHECK(GetCompilerOptions().GetCompilePic());
4921 break;
4922 case HLoadClass::LoadKind::kBootImageAddress:
4923 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004924 case HLoadClass::LoadKind::kBssEntry:
4925 DCHECK(!Runtime::Current()->UseJitCompilation());
4926 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004927 case HLoadClass::LoadKind::kJitTableAddress:
4928 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004929 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004930 case HLoadClass::LoadKind::kDexCacheViaMethod:
4931 break;
4932 }
4933 if (fallback_load) {
4934 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4935 }
4936 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937}
4938
Vladimir Markodc151b22015-10-15 18:02:30 +01004939HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4940 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004941 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004942 // On MIPS64 we support all dispatch types.
4943 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004944}
4945
Alexey Frunze4dda3372015-06-01 18:31:49 -07004946void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4947 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004948 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004949 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4950 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4951
Alexey Frunze19f6c692016-11-30 19:19:55 -08004952 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004953 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004954 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004955 uint32_t offset =
4956 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004957 __ LoadFromOffset(kLoadDoubleword,
4958 temp.AsRegister<GpuRegister>(),
4959 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004960 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004961 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004962 }
Vladimir Marko58155012015-08-19 12:49:41 +00004963 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004964 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004965 break;
4966 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004967 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4968 kLoadDoubleword,
4969 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004970 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004971 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4972 uint32_t offset = invoke->GetDexCacheArrayOffset();
4973 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004974 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004975 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4976 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4977 break;
4978 }
Vladimir Marko58155012015-08-19 12:49:41 +00004979 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004980 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004981 GpuRegister reg = temp.AsRegister<GpuRegister>();
4982 GpuRegister method_reg;
4983 if (current_method.IsRegister()) {
4984 method_reg = current_method.AsRegister<GpuRegister>();
4985 } else {
4986 // TODO: use the appropriate DCHECK() here if possible.
4987 // DCHECK(invoke->GetLocations()->Intrinsified());
4988 DCHECK(!current_method.IsValid());
4989 method_reg = reg;
4990 __ Ld(reg, SP, kCurrentMethodStackOffset);
4991 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004992
Vladimir Marko58155012015-08-19 12:49:41 +00004993 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004994 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00004995 reg,
4996 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01004997 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004998 // temp = temp[index_in_cache];
4999 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5000 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00005001 __ LoadFromOffset(kLoadDoubleword,
5002 reg,
5003 reg,
5004 CodeGenerator::GetCachePointerOffset(index_in_cache));
5005 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005006 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005007 }
5008
Alexey Frunze19f6c692016-11-30 19:19:55 -08005009 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00005010 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08005011 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00005012 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005013 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5014 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5015 __ LoadFromOffset(kLoadDoubleword,
5016 T9,
5017 callee_method.AsRegister<GpuRegister>(),
5018 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005019 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005020 // T9()
5021 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005022 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005023 break;
5024 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005025 DCHECK(!IsLeafMethod());
5026}
5027
5028void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005029 // Explicit clinit checks triggered by static invokes must have been pruned by
5030 // art::PrepareForRegisterAllocation.
5031 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005032
5033 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5034 return;
5035 }
5036
5037 LocationSummary* locations = invoke->GetLocations();
5038 codegen_->GenerateStaticOrDirectCall(invoke,
5039 locations->HasTemps()
5040 ? locations->GetTemp(0)
5041 : Location::NoLocation());
5042 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5043}
5044
Alexey Frunze53afca12015-11-05 16:34:23 -08005045void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005046 // Use the calling convention instead of the location of the receiver, as
5047 // intrinsics may have put the receiver in a different register. In the intrinsics
5048 // slow path, the arguments have been moved to the right place, so here we are
5049 // guaranteed that the receiver is the first register of the calling convention.
5050 InvokeDexCallingConvention calling_convention;
5051 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5052
Alexey Frunze53afca12015-11-05 16:34:23 -08005053 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005054 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5055 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5056 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005057 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005058
5059 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005060 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005061 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005062 // Instead of simply (possibly) unpoisoning `temp` here, we should
5063 // emit a read barrier for the previous class reference load.
5064 // However this is not required in practice, as this is an
5065 // intermediate/temporary reference and because the current
5066 // concurrent copying collector keeps the from-space memory
5067 // intact/accessible until the end of the marking phase (the
5068 // concurrent copying collector may not in the future).
5069 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005070 // temp = temp->GetMethodAt(method_offset);
5071 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5072 // T9 = temp->GetEntryPoint();
5073 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5074 // T9();
5075 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005076 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08005077}
5078
5079void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5080 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5081 return;
5082 }
5083
5084 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005085 DCHECK(!codegen_->IsLeafMethod());
5086 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5087}
5088
5089void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005090 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5091 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005092 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005093 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005094 cls,
5095 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005096 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005097 return;
5098 }
Vladimir Marko41559982017-01-06 14:04:23 +00005099 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005100
Alexey Frunze15958152017-02-09 19:08:30 -08005101 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5102 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005103 ? LocationSummary::kCallOnSlowPath
5104 : LocationSummary::kNoCall;
5105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00005106 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005107 locations->SetInAt(0, Location::RequiresRegister());
5108 }
5109 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005110}
5111
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005112// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5113// move.
5114void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005115 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5116 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5117 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005118 return;
5119 }
Vladimir Marko41559982017-01-06 14:04:23 +00005120 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005121
Vladimir Marko41559982017-01-06 14:04:23 +00005122 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005123 Location out_loc = locations->Out();
5124 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5125 GpuRegister current_method_reg = ZERO;
5126 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5127 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5128 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5129 }
5130
Alexey Frunze15958152017-02-09 19:08:30 -08005131 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5132 ? kWithoutReadBarrier
5133 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005134 bool generate_null_check = false;
5135 switch (load_kind) {
5136 case HLoadClass::LoadKind::kReferrersClass:
5137 DCHECK(!cls->CanCallRuntime());
5138 DCHECK(!cls->MustGenerateClinitCheck());
5139 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5140 GenerateGcRootFieldLoad(cls,
5141 out_loc,
5142 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005143 ArtMethod::DeclaringClassOffset().Int32Value(),
5144 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005145 break;
5146 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005147 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005148 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005149 __ LoadLiteral(out,
5150 kLoadUnsignedWord,
5151 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5152 cls->GetTypeIndex()));
5153 break;
5154 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005155 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005156 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005157 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5158 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5159 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5160 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5161 break;
5162 }
5163 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005164 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005165 uint32_t address = dchecked_integral_cast<uint32_t>(
5166 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5167 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005168 __ LoadLiteral(out,
5169 kLoadUnsignedWord,
5170 codegen_->DeduplicateBootImageAddressLiteral(address));
5171 break;
5172 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005173 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005174 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005175 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005176 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005177 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005178 generate_null_check = true;
5179 break;
5180 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005181 case HLoadClass::LoadKind::kJitTableAddress:
5182 __ LoadLiteral(out,
5183 kLoadUnsignedWord,
5184 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5185 cls->GetTypeIndex(),
5186 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005187 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005188 break;
Vladimir Marko41559982017-01-06 14:04:23 +00005189 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005190 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005191 LOG(FATAL) << "UNREACHABLE";
5192 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005193 }
5194
5195 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5196 DCHECK(cls->CanCallRuntime());
5197 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5198 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5199 codegen_->AddSlowPath(slow_path);
5200 if (generate_null_check) {
5201 __ Beqzc(out, slow_path->GetEntryLabel());
5202 }
5203 if (cls->MustGenerateClinitCheck()) {
5204 GenerateClassInitializationCheck(slow_path, out);
5205 } else {
5206 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005207 }
5208 }
5209}
5210
David Brazdilcb1c0552015-08-04 16:22:25 +01005211static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005212 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005213}
5214
Alexey Frunze4dda3372015-06-01 18:31:49 -07005215void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5216 LocationSummary* locations =
5217 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5218 locations->SetOut(Location::RequiresRegister());
5219}
5220
5221void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5222 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005223 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5224}
5225
5226void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5227 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5228}
5229
5230void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5231 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005232}
5233
Alexey Frunze4dda3372015-06-01 18:31:49 -07005234void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005235 HLoadString::LoadKind load_kind = load->GetLoadKind();
5236 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005237 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005238 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5239 InvokeRuntimeCallingConvention calling_convention;
5240 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5241 } else {
5242 locations->SetOut(Location::RequiresRegister());
5243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005244}
5245
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005246// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5247// move.
5248void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005249 HLoadString::LoadKind load_kind = load->GetLoadKind();
5250 LocationSummary* locations = load->GetLocations();
5251 Location out_loc = locations->Out();
5252 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5253
5254 switch (load_kind) {
5255 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005256 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005257 __ LoadLiteral(out,
5258 kLoadUnsignedWord,
5259 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5260 load->GetStringIndex()));
5261 return; // No dex cache slow path.
5262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5263 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5264 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005265 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005266 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5267 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5268 return; // No dex cache slow path.
5269 }
5270 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005271 uint32_t address = dchecked_integral_cast<uint32_t>(
5272 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5273 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005274 __ LoadLiteral(out,
5275 kLoadUnsignedWord,
5276 codegen_->DeduplicateBootImageAddressLiteral(address));
5277 return; // No dex cache slow path.
5278 }
5279 case HLoadString::LoadKind::kBssEntry: {
5280 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5281 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005282 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005283 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005284 GenerateGcRootFieldLoad(load,
5285 out_loc,
5286 out,
5287 /* placeholder */ 0x5678,
5288 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005289 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5290 codegen_->AddSlowPath(slow_path);
5291 __ Beqzc(out, slow_path->GetEntryLabel());
5292 __ Bind(slow_path->GetExitLabel());
5293 return;
5294 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005295 case HLoadString::LoadKind::kJitTableAddress:
5296 __ LoadLiteral(out,
5297 kLoadUnsignedWord,
5298 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5299 load->GetStringIndex(),
5300 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005301 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005302 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005303 default:
5304 break;
5305 }
5306
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005307 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005308 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5309 InvokeRuntimeCallingConvention calling_convention;
5310 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5311 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5312 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005313}
5314
Alexey Frunze4dda3372015-06-01 18:31:49 -07005315void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5317 locations->SetOut(Location::ConstantLocation(constant));
5318}
5319
5320void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5321 // Will be generated at use site.
5322}
5323
5324void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5325 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005327 InvokeRuntimeCallingConvention calling_convention;
5328 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5329}
5330
5331void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005332 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005333 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005334 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005335 if (instruction->IsEnter()) {
5336 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5337 } else {
5338 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5339 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005340}
5341
5342void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5343 LocationSummary* locations =
5344 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5345 switch (mul->GetResultType()) {
5346 case Primitive::kPrimInt:
5347 case Primitive::kPrimLong:
5348 locations->SetInAt(0, Location::RequiresRegister());
5349 locations->SetInAt(1, Location::RequiresRegister());
5350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5351 break;
5352
5353 case Primitive::kPrimFloat:
5354 case Primitive::kPrimDouble:
5355 locations->SetInAt(0, Location::RequiresFpuRegister());
5356 locations->SetInAt(1, Location::RequiresFpuRegister());
5357 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5358 break;
5359
5360 default:
5361 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5362 }
5363}
5364
5365void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5366 Primitive::Type type = instruction->GetType();
5367 LocationSummary* locations = instruction->GetLocations();
5368
5369 switch (type) {
5370 case Primitive::kPrimInt:
5371 case Primitive::kPrimLong: {
5372 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5373 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5374 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5375 if (type == Primitive::kPrimInt)
5376 __ MulR6(dst, lhs, rhs);
5377 else
5378 __ Dmul(dst, lhs, rhs);
5379 break;
5380 }
5381 case Primitive::kPrimFloat:
5382 case Primitive::kPrimDouble: {
5383 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5384 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5385 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5386 if (type == Primitive::kPrimFloat)
5387 __ MulS(dst, lhs, rhs);
5388 else
5389 __ MulD(dst, lhs, rhs);
5390 break;
5391 }
5392 default:
5393 LOG(FATAL) << "Unexpected mul type " << type;
5394 }
5395}
5396
5397void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5398 LocationSummary* locations =
5399 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5400 switch (neg->GetResultType()) {
5401 case Primitive::kPrimInt:
5402 case Primitive::kPrimLong:
5403 locations->SetInAt(0, Location::RequiresRegister());
5404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5405 break;
5406
5407 case Primitive::kPrimFloat:
5408 case Primitive::kPrimDouble:
5409 locations->SetInAt(0, Location::RequiresFpuRegister());
5410 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5411 break;
5412
5413 default:
5414 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5415 }
5416}
5417
5418void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5419 Primitive::Type type = instruction->GetType();
5420 LocationSummary* locations = instruction->GetLocations();
5421
5422 switch (type) {
5423 case Primitive::kPrimInt:
5424 case Primitive::kPrimLong: {
5425 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5426 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5427 if (type == Primitive::kPrimInt)
5428 __ Subu(dst, ZERO, src);
5429 else
5430 __ Dsubu(dst, ZERO, src);
5431 break;
5432 }
5433 case Primitive::kPrimFloat:
5434 case Primitive::kPrimDouble: {
5435 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5436 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5437 if (type == Primitive::kPrimFloat)
5438 __ NegS(dst, src);
5439 else
5440 __ NegD(dst, src);
5441 break;
5442 }
5443 default:
5444 LOG(FATAL) << "Unexpected neg type " << type;
5445 }
5446}
5447
5448void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5449 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005451 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005452 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005453 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5454 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005455}
5456
5457void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005458 // Note: if heap poisoning is enabled, the entry point takes care
5459 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005460 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5461 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005462}
5463
5464void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5465 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005467 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005468 if (instruction->IsStringAlloc()) {
5469 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5470 } else {
5471 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005472 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005473 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5474}
5475
5476void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005477 // Note: if heap poisoning is enabled, the entry point takes care
5478 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005479 if (instruction->IsStringAlloc()) {
5480 // String is allocated through StringFactory. Call NewEmptyString entry point.
5481 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005482 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005483 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005484 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5485 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5486 __ Jalr(T9);
5487 __ Nop();
5488 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5489 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005490 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005491 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005492 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005493}
5494
5495void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5497 locations->SetInAt(0, Location::RequiresRegister());
5498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5499}
5500
5501void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5502 Primitive::Type type = instruction->GetType();
5503 LocationSummary* locations = instruction->GetLocations();
5504
5505 switch (type) {
5506 case Primitive::kPrimInt:
5507 case Primitive::kPrimLong: {
5508 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5509 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5510 __ Nor(dst, src, ZERO);
5511 break;
5512 }
5513
5514 default:
5515 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5516 }
5517}
5518
5519void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5521 locations->SetInAt(0, Location::RequiresRegister());
5522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5523}
5524
5525void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5526 LocationSummary* locations = instruction->GetLocations();
5527 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5528 locations->InAt(0).AsRegister<GpuRegister>(),
5529 1);
5530}
5531
5532void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005533 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5534 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005535}
5536
Calin Juravle2ae48182016-03-16 14:05:09 +00005537void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5538 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005539 return;
5540 }
5541 Location obj = instruction->GetLocations()->InAt(0);
5542
5543 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005544 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005545}
5546
Calin Juravle2ae48182016-03-16 14:05:09 +00005547void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005548 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005549 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005550
5551 Location obj = instruction->GetLocations()->InAt(0);
5552
5553 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5554}
5555
5556void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005557 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005558}
5559
5560void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5561 HandleBinaryOp(instruction);
5562}
5563
5564void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5565 HandleBinaryOp(instruction);
5566}
5567
5568void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5569 LOG(FATAL) << "Unreachable";
5570}
5571
5572void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5573 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5574}
5575
5576void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5577 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5578 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5579 if (location.IsStackSlot()) {
5580 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5581 } else if (location.IsDoubleStackSlot()) {
5582 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5583 }
5584 locations->SetOut(location);
5585}
5586
5587void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5588 ATTRIBUTE_UNUSED) {
5589 // Nothing to do, the parameter is already at its location.
5590}
5591
5592void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5593 LocationSummary* locations =
5594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5595 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5596}
5597
5598void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5599 ATTRIBUTE_UNUSED) {
5600 // Nothing to do, the method is already at its location.
5601}
5602
5603void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005605 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005606 locations->SetInAt(i, Location::Any());
5607 }
5608 locations->SetOut(Location::Any());
5609}
5610
5611void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5612 LOG(FATAL) << "Unreachable";
5613}
5614
5615void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5616 Primitive::Type type = rem->GetResultType();
5617 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005618 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5619 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5621
5622 switch (type) {
5623 case Primitive::kPrimInt:
5624 case Primitive::kPrimLong:
5625 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005626 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5628 break;
5629
5630 case Primitive::kPrimFloat:
5631 case Primitive::kPrimDouble: {
5632 InvokeRuntimeCallingConvention calling_convention;
5633 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5634 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5635 locations->SetOut(calling_convention.GetReturnLocation(type));
5636 break;
5637 }
5638
5639 default:
5640 LOG(FATAL) << "Unexpected rem type " << type;
5641 }
5642}
5643
5644void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5645 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005646
5647 switch (type) {
5648 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005649 case Primitive::kPrimLong:
5650 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005651 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005652
5653 case Primitive::kPrimFloat:
5654 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005655 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5656 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005657 if (type == Primitive::kPrimFloat) {
5658 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5659 } else {
5660 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5661 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005662 break;
5663 }
5664 default:
5665 LOG(FATAL) << "Unexpected rem type " << type;
5666 }
5667}
5668
5669void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5670 memory_barrier->SetLocations(nullptr);
5671}
5672
5673void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5674 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5675}
5676
5677void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5679 Primitive::Type return_type = ret->InputAt(0)->GetType();
5680 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5681}
5682
5683void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5684 codegen_->GenerateFrameExit();
5685}
5686
5687void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5688 ret->SetLocations(nullptr);
5689}
5690
5691void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5692 codegen_->GenerateFrameExit();
5693}
5694
Alexey Frunze92d90602015-12-18 18:16:36 -08005695void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5696 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005697}
5698
Alexey Frunze92d90602015-12-18 18:16:36 -08005699void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5700 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005701}
5702
Alexey Frunze4dda3372015-06-01 18:31:49 -07005703void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5704 HandleShift(shl);
5705}
5706
5707void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5708 HandleShift(shl);
5709}
5710
5711void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5712 HandleShift(shr);
5713}
5714
5715void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5716 HandleShift(shr);
5717}
5718
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5720 HandleBinaryOp(instruction);
5721}
5722
5723void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5724 HandleBinaryOp(instruction);
5725}
5726
5727void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5728 HandleFieldGet(instruction, instruction->GetFieldInfo());
5729}
5730
5731void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5732 HandleFieldGet(instruction, instruction->GetFieldInfo());
5733}
5734
5735void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5736 HandleFieldSet(instruction, instruction->GetFieldInfo());
5737}
5738
5739void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005740 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005741}
5742
Calin Juravlee460d1d2015-09-29 04:52:17 +01005743void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5744 HUnresolvedInstanceFieldGet* instruction) {
5745 FieldAccessCallingConventionMIPS64 calling_convention;
5746 codegen_->CreateUnresolvedFieldLocationSummary(
5747 instruction, instruction->GetFieldType(), calling_convention);
5748}
5749
5750void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5751 HUnresolvedInstanceFieldGet* instruction) {
5752 FieldAccessCallingConventionMIPS64 calling_convention;
5753 codegen_->GenerateUnresolvedFieldAccess(instruction,
5754 instruction->GetFieldType(),
5755 instruction->GetFieldIndex(),
5756 instruction->GetDexPc(),
5757 calling_convention);
5758}
5759
5760void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5761 HUnresolvedInstanceFieldSet* instruction) {
5762 FieldAccessCallingConventionMIPS64 calling_convention;
5763 codegen_->CreateUnresolvedFieldLocationSummary(
5764 instruction, instruction->GetFieldType(), calling_convention);
5765}
5766
5767void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5768 HUnresolvedInstanceFieldSet* instruction) {
5769 FieldAccessCallingConventionMIPS64 calling_convention;
5770 codegen_->GenerateUnresolvedFieldAccess(instruction,
5771 instruction->GetFieldType(),
5772 instruction->GetFieldIndex(),
5773 instruction->GetDexPc(),
5774 calling_convention);
5775}
5776
5777void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5778 HUnresolvedStaticFieldGet* instruction) {
5779 FieldAccessCallingConventionMIPS64 calling_convention;
5780 codegen_->CreateUnresolvedFieldLocationSummary(
5781 instruction, instruction->GetFieldType(), calling_convention);
5782}
5783
5784void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5785 HUnresolvedStaticFieldGet* instruction) {
5786 FieldAccessCallingConventionMIPS64 calling_convention;
5787 codegen_->GenerateUnresolvedFieldAccess(instruction,
5788 instruction->GetFieldType(),
5789 instruction->GetFieldIndex(),
5790 instruction->GetDexPc(),
5791 calling_convention);
5792}
5793
5794void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5795 HUnresolvedStaticFieldSet* instruction) {
5796 FieldAccessCallingConventionMIPS64 calling_convention;
5797 codegen_->CreateUnresolvedFieldLocationSummary(
5798 instruction, instruction->GetFieldType(), calling_convention);
5799}
5800
5801void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5802 HUnresolvedStaticFieldSet* instruction) {
5803 FieldAccessCallingConventionMIPS64 calling_convention;
5804 codegen_->GenerateUnresolvedFieldAccess(instruction,
5805 instruction->GetFieldType(),
5806 instruction->GetFieldIndex(),
5807 instruction->GetDexPc(),
5808 calling_convention);
5809}
5810
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005812 LocationSummary* locations =
5813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005814 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005815}
5816
5817void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5818 HBasicBlock* block = instruction->GetBlock();
5819 if (block->GetLoopInformation() != nullptr) {
5820 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5821 // The back edge will generate the suspend check.
5822 return;
5823 }
5824 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5825 // The goto will generate the suspend check.
5826 return;
5827 }
5828 GenerateSuspendCheck(instruction, nullptr);
5829}
5830
Alexey Frunze4dda3372015-06-01 18:31:49 -07005831void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5832 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005834 InvokeRuntimeCallingConvention calling_convention;
5835 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5836}
5837
5838void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005839 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005840 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5841}
5842
5843void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5844 Primitive::Type input_type = conversion->GetInputType();
5845 Primitive::Type result_type = conversion->GetResultType();
5846 DCHECK_NE(input_type, result_type);
5847
5848 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5849 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5850 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5851 }
5852
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005853 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5854
5855 if (Primitive::IsFloatingPointType(input_type)) {
5856 locations->SetInAt(0, Location::RequiresFpuRegister());
5857 } else {
5858 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005859 }
5860
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005861 if (Primitive::IsFloatingPointType(result_type)) {
5862 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005863 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005865 }
5866}
5867
5868void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5869 LocationSummary* locations = conversion->GetLocations();
5870 Primitive::Type result_type = conversion->GetResultType();
5871 Primitive::Type input_type = conversion->GetInputType();
5872
5873 DCHECK_NE(input_type, result_type);
5874
5875 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5876 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5877 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5878
5879 switch (result_type) {
5880 case Primitive::kPrimChar:
5881 __ Andi(dst, src, 0xFFFF);
5882 break;
5883 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005884 if (input_type == Primitive::kPrimLong) {
5885 // Type conversion from long to types narrower than int is a result of code
5886 // transformations. To avoid unpredictable results for SEB and SEH, we first
5887 // need to sign-extend the low 32-bit value into bits 32 through 63.
5888 __ Sll(dst, src, 0);
5889 __ Seb(dst, dst);
5890 } else {
5891 __ Seb(dst, src);
5892 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005893 break;
5894 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005895 if (input_type == Primitive::kPrimLong) {
5896 // Type conversion from long to types narrower than int is a result of code
5897 // transformations. To avoid unpredictable results for SEB and SEH, we first
5898 // need to sign-extend the low 32-bit value into bits 32 through 63.
5899 __ Sll(dst, src, 0);
5900 __ Seh(dst, dst);
5901 } else {
5902 __ Seh(dst, src);
5903 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005904 break;
5905 case Primitive::kPrimInt:
5906 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005907 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5908 // conversions, except when the input and output registers are the same and we are not
5909 // converting longs to shorter types. In these cases, do nothing.
5910 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5911 __ Sll(dst, src, 0);
5912 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005913 break;
5914
5915 default:
5916 LOG(FATAL) << "Unexpected type conversion from " << input_type
5917 << " to " << result_type;
5918 }
5919 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005920 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5921 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5922 if (input_type == Primitive::kPrimLong) {
5923 __ Dmtc1(src, FTMP);
5924 if (result_type == Primitive::kPrimFloat) {
5925 __ Cvtsl(dst, FTMP);
5926 } else {
5927 __ Cvtdl(dst, FTMP);
5928 }
5929 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005930 __ Mtc1(src, FTMP);
5931 if (result_type == Primitive::kPrimFloat) {
5932 __ Cvtsw(dst, FTMP);
5933 } else {
5934 __ Cvtdw(dst, FTMP);
5935 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005936 }
5937 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5938 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005939 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5940 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5941 Mips64Label truncate;
5942 Mips64Label done;
5943
5944 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5945 // value when the input is either a NaN or is outside of the range of the output type
5946 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5947 // the same result.
5948 //
5949 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5950 // value of the output type if the input is outside of the range after the truncation or
5951 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5952 // results. This matches the desired float/double-to-int/long conversion exactly.
5953 //
5954 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5955 //
5956 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5957 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5958 // even though it must be NAN2008=1 on R6.
5959 //
5960 // The code takes care of the different behaviors by first comparing the input to the
5961 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5962 // If the input is greater than or equal to the minimum, it procedes to the truncate
5963 // instruction, which will handle such an input the same way irrespective of NAN2008.
5964 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5965 // in order to return either zero or the minimum value.
5966 //
5967 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5968 // truncate instruction for MIPS64R6.
5969 if (input_type == Primitive::kPrimFloat) {
5970 uint32_t min_val = (result_type == Primitive::kPrimLong)
5971 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
5972 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5973 __ LoadConst32(TMP, min_val);
5974 __ Mtc1(TMP, FTMP);
5975 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005976 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005977 uint64_t min_val = (result_type == Primitive::kPrimLong)
5978 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
5979 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5980 __ LoadConst64(TMP, min_val);
5981 __ Dmtc1(TMP, FTMP);
5982 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005983 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005984
5985 __ Bc1nez(FTMP, &truncate);
5986
5987 if (input_type == Primitive::kPrimFloat) {
5988 __ CmpEqS(FTMP, src, src);
5989 } else {
5990 __ CmpEqD(FTMP, src, src);
5991 }
5992 if (result_type == Primitive::kPrimLong) {
5993 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
5994 } else {
5995 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5996 }
5997 __ Mfc1(TMP, FTMP);
5998 __ And(dst, dst, TMP);
5999
6000 __ Bc(&done);
6001
6002 __ Bind(&truncate);
6003
6004 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006005 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006006 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006007 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006008 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006009 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006010 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006011 } else {
6012 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006013 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006014 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006015 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006016 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006017 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006018 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006019
6020 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006021 } else if (Primitive::IsFloatingPointType(result_type) &&
6022 Primitive::IsFloatingPointType(input_type)) {
6023 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6024 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6025 if (result_type == Primitive::kPrimFloat) {
6026 __ Cvtsd(dst, src);
6027 } else {
6028 __ Cvtds(dst, src);
6029 }
6030 } else {
6031 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6032 << " to " << result_type;
6033 }
6034}
6035
6036void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6037 HandleShift(ushr);
6038}
6039
6040void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6041 HandleShift(ushr);
6042}
6043
6044void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6045 HandleBinaryOp(instruction);
6046}
6047
6048void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6049 HandleBinaryOp(instruction);
6050}
6051
6052void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6053 // Nothing to do, this should be removed during prepare for register allocator.
6054 LOG(FATAL) << "Unreachable";
6055}
6056
6057void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6058 // Nothing to do, this should be removed during prepare for register allocator.
6059 LOG(FATAL) << "Unreachable";
6060}
6061
6062void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064}
6065
6066void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006067 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006068}
6069
6070void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006071 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006072}
6073
6074void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006075 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006076}
6077
6078void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006079 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006080}
6081
6082void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006083 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006084}
6085
6086void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006087 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088}
6089
6090void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006091 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006092}
6093
6094void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006095 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006096}
6097
6098void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006099 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006100}
6101
6102void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006103 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006104}
6105
6106void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006107 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006108}
6109
Aart Bike9f37602015-10-09 11:15:55 -07006110void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006112}
6113
6114void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006116}
6117
6118void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006120}
6121
6122void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006124}
6125
6126void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006128}
6129
6130void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006131 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006132}
6133
6134void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006135 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006136}
6137
6138void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006139 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006140}
6141
Mark Mendellfe57faa2015-09-18 09:26:15 -04006142// Simple implementation of packed switch - generate cascaded compare/jumps.
6143void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6144 LocationSummary* locations =
6145 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6146 locations->SetInAt(0, Location::RequiresRegister());
6147}
6148
Alexey Frunze0960ac52016-12-20 17:24:59 -08006149void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6150 int32_t lower_bound,
6151 uint32_t num_entries,
6152 HBasicBlock* switch_block,
6153 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006154 // Create a set of compare/jumps.
6155 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006156 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006157 // Jump to default if index is negative
6158 // Note: We don't check the case that index is positive while value < lower_bound, because in
6159 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6160 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6161
Alexey Frunze0960ac52016-12-20 17:24:59 -08006162 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006163 // Jump to successors[0] if value == lower_bound.
6164 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6165 int32_t last_index = 0;
6166 for (; num_entries - last_index > 2; last_index += 2) {
6167 __ Addiu(temp_reg, temp_reg, -2);
6168 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6169 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6170 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6171 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6172 }
6173 if (num_entries - last_index == 2) {
6174 // The last missing case_value.
6175 __ Addiu(temp_reg, temp_reg, -1);
6176 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006177 }
6178
6179 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006180 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006181 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006182 }
6183}
6184
Alexey Frunze0960ac52016-12-20 17:24:59 -08006185void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6186 int32_t lower_bound,
6187 uint32_t num_entries,
6188 HBasicBlock* switch_block,
6189 HBasicBlock* default_block) {
6190 // Create a jump table.
6191 std::vector<Mips64Label*> labels(num_entries);
6192 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6193 for (uint32_t i = 0; i < num_entries; i++) {
6194 labels[i] = codegen_->GetLabelOf(successors[i]);
6195 }
6196 JumpTable* table = __ CreateJumpTable(std::move(labels));
6197
6198 // Is the value in range?
6199 __ Addiu32(TMP, value_reg, -lower_bound);
6200 __ LoadConst32(AT, num_entries);
6201 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6202
6203 // We are in the range of the table.
6204 // Load the target address from the jump table, indexing by the value.
6205 __ LoadLabelAddress(AT, table->GetLabel());
6206 __ Sll(TMP, TMP, 2);
6207 __ Daddu(TMP, TMP, AT);
6208 __ Lw(TMP, TMP, 0);
6209 // Compute the absolute target address by adding the table start address
6210 // (the table contains offsets to targets relative to its start).
6211 __ Daddu(TMP, TMP, AT);
6212 // And jump.
6213 __ Jr(TMP);
6214 __ Nop();
6215}
6216
6217void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6218 int32_t lower_bound = switch_instr->GetStartValue();
6219 uint32_t num_entries = switch_instr->GetNumEntries();
6220 LocationSummary* locations = switch_instr->GetLocations();
6221 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6222 HBasicBlock* switch_block = switch_instr->GetBlock();
6223 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6224
6225 if (num_entries > kPackedSwitchJumpTableThreshold) {
6226 GenTableBasedPackedSwitch(value_reg,
6227 lower_bound,
6228 num_entries,
6229 switch_block,
6230 default_block);
6231 } else {
6232 GenPackedSwitchWithCompares(value_reg,
6233 lower_bound,
6234 num_entries,
6235 switch_block,
6236 default_block);
6237 }
6238}
6239
Chris Larsenc9905a62017-03-13 17:06:18 -07006240void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6241 LocationSummary* locations =
6242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6243 locations->SetInAt(0, Location::RequiresRegister());
6244 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006245}
6246
Chris Larsenc9905a62017-03-13 17:06:18 -07006247void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6248 LocationSummary* locations = instruction->GetLocations();
6249 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6250 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6251 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6252 __ LoadFromOffset(kLoadDoubleword,
6253 locations->Out().AsRegister<GpuRegister>(),
6254 locations->InAt(0).AsRegister<GpuRegister>(),
6255 method_offset);
6256 } else {
6257 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6258 instruction->GetIndex(), kMips64PointerSize));
6259 __ LoadFromOffset(kLoadDoubleword,
6260 locations->Out().AsRegister<GpuRegister>(),
6261 locations->InAt(0).AsRegister<GpuRegister>(),
6262 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6263 __ LoadFromOffset(kLoadDoubleword,
6264 locations->Out().AsRegister<GpuRegister>(),
6265 locations->Out().AsRegister<GpuRegister>(),
6266 method_offset);
6267 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006268}
6269
Alexey Frunze4dda3372015-06-01 18:31:49 -07006270} // namespace mips64
6271} // namespace art