blob: 6f37ed44c4c4452da721205cd654553493004ff0 [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 {
Chris Larsencd0295d2017-03-31 15:26:54 -07002062 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002063 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 }
2065 break;
2066 }
2067
2068 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002069 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002070 if (maybe_compressed_char_at) {
2071 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002072 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002073 __ Dext(TMP, TMP, 0, 1);
2074 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2075 "Expecting 0=compressed, 1=uncompressed");
2076 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002077 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002078 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2079 if (maybe_compressed_char_at) {
2080 Mips64Label uncompressed_load, done;
2081 __ Bnezc(TMP, &uncompressed_load);
2082 __ LoadFromOffset(kLoadUnsignedByte,
2083 out,
2084 obj,
2085 data_offset + (const_index << TIMES_1));
2086 __ Bc(&done);
2087 __ Bind(&uncompressed_load);
2088 __ LoadFromOffset(kLoadUnsignedHalfword,
2089 out,
2090 obj,
2091 data_offset + (const_index << TIMES_2));
2092 __ Bind(&done);
2093 } else {
2094 __ LoadFromOffset(kLoadUnsignedHalfword,
2095 out,
2096 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002097 data_offset + (const_index << TIMES_2),
2098 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002099 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002100 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002101 GpuRegister index_reg = index.AsRegister<GpuRegister>();
2102 if (maybe_compressed_char_at) {
2103 Mips64Label uncompressed_load, done;
2104 __ Bnezc(TMP, &uncompressed_load);
2105 __ Daddu(TMP, obj, index_reg);
2106 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2107 __ Bc(&done);
2108 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002109 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002110 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2111 __ Bind(&done);
2112 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002113 __ Dlsa(TMP, index_reg, obj, TIMES_2);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002114 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002115 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002116 }
2117 break;
2118 }
2119
Alexey Frunze15958152017-02-09 19:08:30 -08002120 case Primitive::kPrimInt: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002122 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002123 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
2124 if (index.IsConstant()) {
2125 size_t offset =
2126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002127 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002128 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002129 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002130 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 }
2132 break;
2133 }
2134
Alexey Frunze15958152017-02-09 19:08:30 -08002135 case Primitive::kPrimNot: {
2136 static_assert(
2137 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2138 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2139 // /* HeapReference<Object> */ out =
2140 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2141 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2142 Location temp = locations->GetTemp(0);
2143 // Note that a potential implicit null check is handled in this
2144 // CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier call.
2145 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2146 out_loc,
2147 obj,
2148 data_offset,
2149 index,
2150 temp,
2151 /* needs_null_check */ true);
2152 } else {
2153 GpuRegister out = out_loc.AsRegister<GpuRegister>();
2154 if (index.IsConstant()) {
2155 size_t offset =
2156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2157 __ LoadFromOffset(kLoadUnsignedWord, out, obj, offset, null_checker);
2158 // If read barriers are enabled, emit read barriers other than
2159 // Baker's using a slow path (and also unpoison the loaded
2160 // reference, if heap poisoning is enabled).
2161 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2162 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002163 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002164 __ LoadFromOffset(kLoadUnsignedWord, out, TMP, data_offset, null_checker);
2165 // If read barriers are enabled, emit read barriers other than
2166 // Baker's using a slow path (and also unpoison the loaded
2167 // reference, if heap poisoning is enabled).
2168 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2169 out_loc,
2170 out_loc,
2171 obj_loc,
2172 data_offset,
2173 index);
2174 }
2175 }
2176 break;
2177 }
2178
Alexey Frunze4dda3372015-06-01 18:31:49 -07002179 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002180 GpuRegister out = out_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002181 if (index.IsConstant()) {
2182 size_t offset =
2183 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002184 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002186 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002187 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002188 }
2189 break;
2190 }
2191
2192 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002193 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002194 if (index.IsConstant()) {
2195 size_t offset =
2196 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002197 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002198 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002199 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002200 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002201 }
2202 break;
2203 }
2204
2205 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002206 FpuRegister out = out_loc.AsFpuRegister<FpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002207 if (index.IsConstant()) {
2208 size_t offset =
2209 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002210 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002211 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002212 __ Dlsa(TMP, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002213 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002214 }
2215 break;
2216 }
2217
2218 case Primitive::kPrimVoid:
2219 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2220 UNREACHABLE();
2221 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002222}
2223
2224void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
2225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2226 locations->SetInAt(0, Location::RequiresRegister());
2227 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2228}
2229
2230void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
2231 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002232 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002233 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2234 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2235 __ LoadFromOffset(kLoadWord, out, obj, offset);
2236 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002237 // Mask out compression flag from String's array length.
2238 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2239 __ Srl(out, out, 1u);
2240 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002241}
2242
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002243Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
2244 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2245 ? Location::ConstantLocation(instruction->AsConstant())
2246 : Location::RequiresRegister();
2247}
2248
2249Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2250 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2251 // We can store a non-zero float or double constant without first loading it into the FPU,
2252 // but we should only prefer this if the constant has a single use.
2253 if (instruction->IsConstant() &&
2254 (instruction->AsConstant()->IsZeroBitPattern() ||
2255 instruction->GetUses().HasExactlyOneElement())) {
2256 return Location::ConstantLocation(instruction->AsConstant());
2257 // Otherwise fall through and require an FPU register for the constant.
2258 }
2259 return Location::RequiresFpuRegister();
2260}
2261
Alexey Frunze4dda3372015-06-01 18:31:49 -07002262void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002263 Primitive::Type value_type = instruction->GetComponentType();
2264
2265 bool needs_write_barrier =
2266 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2267 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2268
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2270 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002271 may_need_runtime_call_for_type_check ?
2272 LocationSummary::kCallOnSlowPath :
2273 LocationSummary::kNoCall);
2274
2275 locations->SetInAt(0, Location::RequiresRegister());
2276 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2277 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2278 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002279 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002280 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2281 }
2282 if (needs_write_barrier) {
2283 // Temporary register for the write barrier.
2284 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002285 }
2286}
2287
2288void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
2289 LocationSummary* locations = instruction->GetLocations();
2290 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2291 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002292 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002293 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002294 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002295 bool needs_write_barrier =
2296 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002297 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002298 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002299
2300 switch (value_type) {
2301 case Primitive::kPrimBoolean:
2302 case Primitive::kPrimByte: {
2303 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002304 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002305 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002307 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
2308 }
2309 if (value_location.IsConstant()) {
2310 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2311 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2312 } else {
2313 GpuRegister value = value_location.AsRegister<GpuRegister>();
2314 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002315 }
2316 break;
2317 }
2318
2319 case Primitive::kPrimShort:
2320 case Primitive::kPrimChar: {
2321 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002322 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002323 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002324 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002325 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_2);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002326 }
2327 if (value_location.IsConstant()) {
2328 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2329 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2330 } else {
2331 GpuRegister value = value_location.AsRegister<GpuRegister>();
2332 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002333 }
2334 break;
2335 }
2336
Alexey Frunze15958152017-02-09 19:08:30 -08002337 case Primitive::kPrimInt: {
2338 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2339 if (index.IsConstant()) {
2340 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2341 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002342 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002343 }
2344 if (value_location.IsConstant()) {
2345 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2346 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2347 } else {
2348 GpuRegister value = value_location.AsRegister<GpuRegister>();
2349 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2350 }
2351 break;
2352 }
2353
Alexey Frunze4dda3372015-06-01 18:31:49 -07002354 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002355 if (value_location.IsConstant()) {
2356 // Just setting null.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002357 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002358 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08002359 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002360 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002361 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunzec061de12017-02-14 13:27:23 -08002362 }
Alexey Frunze15958152017-02-09 19:08:30 -08002363 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2364 DCHECK_EQ(value, 0);
2365 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2366 DCHECK(!needs_write_barrier);
2367 DCHECK(!may_need_runtime_call_for_type_check);
2368 break;
2369 }
2370
2371 DCHECK(needs_write_barrier);
2372 GpuRegister value = value_location.AsRegister<GpuRegister>();
2373 GpuRegister temp1 = locations->GetTemp(0).AsRegister<GpuRegister>();
2374 GpuRegister temp2 = TMP; // Doesn't need to survive slow path.
2375 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2376 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2377 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2378 Mips64Label done;
2379 SlowPathCodeMIPS64* slow_path = nullptr;
2380
2381 if (may_need_runtime_call_for_type_check) {
2382 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS64(instruction);
2383 codegen_->AddSlowPath(slow_path);
2384 if (instruction->GetValueCanBeNull()) {
2385 Mips64Label non_zero;
2386 __ Bnezc(value, &non_zero);
2387 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2388 if (index.IsConstant()) {
2389 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002390 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002391 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002392 }
Alexey Frunze15958152017-02-09 19:08:30 -08002393 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2394 __ Bc(&done);
2395 __ Bind(&non_zero);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002396 }
Alexey Frunze15958152017-02-09 19:08:30 -08002397
2398 // Note that when read barriers are enabled, the type checks
2399 // are performed without read barriers. This is fine, even in
2400 // the case where a class object is in the from-space after
2401 // the flip, as a comparison involving such a type would not
2402 // produce a false positive; it may of course produce a false
2403 // negative, in which case we would take the ArraySet slow
2404 // path.
2405
2406 // /* HeapReference<Class> */ temp1 = obj->klass_
2407 __ LoadFromOffset(kLoadUnsignedWord, temp1, obj, class_offset, null_checker);
2408 __ MaybeUnpoisonHeapReference(temp1);
2409
2410 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2411 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, component_offset);
2412 // /* HeapReference<Class> */ temp2 = value->klass_
2413 __ LoadFromOffset(kLoadUnsignedWord, temp2, value, class_offset);
2414 // If heap poisoning is enabled, no need to unpoison `temp1`
2415 // nor `temp2`, as we are comparing two poisoned references.
2416
2417 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2418 Mips64Label do_put;
2419 __ Beqc(temp1, temp2, &do_put);
2420 // If heap poisoning is enabled, the `temp1` reference has
2421 // not been unpoisoned yet; unpoison it now.
2422 __ MaybeUnpoisonHeapReference(temp1);
2423
2424 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2425 __ LoadFromOffset(kLoadUnsignedWord, temp1, temp1, super_offset);
2426 // If heap poisoning is enabled, no need to unpoison
2427 // `temp1`, as we are comparing against null below.
2428 __ Bnezc(temp1, slow_path->GetEntryLabel());
2429 __ Bind(&do_put);
2430 } else {
2431 __ Bnec(temp1, temp2, slow_path->GetEntryLabel());
2432 }
2433 }
2434
2435 GpuRegister source = value;
2436 if (kPoisonHeapReferences) {
2437 // Note that in the case where `value` is a null reference,
2438 // we do not enter this block, as a null reference does not
2439 // need poisoning.
2440 __ Move(temp1, value);
2441 __ PoisonHeapReference(temp1);
2442 source = temp1;
2443 }
2444
2445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2446 if (index.IsConstant()) {
2447 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002449 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Alexey Frunze15958152017-02-09 19:08:30 -08002450 }
2451 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2452
2453 if (!may_need_runtime_call_for_type_check) {
2454 codegen_->MaybeRecordImplicitNullCheck(instruction);
2455 }
2456
2457 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2458
2459 if (done.IsLinked()) {
2460 __ Bind(&done);
2461 }
2462
2463 if (slow_path != nullptr) {
2464 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002465 }
2466 break;
2467 }
2468
2469 case Primitive::kPrimLong: {
2470 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002471 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002472 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002473 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002474 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002475 }
2476 if (value_location.IsConstant()) {
2477 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2478 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2479 } else {
2480 GpuRegister value = value_location.AsRegister<GpuRegister>();
2481 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002482 }
2483 break;
2484 }
2485
2486 case Primitive::kPrimFloat: {
2487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002488 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002489 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002490 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002491 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_4);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002492 }
2493 if (value_location.IsConstant()) {
2494 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2495 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2496 } else {
2497 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2498 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002499 }
2500 break;
2501 }
2502
2503 case Primitive::kPrimDouble: {
2504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002505 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002506 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002507 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002508 __ Dlsa(base_reg, index.AsRegister<GpuRegister>(), obj, TIMES_8);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01002509 }
2510 if (value_location.IsConstant()) {
2511 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2512 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2513 } else {
2514 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
2515 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002516 }
2517 break;
2518 }
2519
2520 case Primitive::kPrimVoid:
2521 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2522 UNREACHABLE();
2523 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524}
2525
2526void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002527 RegisterSet caller_saves = RegisterSet::Empty();
2528 InvokeRuntimeCallingConvention calling_convention;
2529 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2530 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2531 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002532 locations->SetInAt(0, Location::RequiresRegister());
2533 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002534}
2535
2536void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
2537 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002538 BoundsCheckSlowPathMIPS64* slow_path =
2539 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002540 codegen_->AddSlowPath(slow_path);
2541
2542 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
2543 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
2544
2545 // length is limited by the maximum positive signed 32-bit integer.
2546 // Unsigned comparison of length and index checks for index < 0
2547 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002548 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002549}
2550
Alexey Frunze15958152017-02-09 19:08:30 -08002551// Temp is used for read barrier.
2552static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
2553 if (kEmitCompilerReadBarrier &&
2554 (kUseBakerReadBarrier ||
2555 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2556 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2557 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
2558 return 1;
2559 }
2560 return 0;
2561}
2562
2563// Extra temp is used for read barrier.
2564static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
2565 return 1 + NumberOfInstanceOfTemps(type_check_kind);
2566}
2567
Alexey Frunze4dda3372015-06-01 18:31:49 -07002568void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002569 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
2570 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
2571
2572 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
2573 switch (type_check_kind) {
2574 case TypeCheckKind::kExactCheck:
2575 case TypeCheckKind::kAbstractClassCheck:
2576 case TypeCheckKind::kClassHierarchyCheck:
2577 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08002578 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002579 ? LocationSummary::kCallOnSlowPath
2580 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
2581 break;
2582 case TypeCheckKind::kArrayCheck:
2583 case TypeCheckKind::kUnresolvedCheck:
2584 case TypeCheckKind::kInterfaceCheck:
2585 call_kind = LocationSummary::kCallOnSlowPath;
2586 break;
2587 }
2588
2589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002590 locations->SetInAt(0, Location::RequiresRegister());
2591 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08002592 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593}
2594
2595void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002596 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002597 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002598 Location obj_loc = locations->InAt(0);
2599 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002600 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08002601 Location temp_loc = locations->GetTemp(0);
2602 GpuRegister temp = temp_loc.AsRegister<GpuRegister>();
2603 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
2604 DCHECK_LE(num_temps, 2u);
2605 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002606 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2607 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2608 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2609 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2610 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
2611 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
2612 const uint32_t object_array_data_offset =
2613 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2614 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002615
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002616 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
2617 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
2618 // read barriers is done for performance and code size reasons.
2619 bool is_type_check_slow_path_fatal = false;
2620 if (!kEmitCompilerReadBarrier) {
2621 is_type_check_slow_path_fatal =
2622 (type_check_kind == TypeCheckKind::kExactCheck ||
2623 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
2624 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
2625 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
2626 !instruction->CanThrowIntoCatchBlock();
2627 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002628 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002629 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
2630 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002631 codegen_->AddSlowPath(slow_path);
2632
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002633 // Avoid this check if we know `obj` is not null.
2634 if (instruction->MustDoNullCheck()) {
2635 __ Beqzc(obj, &done);
2636 }
2637
2638 switch (type_check_kind) {
2639 case TypeCheckKind::kExactCheck:
2640 case TypeCheckKind::kArrayCheck: {
2641 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002642 GenerateReferenceLoadTwoRegisters(instruction,
2643 temp_loc,
2644 obj_loc,
2645 class_offset,
2646 maybe_temp2_loc,
2647 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002648 // Jump to slow path for throwing the exception or doing a
2649 // more involved array check.
2650 __ Bnec(temp, cls, slow_path->GetEntryLabel());
2651 break;
2652 }
2653
2654 case TypeCheckKind::kAbstractClassCheck: {
2655 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002656 GenerateReferenceLoadTwoRegisters(instruction,
2657 temp_loc,
2658 obj_loc,
2659 class_offset,
2660 maybe_temp2_loc,
2661 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002662 // If the class is abstract, we eagerly fetch the super class of the
2663 // object to avoid doing a comparison we know will fail.
2664 Mips64Label loop;
2665 __ Bind(&loop);
2666 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002667 GenerateReferenceLoadOneRegister(instruction,
2668 temp_loc,
2669 super_offset,
2670 maybe_temp2_loc,
2671 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002672 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2673 // exception.
2674 __ Beqzc(temp, slow_path->GetEntryLabel());
2675 // Otherwise, compare the classes.
2676 __ Bnec(temp, cls, &loop);
2677 break;
2678 }
2679
2680 case TypeCheckKind::kClassHierarchyCheck: {
2681 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002682 GenerateReferenceLoadTwoRegisters(instruction,
2683 temp_loc,
2684 obj_loc,
2685 class_offset,
2686 maybe_temp2_loc,
2687 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002688 // Walk over the class hierarchy to find a match.
2689 Mips64Label loop;
2690 __ Bind(&loop);
2691 __ Beqc(temp, cls, &done);
2692 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08002693 GenerateReferenceLoadOneRegister(instruction,
2694 temp_loc,
2695 super_offset,
2696 maybe_temp2_loc,
2697 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002698 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2699 // exception. Otherwise, jump to the beginning of the loop.
2700 __ Bnezc(temp, &loop);
2701 __ Bc(slow_path->GetEntryLabel());
2702 break;
2703 }
2704
2705 case TypeCheckKind::kArrayObjectCheck: {
2706 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002707 GenerateReferenceLoadTwoRegisters(instruction,
2708 temp_loc,
2709 obj_loc,
2710 class_offset,
2711 maybe_temp2_loc,
2712 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002713 // Do an exact check.
2714 __ Beqc(temp, cls, &done);
2715 // Otherwise, we need to check that the object's class is a non-primitive array.
2716 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08002717 GenerateReferenceLoadOneRegister(instruction,
2718 temp_loc,
2719 component_offset,
2720 maybe_temp2_loc,
2721 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002722 // If the component type is null, jump to the slow path to throw the exception.
2723 __ Beqzc(temp, slow_path->GetEntryLabel());
2724 // Otherwise, the object is indeed an array, further check that this component
2725 // type is not a primitive type.
2726 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2727 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2728 __ Bnezc(temp, slow_path->GetEntryLabel());
2729 break;
2730 }
2731
2732 case TypeCheckKind::kUnresolvedCheck:
2733 // We always go into the type check slow path for the unresolved check case.
2734 // We cannot directly call the CheckCast runtime entry point
2735 // without resorting to a type checking slow path here (i.e. by
2736 // calling InvokeRuntime directly), as it would require to
2737 // assign fixed registers for the inputs of this HInstanceOf
2738 // instruction (following the runtime calling convention), which
2739 // might be cluttered by the potential first read barrier
2740 // emission at the beginning of this method.
2741 __ Bc(slow_path->GetEntryLabel());
2742 break;
2743
2744 case TypeCheckKind::kInterfaceCheck: {
2745 // Avoid read barriers to improve performance of the fast path. We can not get false
2746 // positives by doing this.
2747 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08002748 GenerateReferenceLoadTwoRegisters(instruction,
2749 temp_loc,
2750 obj_loc,
2751 class_offset,
2752 maybe_temp2_loc,
2753 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002754 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08002755 GenerateReferenceLoadTwoRegisters(instruction,
2756 temp_loc,
2757 temp_loc,
2758 iftable_offset,
2759 maybe_temp2_loc,
2760 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08002761 // Iftable is never null.
2762 __ Lw(TMP, temp, array_length_offset);
2763 // Loop through the iftable and check if any class matches.
2764 Mips64Label loop;
2765 __ Bind(&loop);
2766 __ Beqzc(TMP, slow_path->GetEntryLabel());
2767 __ Lwu(AT, temp, object_array_data_offset);
2768 __ MaybeUnpoisonHeapReference(AT);
2769 // Go to next interface.
2770 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2771 __ Addiu(TMP, TMP, -2);
2772 // Compare the classes and continue the loop if they do not match.
2773 __ Bnec(AT, cls, &loop);
2774 break;
2775 }
2776 }
2777
2778 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002779 __ Bind(slow_path->GetExitLabel());
2780}
2781
2782void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2783 LocationSummary* locations =
2784 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2785 locations->SetInAt(0, Location::RequiresRegister());
2786 if (check->HasUses()) {
2787 locations->SetOut(Location::SameAsFirstInput());
2788 }
2789}
2790
2791void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2792 // We assume the class is not null.
2793 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2794 check->GetLoadClass(),
2795 check,
2796 check->GetDexPc(),
2797 true);
2798 codegen_->AddSlowPath(slow_path);
2799 GenerateClassInitializationCheck(slow_path,
2800 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2801}
2802
2803void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2804 Primitive::Type in_type = compare->InputAt(0)->GetType();
2805
Alexey Frunze299a9392015-12-08 16:08:02 -08002806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002807
2808 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002809 case Primitive::kPrimBoolean:
2810 case Primitive::kPrimByte:
2811 case Primitive::kPrimShort:
2812 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002813 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002814 case Primitive::kPrimLong:
2815 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002816 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2818 break;
2819
2820 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002821 case Primitive::kPrimDouble:
2822 locations->SetInAt(0, Location::RequiresFpuRegister());
2823 locations->SetInAt(1, Location::RequiresFpuRegister());
2824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002825 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002826
2827 default:
2828 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2829 }
2830}
2831
2832void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2833 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002834 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002835 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2836
2837 // 0 if: left == right
2838 // 1 if: left > right
2839 // -1 if: left < right
2840 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002841 case Primitive::kPrimBoolean:
2842 case Primitive::kPrimByte:
2843 case Primitive::kPrimShort:
2844 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002845 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002846 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002847 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002848 Location rhs_location = locations->InAt(1);
2849 bool use_imm = rhs_location.IsConstant();
2850 GpuRegister rhs = ZERO;
2851 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002852 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002853 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2854 if (value != 0) {
2855 rhs = AT;
2856 __ LoadConst64(rhs, value);
2857 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002858 } else {
2859 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2860 if (value != 0) {
2861 rhs = AT;
2862 __ LoadConst32(rhs, value);
2863 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002864 }
2865 } else {
2866 rhs = rhs_location.AsRegister<GpuRegister>();
2867 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002868 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002869 __ Slt(res, rhs, lhs);
2870 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002871 break;
2872 }
2873
Alexey Frunze299a9392015-12-08 16:08:02 -08002874 case Primitive::kPrimFloat: {
2875 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2876 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2877 Mips64Label done;
2878 __ CmpEqS(FTMP, lhs, rhs);
2879 __ LoadConst32(res, 0);
2880 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002881 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002882 __ CmpLtS(FTMP, lhs, rhs);
2883 __ LoadConst32(res, -1);
2884 __ Bc1nez(FTMP, &done);
2885 __ LoadConst32(res, 1);
2886 } else {
2887 __ CmpLtS(FTMP, rhs, lhs);
2888 __ LoadConst32(res, 1);
2889 __ Bc1nez(FTMP, &done);
2890 __ LoadConst32(res, -1);
2891 }
2892 __ Bind(&done);
2893 break;
2894 }
2895
Alexey Frunze4dda3372015-06-01 18:31:49 -07002896 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002897 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2898 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2899 Mips64Label done;
2900 __ CmpEqD(FTMP, lhs, rhs);
2901 __ LoadConst32(res, 0);
2902 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002903 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002904 __ CmpLtD(FTMP, lhs, rhs);
2905 __ LoadConst32(res, -1);
2906 __ Bc1nez(FTMP, &done);
2907 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002908 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002909 __ CmpLtD(FTMP, rhs, lhs);
2910 __ LoadConst32(res, 1);
2911 __ Bc1nez(FTMP, &done);
2912 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002914 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002915 break;
2916 }
2917
2918 default:
2919 LOG(FATAL) << "Unimplemented compare type " << in_type;
2920 }
2921}
2922
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002923void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002925 switch (instruction->InputAt(0)->GetType()) {
2926 default:
2927 case Primitive::kPrimLong:
2928 locations->SetInAt(0, Location::RequiresRegister());
2929 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2930 break;
2931
2932 case Primitive::kPrimFloat:
2933 case Primitive::kPrimDouble:
2934 locations->SetInAt(0, Location::RequiresFpuRegister());
2935 locations->SetInAt(1, Location::RequiresFpuRegister());
2936 break;
2937 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002938 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2940 }
2941}
2942
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002943void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002944 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002945 return;
2946 }
2947
Alexey Frunze299a9392015-12-08 16:08:02 -08002948 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002949 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002950 switch (type) {
2951 default:
2952 // Integer case.
2953 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2954 return;
2955 case Primitive::kPrimLong:
2956 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2957 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002958 case Primitive::kPrimFloat:
2959 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002960 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2961 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002962 }
2963}
2964
Alexey Frunzec857c742015-09-23 15:12:39 -07002965void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2966 DCHECK(instruction->IsDiv() || instruction->IsRem());
2967 Primitive::Type type = instruction->GetResultType();
2968
2969 LocationSummary* locations = instruction->GetLocations();
2970 Location second = locations->InAt(1);
2971 DCHECK(second.IsConstant());
2972
2973 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2974 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2975 int64_t imm = Int64FromConstant(second.GetConstant());
2976 DCHECK(imm == 1 || imm == -1);
2977
2978 if (instruction->IsRem()) {
2979 __ Move(out, ZERO);
2980 } else {
2981 if (imm == -1) {
2982 if (type == Primitive::kPrimInt) {
2983 __ Subu(out, ZERO, dividend);
2984 } else {
2985 DCHECK_EQ(type, Primitive::kPrimLong);
2986 __ Dsubu(out, ZERO, dividend);
2987 }
2988 } else if (out != dividend) {
2989 __ Move(out, dividend);
2990 }
2991 }
2992}
2993
2994void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2995 DCHECK(instruction->IsDiv() || instruction->IsRem());
2996 Primitive::Type type = instruction->GetResultType();
2997
2998 LocationSummary* locations = instruction->GetLocations();
2999 Location second = locations->InAt(1);
3000 DCHECK(second.IsConstant());
3001
3002 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3003 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3004 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003005 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07003006 int ctz_imm = CTZ(abs_imm);
3007
3008 if (instruction->IsDiv()) {
3009 if (type == Primitive::kPrimInt) {
3010 if (ctz_imm == 1) {
3011 // Fast path for division by +/-2, which is very common.
3012 __ Srl(TMP, dividend, 31);
3013 } else {
3014 __ Sra(TMP, dividend, 31);
3015 __ Srl(TMP, TMP, 32 - ctz_imm);
3016 }
3017 __ Addu(out, dividend, TMP);
3018 __ Sra(out, out, ctz_imm);
3019 if (imm < 0) {
3020 __ Subu(out, ZERO, out);
3021 }
3022 } else {
3023 DCHECK_EQ(type, Primitive::kPrimLong);
3024 if (ctz_imm == 1) {
3025 // Fast path for division by +/-2, which is very common.
3026 __ Dsrl32(TMP, dividend, 31);
3027 } else {
3028 __ Dsra32(TMP, dividend, 31);
3029 if (ctz_imm > 32) {
3030 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3031 } else {
3032 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3033 }
3034 }
3035 __ Daddu(out, dividend, TMP);
3036 if (ctz_imm < 32) {
3037 __ Dsra(out, out, ctz_imm);
3038 } else {
3039 __ Dsra32(out, out, ctz_imm - 32);
3040 }
3041 if (imm < 0) {
3042 __ Dsubu(out, ZERO, out);
3043 }
3044 }
3045 } else {
3046 if (type == Primitive::kPrimInt) {
3047 if (ctz_imm == 1) {
3048 // Fast path for modulo +/-2, which is very common.
3049 __ Sra(TMP, dividend, 31);
3050 __ Subu(out, dividend, TMP);
3051 __ Andi(out, out, 1);
3052 __ Addu(out, out, TMP);
3053 } else {
3054 __ Sra(TMP, dividend, 31);
3055 __ Srl(TMP, TMP, 32 - ctz_imm);
3056 __ Addu(out, dividend, TMP);
3057 if (IsUint<16>(abs_imm - 1)) {
3058 __ Andi(out, out, abs_imm - 1);
3059 } else {
3060 __ Sll(out, out, 32 - ctz_imm);
3061 __ Srl(out, out, 32 - ctz_imm);
3062 }
3063 __ Subu(out, out, TMP);
3064 }
3065 } else {
3066 DCHECK_EQ(type, Primitive::kPrimLong);
3067 if (ctz_imm == 1) {
3068 // Fast path for modulo +/-2, which is very common.
3069 __ Dsra32(TMP, dividend, 31);
3070 __ Dsubu(out, dividend, TMP);
3071 __ Andi(out, out, 1);
3072 __ Daddu(out, out, TMP);
3073 } else {
3074 __ Dsra32(TMP, dividend, 31);
3075 if (ctz_imm > 32) {
3076 __ Dsrl(TMP, TMP, 64 - ctz_imm);
3077 } else {
3078 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
3079 }
3080 __ Daddu(out, dividend, TMP);
3081 if (IsUint<16>(abs_imm - 1)) {
3082 __ Andi(out, out, abs_imm - 1);
3083 } else {
3084 if (ctz_imm > 32) {
3085 __ Dsll(out, out, 64 - ctz_imm);
3086 __ Dsrl(out, out, 64 - ctz_imm);
3087 } else {
3088 __ Dsll32(out, out, 32 - ctz_imm);
3089 __ Dsrl32(out, out, 32 - ctz_imm);
3090 }
3091 }
3092 __ Dsubu(out, out, TMP);
3093 }
3094 }
3095 }
3096}
3097
3098void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3099 DCHECK(instruction->IsDiv() || instruction->IsRem());
3100
3101 LocationSummary* locations = instruction->GetLocations();
3102 Location second = locations->InAt(1);
3103 DCHECK(second.IsConstant());
3104
3105 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3106 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3107 int64_t imm = Int64FromConstant(second.GetConstant());
3108
3109 Primitive::Type type = instruction->GetResultType();
3110 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3111
3112 int64_t magic;
3113 int shift;
3114 CalculateMagicAndShiftForDivRem(imm,
3115 (type == Primitive::kPrimLong),
3116 &magic,
3117 &shift);
3118
3119 if (type == Primitive::kPrimInt) {
3120 __ LoadConst32(TMP, magic);
3121 __ MuhR6(TMP, dividend, TMP);
3122
3123 if (imm > 0 && magic < 0) {
3124 __ Addu(TMP, TMP, dividend);
3125 } else if (imm < 0 && magic > 0) {
3126 __ Subu(TMP, TMP, dividend);
3127 }
3128
3129 if (shift != 0) {
3130 __ Sra(TMP, TMP, shift);
3131 }
3132
3133 if (instruction->IsDiv()) {
3134 __ Sra(out, TMP, 31);
3135 __ Subu(out, TMP, out);
3136 } else {
3137 __ Sra(AT, TMP, 31);
3138 __ Subu(AT, TMP, AT);
3139 __ LoadConst32(TMP, imm);
3140 __ MulR6(TMP, AT, TMP);
3141 __ Subu(out, dividend, TMP);
3142 }
3143 } else {
3144 __ LoadConst64(TMP, magic);
3145 __ Dmuh(TMP, dividend, TMP);
3146
3147 if (imm > 0 && magic < 0) {
3148 __ Daddu(TMP, TMP, dividend);
3149 } else if (imm < 0 && magic > 0) {
3150 __ Dsubu(TMP, TMP, dividend);
3151 }
3152
3153 if (shift >= 32) {
3154 __ Dsra32(TMP, TMP, shift - 32);
3155 } else if (shift > 0) {
3156 __ Dsra(TMP, TMP, shift);
3157 }
3158
3159 if (instruction->IsDiv()) {
3160 __ Dsra32(out, TMP, 31);
3161 __ Dsubu(out, TMP, out);
3162 } else {
3163 __ Dsra32(AT, TMP, 31);
3164 __ Dsubu(AT, TMP, AT);
3165 __ LoadConst64(TMP, imm);
3166 __ Dmul(TMP, AT, TMP);
3167 __ Dsubu(out, dividend, TMP);
3168 }
3169 }
3170}
3171
3172void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3173 DCHECK(instruction->IsDiv() || instruction->IsRem());
3174 Primitive::Type type = instruction->GetResultType();
3175 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
3176
3177 LocationSummary* locations = instruction->GetLocations();
3178 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3179 Location second = locations->InAt(1);
3180
3181 if (second.IsConstant()) {
3182 int64_t imm = Int64FromConstant(second.GetConstant());
3183 if (imm == 0) {
3184 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3185 } else if (imm == 1 || imm == -1) {
3186 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003187 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07003188 DivRemByPowerOfTwo(instruction);
3189 } else {
3190 DCHECK(imm <= -2 || imm >= 2);
3191 GenerateDivRemWithAnyConstant(instruction);
3192 }
3193 } else {
3194 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
3195 GpuRegister divisor = second.AsRegister<GpuRegister>();
3196 if (instruction->IsDiv()) {
3197 if (type == Primitive::kPrimInt)
3198 __ DivR6(out, dividend, divisor);
3199 else
3200 __ Ddiv(out, dividend, divisor);
3201 } else {
3202 if (type == Primitive::kPrimInt)
3203 __ ModR6(out, dividend, divisor);
3204 else
3205 __ Dmod(out, dividend, divisor);
3206 }
3207 }
3208}
3209
Alexey Frunze4dda3372015-06-01 18:31:49 -07003210void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
3211 LocationSummary* locations =
3212 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3213 switch (div->GetResultType()) {
3214 case Primitive::kPrimInt:
3215 case Primitive::kPrimLong:
3216 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003217 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003218 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3219 break;
3220
3221 case Primitive::kPrimFloat:
3222 case Primitive::kPrimDouble:
3223 locations->SetInAt(0, Location::RequiresFpuRegister());
3224 locations->SetInAt(1, Location::RequiresFpuRegister());
3225 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3226 break;
3227
3228 default:
3229 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3230 }
3231}
3232
3233void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
3234 Primitive::Type type = instruction->GetType();
3235 LocationSummary* locations = instruction->GetLocations();
3236
3237 switch (type) {
3238 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003239 case Primitive::kPrimLong:
3240 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003242 case Primitive::kPrimFloat:
3243 case Primitive::kPrimDouble: {
3244 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3245 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3246 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3247 if (type == Primitive::kPrimFloat)
3248 __ DivS(dst, lhs, rhs);
3249 else
3250 __ DivD(dst, lhs, rhs);
3251 break;
3252 }
3253 default:
3254 LOG(FATAL) << "Unexpected div type " << type;
3255 }
3256}
3257
3258void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003259 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003260 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261}
3262
3263void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3264 SlowPathCodeMIPS64* slow_path =
3265 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
3266 codegen_->AddSlowPath(slow_path);
3267 Location value = instruction->GetLocations()->InAt(0);
3268
3269 Primitive::Type type = instruction->GetType();
3270
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003271 if (!Primitive::IsIntegralType(type)) {
3272 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003273 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003274 }
3275
3276 if (value.IsConstant()) {
3277 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
3278 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003279 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003280 } else {
3281 // A division by a non-null constant is valid. We don't need to perform
3282 // any check, so simply fall through.
3283 }
3284 } else {
3285 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3286 }
3287}
3288
3289void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
3290 LocationSummary* locations =
3291 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3292 locations->SetOut(Location::ConstantLocation(constant));
3293}
3294
3295void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3296 // Will be generated at use site.
3297}
3298
3299void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
3300 exit->SetLocations(nullptr);
3301}
3302
3303void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3304}
3305
3306void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
3307 LocationSummary* locations =
3308 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3309 locations->SetOut(Location::ConstantLocation(constant));
3310}
3311
3312void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3313 // Will be generated at use site.
3314}
3315
David Brazdilfc6a86a2015-06-26 10:33:45 +00003316void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003317 DCHECK(!successor->IsExitBlock());
3318 HBasicBlock* block = got->GetBlock();
3319 HInstruction* previous = got->GetPrevious();
3320 HLoopInformation* info = block->GetLoopInformation();
3321
3322 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3323 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3324 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3325 return;
3326 }
3327 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3328 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3329 }
3330 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003331 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003332 }
3333}
3334
David Brazdilfc6a86a2015-06-26 10:33:45 +00003335void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
3336 got->SetLocations(nullptr);
3337}
3338
3339void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
3340 HandleGoto(got, got->GetSuccessor());
3341}
3342
3343void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3344 try_boundary->SetLocations(nullptr);
3345}
3346
3347void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
3348 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3349 if (!successor->IsExitBlock()) {
3350 HandleGoto(try_boundary, successor);
3351 }
3352}
3353
Alexey Frunze299a9392015-12-08 16:08:02 -08003354void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
3355 bool is64bit,
3356 LocationSummary* locations) {
3357 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3358 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3359 Location rhs_location = locations->InAt(1);
3360 GpuRegister rhs_reg = ZERO;
3361 int64_t rhs_imm = 0;
3362 bool use_imm = rhs_location.IsConstant();
3363 if (use_imm) {
3364 if (is64bit) {
3365 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3366 } else {
3367 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3368 }
3369 } else {
3370 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3371 }
3372 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
3373
3374 switch (cond) {
3375 case kCondEQ:
3376 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003377 if (use_imm && IsInt<16>(-rhs_imm)) {
3378 if (rhs_imm == 0) {
3379 if (cond == kCondEQ) {
3380 __ Sltiu(dst, lhs, 1);
3381 } else {
3382 __ Sltu(dst, ZERO, lhs);
3383 }
3384 } else {
3385 if (is64bit) {
3386 __ Daddiu(dst, lhs, -rhs_imm);
3387 } else {
3388 __ Addiu(dst, lhs, -rhs_imm);
3389 }
3390 if (cond == kCondEQ) {
3391 __ Sltiu(dst, dst, 1);
3392 } else {
3393 __ Sltu(dst, ZERO, dst);
3394 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003395 }
Alexey Frunze299a9392015-12-08 16:08:02 -08003396 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01003397 if (use_imm && IsUint<16>(rhs_imm)) {
3398 __ Xori(dst, lhs, rhs_imm);
3399 } else {
3400 if (use_imm) {
3401 rhs_reg = TMP;
3402 __ LoadConst64(rhs_reg, rhs_imm);
3403 }
3404 __ Xor(dst, lhs, rhs_reg);
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 }
3412 break;
3413
3414 case kCondLT:
3415 case kCondGE:
3416 if (use_imm && IsInt<16>(rhs_imm)) {
3417 __ Slti(dst, lhs, rhs_imm);
3418 } else {
3419 if (use_imm) {
3420 rhs_reg = TMP;
3421 __ LoadConst64(rhs_reg, rhs_imm);
3422 }
3423 __ Slt(dst, lhs, rhs_reg);
3424 }
3425 if (cond == kCondGE) {
3426 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3427 // only the slt instruction but no sge.
3428 __ Xori(dst, dst, 1);
3429 }
3430 break;
3431
3432 case kCondLE:
3433 case kCondGT:
3434 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
3435 // Simulate lhs <= rhs via lhs < rhs + 1.
3436 __ Slti(dst, lhs, rhs_imm_plus_one);
3437 if (cond == kCondGT) {
3438 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3439 // only the slti instruction but no sgti.
3440 __ Xori(dst, dst, 1);
3441 }
3442 } else {
3443 if (use_imm) {
3444 rhs_reg = TMP;
3445 __ LoadConst64(rhs_reg, rhs_imm);
3446 }
3447 __ Slt(dst, rhs_reg, lhs);
3448 if (cond == kCondLE) {
3449 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3450 // only the slt instruction but no sle.
3451 __ Xori(dst, dst, 1);
3452 }
3453 }
3454 break;
3455
3456 case kCondB:
3457 case kCondAE:
3458 if (use_imm && IsInt<16>(rhs_imm)) {
3459 // Sltiu sign-extends its 16-bit immediate operand before
3460 // the comparison and thus lets us compare directly with
3461 // unsigned values in the ranges [0, 0x7fff] and
3462 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3463 __ Sltiu(dst, lhs, rhs_imm);
3464 } else {
3465 if (use_imm) {
3466 rhs_reg = TMP;
3467 __ LoadConst64(rhs_reg, rhs_imm);
3468 }
3469 __ Sltu(dst, lhs, rhs_reg);
3470 }
3471 if (cond == kCondAE) {
3472 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3473 // only the sltu instruction but no sgeu.
3474 __ Xori(dst, dst, 1);
3475 }
3476 break;
3477
3478 case kCondBE:
3479 case kCondA:
3480 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
3481 // Simulate lhs <= rhs via lhs < rhs + 1.
3482 // Note that this only works if rhs + 1 does not overflow
3483 // to 0, hence the check above.
3484 // Sltiu sign-extends its 16-bit immediate operand before
3485 // the comparison and thus lets us compare directly with
3486 // unsigned values in the ranges [0, 0x7fff] and
3487 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
3488 __ Sltiu(dst, lhs, rhs_imm_plus_one);
3489 if (cond == kCondA) {
3490 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3491 // only the sltiu instruction but no sgtiu.
3492 __ Xori(dst, dst, 1);
3493 }
3494 } else {
3495 if (use_imm) {
3496 rhs_reg = TMP;
3497 __ LoadConst64(rhs_reg, rhs_imm);
3498 }
3499 __ Sltu(dst, rhs_reg, lhs);
3500 if (cond == kCondBE) {
3501 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3502 // only the sltu instruction but no sleu.
3503 __ Xori(dst, dst, 1);
3504 }
3505 }
3506 break;
3507 }
3508}
3509
3510void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
3511 bool is64bit,
3512 LocationSummary* locations,
3513 Mips64Label* label) {
3514 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3515 Location rhs_location = locations->InAt(1);
3516 GpuRegister rhs_reg = ZERO;
3517 int64_t rhs_imm = 0;
3518 bool use_imm = rhs_location.IsConstant();
3519 if (use_imm) {
3520 if (is64bit) {
3521 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
3522 } else {
3523 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3524 }
3525 } else {
3526 rhs_reg = rhs_location.AsRegister<GpuRegister>();
3527 }
3528
3529 if (use_imm && rhs_imm == 0) {
3530 switch (cond) {
3531 case kCondEQ:
3532 case kCondBE: // <= 0 if zero
3533 __ Beqzc(lhs, label);
3534 break;
3535 case kCondNE:
3536 case kCondA: // > 0 if non-zero
3537 __ Bnezc(lhs, label);
3538 break;
3539 case kCondLT:
3540 __ Bltzc(lhs, label);
3541 break;
3542 case kCondGE:
3543 __ Bgezc(lhs, label);
3544 break;
3545 case kCondLE:
3546 __ Blezc(lhs, label);
3547 break;
3548 case kCondGT:
3549 __ Bgtzc(lhs, label);
3550 break;
3551 case kCondB: // always false
3552 break;
3553 case kCondAE: // always true
3554 __ Bc(label);
3555 break;
3556 }
3557 } else {
3558 if (use_imm) {
3559 rhs_reg = TMP;
3560 __ LoadConst64(rhs_reg, rhs_imm);
3561 }
3562 switch (cond) {
3563 case kCondEQ:
3564 __ Beqc(lhs, rhs_reg, label);
3565 break;
3566 case kCondNE:
3567 __ Bnec(lhs, rhs_reg, label);
3568 break;
3569 case kCondLT:
3570 __ Bltc(lhs, rhs_reg, label);
3571 break;
3572 case kCondGE:
3573 __ Bgec(lhs, rhs_reg, label);
3574 break;
3575 case kCondLE:
3576 __ Bgec(rhs_reg, lhs, label);
3577 break;
3578 case kCondGT:
3579 __ Bltc(rhs_reg, lhs, label);
3580 break;
3581 case kCondB:
3582 __ Bltuc(lhs, rhs_reg, label);
3583 break;
3584 case kCondAE:
3585 __ Bgeuc(lhs, rhs_reg, label);
3586 break;
3587 case kCondBE:
3588 __ Bgeuc(rhs_reg, lhs, label);
3589 break;
3590 case kCondA:
3591 __ Bltuc(rhs_reg, lhs, label);
3592 break;
3593 }
3594 }
3595}
3596
Tijana Jakovljevic43758192016-12-30 09:23:01 +01003597void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
3598 bool gt_bias,
3599 Primitive::Type type,
3600 LocationSummary* locations) {
3601 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3602 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3603 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3604 if (type == Primitive::kPrimFloat) {
3605 switch (cond) {
3606 case kCondEQ:
3607 __ CmpEqS(FTMP, lhs, rhs);
3608 __ Mfc1(dst, FTMP);
3609 __ Andi(dst, dst, 1);
3610 break;
3611 case kCondNE:
3612 __ CmpEqS(FTMP, lhs, rhs);
3613 __ Mfc1(dst, FTMP);
3614 __ Addiu(dst, dst, 1);
3615 break;
3616 case kCondLT:
3617 if (gt_bias) {
3618 __ CmpLtS(FTMP, lhs, rhs);
3619 } else {
3620 __ CmpUltS(FTMP, lhs, rhs);
3621 }
3622 __ Mfc1(dst, FTMP);
3623 __ Andi(dst, dst, 1);
3624 break;
3625 case kCondLE:
3626 if (gt_bias) {
3627 __ CmpLeS(FTMP, lhs, rhs);
3628 } else {
3629 __ CmpUleS(FTMP, lhs, rhs);
3630 }
3631 __ Mfc1(dst, FTMP);
3632 __ Andi(dst, dst, 1);
3633 break;
3634 case kCondGT:
3635 if (gt_bias) {
3636 __ CmpUltS(FTMP, rhs, lhs);
3637 } else {
3638 __ CmpLtS(FTMP, rhs, lhs);
3639 }
3640 __ Mfc1(dst, FTMP);
3641 __ Andi(dst, dst, 1);
3642 break;
3643 case kCondGE:
3644 if (gt_bias) {
3645 __ CmpUleS(FTMP, rhs, lhs);
3646 } else {
3647 __ CmpLeS(FTMP, rhs, lhs);
3648 }
3649 __ Mfc1(dst, FTMP);
3650 __ Andi(dst, dst, 1);
3651 break;
3652 default:
3653 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3654 UNREACHABLE();
3655 }
3656 } else {
3657 DCHECK_EQ(type, Primitive::kPrimDouble);
3658 switch (cond) {
3659 case kCondEQ:
3660 __ CmpEqD(FTMP, lhs, rhs);
3661 __ Mfc1(dst, FTMP);
3662 __ Andi(dst, dst, 1);
3663 break;
3664 case kCondNE:
3665 __ CmpEqD(FTMP, lhs, rhs);
3666 __ Mfc1(dst, FTMP);
3667 __ Addiu(dst, dst, 1);
3668 break;
3669 case kCondLT:
3670 if (gt_bias) {
3671 __ CmpLtD(FTMP, lhs, rhs);
3672 } else {
3673 __ CmpUltD(FTMP, lhs, rhs);
3674 }
3675 __ Mfc1(dst, FTMP);
3676 __ Andi(dst, dst, 1);
3677 break;
3678 case kCondLE:
3679 if (gt_bias) {
3680 __ CmpLeD(FTMP, lhs, rhs);
3681 } else {
3682 __ CmpUleD(FTMP, lhs, rhs);
3683 }
3684 __ Mfc1(dst, FTMP);
3685 __ Andi(dst, dst, 1);
3686 break;
3687 case kCondGT:
3688 if (gt_bias) {
3689 __ CmpUltD(FTMP, rhs, lhs);
3690 } else {
3691 __ CmpLtD(FTMP, rhs, lhs);
3692 }
3693 __ Mfc1(dst, FTMP);
3694 __ Andi(dst, dst, 1);
3695 break;
3696 case kCondGE:
3697 if (gt_bias) {
3698 __ CmpUleD(FTMP, rhs, lhs);
3699 } else {
3700 __ CmpLeD(FTMP, rhs, lhs);
3701 }
3702 __ Mfc1(dst, FTMP);
3703 __ Andi(dst, dst, 1);
3704 break;
3705 default:
3706 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3707 UNREACHABLE();
3708 }
3709 }
3710}
3711
Alexey Frunze299a9392015-12-08 16:08:02 -08003712void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3713 bool gt_bias,
3714 Primitive::Type type,
3715 LocationSummary* locations,
3716 Mips64Label* label) {
3717 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3718 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3719 if (type == Primitive::kPrimFloat) {
3720 switch (cond) {
3721 case kCondEQ:
3722 __ CmpEqS(FTMP, lhs, rhs);
3723 __ Bc1nez(FTMP, label);
3724 break;
3725 case kCondNE:
3726 __ CmpEqS(FTMP, lhs, rhs);
3727 __ Bc1eqz(FTMP, label);
3728 break;
3729 case kCondLT:
3730 if (gt_bias) {
3731 __ CmpLtS(FTMP, lhs, rhs);
3732 } else {
3733 __ CmpUltS(FTMP, lhs, rhs);
3734 }
3735 __ Bc1nez(FTMP, label);
3736 break;
3737 case kCondLE:
3738 if (gt_bias) {
3739 __ CmpLeS(FTMP, lhs, rhs);
3740 } else {
3741 __ CmpUleS(FTMP, lhs, rhs);
3742 }
3743 __ Bc1nez(FTMP, label);
3744 break;
3745 case kCondGT:
3746 if (gt_bias) {
3747 __ CmpUltS(FTMP, rhs, lhs);
3748 } else {
3749 __ CmpLtS(FTMP, rhs, lhs);
3750 }
3751 __ Bc1nez(FTMP, label);
3752 break;
3753 case kCondGE:
3754 if (gt_bias) {
3755 __ CmpUleS(FTMP, rhs, lhs);
3756 } else {
3757 __ CmpLeS(FTMP, rhs, lhs);
3758 }
3759 __ Bc1nez(FTMP, label);
3760 break;
3761 default:
3762 LOG(FATAL) << "Unexpected non-floating-point condition";
3763 }
3764 } else {
3765 DCHECK_EQ(type, Primitive::kPrimDouble);
3766 switch (cond) {
3767 case kCondEQ:
3768 __ CmpEqD(FTMP, lhs, rhs);
3769 __ Bc1nez(FTMP, label);
3770 break;
3771 case kCondNE:
3772 __ CmpEqD(FTMP, lhs, rhs);
3773 __ Bc1eqz(FTMP, label);
3774 break;
3775 case kCondLT:
3776 if (gt_bias) {
3777 __ CmpLtD(FTMP, lhs, rhs);
3778 } else {
3779 __ CmpUltD(FTMP, lhs, rhs);
3780 }
3781 __ Bc1nez(FTMP, label);
3782 break;
3783 case kCondLE:
3784 if (gt_bias) {
3785 __ CmpLeD(FTMP, lhs, rhs);
3786 } else {
3787 __ CmpUleD(FTMP, lhs, rhs);
3788 }
3789 __ Bc1nez(FTMP, label);
3790 break;
3791 case kCondGT:
3792 if (gt_bias) {
3793 __ CmpUltD(FTMP, rhs, lhs);
3794 } else {
3795 __ CmpLtD(FTMP, rhs, lhs);
3796 }
3797 __ Bc1nez(FTMP, label);
3798 break;
3799 case kCondGE:
3800 if (gt_bias) {
3801 __ CmpUleD(FTMP, rhs, lhs);
3802 } else {
3803 __ CmpLeD(FTMP, rhs, lhs);
3804 }
3805 __ Bc1nez(FTMP, label);
3806 break;
3807 default:
3808 LOG(FATAL) << "Unexpected non-floating-point condition";
3809 }
3810 }
3811}
3812
Alexey Frunze4dda3372015-06-01 18:31:49 -07003813void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003814 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003815 Mips64Label* true_target,
3816 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003817 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003818
David Brazdil0debae72015-11-12 18:37:00 +00003819 if (true_target == nullptr && false_target == nullptr) {
3820 // Nothing to do. The code always falls through.
3821 return;
3822 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003823 // Constant condition, statically compared against "true" (integer value 1).
3824 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003825 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003826 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003827 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003828 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003829 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003830 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003831 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003832 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003833 }
David Brazdil0debae72015-11-12 18:37:00 +00003834 return;
3835 }
3836
3837 // The following code generates these patterns:
3838 // (1) true_target == nullptr && false_target != nullptr
3839 // - opposite condition true => branch to false_target
3840 // (2) true_target != nullptr && false_target == nullptr
3841 // - condition true => branch to true_target
3842 // (3) true_target != nullptr && false_target != nullptr
3843 // - condition true => branch to true_target
3844 // - branch to false_target
3845 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003846 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003847 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003848 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003849 if (true_target == nullptr) {
3850 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3851 } else {
3852 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3853 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003854 } else {
3855 // The condition instruction has not been materialized, use its inputs as
3856 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003857 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003858 Primitive::Type type = condition->InputAt(0)->GetType();
3859 LocationSummary* locations = cond->GetLocations();
3860 IfCondition if_cond = condition->GetCondition();
3861 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003862
David Brazdil0debae72015-11-12 18:37:00 +00003863 if (true_target == nullptr) {
3864 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003865 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003866 }
3867
Alexey Frunze299a9392015-12-08 16:08:02 -08003868 switch (type) {
3869 default:
3870 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3871 break;
3872 case Primitive::kPrimLong:
3873 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3874 break;
3875 case Primitive::kPrimFloat:
3876 case Primitive::kPrimDouble:
3877 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3878 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003879 }
3880 }
David Brazdil0debae72015-11-12 18:37:00 +00003881
3882 // If neither branch falls through (case 3), the conditional branch to `true_target`
3883 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3884 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003885 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003886 }
3887}
3888
3889void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003891 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003892 locations->SetInAt(0, Location::RequiresRegister());
3893 }
3894}
3895
3896void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003897 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3898 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003899 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003900 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003901 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003902 nullptr : codegen_->GetLabelOf(false_successor);
3903 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003904}
3905
3906void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3907 LocationSummary* locations = new (GetGraph()->GetArena())
3908 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003909 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003910 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003911 locations->SetInAt(0, Location::RequiresRegister());
3912 }
3913}
3914
3915void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003916 SlowPathCodeMIPS64* slow_path =
3917 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003918 GenerateTestAndBranch(deoptimize,
3919 /* condition_input_index */ 0,
3920 slow_path->GetEntryLabel(),
3921 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003922}
3923
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003924void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3925 LocationSummary* locations = new (GetGraph()->GetArena())
3926 LocationSummary(flag, LocationSummary::kNoCall);
3927 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003928}
3929
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003930void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3931 __ LoadFromOffset(kLoadWord,
3932 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3933 SP,
3934 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003935}
3936
David Brazdil74eb1b22015-12-14 11:44:01 +00003937void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3939 if (Primitive::IsFloatingPointType(select->GetType())) {
3940 locations->SetInAt(0, Location::RequiresFpuRegister());
3941 locations->SetInAt(1, Location::RequiresFpuRegister());
3942 } else {
3943 locations->SetInAt(0, Location::RequiresRegister());
3944 locations->SetInAt(1, Location::RequiresRegister());
3945 }
3946 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3947 locations->SetInAt(2, Location::RequiresRegister());
3948 }
3949 locations->SetOut(Location::SameAsFirstInput());
3950}
3951
3952void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3953 LocationSummary* locations = select->GetLocations();
3954 Mips64Label false_target;
3955 GenerateTestAndBranch(select,
3956 /* condition_input_index */ 2,
3957 /* true_target */ nullptr,
3958 &false_target);
3959 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3960 __ Bind(&false_target);
3961}
3962
David Srbecky0cf44932015-12-09 14:09:59 +00003963void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3964 new (GetGraph()->GetArena()) LocationSummary(info);
3965}
3966
David Srbeckyd28f4a02016-03-14 17:14:24 +00003967void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3968 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003969}
3970
3971void CodeGeneratorMIPS64::GenerateNop() {
3972 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003973}
3974
Alexey Frunze4dda3372015-06-01 18:31:49 -07003975void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003976 const FieldInfo& field_info) {
3977 Primitive::Type field_type = field_info.GetFieldType();
3978 bool object_field_get_with_read_barrier =
3979 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
3980 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3981 instruction,
3982 object_field_get_with_read_barrier
3983 ? LocationSummary::kCallOnSlowPath
3984 : LocationSummary::kNoCall);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003985 locations->SetInAt(0, Location::RequiresRegister());
3986 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3987 locations->SetOut(Location::RequiresFpuRegister());
3988 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08003989 // The output overlaps in the case of an object field get with
3990 // read barriers enabled: we do not want the move to overwrite the
3991 // object's location, as we need it to emit the read barrier.
3992 locations->SetOut(Location::RequiresRegister(),
3993 object_field_get_with_read_barrier
3994 ? Location::kOutputOverlap
3995 : Location::kNoOutputOverlap);
3996 }
3997 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
3998 // We need a temporary register for the read barrier marking slow
3999 // path in CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier.
4000 locations->AddTemp(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004001 }
4002}
4003
4004void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
4005 const FieldInfo& field_info) {
4006 Primitive::Type type = field_info.GetFieldType();
4007 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004008 Location obj_loc = locations->InAt(0);
4009 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
4010 Location dst_loc = locations->Out();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004011 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004012 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004013 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004014 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4015
Alexey Frunze4dda3372015-06-01 18:31:49 -07004016 switch (type) {
4017 case Primitive::kPrimBoolean:
4018 load_type = kLoadUnsignedByte;
4019 break;
4020 case Primitive::kPrimByte:
4021 load_type = kLoadSignedByte;
4022 break;
4023 case Primitive::kPrimShort:
4024 load_type = kLoadSignedHalfword;
4025 break;
4026 case Primitive::kPrimChar:
4027 load_type = kLoadUnsignedHalfword;
4028 break;
4029 case Primitive::kPrimInt:
4030 case Primitive::kPrimFloat:
4031 load_type = kLoadWord;
4032 break;
4033 case Primitive::kPrimLong:
4034 case Primitive::kPrimDouble:
4035 load_type = kLoadDoubleword;
4036 break;
4037 case Primitive::kPrimNot:
4038 load_type = kLoadUnsignedWord;
4039 break;
4040 case Primitive::kPrimVoid:
4041 LOG(FATAL) << "Unreachable type " << type;
4042 UNREACHABLE();
4043 }
4044 if (!Primitive::IsFloatingPointType(type)) {
Alexey Frunze15958152017-02-09 19:08:30 -08004045 DCHECK(dst_loc.IsRegister());
4046 GpuRegister dst = dst_loc.AsRegister<GpuRegister>();
4047 if (type == Primitive::kPrimNot) {
4048 // /* HeapReference<Object> */ dst = *(obj + offset)
4049 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4050 Location temp_loc = locations->GetTemp(0);
4051 // Note that a potential implicit null check is handled in this
4052 // CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier call.
4053 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4054 dst_loc,
4055 obj,
4056 offset,
4057 temp_loc,
4058 /* needs_null_check */ true);
4059 if (is_volatile) {
4060 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4061 }
4062 } else {
4063 __ LoadFromOffset(kLoadUnsignedWord, dst, obj, offset, null_checker);
4064 if (is_volatile) {
4065 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4066 }
4067 // If read barriers are enabled, emit read barriers other than
4068 // Baker's using a slow path (and also unpoison the loaded
4069 // reference, if heap poisoning is enabled).
4070 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
4071 }
4072 } else {
4073 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
4074 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004075 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08004076 DCHECK(dst_loc.IsFpuRegister());
4077 FpuRegister dst = dst_loc.AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004078 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004079 }
Alexey Frunzec061de12017-02-14 13:27:23 -08004080
Alexey Frunze15958152017-02-09 19:08:30 -08004081 // Memory barriers, in the case of references, are handled in the
4082 // previous switch statement.
4083 if (is_volatile && (type != Primitive::kPrimNot)) {
4084 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Alexey Frunzec061de12017-02-14 13:27:23 -08004085 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004086}
4087
4088void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
4089 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
4090 LocationSummary* locations =
4091 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4092 locations->SetInAt(0, Location::RequiresRegister());
4093 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004094 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004095 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004096 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004097 }
4098}
4099
4100void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004101 const FieldInfo& field_info,
4102 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004103 Primitive::Type type = field_info.GetFieldType();
4104 LocationSummary* locations = instruction->GetLocations();
4105 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004106 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107 StoreOperandType store_type = kStoreByte;
Alexey Frunze15958152017-02-09 19:08:30 -08004108 bool is_volatile = field_info.IsVolatile();
Alexey Frunzec061de12017-02-14 13:27:23 -08004109 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4110 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01004111 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
4112
Alexey Frunze4dda3372015-06-01 18:31:49 -07004113 switch (type) {
4114 case Primitive::kPrimBoolean:
4115 case Primitive::kPrimByte:
4116 store_type = kStoreByte;
4117 break;
4118 case Primitive::kPrimShort:
4119 case Primitive::kPrimChar:
4120 store_type = kStoreHalfword;
4121 break;
4122 case Primitive::kPrimInt:
4123 case Primitive::kPrimFloat:
4124 case Primitive::kPrimNot:
4125 store_type = kStoreWord;
4126 break;
4127 case Primitive::kPrimLong:
4128 case Primitive::kPrimDouble:
4129 store_type = kStoreDoubleword;
4130 break;
4131 case Primitive::kPrimVoid:
4132 LOG(FATAL) << "Unreachable type " << type;
4133 UNREACHABLE();
4134 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004135
Alexey Frunze15958152017-02-09 19:08:30 -08004136 if (is_volatile) {
4137 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4138 }
4139
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004140 if (value_location.IsConstant()) {
4141 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4142 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4143 } else {
4144 if (!Primitive::IsFloatingPointType(type)) {
4145 DCHECK(value_location.IsRegister());
4146 GpuRegister src = value_location.AsRegister<GpuRegister>();
4147 if (kPoisonHeapReferences && needs_write_barrier) {
4148 // Note that in the case where `value` is a null reference,
4149 // we do not enter this block, as a null reference does not
4150 // need poisoning.
4151 DCHECK_EQ(type, Primitive::kPrimNot);
4152 __ PoisonHeapReference(TMP, src);
4153 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
4154 } else {
4155 __ StoreToOffset(store_type, src, obj, offset, null_checker);
4156 }
4157 } else {
4158 DCHECK(value_location.IsFpuRegister());
4159 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
4160 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
4161 }
4162 }
Alexey Frunze15958152017-02-09 19:08:30 -08004163
Alexey Frunzec061de12017-02-14 13:27:23 -08004164 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01004165 DCHECK(value_location.IsRegister());
4166 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004167 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004168 }
Alexey Frunze15958152017-02-09 19:08:30 -08004169
4170 if (is_volatile) {
4171 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4172 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004173}
4174
4175void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4176 HandleFieldGet(instruction, instruction->GetFieldInfo());
4177}
4178
4179void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4180 HandleFieldGet(instruction, instruction->GetFieldInfo());
4181}
4182
4183void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4184 HandleFieldSet(instruction, instruction->GetFieldInfo());
4185}
4186
4187void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004188 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004189}
4190
Alexey Frunze15958152017-02-09 19:08:30 -08004191void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadOneRegister(
4192 HInstruction* instruction,
4193 Location out,
4194 uint32_t offset,
4195 Location maybe_temp,
4196 ReadBarrierOption read_barrier_option) {
4197 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4198 if (read_barrier_option == kWithReadBarrier) {
4199 CHECK(kEmitCompilerReadBarrier);
4200 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4201 if (kUseBakerReadBarrier) {
4202 // Load with fast path based Baker's read barrier.
4203 // /* HeapReference<Object> */ out = *(out + offset)
4204 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4205 out,
4206 out_reg,
4207 offset,
4208 maybe_temp,
4209 /* needs_null_check */ false);
4210 } else {
4211 // Load with slow path based read barrier.
4212 // Save the value of `out` into `maybe_temp` before overwriting it
4213 // in the following move operation, as we will need it for the
4214 // read barrier below.
4215 __ Move(maybe_temp.AsRegister<GpuRegister>(), out_reg);
4216 // /* HeapReference<Object> */ out = *(out + offset)
4217 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4218 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4219 }
4220 } else {
4221 // Plain load with no read barrier.
4222 // /* HeapReference<Object> */ out = *(out + offset)
4223 __ LoadFromOffset(kLoadUnsignedWord, out_reg, out_reg, offset);
4224 __ MaybeUnpoisonHeapReference(out_reg);
4225 }
4226}
4227
4228void InstructionCodeGeneratorMIPS64::GenerateReferenceLoadTwoRegisters(
4229 HInstruction* instruction,
4230 Location out,
4231 Location obj,
4232 uint32_t offset,
4233 Location maybe_temp,
4234 ReadBarrierOption read_barrier_option) {
4235 GpuRegister out_reg = out.AsRegister<GpuRegister>();
4236 GpuRegister obj_reg = obj.AsRegister<GpuRegister>();
4237 if (read_barrier_option == kWithReadBarrier) {
4238 CHECK(kEmitCompilerReadBarrier);
4239 if (kUseBakerReadBarrier) {
4240 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
4241 // Load with fast path based Baker's read barrier.
4242 // /* HeapReference<Object> */ out = *(obj + offset)
4243 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4244 out,
4245 obj_reg,
4246 offset,
4247 maybe_temp,
4248 /* needs_null_check */ false);
4249 } else {
4250 // Load with slow path based read barrier.
4251 // /* HeapReference<Object> */ out = *(obj + offset)
4252 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4253 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4254 }
4255 } else {
4256 // Plain load with no read barrier.
4257 // /* HeapReference<Object> */ out = *(obj + offset)
4258 __ LoadFromOffset(kLoadUnsignedWord, out_reg, obj_reg, offset);
4259 __ MaybeUnpoisonHeapReference(out_reg);
4260 }
4261}
4262
Alexey Frunzef63f5692016-12-13 17:43:11 -08004263void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
Alexey Frunze15958152017-02-09 19:08:30 -08004264 HInstruction* instruction,
Alexey Frunzef63f5692016-12-13 17:43:11 -08004265 Location root,
4266 GpuRegister obj,
Alexey Frunze15958152017-02-09 19:08:30 -08004267 uint32_t offset,
4268 ReadBarrierOption read_barrier_option) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004269 GpuRegister root_reg = root.AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004270 if (read_barrier_option == kWithReadBarrier) {
4271 DCHECK(kEmitCompilerReadBarrier);
4272 if (kUseBakerReadBarrier) {
4273 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4274 // Baker's read barrier are used:
4275 //
4276 // root = obj.field;
4277 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4278 // if (temp != null) {
4279 // root = temp(root)
4280 // }
4281
4282 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4283 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4284 static_assert(
4285 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4286 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4287 "have different sizes.");
4288 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4289 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4290 "have different sizes.");
4291
4292 // Slow path marking the GC root `root`.
4293 Location temp = Location::RegisterLocation(T9);
4294 SlowPathCodeMIPS64* slow_path =
4295 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(
4296 instruction,
4297 root,
4298 /*entrypoint*/ temp);
4299 codegen_->AddSlowPath(slow_path);
4300
4301 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
4302 const int32_t entry_point_offset =
4303 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMips64PointerSize>(root.reg() - 1);
4304 // Loading the entrypoint does not require a load acquire since it is only changed when
4305 // threads are suspended or running a checkpoint.
4306 __ LoadFromOffset(kLoadDoubleword, temp.AsRegister<GpuRegister>(), TR, entry_point_offset);
4307 // The entrypoint is null when the GC is not marking, this prevents one load compared to
4308 // checking GetIsGcMarking.
4309 __ Bnezc(temp.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4310 __ Bind(slow_path->GetExitLabel());
4311 } else {
4312 // GC root loaded through a slow path for read barriers other
4313 // than Baker's.
4314 // /* GcRoot<mirror::Object>* */ root = obj + offset
4315 __ Daddiu64(root_reg, obj, static_cast<int32_t>(offset));
4316 // /* mirror::Object* */ root = root->Read()
4317 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4318 }
Alexey Frunzef63f5692016-12-13 17:43:11 -08004319 } else {
4320 // Plain GC root load with no read barrier.
4321 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4322 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
4323 // Note that GC roots are not affected by heap poisoning, thus we
4324 // do not have to unpoison `root_reg` here.
4325 }
4326}
4327
Alexey Frunze15958152017-02-09 19:08:30 -08004328void CodeGeneratorMIPS64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4329 Location ref,
4330 GpuRegister obj,
4331 uint32_t offset,
4332 Location temp,
4333 bool needs_null_check) {
4334 DCHECK(kEmitCompilerReadBarrier);
4335 DCHECK(kUseBakerReadBarrier);
4336
4337 // /* HeapReference<Object> */ ref = *(obj + offset)
4338 Location no_index = Location::NoLocation();
4339 ScaleFactor no_scale_factor = TIMES_1;
4340 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4341 ref,
4342 obj,
4343 offset,
4344 no_index,
4345 no_scale_factor,
4346 temp,
4347 needs_null_check);
4348}
4349
4350void CodeGeneratorMIPS64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4351 Location ref,
4352 GpuRegister obj,
4353 uint32_t data_offset,
4354 Location index,
4355 Location temp,
4356 bool needs_null_check) {
4357 DCHECK(kEmitCompilerReadBarrier);
4358 DCHECK(kUseBakerReadBarrier);
4359
4360 static_assert(
4361 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4362 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4363 // /* HeapReference<Object> */ ref =
4364 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4365 ScaleFactor scale_factor = TIMES_4;
4366 GenerateReferenceLoadWithBakerReadBarrier(instruction,
4367 ref,
4368 obj,
4369 data_offset,
4370 index,
4371 scale_factor,
4372 temp,
4373 needs_null_check);
4374}
4375
4376void CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4377 Location ref,
4378 GpuRegister obj,
4379 uint32_t offset,
4380 Location index,
4381 ScaleFactor scale_factor,
4382 Location temp,
4383 bool needs_null_check,
4384 bool always_update_field) {
4385 DCHECK(kEmitCompilerReadBarrier);
4386 DCHECK(kUseBakerReadBarrier);
4387
4388 // In slow path based read barriers, the read barrier call is
4389 // inserted after the original load. However, in fast path based
4390 // Baker's read barriers, we need to perform the load of
4391 // mirror::Object::monitor_ *before* the original reference load.
4392 // This load-load ordering is required by the read barrier.
4393 // The fast path/slow path (for Baker's algorithm) should look like:
4394 //
4395 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4396 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4397 // HeapReference<Object> ref = *src; // Original reference load.
4398 // bool is_gray = (rb_state == ReadBarrier::GrayState());
4399 // if (is_gray) {
4400 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4401 // }
4402 //
4403 // Note: the original implementation in ReadBarrier::Barrier is
4404 // slightly more complex as it performs additional checks that we do
4405 // not do here for performance reasons.
4406
4407 GpuRegister ref_reg = ref.AsRegister<GpuRegister>();
4408 GpuRegister temp_reg = temp.AsRegister<GpuRegister>();
4409 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
4410
4411 // /* int32_t */ monitor = obj->monitor_
4412 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
4413 if (needs_null_check) {
4414 MaybeRecordImplicitNullCheck(instruction);
4415 }
4416 // /* LockWord */ lock_word = LockWord(monitor)
4417 static_assert(sizeof(LockWord) == sizeof(int32_t),
4418 "art::LockWord and int32_t have different sizes.");
4419
4420 __ Sync(0); // Barrier to prevent load-load reordering.
4421
4422 // The actual reference load.
4423 if (index.IsValid()) {
4424 // Load types involving an "index": ArrayGet,
4425 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
4426 // intrinsics.
4427 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
4428 if (index.IsConstant()) {
4429 size_t computed_offset =
4430 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
4431 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, computed_offset);
4432 } else {
4433 GpuRegister index_reg = index.AsRegister<GpuRegister>();
Chris Larsencd0295d2017-03-31 15:26:54 -07004434 if (scale_factor == TIMES_1) {
4435 __ Daddu(TMP, index_reg, obj);
4436 } else {
4437 __ Dlsa(TMP, index_reg, obj, scale_factor);
4438 }
Alexey Frunze15958152017-02-09 19:08:30 -08004439 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, TMP, offset);
4440 }
4441 } else {
4442 // /* HeapReference<Object> */ ref = *(obj + offset)
4443 __ LoadFromOffset(kLoadUnsignedWord, ref_reg, obj, offset);
4444 }
4445
4446 // Object* ref = ref_addr->AsMirrorPtr()
4447 __ MaybeUnpoisonHeapReference(ref_reg);
4448
4449 // Slow path marking the object `ref` when it is gray.
4450 SlowPathCodeMIPS64* slow_path;
4451 if (always_update_field) {
4452 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS64 only supports address
4453 // of the form `obj + field_offset`, where `obj` is a register and
4454 // `field_offset` is a register. Thus `offset` and `scale_factor`
4455 // above are expected to be null in this code path.
4456 DCHECK_EQ(offset, 0u);
4457 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
4458 slow_path = new (GetGraph()->GetArena())
4459 ReadBarrierMarkAndUpdateFieldSlowPathMIPS64(instruction,
4460 ref,
4461 obj,
4462 /* field_offset */ index,
4463 temp_reg);
4464 } else {
4465 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS64(instruction, ref);
4466 }
4467 AddSlowPath(slow_path);
4468
4469 // if (rb_state == ReadBarrier::GrayState())
4470 // ref = ReadBarrier::Mark(ref);
4471 // Given the numeric representation, it's enough to check the low bit of the
4472 // rb_state. We do that by shifting the bit into the sign bit (31) and
4473 // performing a branch on less than zero.
4474 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
4475 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
4476 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
4477 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
4478 __ Bltzc(temp_reg, slow_path->GetEntryLabel());
4479 __ Bind(slow_path->GetExitLabel());
4480}
4481
4482void CodeGeneratorMIPS64::GenerateReadBarrierSlow(HInstruction* instruction,
4483 Location out,
4484 Location ref,
4485 Location obj,
4486 uint32_t offset,
4487 Location index) {
4488 DCHECK(kEmitCompilerReadBarrier);
4489
4490 // Insert a slow path based read barrier *after* the reference load.
4491 //
4492 // If heap poisoning is enabled, the unpoisoning of the loaded
4493 // reference will be carried out by the runtime within the slow
4494 // path.
4495 //
4496 // Note that `ref` currently does not get unpoisoned (when heap
4497 // poisoning is enabled), which is alright as the `ref` argument is
4498 // not used by the artReadBarrierSlow entry point.
4499 //
4500 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
4501 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
4502 ReadBarrierForHeapReferenceSlowPathMIPS64(instruction, out, ref, obj, offset, index);
4503 AddSlowPath(slow_path);
4504
4505 __ Bc(slow_path->GetEntryLabel());
4506 __ Bind(slow_path->GetExitLabel());
4507}
4508
4509void CodeGeneratorMIPS64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
4510 Location out,
4511 Location ref,
4512 Location obj,
4513 uint32_t offset,
4514 Location index) {
4515 if (kEmitCompilerReadBarrier) {
4516 // Baker's read barriers shall be handled by the fast path
4517 // (CodeGeneratorMIPS64::GenerateReferenceLoadWithBakerReadBarrier).
4518 DCHECK(!kUseBakerReadBarrier);
4519 // If heap poisoning is enabled, unpoisoning will be taken care of
4520 // by the runtime within the slow path.
4521 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
4522 } else if (kPoisonHeapReferences) {
4523 __ UnpoisonHeapReference(out.AsRegister<GpuRegister>());
4524 }
4525}
4526
4527void CodeGeneratorMIPS64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
4528 Location out,
4529 Location root) {
4530 DCHECK(kEmitCompilerReadBarrier);
4531
4532 // Insert a slow path based read barrier *after* the GC root load.
4533 //
4534 // Note that GC roots are not affected by heap poisoning, so we do
4535 // not need to do anything special for this here.
4536 SlowPathCodeMIPS64* slow_path =
4537 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS64(instruction, out, root);
4538 AddSlowPath(slow_path);
4539
4540 __ Bc(slow_path->GetEntryLabel());
4541 __ Bind(slow_path->GetExitLabel());
4542}
4543
Alexey Frunze4dda3372015-06-01 18:31:49 -07004544void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004545 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4546 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4547 switch (type_check_kind) {
4548 case TypeCheckKind::kExactCheck:
4549 case TypeCheckKind::kAbstractClassCheck:
4550 case TypeCheckKind::kClassHierarchyCheck:
4551 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08004552 call_kind =
4553 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004554 break;
4555 case TypeCheckKind::kArrayCheck:
4556 case TypeCheckKind::kUnresolvedCheck:
4557 case TypeCheckKind::kInterfaceCheck:
4558 call_kind = LocationSummary::kCallOnSlowPath;
4559 break;
4560 }
4561
Alexey Frunze4dda3372015-06-01 18:31:49 -07004562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4563 locations->SetInAt(0, Location::RequiresRegister());
4564 locations->SetInAt(1, Location::RequiresRegister());
4565 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004566 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004567 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08004568 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004569}
4570
4571void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004572 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004573 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08004574 Location obj_loc = locations->InAt(0);
4575 GpuRegister obj = obj_loc.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004576 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze15958152017-02-09 19:08:30 -08004577 Location out_loc = locations->Out();
4578 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4579 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
4580 DCHECK_LE(num_temps, 1u);
4581 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004582 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4583 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4584 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4585 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004586 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004587 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004588
4589 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004590 // Avoid this check if we know `obj` is not null.
4591 if (instruction->MustDoNullCheck()) {
4592 __ Move(out, ZERO);
4593 __ Beqzc(obj, &done);
4594 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004595
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004596 switch (type_check_kind) {
4597 case TypeCheckKind::kExactCheck: {
4598 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004599 GenerateReferenceLoadTwoRegisters(instruction,
4600 out_loc,
4601 obj_loc,
4602 class_offset,
4603 maybe_temp_loc,
4604 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004605 // Classes must be equal for the instanceof to succeed.
4606 __ Xor(out, out, cls);
4607 __ Sltiu(out, out, 1);
4608 break;
4609 }
4610
4611 case TypeCheckKind::kAbstractClassCheck: {
4612 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004613 GenerateReferenceLoadTwoRegisters(instruction,
4614 out_loc,
4615 obj_loc,
4616 class_offset,
4617 maybe_temp_loc,
4618 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004619 // If the class is abstract, we eagerly fetch the super class of the
4620 // object to avoid doing a comparison we know will fail.
4621 Mips64Label loop;
4622 __ Bind(&loop);
4623 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004624 GenerateReferenceLoadOneRegister(instruction,
4625 out_loc,
4626 super_offset,
4627 maybe_temp_loc,
4628 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004629 // If `out` is null, we use it for the result, and jump to `done`.
4630 __ Beqzc(out, &done);
4631 __ Bnec(out, cls, &loop);
4632 __ LoadConst32(out, 1);
4633 break;
4634 }
4635
4636 case TypeCheckKind::kClassHierarchyCheck: {
4637 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004638 GenerateReferenceLoadTwoRegisters(instruction,
4639 out_loc,
4640 obj_loc,
4641 class_offset,
4642 maybe_temp_loc,
4643 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004644 // Walk over the class hierarchy to find a match.
4645 Mips64Label loop, success;
4646 __ Bind(&loop);
4647 __ Beqc(out, cls, &success);
4648 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08004649 GenerateReferenceLoadOneRegister(instruction,
4650 out_loc,
4651 super_offset,
4652 maybe_temp_loc,
4653 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004654 __ Bnezc(out, &loop);
4655 // If `out` is null, we use it for the result, and jump to `done`.
4656 __ Bc(&done);
4657 __ Bind(&success);
4658 __ LoadConst32(out, 1);
4659 break;
4660 }
4661
4662 case TypeCheckKind::kArrayObjectCheck: {
4663 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004664 GenerateReferenceLoadTwoRegisters(instruction,
4665 out_loc,
4666 obj_loc,
4667 class_offset,
4668 maybe_temp_loc,
4669 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004670 // Do an exact check.
4671 Mips64Label success;
4672 __ Beqc(out, cls, &success);
4673 // Otherwise, we need to check that the object's class is a non-primitive array.
4674 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08004675 GenerateReferenceLoadOneRegister(instruction,
4676 out_loc,
4677 component_offset,
4678 maybe_temp_loc,
4679 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004680 // If `out` is null, we use it for the result, and jump to `done`.
4681 __ Beqzc(out, &done);
4682 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
4683 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4684 __ Sltiu(out, out, 1);
4685 __ Bc(&done);
4686 __ Bind(&success);
4687 __ LoadConst32(out, 1);
4688 break;
4689 }
4690
4691 case TypeCheckKind::kArrayCheck: {
4692 // No read barrier since the slow path will retry upon failure.
4693 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08004694 GenerateReferenceLoadTwoRegisters(instruction,
4695 out_loc,
4696 obj_loc,
4697 class_offset,
4698 maybe_temp_loc,
4699 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004700 DCHECK(locations->OnlyCallsOnSlowPath());
4701 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4702 /* is_fatal */ false);
4703 codegen_->AddSlowPath(slow_path);
4704 __ Bnec(out, cls, slow_path->GetEntryLabel());
4705 __ LoadConst32(out, 1);
4706 break;
4707 }
4708
4709 case TypeCheckKind::kUnresolvedCheck:
4710 case TypeCheckKind::kInterfaceCheck: {
4711 // Note that we indeed only call on slow path, but we always go
4712 // into the slow path for the unresolved and interface check
4713 // cases.
4714 //
4715 // We cannot directly call the InstanceofNonTrivial runtime
4716 // entry point without resorting to a type checking slow path
4717 // here (i.e. by calling InvokeRuntime directly), as it would
4718 // require to assign fixed registers for the inputs of this
4719 // HInstanceOf instruction (following the runtime calling
4720 // convention), which might be cluttered by the potential first
4721 // read barrier emission at the beginning of this method.
4722 //
4723 // TODO: Introduce a new runtime entry point taking the object
4724 // to test (instead of its class) as argument, and let it deal
4725 // with the read barrier issues. This will let us refactor this
4726 // case of the `switch` code as it was previously (with a direct
4727 // call to the runtime not using a type checking slow path).
4728 // This should also be beneficial for the other cases above.
4729 DCHECK(locations->OnlyCallsOnSlowPath());
4730 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
4731 /* is_fatal */ false);
4732 codegen_->AddSlowPath(slow_path);
4733 __ Bc(slow_path->GetEntryLabel());
4734 break;
4735 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004736 }
4737
4738 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08004739
4740 if (slow_path != nullptr) {
4741 __ Bind(slow_path->GetExitLabel());
4742 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004743}
4744
4745void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
4746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4747 locations->SetOut(Location::ConstantLocation(constant));
4748}
4749
4750void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4751 // Will be generated at use site.
4752}
4753
4754void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
4755 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4756 locations->SetOut(Location::ConstantLocation(constant));
4757}
4758
4759void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4760 // Will be generated at use site.
4761}
4762
Calin Juravle175dc732015-08-25 15:42:32 +01004763void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4764 // The trampoline uses the same calling convention as dex calling conventions,
4765 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4766 // the method_idx.
4767 HandleInvoke(invoke);
4768}
4769
4770void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4771 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4772}
4773
Alexey Frunze4dda3372015-06-01 18:31:49 -07004774void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
4775 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
4776 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4777}
4778
4779void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4780 HandleInvoke(invoke);
4781 // The register T0 is required to be used for the hidden argument in
4782 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4783 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4784}
4785
4786void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
4787 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4788 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004789 Location receiver = invoke->GetLocations()->InAt(0);
4790 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004791 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004792
4793 // Set the hidden argument.
4794 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
4795 invoke->GetDexMethodIndex());
4796
4797 // temp = object->GetClass();
4798 if (receiver.IsStackSlot()) {
4799 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
4800 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
4801 } else {
4802 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
4803 }
4804 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08004805 // Instead of simply (possibly) unpoisoning `temp` here, we should
4806 // emit a read barrier for the previous class reference load.
4807 // However this is not required in practice, as this is an
4808 // intermediate/temporary reference and because the current
4809 // concurrent copying collector keeps the from-space memory
4810 // intact/accessible until the end of the marking phase (the
4811 // concurrent copying collector may not in the future).
4812 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004813 __ LoadFromOffset(kLoadDoubleword, temp, temp,
4814 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
4815 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004816 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004817 // temp = temp->GetImtEntryAt(method_offset);
4818 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
4819 // T9 = temp->GetEntryPoint();
4820 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
4821 // T9();
4822 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004823 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004824 DCHECK(!codegen_->IsLeafMethod());
4825 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4826}
4827
4828void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07004829 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
4830 if (intrinsic.TryDispatch(invoke)) {
4831 return;
4832 }
4833
Alexey Frunze4dda3372015-06-01 18:31:49 -07004834 HandleInvoke(invoke);
4835}
4836
4837void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004838 // Explicit clinit checks triggered by static invokes must have been pruned by
4839 // art::PrepareForRegisterAllocation.
4840 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004841
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);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004848}
4849
Orion Hodsonac141392017-01-13 11:53:47 +00004850void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4851 HandleInvoke(invoke);
4852}
4853
4854void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4855 codegen_->GenerateInvokePolymorphicCall(invoke);
4856}
4857
Chris Larsen3039e382015-08-26 07:54:08 -07004858static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004859 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07004860 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
4861 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004862 return true;
4863 }
4864 return false;
4865}
4866
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004867HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08004868 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004869 bool fallback_load = false;
4870 switch (desired_string_load_kind) {
4871 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4872 DCHECK(!GetCompilerOptions().GetCompilePic());
4873 break;
4874 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4875 DCHECK(GetCompilerOptions().GetCompilePic());
4876 break;
4877 case HLoadString::LoadKind::kBootImageAddress:
4878 break;
4879 case HLoadString::LoadKind::kBssEntry:
4880 DCHECK(!Runtime::Current()->UseJitCompilation());
4881 break;
4882 case HLoadString::LoadKind::kDexCacheViaMethod:
4883 break;
4884 case HLoadString::LoadKind::kJitTableAddress:
4885 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004886 break;
4887 }
4888 if (fallback_load) {
4889 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4890 }
4891 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004892}
4893
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004894HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
4895 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004896 bool fallback_load = false;
4897 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004898 case HLoadClass::LoadKind::kInvalid:
4899 LOG(FATAL) << "UNREACHABLE";
4900 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004901 case HLoadClass::LoadKind::kReferrersClass:
4902 break;
4903 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4904 DCHECK(!GetCompilerOptions().GetCompilePic());
4905 break;
4906 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4907 DCHECK(GetCompilerOptions().GetCompilePic());
4908 break;
4909 case HLoadClass::LoadKind::kBootImageAddress:
4910 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004911 case HLoadClass::LoadKind::kBssEntry:
4912 DCHECK(!Runtime::Current()->UseJitCompilation());
4913 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004914 case HLoadClass::LoadKind::kJitTableAddress:
4915 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004916 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004917 case HLoadClass::LoadKind::kDexCacheViaMethod:
4918 break;
4919 }
4920 if (fallback_load) {
4921 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4922 }
4923 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924}
4925
Vladimir Markodc151b22015-10-15 18:02:30 +01004926HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
4927 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004928 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08004929 // On MIPS64 we support all dispatch types.
4930 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004931}
4932
Alexey Frunze4dda3372015-06-01 18:31:49 -07004933void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4934 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004935 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08004936 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4937 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4938
Alexey Frunze19f6c692016-11-30 19:19:55 -08004939 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004940 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004941 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004942 uint32_t offset =
4943 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004944 __ LoadFromOffset(kLoadDoubleword,
4945 temp.AsRegister<GpuRegister>(),
4946 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004947 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00004948 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004949 }
Vladimir Marko58155012015-08-19 12:49:41 +00004950 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004951 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004952 break;
4953 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004954 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
4955 kLoadDoubleword,
4956 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004957 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08004958 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4959 uint32_t offset = invoke->GetDexCacheArrayOffset();
4960 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004961 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08004962 EmitPcRelativeAddressPlaceholderHigh(info, AT);
4963 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
4964 break;
4965 }
Vladimir Marko58155012015-08-19 12:49:41 +00004966 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004967 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004968 GpuRegister reg = temp.AsRegister<GpuRegister>();
4969 GpuRegister method_reg;
4970 if (current_method.IsRegister()) {
4971 method_reg = current_method.AsRegister<GpuRegister>();
4972 } else {
4973 // TODO: use the appropriate DCHECK() here if possible.
4974 // DCHECK(invoke->GetLocations()->Intrinsified());
4975 DCHECK(!current_method.IsValid());
4976 method_reg = reg;
4977 __ Ld(reg, SP, kCurrentMethodStackOffset);
4978 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004979
Vladimir Marko58155012015-08-19 12:49:41 +00004980 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004981 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00004982 reg,
4983 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01004984 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004985 // temp = temp[index_in_cache];
4986 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4987 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004988 __ LoadFromOffset(kLoadDoubleword,
4989 reg,
4990 reg,
4991 CodeGenerator::GetCachePointerOffset(index_in_cache));
4992 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004993 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004994 }
4995
Alexey Frunze19f6c692016-11-30 19:19:55 -08004996 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00004997 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08004998 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00004999 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005000 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5001 // T9 = callee_method->entry_point_from_quick_compiled_code_;
5002 __ LoadFromOffset(kLoadDoubleword,
5003 T9,
5004 callee_method.AsRegister<GpuRegister>(),
5005 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005006 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00005007 // T9()
5008 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005009 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00005010 break;
5011 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005012 DCHECK(!IsLeafMethod());
5013}
5014
5015void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005016 // Explicit clinit checks triggered by static invokes must have been pruned by
5017 // art::PrepareForRegisterAllocation.
5018 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005019
5020 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5021 return;
5022 }
5023
5024 LocationSummary* locations = invoke->GetLocations();
5025 codegen_->GenerateStaticOrDirectCall(invoke,
5026 locations->HasTemps()
5027 ? locations->GetTemp(0)
5028 : Location::NoLocation());
5029 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5030}
5031
Alexey Frunze53afca12015-11-05 16:34:23 -08005032void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005033 // Use the calling convention instead of the location of the receiver, as
5034 // intrinsics may have put the receiver in a different register. In the intrinsics
5035 // slow path, the arguments have been moved to the right place, so here we are
5036 // guaranteed that the receiver is the first register of the calling convention.
5037 InvokeDexCallingConvention calling_convention;
5038 GpuRegister receiver = calling_convention.GetRegisterAt(0);
5039
Alexey Frunze53afca12015-11-05 16:34:23 -08005040 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005041 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5042 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
5043 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005044 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005045
5046 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005047 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08005048 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08005049 // Instead of simply (possibly) unpoisoning `temp` here, we should
5050 // emit a read barrier for the previous class reference load.
5051 // However this is not required in practice, as this is an
5052 // intermediate/temporary reference and because the current
5053 // concurrent copying collector keeps the from-space memory
5054 // intact/accessible until the end of the marking phase (the
5055 // concurrent copying collector may not in the future).
5056 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005057 // temp = temp->GetMethodAt(method_offset);
5058 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
5059 // T9 = temp->GetEntryPoint();
5060 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
5061 // T9();
5062 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005063 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08005064}
5065
5066void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5067 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5068 return;
5069 }
5070
5071 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005072 DCHECK(!codegen_->IsLeafMethod());
5073 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5074}
5075
5076void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005077 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5078 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005079 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005080 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08005081 cls,
5082 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005083 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08005084 return;
5085 }
Vladimir Marko41559982017-01-06 14:04:23 +00005086 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005087
Alexey Frunze15958152017-02-09 19:08:30 -08005088 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5089 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunzef63f5692016-12-13 17:43:11 -08005090 ? LocationSummary::kCallOnSlowPath
5091 : LocationSummary::kNoCall;
5092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00005093 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005094 locations->SetInAt(0, Location::RequiresRegister());
5095 }
5096 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005097}
5098
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005099// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5100// move.
5101void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005102 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5103 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5104 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005105 return;
5106 }
Vladimir Marko41559982017-01-06 14:04:23 +00005107 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005108
Vladimir Marko41559982017-01-06 14:04:23 +00005109 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005110 Location out_loc = locations->Out();
5111 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5112 GpuRegister current_method_reg = ZERO;
5113 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5114 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5115 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
5116 }
5117
Alexey Frunze15958152017-02-09 19:08:30 -08005118 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5119 ? kWithoutReadBarrier
5120 : kCompilerReadBarrierOption;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005121 bool generate_null_check = false;
5122 switch (load_kind) {
5123 case HLoadClass::LoadKind::kReferrersClass:
5124 DCHECK(!cls->CanCallRuntime());
5125 DCHECK(!cls->MustGenerateClinitCheck());
5126 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5127 GenerateGcRootFieldLoad(cls,
5128 out_loc,
5129 current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08005130 ArtMethod::DeclaringClassOffset().Int32Value(),
5131 read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005132 break;
5133 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005134 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005135 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005136 __ LoadLiteral(out,
5137 kLoadUnsignedWord,
5138 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5139 cls->GetTypeIndex()));
5140 break;
5141 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005142 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08005143 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005144 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
5145 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5146 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5147 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5148 break;
5149 }
5150 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08005151 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005152 uint32_t address = dchecked_integral_cast<uint32_t>(
5153 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5154 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005155 __ LoadLiteral(out,
5156 kLoadUnsignedWord,
5157 codegen_->DeduplicateBootImageAddressLiteral(address));
5158 break;
5159 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005160 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005161 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005162 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005163 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005164 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005165 generate_null_check = true;
5166 break;
5167 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005168 case HLoadClass::LoadKind::kJitTableAddress:
5169 __ LoadLiteral(out,
5170 kLoadUnsignedWord,
5171 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5172 cls->GetTypeIndex(),
5173 cls->GetClass()));
Alexey Frunze15958152017-02-09 19:08:30 -08005174 GenerateGcRootFieldLoad(cls, out_loc, out, 0, read_barrier_option);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005175 break;
Vladimir Marko41559982017-01-06 14:04:23 +00005176 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005177 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005178 LOG(FATAL) << "UNREACHABLE";
5179 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08005180 }
5181
5182 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5183 DCHECK(cls->CanCallRuntime());
5184 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
5185 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5186 codegen_->AddSlowPath(slow_path);
5187 if (generate_null_check) {
5188 __ Beqzc(out, slow_path->GetEntryLabel());
5189 }
5190 if (cls->MustGenerateClinitCheck()) {
5191 GenerateClassInitializationCheck(slow_path, out);
5192 } else {
5193 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005194 }
5195 }
5196}
5197
David Brazdilcb1c0552015-08-04 16:22:25 +01005198static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005199 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005200}
5201
Alexey Frunze4dda3372015-06-01 18:31:49 -07005202void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
5203 LocationSummary* locations =
5204 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5205 locations->SetOut(Location::RequiresRegister());
5206}
5207
5208void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
5209 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005210 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
5211}
5212
5213void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
5214 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5215}
5216
5217void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5218 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005219}
5220
Alexey Frunze4dda3372015-06-01 18:31:49 -07005221void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005222 HLoadString::LoadKind load_kind = load->GetLoadKind();
5223 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005224 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005225 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5226 InvokeRuntimeCallingConvention calling_convention;
5227 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5228 } else {
5229 locations->SetOut(Location::RequiresRegister());
5230 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005231}
5232
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005233// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5234// move.
5235void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005236 HLoadString::LoadKind load_kind = load->GetLoadKind();
5237 LocationSummary* locations = load->GetLocations();
5238 Location out_loc = locations->Out();
5239 GpuRegister out = out_loc.AsRegister<GpuRegister>();
5240
5241 switch (load_kind) {
5242 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005243 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005244 __ LoadLiteral(out,
5245 kLoadUnsignedWord,
5246 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5247 load->GetStringIndex()));
5248 return; // No dex cache slow path.
5249 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5250 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
5251 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005252 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08005253 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
5254 __ Daddiu(out, AT, /* placeholder */ 0x5678);
5255 return; // No dex cache slow path.
5256 }
5257 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005258 uint32_t address = dchecked_integral_cast<uint32_t>(
5259 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5260 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005261 __ LoadLiteral(out,
5262 kLoadUnsignedWord,
5263 codegen_->DeduplicateBootImageAddressLiteral(address));
5264 return; // No dex cache slow path.
5265 }
5266 case HLoadString::LoadKind::kBssEntry: {
5267 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5268 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005269 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08005270 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
Alexey Frunze15958152017-02-09 19:08:30 -08005271 GenerateGcRootFieldLoad(load,
5272 out_loc,
5273 out,
5274 /* placeholder */ 0x5678,
5275 kCompilerReadBarrierOption);
Alexey Frunzef63f5692016-12-13 17:43:11 -08005276 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
5277 codegen_->AddSlowPath(slow_path);
5278 __ Beqzc(out, slow_path->GetEntryLabel());
5279 __ Bind(slow_path->GetExitLabel());
5280 return;
5281 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08005282 case HLoadString::LoadKind::kJitTableAddress:
5283 __ LoadLiteral(out,
5284 kLoadUnsignedWord,
5285 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5286 load->GetStringIndex(),
5287 load->GetString()));
Alexey Frunze15958152017-02-09 19:08:30 -08005288 GenerateGcRootFieldLoad(load, out_loc, out, 0, kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08005289 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08005290 default:
5291 break;
5292 }
5293
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005294 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08005295 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5296 InvokeRuntimeCallingConvention calling_convention;
5297 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
5298 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005300}
5301
Alexey Frunze4dda3372015-06-01 18:31:49 -07005302void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
5303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5304 locations->SetOut(Location::ConstantLocation(constant));
5305}
5306
5307void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5308 // Will be generated at use site.
5309}
5310
5311void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
5312 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005314 InvokeRuntimeCallingConvention calling_convention;
5315 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5316}
5317
5318void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005319 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07005320 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01005321 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005322 if (instruction->IsEnter()) {
5323 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5324 } else {
5325 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5326 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005327}
5328
5329void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
5330 LocationSummary* locations =
5331 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5332 switch (mul->GetResultType()) {
5333 case Primitive::kPrimInt:
5334 case Primitive::kPrimLong:
5335 locations->SetInAt(0, Location::RequiresRegister());
5336 locations->SetInAt(1, Location::RequiresRegister());
5337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5338 break;
5339
5340 case Primitive::kPrimFloat:
5341 case Primitive::kPrimDouble:
5342 locations->SetInAt(0, Location::RequiresFpuRegister());
5343 locations->SetInAt(1, Location::RequiresFpuRegister());
5344 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5345 break;
5346
5347 default:
5348 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5349 }
5350}
5351
5352void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
5353 Primitive::Type type = instruction->GetType();
5354 LocationSummary* locations = instruction->GetLocations();
5355
5356 switch (type) {
5357 case Primitive::kPrimInt:
5358 case Primitive::kPrimLong: {
5359 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5360 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
5361 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
5362 if (type == Primitive::kPrimInt)
5363 __ MulR6(dst, lhs, rhs);
5364 else
5365 __ Dmul(dst, lhs, rhs);
5366 break;
5367 }
5368 case Primitive::kPrimFloat:
5369 case Primitive::kPrimDouble: {
5370 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5371 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
5372 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
5373 if (type == Primitive::kPrimFloat)
5374 __ MulS(dst, lhs, rhs);
5375 else
5376 __ MulD(dst, lhs, rhs);
5377 break;
5378 }
5379 default:
5380 LOG(FATAL) << "Unexpected mul type " << type;
5381 }
5382}
5383
5384void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
5385 LocationSummary* locations =
5386 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5387 switch (neg->GetResultType()) {
5388 case Primitive::kPrimInt:
5389 case Primitive::kPrimLong:
5390 locations->SetInAt(0, Location::RequiresRegister());
5391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5392 break;
5393
5394 case Primitive::kPrimFloat:
5395 case Primitive::kPrimDouble:
5396 locations->SetInAt(0, Location::RequiresFpuRegister());
5397 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5398 break;
5399
5400 default:
5401 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5402 }
5403}
5404
5405void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
5406 Primitive::Type type = instruction->GetType();
5407 LocationSummary* locations = instruction->GetLocations();
5408
5409 switch (type) {
5410 case Primitive::kPrimInt:
5411 case Primitive::kPrimLong: {
5412 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5413 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5414 if (type == Primitive::kPrimInt)
5415 __ Subu(dst, ZERO, src);
5416 else
5417 __ Dsubu(dst, ZERO, src);
5418 break;
5419 }
5420 case Primitive::kPrimFloat:
5421 case Primitive::kPrimDouble: {
5422 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5423 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5424 if (type == Primitive::kPrimFloat)
5425 __ NegS(dst, src);
5426 else
5427 __ NegD(dst, src);
5428 break;
5429 }
5430 default:
5431 LOG(FATAL) << "Unexpected neg type " << type;
5432 }
5433}
5434
5435void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
5436 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005438 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005439 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005440 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5441 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005442}
5443
5444void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005445 // Note: if heap poisoning is enabled, the entry point takes care
5446 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005447 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
5448 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005449}
5450
5451void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
5452 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005454 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005455 if (instruction->IsStringAlloc()) {
5456 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5457 } else {
5458 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005459 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005460 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5461}
5462
5463void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08005464 // Note: if heap poisoning is enabled, the entry point takes care
5465 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005466 if (instruction->IsStringAlloc()) {
5467 // String is allocated through StringFactory. Call NewEmptyString entry point.
5468 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02005469 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07005470 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005471 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5472 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
5473 __ Jalr(T9);
5474 __ Nop();
5475 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5476 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01005477 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005478 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005479 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005480}
5481
5482void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
5483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5484 locations->SetInAt(0, Location::RequiresRegister());
5485 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5486}
5487
5488void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
5489 Primitive::Type type = instruction->GetType();
5490 LocationSummary* locations = instruction->GetLocations();
5491
5492 switch (type) {
5493 case Primitive::kPrimInt:
5494 case Primitive::kPrimLong: {
5495 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5496 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5497 __ Nor(dst, src, ZERO);
5498 break;
5499 }
5500
5501 default:
5502 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5503 }
5504}
5505
5506void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5508 locations->SetInAt(0, Location::RequiresRegister());
5509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5510}
5511
5512void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
5513 LocationSummary* locations = instruction->GetLocations();
5514 __ Xori(locations->Out().AsRegister<GpuRegister>(),
5515 locations->InAt(0).AsRegister<GpuRegister>(),
5516 1);
5517}
5518
5519void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005520 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5521 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005522}
5523
Calin Juravle2ae48182016-03-16 14:05:09 +00005524void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5525 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005526 return;
5527 }
5528 Location obj = instruction->GetLocations()->InAt(0);
5529
5530 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005531 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005532}
5533
Calin Juravle2ae48182016-03-16 14:05:09 +00005534void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005535 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005536 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005537
5538 Location obj = instruction->GetLocations()->InAt(0);
5539
5540 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
5541}
5542
5543void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005544 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005545}
5546
5547void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
5548 HandleBinaryOp(instruction);
5549}
5550
5551void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
5552 HandleBinaryOp(instruction);
5553}
5554
5555void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5556 LOG(FATAL) << "Unreachable";
5557}
5558
5559void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
5560 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5561}
5562
5563void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
5564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5565 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5566 if (location.IsStackSlot()) {
5567 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5568 } else if (location.IsDoubleStackSlot()) {
5569 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5570 }
5571 locations->SetOut(location);
5572}
5573
5574void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
5575 ATTRIBUTE_UNUSED) {
5576 // Nothing to do, the parameter is already at its location.
5577}
5578
5579void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
5580 LocationSummary* locations =
5581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5582 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5583}
5584
5585void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
5586 ATTRIBUTE_UNUSED) {
5587 // Nothing to do, the method is already at its location.
5588}
5589
5590void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
5591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005592 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005593 locations->SetInAt(i, Location::Any());
5594 }
5595 locations->SetOut(Location::Any());
5596}
5597
5598void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5599 LOG(FATAL) << "Unreachable";
5600}
5601
5602void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
5603 Primitive::Type type = rem->GetResultType();
5604 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005605 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5606 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5608
5609 switch (type) {
5610 case Primitive::kPrimInt:
5611 case Primitive::kPrimLong:
5612 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07005613 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07005614 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5615 break;
5616
5617 case Primitive::kPrimFloat:
5618 case Primitive::kPrimDouble: {
5619 InvokeRuntimeCallingConvention calling_convention;
5620 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5621 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5622 locations->SetOut(calling_convention.GetReturnLocation(type));
5623 break;
5624 }
5625
5626 default:
5627 LOG(FATAL) << "Unexpected rem type " << type;
5628 }
5629}
5630
5631void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
5632 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07005633
5634 switch (type) {
5635 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07005636 case Primitive::kPrimLong:
5637 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005638 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07005639
5640 case Primitive::kPrimFloat:
5641 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01005642 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5643 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005644 if (type == Primitive::kPrimFloat) {
5645 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5646 } else {
5647 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5648 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005649 break;
5650 }
5651 default:
5652 LOG(FATAL) << "Unexpected rem type " << type;
5653 }
5654}
5655
Igor Murashkind01745e2017-04-05 16:40:31 -07005656void LocationsBuilderMIPS64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5657 constructor_fence->SetLocations(nullptr);
5658}
5659
5660void InstructionCodeGeneratorMIPS64::VisitConstructorFence(
5661 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5662 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5663}
5664
Alexey Frunze4dda3372015-06-01 18:31:49 -07005665void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5666 memory_barrier->SetLocations(nullptr);
5667}
5668
5669void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5670 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5671}
5672
5673void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
5674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5675 Primitive::Type return_type = ret->InputAt(0)->GetType();
5676 locations->SetInAt(0, Mips64ReturnLocation(return_type));
5677}
5678
5679void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5680 codegen_->GenerateFrameExit();
5681}
5682
5683void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
5684 ret->SetLocations(nullptr);
5685}
5686
5687void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5688 codegen_->GenerateFrameExit();
5689}
5690
Alexey Frunze92d90602015-12-18 18:16:36 -08005691void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
5692 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005693}
5694
Alexey Frunze92d90602015-12-18 18:16:36 -08005695void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
5696 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005697}
5698
Alexey Frunze4dda3372015-06-01 18:31:49 -07005699void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
5700 HandleShift(shl);
5701}
5702
5703void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
5704 HandleShift(shl);
5705}
5706
5707void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
5708 HandleShift(shr);
5709}
5710
5711void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
5712 HandleShift(shr);
5713}
5714
Alexey Frunze4dda3372015-06-01 18:31:49 -07005715void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
5716 HandleBinaryOp(instruction);
5717}
5718
5719void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
5720 HandleBinaryOp(instruction);
5721}
5722
5723void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5724 HandleFieldGet(instruction, instruction->GetFieldInfo());
5725}
5726
5727void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5728 HandleFieldGet(instruction, instruction->GetFieldInfo());
5729}
5730
5731void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5732 HandleFieldSet(instruction, instruction->GetFieldInfo());
5733}
5734
5735void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01005736 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005737}
5738
Calin Juravlee460d1d2015-09-29 04:52:17 +01005739void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
5740 HUnresolvedInstanceFieldGet* instruction) {
5741 FieldAccessCallingConventionMIPS64 calling_convention;
5742 codegen_->CreateUnresolvedFieldLocationSummary(
5743 instruction, instruction->GetFieldType(), calling_convention);
5744}
5745
5746void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
5747 HUnresolvedInstanceFieldGet* instruction) {
5748 FieldAccessCallingConventionMIPS64 calling_convention;
5749 codegen_->GenerateUnresolvedFieldAccess(instruction,
5750 instruction->GetFieldType(),
5751 instruction->GetFieldIndex(),
5752 instruction->GetDexPc(),
5753 calling_convention);
5754}
5755
5756void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
5757 HUnresolvedInstanceFieldSet* instruction) {
5758 FieldAccessCallingConventionMIPS64 calling_convention;
5759 codegen_->CreateUnresolvedFieldLocationSummary(
5760 instruction, instruction->GetFieldType(), calling_convention);
5761}
5762
5763void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
5764 HUnresolvedInstanceFieldSet* instruction) {
5765 FieldAccessCallingConventionMIPS64 calling_convention;
5766 codegen_->GenerateUnresolvedFieldAccess(instruction,
5767 instruction->GetFieldType(),
5768 instruction->GetFieldIndex(),
5769 instruction->GetDexPc(),
5770 calling_convention);
5771}
5772
5773void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
5774 HUnresolvedStaticFieldGet* instruction) {
5775 FieldAccessCallingConventionMIPS64 calling_convention;
5776 codegen_->CreateUnresolvedFieldLocationSummary(
5777 instruction, instruction->GetFieldType(), calling_convention);
5778}
5779
5780void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
5781 HUnresolvedStaticFieldGet* instruction) {
5782 FieldAccessCallingConventionMIPS64 calling_convention;
5783 codegen_->GenerateUnresolvedFieldAccess(instruction,
5784 instruction->GetFieldType(),
5785 instruction->GetFieldIndex(),
5786 instruction->GetDexPc(),
5787 calling_convention);
5788}
5789
5790void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
5791 HUnresolvedStaticFieldSet* instruction) {
5792 FieldAccessCallingConventionMIPS64 calling_convention;
5793 codegen_->CreateUnresolvedFieldLocationSummary(
5794 instruction, instruction->GetFieldType(), calling_convention);
5795}
5796
5797void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
5798 HUnresolvedStaticFieldSet* instruction) {
5799 FieldAccessCallingConventionMIPS64 calling_convention;
5800 codegen_->GenerateUnresolvedFieldAccess(instruction,
5801 instruction->GetFieldType(),
5802 instruction->GetFieldIndex(),
5803 instruction->GetDexPc(),
5804 calling_convention);
5805}
5806
Alexey Frunze4dda3372015-06-01 18:31:49 -07005807void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005808 LocationSummary* locations =
5809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005810 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07005811}
5812
5813void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
5814 HBasicBlock* block = instruction->GetBlock();
5815 if (block->GetLoopInformation() != nullptr) {
5816 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5817 // The back edge will generate the suspend check.
5818 return;
5819 }
5820 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5821 // The goto will generate the suspend check.
5822 return;
5823 }
5824 GenerateSuspendCheck(instruction, nullptr);
5825}
5826
Alexey Frunze4dda3372015-06-01 18:31:49 -07005827void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
5828 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005830 InvokeRuntimeCallingConvention calling_convention;
5831 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5832}
5833
5834void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01005835 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005836 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5837}
5838
5839void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5840 Primitive::Type input_type = conversion->GetInputType();
5841 Primitive::Type result_type = conversion->GetResultType();
5842 DCHECK_NE(input_type, result_type);
5843
5844 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5845 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5846 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5847 }
5848
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
5850
5851 if (Primitive::IsFloatingPointType(input_type)) {
5852 locations->SetInAt(0, Location::RequiresFpuRegister());
5853 } else {
5854 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07005855 }
5856
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005857 if (Primitive::IsFloatingPointType(result_type)) {
5858 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005859 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005861 }
5862}
5863
5864void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
5865 LocationSummary* locations = conversion->GetLocations();
5866 Primitive::Type result_type = conversion->GetResultType();
5867 Primitive::Type input_type = conversion->GetInputType();
5868
5869 DCHECK_NE(input_type, result_type);
5870
5871 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5872 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5873 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5874
5875 switch (result_type) {
5876 case Primitive::kPrimChar:
5877 __ Andi(dst, src, 0xFFFF);
5878 break;
5879 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005880 if (input_type == Primitive::kPrimLong) {
5881 // Type conversion from long to types narrower than int is a result of code
5882 // transformations. To avoid unpredictable results for SEB and SEH, we first
5883 // need to sign-extend the low 32-bit value into bits 32 through 63.
5884 __ Sll(dst, src, 0);
5885 __ Seb(dst, dst);
5886 } else {
5887 __ Seb(dst, src);
5888 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005889 break;
5890 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00005891 if (input_type == Primitive::kPrimLong) {
5892 // Type conversion from long to types narrower than int is a result of code
5893 // transformations. To avoid unpredictable results for SEB and SEH, we first
5894 // need to sign-extend the low 32-bit value into bits 32 through 63.
5895 __ Sll(dst, src, 0);
5896 __ Seh(dst, dst);
5897 } else {
5898 __ Seh(dst, src);
5899 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005900 break;
5901 case Primitive::kPrimInt:
5902 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01005903 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
5904 // conversions, except when the input and output registers are the same and we are not
5905 // converting longs to shorter types. In these cases, do nothing.
5906 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
5907 __ Sll(dst, src, 0);
5908 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005909 break;
5910
5911 default:
5912 LOG(FATAL) << "Unexpected type conversion from " << input_type
5913 << " to " << result_type;
5914 }
5915 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005916 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
5917 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
5918 if (input_type == Primitive::kPrimLong) {
5919 __ Dmtc1(src, FTMP);
5920 if (result_type == Primitive::kPrimFloat) {
5921 __ Cvtsl(dst, FTMP);
5922 } else {
5923 __ Cvtdl(dst, FTMP);
5924 }
5925 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07005926 __ Mtc1(src, FTMP);
5927 if (result_type == Primitive::kPrimFloat) {
5928 __ Cvtsw(dst, FTMP);
5929 } else {
5930 __ Cvtdw(dst, FTMP);
5931 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07005932 }
5933 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5934 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005935 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
5936 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
5937 Mips64Label truncate;
5938 Mips64Label done;
5939
5940 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5941 // value when the input is either a NaN or is outside of the range of the output type
5942 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5943 // the same result.
5944 //
5945 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5946 // value of the output type if the input is outside of the range after the truncation or
5947 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5948 // results. This matches the desired float/double-to-int/long conversion exactly.
5949 //
5950 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5951 //
5952 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5953 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5954 // even though it must be NAN2008=1 on R6.
5955 //
5956 // The code takes care of the different behaviors by first comparing the input to the
5957 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5958 // If the input is greater than or equal to the minimum, it procedes to the truncate
5959 // instruction, which will handle such an input the same way irrespective of NAN2008.
5960 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5961 // in order to return either zero or the minimum value.
5962 //
5963 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5964 // truncate instruction for MIPS64R6.
5965 if (input_type == Primitive::kPrimFloat) {
5966 uint32_t min_val = (result_type == Primitive::kPrimLong)
5967 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
5968 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5969 __ LoadConst32(TMP, min_val);
5970 __ Mtc1(TMP, FTMP);
5971 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005972 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005973 uint64_t min_val = (result_type == Primitive::kPrimLong)
5974 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
5975 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5976 __ LoadConst64(TMP, min_val);
5977 __ Dmtc1(TMP, FTMP);
5978 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07005979 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005980
5981 __ Bc1nez(FTMP, &truncate);
5982
5983 if (input_type == Primitive::kPrimFloat) {
5984 __ CmpEqS(FTMP, src, src);
5985 } else {
5986 __ CmpEqD(FTMP, src, src);
5987 }
5988 if (result_type == Primitive::kPrimLong) {
5989 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
5990 } else {
5991 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5992 }
5993 __ Mfc1(TMP, FTMP);
5994 __ And(dst, dst, TMP);
5995
5996 __ Bc(&done);
5997
5998 __ Bind(&truncate);
5999
6000 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00006001 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006002 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006003 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006004 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006005 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006006 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006007 } else {
6008 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006009 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006010 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006011 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00006012 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006013 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00006014 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006015
6016 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006017 } else if (Primitive::IsFloatingPointType(result_type) &&
6018 Primitive::IsFloatingPointType(input_type)) {
6019 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
6020 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
6021 if (result_type == Primitive::kPrimFloat) {
6022 __ Cvtsd(dst, src);
6023 } else {
6024 __ Cvtds(dst, src);
6025 }
6026 } else {
6027 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6028 << " to " << result_type;
6029 }
6030}
6031
6032void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
6033 HandleShift(ushr);
6034}
6035
6036void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
6037 HandleShift(ushr);
6038}
6039
6040void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
6041 HandleBinaryOp(instruction);
6042}
6043
6044void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
6045 HandleBinaryOp(instruction);
6046}
6047
6048void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6049 // Nothing to do, this should be removed during prepare for register allocator.
6050 LOG(FATAL) << "Unreachable";
6051}
6052
6053void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6054 // Nothing to do, this should be removed during prepare for register allocator.
6055 LOG(FATAL) << "Unreachable";
6056}
6057
6058void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006059 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006060}
6061
6062void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006064}
6065
6066void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006067 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006068}
6069
6070void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006071 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006072}
6073
6074void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006075 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006076}
6077
6078void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006079 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006080}
6081
6082void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006083 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006084}
6085
6086void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006087 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006088}
6089
6090void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006091 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006092}
6093
6094void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006095 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006096}
6097
6098void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006099 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006100}
6101
6102void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006103 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07006104}
6105
Aart Bike9f37602015-10-09 11:15:55 -07006106void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006108}
6109
6110void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006112}
6113
6114void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006116}
6117
6118void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006120}
6121
6122void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006124}
6125
6126void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006128}
6129
6130void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006131 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006132}
6133
6134void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006135 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07006136}
6137
Mark Mendellfe57faa2015-09-18 09:26:15 -04006138// Simple implementation of packed switch - generate cascaded compare/jumps.
6139void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6140 LocationSummary* locations =
6141 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6142 locations->SetInAt(0, Location::RequiresRegister());
6143}
6144
Alexey Frunze0960ac52016-12-20 17:24:59 -08006145void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
6146 int32_t lower_bound,
6147 uint32_t num_entries,
6148 HBasicBlock* switch_block,
6149 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006150 // Create a set of compare/jumps.
6151 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08006152 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006153 // Jump to default if index is negative
6154 // Note: We don't check the case that index is positive while value < lower_bound, because in
6155 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6156 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
6157
Alexey Frunze0960ac52016-12-20 17:24:59 -08006158 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006159 // Jump to successors[0] if value == lower_bound.
6160 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
6161 int32_t last_index = 0;
6162 for (; num_entries - last_index > 2; last_index += 2) {
6163 __ Addiu(temp_reg, temp_reg, -2);
6164 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6165 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6166 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6167 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6168 }
6169 if (num_entries - last_index == 2) {
6170 // The last missing case_value.
6171 __ Addiu(temp_reg, temp_reg, -1);
6172 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006173 }
6174
6175 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08006176 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07006177 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006178 }
6179}
6180
Alexey Frunze0960ac52016-12-20 17:24:59 -08006181void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
6182 int32_t lower_bound,
6183 uint32_t num_entries,
6184 HBasicBlock* switch_block,
6185 HBasicBlock* default_block) {
6186 // Create a jump table.
6187 std::vector<Mips64Label*> labels(num_entries);
6188 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6189 for (uint32_t i = 0; i < num_entries; i++) {
6190 labels[i] = codegen_->GetLabelOf(successors[i]);
6191 }
6192 JumpTable* table = __ CreateJumpTable(std::move(labels));
6193
6194 // Is the value in range?
6195 __ Addiu32(TMP, value_reg, -lower_bound);
6196 __ LoadConst32(AT, num_entries);
6197 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
6198
6199 // We are in the range of the table.
6200 // Load the target address from the jump table, indexing by the value.
6201 __ LoadLabelAddress(AT, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07006202 __ Dlsa(TMP, TMP, AT, 2);
Alexey Frunze0960ac52016-12-20 17:24:59 -08006203 __ Lw(TMP, TMP, 0);
6204 // Compute the absolute target address by adding the table start address
6205 // (the table contains offsets to targets relative to its start).
6206 __ Daddu(TMP, TMP, AT);
6207 // And jump.
6208 __ Jr(TMP);
6209 __ Nop();
6210}
6211
6212void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6213 int32_t lower_bound = switch_instr->GetStartValue();
6214 uint32_t num_entries = switch_instr->GetNumEntries();
6215 LocationSummary* locations = switch_instr->GetLocations();
6216 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
6217 HBasicBlock* switch_block = switch_instr->GetBlock();
6218 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6219
6220 if (num_entries > kPackedSwitchJumpTableThreshold) {
6221 GenTableBasedPackedSwitch(value_reg,
6222 lower_bound,
6223 num_entries,
6224 switch_block,
6225 default_block);
6226 } else {
6227 GenPackedSwitchWithCompares(value_reg,
6228 lower_bound,
6229 num_entries,
6230 switch_block,
6231 default_block);
6232 }
6233}
6234
Chris Larsenc9905a62017-03-13 17:06:18 -07006235void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6236 LocationSummary* locations =
6237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6238 locations->SetInAt(0, Location::RequiresRegister());
6239 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006240}
6241
Chris Larsenc9905a62017-03-13 17:06:18 -07006242void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet* instruction) {
6243 LocationSummary* locations = instruction->GetLocations();
6244 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
6245 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6246 instruction->GetIndex(), kMips64PointerSize).SizeValue();
6247 __ LoadFromOffset(kLoadDoubleword,
6248 locations->Out().AsRegister<GpuRegister>(),
6249 locations->InAt(0).AsRegister<GpuRegister>(),
6250 method_offset);
6251 } else {
6252 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
6253 instruction->GetIndex(), kMips64PointerSize));
6254 __ LoadFromOffset(kLoadDoubleword,
6255 locations->Out().AsRegister<GpuRegister>(),
6256 locations->InAt(0).AsRegister<GpuRegister>(),
6257 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
6258 __ LoadFromOffset(kLoadDoubleword,
6259 locations->Out().AsRegister<GpuRegister>(),
6260 locations->Out().AsRegister<GpuRegister>(),
6261 method_offset);
6262 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006263}
6264
Alexey Frunze4dda3372015-06-01 18:31:49 -07006265} // namespace mips64
6266} // namespace art