blob: d3f3598e2aed0fe34f999658af9dbc70614d1b13 [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:
David Srbecky9cd6d372016-02-09 15:24:47 +0000339 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
347 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
348
349 __ Bind(GetEntryLabel());
350 SaveLiveRegisters(codegen, locations);
351
352 // We're moving two locations to locations that could overlap, so we need a parallel
353 // move resolver.
354 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800355 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
357 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
360 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100362 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Primitive::Type ret_type = instruction_->GetType();
365 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
366 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 } else {
368 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800369 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
370 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 }
372
373 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700374 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 }
376
Roland Levillain46648892015-06-19 16:07:18 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
378
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
381};
382
383class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800389 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100391 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 }
394
Roland Levillain46648892015-06-19 16:07:18 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
396
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
399};
400
401CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
402 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100403 const CompilerOptions& compiler_options,
404 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 : CodeGenerator(graph,
406 kNumberOfGpuRegisters,
407 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000408 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
410 arraysize(kCoreCalleeSaves)),
411 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
412 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100413 compiler_options,
414 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100415 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700416 location_builder_(graph, this),
417 instruction_visitor_(graph, this),
418 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100419 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800420 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800421 uint32_literals_(std::less<uint32_t>(),
422 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800423 uint64_literals_(std::less<uint64_t>(),
424 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800425 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800426 boot_image_string_patches_(StringReferenceValueComparator(),
427 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
428 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
429 boot_image_type_patches_(TypeReferenceValueComparator(),
430 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
431 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000432 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800433 boot_image_address_patches_(std::less<uint32_t>(),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800434 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
435 jit_string_patches_(StringReferenceValueComparator(),
436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
437 jit_class_patches_(TypeReferenceValueComparator(),
438 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700439 // Save RA (containing the return address) to mimic Quick.
440 AddAllocatedRegister(Location::RegisterLocation(RA));
441}
442
443#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100444// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
445#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700446#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700447
448void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700449 // Ensure that we fix up branches.
450 __ FinalizeCode();
451
452 // Adjust native pc offsets in stack maps.
453 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800454 uint32_t old_position =
455 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700456 uint32_t new_position = __ GetAdjustedPosition(old_position);
457 DCHECK_GE(new_position, old_position);
458 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
459 }
460
461 // Adjust pc offsets for the disassembly information.
462 if (disasm_info_ != nullptr) {
463 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
464 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
465 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
466 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
467 it.second.start = __ GetAdjustedPosition(it.second.start);
468 it.second.end = __ GetAdjustedPosition(it.second.end);
469 }
470 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
471 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
472 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
473 }
474 }
475
Alexey Frunze4dda3372015-06-01 18:31:49 -0700476 CodeGenerator::Finalize(allocator);
477}
478
479Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
480 return codegen_->GetAssembler();
481}
482
483void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100484 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700485 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
486}
487
488void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100489 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
491}
492
493void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
494 // Pop reg
495 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200496 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497}
498
499void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
500 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200501 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700502 __ Sd(GpuRegister(reg), SP, 0);
503}
504
505void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
506 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
507 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
508 // Allocate a scratch register other than TMP, if available.
509 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
510 // automatically unspilled when the scratch scope object is destroyed).
511 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
512 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200513 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700514 __ LoadFromOffset(load_type,
515 GpuRegister(ensure_scratch.GetRegister()),
516 SP,
517 index1 + stack_offset);
518 __ LoadFromOffset(load_type,
519 TMP,
520 SP,
521 index2 + stack_offset);
522 __ StoreToOffset(store_type,
523 GpuRegister(ensure_scratch.GetRegister()),
524 SP,
525 index2 + stack_offset);
526 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
527}
528
529static dwarf::Reg DWARFReg(GpuRegister reg) {
530 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
531}
532
David Srbeckyba702002016-02-01 18:15:29 +0000533static dwarf::Reg DWARFReg(FpuRegister reg) {
534 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
535}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700536
537void CodeGeneratorMIPS64::GenerateFrameEntry() {
538 __ Bind(&frame_entry_label_);
539
540 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
541
542 if (do_overflow_check) {
543 __ LoadFromOffset(kLoadWord,
544 ZERO,
545 SP,
546 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
547 RecordPcInfo(nullptr, 0);
548 }
549
Alexey Frunze4dda3372015-06-01 18:31:49 -0700550 if (HasEmptyFrame()) {
551 return;
552 }
553
554 // Make sure the frame size isn't unreasonably large. Per the various APIs
555 // it looks like it should always be less than 2GB in size, which allows
556 // us using 32-bit signed offsets from the stack pointer.
557 if (GetFrameSize() > 0x7FFFFFFF)
558 LOG(FATAL) << "Stack frame larger than 2GB";
559
560 // Spill callee-saved registers.
561 // Note that their cumulative size is small and they can be indexed using
562 // 16-bit offsets.
563
564 // TODO: increment/decrement SP in one step instead of two or remove this comment.
565
566 uint32_t ofs = FrameEntrySpillSize();
567 __ IncreaseFrameSize(ofs);
568
569 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
570 GpuRegister reg = kCoreCalleeSaves[i];
571 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200572 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700573 __ Sd(reg, SP, ofs);
574 __ cfi().RelOffset(DWARFReg(reg), ofs);
575 }
576 }
577
578 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
579 FpuRegister reg = kFpuCalleeSaves[i];
580 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200581 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700582 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000583 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700584 }
585 }
586
587 // Allocate the rest of the frame and store the current method pointer
588 // at its end.
589
590 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
591
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100592 // Save the current method if we need it. Note that we do not
593 // do this in HCurrentMethod, as the instruction might have been removed
594 // in the SSA graph.
595 if (RequiresCurrentMethod()) {
596 static_assert(IsInt<16>(kCurrentMethodStackOffset),
597 "kCurrentMethodStackOffset must fit into int16_t");
598 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
599 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100600
601 if (GetGraph()->HasShouldDeoptimizeFlag()) {
602 // Initialize should_deoptimize flag to 0.
603 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
604 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700605}
606
607void CodeGeneratorMIPS64::GenerateFrameExit() {
608 __ cfi().RememberState();
609
Alexey Frunze4dda3372015-06-01 18:31:49 -0700610 if (!HasEmptyFrame()) {
611 // Deallocate the rest of the frame.
612
613 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
614
615 // Restore callee-saved registers.
616 // Note that their cumulative size is small and they can be indexed using
617 // 16-bit offsets.
618
619 // TODO: increment/decrement SP in one step instead of two or remove this comment.
620
621 uint32_t ofs = 0;
622
623 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
624 FpuRegister reg = kFpuCalleeSaves[i];
625 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
626 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200627 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000628 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700629 }
630 }
631
632 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
633 GpuRegister reg = kCoreCalleeSaves[i];
634 if (allocated_registers_.ContainsCoreRegister(reg)) {
635 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200636 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700637 __ cfi().Restore(DWARFReg(reg));
638 }
639 }
640
641 DCHECK_EQ(ofs, FrameEntrySpillSize());
642 __ DecreaseFrameSize(ofs);
643 }
644
645 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700646 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700647
648 __ cfi().RestoreState();
649 __ cfi().DefCFAOffset(GetFrameSize());
650}
651
652void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
653 __ Bind(GetLabelOf(block));
654}
655
656void CodeGeneratorMIPS64::MoveLocation(Location destination,
657 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100658 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 if (source.Equals(destination)) {
660 return;
661 }
662
663 // A valid move can always be inferred from the destination and source
664 // locations. When moving from and to a register, the argument type can be
665 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100666 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700667 DCHECK_EQ(unspecified_type, false);
668
669 if (destination.IsRegister() || destination.IsFpuRegister()) {
670 if (unspecified_type) {
671 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
672 if (source.IsStackSlot() ||
673 (src_cst != nullptr && (src_cst->IsIntConstant()
674 || src_cst->IsFloatConstant()
675 || src_cst->IsNullConstant()))) {
676 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100677 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 } else {
679 // If the source is a double stack slot or a 64bit constant, a 64bit
680 // type is appropriate. Else the source is a register, and since the
681 // type has not been specified, we chose a 64bit type to force a 64bit
682 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 }
685 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100686 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
687 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700688 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
689 // Move to GPR/FPR from stack
690 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 __ LoadFpuFromOffset(load_type,
693 destination.AsFpuRegister<FpuRegister>(),
694 SP,
695 source.GetStackIndex());
696 } else {
697 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
698 __ LoadFromOffset(load_type,
699 destination.AsRegister<GpuRegister>(),
700 SP,
701 source.GetStackIndex());
702 }
703 } else if (source.IsConstant()) {
704 // Move to GPR/FPR from constant
705 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 gpr = destination.AsRegister<GpuRegister>();
708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700710 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
711 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
712 gpr = ZERO;
713 } else {
714 __ LoadConst32(gpr, value);
715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700717 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
718 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
719 gpr = ZERO;
720 } else {
721 __ LoadConst64(gpr, value);
722 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700723 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700725 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100726 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700727 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
728 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 if (destination.IsRegister()) {
731 // Move to GPR from GPR
732 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
733 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100734 DCHECK(destination.IsFpuRegister());
735 if (Primitive::Is64BitType(dst_type)) {
736 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
737 } else {
738 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
739 }
740 }
741 } else if (source.IsFpuRegister()) {
742 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700743 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100744 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700745 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
746 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100747 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
749 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100750 } else {
751 DCHECK(destination.IsRegister());
752 if (Primitive::Is64BitType(dst_type)) {
753 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
754 } else {
755 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 }
758 }
759 } else { // The destination is not a register. It must be a stack slot.
760 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
761 if (source.IsRegister() || source.IsFpuRegister()) {
762 if (unspecified_type) {
763 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100764 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700765 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100766 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700767 }
768 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100769 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
770 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700771 // Move to stack from GPR/FPR
772 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
773 if (source.IsRegister()) {
774 __ StoreToOffset(store_type,
775 source.AsRegister<GpuRegister>(),
776 SP,
777 destination.GetStackIndex());
778 } else {
779 __ StoreFpuToOffset(store_type,
780 source.AsFpuRegister<FpuRegister>(),
781 SP,
782 destination.GetStackIndex());
783 }
784 } else if (source.IsConstant()) {
785 // Move to stack from constant
786 HConstant* src_cst = source.GetConstant();
787 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
791 if (value != 0) {
792 gpr = TMP;
793 __ LoadConst32(gpr, value);
794 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700795 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700796 DCHECK(destination.IsDoubleStackSlot());
797 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
798 if (value != 0) {
799 gpr = TMP;
800 __ LoadConst64(gpr, value);
801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700802 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700803 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700804 } else {
805 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
806 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
807 // Move to stack from stack
808 if (destination.IsStackSlot()) {
809 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
810 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
811 } else {
812 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
813 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
814 }
815 }
816 }
817}
818
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700819void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700820 DCHECK(!loc1.IsConstant());
821 DCHECK(!loc2.IsConstant());
822
823 if (loc1.Equals(loc2)) {
824 return;
825 }
826
827 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
828 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
829 bool is_fp_reg1 = loc1.IsFpuRegister();
830 bool is_fp_reg2 = loc2.IsFpuRegister();
831
832 if (loc2.IsRegister() && loc1.IsRegister()) {
833 // Swap 2 GPRs
834 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
835 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
836 __ Move(TMP, r2);
837 __ Move(r2, r1);
838 __ Move(r1, TMP);
839 } else if (is_fp_reg2 && is_fp_reg1) {
840 // Swap 2 FPRs
841 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
842 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700843 if (type == Primitive::kPrimFloat) {
844 __ MovS(FTMP, r1);
845 __ MovS(r1, r2);
846 __ MovS(r2, FTMP);
847 } else {
848 DCHECK_EQ(type, Primitive::kPrimDouble);
849 __ MovD(FTMP, r1);
850 __ MovD(r1, r2);
851 __ MovD(r2, FTMP);
852 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700853 } else if (is_slot1 != is_slot2) {
854 // Swap GPR/FPR and stack slot
855 Location reg_loc = is_slot1 ? loc2 : loc1;
856 Location mem_loc = is_slot1 ? loc1 : loc2;
857 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
858 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
859 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
860 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
861 if (reg_loc.IsFpuRegister()) {
862 __ StoreFpuToOffset(store_type,
863 reg_loc.AsFpuRegister<FpuRegister>(),
864 SP,
865 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700866 if (mem_loc.IsStackSlot()) {
867 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
868 } else {
869 DCHECK(mem_loc.IsDoubleStackSlot());
870 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
871 }
872 } else {
873 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
874 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
875 }
876 } else if (is_slot1 && is_slot2) {
877 move_resolver_.Exchange(loc1.GetStackIndex(),
878 loc2.GetStackIndex(),
879 loc1.IsDoubleStackSlot());
880 } else {
881 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
882 }
883}
884
Calin Juravle175dc732015-08-25 15:42:32 +0100885void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
886 DCHECK(location.IsRegister());
887 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
888}
889
Calin Juravlee460d1d2015-09-29 04:52:17 +0100890void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
891 if (location.IsRegister()) {
892 locations->AddTemp(location);
893 } else {
894 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
895 }
896}
897
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100898void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
899 GpuRegister value,
900 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700901 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700902 GpuRegister card = AT;
903 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100904 if (value_can_be_null) {
905 __ Beqzc(value, &done);
906 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700907 __ LoadFromOffset(kLoadDoubleword,
908 card,
909 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700910 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
912 __ Daddu(temp, card, temp);
913 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100914 if (value_can_be_null) {
915 __ Bind(&done);
916 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700917}
918
Alexey Frunze19f6c692016-11-30 19:19:55 -0800919template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
920inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
921 const ArenaDeque<PcRelativePatchInfo>& infos,
922 ArenaVector<LinkerPatch>* linker_patches) {
923 for (const PcRelativePatchInfo& info : infos) {
924 const DexFile& dex_file = info.target_dex_file;
925 size_t offset_or_index = info.offset_or_index;
926 DCHECK(info.pc_rel_label.IsBound());
927 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
928 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
929 }
930}
931
932void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
933 DCHECK(linker_patches->empty());
934 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -0800935 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800936 pc_relative_string_patches_.size() +
937 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +0000938 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800939 boot_image_string_patches_.size() +
940 boot_image_type_patches_.size() +
941 boot_image_address_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -0800942 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800943 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
944 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800945 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +0000946 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800947 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
948 linker_patches);
949 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000950 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
951 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800952 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
953 linker_patches);
954 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000955 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
956 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800957 for (const auto& entry : boot_image_string_patches_) {
958 const StringReference& target_string = entry.first;
959 Literal* literal = entry.second;
960 DCHECK(literal->GetLabel()->IsBound());
961 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
962 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
963 target_string.dex_file,
964 target_string.string_index.index_));
965 }
966 for (const auto& entry : boot_image_type_patches_) {
967 const TypeReference& target_type = entry.first;
968 Literal* literal = entry.second;
969 DCHECK(literal->GetLabel()->IsBound());
970 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
971 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
972 target_type.dex_file,
973 target_type.type_index.index_));
974 }
975 for (const auto& entry : boot_image_address_patches_) {
976 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
977 Literal* literal = entry.second;
978 DCHECK(literal->GetLabel()->IsBound());
979 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
980 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
981 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000982 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800983}
984
985CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000986 const DexFile& dex_file, dex::StringIndex string_index) {
987 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800988}
989
990CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
991 const DexFile& dex_file, dex::TypeIndex type_index) {
992 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800993}
994
Vladimir Marko1998cd02017-01-13 13:02:58 +0000995CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
996 const DexFile& dex_file, dex::TypeIndex type_index) {
997 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
998}
999
Alexey Frunze19f6c692016-11-30 19:19:55 -08001000CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1001 const DexFile& dex_file, uint32_t element_offset) {
1002 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1003}
1004
Alexey Frunze19f6c692016-11-30 19:19:55 -08001005CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1006 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1007 patches->emplace_back(dex_file, offset_or_index);
1008 return &patches->back();
1009}
1010
Alexey Frunzef63f5692016-12-13 17:43:11 -08001011Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1012 return map->GetOrCreate(
1013 value,
1014 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1015}
1016
Alexey Frunze19f6c692016-11-30 19:19:55 -08001017Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1018 return uint64_literals_.GetOrCreate(
1019 value,
1020 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1021}
1022
1023Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1024 MethodToLiteralMap* map) {
1025 return map->GetOrCreate(
1026 target_method,
1027 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1028}
1029
Alexey Frunzef63f5692016-12-13 17:43:11 -08001030Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1031 dex::StringIndex string_index) {
1032 return boot_image_string_patches_.GetOrCreate(
1033 StringReference(&dex_file, string_index),
1034 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1035}
1036
1037Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1038 dex::TypeIndex type_index) {
1039 return boot_image_type_patches_.GetOrCreate(
1040 TypeReference(&dex_file, type_index),
1041 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1042}
1043
1044Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
1045 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1046 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1047 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1048}
1049
Alexey Frunze19f6c692016-11-30 19:19:55 -08001050void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1051 GpuRegister out) {
1052 __ Bind(&info->pc_rel_label);
1053 // Add the high half of a 32-bit offset to PC.
1054 __ Auipc(out, /* placeholder */ 0x1234);
1055 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001056 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001057}
1058
Alexey Frunze627c1a02017-01-30 19:28:14 -08001059Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1060 dex::StringIndex string_index,
1061 Handle<mirror::String> handle) {
1062 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1063 reinterpret_cast64<uint64_t>(handle.GetReference()));
1064 return jit_string_patches_.GetOrCreate(
1065 StringReference(&dex_file, string_index),
1066 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1067}
1068
1069Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1070 dex::TypeIndex type_index,
1071 Handle<mirror::Class> handle) {
1072 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1073 reinterpret_cast64<uint64_t>(handle.GetReference()));
1074 return jit_class_patches_.GetOrCreate(
1075 TypeReference(&dex_file, type_index),
1076 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1077}
1078
1079void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1080 const uint8_t* roots_data,
1081 const Literal* literal,
1082 uint64_t index_in_table) const {
1083 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1084 uintptr_t address =
1085 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1086 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1087}
1088
1089void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1090 for (const auto& entry : jit_string_patches_) {
1091 const auto& it = jit_string_roots_.find(entry.first);
1092 DCHECK(it != jit_string_roots_.end());
1093 PatchJitRootUse(code, roots_data, entry.second, it->second);
1094 }
1095 for (const auto& entry : jit_class_patches_) {
1096 const auto& it = jit_class_roots_.find(entry.first);
1097 DCHECK(it != jit_class_roots_.end());
1098 PatchJitRootUse(code, roots_data, entry.second, it->second);
1099 }
1100}
1101
David Brazdil58282f42016-01-14 12:45:10 +00001102void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001103 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1104 blocked_core_registers_[ZERO] = true;
1105 blocked_core_registers_[K0] = true;
1106 blocked_core_registers_[K1] = true;
1107 blocked_core_registers_[GP] = true;
1108 blocked_core_registers_[SP] = true;
1109 blocked_core_registers_[RA] = true;
1110
Lazar Trsicd9672662015-09-03 17:33:01 +02001111 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1112 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113 blocked_core_registers_[AT] = true;
1114 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001115 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001116 blocked_fpu_registers_[FTMP] = true;
1117
1118 // Reserve suspend and thread registers.
1119 blocked_core_registers_[S0] = true;
1120 blocked_core_registers_[TR] = true;
1121
1122 // Reserve T9 for function calls
1123 blocked_core_registers_[T9] = true;
1124
Goran Jakovljevic782be112016-06-21 12:39:04 +02001125 if (GetGraph()->IsDebuggable()) {
1126 // Stubs do not save callee-save floating point registers. If the graph
1127 // is debuggable, we need to deal with these registers differently. For
1128 // now, just block them.
1129 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1130 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1131 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 }
1133}
1134
Alexey Frunze4dda3372015-06-01 18:31:49 -07001135size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1136 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001137 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138}
1139
1140size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1141 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001142 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001143}
1144
1145size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1146 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001147 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001148}
1149
1150size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1151 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001152 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153}
1154
1155void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001156 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157}
1158
1159void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001160 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161}
1162
Calin Juravle175dc732015-08-25 15:42:32 +01001163void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001164 HInstruction* instruction,
1165 uint32_t dex_pc,
1166 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001167 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001168 __ LoadFromOffset(kLoadDoubleword,
1169 T9,
1170 TR,
1171 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001172 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001173 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001174 if (EntrypointRequiresStackMap(entrypoint)) {
1175 RecordPcInfo(instruction, dex_pc, slow_path);
1176 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001177}
1178
1179void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1180 GpuRegister class_reg) {
1181 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1182 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1183 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1184 // TODO: barrier needed?
1185 __ Bind(slow_path->GetExitLabel());
1186}
1187
1188void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1189 __ Sync(0); // only stype 0 is supported
1190}
1191
1192void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1193 HBasicBlock* successor) {
1194 SuspendCheckSlowPathMIPS64* slow_path =
1195 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1196 codegen_->AddSlowPath(slow_path);
1197
1198 __ LoadFromOffset(kLoadUnsignedHalfword,
1199 TMP,
1200 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001201 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001202 if (successor == nullptr) {
1203 __ Bnezc(TMP, slow_path->GetEntryLabel());
1204 __ Bind(slow_path->GetReturnLabel());
1205 } else {
1206 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001207 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 // slow_path will return to GetLabelOf(successor).
1209 }
1210}
1211
1212InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1213 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001214 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 assembler_(codegen->GetAssembler()),
1216 codegen_(codegen) {}
1217
1218void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1219 DCHECK_EQ(instruction->InputCount(), 2U);
1220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1221 Primitive::Type type = instruction->GetResultType();
1222 switch (type) {
1223 case Primitive::kPrimInt:
1224 case Primitive::kPrimLong: {
1225 locations->SetInAt(0, Location::RequiresRegister());
1226 HInstruction* right = instruction->InputAt(1);
1227 bool can_use_imm = false;
1228 if (right->IsConstant()) {
1229 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1230 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1231 can_use_imm = IsUint<16>(imm);
1232 } else if (instruction->IsAdd()) {
1233 can_use_imm = IsInt<16>(imm);
1234 } else {
1235 DCHECK(instruction->IsSub());
1236 can_use_imm = IsInt<16>(-imm);
1237 }
1238 }
1239 if (can_use_imm)
1240 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1241 else
1242 locations->SetInAt(1, Location::RequiresRegister());
1243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1244 }
1245 break;
1246
1247 case Primitive::kPrimFloat:
1248 case Primitive::kPrimDouble:
1249 locations->SetInAt(0, Location::RequiresFpuRegister());
1250 locations->SetInAt(1, Location::RequiresFpuRegister());
1251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1252 break;
1253
1254 default:
1255 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1256 }
1257}
1258
1259void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1260 Primitive::Type type = instruction->GetType();
1261 LocationSummary* locations = instruction->GetLocations();
1262
1263 switch (type) {
1264 case Primitive::kPrimInt:
1265 case Primitive::kPrimLong: {
1266 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1267 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1268 Location rhs_location = locations->InAt(1);
1269
1270 GpuRegister rhs_reg = ZERO;
1271 int64_t rhs_imm = 0;
1272 bool use_imm = rhs_location.IsConstant();
1273 if (use_imm) {
1274 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1275 } else {
1276 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1277 }
1278
1279 if (instruction->IsAnd()) {
1280 if (use_imm)
1281 __ Andi(dst, lhs, rhs_imm);
1282 else
1283 __ And(dst, lhs, rhs_reg);
1284 } else if (instruction->IsOr()) {
1285 if (use_imm)
1286 __ Ori(dst, lhs, rhs_imm);
1287 else
1288 __ Or(dst, lhs, rhs_reg);
1289 } else if (instruction->IsXor()) {
1290 if (use_imm)
1291 __ Xori(dst, lhs, rhs_imm);
1292 else
1293 __ Xor(dst, lhs, rhs_reg);
1294 } else if (instruction->IsAdd()) {
1295 if (type == Primitive::kPrimInt) {
1296 if (use_imm)
1297 __ Addiu(dst, lhs, rhs_imm);
1298 else
1299 __ Addu(dst, lhs, rhs_reg);
1300 } else {
1301 if (use_imm)
1302 __ Daddiu(dst, lhs, rhs_imm);
1303 else
1304 __ Daddu(dst, lhs, rhs_reg);
1305 }
1306 } else {
1307 DCHECK(instruction->IsSub());
1308 if (type == Primitive::kPrimInt) {
1309 if (use_imm)
1310 __ Addiu(dst, lhs, -rhs_imm);
1311 else
1312 __ Subu(dst, lhs, rhs_reg);
1313 } else {
1314 if (use_imm)
1315 __ Daddiu(dst, lhs, -rhs_imm);
1316 else
1317 __ Dsubu(dst, lhs, rhs_reg);
1318 }
1319 }
1320 break;
1321 }
1322 case Primitive::kPrimFloat:
1323 case Primitive::kPrimDouble: {
1324 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1325 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1326 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1327 if (instruction->IsAdd()) {
1328 if (type == Primitive::kPrimFloat)
1329 __ AddS(dst, lhs, rhs);
1330 else
1331 __ AddD(dst, lhs, rhs);
1332 } else if (instruction->IsSub()) {
1333 if (type == Primitive::kPrimFloat)
1334 __ SubS(dst, lhs, rhs);
1335 else
1336 __ SubD(dst, lhs, rhs);
1337 } else {
1338 LOG(FATAL) << "Unexpected floating-point binary operation";
1339 }
1340 break;
1341 }
1342 default:
1343 LOG(FATAL) << "Unexpected binary operation type " << type;
1344 }
1345}
1346
1347void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001348 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001349
1350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1351 Primitive::Type type = instr->GetResultType();
1352 switch (type) {
1353 case Primitive::kPrimInt:
1354 case Primitive::kPrimLong: {
1355 locations->SetInAt(0, Location::RequiresRegister());
1356 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001358 break;
1359 }
1360 default:
1361 LOG(FATAL) << "Unexpected shift type " << type;
1362 }
1363}
1364
1365void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001366 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001367 LocationSummary* locations = instr->GetLocations();
1368 Primitive::Type type = instr->GetType();
1369
1370 switch (type) {
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimLong: {
1373 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1374 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1375 Location rhs_location = locations->InAt(1);
1376
1377 GpuRegister rhs_reg = ZERO;
1378 int64_t rhs_imm = 0;
1379 bool use_imm = rhs_location.IsConstant();
1380 if (use_imm) {
1381 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1382 } else {
1383 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1384 }
1385
1386 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001387 uint32_t shift_value = rhs_imm &
1388 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001389
Alexey Frunze92d90602015-12-18 18:16:36 -08001390 if (shift_value == 0) {
1391 if (dst != lhs) {
1392 __ Move(dst, lhs);
1393 }
1394 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001395 if (instr->IsShl()) {
1396 __ Sll(dst, lhs, shift_value);
1397 } else if (instr->IsShr()) {
1398 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001399 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001400 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001401 } else {
1402 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001403 }
1404 } else {
1405 if (shift_value < 32) {
1406 if (instr->IsShl()) {
1407 __ Dsll(dst, lhs, shift_value);
1408 } else if (instr->IsShr()) {
1409 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001410 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001411 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001412 } else {
1413 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 }
1415 } else {
1416 shift_value -= 32;
1417 if (instr->IsShl()) {
1418 __ Dsll32(dst, lhs, shift_value);
1419 } else if (instr->IsShr()) {
1420 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001421 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001422 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001423 } else {
1424 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 }
1426 }
1427 }
1428 } else {
1429 if (type == Primitive::kPrimInt) {
1430 if (instr->IsShl()) {
1431 __ Sllv(dst, lhs, rhs_reg);
1432 } else if (instr->IsShr()) {
1433 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001434 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001435 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001436 } else {
1437 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001438 }
1439 } else {
1440 if (instr->IsShl()) {
1441 __ Dsllv(dst, lhs, rhs_reg);
1442 } else if (instr->IsShr()) {
1443 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001444 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001445 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001446 } else {
1447 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001448 }
1449 }
1450 }
1451 break;
1452 }
1453 default:
1454 LOG(FATAL) << "Unexpected shift operation type " << type;
1455 }
1456}
1457
1458void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1459 HandleBinaryOp(instruction);
1460}
1461
1462void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1463 HandleBinaryOp(instruction);
1464}
1465
1466void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1467 HandleBinaryOp(instruction);
1468}
1469
1470void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1471 HandleBinaryOp(instruction);
1472}
1473
1474void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1475 LocationSummary* locations =
1476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1479 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1480 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1481 } else {
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 }
1484}
1485
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001486static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
1487 auto null_checker = [codegen, instruction]() {
1488 codegen->MaybeRecordImplicitNullCheck(instruction);
1489 };
1490 return null_checker;
1491}
1492
Alexey Frunze4dda3372015-06-01 18:31:49 -07001493void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1494 LocationSummary* locations = instruction->GetLocations();
1495 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1496 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001497 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001498 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001499
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001500 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001501 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1502 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001503 switch (type) {
1504 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001505 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1506 if (index.IsConstant()) {
1507 size_t offset =
1508 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001509 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001510 } else {
1511 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001512 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001513 }
1514 break;
1515 }
1516
1517 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001518 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1519 if (index.IsConstant()) {
1520 size_t offset =
1521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001522 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001523 } else {
1524 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001525 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001526 }
1527 break;
1528 }
1529
1530 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001531 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1532 if (index.IsConstant()) {
1533 size_t offset =
1534 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001535 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001536 } else {
1537 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1538 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001539 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001540 }
1541 break;
1542 }
1543
1544 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001545 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001546 if (maybe_compressed_char_at) {
1547 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001548 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001549 __ Dext(TMP, TMP, 0, 1);
1550 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1551 "Expecting 0=compressed, 1=uncompressed");
1552 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001553 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001554 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1555 if (maybe_compressed_char_at) {
1556 Mips64Label uncompressed_load, done;
1557 __ Bnezc(TMP, &uncompressed_load);
1558 __ LoadFromOffset(kLoadUnsignedByte,
1559 out,
1560 obj,
1561 data_offset + (const_index << TIMES_1));
1562 __ Bc(&done);
1563 __ Bind(&uncompressed_load);
1564 __ LoadFromOffset(kLoadUnsignedHalfword,
1565 out,
1566 obj,
1567 data_offset + (const_index << TIMES_2));
1568 __ Bind(&done);
1569 } else {
1570 __ LoadFromOffset(kLoadUnsignedHalfword,
1571 out,
1572 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001573 data_offset + (const_index << TIMES_2),
1574 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001575 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001576 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001577 GpuRegister index_reg = index.AsRegister<GpuRegister>();
1578 if (maybe_compressed_char_at) {
1579 Mips64Label uncompressed_load, done;
1580 __ Bnezc(TMP, &uncompressed_load);
1581 __ Daddu(TMP, obj, index_reg);
1582 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1583 __ Bc(&done);
1584 __ Bind(&uncompressed_load);
1585 __ Dsll(TMP, index_reg, TIMES_2);
1586 __ Daddu(TMP, obj, TMP);
1587 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1588 __ Bind(&done);
1589 } else {
1590 __ Dsll(TMP, index_reg, TIMES_2);
1591 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001592 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001593 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001594 }
1595 break;
1596 }
1597
1598 case Primitive::kPrimInt:
1599 case Primitive::kPrimNot: {
1600 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001601 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1602 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1603 if (index.IsConstant()) {
1604 size_t offset =
1605 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001606 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001607 } else {
1608 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1609 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001610 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001616 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1617 if (index.IsConstant()) {
1618 size_t offset =
1619 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001620 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001621 } else {
1622 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1623 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001624 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001625 }
1626 break;
1627 }
1628
1629 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001630 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1631 if (index.IsConstant()) {
1632 size_t offset =
1633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001634 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001635 } else {
1636 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1637 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001638 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001639 }
1640 break;
1641 }
1642
1643 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001644 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1645 if (index.IsConstant()) {
1646 size_t offset =
1647 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001648 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001649 } else {
1650 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1651 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001652 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001653 }
1654 break;
1655 }
1656
1657 case Primitive::kPrimVoid:
1658 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1659 UNREACHABLE();
1660 }
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001661
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001662 if (!maybe_compressed_char_at) {
1663 codegen_->MaybeRecordImplicitNullCheck(instruction);
1664 }
Alexey Frunzec061de12017-02-14 13:27:23 -08001665
1666 if (type == Primitive::kPrimNot) {
1667 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1668 __ MaybeUnpoisonHeapReference(out);
1669 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001670}
1671
1672void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1673 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1676}
1677
1678void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1679 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001680 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1682 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1683 __ LoadFromOffset(kLoadWord, out, obj, offset);
1684 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001685 // Mask out compression flag from String's array length.
1686 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
1687 __ Srl(out, out, 1u);
1688 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001689}
1690
1691void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001692 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1694 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001695 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001696 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001697 InvokeRuntimeCallingConvention calling_convention;
1698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1699 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1700 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1701 } else {
1702 locations->SetInAt(0, Location::RequiresRegister());
1703 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1704 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1705 locations->SetInAt(2, Location::RequiresFpuRegister());
1706 } else {
1707 locations->SetInAt(2, Location::RequiresRegister());
1708 }
1709 }
1710}
1711
1712void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1713 LocationSummary* locations = instruction->GetLocations();
1714 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1715 Location index = locations->InAt(1);
1716 Primitive::Type value_type = instruction->GetComponentType();
1717 bool needs_runtime_call = locations->WillCall();
1718 bool needs_write_barrier =
1719 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001720 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001721
1722 switch (value_type) {
1723 case Primitive::kPrimBoolean:
1724 case Primitive::kPrimByte: {
1725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1726 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1727 if (index.IsConstant()) {
1728 size_t offset =
1729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001730 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731 } else {
1732 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001733 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001734 }
1735 break;
1736 }
1737
1738 case Primitive::kPrimShort:
1739 case Primitive::kPrimChar: {
1740 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1741 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1742 if (index.IsConstant()) {
1743 size_t offset =
1744 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001745 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001746 } else {
1747 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1748 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001749 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001750 }
1751 break;
1752 }
1753
1754 case Primitive::kPrimInt:
1755 case Primitive::kPrimNot: {
1756 if (!needs_runtime_call) {
1757 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08001758 GpuRegister base_reg;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001759 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1760 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001761 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
1762 base_reg = obj;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001763 } else {
1764 DCHECK(index.IsRegister()) << index;
1765 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1766 __ Daddu(TMP, obj, TMP);
Alexey Frunzec061de12017-02-14 13:27:23 -08001767 base_reg = TMP;
1768 }
1769 if (kPoisonHeapReferences && needs_write_barrier) {
1770 // Note that in the case where `value` is a null reference,
1771 // we do not enter this block, as a null reference does not
1772 // need poisoning.
1773 DCHECK_EQ(value_type, Primitive::kPrimNot);
1774 // Use Sw() instead of StoreToOffset() in order to be able to
1775 // hold the poisoned reference in AT and thus avoid allocating
1776 // yet another temporary register.
1777 if (index.IsConstant()) {
1778 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
1779 int16_t low16 = Low16Bits(data_offset);
1780 // For consistency with StoreToOffset() and such treat data_offset as int32_t.
1781 uint64_t high48 = static_cast<uint64_t>(static_cast<int32_t>(data_offset)) - low16;
1782 int16_t upper16 = High16Bits(high48);
1783 // Allow the full [-2GB,+2GB) range in case `low16` is negative and needs a
1784 // compensatory 64KB added, which may push `high48` above 2GB and require
1785 // the dahi instruction.
1786 int16_t higher16 = High32Bits(high48) + ((upper16 < 0) ? 1 : 0);
1787 __ Daui(TMP, obj, upper16);
1788 if (higher16 != 0) {
1789 __ Dahi(TMP, higher16);
1790 }
1791 base_reg = TMP;
1792 data_offset = low16;
1793 }
1794 } else {
1795 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
1796 }
1797 __ PoisonHeapReference(AT, value);
1798 __ Sw(AT, base_reg, data_offset);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001799 null_checker();
Alexey Frunzec061de12017-02-14 13:27:23 -08001800 } else {
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001801 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 if (needs_write_barrier) {
1804 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001805 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806 }
1807 } else {
1808 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08001809 // Note: if heap poisoning is enabled, pAputObject takes care
1810 // of poisoning the reference.
Serban Constantinescufc734082016-07-19 17:18:07 +01001811 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001812 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001813 }
1814 break;
1815 }
1816
1817 case Primitive::kPrimLong: {
1818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1819 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1820 if (index.IsConstant()) {
1821 size_t offset =
1822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001823 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001824 } else {
1825 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1826 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001827 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 }
1829 break;
1830 }
1831
1832 case Primitive::kPrimFloat: {
1833 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1834 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1835 DCHECK(locations->InAt(2).IsFpuRegister());
1836 if (index.IsConstant()) {
1837 size_t offset =
1838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001839 __ StoreFpuToOffset(kStoreWord, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001840 } else {
1841 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1842 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001843 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844 }
1845 break;
1846 }
1847
1848 case Primitive::kPrimDouble: {
1849 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1850 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1851 DCHECK(locations->InAt(2).IsFpuRegister());
1852 if (index.IsConstant()) {
1853 size_t offset =
1854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001855 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001856 } else {
1857 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1858 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001859 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001860 }
1861 break;
1862 }
1863
1864 case Primitive::kPrimVoid:
1865 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1866 UNREACHABLE();
1867 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001868}
1869
1870void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001871 RegisterSet caller_saves = RegisterSet::Empty();
1872 InvokeRuntimeCallingConvention calling_convention;
1873 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1874 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1875 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001876 locations->SetInAt(0, Location::RequiresRegister());
1877 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001878}
1879
1880void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1881 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001882 BoundsCheckSlowPathMIPS64* slow_path =
1883 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001884 codegen_->AddSlowPath(slow_path);
1885
1886 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1887 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1888
1889 // length is limited by the maximum positive signed 32-bit integer.
1890 // Unsigned comparison of length and index checks for index < 0
1891 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001892 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001893}
1894
1895void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1897 instruction,
1898 LocationSummary::kCallOnSlowPath);
1899 locations->SetInAt(0, Location::RequiresRegister());
1900 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001901 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902 locations->AddTemp(Location::RequiresRegister());
1903}
1904
1905void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1906 LocationSummary* locations = instruction->GetLocations();
1907 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1908 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1909 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1910
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001911 SlowPathCodeMIPS64* slow_path =
1912 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913 codegen_->AddSlowPath(slow_path);
1914
1915 // TODO: avoid this check if we know obj is not null.
1916 __ Beqzc(obj, slow_path->GetExitLabel());
1917 // Compare the class of `obj` with `cls`.
1918 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08001919 __ MaybeUnpoisonHeapReference(obj_cls);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001920 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1921 __ Bind(slow_path->GetExitLabel());
1922}
1923
1924void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1925 LocationSummary* locations =
1926 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1927 locations->SetInAt(0, Location::RequiresRegister());
1928 if (check->HasUses()) {
1929 locations->SetOut(Location::SameAsFirstInput());
1930 }
1931}
1932
1933void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1934 // We assume the class is not null.
1935 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1936 check->GetLoadClass(),
1937 check,
1938 check->GetDexPc(),
1939 true);
1940 codegen_->AddSlowPath(slow_path);
1941 GenerateClassInitializationCheck(slow_path,
1942 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1943}
1944
1945void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1946 Primitive::Type in_type = compare->InputAt(0)->GetType();
1947
Alexey Frunze299a9392015-12-08 16:08:02 -08001948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001949
1950 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001951 case Primitive::kPrimBoolean:
1952 case Primitive::kPrimByte:
1953 case Primitive::kPrimShort:
1954 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001955 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001956 case Primitive::kPrimLong:
1957 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001958 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1960 break;
1961
1962 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001963 case Primitive::kPrimDouble:
1964 locations->SetInAt(0, Location::RequiresFpuRegister());
1965 locations->SetInAt(1, Location::RequiresFpuRegister());
1966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001968
1969 default:
1970 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1971 }
1972}
1973
1974void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1975 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001976 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001977 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1978
1979 // 0 if: left == right
1980 // 1 if: left > right
1981 // -1 if: left < right
1982 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001983 case Primitive::kPrimBoolean:
1984 case Primitive::kPrimByte:
1985 case Primitive::kPrimShort:
1986 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001987 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001988 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001989 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001990 Location rhs_location = locations->InAt(1);
1991 bool use_imm = rhs_location.IsConstant();
1992 GpuRegister rhs = ZERO;
1993 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001994 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001995 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1996 if (value != 0) {
1997 rhs = AT;
1998 __ LoadConst64(rhs, value);
1999 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002000 } else {
2001 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2002 if (value != 0) {
2003 rhs = AT;
2004 __ LoadConst32(rhs, value);
2005 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002006 }
2007 } else {
2008 rhs = rhs_location.AsRegister<GpuRegister>();
2009 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002010 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002011 __ Slt(res, rhs, lhs);
2012 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002013 break;
2014 }
2015
Alexey Frunze299a9392015-12-08 16:08:02 -08002016 case Primitive::kPrimFloat: {
2017 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2018 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2019 Mips64Label done;
2020 __ CmpEqS(FTMP, lhs, rhs);
2021 __ LoadConst32(res, 0);
2022 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002023 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002024 __ CmpLtS(FTMP, lhs, rhs);
2025 __ LoadConst32(res, -1);
2026 __ Bc1nez(FTMP, &done);
2027 __ LoadConst32(res, 1);
2028 } else {
2029 __ CmpLtS(FTMP, rhs, lhs);
2030 __ LoadConst32(res, 1);
2031 __ Bc1nez(FTMP, &done);
2032 __ LoadConst32(res, -1);
2033 }
2034 __ Bind(&done);
2035 break;
2036 }
2037
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002039 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2040 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2041 Mips64Label done;
2042 __ CmpEqD(FTMP, lhs, rhs);
2043 __ LoadConst32(res, 0);
2044 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002045 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002046 __ CmpLtD(FTMP, lhs, rhs);
2047 __ LoadConst32(res, -1);
2048 __ Bc1nez(FTMP, &done);
2049 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002051 __ CmpLtD(FTMP, rhs, lhs);
2052 __ LoadConst32(res, 1);
2053 __ Bc1nez(FTMP, &done);
2054 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002055 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002056 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002057 break;
2058 }
2059
2060 default:
2061 LOG(FATAL) << "Unimplemented compare type " << in_type;
2062 }
2063}
2064
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002067 switch (instruction->InputAt(0)->GetType()) {
2068 default:
2069 case Primitive::kPrimLong:
2070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2072 break;
2073
2074 case Primitive::kPrimFloat:
2075 case Primitive::kPrimDouble:
2076 locations->SetInAt(0, Location::RequiresFpuRegister());
2077 locations->SetInAt(1, Location::RequiresFpuRegister());
2078 break;
2079 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002080 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2082 }
2083}
2084
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002086 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 return;
2088 }
2089
Alexey Frunze299a9392015-12-08 16:08:02 -08002090 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002091 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002092 switch (type) {
2093 default:
2094 // Integer case.
2095 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2096 return;
2097 case Primitive::kPrimLong:
2098 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2099 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002100 case Primitive::kPrimFloat:
2101 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002102 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2103 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002104 }
2105}
2106
Alexey Frunzec857c742015-09-23 15:12:39 -07002107void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2108 DCHECK(instruction->IsDiv() || instruction->IsRem());
2109 Primitive::Type type = instruction->GetResultType();
2110
2111 LocationSummary* locations = instruction->GetLocations();
2112 Location second = locations->InAt(1);
2113 DCHECK(second.IsConstant());
2114
2115 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2116 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2117 int64_t imm = Int64FromConstant(second.GetConstant());
2118 DCHECK(imm == 1 || imm == -1);
2119
2120 if (instruction->IsRem()) {
2121 __ Move(out, ZERO);
2122 } else {
2123 if (imm == -1) {
2124 if (type == Primitive::kPrimInt) {
2125 __ Subu(out, ZERO, dividend);
2126 } else {
2127 DCHECK_EQ(type, Primitive::kPrimLong);
2128 __ Dsubu(out, ZERO, dividend);
2129 }
2130 } else if (out != dividend) {
2131 __ Move(out, dividend);
2132 }
2133 }
2134}
2135
2136void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2137 DCHECK(instruction->IsDiv() || instruction->IsRem());
2138 Primitive::Type type = instruction->GetResultType();
2139
2140 LocationSummary* locations = instruction->GetLocations();
2141 Location second = locations->InAt(1);
2142 DCHECK(second.IsConstant());
2143
2144 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2145 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2146 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002147 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002148 int ctz_imm = CTZ(abs_imm);
2149
2150 if (instruction->IsDiv()) {
2151 if (type == Primitive::kPrimInt) {
2152 if (ctz_imm == 1) {
2153 // Fast path for division by +/-2, which is very common.
2154 __ Srl(TMP, dividend, 31);
2155 } else {
2156 __ Sra(TMP, dividend, 31);
2157 __ Srl(TMP, TMP, 32 - ctz_imm);
2158 }
2159 __ Addu(out, dividend, TMP);
2160 __ Sra(out, out, ctz_imm);
2161 if (imm < 0) {
2162 __ Subu(out, ZERO, out);
2163 }
2164 } else {
2165 DCHECK_EQ(type, Primitive::kPrimLong);
2166 if (ctz_imm == 1) {
2167 // Fast path for division by +/-2, which is very common.
2168 __ Dsrl32(TMP, dividend, 31);
2169 } else {
2170 __ Dsra32(TMP, dividend, 31);
2171 if (ctz_imm > 32) {
2172 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2173 } else {
2174 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2175 }
2176 }
2177 __ Daddu(out, dividend, TMP);
2178 if (ctz_imm < 32) {
2179 __ Dsra(out, out, ctz_imm);
2180 } else {
2181 __ Dsra32(out, out, ctz_imm - 32);
2182 }
2183 if (imm < 0) {
2184 __ Dsubu(out, ZERO, out);
2185 }
2186 }
2187 } else {
2188 if (type == Primitive::kPrimInt) {
2189 if (ctz_imm == 1) {
2190 // Fast path for modulo +/-2, which is very common.
2191 __ Sra(TMP, dividend, 31);
2192 __ Subu(out, dividend, TMP);
2193 __ Andi(out, out, 1);
2194 __ Addu(out, out, TMP);
2195 } else {
2196 __ Sra(TMP, dividend, 31);
2197 __ Srl(TMP, TMP, 32 - ctz_imm);
2198 __ Addu(out, dividend, TMP);
2199 if (IsUint<16>(abs_imm - 1)) {
2200 __ Andi(out, out, abs_imm - 1);
2201 } else {
2202 __ Sll(out, out, 32 - ctz_imm);
2203 __ Srl(out, out, 32 - ctz_imm);
2204 }
2205 __ Subu(out, out, TMP);
2206 }
2207 } else {
2208 DCHECK_EQ(type, Primitive::kPrimLong);
2209 if (ctz_imm == 1) {
2210 // Fast path for modulo +/-2, which is very common.
2211 __ Dsra32(TMP, dividend, 31);
2212 __ Dsubu(out, dividend, TMP);
2213 __ Andi(out, out, 1);
2214 __ Daddu(out, out, TMP);
2215 } else {
2216 __ Dsra32(TMP, dividend, 31);
2217 if (ctz_imm > 32) {
2218 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2219 } else {
2220 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2221 }
2222 __ Daddu(out, dividend, TMP);
2223 if (IsUint<16>(abs_imm - 1)) {
2224 __ Andi(out, out, abs_imm - 1);
2225 } else {
2226 if (ctz_imm > 32) {
2227 __ Dsll(out, out, 64 - ctz_imm);
2228 __ Dsrl(out, out, 64 - ctz_imm);
2229 } else {
2230 __ Dsll32(out, out, 32 - ctz_imm);
2231 __ Dsrl32(out, out, 32 - ctz_imm);
2232 }
2233 }
2234 __ Dsubu(out, out, TMP);
2235 }
2236 }
2237 }
2238}
2239
2240void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2241 DCHECK(instruction->IsDiv() || instruction->IsRem());
2242
2243 LocationSummary* locations = instruction->GetLocations();
2244 Location second = locations->InAt(1);
2245 DCHECK(second.IsConstant());
2246
2247 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2248 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2249 int64_t imm = Int64FromConstant(second.GetConstant());
2250
2251 Primitive::Type type = instruction->GetResultType();
2252 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2253
2254 int64_t magic;
2255 int shift;
2256 CalculateMagicAndShiftForDivRem(imm,
2257 (type == Primitive::kPrimLong),
2258 &magic,
2259 &shift);
2260
2261 if (type == Primitive::kPrimInt) {
2262 __ LoadConst32(TMP, magic);
2263 __ MuhR6(TMP, dividend, TMP);
2264
2265 if (imm > 0 && magic < 0) {
2266 __ Addu(TMP, TMP, dividend);
2267 } else if (imm < 0 && magic > 0) {
2268 __ Subu(TMP, TMP, dividend);
2269 }
2270
2271 if (shift != 0) {
2272 __ Sra(TMP, TMP, shift);
2273 }
2274
2275 if (instruction->IsDiv()) {
2276 __ Sra(out, TMP, 31);
2277 __ Subu(out, TMP, out);
2278 } else {
2279 __ Sra(AT, TMP, 31);
2280 __ Subu(AT, TMP, AT);
2281 __ LoadConst32(TMP, imm);
2282 __ MulR6(TMP, AT, TMP);
2283 __ Subu(out, dividend, TMP);
2284 }
2285 } else {
2286 __ LoadConst64(TMP, magic);
2287 __ Dmuh(TMP, dividend, TMP);
2288
2289 if (imm > 0 && magic < 0) {
2290 __ Daddu(TMP, TMP, dividend);
2291 } else if (imm < 0 && magic > 0) {
2292 __ Dsubu(TMP, TMP, dividend);
2293 }
2294
2295 if (shift >= 32) {
2296 __ Dsra32(TMP, TMP, shift - 32);
2297 } else if (shift > 0) {
2298 __ Dsra(TMP, TMP, shift);
2299 }
2300
2301 if (instruction->IsDiv()) {
2302 __ Dsra32(out, TMP, 31);
2303 __ Dsubu(out, TMP, out);
2304 } else {
2305 __ Dsra32(AT, TMP, 31);
2306 __ Dsubu(AT, TMP, AT);
2307 __ LoadConst64(TMP, imm);
2308 __ Dmul(TMP, AT, TMP);
2309 __ Dsubu(out, dividend, TMP);
2310 }
2311 }
2312}
2313
2314void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2315 DCHECK(instruction->IsDiv() || instruction->IsRem());
2316 Primitive::Type type = instruction->GetResultType();
2317 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2318
2319 LocationSummary* locations = instruction->GetLocations();
2320 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2321 Location second = locations->InAt(1);
2322
2323 if (second.IsConstant()) {
2324 int64_t imm = Int64FromConstant(second.GetConstant());
2325 if (imm == 0) {
2326 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2327 } else if (imm == 1 || imm == -1) {
2328 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002329 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002330 DivRemByPowerOfTwo(instruction);
2331 } else {
2332 DCHECK(imm <= -2 || imm >= 2);
2333 GenerateDivRemWithAnyConstant(instruction);
2334 }
2335 } else {
2336 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2337 GpuRegister divisor = second.AsRegister<GpuRegister>();
2338 if (instruction->IsDiv()) {
2339 if (type == Primitive::kPrimInt)
2340 __ DivR6(out, dividend, divisor);
2341 else
2342 __ Ddiv(out, dividend, divisor);
2343 } else {
2344 if (type == Primitive::kPrimInt)
2345 __ ModR6(out, dividend, divisor);
2346 else
2347 __ Dmod(out, dividend, divisor);
2348 }
2349 }
2350}
2351
Alexey Frunze4dda3372015-06-01 18:31:49 -07002352void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2355 switch (div->GetResultType()) {
2356 case Primitive::kPrimInt:
2357 case Primitive::kPrimLong:
2358 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002359 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2361 break;
2362
2363 case Primitive::kPrimFloat:
2364 case Primitive::kPrimDouble:
2365 locations->SetInAt(0, Location::RequiresFpuRegister());
2366 locations->SetInAt(1, Location::RequiresFpuRegister());
2367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2372 }
2373}
2374
2375void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2376 Primitive::Type type = instruction->GetType();
2377 LocationSummary* locations = instruction->GetLocations();
2378
2379 switch (type) {
2380 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002381 case Primitive::kPrimLong:
2382 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002383 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002384 case Primitive::kPrimFloat:
2385 case Primitive::kPrimDouble: {
2386 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2387 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2388 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2389 if (type == Primitive::kPrimFloat)
2390 __ DivS(dst, lhs, rhs);
2391 else
2392 __ DivD(dst, lhs, rhs);
2393 break;
2394 }
2395 default:
2396 LOG(FATAL) << "Unexpected div type " << type;
2397 }
2398}
2399
2400void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002401 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002402 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002403}
2404
2405void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2406 SlowPathCodeMIPS64* slow_path =
2407 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2408 codegen_->AddSlowPath(slow_path);
2409 Location value = instruction->GetLocations()->InAt(0);
2410
2411 Primitive::Type type = instruction->GetType();
2412
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002413 if (!Primitive::IsIntegralType(type)) {
2414 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002415 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002416 }
2417
2418 if (value.IsConstant()) {
2419 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2420 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002421 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002422 } else {
2423 // A division by a non-null constant is valid. We don't need to perform
2424 // any check, so simply fall through.
2425 }
2426 } else {
2427 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2428 }
2429}
2430
2431void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2432 LocationSummary* locations =
2433 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2434 locations->SetOut(Location::ConstantLocation(constant));
2435}
2436
2437void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2438 // Will be generated at use site.
2439}
2440
2441void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2442 exit->SetLocations(nullptr);
2443}
2444
2445void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2446}
2447
2448void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2449 LocationSummary* locations =
2450 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2451 locations->SetOut(Location::ConstantLocation(constant));
2452}
2453
2454void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2455 // Will be generated at use site.
2456}
2457
David Brazdilfc6a86a2015-06-26 10:33:45 +00002458void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002459 DCHECK(!successor->IsExitBlock());
2460 HBasicBlock* block = got->GetBlock();
2461 HInstruction* previous = got->GetPrevious();
2462 HLoopInformation* info = block->GetLoopInformation();
2463
2464 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2465 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2466 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2467 return;
2468 }
2469 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2470 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2471 }
2472 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002473 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002474 }
2475}
2476
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2478 got->SetLocations(nullptr);
2479}
2480
2481void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2482 HandleGoto(got, got->GetSuccessor());
2483}
2484
2485void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2486 try_boundary->SetLocations(nullptr);
2487}
2488
2489void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2490 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2491 if (!successor->IsExitBlock()) {
2492 HandleGoto(try_boundary, successor);
2493 }
2494}
2495
Alexey Frunze299a9392015-12-08 16:08:02 -08002496void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2497 bool is64bit,
2498 LocationSummary* locations) {
2499 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2500 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2501 Location rhs_location = locations->InAt(1);
2502 GpuRegister rhs_reg = ZERO;
2503 int64_t rhs_imm = 0;
2504 bool use_imm = rhs_location.IsConstant();
2505 if (use_imm) {
2506 if (is64bit) {
2507 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2508 } else {
2509 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2510 }
2511 } else {
2512 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2513 }
2514 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2515
2516 switch (cond) {
2517 case kCondEQ:
2518 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002519 if (use_imm && IsInt<16>(-rhs_imm)) {
2520 if (rhs_imm == 0) {
2521 if (cond == kCondEQ) {
2522 __ Sltiu(dst, lhs, 1);
2523 } else {
2524 __ Sltu(dst, ZERO, lhs);
2525 }
2526 } else {
2527 if (is64bit) {
2528 __ Daddiu(dst, lhs, -rhs_imm);
2529 } else {
2530 __ Addiu(dst, lhs, -rhs_imm);
2531 }
2532 if (cond == kCondEQ) {
2533 __ Sltiu(dst, dst, 1);
2534 } else {
2535 __ Sltu(dst, ZERO, dst);
2536 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002537 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002538 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002539 if (use_imm && IsUint<16>(rhs_imm)) {
2540 __ Xori(dst, lhs, rhs_imm);
2541 } else {
2542 if (use_imm) {
2543 rhs_reg = TMP;
2544 __ LoadConst64(rhs_reg, rhs_imm);
2545 }
2546 __ Xor(dst, lhs, rhs_reg);
2547 }
2548 if (cond == kCondEQ) {
2549 __ Sltiu(dst, dst, 1);
2550 } else {
2551 __ Sltu(dst, ZERO, dst);
2552 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002553 }
2554 break;
2555
2556 case kCondLT:
2557 case kCondGE:
2558 if (use_imm && IsInt<16>(rhs_imm)) {
2559 __ Slti(dst, lhs, rhs_imm);
2560 } else {
2561 if (use_imm) {
2562 rhs_reg = TMP;
2563 __ LoadConst64(rhs_reg, rhs_imm);
2564 }
2565 __ Slt(dst, lhs, rhs_reg);
2566 }
2567 if (cond == kCondGE) {
2568 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2569 // only the slt instruction but no sge.
2570 __ Xori(dst, dst, 1);
2571 }
2572 break;
2573
2574 case kCondLE:
2575 case kCondGT:
2576 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2577 // Simulate lhs <= rhs via lhs < rhs + 1.
2578 __ Slti(dst, lhs, rhs_imm_plus_one);
2579 if (cond == kCondGT) {
2580 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2581 // only the slti instruction but no sgti.
2582 __ Xori(dst, dst, 1);
2583 }
2584 } else {
2585 if (use_imm) {
2586 rhs_reg = TMP;
2587 __ LoadConst64(rhs_reg, rhs_imm);
2588 }
2589 __ Slt(dst, rhs_reg, lhs);
2590 if (cond == kCondLE) {
2591 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2592 // only the slt instruction but no sle.
2593 __ Xori(dst, dst, 1);
2594 }
2595 }
2596 break;
2597
2598 case kCondB:
2599 case kCondAE:
2600 if (use_imm && IsInt<16>(rhs_imm)) {
2601 // Sltiu sign-extends its 16-bit immediate operand before
2602 // the comparison and thus lets us compare directly with
2603 // unsigned values in the ranges [0, 0x7fff] and
2604 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2605 __ Sltiu(dst, lhs, rhs_imm);
2606 } else {
2607 if (use_imm) {
2608 rhs_reg = TMP;
2609 __ LoadConst64(rhs_reg, rhs_imm);
2610 }
2611 __ Sltu(dst, lhs, rhs_reg);
2612 }
2613 if (cond == kCondAE) {
2614 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2615 // only the sltu instruction but no sgeu.
2616 __ Xori(dst, dst, 1);
2617 }
2618 break;
2619
2620 case kCondBE:
2621 case kCondA:
2622 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2623 // Simulate lhs <= rhs via lhs < rhs + 1.
2624 // Note that this only works if rhs + 1 does not overflow
2625 // to 0, hence the check above.
2626 // Sltiu sign-extends its 16-bit immediate operand before
2627 // the comparison and thus lets us compare directly with
2628 // unsigned values in the ranges [0, 0x7fff] and
2629 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2630 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2631 if (cond == kCondA) {
2632 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2633 // only the sltiu instruction but no sgtiu.
2634 __ Xori(dst, dst, 1);
2635 }
2636 } else {
2637 if (use_imm) {
2638 rhs_reg = TMP;
2639 __ LoadConst64(rhs_reg, rhs_imm);
2640 }
2641 __ Sltu(dst, rhs_reg, lhs);
2642 if (cond == kCondBE) {
2643 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2644 // only the sltu instruction but no sleu.
2645 __ Xori(dst, dst, 1);
2646 }
2647 }
2648 break;
2649 }
2650}
2651
2652void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2653 bool is64bit,
2654 LocationSummary* locations,
2655 Mips64Label* label) {
2656 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2657 Location rhs_location = locations->InAt(1);
2658 GpuRegister rhs_reg = ZERO;
2659 int64_t rhs_imm = 0;
2660 bool use_imm = rhs_location.IsConstant();
2661 if (use_imm) {
2662 if (is64bit) {
2663 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2664 } else {
2665 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2666 }
2667 } else {
2668 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2669 }
2670
2671 if (use_imm && rhs_imm == 0) {
2672 switch (cond) {
2673 case kCondEQ:
2674 case kCondBE: // <= 0 if zero
2675 __ Beqzc(lhs, label);
2676 break;
2677 case kCondNE:
2678 case kCondA: // > 0 if non-zero
2679 __ Bnezc(lhs, label);
2680 break;
2681 case kCondLT:
2682 __ Bltzc(lhs, label);
2683 break;
2684 case kCondGE:
2685 __ Bgezc(lhs, label);
2686 break;
2687 case kCondLE:
2688 __ Blezc(lhs, label);
2689 break;
2690 case kCondGT:
2691 __ Bgtzc(lhs, label);
2692 break;
2693 case kCondB: // always false
2694 break;
2695 case kCondAE: // always true
2696 __ Bc(label);
2697 break;
2698 }
2699 } else {
2700 if (use_imm) {
2701 rhs_reg = TMP;
2702 __ LoadConst64(rhs_reg, rhs_imm);
2703 }
2704 switch (cond) {
2705 case kCondEQ:
2706 __ Beqc(lhs, rhs_reg, label);
2707 break;
2708 case kCondNE:
2709 __ Bnec(lhs, rhs_reg, label);
2710 break;
2711 case kCondLT:
2712 __ Bltc(lhs, rhs_reg, label);
2713 break;
2714 case kCondGE:
2715 __ Bgec(lhs, rhs_reg, label);
2716 break;
2717 case kCondLE:
2718 __ Bgec(rhs_reg, lhs, label);
2719 break;
2720 case kCondGT:
2721 __ Bltc(rhs_reg, lhs, label);
2722 break;
2723 case kCondB:
2724 __ Bltuc(lhs, rhs_reg, label);
2725 break;
2726 case kCondAE:
2727 __ Bgeuc(lhs, rhs_reg, label);
2728 break;
2729 case kCondBE:
2730 __ Bgeuc(rhs_reg, lhs, label);
2731 break;
2732 case kCondA:
2733 __ Bltuc(rhs_reg, lhs, label);
2734 break;
2735 }
2736 }
2737}
2738
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002739void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2740 bool gt_bias,
2741 Primitive::Type type,
2742 LocationSummary* locations) {
2743 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2744 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2745 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2746 if (type == Primitive::kPrimFloat) {
2747 switch (cond) {
2748 case kCondEQ:
2749 __ CmpEqS(FTMP, lhs, rhs);
2750 __ Mfc1(dst, FTMP);
2751 __ Andi(dst, dst, 1);
2752 break;
2753 case kCondNE:
2754 __ CmpEqS(FTMP, lhs, rhs);
2755 __ Mfc1(dst, FTMP);
2756 __ Addiu(dst, dst, 1);
2757 break;
2758 case kCondLT:
2759 if (gt_bias) {
2760 __ CmpLtS(FTMP, lhs, rhs);
2761 } else {
2762 __ CmpUltS(FTMP, lhs, rhs);
2763 }
2764 __ Mfc1(dst, FTMP);
2765 __ Andi(dst, dst, 1);
2766 break;
2767 case kCondLE:
2768 if (gt_bias) {
2769 __ CmpLeS(FTMP, lhs, rhs);
2770 } else {
2771 __ CmpUleS(FTMP, lhs, rhs);
2772 }
2773 __ Mfc1(dst, FTMP);
2774 __ Andi(dst, dst, 1);
2775 break;
2776 case kCondGT:
2777 if (gt_bias) {
2778 __ CmpUltS(FTMP, rhs, lhs);
2779 } else {
2780 __ CmpLtS(FTMP, rhs, lhs);
2781 }
2782 __ Mfc1(dst, FTMP);
2783 __ Andi(dst, dst, 1);
2784 break;
2785 case kCondGE:
2786 if (gt_bias) {
2787 __ CmpUleS(FTMP, rhs, lhs);
2788 } else {
2789 __ CmpLeS(FTMP, rhs, lhs);
2790 }
2791 __ Mfc1(dst, FTMP);
2792 __ Andi(dst, dst, 1);
2793 break;
2794 default:
2795 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2796 UNREACHABLE();
2797 }
2798 } else {
2799 DCHECK_EQ(type, Primitive::kPrimDouble);
2800 switch (cond) {
2801 case kCondEQ:
2802 __ CmpEqD(FTMP, lhs, rhs);
2803 __ Mfc1(dst, FTMP);
2804 __ Andi(dst, dst, 1);
2805 break;
2806 case kCondNE:
2807 __ CmpEqD(FTMP, lhs, rhs);
2808 __ Mfc1(dst, FTMP);
2809 __ Addiu(dst, dst, 1);
2810 break;
2811 case kCondLT:
2812 if (gt_bias) {
2813 __ CmpLtD(FTMP, lhs, rhs);
2814 } else {
2815 __ CmpUltD(FTMP, lhs, rhs);
2816 }
2817 __ Mfc1(dst, FTMP);
2818 __ Andi(dst, dst, 1);
2819 break;
2820 case kCondLE:
2821 if (gt_bias) {
2822 __ CmpLeD(FTMP, lhs, rhs);
2823 } else {
2824 __ CmpUleD(FTMP, lhs, rhs);
2825 }
2826 __ Mfc1(dst, FTMP);
2827 __ Andi(dst, dst, 1);
2828 break;
2829 case kCondGT:
2830 if (gt_bias) {
2831 __ CmpUltD(FTMP, rhs, lhs);
2832 } else {
2833 __ CmpLtD(FTMP, rhs, lhs);
2834 }
2835 __ Mfc1(dst, FTMP);
2836 __ Andi(dst, dst, 1);
2837 break;
2838 case kCondGE:
2839 if (gt_bias) {
2840 __ CmpUleD(FTMP, rhs, lhs);
2841 } else {
2842 __ CmpLeD(FTMP, rhs, lhs);
2843 }
2844 __ Mfc1(dst, FTMP);
2845 __ Andi(dst, dst, 1);
2846 break;
2847 default:
2848 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2849 UNREACHABLE();
2850 }
2851 }
2852}
2853
Alexey Frunze299a9392015-12-08 16:08:02 -08002854void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2855 bool gt_bias,
2856 Primitive::Type type,
2857 LocationSummary* locations,
2858 Mips64Label* label) {
2859 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2860 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2861 if (type == Primitive::kPrimFloat) {
2862 switch (cond) {
2863 case kCondEQ:
2864 __ CmpEqS(FTMP, lhs, rhs);
2865 __ Bc1nez(FTMP, label);
2866 break;
2867 case kCondNE:
2868 __ CmpEqS(FTMP, lhs, rhs);
2869 __ Bc1eqz(FTMP, label);
2870 break;
2871 case kCondLT:
2872 if (gt_bias) {
2873 __ CmpLtS(FTMP, lhs, rhs);
2874 } else {
2875 __ CmpUltS(FTMP, lhs, rhs);
2876 }
2877 __ Bc1nez(FTMP, label);
2878 break;
2879 case kCondLE:
2880 if (gt_bias) {
2881 __ CmpLeS(FTMP, lhs, rhs);
2882 } else {
2883 __ CmpUleS(FTMP, lhs, rhs);
2884 }
2885 __ Bc1nez(FTMP, label);
2886 break;
2887 case kCondGT:
2888 if (gt_bias) {
2889 __ CmpUltS(FTMP, rhs, lhs);
2890 } else {
2891 __ CmpLtS(FTMP, rhs, lhs);
2892 }
2893 __ Bc1nez(FTMP, label);
2894 break;
2895 case kCondGE:
2896 if (gt_bias) {
2897 __ CmpUleS(FTMP, rhs, lhs);
2898 } else {
2899 __ CmpLeS(FTMP, rhs, lhs);
2900 }
2901 __ Bc1nez(FTMP, label);
2902 break;
2903 default:
2904 LOG(FATAL) << "Unexpected non-floating-point condition";
2905 }
2906 } else {
2907 DCHECK_EQ(type, Primitive::kPrimDouble);
2908 switch (cond) {
2909 case kCondEQ:
2910 __ CmpEqD(FTMP, lhs, rhs);
2911 __ Bc1nez(FTMP, label);
2912 break;
2913 case kCondNE:
2914 __ CmpEqD(FTMP, lhs, rhs);
2915 __ Bc1eqz(FTMP, label);
2916 break;
2917 case kCondLT:
2918 if (gt_bias) {
2919 __ CmpLtD(FTMP, lhs, rhs);
2920 } else {
2921 __ CmpUltD(FTMP, lhs, rhs);
2922 }
2923 __ Bc1nez(FTMP, label);
2924 break;
2925 case kCondLE:
2926 if (gt_bias) {
2927 __ CmpLeD(FTMP, lhs, rhs);
2928 } else {
2929 __ CmpUleD(FTMP, lhs, rhs);
2930 }
2931 __ Bc1nez(FTMP, label);
2932 break;
2933 case kCondGT:
2934 if (gt_bias) {
2935 __ CmpUltD(FTMP, rhs, lhs);
2936 } else {
2937 __ CmpLtD(FTMP, rhs, lhs);
2938 }
2939 __ Bc1nez(FTMP, label);
2940 break;
2941 case kCondGE:
2942 if (gt_bias) {
2943 __ CmpUleD(FTMP, rhs, lhs);
2944 } else {
2945 __ CmpLeD(FTMP, rhs, lhs);
2946 }
2947 __ Bc1nez(FTMP, label);
2948 break;
2949 default:
2950 LOG(FATAL) << "Unexpected non-floating-point condition";
2951 }
2952 }
2953}
2954
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002956 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002957 Mips64Label* true_target,
2958 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002959 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960
David Brazdil0debae72015-11-12 18:37:00 +00002961 if (true_target == nullptr && false_target == nullptr) {
2962 // Nothing to do. The code always falls through.
2963 return;
2964 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002965 // Constant condition, statically compared against "true" (integer value 1).
2966 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002967 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002968 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002969 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002971 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002972 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002973 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002974 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002975 }
David Brazdil0debae72015-11-12 18:37:00 +00002976 return;
2977 }
2978
2979 // The following code generates these patterns:
2980 // (1) true_target == nullptr && false_target != nullptr
2981 // - opposite condition true => branch to false_target
2982 // (2) true_target != nullptr && false_target == nullptr
2983 // - condition true => branch to true_target
2984 // (3) true_target != nullptr && false_target != nullptr
2985 // - condition true => branch to true_target
2986 // - branch to false_target
2987 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002989 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002990 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002991 if (true_target == nullptr) {
2992 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2993 } else {
2994 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2995 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002996 } else {
2997 // The condition instruction has not been materialized, use its inputs as
2998 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002999 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003000 Primitive::Type type = condition->InputAt(0)->GetType();
3001 LocationSummary* locations = cond->GetLocations();
3002 IfCondition if_cond = condition->GetCondition();
3003 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003004
David Brazdil0debae72015-11-12 18:37:00 +00003005 if (true_target == nullptr) {
3006 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003007 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003008 }
3009
Alexey Frunze299a9392015-12-08 16:08:02 -08003010 switch (type) {
3011 default:
3012 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3013 break;
3014 case Primitive::kPrimLong:
3015 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3016 break;
3017 case Primitive::kPrimFloat:
3018 case Primitive::kPrimDouble:
3019 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3020 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021 }
3022 }
David Brazdil0debae72015-11-12 18:37:00 +00003023
3024 // If neither branch falls through (case 3), the conditional branch to `true_target`
3025 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3026 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003027 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003028 }
3029}
3030
3031void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003033 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003034 locations->SetInAt(0, Location::RequiresRegister());
3035 }
3036}
3037
3038void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003039 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3040 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003041 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003042 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003043 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003044 nullptr : codegen_->GetLabelOf(false_successor);
3045 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003046}
3047
3048void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3049 LocationSummary* locations = new (GetGraph()->GetArena())
3050 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003051 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003052 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003053 locations->SetInAt(0, Location::RequiresRegister());
3054 }
3055}
3056
3057void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003058 SlowPathCodeMIPS64* slow_path =
3059 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003060 GenerateTestAndBranch(deoptimize,
3061 /* condition_input_index */ 0,
3062 slow_path->GetEntryLabel(),
3063 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003064}
3065
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003066void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3067 LocationSummary* locations = new (GetGraph()->GetArena())
3068 LocationSummary(flag, LocationSummary::kNoCall);
3069 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003070}
3071
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003072void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3073 __ LoadFromOffset(kLoadWord,
3074 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3075 SP,
3076 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003077}
3078
David Brazdil74eb1b22015-12-14 11:44:01 +00003079void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3081 if (Primitive::IsFloatingPointType(select->GetType())) {
3082 locations->SetInAt(0, Location::RequiresFpuRegister());
3083 locations->SetInAt(1, Location::RequiresFpuRegister());
3084 } else {
3085 locations->SetInAt(0, Location::RequiresRegister());
3086 locations->SetInAt(1, Location::RequiresRegister());
3087 }
3088 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3089 locations->SetInAt(2, Location::RequiresRegister());
3090 }
3091 locations->SetOut(Location::SameAsFirstInput());
3092}
3093
3094void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3095 LocationSummary* locations = select->GetLocations();
3096 Mips64Label false_target;
3097 GenerateTestAndBranch(select,
3098 /* condition_input_index */ 2,
3099 /* true_target */ nullptr,
3100 &false_target);
3101 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3102 __ Bind(&false_target);
3103}
3104
David Srbecky0cf44932015-12-09 14:09:59 +00003105void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3106 new (GetGraph()->GetArena()) LocationSummary(info);
3107}
3108
David Srbeckyd28f4a02016-03-14 17:14:24 +00003109void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3110 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003111}
3112
3113void CodeGeneratorMIPS64::GenerateNop() {
3114 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003115}
3116
Alexey Frunze4dda3372015-06-01 18:31:49 -07003117void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3118 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3119 LocationSummary* locations =
3120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3121 locations->SetInAt(0, Location::RequiresRegister());
3122 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3123 locations->SetOut(Location::RequiresFpuRegister());
3124 } else {
3125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3126 }
3127}
3128
3129void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3130 const FieldInfo& field_info) {
3131 Primitive::Type type = field_info.GetFieldType();
3132 LocationSummary* locations = instruction->GetLocations();
3133 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3134 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003135 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003136 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3137
Alexey Frunze4dda3372015-06-01 18:31:49 -07003138 switch (type) {
3139 case Primitive::kPrimBoolean:
3140 load_type = kLoadUnsignedByte;
3141 break;
3142 case Primitive::kPrimByte:
3143 load_type = kLoadSignedByte;
3144 break;
3145 case Primitive::kPrimShort:
3146 load_type = kLoadSignedHalfword;
3147 break;
3148 case Primitive::kPrimChar:
3149 load_type = kLoadUnsignedHalfword;
3150 break;
3151 case Primitive::kPrimInt:
3152 case Primitive::kPrimFloat:
3153 load_type = kLoadWord;
3154 break;
3155 case Primitive::kPrimLong:
3156 case Primitive::kPrimDouble:
3157 load_type = kLoadDoubleword;
3158 break;
3159 case Primitive::kPrimNot:
3160 load_type = kLoadUnsignedWord;
3161 break;
3162 case Primitive::kPrimVoid:
3163 LOG(FATAL) << "Unreachable type " << type;
3164 UNREACHABLE();
3165 }
3166 if (!Primitive::IsFloatingPointType(type)) {
3167 DCHECK(locations->Out().IsRegister());
3168 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003169 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003170 } else {
3171 DCHECK(locations->Out().IsFpuRegister());
3172 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003173 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003174 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175 // TODO: memory barrier?
Alexey Frunzec061de12017-02-14 13:27:23 -08003176
3177 if (type == Primitive::kPrimNot) {
3178 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3179 __ MaybeUnpoisonHeapReference(dst);
3180 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181}
3182
3183void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3184 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3185 LocationSummary* locations =
3186 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3187 locations->SetInAt(0, Location::RequiresRegister());
3188 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3189 locations->SetInAt(1, Location::RequiresFpuRegister());
3190 } else {
3191 locations->SetInAt(1, Location::RequiresRegister());
3192 }
3193}
3194
3195void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003196 const FieldInfo& field_info,
3197 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003198 Primitive::Type type = field_info.GetFieldType();
3199 LocationSummary* locations = instruction->GetLocations();
3200 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3201 StoreOperandType store_type = kStoreByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003202 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3203 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003204 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3205
Alexey Frunze4dda3372015-06-01 18:31:49 -07003206 switch (type) {
3207 case Primitive::kPrimBoolean:
3208 case Primitive::kPrimByte:
3209 store_type = kStoreByte;
3210 break;
3211 case Primitive::kPrimShort:
3212 case Primitive::kPrimChar:
3213 store_type = kStoreHalfword;
3214 break;
3215 case Primitive::kPrimInt:
3216 case Primitive::kPrimFloat:
3217 case Primitive::kPrimNot:
3218 store_type = kStoreWord;
3219 break;
3220 case Primitive::kPrimLong:
3221 case Primitive::kPrimDouble:
3222 store_type = kStoreDoubleword;
3223 break;
3224 case Primitive::kPrimVoid:
3225 LOG(FATAL) << "Unreachable type " << type;
3226 UNREACHABLE();
3227 }
3228 if (!Primitive::IsFloatingPointType(type)) {
3229 DCHECK(locations->InAt(1).IsRegister());
3230 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003231 if (kPoisonHeapReferences && needs_write_barrier) {
3232 // Note that in the case where `value` is a null reference,
3233 // we do not enter this block, as a null reference does not
3234 // need poisoning.
3235 DCHECK_EQ(type, Primitive::kPrimNot);
3236 __ PoisonHeapReference(TMP, src);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003237 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08003238 } else {
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003239 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08003240 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241 } else {
3242 DCHECK(locations->InAt(1).IsFpuRegister());
3243 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003244 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003245 }
3246
Alexey Frunze4dda3372015-06-01 18:31:49 -07003247 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08003248 if (needs_write_barrier) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003249 DCHECK(locations->InAt(1).IsRegister());
3250 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003251 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252 }
3253}
3254
3255void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3256 HandleFieldGet(instruction, instruction->GetFieldInfo());
3257}
3258
3259void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3260 HandleFieldGet(instruction, instruction->GetFieldInfo());
3261}
3262
3263void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3264 HandleFieldSet(instruction, instruction->GetFieldInfo());
3265}
3266
3267void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003268 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003269}
3270
Alexey Frunzef63f5692016-12-13 17:43:11 -08003271void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3272 HInstruction* instruction ATTRIBUTE_UNUSED,
3273 Location root,
3274 GpuRegister obj,
3275 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003276 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3277 if (kEmitCompilerReadBarrier) {
3278 UNIMPLEMENTED(FATAL) << "for read barrier";
3279 } else {
3280 // Plain GC root load with no read barrier.
3281 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3282 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3283 // Note that GC roots are not affected by heap poisoning, thus we
3284 // do not have to unpoison `root_reg` here.
3285 }
3286}
3287
Alexey Frunze4dda3372015-06-01 18:31:49 -07003288void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3289 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003290 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003291 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3292 locations->SetInAt(0, Location::RequiresRegister());
3293 locations->SetInAt(1, Location::RequiresRegister());
3294 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003295 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003296 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3297}
3298
3299void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3300 LocationSummary* locations = instruction->GetLocations();
3301 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3302 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3303 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3304
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003305 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003306
3307 // Return 0 if `obj` is null.
3308 // TODO: Avoid this check if we know `obj` is not null.
3309 __ Move(out, ZERO);
3310 __ Beqzc(obj, &done);
3311
3312 // Compare the class of `obj` with `cls`.
3313 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08003314 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003315 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003316 // Classes must be equal for the instanceof to succeed.
3317 __ Xor(out, out, cls);
3318 __ Sltiu(out, out, 1);
3319 } else {
3320 // If the classes are not equal, we go into a slow path.
3321 DCHECK(locations->OnlyCallsOnSlowPath());
3322 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003323 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003324 codegen_->AddSlowPath(slow_path);
3325 __ Bnec(out, cls, slow_path->GetEntryLabel());
3326 __ LoadConst32(out, 1);
3327 __ Bind(slow_path->GetExitLabel());
3328 }
3329
3330 __ Bind(&done);
3331}
3332
3333void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3335 locations->SetOut(Location::ConstantLocation(constant));
3336}
3337
3338void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3339 // Will be generated at use site.
3340}
3341
3342void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3344 locations->SetOut(Location::ConstantLocation(constant));
3345}
3346
3347void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3348 // Will be generated at use site.
3349}
3350
Calin Juravle175dc732015-08-25 15:42:32 +01003351void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3352 // The trampoline uses the same calling convention as dex calling conventions,
3353 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3354 // the method_idx.
3355 HandleInvoke(invoke);
3356}
3357
3358void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3359 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3360}
3361
Alexey Frunze4dda3372015-06-01 18:31:49 -07003362void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3363 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3364 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3365}
3366
3367void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3368 HandleInvoke(invoke);
3369 // The register T0 is required to be used for the hidden argument in
3370 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3371 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3372}
3373
3374void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3375 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3376 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377 Location receiver = invoke->GetLocations()->InAt(0);
3378 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003379 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003380
3381 // Set the hidden argument.
3382 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3383 invoke->GetDexMethodIndex());
3384
3385 // temp = object->GetClass();
3386 if (receiver.IsStackSlot()) {
3387 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3388 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3389 } else {
3390 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3391 }
3392 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003393 // Instead of simply (possibly) unpoisoning `temp` here, we should
3394 // emit a read barrier for the previous class reference load.
3395 // However this is not required in practice, as this is an
3396 // intermediate/temporary reference and because the current
3397 // concurrent copying collector keeps the from-space memory
3398 // intact/accessible until the end of the marking phase (the
3399 // concurrent copying collector may not in the future).
3400 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003401 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3402 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3403 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003404 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003405 // temp = temp->GetImtEntryAt(method_offset);
3406 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3407 // T9 = temp->GetEntryPoint();
3408 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3409 // T9();
3410 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003411 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003412 DCHECK(!codegen_->IsLeafMethod());
3413 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3414}
3415
3416void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003417 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3418 if (intrinsic.TryDispatch(invoke)) {
3419 return;
3420 }
3421
Alexey Frunze4dda3372015-06-01 18:31:49 -07003422 HandleInvoke(invoke);
3423}
3424
3425void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003426 // Explicit clinit checks triggered by static invokes must have been pruned by
3427 // art::PrepareForRegisterAllocation.
3428 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429
Chris Larsen3039e382015-08-26 07:54:08 -07003430 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3431 if (intrinsic.TryDispatch(invoke)) {
3432 return;
3433 }
3434
Alexey Frunze4dda3372015-06-01 18:31:49 -07003435 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003436}
3437
Orion Hodsonac141392017-01-13 11:53:47 +00003438void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3439 HandleInvoke(invoke);
3440}
3441
3442void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3443 codegen_->GenerateInvokePolymorphicCall(invoke);
3444}
3445
Chris Larsen3039e382015-08-26 07:54:08 -07003446static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003447 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003448 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3449 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003450 return true;
3451 }
3452 return false;
3453}
3454
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003455HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003456 HLoadString::LoadKind desired_string_load_kind) {
3457 if (kEmitCompilerReadBarrier) {
3458 UNIMPLEMENTED(FATAL) << "for read barrier";
3459 }
3460 bool fallback_load = false;
3461 switch (desired_string_load_kind) {
3462 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3463 DCHECK(!GetCompilerOptions().GetCompilePic());
3464 break;
3465 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3466 DCHECK(GetCompilerOptions().GetCompilePic());
3467 break;
3468 case HLoadString::LoadKind::kBootImageAddress:
3469 break;
3470 case HLoadString::LoadKind::kBssEntry:
3471 DCHECK(!Runtime::Current()->UseJitCompilation());
3472 break;
3473 case HLoadString::LoadKind::kDexCacheViaMethod:
3474 break;
3475 case HLoadString::LoadKind::kJitTableAddress:
3476 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003477 break;
3478 }
3479 if (fallback_load) {
3480 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3481 }
3482 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003483}
3484
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003485HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3486 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003487 if (kEmitCompilerReadBarrier) {
3488 UNIMPLEMENTED(FATAL) << "for read barrier";
3489 }
3490 bool fallback_load = false;
3491 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003492 case HLoadClass::LoadKind::kInvalid:
3493 LOG(FATAL) << "UNREACHABLE";
3494 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003495 case HLoadClass::LoadKind::kReferrersClass:
3496 break;
3497 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3498 DCHECK(!GetCompilerOptions().GetCompilePic());
3499 break;
3500 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3501 DCHECK(GetCompilerOptions().GetCompilePic());
3502 break;
3503 case HLoadClass::LoadKind::kBootImageAddress:
3504 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003505 case HLoadClass::LoadKind::kBssEntry:
3506 DCHECK(!Runtime::Current()->UseJitCompilation());
3507 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003508 case HLoadClass::LoadKind::kJitTableAddress:
3509 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003510 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003511 case HLoadClass::LoadKind::kDexCacheViaMethod:
3512 break;
3513 }
3514 if (fallback_load) {
3515 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3516 }
3517 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003518}
3519
Vladimir Markodc151b22015-10-15 18:02:30 +01003520HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3521 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003522 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003523 // On MIPS64 we support all dispatch types.
3524 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003525}
3526
Alexey Frunze4dda3372015-06-01 18:31:49 -07003527void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3528 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003529 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003530 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3531 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3532
Alexey Frunze19f6c692016-11-30 19:19:55 -08003533 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003534 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003535 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003536 uint32_t offset =
3537 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003538 __ LoadFromOffset(kLoadDoubleword,
3539 temp.AsRegister<GpuRegister>(),
3540 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003541 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003542 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003543 }
Vladimir Marko58155012015-08-19 12:49:41 +00003544 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003545 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003546 break;
3547 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003548 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3549 kLoadDoubleword,
3550 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003551 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003552 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3553 uint32_t offset = invoke->GetDexCacheArrayOffset();
3554 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003555 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003556 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3557 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3558 break;
3559 }
Vladimir Marko58155012015-08-19 12:49:41 +00003560 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003561 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003562 GpuRegister reg = temp.AsRegister<GpuRegister>();
3563 GpuRegister method_reg;
3564 if (current_method.IsRegister()) {
3565 method_reg = current_method.AsRegister<GpuRegister>();
3566 } else {
3567 // TODO: use the appropriate DCHECK() here if possible.
3568 // DCHECK(invoke->GetLocations()->Intrinsified());
3569 DCHECK(!current_method.IsValid());
3570 method_reg = reg;
3571 __ Ld(reg, SP, kCurrentMethodStackOffset);
3572 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003573
Vladimir Marko58155012015-08-19 12:49:41 +00003574 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003575 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003576 reg,
3577 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003578 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003579 // temp = temp[index_in_cache];
3580 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3581 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003582 __ LoadFromOffset(kLoadDoubleword,
3583 reg,
3584 reg,
3585 CodeGenerator::GetCachePointerOffset(index_in_cache));
3586 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 }
3589
Alexey Frunze19f6c692016-11-30 19:19:55 -08003590 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003591 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003592 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003593 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003594 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3595 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3596 __ LoadFromOffset(kLoadDoubleword,
3597 T9,
3598 callee_method.AsRegister<GpuRegister>(),
3599 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003600 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003601 // T9()
3602 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003603 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003604 break;
3605 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003606 DCHECK(!IsLeafMethod());
3607}
3608
3609void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003610 // Explicit clinit checks triggered by static invokes must have been pruned by
3611 // art::PrepareForRegisterAllocation.
3612 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003613
3614 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3615 return;
3616 }
3617
3618 LocationSummary* locations = invoke->GetLocations();
3619 codegen_->GenerateStaticOrDirectCall(invoke,
3620 locations->HasTemps()
3621 ? locations->GetTemp(0)
3622 : Location::NoLocation());
3623 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3624}
3625
Alexey Frunze53afca12015-11-05 16:34:23 -08003626void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003627 // Use the calling convention instead of the location of the receiver, as
3628 // intrinsics may have put the receiver in a different register. In the intrinsics
3629 // slow path, the arguments have been moved to the right place, so here we are
3630 // guaranteed that the receiver is the first register of the calling convention.
3631 InvokeDexCallingConvention calling_convention;
3632 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3633
Alexey Frunze53afca12015-11-05 16:34:23 -08003634 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3636 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3637 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003638 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003639
3640 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003641 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003642 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003643 // Instead of simply (possibly) unpoisoning `temp` here, we should
3644 // emit a read barrier for the previous class reference load.
3645 // However this is not required in practice, as this is an
3646 // intermediate/temporary reference and because the current
3647 // concurrent copying collector keeps the from-space memory
3648 // intact/accessible until the end of the marking phase (the
3649 // concurrent copying collector may not in the future).
3650 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003651 // temp = temp->GetMethodAt(method_offset);
3652 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3653 // T9 = temp->GetEntryPoint();
3654 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3655 // T9();
3656 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003657 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003658}
3659
3660void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3661 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3662 return;
3663 }
3664
3665 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003666 DCHECK(!codegen_->IsLeafMethod());
3667 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3668}
3669
3670void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003671 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3672 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003673 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003674 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003675 cls,
3676 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003677 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003678 return;
3679 }
Vladimir Marko41559982017-01-06 14:04:23 +00003680 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003681
3682 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3683 ? LocationSummary::kCallOnSlowPath
3684 : LocationSummary::kNoCall;
3685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003686 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003687 locations->SetInAt(0, Location::RequiresRegister());
3688 }
3689 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003690}
3691
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003692// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3693// move.
3694void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003695 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3696 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3697 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003698 return;
3699 }
Vladimir Marko41559982017-01-06 14:04:23 +00003700 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003701
Vladimir Marko41559982017-01-06 14:04:23 +00003702 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003703 Location out_loc = locations->Out();
3704 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3705 GpuRegister current_method_reg = ZERO;
3706 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3707 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3708 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3709 }
3710
3711 bool generate_null_check = false;
3712 switch (load_kind) {
3713 case HLoadClass::LoadKind::kReferrersClass:
3714 DCHECK(!cls->CanCallRuntime());
3715 DCHECK(!cls->MustGenerateClinitCheck());
3716 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3717 GenerateGcRootFieldLoad(cls,
3718 out_loc,
3719 current_method_reg,
3720 ArtMethod::DeclaringClassOffset().Int32Value());
3721 break;
3722 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003723 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003724 __ LoadLiteral(out,
3725 kLoadUnsignedWord,
3726 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3727 cls->GetTypeIndex()));
3728 break;
3729 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003730 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003731 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3732 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3733 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3734 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3735 break;
3736 }
3737 case HLoadClass::LoadKind::kBootImageAddress: {
3738 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003739 uint32_t address = dchecked_integral_cast<uint32_t>(
3740 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
3741 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003742 __ LoadLiteral(out,
3743 kLoadUnsignedWord,
3744 codegen_->DeduplicateBootImageAddressLiteral(address));
3745 break;
3746 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003747 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003748 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00003749 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003750 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3751 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003752 generate_null_check = true;
3753 break;
3754 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003755 case HLoadClass::LoadKind::kJitTableAddress:
3756 __ LoadLiteral(out,
3757 kLoadUnsignedWord,
3758 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
3759 cls->GetTypeIndex(),
3760 cls->GetClass()));
3761 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003762 break;
Vladimir Marko41559982017-01-06 14:04:23 +00003763 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003764 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00003765 LOG(FATAL) << "UNREACHABLE";
3766 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003767 }
3768
3769 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3770 DCHECK(cls->CanCallRuntime());
3771 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3772 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3773 codegen_->AddSlowPath(slow_path);
3774 if (generate_null_check) {
3775 __ Beqzc(out, slow_path->GetEntryLabel());
3776 }
3777 if (cls->MustGenerateClinitCheck()) {
3778 GenerateClassInitializationCheck(slow_path, out);
3779 } else {
3780 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003781 }
3782 }
3783}
3784
David Brazdilcb1c0552015-08-04 16:22:25 +01003785static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003786 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003787}
3788
Alexey Frunze4dda3372015-06-01 18:31:49 -07003789void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3790 LocationSummary* locations =
3791 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3792 locations->SetOut(Location::RequiresRegister());
3793}
3794
3795void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3796 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003797 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3798}
3799
3800void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3801 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3802}
3803
3804void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3805 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003806}
3807
Alexey Frunze4dda3372015-06-01 18:31:49 -07003808void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003809 HLoadString::LoadKind load_kind = load->GetLoadKind();
3810 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003812 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3813 InvokeRuntimeCallingConvention calling_convention;
3814 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3815 } else {
3816 locations->SetOut(Location::RequiresRegister());
3817 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003818}
3819
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003820// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3821// move.
3822void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003823 HLoadString::LoadKind load_kind = load->GetLoadKind();
3824 LocationSummary* locations = load->GetLocations();
3825 Location out_loc = locations->Out();
3826 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3827
3828 switch (load_kind) {
3829 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003830 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003831 __ LoadLiteral(out,
3832 kLoadUnsignedWord,
3833 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3834 load->GetStringIndex()));
3835 return; // No dex cache slow path.
3836 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3837 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3838 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003839 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003840 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3841 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3842 return; // No dex cache slow path.
3843 }
3844 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003845 uint32_t address = dchecked_integral_cast<uint32_t>(
3846 reinterpret_cast<uintptr_t>(load->GetString().Get()));
3847 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003848 __ LoadLiteral(out,
3849 kLoadUnsignedWord,
3850 codegen_->DeduplicateBootImageAddressLiteral(address));
3851 return; // No dex cache slow path.
3852 }
3853 case HLoadString::LoadKind::kBssEntry: {
3854 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3855 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003856 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003857 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3858 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003859 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3860 codegen_->AddSlowPath(slow_path);
3861 __ Beqzc(out, slow_path->GetEntryLabel());
3862 __ Bind(slow_path->GetExitLabel());
3863 return;
3864 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003865 case HLoadString::LoadKind::kJitTableAddress:
3866 __ LoadLiteral(out,
3867 kLoadUnsignedWord,
3868 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
3869 load->GetStringIndex(),
3870 load->GetString()));
3871 GenerateGcRootFieldLoad(load, out_loc, out, 0);
3872 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003873 default:
3874 break;
3875 }
3876
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003877 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003878 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3879 InvokeRuntimeCallingConvention calling_convention;
3880 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3881 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3882 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003883}
3884
Alexey Frunze4dda3372015-06-01 18:31:49 -07003885void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3887 locations->SetOut(Location::ConstantLocation(constant));
3888}
3889
3890void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3891 // Will be generated at use site.
3892}
3893
3894void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3895 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003897 InvokeRuntimeCallingConvention calling_convention;
3898 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3899}
3900
3901void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003902 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003903 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003904 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003905 if (instruction->IsEnter()) {
3906 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3907 } else {
3908 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3909 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003910}
3911
3912void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3915 switch (mul->GetResultType()) {
3916 case Primitive::kPrimInt:
3917 case Primitive::kPrimLong:
3918 locations->SetInAt(0, Location::RequiresRegister());
3919 locations->SetInAt(1, Location::RequiresRegister());
3920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3921 break;
3922
3923 case Primitive::kPrimFloat:
3924 case Primitive::kPrimDouble:
3925 locations->SetInAt(0, Location::RequiresFpuRegister());
3926 locations->SetInAt(1, Location::RequiresFpuRegister());
3927 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3928 break;
3929
3930 default:
3931 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3932 }
3933}
3934
3935void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3936 Primitive::Type type = instruction->GetType();
3937 LocationSummary* locations = instruction->GetLocations();
3938
3939 switch (type) {
3940 case Primitive::kPrimInt:
3941 case Primitive::kPrimLong: {
3942 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3943 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3944 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3945 if (type == Primitive::kPrimInt)
3946 __ MulR6(dst, lhs, rhs);
3947 else
3948 __ Dmul(dst, lhs, rhs);
3949 break;
3950 }
3951 case Primitive::kPrimFloat:
3952 case Primitive::kPrimDouble: {
3953 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3954 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3955 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3956 if (type == Primitive::kPrimFloat)
3957 __ MulS(dst, lhs, rhs);
3958 else
3959 __ MulD(dst, lhs, rhs);
3960 break;
3961 }
3962 default:
3963 LOG(FATAL) << "Unexpected mul type " << type;
3964 }
3965}
3966
3967void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3968 LocationSummary* locations =
3969 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3970 switch (neg->GetResultType()) {
3971 case Primitive::kPrimInt:
3972 case Primitive::kPrimLong:
3973 locations->SetInAt(0, Location::RequiresRegister());
3974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3975 break;
3976
3977 case Primitive::kPrimFloat:
3978 case Primitive::kPrimDouble:
3979 locations->SetInAt(0, Location::RequiresFpuRegister());
3980 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3981 break;
3982
3983 default:
3984 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3985 }
3986}
3987
3988void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3989 Primitive::Type type = instruction->GetType();
3990 LocationSummary* locations = instruction->GetLocations();
3991
3992 switch (type) {
3993 case Primitive::kPrimInt:
3994 case Primitive::kPrimLong: {
3995 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3996 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3997 if (type == Primitive::kPrimInt)
3998 __ Subu(dst, ZERO, src);
3999 else
4000 __ Dsubu(dst, ZERO, src);
4001 break;
4002 }
4003 case Primitive::kPrimFloat:
4004 case Primitive::kPrimDouble: {
4005 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4006 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4007 if (type == Primitive::kPrimFloat)
4008 __ NegS(dst, src);
4009 else
4010 __ NegD(dst, src);
4011 break;
4012 }
4013 default:
4014 LOG(FATAL) << "Unexpected neg type " << type;
4015 }
4016}
4017
4018void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
4019 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004021 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004022 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004023 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4024 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004025}
4026
4027void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004028 // Note: if heap poisoning is enabled, the entry point takes care
4029 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004030 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4031 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004032}
4033
4034void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
4035 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004037 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004038 if (instruction->IsStringAlloc()) {
4039 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4040 } else {
4041 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004042 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004043 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4044}
4045
4046void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004047 // Note: if heap poisoning is enabled, the entry point takes care
4048 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004049 if (instruction->IsStringAlloc()) {
4050 // String is allocated through StringFactory. Call NewEmptyString entry point.
4051 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02004052 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07004053 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004054 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4055 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
4056 __ Jalr(T9);
4057 __ Nop();
4058 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4059 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01004060 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004061 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004062 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004063}
4064
4065void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
4066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4067 locations->SetInAt(0, Location::RequiresRegister());
4068 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4069}
4070
4071void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
4072 Primitive::Type type = instruction->GetType();
4073 LocationSummary* locations = instruction->GetLocations();
4074
4075 switch (type) {
4076 case Primitive::kPrimInt:
4077 case Primitive::kPrimLong: {
4078 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4079 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4080 __ Nor(dst, src, ZERO);
4081 break;
4082 }
4083
4084 default:
4085 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4086 }
4087}
4088
4089void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4090 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4093}
4094
4095void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4096 LocationSummary* locations = instruction->GetLocations();
4097 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4098 locations->InAt(0).AsRegister<GpuRegister>(),
4099 1);
4100}
4101
4102void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004103 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4104 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004105}
4106
Calin Juravle2ae48182016-03-16 14:05:09 +00004107void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4108 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109 return;
4110 }
4111 Location obj = instruction->GetLocations()->InAt(0);
4112
4113 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004114 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115}
4116
Calin Juravle2ae48182016-03-16 14:05:09 +00004117void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004118 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004119 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004120
4121 Location obj = instruction->GetLocations()->InAt(0);
4122
4123 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4124}
4125
4126void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004127 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004128}
4129
4130void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4131 HandleBinaryOp(instruction);
4132}
4133
4134void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4135 HandleBinaryOp(instruction);
4136}
4137
4138void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4139 LOG(FATAL) << "Unreachable";
4140}
4141
4142void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4143 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4144}
4145
4146void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4148 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4149 if (location.IsStackSlot()) {
4150 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4151 } else if (location.IsDoubleStackSlot()) {
4152 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4153 }
4154 locations->SetOut(location);
4155}
4156
4157void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4158 ATTRIBUTE_UNUSED) {
4159 // Nothing to do, the parameter is already at its location.
4160}
4161
4162void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4163 LocationSummary* locations =
4164 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4165 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4166}
4167
4168void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4169 ATTRIBUTE_UNUSED) {
4170 // Nothing to do, the method is already at its location.
4171}
4172
4173void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004175 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004176 locations->SetInAt(i, Location::Any());
4177 }
4178 locations->SetOut(Location::Any());
4179}
4180
4181void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4182 LOG(FATAL) << "Unreachable";
4183}
4184
4185void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4186 Primitive::Type type = rem->GetResultType();
4187 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004188 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4189 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4191
4192 switch (type) {
4193 case Primitive::kPrimInt:
4194 case Primitive::kPrimLong:
4195 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004196 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4198 break;
4199
4200 case Primitive::kPrimFloat:
4201 case Primitive::kPrimDouble: {
4202 InvokeRuntimeCallingConvention calling_convention;
4203 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4204 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4205 locations->SetOut(calling_convention.GetReturnLocation(type));
4206 break;
4207 }
4208
4209 default:
4210 LOG(FATAL) << "Unexpected rem type " << type;
4211 }
4212}
4213
4214void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4215 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004216
4217 switch (type) {
4218 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004219 case Primitive::kPrimLong:
4220 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004221 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004222
4223 case Primitive::kPrimFloat:
4224 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004225 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4226 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004227 if (type == Primitive::kPrimFloat) {
4228 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4229 } else {
4230 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4231 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004232 break;
4233 }
4234 default:
4235 LOG(FATAL) << "Unexpected rem type " << type;
4236 }
4237}
4238
4239void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4240 memory_barrier->SetLocations(nullptr);
4241}
4242
4243void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4244 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4245}
4246
4247void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4249 Primitive::Type return_type = ret->InputAt(0)->GetType();
4250 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4251}
4252
4253void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4254 codegen_->GenerateFrameExit();
4255}
4256
4257void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4258 ret->SetLocations(nullptr);
4259}
4260
4261void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4262 codegen_->GenerateFrameExit();
4263}
4264
Alexey Frunze92d90602015-12-18 18:16:36 -08004265void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4266 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004267}
4268
Alexey Frunze92d90602015-12-18 18:16:36 -08004269void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4270 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004271}
4272
Alexey Frunze4dda3372015-06-01 18:31:49 -07004273void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4274 HandleShift(shl);
4275}
4276
4277void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4278 HandleShift(shl);
4279}
4280
4281void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4282 HandleShift(shr);
4283}
4284
4285void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4286 HandleShift(shr);
4287}
4288
Alexey Frunze4dda3372015-06-01 18:31:49 -07004289void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4290 HandleBinaryOp(instruction);
4291}
4292
4293void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4294 HandleBinaryOp(instruction);
4295}
4296
4297void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4298 HandleFieldGet(instruction, instruction->GetFieldInfo());
4299}
4300
4301void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4302 HandleFieldGet(instruction, instruction->GetFieldInfo());
4303}
4304
4305void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4306 HandleFieldSet(instruction, instruction->GetFieldInfo());
4307}
4308
4309void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004310 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004311}
4312
Calin Juravlee460d1d2015-09-29 04:52:17 +01004313void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4314 HUnresolvedInstanceFieldGet* instruction) {
4315 FieldAccessCallingConventionMIPS64 calling_convention;
4316 codegen_->CreateUnresolvedFieldLocationSummary(
4317 instruction, instruction->GetFieldType(), calling_convention);
4318}
4319
4320void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4321 HUnresolvedInstanceFieldGet* instruction) {
4322 FieldAccessCallingConventionMIPS64 calling_convention;
4323 codegen_->GenerateUnresolvedFieldAccess(instruction,
4324 instruction->GetFieldType(),
4325 instruction->GetFieldIndex(),
4326 instruction->GetDexPc(),
4327 calling_convention);
4328}
4329
4330void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4331 HUnresolvedInstanceFieldSet* instruction) {
4332 FieldAccessCallingConventionMIPS64 calling_convention;
4333 codegen_->CreateUnresolvedFieldLocationSummary(
4334 instruction, instruction->GetFieldType(), calling_convention);
4335}
4336
4337void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4338 HUnresolvedInstanceFieldSet* instruction) {
4339 FieldAccessCallingConventionMIPS64 calling_convention;
4340 codegen_->GenerateUnresolvedFieldAccess(instruction,
4341 instruction->GetFieldType(),
4342 instruction->GetFieldIndex(),
4343 instruction->GetDexPc(),
4344 calling_convention);
4345}
4346
4347void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4348 HUnresolvedStaticFieldGet* instruction) {
4349 FieldAccessCallingConventionMIPS64 calling_convention;
4350 codegen_->CreateUnresolvedFieldLocationSummary(
4351 instruction, instruction->GetFieldType(), calling_convention);
4352}
4353
4354void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4355 HUnresolvedStaticFieldGet* instruction) {
4356 FieldAccessCallingConventionMIPS64 calling_convention;
4357 codegen_->GenerateUnresolvedFieldAccess(instruction,
4358 instruction->GetFieldType(),
4359 instruction->GetFieldIndex(),
4360 instruction->GetDexPc(),
4361 calling_convention);
4362}
4363
4364void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4365 HUnresolvedStaticFieldSet* instruction) {
4366 FieldAccessCallingConventionMIPS64 calling_convention;
4367 codegen_->CreateUnresolvedFieldLocationSummary(
4368 instruction, instruction->GetFieldType(), calling_convention);
4369}
4370
4371void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4372 HUnresolvedStaticFieldSet* instruction) {
4373 FieldAccessCallingConventionMIPS64 calling_convention;
4374 codegen_->GenerateUnresolvedFieldAccess(instruction,
4375 instruction->GetFieldType(),
4376 instruction->GetFieldIndex(),
4377 instruction->GetDexPc(),
4378 calling_convention);
4379}
4380
Alexey Frunze4dda3372015-06-01 18:31:49 -07004381void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004382 LocationSummary* locations =
4383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004384 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004385}
4386
4387void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4388 HBasicBlock* block = instruction->GetBlock();
4389 if (block->GetLoopInformation() != nullptr) {
4390 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4391 // The back edge will generate the suspend check.
4392 return;
4393 }
4394 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4395 // The goto will generate the suspend check.
4396 return;
4397 }
4398 GenerateSuspendCheck(instruction, nullptr);
4399}
4400
Alexey Frunze4dda3372015-06-01 18:31:49 -07004401void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4402 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004404 InvokeRuntimeCallingConvention calling_convention;
4405 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4406}
4407
4408void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004409 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004410 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4411}
4412
4413void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4414 Primitive::Type input_type = conversion->GetInputType();
4415 Primitive::Type result_type = conversion->GetResultType();
4416 DCHECK_NE(input_type, result_type);
4417
4418 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4419 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4420 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4421 }
4422
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004423 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4424
4425 if (Primitive::IsFloatingPointType(input_type)) {
4426 locations->SetInAt(0, Location::RequiresFpuRegister());
4427 } else {
4428 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004429 }
4430
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004431 if (Primitive::IsFloatingPointType(result_type)) {
4432 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004433 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004435 }
4436}
4437
4438void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4439 LocationSummary* locations = conversion->GetLocations();
4440 Primitive::Type result_type = conversion->GetResultType();
4441 Primitive::Type input_type = conversion->GetInputType();
4442
4443 DCHECK_NE(input_type, result_type);
4444
4445 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4446 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4447 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4448
4449 switch (result_type) {
4450 case Primitive::kPrimChar:
4451 __ Andi(dst, src, 0xFFFF);
4452 break;
4453 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004454 if (input_type == Primitive::kPrimLong) {
4455 // Type conversion from long to types narrower than int is a result of code
4456 // transformations. To avoid unpredictable results for SEB and SEH, we first
4457 // need to sign-extend the low 32-bit value into bits 32 through 63.
4458 __ Sll(dst, src, 0);
4459 __ Seb(dst, dst);
4460 } else {
4461 __ Seb(dst, src);
4462 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004463 break;
4464 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004465 if (input_type == Primitive::kPrimLong) {
4466 // Type conversion from long to types narrower than int is a result of code
4467 // transformations. To avoid unpredictable results for SEB and SEH, we first
4468 // need to sign-extend the low 32-bit value into bits 32 through 63.
4469 __ Sll(dst, src, 0);
4470 __ Seh(dst, dst);
4471 } else {
4472 __ Seh(dst, src);
4473 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004474 break;
4475 case Primitive::kPrimInt:
4476 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004477 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4478 // conversions, except when the input and output registers are the same and we are not
4479 // converting longs to shorter types. In these cases, do nothing.
4480 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4481 __ Sll(dst, src, 0);
4482 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004483 break;
4484
4485 default:
4486 LOG(FATAL) << "Unexpected type conversion from " << input_type
4487 << " to " << result_type;
4488 }
4489 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004490 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4491 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4492 if (input_type == Primitive::kPrimLong) {
4493 __ Dmtc1(src, FTMP);
4494 if (result_type == Primitive::kPrimFloat) {
4495 __ Cvtsl(dst, FTMP);
4496 } else {
4497 __ Cvtdl(dst, FTMP);
4498 }
4499 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004500 __ Mtc1(src, FTMP);
4501 if (result_type == Primitive::kPrimFloat) {
4502 __ Cvtsw(dst, FTMP);
4503 } else {
4504 __ Cvtdw(dst, FTMP);
4505 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004506 }
4507 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4508 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004509 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4510 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4511 Mips64Label truncate;
4512 Mips64Label done;
4513
4514 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4515 // value when the input is either a NaN or is outside of the range of the output type
4516 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4517 // the same result.
4518 //
4519 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4520 // value of the output type if the input is outside of the range after the truncation or
4521 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4522 // results. This matches the desired float/double-to-int/long conversion exactly.
4523 //
4524 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4525 //
4526 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4527 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4528 // even though it must be NAN2008=1 on R6.
4529 //
4530 // The code takes care of the different behaviors by first comparing the input to the
4531 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4532 // If the input is greater than or equal to the minimum, it procedes to the truncate
4533 // instruction, which will handle such an input the same way irrespective of NAN2008.
4534 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4535 // in order to return either zero or the minimum value.
4536 //
4537 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4538 // truncate instruction for MIPS64R6.
4539 if (input_type == Primitive::kPrimFloat) {
4540 uint32_t min_val = (result_type == Primitive::kPrimLong)
4541 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4542 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4543 __ LoadConst32(TMP, min_val);
4544 __ Mtc1(TMP, FTMP);
4545 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004546 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004547 uint64_t min_val = (result_type == Primitive::kPrimLong)
4548 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4549 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4550 __ LoadConst64(TMP, min_val);
4551 __ Dmtc1(TMP, FTMP);
4552 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004553 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004554
4555 __ Bc1nez(FTMP, &truncate);
4556
4557 if (input_type == Primitive::kPrimFloat) {
4558 __ CmpEqS(FTMP, src, src);
4559 } else {
4560 __ CmpEqD(FTMP, src, src);
4561 }
4562 if (result_type == Primitive::kPrimLong) {
4563 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4564 } else {
4565 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4566 }
4567 __ Mfc1(TMP, FTMP);
4568 __ And(dst, dst, TMP);
4569
4570 __ Bc(&done);
4571
4572 __ Bind(&truncate);
4573
4574 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004575 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004576 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004577 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004578 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004579 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004580 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004581 } else {
4582 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004583 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004584 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004585 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004586 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004587 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004588 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004589
4590 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004591 } else if (Primitive::IsFloatingPointType(result_type) &&
4592 Primitive::IsFloatingPointType(input_type)) {
4593 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4594 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4595 if (result_type == Primitive::kPrimFloat) {
4596 __ Cvtsd(dst, src);
4597 } else {
4598 __ Cvtds(dst, src);
4599 }
4600 } else {
4601 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4602 << " to " << result_type;
4603 }
4604}
4605
4606void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4607 HandleShift(ushr);
4608}
4609
4610void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4611 HandleShift(ushr);
4612}
4613
4614void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4615 HandleBinaryOp(instruction);
4616}
4617
4618void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4619 HandleBinaryOp(instruction);
4620}
4621
4622void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4623 // Nothing to do, this should be removed during prepare for register allocator.
4624 LOG(FATAL) << "Unreachable";
4625}
4626
4627void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4628 // Nothing to do, this should be removed during prepare for register allocator.
4629 LOG(FATAL) << "Unreachable";
4630}
4631
4632void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004633 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004634}
4635
4636void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004637 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004638}
4639
4640void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004641 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004642}
4643
4644void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004645 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004646}
4647
4648void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004649 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004650}
4651
4652void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004653 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004654}
4655
4656void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004657 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004658}
4659
4660void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004661 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004662}
4663
4664void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004665 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004666}
4667
4668void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004669 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004670}
4671
4672void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004673 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004674}
4675
4676void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004677 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004678}
4679
Aart Bike9f37602015-10-09 11:15:55 -07004680void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004681 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004682}
4683
4684void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004685 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004686}
4687
4688void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004689 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004690}
4691
4692void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004693 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004694}
4695
4696void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004697 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004698}
4699
4700void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004701 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004702}
4703
4704void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004705 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004706}
4707
4708void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004709 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004710}
4711
Mark Mendellfe57faa2015-09-18 09:26:15 -04004712// Simple implementation of packed switch - generate cascaded compare/jumps.
4713void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4714 LocationSummary* locations =
4715 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4716 locations->SetInAt(0, Location::RequiresRegister());
4717}
4718
Alexey Frunze0960ac52016-12-20 17:24:59 -08004719void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4720 int32_t lower_bound,
4721 uint32_t num_entries,
4722 HBasicBlock* switch_block,
4723 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004724 // Create a set of compare/jumps.
4725 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004726 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004727 // Jump to default if index is negative
4728 // Note: We don't check the case that index is positive while value < lower_bound, because in
4729 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4730 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4731
Alexey Frunze0960ac52016-12-20 17:24:59 -08004732 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004733 // Jump to successors[0] if value == lower_bound.
4734 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4735 int32_t last_index = 0;
4736 for (; num_entries - last_index > 2; last_index += 2) {
4737 __ Addiu(temp_reg, temp_reg, -2);
4738 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4739 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4740 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4741 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4742 }
4743 if (num_entries - last_index == 2) {
4744 // The last missing case_value.
4745 __ Addiu(temp_reg, temp_reg, -1);
4746 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004747 }
4748
4749 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08004750 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004751 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004752 }
4753}
4754
Alexey Frunze0960ac52016-12-20 17:24:59 -08004755void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
4756 int32_t lower_bound,
4757 uint32_t num_entries,
4758 HBasicBlock* switch_block,
4759 HBasicBlock* default_block) {
4760 // Create a jump table.
4761 std::vector<Mips64Label*> labels(num_entries);
4762 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
4763 for (uint32_t i = 0; i < num_entries; i++) {
4764 labels[i] = codegen_->GetLabelOf(successors[i]);
4765 }
4766 JumpTable* table = __ CreateJumpTable(std::move(labels));
4767
4768 // Is the value in range?
4769 __ Addiu32(TMP, value_reg, -lower_bound);
4770 __ LoadConst32(AT, num_entries);
4771 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
4772
4773 // We are in the range of the table.
4774 // Load the target address from the jump table, indexing by the value.
4775 __ LoadLabelAddress(AT, table->GetLabel());
4776 __ Sll(TMP, TMP, 2);
4777 __ Daddu(TMP, TMP, AT);
4778 __ Lw(TMP, TMP, 0);
4779 // Compute the absolute target address by adding the table start address
4780 // (the table contains offsets to targets relative to its start).
4781 __ Daddu(TMP, TMP, AT);
4782 // And jump.
4783 __ Jr(TMP);
4784 __ Nop();
4785}
4786
4787void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4788 int32_t lower_bound = switch_instr->GetStartValue();
4789 uint32_t num_entries = switch_instr->GetNumEntries();
4790 LocationSummary* locations = switch_instr->GetLocations();
4791 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4792 HBasicBlock* switch_block = switch_instr->GetBlock();
4793 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4794
4795 if (num_entries > kPackedSwitchJumpTableThreshold) {
4796 GenTableBasedPackedSwitch(value_reg,
4797 lower_bound,
4798 num_entries,
4799 switch_block,
4800 default_block);
4801 } else {
4802 GenPackedSwitchWithCompares(value_reg,
4803 lower_bound,
4804 num_entries,
4805 switch_block,
4806 default_block);
4807 }
4808}
4809
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004810void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4811 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4812}
4813
4814void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4815 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4816}
4817
Alexey Frunze4dda3372015-06-01 18:31:49 -07004818} // namespace mips64
4819} // namespace art