blob: 580bc4737c44cdacded82631ec5c9d6ed756ea5c [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 Frunze627c1a02017-01-30 19:28:14 -0800433 jit_string_patches_(StringReferenceValueComparator(),
434 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
435 jit_class_patches_(TypeReferenceValueComparator(),
436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700437 // Save RA (containing the return address) to mimic Quick.
438 AddAllocatedRegister(Location::RegisterLocation(RA));
439}
440
441#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100442// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
443#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700444#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700445
446void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700447 // Ensure that we fix up branches.
448 __ FinalizeCode();
449
450 // Adjust native pc offsets in stack maps.
451 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800452 uint32_t old_position =
453 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700454 uint32_t new_position = __ GetAdjustedPosition(old_position);
455 DCHECK_GE(new_position, old_position);
456 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
457 }
458
459 // Adjust pc offsets for the disassembly information.
460 if (disasm_info_ != nullptr) {
461 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
462 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
463 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
464 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
465 it.second.start = __ GetAdjustedPosition(it.second.start);
466 it.second.end = __ GetAdjustedPosition(it.second.end);
467 }
468 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
469 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
470 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
471 }
472 }
473
Alexey Frunze4dda3372015-06-01 18:31:49 -0700474 CodeGenerator::Finalize(allocator);
475}
476
477Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
478 return codegen_->GetAssembler();
479}
480
481void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100482 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
484}
485
486void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100487 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700488 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
489}
490
491void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
492 // Pop reg
493 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200494 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700495}
496
497void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
498 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200499 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700500 __ Sd(GpuRegister(reg), SP, 0);
501}
502
503void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
504 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
505 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
506 // Allocate a scratch register other than TMP, if available.
507 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
508 // automatically unspilled when the scratch scope object is destroyed).
509 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
510 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200511 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700512 __ LoadFromOffset(load_type,
513 GpuRegister(ensure_scratch.GetRegister()),
514 SP,
515 index1 + stack_offset);
516 __ LoadFromOffset(load_type,
517 TMP,
518 SP,
519 index2 + stack_offset);
520 __ StoreToOffset(store_type,
521 GpuRegister(ensure_scratch.GetRegister()),
522 SP,
523 index2 + stack_offset);
524 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
525}
526
527static dwarf::Reg DWARFReg(GpuRegister reg) {
528 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
529}
530
David Srbeckyba702002016-02-01 18:15:29 +0000531static dwarf::Reg DWARFReg(FpuRegister reg) {
532 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
533}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700534
535void CodeGeneratorMIPS64::GenerateFrameEntry() {
536 __ Bind(&frame_entry_label_);
537
538 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
539
540 if (do_overflow_check) {
541 __ LoadFromOffset(kLoadWord,
542 ZERO,
543 SP,
544 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
545 RecordPcInfo(nullptr, 0);
546 }
547
Alexey Frunze4dda3372015-06-01 18:31:49 -0700548 if (HasEmptyFrame()) {
549 return;
550 }
551
552 // Make sure the frame size isn't unreasonably large. Per the various APIs
553 // it looks like it should always be less than 2GB in size, which allows
554 // us using 32-bit signed offsets from the stack pointer.
555 if (GetFrameSize() > 0x7FFFFFFF)
556 LOG(FATAL) << "Stack frame larger than 2GB";
557
558 // Spill callee-saved registers.
559 // Note that their cumulative size is small and they can be indexed using
560 // 16-bit offsets.
561
562 // TODO: increment/decrement SP in one step instead of two or remove this comment.
563
564 uint32_t ofs = FrameEntrySpillSize();
565 __ IncreaseFrameSize(ofs);
566
567 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
568 GpuRegister reg = kCoreCalleeSaves[i];
569 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200570 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 __ Sd(reg, SP, ofs);
572 __ cfi().RelOffset(DWARFReg(reg), ofs);
573 }
574 }
575
576 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
577 FpuRegister reg = kFpuCalleeSaves[i];
578 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200579 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700580 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000581 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700582 }
583 }
584
585 // Allocate the rest of the frame and store the current method pointer
586 // at its end.
587
588 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
589
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100590 // Save the current method if we need it. Note that we do not
591 // do this in HCurrentMethod, as the instruction might have been removed
592 // in the SSA graph.
593 if (RequiresCurrentMethod()) {
594 static_assert(IsInt<16>(kCurrentMethodStackOffset),
595 "kCurrentMethodStackOffset must fit into int16_t");
596 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
597 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100598
599 if (GetGraph()->HasShouldDeoptimizeFlag()) {
600 // Initialize should_deoptimize flag to 0.
601 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
602 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700603}
604
605void CodeGeneratorMIPS64::GenerateFrameExit() {
606 __ cfi().RememberState();
607
Alexey Frunze4dda3372015-06-01 18:31:49 -0700608 if (!HasEmptyFrame()) {
609 // Deallocate the rest of the frame.
610
611 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
612
613 // Restore callee-saved registers.
614 // Note that their cumulative size is small and they can be indexed using
615 // 16-bit offsets.
616
617 // TODO: increment/decrement SP in one step instead of two or remove this comment.
618
619 uint32_t ofs = 0;
620
621 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
622 FpuRegister reg = kFpuCalleeSaves[i];
623 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
624 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200625 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000626 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700627 }
628 }
629
630 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
631 GpuRegister reg = kCoreCalleeSaves[i];
632 if (allocated_registers_.ContainsCoreRegister(reg)) {
633 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200634 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700635 __ cfi().Restore(DWARFReg(reg));
636 }
637 }
638
639 DCHECK_EQ(ofs, FrameEntrySpillSize());
640 __ DecreaseFrameSize(ofs);
641 }
642
643 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700644 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700645
646 __ cfi().RestoreState();
647 __ cfi().DefCFAOffset(GetFrameSize());
648}
649
650void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
651 __ Bind(GetLabelOf(block));
652}
653
654void CodeGeneratorMIPS64::MoveLocation(Location destination,
655 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100656 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700657 if (source.Equals(destination)) {
658 return;
659 }
660
661 // A valid move can always be inferred from the destination and source
662 // locations. When moving from and to a register, the argument type can be
663 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100664 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 DCHECK_EQ(unspecified_type, false);
666
667 if (destination.IsRegister() || destination.IsFpuRegister()) {
668 if (unspecified_type) {
669 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
670 if (source.IsStackSlot() ||
671 (src_cst != nullptr && (src_cst->IsIntConstant()
672 || src_cst->IsFloatConstant()
673 || src_cst->IsNullConstant()))) {
674 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100675 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700676 } else {
677 // If the source is a double stack slot or a 64bit constant, a 64bit
678 // type is appropriate. Else the source is a register, and since the
679 // type has not been specified, we chose a 64bit type to force a 64bit
680 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100681 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 }
683 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100684 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
685 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
687 // Move to GPR/FPR from stack
688 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100689 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700690 __ LoadFpuFromOffset(load_type,
691 destination.AsFpuRegister<FpuRegister>(),
692 SP,
693 source.GetStackIndex());
694 } else {
695 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
696 __ LoadFromOffset(load_type,
697 destination.AsRegister<GpuRegister>(),
698 SP,
699 source.GetStackIndex());
700 }
701 } else if (source.IsConstant()) {
702 // Move to GPR/FPR from constant
703 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100704 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700705 gpr = destination.AsRegister<GpuRegister>();
706 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100707 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700708 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
709 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
710 gpr = ZERO;
711 } else {
712 __ LoadConst32(gpr, value);
713 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700715 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
716 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
717 gpr = ZERO;
718 } else {
719 __ LoadConst64(gpr, value);
720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700721 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100722 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700723 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700725 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
726 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100727 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 if (destination.IsRegister()) {
729 // Move to GPR from GPR
730 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
731 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100732 DCHECK(destination.IsFpuRegister());
733 if (Primitive::Is64BitType(dst_type)) {
734 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
735 } else {
736 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
737 }
738 }
739 } else if (source.IsFpuRegister()) {
740 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700741 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100742 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700743 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
744 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100745 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700746 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
747 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100748 } else {
749 DCHECK(destination.IsRegister());
750 if (Primitive::Is64BitType(dst_type)) {
751 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
752 } else {
753 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
754 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700755 }
756 }
757 } else { // The destination is not a register. It must be a stack slot.
758 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
759 if (source.IsRegister() || source.IsFpuRegister()) {
760 if (unspecified_type) {
761 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100762 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700763 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100764 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700765 }
766 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100767 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
768 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700769 // Move to stack from GPR/FPR
770 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
771 if (source.IsRegister()) {
772 __ StoreToOffset(store_type,
773 source.AsRegister<GpuRegister>(),
774 SP,
775 destination.GetStackIndex());
776 } else {
777 __ StoreFpuToOffset(store_type,
778 source.AsFpuRegister<FpuRegister>(),
779 SP,
780 destination.GetStackIndex());
781 }
782 } else if (source.IsConstant()) {
783 // Move to stack from constant
784 HConstant* src_cst = source.GetConstant();
785 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700786 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700787 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
789 if (value != 0) {
790 gpr = TMP;
791 __ LoadConst32(gpr, value);
792 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700793 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700794 DCHECK(destination.IsDoubleStackSlot());
795 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
796 if (value != 0) {
797 gpr = TMP;
798 __ LoadConst64(gpr, value);
799 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700800 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700801 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700802 } else {
803 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
804 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
805 // Move to stack from stack
806 if (destination.IsStackSlot()) {
807 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
808 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
809 } else {
810 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
811 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
812 }
813 }
814 }
815}
816
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700817void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700818 DCHECK(!loc1.IsConstant());
819 DCHECK(!loc2.IsConstant());
820
821 if (loc1.Equals(loc2)) {
822 return;
823 }
824
825 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
826 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
827 bool is_fp_reg1 = loc1.IsFpuRegister();
828 bool is_fp_reg2 = loc2.IsFpuRegister();
829
830 if (loc2.IsRegister() && loc1.IsRegister()) {
831 // Swap 2 GPRs
832 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
833 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
834 __ Move(TMP, r2);
835 __ Move(r2, r1);
836 __ Move(r1, TMP);
837 } else if (is_fp_reg2 && is_fp_reg1) {
838 // Swap 2 FPRs
839 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
840 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700841 if (type == Primitive::kPrimFloat) {
842 __ MovS(FTMP, r1);
843 __ MovS(r1, r2);
844 __ MovS(r2, FTMP);
845 } else {
846 DCHECK_EQ(type, Primitive::kPrimDouble);
847 __ MovD(FTMP, r1);
848 __ MovD(r1, r2);
849 __ MovD(r2, FTMP);
850 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700851 } else if (is_slot1 != is_slot2) {
852 // Swap GPR/FPR and stack slot
853 Location reg_loc = is_slot1 ? loc2 : loc1;
854 Location mem_loc = is_slot1 ? loc1 : loc2;
855 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
856 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
857 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
858 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
859 if (reg_loc.IsFpuRegister()) {
860 __ StoreFpuToOffset(store_type,
861 reg_loc.AsFpuRegister<FpuRegister>(),
862 SP,
863 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700864 if (mem_loc.IsStackSlot()) {
865 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
866 } else {
867 DCHECK(mem_loc.IsDoubleStackSlot());
868 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
869 }
870 } else {
871 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
872 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
873 }
874 } else if (is_slot1 && is_slot2) {
875 move_resolver_.Exchange(loc1.GetStackIndex(),
876 loc2.GetStackIndex(),
877 loc1.IsDoubleStackSlot());
878 } else {
879 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
880 }
881}
882
Calin Juravle175dc732015-08-25 15:42:32 +0100883void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
884 DCHECK(location.IsRegister());
885 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
886}
887
Calin Juravlee460d1d2015-09-29 04:52:17 +0100888void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
889 if (location.IsRegister()) {
890 locations->AddTemp(location);
891 } else {
892 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
893 }
894}
895
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100896void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
897 GpuRegister value,
898 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700899 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700900 GpuRegister card = AT;
901 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100902 if (value_can_be_null) {
903 __ Beqzc(value, &done);
904 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700905 __ LoadFromOffset(kLoadDoubleword,
906 card,
907 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700908 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700909 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
910 __ Daddu(temp, card, temp);
911 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100912 if (value_can_be_null) {
913 __ Bind(&done);
914 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700915}
916
Alexey Frunze19f6c692016-11-30 19:19:55 -0800917template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
918inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
919 const ArenaDeque<PcRelativePatchInfo>& infos,
920 ArenaVector<LinkerPatch>* linker_patches) {
921 for (const PcRelativePatchInfo& info : infos) {
922 const DexFile& dex_file = info.target_dex_file;
923 size_t offset_or_index = info.offset_or_index;
924 DCHECK(info.pc_rel_label.IsBound());
925 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
926 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
927 }
928}
929
930void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
931 DCHECK(linker_patches->empty());
932 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -0800933 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800934 pc_relative_string_patches_.size() +
935 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +0000936 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800937 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +0000938 boot_image_type_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -0800939 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800940 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
941 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800942 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +0000943 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800944 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
945 linker_patches);
946 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000947 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
948 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800949 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
950 linker_patches);
951 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000952 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
953 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800954 for (const auto& entry : boot_image_string_patches_) {
955 const StringReference& target_string = entry.first;
956 Literal* literal = entry.second;
957 DCHECK(literal->GetLabel()->IsBound());
958 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
959 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
960 target_string.dex_file,
961 target_string.string_index.index_));
962 }
963 for (const auto& entry : boot_image_type_patches_) {
964 const TypeReference& target_type = entry.first;
965 Literal* literal = entry.second;
966 DCHECK(literal->GetLabel()->IsBound());
967 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
968 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
969 target_type.dex_file,
970 target_type.type_index.index_));
971 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000972 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800973}
974
975CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000976 const DexFile& dex_file, dex::StringIndex string_index) {
977 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800978}
979
980CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
981 const DexFile& dex_file, dex::TypeIndex type_index) {
982 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800983}
984
Vladimir Marko1998cd02017-01-13 13:02:58 +0000985CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
986 const DexFile& dex_file, dex::TypeIndex type_index) {
987 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
988}
989
Alexey Frunze19f6c692016-11-30 19:19:55 -0800990CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
991 const DexFile& dex_file, uint32_t element_offset) {
992 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
993}
994
Alexey Frunze19f6c692016-11-30 19:19:55 -0800995CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
996 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
997 patches->emplace_back(dex_file, offset_or_index);
998 return &patches->back();
999}
1000
Alexey Frunzef63f5692016-12-13 17:43:11 -08001001Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1002 return map->GetOrCreate(
1003 value,
1004 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1005}
1006
Alexey Frunze19f6c692016-11-30 19:19:55 -08001007Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1008 return uint64_literals_.GetOrCreate(
1009 value,
1010 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1011}
1012
1013Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1014 MethodToLiteralMap* map) {
1015 return map->GetOrCreate(
1016 target_method,
1017 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1018}
1019
Alexey Frunzef63f5692016-12-13 17:43:11 -08001020Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1021 dex::StringIndex string_index) {
1022 return boot_image_string_patches_.GetOrCreate(
1023 StringReference(&dex_file, string_index),
1024 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1025}
1026
1027Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1028 dex::TypeIndex type_index) {
1029 return boot_image_type_patches_.GetOrCreate(
1030 TypeReference(&dex_file, type_index),
1031 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1032}
1033
1034Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001035 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001036}
1037
Alexey Frunze19f6c692016-11-30 19:19:55 -08001038void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1039 GpuRegister out) {
1040 __ Bind(&info->pc_rel_label);
1041 // Add the high half of a 32-bit offset to PC.
1042 __ Auipc(out, /* placeholder */ 0x1234);
1043 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001044 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001045}
1046
Alexey Frunze627c1a02017-01-30 19:28:14 -08001047Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1048 dex::StringIndex string_index,
1049 Handle<mirror::String> handle) {
1050 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1051 reinterpret_cast64<uint64_t>(handle.GetReference()));
1052 return jit_string_patches_.GetOrCreate(
1053 StringReference(&dex_file, string_index),
1054 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1055}
1056
1057Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1058 dex::TypeIndex type_index,
1059 Handle<mirror::Class> handle) {
1060 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1061 reinterpret_cast64<uint64_t>(handle.GetReference()));
1062 return jit_class_patches_.GetOrCreate(
1063 TypeReference(&dex_file, type_index),
1064 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1065}
1066
1067void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1068 const uint8_t* roots_data,
1069 const Literal* literal,
1070 uint64_t index_in_table) const {
1071 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1072 uintptr_t address =
1073 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1074 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1075}
1076
1077void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1078 for (const auto& entry : jit_string_patches_) {
1079 const auto& it = jit_string_roots_.find(entry.first);
1080 DCHECK(it != jit_string_roots_.end());
1081 PatchJitRootUse(code, roots_data, entry.second, it->second);
1082 }
1083 for (const auto& entry : jit_class_patches_) {
1084 const auto& it = jit_class_roots_.find(entry.first);
1085 DCHECK(it != jit_class_roots_.end());
1086 PatchJitRootUse(code, roots_data, entry.second, it->second);
1087 }
1088}
1089
David Brazdil58282f42016-01-14 12:45:10 +00001090void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1092 blocked_core_registers_[ZERO] = true;
1093 blocked_core_registers_[K0] = true;
1094 blocked_core_registers_[K1] = true;
1095 blocked_core_registers_[GP] = true;
1096 blocked_core_registers_[SP] = true;
1097 blocked_core_registers_[RA] = true;
1098
Lazar Trsicd9672662015-09-03 17:33:01 +02001099 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1100 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001101 blocked_core_registers_[AT] = true;
1102 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001103 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001104 blocked_fpu_registers_[FTMP] = true;
1105
1106 // Reserve suspend and thread registers.
1107 blocked_core_registers_[S0] = true;
1108 blocked_core_registers_[TR] = true;
1109
1110 // Reserve T9 for function calls
1111 blocked_core_registers_[T9] = true;
1112
Goran Jakovljevic782be112016-06-21 12:39:04 +02001113 if (GetGraph()->IsDebuggable()) {
1114 // Stubs do not save callee-save floating point registers. If the graph
1115 // is debuggable, we need to deal with these registers differently. For
1116 // now, just block them.
1117 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1118 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1119 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001120 }
1121}
1122
Alexey Frunze4dda3372015-06-01 18:31:49 -07001123size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1124 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001125 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001126}
1127
1128size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1129 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001130 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131}
1132
1133size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1134 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001135 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001136}
1137
1138size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1139 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001140 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001141}
1142
1143void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001144 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001145}
1146
1147void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001148 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149}
1150
Calin Juravle175dc732015-08-25 15:42:32 +01001151void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001152 HInstruction* instruction,
1153 uint32_t dex_pc,
1154 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001155 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001156 __ LoadFromOffset(kLoadDoubleword,
1157 T9,
1158 TR,
1159 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001160 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001161 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001162 if (EntrypointRequiresStackMap(entrypoint)) {
1163 RecordPcInfo(instruction, dex_pc, slow_path);
1164 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001165}
1166
1167void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1168 GpuRegister class_reg) {
1169 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1170 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1171 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1172 // TODO: barrier needed?
1173 __ Bind(slow_path->GetExitLabel());
1174}
1175
1176void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1177 __ Sync(0); // only stype 0 is supported
1178}
1179
1180void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1181 HBasicBlock* successor) {
1182 SuspendCheckSlowPathMIPS64* slow_path =
1183 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1184 codegen_->AddSlowPath(slow_path);
1185
1186 __ LoadFromOffset(kLoadUnsignedHalfword,
1187 TMP,
1188 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001189 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001190 if (successor == nullptr) {
1191 __ Bnezc(TMP, slow_path->GetEntryLabel());
1192 __ Bind(slow_path->GetReturnLabel());
1193 } else {
1194 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001195 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001196 // slow_path will return to GetLabelOf(successor).
1197 }
1198}
1199
1200InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1201 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001202 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001203 assembler_(codegen->GetAssembler()),
1204 codegen_(codegen) {}
1205
1206void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1207 DCHECK_EQ(instruction->InputCount(), 2U);
1208 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1209 Primitive::Type type = instruction->GetResultType();
1210 switch (type) {
1211 case Primitive::kPrimInt:
1212 case Primitive::kPrimLong: {
1213 locations->SetInAt(0, Location::RequiresRegister());
1214 HInstruction* right = instruction->InputAt(1);
1215 bool can_use_imm = false;
1216 if (right->IsConstant()) {
1217 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1218 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1219 can_use_imm = IsUint<16>(imm);
1220 } else if (instruction->IsAdd()) {
1221 can_use_imm = IsInt<16>(imm);
1222 } else {
1223 DCHECK(instruction->IsSub());
1224 can_use_imm = IsInt<16>(-imm);
1225 }
1226 }
1227 if (can_use_imm)
1228 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1229 else
1230 locations->SetInAt(1, Location::RequiresRegister());
1231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1232 }
1233 break;
1234
1235 case Primitive::kPrimFloat:
1236 case Primitive::kPrimDouble:
1237 locations->SetInAt(0, Location::RequiresFpuRegister());
1238 locations->SetInAt(1, Location::RequiresFpuRegister());
1239 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1240 break;
1241
1242 default:
1243 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1244 }
1245}
1246
1247void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1248 Primitive::Type type = instruction->GetType();
1249 LocationSummary* locations = instruction->GetLocations();
1250
1251 switch (type) {
1252 case Primitive::kPrimInt:
1253 case Primitive::kPrimLong: {
1254 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1255 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1256 Location rhs_location = locations->InAt(1);
1257
1258 GpuRegister rhs_reg = ZERO;
1259 int64_t rhs_imm = 0;
1260 bool use_imm = rhs_location.IsConstant();
1261 if (use_imm) {
1262 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1263 } else {
1264 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1265 }
1266
1267 if (instruction->IsAnd()) {
1268 if (use_imm)
1269 __ Andi(dst, lhs, rhs_imm);
1270 else
1271 __ And(dst, lhs, rhs_reg);
1272 } else if (instruction->IsOr()) {
1273 if (use_imm)
1274 __ Ori(dst, lhs, rhs_imm);
1275 else
1276 __ Or(dst, lhs, rhs_reg);
1277 } else if (instruction->IsXor()) {
1278 if (use_imm)
1279 __ Xori(dst, lhs, rhs_imm);
1280 else
1281 __ Xor(dst, lhs, rhs_reg);
1282 } else if (instruction->IsAdd()) {
1283 if (type == Primitive::kPrimInt) {
1284 if (use_imm)
1285 __ Addiu(dst, lhs, rhs_imm);
1286 else
1287 __ Addu(dst, lhs, rhs_reg);
1288 } else {
1289 if (use_imm)
1290 __ Daddiu(dst, lhs, rhs_imm);
1291 else
1292 __ Daddu(dst, lhs, rhs_reg);
1293 }
1294 } else {
1295 DCHECK(instruction->IsSub());
1296 if (type == Primitive::kPrimInt) {
1297 if (use_imm)
1298 __ Addiu(dst, lhs, -rhs_imm);
1299 else
1300 __ Subu(dst, lhs, rhs_reg);
1301 } else {
1302 if (use_imm)
1303 __ Daddiu(dst, lhs, -rhs_imm);
1304 else
1305 __ Dsubu(dst, lhs, rhs_reg);
1306 }
1307 }
1308 break;
1309 }
1310 case Primitive::kPrimFloat:
1311 case Primitive::kPrimDouble: {
1312 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1313 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1314 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1315 if (instruction->IsAdd()) {
1316 if (type == Primitive::kPrimFloat)
1317 __ AddS(dst, lhs, rhs);
1318 else
1319 __ AddD(dst, lhs, rhs);
1320 } else if (instruction->IsSub()) {
1321 if (type == Primitive::kPrimFloat)
1322 __ SubS(dst, lhs, rhs);
1323 else
1324 __ SubD(dst, lhs, rhs);
1325 } else {
1326 LOG(FATAL) << "Unexpected floating-point binary operation";
1327 }
1328 break;
1329 }
1330 default:
1331 LOG(FATAL) << "Unexpected binary operation type " << type;
1332 }
1333}
1334
1335void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001336 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001337
1338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1339 Primitive::Type type = instr->GetResultType();
1340 switch (type) {
1341 case Primitive::kPrimInt:
1342 case Primitive::kPrimLong: {
1343 locations->SetInAt(0, Location::RequiresRegister());
1344 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346 break;
1347 }
1348 default:
1349 LOG(FATAL) << "Unexpected shift type " << type;
1350 }
1351}
1352
1353void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001354 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 LocationSummary* locations = instr->GetLocations();
1356 Primitive::Type type = instr->GetType();
1357
1358 switch (type) {
1359 case Primitive::kPrimInt:
1360 case Primitive::kPrimLong: {
1361 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1362 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1363 Location rhs_location = locations->InAt(1);
1364
1365 GpuRegister rhs_reg = ZERO;
1366 int64_t rhs_imm = 0;
1367 bool use_imm = rhs_location.IsConstant();
1368 if (use_imm) {
1369 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1370 } else {
1371 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1372 }
1373
1374 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001375 uint32_t shift_value = rhs_imm &
1376 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001377
Alexey Frunze92d90602015-12-18 18:16:36 -08001378 if (shift_value == 0) {
1379 if (dst != lhs) {
1380 __ Move(dst, lhs);
1381 }
1382 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001383 if (instr->IsShl()) {
1384 __ Sll(dst, lhs, shift_value);
1385 } else if (instr->IsShr()) {
1386 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001387 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001388 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001389 } else {
1390 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001391 }
1392 } else {
1393 if (shift_value < 32) {
1394 if (instr->IsShl()) {
1395 __ Dsll(dst, lhs, shift_value);
1396 } else if (instr->IsShr()) {
1397 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001398 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001399 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001400 } else {
1401 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001402 }
1403 } else {
1404 shift_value -= 32;
1405 if (instr->IsShl()) {
1406 __ Dsll32(dst, lhs, shift_value);
1407 } else if (instr->IsShr()) {
1408 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001409 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001410 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001411 } else {
1412 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001413 }
1414 }
1415 }
1416 } else {
1417 if (type == Primitive::kPrimInt) {
1418 if (instr->IsShl()) {
1419 __ Sllv(dst, lhs, rhs_reg);
1420 } else if (instr->IsShr()) {
1421 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001422 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001423 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001424 } else {
1425 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001426 }
1427 } else {
1428 if (instr->IsShl()) {
1429 __ Dsllv(dst, lhs, rhs_reg);
1430 } else if (instr->IsShr()) {
1431 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001432 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001433 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001434 } else {
1435 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001436 }
1437 }
1438 }
1439 break;
1440 }
1441 default:
1442 LOG(FATAL) << "Unexpected shift operation type " << type;
1443 }
1444}
1445
1446void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1447 HandleBinaryOp(instruction);
1448}
1449
1450void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1451 HandleBinaryOp(instruction);
1452}
1453
1454void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1455 HandleBinaryOp(instruction);
1456}
1457
1458void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1459 HandleBinaryOp(instruction);
1460}
1461
1462void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1463 LocationSummary* locations =
1464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1465 locations->SetInAt(0, Location::RequiresRegister());
1466 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1467 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1469 } else {
1470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1471 }
1472}
1473
1474void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1475 LocationSummary* locations = instruction->GetLocations();
1476 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1477 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001478 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001479
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001480 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001481 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1482 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001483 switch (type) {
1484 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001485 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1486 if (index.IsConstant()) {
1487 size_t offset =
1488 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1489 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1490 } else {
1491 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1492 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1493 }
1494 break;
1495 }
1496
1497 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001498 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1499 if (index.IsConstant()) {
1500 size_t offset =
1501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1502 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1503 } else {
1504 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1505 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1506 }
1507 break;
1508 }
1509
1510 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001511 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1512 if (index.IsConstant()) {
1513 size_t offset =
1514 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1515 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1516 } else {
1517 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1518 __ Daddu(TMP, obj, TMP);
1519 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1520 }
1521 break;
1522 }
1523
1524 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001525 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001526 if (maybe_compressed_char_at) {
1527 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1528 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset);
1529 codegen_->MaybeRecordImplicitNullCheck(instruction);
1530 __ Dext(TMP, TMP, 0, 1);
1531 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1532 "Expecting 0=compressed, 1=uncompressed");
1533 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001534 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001535 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1536 if (maybe_compressed_char_at) {
1537 Mips64Label uncompressed_load, done;
1538 __ Bnezc(TMP, &uncompressed_load);
1539 __ LoadFromOffset(kLoadUnsignedByte,
1540 out,
1541 obj,
1542 data_offset + (const_index << TIMES_1));
1543 __ Bc(&done);
1544 __ Bind(&uncompressed_load);
1545 __ LoadFromOffset(kLoadUnsignedHalfword,
1546 out,
1547 obj,
1548 data_offset + (const_index << TIMES_2));
1549 __ Bind(&done);
1550 } else {
1551 __ LoadFromOffset(kLoadUnsignedHalfword,
1552 out,
1553 obj,
1554 data_offset + (const_index << TIMES_2));
1555 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001556 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001557 GpuRegister index_reg = index.AsRegister<GpuRegister>();
1558 if (maybe_compressed_char_at) {
1559 Mips64Label uncompressed_load, done;
1560 __ Bnezc(TMP, &uncompressed_load);
1561 __ Daddu(TMP, obj, index_reg);
1562 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1563 __ Bc(&done);
1564 __ Bind(&uncompressed_load);
1565 __ Dsll(TMP, index_reg, TIMES_2);
1566 __ Daddu(TMP, obj, TMP);
1567 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1568 __ Bind(&done);
1569 } else {
1570 __ Dsll(TMP, index_reg, TIMES_2);
1571 __ Daddu(TMP, obj, TMP);
1572 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1573 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001574 }
1575 break;
1576 }
1577
1578 case Primitive::kPrimInt:
1579 case Primitive::kPrimNot: {
1580 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001581 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1582 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1583 if (index.IsConstant()) {
1584 size_t offset =
1585 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1586 __ LoadFromOffset(load_type, out, obj, offset);
1587 } else {
1588 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1589 __ Daddu(TMP, obj, TMP);
1590 __ LoadFromOffset(load_type, out, TMP, data_offset);
1591 }
1592 break;
1593 }
1594
1595 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001596 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1597 if (index.IsConstant()) {
1598 size_t offset =
1599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1600 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1601 } else {
1602 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1603 __ Daddu(TMP, obj, TMP);
1604 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1605 }
1606 break;
1607 }
1608
1609 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001610 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1611 if (index.IsConstant()) {
1612 size_t offset =
1613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1614 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1615 } else {
1616 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1617 __ Daddu(TMP, obj, TMP);
1618 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1619 }
1620 break;
1621 }
1622
1623 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001624 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1625 if (index.IsConstant()) {
1626 size_t offset =
1627 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1628 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1629 } else {
1630 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1631 __ Daddu(TMP, obj, TMP);
1632 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1633 }
1634 break;
1635 }
1636
1637 case Primitive::kPrimVoid:
1638 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1639 UNREACHABLE();
1640 }
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001641 if (!maybe_compressed_char_at) {
1642 codegen_->MaybeRecordImplicitNullCheck(instruction);
1643 }
Alexey Frunzec061de12017-02-14 13:27:23 -08001644
1645 if (type == Primitive::kPrimNot) {
1646 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1647 __ MaybeUnpoisonHeapReference(out);
1648 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001649}
1650
1651void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1653 locations->SetInAt(0, Location::RequiresRegister());
1654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1655}
1656
1657void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1658 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001659 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001660 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1661 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1662 __ LoadFromOffset(kLoadWord, out, obj, offset);
1663 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001664 // Mask out compression flag from String's array length.
1665 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
1666 __ Srl(out, out, 1u);
1667 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001668}
1669
1670void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001671 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1673 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001674 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001675 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 InvokeRuntimeCallingConvention calling_convention;
1677 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1678 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1679 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1680 } else {
1681 locations->SetInAt(0, Location::RequiresRegister());
1682 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1683 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1684 locations->SetInAt(2, Location::RequiresFpuRegister());
1685 } else {
1686 locations->SetInAt(2, Location::RequiresRegister());
1687 }
1688 }
1689}
1690
1691void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1692 LocationSummary* locations = instruction->GetLocations();
1693 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1694 Location index = locations->InAt(1);
1695 Primitive::Type value_type = instruction->GetComponentType();
1696 bool needs_runtime_call = locations->WillCall();
1697 bool needs_write_barrier =
1698 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1699
1700 switch (value_type) {
1701 case Primitive::kPrimBoolean:
1702 case Primitive::kPrimByte: {
1703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1704 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1705 if (index.IsConstant()) {
1706 size_t offset =
1707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1708 __ StoreToOffset(kStoreByte, value, obj, offset);
1709 } else {
1710 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1711 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1712 }
1713 break;
1714 }
1715
1716 case Primitive::kPrimShort:
1717 case Primitive::kPrimChar: {
1718 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1719 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1720 if (index.IsConstant()) {
1721 size_t offset =
1722 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1723 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1724 } else {
1725 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1726 __ Daddu(TMP, obj, TMP);
1727 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1728 }
1729 break;
1730 }
1731
1732 case Primitive::kPrimInt:
1733 case Primitive::kPrimNot: {
1734 if (!needs_runtime_call) {
1735 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08001736 GpuRegister base_reg;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001737 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1738 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001739 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
1740 base_reg = obj;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741 } else {
1742 DCHECK(index.IsRegister()) << index;
1743 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1744 __ Daddu(TMP, obj, TMP);
Alexey Frunzec061de12017-02-14 13:27:23 -08001745 base_reg = TMP;
1746 }
1747 if (kPoisonHeapReferences && needs_write_barrier) {
1748 // Note that in the case where `value` is a null reference,
1749 // we do not enter this block, as a null reference does not
1750 // need poisoning.
1751 DCHECK_EQ(value_type, Primitive::kPrimNot);
1752 // Use Sw() instead of StoreToOffset() in order to be able to
1753 // hold the poisoned reference in AT and thus avoid allocating
1754 // yet another temporary register.
1755 if (index.IsConstant()) {
1756 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
1757 int16_t low16 = Low16Bits(data_offset);
1758 // For consistency with StoreToOffset() and such treat data_offset as int32_t.
1759 uint64_t high48 = static_cast<uint64_t>(static_cast<int32_t>(data_offset)) - low16;
1760 int16_t upper16 = High16Bits(high48);
1761 // Allow the full [-2GB,+2GB) range in case `low16` is negative and needs a
1762 // compensatory 64KB added, which may push `high48` above 2GB and require
1763 // the dahi instruction.
1764 int16_t higher16 = High32Bits(high48) + ((upper16 < 0) ? 1 : 0);
1765 __ Daui(TMP, obj, upper16);
1766 if (higher16 != 0) {
1767 __ Dahi(TMP, higher16);
1768 }
1769 base_reg = TMP;
1770 data_offset = low16;
1771 }
1772 } else {
1773 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
1774 }
1775 __ PoisonHeapReference(AT, value);
1776 __ Sw(AT, base_reg, data_offset);
1777 } else {
1778 __ StoreToOffset(kStoreWord, value, base_reg, data_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 }
1780 codegen_->MaybeRecordImplicitNullCheck(instruction);
1781 if (needs_write_barrier) {
1782 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001783 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 }
1785 } else {
1786 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08001787 // Note: if heap poisoning is enabled, pAputObject takes care
1788 // of poisoning the reference.
Serban Constantinescufc734082016-07-19 17:18:07 +01001789 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001790 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791 }
1792 break;
1793 }
1794
1795 case Primitive::kPrimLong: {
1796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1797 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1798 if (index.IsConstant()) {
1799 size_t offset =
1800 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1801 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1802 } else {
1803 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1804 __ Daddu(TMP, obj, TMP);
1805 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1806 }
1807 break;
1808 }
1809
1810 case Primitive::kPrimFloat: {
1811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1812 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1813 DCHECK(locations->InAt(2).IsFpuRegister());
1814 if (index.IsConstant()) {
1815 size_t offset =
1816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1817 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1818 } else {
1819 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1820 __ Daddu(TMP, obj, TMP);
1821 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1822 }
1823 break;
1824 }
1825
1826 case Primitive::kPrimDouble: {
1827 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1828 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1829 DCHECK(locations->InAt(2).IsFpuRegister());
1830 if (index.IsConstant()) {
1831 size_t offset =
1832 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1833 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1834 } else {
1835 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1836 __ Daddu(TMP, obj, TMP);
1837 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1838 }
1839 break;
1840 }
1841
1842 case Primitive::kPrimVoid:
1843 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1844 UNREACHABLE();
1845 }
1846
1847 // Ints and objects are handled in the switch.
1848 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1849 codegen_->MaybeRecordImplicitNullCheck(instruction);
1850 }
1851}
1852
1853void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001854 RegisterSet caller_saves = RegisterSet::Empty();
1855 InvokeRuntimeCallingConvention calling_convention;
1856 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1857 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1858 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001859 locations->SetInAt(0, Location::RequiresRegister());
1860 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861}
1862
1863void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1864 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001865 BoundsCheckSlowPathMIPS64* slow_path =
1866 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001867 codegen_->AddSlowPath(slow_path);
1868
1869 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1870 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1871
1872 // length is limited by the maximum positive signed 32-bit integer.
1873 // Unsigned comparison of length and index checks for index < 0
1874 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001875 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001876}
1877
1878void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1880 instruction,
1881 LocationSummary::kCallOnSlowPath);
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001884 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001885 locations->AddTemp(Location::RequiresRegister());
1886}
1887
1888void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1889 LocationSummary* locations = instruction->GetLocations();
1890 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1891 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1892 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1893
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001894 SlowPathCodeMIPS64* slow_path =
1895 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001896 codegen_->AddSlowPath(slow_path);
1897
1898 // TODO: avoid this check if we know obj is not null.
1899 __ Beqzc(obj, slow_path->GetExitLabel());
1900 // Compare the class of `obj` with `cls`.
1901 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08001902 __ MaybeUnpoisonHeapReference(obj_cls);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1904 __ Bind(slow_path->GetExitLabel());
1905}
1906
1907void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1908 LocationSummary* locations =
1909 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1910 locations->SetInAt(0, Location::RequiresRegister());
1911 if (check->HasUses()) {
1912 locations->SetOut(Location::SameAsFirstInput());
1913 }
1914}
1915
1916void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1917 // We assume the class is not null.
1918 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1919 check->GetLoadClass(),
1920 check,
1921 check->GetDexPc(),
1922 true);
1923 codegen_->AddSlowPath(slow_path);
1924 GenerateClassInitializationCheck(slow_path,
1925 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1926}
1927
1928void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1929 Primitive::Type in_type = compare->InputAt(0)->GetType();
1930
Alexey Frunze299a9392015-12-08 16:08:02 -08001931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001932
1933 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001934 case Primitive::kPrimBoolean:
1935 case Primitive::kPrimByte:
1936 case Primitive::kPrimShort:
1937 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001938 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001939 case Primitive::kPrimLong:
1940 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001941 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1943 break;
1944
1945 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001946 case Primitive::kPrimDouble:
1947 locations->SetInAt(0, Location::RequiresFpuRegister());
1948 locations->SetInAt(1, Location::RequiresFpuRegister());
1949 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001950 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001951
1952 default:
1953 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1954 }
1955}
1956
1957void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1958 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001959 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001960 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1961
1962 // 0 if: left == right
1963 // 1 if: left > right
1964 // -1 if: left < right
1965 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001966 case Primitive::kPrimBoolean:
1967 case Primitive::kPrimByte:
1968 case Primitive::kPrimShort:
1969 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001970 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001971 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001972 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001973 Location rhs_location = locations->InAt(1);
1974 bool use_imm = rhs_location.IsConstant();
1975 GpuRegister rhs = ZERO;
1976 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001977 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001978 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1979 if (value != 0) {
1980 rhs = AT;
1981 __ LoadConst64(rhs, value);
1982 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001983 } else {
1984 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1985 if (value != 0) {
1986 rhs = AT;
1987 __ LoadConst32(rhs, value);
1988 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001989 }
1990 } else {
1991 rhs = rhs_location.AsRegister<GpuRegister>();
1992 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001993 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001994 __ Slt(res, rhs, lhs);
1995 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001996 break;
1997 }
1998
Alexey Frunze299a9392015-12-08 16:08:02 -08001999 case Primitive::kPrimFloat: {
2000 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2001 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2002 Mips64Label done;
2003 __ CmpEqS(FTMP, lhs, rhs);
2004 __ LoadConst32(res, 0);
2005 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002006 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002007 __ CmpLtS(FTMP, lhs, rhs);
2008 __ LoadConst32(res, -1);
2009 __ Bc1nez(FTMP, &done);
2010 __ LoadConst32(res, 1);
2011 } else {
2012 __ CmpLtS(FTMP, rhs, lhs);
2013 __ LoadConst32(res, 1);
2014 __ Bc1nez(FTMP, &done);
2015 __ LoadConst32(res, -1);
2016 }
2017 __ Bind(&done);
2018 break;
2019 }
2020
Alexey Frunze4dda3372015-06-01 18:31:49 -07002021 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002022 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2023 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2024 Mips64Label done;
2025 __ CmpEqD(FTMP, lhs, rhs);
2026 __ LoadConst32(res, 0);
2027 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002028 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002029 __ CmpLtD(FTMP, lhs, rhs);
2030 __ LoadConst32(res, -1);
2031 __ Bc1nez(FTMP, &done);
2032 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002033 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002034 __ CmpLtD(FTMP, rhs, lhs);
2035 __ LoadConst32(res, 1);
2036 __ Bc1nez(FTMP, &done);
2037 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002038 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002039 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002040 break;
2041 }
2042
2043 default:
2044 LOG(FATAL) << "Unimplemented compare type " << in_type;
2045 }
2046}
2047
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002049 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002050 switch (instruction->InputAt(0)->GetType()) {
2051 default:
2052 case Primitive::kPrimLong:
2053 locations->SetInAt(0, Location::RequiresRegister());
2054 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2055 break;
2056
2057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble:
2059 locations->SetInAt(0, Location::RequiresFpuRegister());
2060 locations->SetInAt(1, Location::RequiresFpuRegister());
2061 break;
2062 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002063 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2065 }
2066}
2067
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002068void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002069 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002070 return;
2071 }
2072
Alexey Frunze299a9392015-12-08 16:08:02 -08002073 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002074 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002075 switch (type) {
2076 default:
2077 // Integer case.
2078 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2079 return;
2080 case Primitive::kPrimLong:
2081 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2082 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002083 case Primitive::kPrimFloat:
2084 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002085 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2086 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002087 }
2088}
2089
Alexey Frunzec857c742015-09-23 15:12:39 -07002090void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2091 DCHECK(instruction->IsDiv() || instruction->IsRem());
2092 Primitive::Type type = instruction->GetResultType();
2093
2094 LocationSummary* locations = instruction->GetLocations();
2095 Location second = locations->InAt(1);
2096 DCHECK(second.IsConstant());
2097
2098 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2099 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2100 int64_t imm = Int64FromConstant(second.GetConstant());
2101 DCHECK(imm == 1 || imm == -1);
2102
2103 if (instruction->IsRem()) {
2104 __ Move(out, ZERO);
2105 } else {
2106 if (imm == -1) {
2107 if (type == Primitive::kPrimInt) {
2108 __ Subu(out, ZERO, dividend);
2109 } else {
2110 DCHECK_EQ(type, Primitive::kPrimLong);
2111 __ Dsubu(out, ZERO, dividend);
2112 }
2113 } else if (out != dividend) {
2114 __ Move(out, dividend);
2115 }
2116 }
2117}
2118
2119void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2120 DCHECK(instruction->IsDiv() || instruction->IsRem());
2121 Primitive::Type type = instruction->GetResultType();
2122
2123 LocationSummary* locations = instruction->GetLocations();
2124 Location second = locations->InAt(1);
2125 DCHECK(second.IsConstant());
2126
2127 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2128 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2129 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002130 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002131 int ctz_imm = CTZ(abs_imm);
2132
2133 if (instruction->IsDiv()) {
2134 if (type == Primitive::kPrimInt) {
2135 if (ctz_imm == 1) {
2136 // Fast path for division by +/-2, which is very common.
2137 __ Srl(TMP, dividend, 31);
2138 } else {
2139 __ Sra(TMP, dividend, 31);
2140 __ Srl(TMP, TMP, 32 - ctz_imm);
2141 }
2142 __ Addu(out, dividend, TMP);
2143 __ Sra(out, out, ctz_imm);
2144 if (imm < 0) {
2145 __ Subu(out, ZERO, out);
2146 }
2147 } else {
2148 DCHECK_EQ(type, Primitive::kPrimLong);
2149 if (ctz_imm == 1) {
2150 // Fast path for division by +/-2, which is very common.
2151 __ Dsrl32(TMP, dividend, 31);
2152 } else {
2153 __ Dsra32(TMP, dividend, 31);
2154 if (ctz_imm > 32) {
2155 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2156 } else {
2157 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2158 }
2159 }
2160 __ Daddu(out, dividend, TMP);
2161 if (ctz_imm < 32) {
2162 __ Dsra(out, out, ctz_imm);
2163 } else {
2164 __ Dsra32(out, out, ctz_imm - 32);
2165 }
2166 if (imm < 0) {
2167 __ Dsubu(out, ZERO, out);
2168 }
2169 }
2170 } else {
2171 if (type == Primitive::kPrimInt) {
2172 if (ctz_imm == 1) {
2173 // Fast path for modulo +/-2, which is very common.
2174 __ Sra(TMP, dividend, 31);
2175 __ Subu(out, dividend, TMP);
2176 __ Andi(out, out, 1);
2177 __ Addu(out, out, TMP);
2178 } else {
2179 __ Sra(TMP, dividend, 31);
2180 __ Srl(TMP, TMP, 32 - ctz_imm);
2181 __ Addu(out, dividend, TMP);
2182 if (IsUint<16>(abs_imm - 1)) {
2183 __ Andi(out, out, abs_imm - 1);
2184 } else {
2185 __ Sll(out, out, 32 - ctz_imm);
2186 __ Srl(out, out, 32 - ctz_imm);
2187 }
2188 __ Subu(out, out, TMP);
2189 }
2190 } else {
2191 DCHECK_EQ(type, Primitive::kPrimLong);
2192 if (ctz_imm == 1) {
2193 // Fast path for modulo +/-2, which is very common.
2194 __ Dsra32(TMP, dividend, 31);
2195 __ Dsubu(out, dividend, TMP);
2196 __ Andi(out, out, 1);
2197 __ Daddu(out, out, TMP);
2198 } else {
2199 __ Dsra32(TMP, dividend, 31);
2200 if (ctz_imm > 32) {
2201 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2202 } else {
2203 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2204 }
2205 __ Daddu(out, dividend, TMP);
2206 if (IsUint<16>(abs_imm - 1)) {
2207 __ Andi(out, out, abs_imm - 1);
2208 } else {
2209 if (ctz_imm > 32) {
2210 __ Dsll(out, out, 64 - ctz_imm);
2211 __ Dsrl(out, out, 64 - ctz_imm);
2212 } else {
2213 __ Dsll32(out, out, 32 - ctz_imm);
2214 __ Dsrl32(out, out, 32 - ctz_imm);
2215 }
2216 }
2217 __ Dsubu(out, out, TMP);
2218 }
2219 }
2220 }
2221}
2222
2223void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2224 DCHECK(instruction->IsDiv() || instruction->IsRem());
2225
2226 LocationSummary* locations = instruction->GetLocations();
2227 Location second = locations->InAt(1);
2228 DCHECK(second.IsConstant());
2229
2230 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2231 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2232 int64_t imm = Int64FromConstant(second.GetConstant());
2233
2234 Primitive::Type type = instruction->GetResultType();
2235 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2236
2237 int64_t magic;
2238 int shift;
2239 CalculateMagicAndShiftForDivRem(imm,
2240 (type == Primitive::kPrimLong),
2241 &magic,
2242 &shift);
2243
2244 if (type == Primitive::kPrimInt) {
2245 __ LoadConst32(TMP, magic);
2246 __ MuhR6(TMP, dividend, TMP);
2247
2248 if (imm > 0 && magic < 0) {
2249 __ Addu(TMP, TMP, dividend);
2250 } else if (imm < 0 && magic > 0) {
2251 __ Subu(TMP, TMP, dividend);
2252 }
2253
2254 if (shift != 0) {
2255 __ Sra(TMP, TMP, shift);
2256 }
2257
2258 if (instruction->IsDiv()) {
2259 __ Sra(out, TMP, 31);
2260 __ Subu(out, TMP, out);
2261 } else {
2262 __ Sra(AT, TMP, 31);
2263 __ Subu(AT, TMP, AT);
2264 __ LoadConst32(TMP, imm);
2265 __ MulR6(TMP, AT, TMP);
2266 __ Subu(out, dividend, TMP);
2267 }
2268 } else {
2269 __ LoadConst64(TMP, magic);
2270 __ Dmuh(TMP, dividend, TMP);
2271
2272 if (imm > 0 && magic < 0) {
2273 __ Daddu(TMP, TMP, dividend);
2274 } else if (imm < 0 && magic > 0) {
2275 __ Dsubu(TMP, TMP, dividend);
2276 }
2277
2278 if (shift >= 32) {
2279 __ Dsra32(TMP, TMP, shift - 32);
2280 } else if (shift > 0) {
2281 __ Dsra(TMP, TMP, shift);
2282 }
2283
2284 if (instruction->IsDiv()) {
2285 __ Dsra32(out, TMP, 31);
2286 __ Dsubu(out, TMP, out);
2287 } else {
2288 __ Dsra32(AT, TMP, 31);
2289 __ Dsubu(AT, TMP, AT);
2290 __ LoadConst64(TMP, imm);
2291 __ Dmul(TMP, AT, TMP);
2292 __ Dsubu(out, dividend, TMP);
2293 }
2294 }
2295}
2296
2297void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2298 DCHECK(instruction->IsDiv() || instruction->IsRem());
2299 Primitive::Type type = instruction->GetResultType();
2300 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2301
2302 LocationSummary* locations = instruction->GetLocations();
2303 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2304 Location second = locations->InAt(1);
2305
2306 if (second.IsConstant()) {
2307 int64_t imm = Int64FromConstant(second.GetConstant());
2308 if (imm == 0) {
2309 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2310 } else if (imm == 1 || imm == -1) {
2311 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002312 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002313 DivRemByPowerOfTwo(instruction);
2314 } else {
2315 DCHECK(imm <= -2 || imm >= 2);
2316 GenerateDivRemWithAnyConstant(instruction);
2317 }
2318 } else {
2319 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2320 GpuRegister divisor = second.AsRegister<GpuRegister>();
2321 if (instruction->IsDiv()) {
2322 if (type == Primitive::kPrimInt)
2323 __ DivR6(out, dividend, divisor);
2324 else
2325 __ Ddiv(out, dividend, divisor);
2326 } else {
2327 if (type == Primitive::kPrimInt)
2328 __ ModR6(out, dividend, divisor);
2329 else
2330 __ Dmod(out, dividend, divisor);
2331 }
2332 }
2333}
2334
Alexey Frunze4dda3372015-06-01 18:31:49 -07002335void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2336 LocationSummary* locations =
2337 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2338 switch (div->GetResultType()) {
2339 case Primitive::kPrimInt:
2340 case Primitive::kPrimLong:
2341 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002342 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2344 break;
2345
2346 case Primitive::kPrimFloat:
2347 case Primitive::kPrimDouble:
2348 locations->SetInAt(0, Location::RequiresFpuRegister());
2349 locations->SetInAt(1, Location::RequiresFpuRegister());
2350 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2351 break;
2352
2353 default:
2354 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2355 }
2356}
2357
2358void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2359 Primitive::Type type = instruction->GetType();
2360 LocationSummary* locations = instruction->GetLocations();
2361
2362 switch (type) {
2363 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002364 case Primitive::kPrimLong:
2365 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002366 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002367 case Primitive::kPrimFloat:
2368 case Primitive::kPrimDouble: {
2369 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2370 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2371 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2372 if (type == Primitive::kPrimFloat)
2373 __ DivS(dst, lhs, rhs);
2374 else
2375 __ DivD(dst, lhs, rhs);
2376 break;
2377 }
2378 default:
2379 LOG(FATAL) << "Unexpected div type " << type;
2380 }
2381}
2382
2383void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002384 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002385 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002386}
2387
2388void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2389 SlowPathCodeMIPS64* slow_path =
2390 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2391 codegen_->AddSlowPath(slow_path);
2392 Location value = instruction->GetLocations()->InAt(0);
2393
2394 Primitive::Type type = instruction->GetType();
2395
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002396 if (!Primitive::IsIntegralType(type)) {
2397 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002398 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002399 }
2400
2401 if (value.IsConstant()) {
2402 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2403 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002404 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002405 } else {
2406 // A division by a non-null constant is valid. We don't need to perform
2407 // any check, so simply fall through.
2408 }
2409 } else {
2410 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2411 }
2412}
2413
2414void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2415 LocationSummary* locations =
2416 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2417 locations->SetOut(Location::ConstantLocation(constant));
2418}
2419
2420void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2421 // Will be generated at use site.
2422}
2423
2424void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2425 exit->SetLocations(nullptr);
2426}
2427
2428void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2429}
2430
2431void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2432 LocationSummary* locations =
2433 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2434 locations->SetOut(Location::ConstantLocation(constant));
2435}
2436
2437void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2438 // Will be generated at use site.
2439}
2440
David Brazdilfc6a86a2015-06-26 10:33:45 +00002441void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002442 DCHECK(!successor->IsExitBlock());
2443 HBasicBlock* block = got->GetBlock();
2444 HInstruction* previous = got->GetPrevious();
2445 HLoopInformation* info = block->GetLoopInformation();
2446
2447 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2448 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2449 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2450 return;
2451 }
2452 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2453 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2454 }
2455 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002456 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002457 }
2458}
2459
David Brazdilfc6a86a2015-06-26 10:33:45 +00002460void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2461 got->SetLocations(nullptr);
2462}
2463
2464void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2465 HandleGoto(got, got->GetSuccessor());
2466}
2467
2468void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2469 try_boundary->SetLocations(nullptr);
2470}
2471
2472void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2473 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2474 if (!successor->IsExitBlock()) {
2475 HandleGoto(try_boundary, successor);
2476 }
2477}
2478
Alexey Frunze299a9392015-12-08 16:08:02 -08002479void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2480 bool is64bit,
2481 LocationSummary* locations) {
2482 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2483 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2484 Location rhs_location = locations->InAt(1);
2485 GpuRegister rhs_reg = ZERO;
2486 int64_t rhs_imm = 0;
2487 bool use_imm = rhs_location.IsConstant();
2488 if (use_imm) {
2489 if (is64bit) {
2490 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2491 } else {
2492 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2493 }
2494 } else {
2495 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2496 }
2497 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2498
2499 switch (cond) {
2500 case kCondEQ:
2501 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002502 if (use_imm && IsInt<16>(-rhs_imm)) {
2503 if (rhs_imm == 0) {
2504 if (cond == kCondEQ) {
2505 __ Sltiu(dst, lhs, 1);
2506 } else {
2507 __ Sltu(dst, ZERO, lhs);
2508 }
2509 } else {
2510 if (is64bit) {
2511 __ Daddiu(dst, lhs, -rhs_imm);
2512 } else {
2513 __ Addiu(dst, lhs, -rhs_imm);
2514 }
2515 if (cond == kCondEQ) {
2516 __ Sltiu(dst, dst, 1);
2517 } else {
2518 __ Sltu(dst, ZERO, dst);
2519 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002520 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002521 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002522 if (use_imm && IsUint<16>(rhs_imm)) {
2523 __ Xori(dst, lhs, rhs_imm);
2524 } else {
2525 if (use_imm) {
2526 rhs_reg = TMP;
2527 __ LoadConst64(rhs_reg, rhs_imm);
2528 }
2529 __ Xor(dst, lhs, rhs_reg);
2530 }
2531 if (cond == kCondEQ) {
2532 __ Sltiu(dst, dst, 1);
2533 } else {
2534 __ Sltu(dst, ZERO, dst);
2535 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002536 }
2537 break;
2538
2539 case kCondLT:
2540 case kCondGE:
2541 if (use_imm && IsInt<16>(rhs_imm)) {
2542 __ Slti(dst, lhs, rhs_imm);
2543 } else {
2544 if (use_imm) {
2545 rhs_reg = TMP;
2546 __ LoadConst64(rhs_reg, rhs_imm);
2547 }
2548 __ Slt(dst, lhs, rhs_reg);
2549 }
2550 if (cond == kCondGE) {
2551 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2552 // only the slt instruction but no sge.
2553 __ Xori(dst, dst, 1);
2554 }
2555 break;
2556
2557 case kCondLE:
2558 case kCondGT:
2559 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2560 // Simulate lhs <= rhs via lhs < rhs + 1.
2561 __ Slti(dst, lhs, rhs_imm_plus_one);
2562 if (cond == kCondGT) {
2563 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2564 // only the slti instruction but no sgti.
2565 __ Xori(dst, dst, 1);
2566 }
2567 } else {
2568 if (use_imm) {
2569 rhs_reg = TMP;
2570 __ LoadConst64(rhs_reg, rhs_imm);
2571 }
2572 __ Slt(dst, rhs_reg, lhs);
2573 if (cond == kCondLE) {
2574 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2575 // only the slt instruction but no sle.
2576 __ Xori(dst, dst, 1);
2577 }
2578 }
2579 break;
2580
2581 case kCondB:
2582 case kCondAE:
2583 if (use_imm && IsInt<16>(rhs_imm)) {
2584 // Sltiu sign-extends its 16-bit immediate operand before
2585 // the comparison and thus lets us compare directly with
2586 // unsigned values in the ranges [0, 0x7fff] and
2587 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2588 __ Sltiu(dst, lhs, rhs_imm);
2589 } else {
2590 if (use_imm) {
2591 rhs_reg = TMP;
2592 __ LoadConst64(rhs_reg, rhs_imm);
2593 }
2594 __ Sltu(dst, lhs, rhs_reg);
2595 }
2596 if (cond == kCondAE) {
2597 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2598 // only the sltu instruction but no sgeu.
2599 __ Xori(dst, dst, 1);
2600 }
2601 break;
2602
2603 case kCondBE:
2604 case kCondA:
2605 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2606 // Simulate lhs <= rhs via lhs < rhs + 1.
2607 // Note that this only works if rhs + 1 does not overflow
2608 // to 0, hence the check above.
2609 // Sltiu sign-extends its 16-bit immediate operand before
2610 // the comparison and thus lets us compare directly with
2611 // unsigned values in the ranges [0, 0x7fff] and
2612 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2613 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2614 if (cond == kCondA) {
2615 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2616 // only the sltiu instruction but no sgtiu.
2617 __ Xori(dst, dst, 1);
2618 }
2619 } else {
2620 if (use_imm) {
2621 rhs_reg = TMP;
2622 __ LoadConst64(rhs_reg, rhs_imm);
2623 }
2624 __ Sltu(dst, rhs_reg, lhs);
2625 if (cond == kCondBE) {
2626 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2627 // only the sltu instruction but no sleu.
2628 __ Xori(dst, dst, 1);
2629 }
2630 }
2631 break;
2632 }
2633}
2634
2635void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2636 bool is64bit,
2637 LocationSummary* locations,
2638 Mips64Label* label) {
2639 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2640 Location rhs_location = locations->InAt(1);
2641 GpuRegister rhs_reg = ZERO;
2642 int64_t rhs_imm = 0;
2643 bool use_imm = rhs_location.IsConstant();
2644 if (use_imm) {
2645 if (is64bit) {
2646 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2647 } else {
2648 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2649 }
2650 } else {
2651 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2652 }
2653
2654 if (use_imm && rhs_imm == 0) {
2655 switch (cond) {
2656 case kCondEQ:
2657 case kCondBE: // <= 0 if zero
2658 __ Beqzc(lhs, label);
2659 break;
2660 case kCondNE:
2661 case kCondA: // > 0 if non-zero
2662 __ Bnezc(lhs, label);
2663 break;
2664 case kCondLT:
2665 __ Bltzc(lhs, label);
2666 break;
2667 case kCondGE:
2668 __ Bgezc(lhs, label);
2669 break;
2670 case kCondLE:
2671 __ Blezc(lhs, label);
2672 break;
2673 case kCondGT:
2674 __ Bgtzc(lhs, label);
2675 break;
2676 case kCondB: // always false
2677 break;
2678 case kCondAE: // always true
2679 __ Bc(label);
2680 break;
2681 }
2682 } else {
2683 if (use_imm) {
2684 rhs_reg = TMP;
2685 __ LoadConst64(rhs_reg, rhs_imm);
2686 }
2687 switch (cond) {
2688 case kCondEQ:
2689 __ Beqc(lhs, rhs_reg, label);
2690 break;
2691 case kCondNE:
2692 __ Bnec(lhs, rhs_reg, label);
2693 break;
2694 case kCondLT:
2695 __ Bltc(lhs, rhs_reg, label);
2696 break;
2697 case kCondGE:
2698 __ Bgec(lhs, rhs_reg, label);
2699 break;
2700 case kCondLE:
2701 __ Bgec(rhs_reg, lhs, label);
2702 break;
2703 case kCondGT:
2704 __ Bltc(rhs_reg, lhs, label);
2705 break;
2706 case kCondB:
2707 __ Bltuc(lhs, rhs_reg, label);
2708 break;
2709 case kCondAE:
2710 __ Bgeuc(lhs, rhs_reg, label);
2711 break;
2712 case kCondBE:
2713 __ Bgeuc(rhs_reg, lhs, label);
2714 break;
2715 case kCondA:
2716 __ Bltuc(rhs_reg, lhs, label);
2717 break;
2718 }
2719 }
2720}
2721
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002722void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2723 bool gt_bias,
2724 Primitive::Type type,
2725 LocationSummary* locations) {
2726 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2727 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2728 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2729 if (type == Primitive::kPrimFloat) {
2730 switch (cond) {
2731 case kCondEQ:
2732 __ CmpEqS(FTMP, lhs, rhs);
2733 __ Mfc1(dst, FTMP);
2734 __ Andi(dst, dst, 1);
2735 break;
2736 case kCondNE:
2737 __ CmpEqS(FTMP, lhs, rhs);
2738 __ Mfc1(dst, FTMP);
2739 __ Addiu(dst, dst, 1);
2740 break;
2741 case kCondLT:
2742 if (gt_bias) {
2743 __ CmpLtS(FTMP, lhs, rhs);
2744 } else {
2745 __ CmpUltS(FTMP, lhs, rhs);
2746 }
2747 __ Mfc1(dst, FTMP);
2748 __ Andi(dst, dst, 1);
2749 break;
2750 case kCondLE:
2751 if (gt_bias) {
2752 __ CmpLeS(FTMP, lhs, rhs);
2753 } else {
2754 __ CmpUleS(FTMP, lhs, rhs);
2755 }
2756 __ Mfc1(dst, FTMP);
2757 __ Andi(dst, dst, 1);
2758 break;
2759 case kCondGT:
2760 if (gt_bias) {
2761 __ CmpUltS(FTMP, rhs, lhs);
2762 } else {
2763 __ CmpLtS(FTMP, rhs, lhs);
2764 }
2765 __ Mfc1(dst, FTMP);
2766 __ Andi(dst, dst, 1);
2767 break;
2768 case kCondGE:
2769 if (gt_bias) {
2770 __ CmpUleS(FTMP, rhs, lhs);
2771 } else {
2772 __ CmpLeS(FTMP, rhs, lhs);
2773 }
2774 __ Mfc1(dst, FTMP);
2775 __ Andi(dst, dst, 1);
2776 break;
2777 default:
2778 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2779 UNREACHABLE();
2780 }
2781 } else {
2782 DCHECK_EQ(type, Primitive::kPrimDouble);
2783 switch (cond) {
2784 case kCondEQ:
2785 __ CmpEqD(FTMP, lhs, rhs);
2786 __ Mfc1(dst, FTMP);
2787 __ Andi(dst, dst, 1);
2788 break;
2789 case kCondNE:
2790 __ CmpEqD(FTMP, lhs, rhs);
2791 __ Mfc1(dst, FTMP);
2792 __ Addiu(dst, dst, 1);
2793 break;
2794 case kCondLT:
2795 if (gt_bias) {
2796 __ CmpLtD(FTMP, lhs, rhs);
2797 } else {
2798 __ CmpUltD(FTMP, lhs, rhs);
2799 }
2800 __ Mfc1(dst, FTMP);
2801 __ Andi(dst, dst, 1);
2802 break;
2803 case kCondLE:
2804 if (gt_bias) {
2805 __ CmpLeD(FTMP, lhs, rhs);
2806 } else {
2807 __ CmpUleD(FTMP, lhs, rhs);
2808 }
2809 __ Mfc1(dst, FTMP);
2810 __ Andi(dst, dst, 1);
2811 break;
2812 case kCondGT:
2813 if (gt_bias) {
2814 __ CmpUltD(FTMP, rhs, lhs);
2815 } else {
2816 __ CmpLtD(FTMP, rhs, lhs);
2817 }
2818 __ Mfc1(dst, FTMP);
2819 __ Andi(dst, dst, 1);
2820 break;
2821 case kCondGE:
2822 if (gt_bias) {
2823 __ CmpUleD(FTMP, rhs, lhs);
2824 } else {
2825 __ CmpLeD(FTMP, rhs, lhs);
2826 }
2827 __ Mfc1(dst, FTMP);
2828 __ Andi(dst, dst, 1);
2829 break;
2830 default:
2831 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2832 UNREACHABLE();
2833 }
2834 }
2835}
2836
Alexey Frunze299a9392015-12-08 16:08:02 -08002837void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2838 bool gt_bias,
2839 Primitive::Type type,
2840 LocationSummary* locations,
2841 Mips64Label* label) {
2842 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2843 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2844 if (type == Primitive::kPrimFloat) {
2845 switch (cond) {
2846 case kCondEQ:
2847 __ CmpEqS(FTMP, lhs, rhs);
2848 __ Bc1nez(FTMP, label);
2849 break;
2850 case kCondNE:
2851 __ CmpEqS(FTMP, lhs, rhs);
2852 __ Bc1eqz(FTMP, label);
2853 break;
2854 case kCondLT:
2855 if (gt_bias) {
2856 __ CmpLtS(FTMP, lhs, rhs);
2857 } else {
2858 __ CmpUltS(FTMP, lhs, rhs);
2859 }
2860 __ Bc1nez(FTMP, label);
2861 break;
2862 case kCondLE:
2863 if (gt_bias) {
2864 __ CmpLeS(FTMP, lhs, rhs);
2865 } else {
2866 __ CmpUleS(FTMP, lhs, rhs);
2867 }
2868 __ Bc1nez(FTMP, label);
2869 break;
2870 case kCondGT:
2871 if (gt_bias) {
2872 __ CmpUltS(FTMP, rhs, lhs);
2873 } else {
2874 __ CmpLtS(FTMP, rhs, lhs);
2875 }
2876 __ Bc1nez(FTMP, label);
2877 break;
2878 case kCondGE:
2879 if (gt_bias) {
2880 __ CmpUleS(FTMP, rhs, lhs);
2881 } else {
2882 __ CmpLeS(FTMP, rhs, lhs);
2883 }
2884 __ Bc1nez(FTMP, label);
2885 break;
2886 default:
2887 LOG(FATAL) << "Unexpected non-floating-point condition";
2888 }
2889 } else {
2890 DCHECK_EQ(type, Primitive::kPrimDouble);
2891 switch (cond) {
2892 case kCondEQ:
2893 __ CmpEqD(FTMP, lhs, rhs);
2894 __ Bc1nez(FTMP, label);
2895 break;
2896 case kCondNE:
2897 __ CmpEqD(FTMP, lhs, rhs);
2898 __ Bc1eqz(FTMP, label);
2899 break;
2900 case kCondLT:
2901 if (gt_bias) {
2902 __ CmpLtD(FTMP, lhs, rhs);
2903 } else {
2904 __ CmpUltD(FTMP, lhs, rhs);
2905 }
2906 __ Bc1nez(FTMP, label);
2907 break;
2908 case kCondLE:
2909 if (gt_bias) {
2910 __ CmpLeD(FTMP, lhs, rhs);
2911 } else {
2912 __ CmpUleD(FTMP, lhs, rhs);
2913 }
2914 __ Bc1nez(FTMP, label);
2915 break;
2916 case kCondGT:
2917 if (gt_bias) {
2918 __ CmpUltD(FTMP, rhs, lhs);
2919 } else {
2920 __ CmpLtD(FTMP, rhs, lhs);
2921 }
2922 __ Bc1nez(FTMP, label);
2923 break;
2924 case kCondGE:
2925 if (gt_bias) {
2926 __ CmpUleD(FTMP, rhs, lhs);
2927 } else {
2928 __ CmpLeD(FTMP, rhs, lhs);
2929 }
2930 __ Bc1nez(FTMP, label);
2931 break;
2932 default:
2933 LOG(FATAL) << "Unexpected non-floating-point condition";
2934 }
2935 }
2936}
2937
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002939 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002940 Mips64Label* true_target,
2941 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002942 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002943
David Brazdil0debae72015-11-12 18:37:00 +00002944 if (true_target == nullptr && false_target == nullptr) {
2945 // Nothing to do. The code always falls through.
2946 return;
2947 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002948 // Constant condition, statically compared against "true" (integer value 1).
2949 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002950 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002951 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002953 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002954 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002955 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002956 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002957 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002958 }
David Brazdil0debae72015-11-12 18:37:00 +00002959 return;
2960 }
2961
2962 // The following code generates these patterns:
2963 // (1) true_target == nullptr && false_target != nullptr
2964 // - opposite condition true => branch to false_target
2965 // (2) true_target != nullptr && false_target == nullptr
2966 // - condition true => branch to true_target
2967 // (3) true_target != nullptr && false_target != nullptr
2968 // - condition true => branch to true_target
2969 // - branch to false_target
2970 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002971 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002972 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002973 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002974 if (true_target == nullptr) {
2975 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2976 } else {
2977 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2978 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002979 } else {
2980 // The condition instruction has not been materialized, use its inputs as
2981 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002982 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002983 Primitive::Type type = condition->InputAt(0)->GetType();
2984 LocationSummary* locations = cond->GetLocations();
2985 IfCondition if_cond = condition->GetCondition();
2986 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002987
David Brazdil0debae72015-11-12 18:37:00 +00002988 if (true_target == nullptr) {
2989 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002990 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002991 }
2992
Alexey Frunze299a9392015-12-08 16:08:02 -08002993 switch (type) {
2994 default:
2995 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2996 break;
2997 case Primitive::kPrimLong:
2998 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2999 break;
3000 case Primitive::kPrimFloat:
3001 case Primitive::kPrimDouble:
3002 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3003 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003004 }
3005 }
David Brazdil0debae72015-11-12 18:37:00 +00003006
3007 // If neither branch falls through (case 3), the conditional branch to `true_target`
3008 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3009 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003010 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003011 }
3012}
3013
3014void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003016 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003017 locations->SetInAt(0, Location::RequiresRegister());
3018 }
3019}
3020
3021void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003022 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3023 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003024 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003025 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003026 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003027 nullptr : codegen_->GetLabelOf(false_successor);
3028 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029}
3030
3031void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3032 LocationSummary* locations = new (GetGraph()->GetArena())
3033 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003034 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003035 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003036 locations->SetInAt(0, Location::RequiresRegister());
3037 }
3038}
3039
3040void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003041 SlowPathCodeMIPS64* slow_path =
3042 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003043 GenerateTestAndBranch(deoptimize,
3044 /* condition_input_index */ 0,
3045 slow_path->GetEntryLabel(),
3046 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003047}
3048
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003049void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3050 LocationSummary* locations = new (GetGraph()->GetArena())
3051 LocationSummary(flag, LocationSummary::kNoCall);
3052 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003053}
3054
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003055void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3056 __ LoadFromOffset(kLoadWord,
3057 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3058 SP,
3059 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003060}
3061
David Brazdil74eb1b22015-12-14 11:44:01 +00003062void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3064 if (Primitive::IsFloatingPointType(select->GetType())) {
3065 locations->SetInAt(0, Location::RequiresFpuRegister());
3066 locations->SetInAt(1, Location::RequiresFpuRegister());
3067 } else {
3068 locations->SetInAt(0, Location::RequiresRegister());
3069 locations->SetInAt(1, Location::RequiresRegister());
3070 }
3071 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3072 locations->SetInAt(2, Location::RequiresRegister());
3073 }
3074 locations->SetOut(Location::SameAsFirstInput());
3075}
3076
3077void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3078 LocationSummary* locations = select->GetLocations();
3079 Mips64Label false_target;
3080 GenerateTestAndBranch(select,
3081 /* condition_input_index */ 2,
3082 /* true_target */ nullptr,
3083 &false_target);
3084 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3085 __ Bind(&false_target);
3086}
3087
David Srbecky0cf44932015-12-09 14:09:59 +00003088void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3089 new (GetGraph()->GetArena()) LocationSummary(info);
3090}
3091
David Srbeckyd28f4a02016-03-14 17:14:24 +00003092void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3093 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003094}
3095
3096void CodeGeneratorMIPS64::GenerateNop() {
3097 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003098}
3099
Alexey Frunze4dda3372015-06-01 18:31:49 -07003100void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3101 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3102 LocationSummary* locations =
3103 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3104 locations->SetInAt(0, Location::RequiresRegister());
3105 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3106 locations->SetOut(Location::RequiresFpuRegister());
3107 } else {
3108 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3109 }
3110}
3111
3112void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3113 const FieldInfo& field_info) {
3114 Primitive::Type type = field_info.GetFieldType();
3115 LocationSummary* locations = instruction->GetLocations();
3116 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3117 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003118 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003119 switch (type) {
3120 case Primitive::kPrimBoolean:
3121 load_type = kLoadUnsignedByte;
3122 break;
3123 case Primitive::kPrimByte:
3124 load_type = kLoadSignedByte;
3125 break;
3126 case Primitive::kPrimShort:
3127 load_type = kLoadSignedHalfword;
3128 break;
3129 case Primitive::kPrimChar:
3130 load_type = kLoadUnsignedHalfword;
3131 break;
3132 case Primitive::kPrimInt:
3133 case Primitive::kPrimFloat:
3134 load_type = kLoadWord;
3135 break;
3136 case Primitive::kPrimLong:
3137 case Primitive::kPrimDouble:
3138 load_type = kLoadDoubleword;
3139 break;
3140 case Primitive::kPrimNot:
3141 load_type = kLoadUnsignedWord;
3142 break;
3143 case Primitive::kPrimVoid:
3144 LOG(FATAL) << "Unreachable type " << type;
3145 UNREACHABLE();
3146 }
3147 if (!Primitive::IsFloatingPointType(type)) {
3148 DCHECK(locations->Out().IsRegister());
3149 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003150 __ LoadFromOffset(load_type, dst, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151 } else {
3152 DCHECK(locations->Out().IsFpuRegister());
3153 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003154 __ LoadFpuFromOffset(load_type, dst, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003155 }
3156
3157 codegen_->MaybeRecordImplicitNullCheck(instruction);
3158 // TODO: memory barrier?
Alexey Frunzec061de12017-02-14 13:27:23 -08003159
3160 if (type == Primitive::kPrimNot) {
3161 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3162 __ MaybeUnpoisonHeapReference(dst);
3163 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003164}
3165
3166void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3167 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3168 LocationSummary* locations =
3169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3170 locations->SetInAt(0, Location::RequiresRegister());
3171 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3172 locations->SetInAt(1, Location::RequiresFpuRegister());
3173 } else {
3174 locations->SetInAt(1, Location::RequiresRegister());
3175 }
3176}
3177
3178void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003179 const FieldInfo& field_info,
3180 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181 Primitive::Type type = field_info.GetFieldType();
3182 LocationSummary* locations = instruction->GetLocations();
3183 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3184 StoreOperandType store_type = kStoreByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003185 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3186 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003187 switch (type) {
3188 case Primitive::kPrimBoolean:
3189 case Primitive::kPrimByte:
3190 store_type = kStoreByte;
3191 break;
3192 case Primitive::kPrimShort:
3193 case Primitive::kPrimChar:
3194 store_type = kStoreHalfword;
3195 break;
3196 case Primitive::kPrimInt:
3197 case Primitive::kPrimFloat:
3198 case Primitive::kPrimNot:
3199 store_type = kStoreWord;
3200 break;
3201 case Primitive::kPrimLong:
3202 case Primitive::kPrimDouble:
3203 store_type = kStoreDoubleword;
3204 break;
3205 case Primitive::kPrimVoid:
3206 LOG(FATAL) << "Unreachable type " << type;
3207 UNREACHABLE();
3208 }
3209 if (!Primitive::IsFloatingPointType(type)) {
3210 DCHECK(locations->InAt(1).IsRegister());
3211 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003212 if (kPoisonHeapReferences && needs_write_barrier) {
3213 // Note that in the case where `value` is a null reference,
3214 // we do not enter this block, as a null reference does not
3215 // need poisoning.
3216 DCHECK_EQ(type, Primitive::kPrimNot);
3217 __ PoisonHeapReference(TMP, src);
3218 __ StoreToOffset(store_type, TMP, obj, offset);
3219 } else {
3220 __ StoreToOffset(store_type, src, obj, offset);
3221 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003222 } else {
3223 DCHECK(locations->InAt(1).IsFpuRegister());
3224 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003225 __ StoreFpuToOffset(store_type, src, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003226 }
3227
3228 codegen_->MaybeRecordImplicitNullCheck(instruction);
3229 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08003230 if (needs_write_barrier) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231 DCHECK(locations->InAt(1).IsRegister());
3232 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003233 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 }
3235}
3236
3237void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3238 HandleFieldGet(instruction, instruction->GetFieldInfo());
3239}
3240
3241void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3242 HandleFieldGet(instruction, instruction->GetFieldInfo());
3243}
3244
3245void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3246 HandleFieldSet(instruction, instruction->GetFieldInfo());
3247}
3248
3249void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003250 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003251}
3252
Alexey Frunzef63f5692016-12-13 17:43:11 -08003253void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3254 HInstruction* instruction ATTRIBUTE_UNUSED,
3255 Location root,
3256 GpuRegister obj,
3257 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003258 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3259 if (kEmitCompilerReadBarrier) {
3260 UNIMPLEMENTED(FATAL) << "for read barrier";
3261 } else {
3262 // Plain GC root load with no read barrier.
3263 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3264 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3265 // Note that GC roots are not affected by heap poisoning, thus we
3266 // do not have to unpoison `root_reg` here.
3267 }
3268}
3269
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3271 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003273 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3274 locations->SetInAt(0, Location::RequiresRegister());
3275 locations->SetInAt(1, Location::RequiresRegister());
3276 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003277 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003278 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3279}
3280
3281void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3282 LocationSummary* locations = instruction->GetLocations();
3283 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3284 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3285 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3286
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003287 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003288
3289 // Return 0 if `obj` is null.
3290 // TODO: Avoid this check if we know `obj` is not null.
3291 __ Move(out, ZERO);
3292 __ Beqzc(obj, &done);
3293
3294 // Compare the class of `obj` with `cls`.
3295 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08003296 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003297 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003298 // Classes must be equal for the instanceof to succeed.
3299 __ Xor(out, out, cls);
3300 __ Sltiu(out, out, 1);
3301 } else {
3302 // If the classes are not equal, we go into a slow path.
3303 DCHECK(locations->OnlyCallsOnSlowPath());
3304 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003305 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003306 codegen_->AddSlowPath(slow_path);
3307 __ Bnec(out, cls, slow_path->GetEntryLabel());
3308 __ LoadConst32(out, 1);
3309 __ Bind(slow_path->GetExitLabel());
3310 }
3311
3312 __ Bind(&done);
3313}
3314
3315void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3317 locations->SetOut(Location::ConstantLocation(constant));
3318}
3319
3320void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3321 // Will be generated at use site.
3322}
3323
3324void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3326 locations->SetOut(Location::ConstantLocation(constant));
3327}
3328
3329void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3330 // Will be generated at use site.
3331}
3332
Calin Juravle175dc732015-08-25 15:42:32 +01003333void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3334 // The trampoline uses the same calling convention as dex calling conventions,
3335 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3336 // the method_idx.
3337 HandleInvoke(invoke);
3338}
3339
3340void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3341 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3342}
3343
Alexey Frunze4dda3372015-06-01 18:31:49 -07003344void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3345 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3346 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3347}
3348
3349void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3350 HandleInvoke(invoke);
3351 // The register T0 is required to be used for the hidden argument in
3352 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3353 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3354}
3355
3356void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3357 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3358 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003359 Location receiver = invoke->GetLocations()->InAt(0);
3360 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003361 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003362
3363 // Set the hidden argument.
3364 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3365 invoke->GetDexMethodIndex());
3366
3367 // temp = object->GetClass();
3368 if (receiver.IsStackSlot()) {
3369 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3370 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3371 } else {
3372 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3373 }
3374 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003375 // Instead of simply (possibly) unpoisoning `temp` here, we should
3376 // emit a read barrier for the previous class reference load.
3377 // However this is not required in practice, as this is an
3378 // intermediate/temporary reference and because the current
3379 // concurrent copying collector keeps the from-space memory
3380 // intact/accessible until the end of the marking phase (the
3381 // concurrent copying collector may not in the future).
3382 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003383 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3384 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3385 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003386 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003387 // temp = temp->GetImtEntryAt(method_offset);
3388 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3389 // T9 = temp->GetEntryPoint();
3390 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3391 // T9();
3392 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003393 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003394 DCHECK(!codegen_->IsLeafMethod());
3395 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3396}
3397
3398void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003399 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3400 if (intrinsic.TryDispatch(invoke)) {
3401 return;
3402 }
3403
Alexey Frunze4dda3372015-06-01 18:31:49 -07003404 HandleInvoke(invoke);
3405}
3406
3407void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003408 // Explicit clinit checks triggered by static invokes must have been pruned by
3409 // art::PrepareForRegisterAllocation.
3410 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003411
Chris Larsen3039e382015-08-26 07:54:08 -07003412 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3413 if (intrinsic.TryDispatch(invoke)) {
3414 return;
3415 }
3416
Alexey Frunze4dda3372015-06-01 18:31:49 -07003417 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003418}
3419
Orion Hodsonac141392017-01-13 11:53:47 +00003420void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3421 HandleInvoke(invoke);
3422}
3423
3424void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3425 codegen_->GenerateInvokePolymorphicCall(invoke);
3426}
3427
Chris Larsen3039e382015-08-26 07:54:08 -07003428static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003430 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3431 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003432 return true;
3433 }
3434 return false;
3435}
3436
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003437HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003438 HLoadString::LoadKind desired_string_load_kind) {
3439 if (kEmitCompilerReadBarrier) {
3440 UNIMPLEMENTED(FATAL) << "for read barrier";
3441 }
3442 bool fallback_load = false;
3443 switch (desired_string_load_kind) {
3444 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3445 DCHECK(!GetCompilerOptions().GetCompilePic());
3446 break;
3447 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3448 DCHECK(GetCompilerOptions().GetCompilePic());
3449 break;
3450 case HLoadString::LoadKind::kBootImageAddress:
3451 break;
3452 case HLoadString::LoadKind::kBssEntry:
3453 DCHECK(!Runtime::Current()->UseJitCompilation());
3454 break;
3455 case HLoadString::LoadKind::kDexCacheViaMethod:
3456 break;
3457 case HLoadString::LoadKind::kJitTableAddress:
3458 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003459 break;
3460 }
3461 if (fallback_load) {
3462 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3463 }
3464 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003465}
3466
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003467HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3468 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003469 if (kEmitCompilerReadBarrier) {
3470 UNIMPLEMENTED(FATAL) << "for read barrier";
3471 }
3472 bool fallback_load = false;
3473 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003474 case HLoadClass::LoadKind::kInvalid:
3475 LOG(FATAL) << "UNREACHABLE";
3476 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003477 case HLoadClass::LoadKind::kReferrersClass:
3478 break;
3479 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3480 DCHECK(!GetCompilerOptions().GetCompilePic());
3481 break;
3482 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3483 DCHECK(GetCompilerOptions().GetCompilePic());
3484 break;
3485 case HLoadClass::LoadKind::kBootImageAddress:
3486 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003487 case HLoadClass::LoadKind::kBssEntry:
3488 DCHECK(!Runtime::Current()->UseJitCompilation());
3489 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003490 case HLoadClass::LoadKind::kJitTableAddress:
3491 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003492 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003493 case HLoadClass::LoadKind::kDexCacheViaMethod:
3494 break;
3495 }
3496 if (fallback_load) {
3497 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3498 }
3499 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003500}
3501
Vladimir Markodc151b22015-10-15 18:02:30 +01003502HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3503 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003504 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003505 // On MIPS64 we support all dispatch types.
3506 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003507}
3508
Alexey Frunze4dda3372015-06-01 18:31:49 -07003509void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3510 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003511 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003512 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3513 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3514
Alexey Frunze19f6c692016-11-30 19:19:55 -08003515 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003516 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003517 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003518 uint32_t offset =
3519 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003520 __ LoadFromOffset(kLoadDoubleword,
3521 temp.AsRegister<GpuRegister>(),
3522 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003523 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003524 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003525 }
Vladimir Marko58155012015-08-19 12:49:41 +00003526 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003527 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003528 break;
3529 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003530 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3531 kLoadDoubleword,
3532 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003533 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003534 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3535 uint32_t offset = invoke->GetDexCacheArrayOffset();
3536 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003537 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003538 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3539 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3540 break;
3541 }
Vladimir Marko58155012015-08-19 12:49:41 +00003542 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003543 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003544 GpuRegister reg = temp.AsRegister<GpuRegister>();
3545 GpuRegister method_reg;
3546 if (current_method.IsRegister()) {
3547 method_reg = current_method.AsRegister<GpuRegister>();
3548 } else {
3549 // TODO: use the appropriate DCHECK() here if possible.
3550 // DCHECK(invoke->GetLocations()->Intrinsified());
3551 DCHECK(!current_method.IsValid());
3552 method_reg = reg;
3553 __ Ld(reg, SP, kCurrentMethodStackOffset);
3554 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003555
Vladimir Marko58155012015-08-19 12:49:41 +00003556 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003557 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003558 reg,
3559 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003560 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003561 // temp = temp[index_in_cache];
3562 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3563 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003564 __ LoadFromOffset(kLoadDoubleword,
3565 reg,
3566 reg,
3567 CodeGenerator::GetCachePointerOffset(index_in_cache));
3568 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003569 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003570 }
3571
Alexey Frunze19f6c692016-11-30 19:19:55 -08003572 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003573 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003574 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003575 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003576 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3577 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3578 __ LoadFromOffset(kLoadDoubleword,
3579 T9,
3580 callee_method.AsRegister<GpuRegister>(),
3581 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003582 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003583 // T9()
3584 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003585 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003586 break;
3587 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 DCHECK(!IsLeafMethod());
3589}
3590
3591void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003592 // Explicit clinit checks triggered by static invokes must have been pruned by
3593 // art::PrepareForRegisterAllocation.
3594 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003595
3596 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3597 return;
3598 }
3599
3600 LocationSummary* locations = invoke->GetLocations();
3601 codegen_->GenerateStaticOrDirectCall(invoke,
3602 locations->HasTemps()
3603 ? locations->GetTemp(0)
3604 : Location::NoLocation());
3605 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3606}
3607
Alexey Frunze53afca12015-11-05 16:34:23 -08003608void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003609 // Use the calling convention instead of the location of the receiver, as
3610 // intrinsics may have put the receiver in a different register. In the intrinsics
3611 // slow path, the arguments have been moved to the right place, so here we are
3612 // guaranteed that the receiver is the first register of the calling convention.
3613 InvokeDexCallingConvention calling_convention;
3614 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3615
Alexey Frunze53afca12015-11-05 16:34:23 -08003616 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003617 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3618 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3619 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003620 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003621
3622 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003623 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003624 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003625 // Instead of simply (possibly) unpoisoning `temp` here, we should
3626 // emit a read barrier for the previous class reference load.
3627 // However this is not required in practice, as this is an
3628 // intermediate/temporary reference and because the current
3629 // concurrent copying collector keeps the from-space memory
3630 // intact/accessible until the end of the marking phase (the
3631 // concurrent copying collector may not in the future).
3632 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003633 // temp = temp->GetMethodAt(method_offset);
3634 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3635 // T9 = temp->GetEntryPoint();
3636 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3637 // T9();
3638 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003639 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003640}
3641
3642void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3643 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3644 return;
3645 }
3646
3647 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003648 DCHECK(!codegen_->IsLeafMethod());
3649 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3650}
3651
3652void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003653 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3654 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003655 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003656 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003657 cls,
3658 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003659 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003660 return;
3661 }
Vladimir Marko41559982017-01-06 14:04:23 +00003662 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003663
3664 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3665 ? LocationSummary::kCallOnSlowPath
3666 : LocationSummary::kNoCall;
3667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003668 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003669 locations->SetInAt(0, Location::RequiresRegister());
3670 }
3671 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003672}
3673
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003674// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3675// move.
3676void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003677 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3678 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3679 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003680 return;
3681 }
Vladimir Marko41559982017-01-06 14:04:23 +00003682 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003683
Vladimir Marko41559982017-01-06 14:04:23 +00003684 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003685 Location out_loc = locations->Out();
3686 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3687 GpuRegister current_method_reg = ZERO;
3688 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3689 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3690 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3691 }
3692
3693 bool generate_null_check = false;
3694 switch (load_kind) {
3695 case HLoadClass::LoadKind::kReferrersClass:
3696 DCHECK(!cls->CanCallRuntime());
3697 DCHECK(!cls->MustGenerateClinitCheck());
3698 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3699 GenerateGcRootFieldLoad(cls,
3700 out_loc,
3701 current_method_reg,
3702 ArtMethod::DeclaringClassOffset().Int32Value());
3703 break;
3704 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003705 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003706 __ LoadLiteral(out,
3707 kLoadUnsignedWord,
3708 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3709 cls->GetTypeIndex()));
3710 break;
3711 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003712 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003713 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3714 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3715 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3716 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3717 break;
3718 }
3719 case HLoadClass::LoadKind::kBootImageAddress: {
3720 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003721 uint32_t address = dchecked_integral_cast<uint32_t>(
3722 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
3723 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003724 __ LoadLiteral(out,
3725 kLoadUnsignedWord,
3726 codegen_->DeduplicateBootImageAddressLiteral(address));
3727 break;
3728 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003729 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003730 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00003731 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003732 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3733 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003734 generate_null_check = true;
3735 break;
3736 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003737 case HLoadClass::LoadKind::kJitTableAddress:
3738 __ LoadLiteral(out,
3739 kLoadUnsignedWord,
3740 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
3741 cls->GetTypeIndex(),
3742 cls->GetClass()));
3743 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003744 break;
Vladimir Marko41559982017-01-06 14:04:23 +00003745 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003746 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00003747 LOG(FATAL) << "UNREACHABLE";
3748 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003749 }
3750
3751 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3752 DCHECK(cls->CanCallRuntime());
3753 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3754 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3755 codegen_->AddSlowPath(slow_path);
3756 if (generate_null_check) {
3757 __ Beqzc(out, slow_path->GetEntryLabel());
3758 }
3759 if (cls->MustGenerateClinitCheck()) {
3760 GenerateClassInitializationCheck(slow_path, out);
3761 } else {
3762 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003763 }
3764 }
3765}
3766
David Brazdilcb1c0552015-08-04 16:22:25 +01003767static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003768 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003769}
3770
Alexey Frunze4dda3372015-06-01 18:31:49 -07003771void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3772 LocationSummary* locations =
3773 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3774 locations->SetOut(Location::RequiresRegister());
3775}
3776
3777void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3778 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003779 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3780}
3781
3782void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3783 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3784}
3785
3786void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3787 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003788}
3789
Alexey Frunze4dda3372015-06-01 18:31:49 -07003790void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003791 HLoadString::LoadKind load_kind = load->GetLoadKind();
3792 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003794 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3795 InvokeRuntimeCallingConvention calling_convention;
3796 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3797 } else {
3798 locations->SetOut(Location::RequiresRegister());
3799 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003800}
3801
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003802// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3803// move.
3804void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003805 HLoadString::LoadKind load_kind = load->GetLoadKind();
3806 LocationSummary* locations = load->GetLocations();
3807 Location out_loc = locations->Out();
3808 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3809
3810 switch (load_kind) {
3811 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003812 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003813 __ LoadLiteral(out,
3814 kLoadUnsignedWord,
3815 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3816 load->GetStringIndex()));
3817 return; // No dex cache slow path.
3818 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3819 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3820 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003821 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003822 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3823 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3824 return; // No dex cache slow path.
3825 }
3826 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003827 uint32_t address = dchecked_integral_cast<uint32_t>(
3828 reinterpret_cast<uintptr_t>(load->GetString().Get()));
3829 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003830 __ LoadLiteral(out,
3831 kLoadUnsignedWord,
3832 codegen_->DeduplicateBootImageAddressLiteral(address));
3833 return; // No dex cache slow path.
3834 }
3835 case HLoadString::LoadKind::kBssEntry: {
3836 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3837 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003838 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003839 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3840 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003841 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3842 codegen_->AddSlowPath(slow_path);
3843 __ Beqzc(out, slow_path->GetEntryLabel());
3844 __ Bind(slow_path->GetExitLabel());
3845 return;
3846 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003847 case HLoadString::LoadKind::kJitTableAddress:
3848 __ LoadLiteral(out,
3849 kLoadUnsignedWord,
3850 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
3851 load->GetStringIndex(),
3852 load->GetString()));
3853 GenerateGcRootFieldLoad(load, out_loc, out, 0);
3854 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003855 default:
3856 break;
3857 }
3858
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003859 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003860 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3861 InvokeRuntimeCallingConvention calling_convention;
3862 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3863 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3864 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003865}
3866
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3868 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3869 locations->SetOut(Location::ConstantLocation(constant));
3870}
3871
3872void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3873 // Will be generated at use site.
3874}
3875
3876void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3877 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003878 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003879 InvokeRuntimeCallingConvention calling_convention;
3880 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3881}
3882
3883void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003884 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003885 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003886 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003887 if (instruction->IsEnter()) {
3888 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3889 } else {
3890 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3891 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003892}
3893
3894void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3895 LocationSummary* locations =
3896 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3897 switch (mul->GetResultType()) {
3898 case Primitive::kPrimInt:
3899 case Primitive::kPrimLong:
3900 locations->SetInAt(0, Location::RequiresRegister());
3901 locations->SetInAt(1, Location::RequiresRegister());
3902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3903 break;
3904
3905 case Primitive::kPrimFloat:
3906 case Primitive::kPrimDouble:
3907 locations->SetInAt(0, Location::RequiresFpuRegister());
3908 locations->SetInAt(1, Location::RequiresFpuRegister());
3909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3910 break;
3911
3912 default:
3913 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3914 }
3915}
3916
3917void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3918 Primitive::Type type = instruction->GetType();
3919 LocationSummary* locations = instruction->GetLocations();
3920
3921 switch (type) {
3922 case Primitive::kPrimInt:
3923 case Primitive::kPrimLong: {
3924 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3925 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3926 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3927 if (type == Primitive::kPrimInt)
3928 __ MulR6(dst, lhs, rhs);
3929 else
3930 __ Dmul(dst, lhs, rhs);
3931 break;
3932 }
3933 case Primitive::kPrimFloat:
3934 case Primitive::kPrimDouble: {
3935 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3936 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3937 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3938 if (type == Primitive::kPrimFloat)
3939 __ MulS(dst, lhs, rhs);
3940 else
3941 __ MulD(dst, lhs, rhs);
3942 break;
3943 }
3944 default:
3945 LOG(FATAL) << "Unexpected mul type " << type;
3946 }
3947}
3948
3949void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3950 LocationSummary* locations =
3951 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3952 switch (neg->GetResultType()) {
3953 case Primitive::kPrimInt:
3954 case Primitive::kPrimLong:
3955 locations->SetInAt(0, Location::RequiresRegister());
3956 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3957 break;
3958
3959 case Primitive::kPrimFloat:
3960 case Primitive::kPrimDouble:
3961 locations->SetInAt(0, Location::RequiresFpuRegister());
3962 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3963 break;
3964
3965 default:
3966 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3967 }
3968}
3969
3970void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3971 Primitive::Type type = instruction->GetType();
3972 LocationSummary* locations = instruction->GetLocations();
3973
3974 switch (type) {
3975 case Primitive::kPrimInt:
3976 case Primitive::kPrimLong: {
3977 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3978 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3979 if (type == Primitive::kPrimInt)
3980 __ Subu(dst, ZERO, src);
3981 else
3982 __ Dsubu(dst, ZERO, src);
3983 break;
3984 }
3985 case Primitive::kPrimFloat:
3986 case Primitive::kPrimDouble: {
3987 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3988 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3989 if (type == Primitive::kPrimFloat)
3990 __ NegS(dst, src);
3991 else
3992 __ NegD(dst, src);
3993 break;
3994 }
3995 default:
3996 LOG(FATAL) << "Unexpected neg type " << type;
3997 }
3998}
3999
4000void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
4001 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004004 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004005 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4006 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007}
4008
4009void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004010 // Note: if heap poisoning is enabled, the entry point takes care
4011 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004012 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4013 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004014}
4015
4016void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
4017 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004020 if (instruction->IsStringAlloc()) {
4021 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4022 } else {
4023 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004024 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004025 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4026}
4027
4028void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004029 // Note: if heap poisoning is enabled, the entry point takes care
4030 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004031 if (instruction->IsStringAlloc()) {
4032 // String is allocated through StringFactory. Call NewEmptyString entry point.
4033 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02004034 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07004035 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004036 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4037 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
4038 __ Jalr(T9);
4039 __ Nop();
4040 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4041 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01004042 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004043 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004044 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004045}
4046
4047void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
4048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4049 locations->SetInAt(0, Location::RequiresRegister());
4050 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4051}
4052
4053void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
4054 Primitive::Type type = instruction->GetType();
4055 LocationSummary* locations = instruction->GetLocations();
4056
4057 switch (type) {
4058 case Primitive::kPrimInt:
4059 case Primitive::kPrimLong: {
4060 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4061 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4062 __ Nor(dst, src, ZERO);
4063 break;
4064 }
4065
4066 default:
4067 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4068 }
4069}
4070
4071void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4073 locations->SetInAt(0, Location::RequiresRegister());
4074 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4075}
4076
4077void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4078 LocationSummary* locations = instruction->GetLocations();
4079 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4080 locations->InAt(0).AsRegister<GpuRegister>(),
4081 1);
4082}
4083
4084void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004085 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4086 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004087}
4088
Calin Juravle2ae48182016-03-16 14:05:09 +00004089void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4090 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091 return;
4092 }
4093 Location obj = instruction->GetLocations()->InAt(0);
4094
4095 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004096 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004097}
4098
Calin Juravle2ae48182016-03-16 14:05:09 +00004099void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004100 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004101 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004102
4103 Location obj = instruction->GetLocations()->InAt(0);
4104
4105 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4106}
4107
4108void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004109 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004110}
4111
4112void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4113 HandleBinaryOp(instruction);
4114}
4115
4116void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4117 HandleBinaryOp(instruction);
4118}
4119
4120void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4121 LOG(FATAL) << "Unreachable";
4122}
4123
4124void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4125 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4126}
4127
4128void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4129 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4130 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4131 if (location.IsStackSlot()) {
4132 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4133 } else if (location.IsDoubleStackSlot()) {
4134 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4135 }
4136 locations->SetOut(location);
4137}
4138
4139void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4140 ATTRIBUTE_UNUSED) {
4141 // Nothing to do, the parameter is already at its location.
4142}
4143
4144void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4145 LocationSummary* locations =
4146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4147 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4148}
4149
4150void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4151 ATTRIBUTE_UNUSED) {
4152 // Nothing to do, the method is already at its location.
4153}
4154
4155void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4156 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004157 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004158 locations->SetInAt(i, Location::Any());
4159 }
4160 locations->SetOut(Location::Any());
4161}
4162
4163void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4164 LOG(FATAL) << "Unreachable";
4165}
4166
4167void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4168 Primitive::Type type = rem->GetResultType();
4169 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004170 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4171 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4173
4174 switch (type) {
4175 case Primitive::kPrimInt:
4176 case Primitive::kPrimLong:
4177 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004178 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4180 break;
4181
4182 case Primitive::kPrimFloat:
4183 case Primitive::kPrimDouble: {
4184 InvokeRuntimeCallingConvention calling_convention;
4185 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4186 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4187 locations->SetOut(calling_convention.GetReturnLocation(type));
4188 break;
4189 }
4190
4191 default:
4192 LOG(FATAL) << "Unexpected rem type " << type;
4193 }
4194}
4195
4196void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4197 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004198
4199 switch (type) {
4200 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004201 case Primitive::kPrimLong:
4202 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004203 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004204
4205 case Primitive::kPrimFloat:
4206 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004207 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4208 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004209 if (type == Primitive::kPrimFloat) {
4210 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4211 } else {
4212 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4213 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004214 break;
4215 }
4216 default:
4217 LOG(FATAL) << "Unexpected rem type " << type;
4218 }
4219}
4220
4221void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4222 memory_barrier->SetLocations(nullptr);
4223}
4224
4225void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4226 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4227}
4228
4229void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4231 Primitive::Type return_type = ret->InputAt(0)->GetType();
4232 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4233}
4234
4235void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4236 codegen_->GenerateFrameExit();
4237}
4238
4239void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4240 ret->SetLocations(nullptr);
4241}
4242
4243void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4244 codegen_->GenerateFrameExit();
4245}
4246
Alexey Frunze92d90602015-12-18 18:16:36 -08004247void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4248 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004249}
4250
Alexey Frunze92d90602015-12-18 18:16:36 -08004251void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4252 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004253}
4254
Alexey Frunze4dda3372015-06-01 18:31:49 -07004255void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4256 HandleShift(shl);
4257}
4258
4259void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4260 HandleShift(shl);
4261}
4262
4263void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4264 HandleShift(shr);
4265}
4266
4267void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4268 HandleShift(shr);
4269}
4270
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4272 HandleBinaryOp(instruction);
4273}
4274
4275void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4276 HandleBinaryOp(instruction);
4277}
4278
4279void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4280 HandleFieldGet(instruction, instruction->GetFieldInfo());
4281}
4282
4283void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4284 HandleFieldGet(instruction, instruction->GetFieldInfo());
4285}
4286
4287void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4288 HandleFieldSet(instruction, instruction->GetFieldInfo());
4289}
4290
4291void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004292 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004293}
4294
Calin Juravlee460d1d2015-09-29 04:52:17 +01004295void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4296 HUnresolvedInstanceFieldGet* instruction) {
4297 FieldAccessCallingConventionMIPS64 calling_convention;
4298 codegen_->CreateUnresolvedFieldLocationSummary(
4299 instruction, instruction->GetFieldType(), calling_convention);
4300}
4301
4302void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4303 HUnresolvedInstanceFieldGet* instruction) {
4304 FieldAccessCallingConventionMIPS64 calling_convention;
4305 codegen_->GenerateUnresolvedFieldAccess(instruction,
4306 instruction->GetFieldType(),
4307 instruction->GetFieldIndex(),
4308 instruction->GetDexPc(),
4309 calling_convention);
4310}
4311
4312void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4313 HUnresolvedInstanceFieldSet* instruction) {
4314 FieldAccessCallingConventionMIPS64 calling_convention;
4315 codegen_->CreateUnresolvedFieldLocationSummary(
4316 instruction, instruction->GetFieldType(), calling_convention);
4317}
4318
4319void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4320 HUnresolvedInstanceFieldSet* instruction) {
4321 FieldAccessCallingConventionMIPS64 calling_convention;
4322 codegen_->GenerateUnresolvedFieldAccess(instruction,
4323 instruction->GetFieldType(),
4324 instruction->GetFieldIndex(),
4325 instruction->GetDexPc(),
4326 calling_convention);
4327}
4328
4329void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4330 HUnresolvedStaticFieldGet* instruction) {
4331 FieldAccessCallingConventionMIPS64 calling_convention;
4332 codegen_->CreateUnresolvedFieldLocationSummary(
4333 instruction, instruction->GetFieldType(), calling_convention);
4334}
4335
4336void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4337 HUnresolvedStaticFieldGet* instruction) {
4338 FieldAccessCallingConventionMIPS64 calling_convention;
4339 codegen_->GenerateUnresolvedFieldAccess(instruction,
4340 instruction->GetFieldType(),
4341 instruction->GetFieldIndex(),
4342 instruction->GetDexPc(),
4343 calling_convention);
4344}
4345
4346void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4347 HUnresolvedStaticFieldSet* instruction) {
4348 FieldAccessCallingConventionMIPS64 calling_convention;
4349 codegen_->CreateUnresolvedFieldLocationSummary(
4350 instruction, instruction->GetFieldType(), calling_convention);
4351}
4352
4353void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4354 HUnresolvedStaticFieldSet* instruction) {
4355 FieldAccessCallingConventionMIPS64 calling_convention;
4356 codegen_->GenerateUnresolvedFieldAccess(instruction,
4357 instruction->GetFieldType(),
4358 instruction->GetFieldIndex(),
4359 instruction->GetDexPc(),
4360 calling_convention);
4361}
4362
Alexey Frunze4dda3372015-06-01 18:31:49 -07004363void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004364 LocationSummary* locations =
4365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004366 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004367}
4368
4369void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4370 HBasicBlock* block = instruction->GetBlock();
4371 if (block->GetLoopInformation() != nullptr) {
4372 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4373 // The back edge will generate the suspend check.
4374 return;
4375 }
4376 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4377 // The goto will generate the suspend check.
4378 return;
4379 }
4380 GenerateSuspendCheck(instruction, nullptr);
4381}
4382
Alexey Frunze4dda3372015-06-01 18:31:49 -07004383void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4384 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004385 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004386 InvokeRuntimeCallingConvention calling_convention;
4387 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4388}
4389
4390void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004391 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004392 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4393}
4394
4395void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4396 Primitive::Type input_type = conversion->GetInputType();
4397 Primitive::Type result_type = conversion->GetResultType();
4398 DCHECK_NE(input_type, result_type);
4399
4400 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4401 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4402 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4403 }
4404
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4406
4407 if (Primitive::IsFloatingPointType(input_type)) {
4408 locations->SetInAt(0, Location::RequiresFpuRegister());
4409 } else {
4410 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004411 }
4412
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004413 if (Primitive::IsFloatingPointType(result_type)) {
4414 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004415 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004417 }
4418}
4419
4420void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4421 LocationSummary* locations = conversion->GetLocations();
4422 Primitive::Type result_type = conversion->GetResultType();
4423 Primitive::Type input_type = conversion->GetInputType();
4424
4425 DCHECK_NE(input_type, result_type);
4426
4427 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4428 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4429 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4430
4431 switch (result_type) {
4432 case Primitive::kPrimChar:
4433 __ Andi(dst, src, 0xFFFF);
4434 break;
4435 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004436 if (input_type == Primitive::kPrimLong) {
4437 // Type conversion from long to types narrower than int is a result of code
4438 // transformations. To avoid unpredictable results for SEB and SEH, we first
4439 // need to sign-extend the low 32-bit value into bits 32 through 63.
4440 __ Sll(dst, src, 0);
4441 __ Seb(dst, dst);
4442 } else {
4443 __ Seb(dst, src);
4444 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004445 break;
4446 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004447 if (input_type == Primitive::kPrimLong) {
4448 // Type conversion from long to types narrower than int is a result of code
4449 // transformations. To avoid unpredictable results for SEB and SEH, we first
4450 // need to sign-extend the low 32-bit value into bits 32 through 63.
4451 __ Sll(dst, src, 0);
4452 __ Seh(dst, dst);
4453 } else {
4454 __ Seh(dst, src);
4455 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004456 break;
4457 case Primitive::kPrimInt:
4458 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004459 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4460 // conversions, except when the input and output registers are the same and we are not
4461 // converting longs to shorter types. In these cases, do nothing.
4462 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4463 __ Sll(dst, src, 0);
4464 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004465 break;
4466
4467 default:
4468 LOG(FATAL) << "Unexpected type conversion from " << input_type
4469 << " to " << result_type;
4470 }
4471 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004472 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4473 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4474 if (input_type == Primitive::kPrimLong) {
4475 __ Dmtc1(src, FTMP);
4476 if (result_type == Primitive::kPrimFloat) {
4477 __ Cvtsl(dst, FTMP);
4478 } else {
4479 __ Cvtdl(dst, FTMP);
4480 }
4481 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004482 __ Mtc1(src, FTMP);
4483 if (result_type == Primitive::kPrimFloat) {
4484 __ Cvtsw(dst, FTMP);
4485 } else {
4486 __ Cvtdw(dst, FTMP);
4487 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004488 }
4489 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4490 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004491 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4492 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4493 Mips64Label truncate;
4494 Mips64Label done;
4495
4496 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4497 // value when the input is either a NaN or is outside of the range of the output type
4498 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4499 // the same result.
4500 //
4501 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4502 // value of the output type if the input is outside of the range after the truncation or
4503 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4504 // results. This matches the desired float/double-to-int/long conversion exactly.
4505 //
4506 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4507 //
4508 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4509 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4510 // even though it must be NAN2008=1 on R6.
4511 //
4512 // The code takes care of the different behaviors by first comparing the input to the
4513 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4514 // If the input is greater than or equal to the minimum, it procedes to the truncate
4515 // instruction, which will handle such an input the same way irrespective of NAN2008.
4516 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4517 // in order to return either zero or the minimum value.
4518 //
4519 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4520 // truncate instruction for MIPS64R6.
4521 if (input_type == Primitive::kPrimFloat) {
4522 uint32_t min_val = (result_type == Primitive::kPrimLong)
4523 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4524 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4525 __ LoadConst32(TMP, min_val);
4526 __ Mtc1(TMP, FTMP);
4527 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004528 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004529 uint64_t min_val = (result_type == Primitive::kPrimLong)
4530 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4531 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4532 __ LoadConst64(TMP, min_val);
4533 __ Dmtc1(TMP, FTMP);
4534 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004535 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004536
4537 __ Bc1nez(FTMP, &truncate);
4538
4539 if (input_type == Primitive::kPrimFloat) {
4540 __ CmpEqS(FTMP, src, src);
4541 } else {
4542 __ CmpEqD(FTMP, src, src);
4543 }
4544 if (result_type == Primitive::kPrimLong) {
4545 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4546 } else {
4547 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4548 }
4549 __ Mfc1(TMP, FTMP);
4550 __ And(dst, dst, TMP);
4551
4552 __ Bc(&done);
4553
4554 __ Bind(&truncate);
4555
4556 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004557 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004558 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004559 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004560 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004561 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004562 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004563 } else {
4564 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004565 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004566 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004567 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004568 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004569 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004570 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004571
4572 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004573 } else if (Primitive::IsFloatingPointType(result_type) &&
4574 Primitive::IsFloatingPointType(input_type)) {
4575 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4576 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4577 if (result_type == Primitive::kPrimFloat) {
4578 __ Cvtsd(dst, src);
4579 } else {
4580 __ Cvtds(dst, src);
4581 }
4582 } else {
4583 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4584 << " to " << result_type;
4585 }
4586}
4587
4588void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4589 HandleShift(ushr);
4590}
4591
4592void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4593 HandleShift(ushr);
4594}
4595
4596void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4597 HandleBinaryOp(instruction);
4598}
4599
4600void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4601 HandleBinaryOp(instruction);
4602}
4603
4604void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4605 // Nothing to do, this should be removed during prepare for register allocator.
4606 LOG(FATAL) << "Unreachable";
4607}
4608
4609void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4610 // Nothing to do, this should be removed during prepare for register allocator.
4611 LOG(FATAL) << "Unreachable";
4612}
4613
4614void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004615 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004616}
4617
4618void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004619 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004620}
4621
4622void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004623 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004624}
4625
4626void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004627 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004628}
4629
4630void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004631 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004632}
4633
4634void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004635 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004636}
4637
4638void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004639 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004640}
4641
4642void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004643 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004644}
4645
4646void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004647 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004648}
4649
4650void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004651 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004652}
4653
4654void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004655 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004656}
4657
4658void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004659 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004660}
4661
Aart Bike9f37602015-10-09 11:15:55 -07004662void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004663 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004664}
4665
4666void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004667 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004668}
4669
4670void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004671 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004672}
4673
4674void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004675 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004676}
4677
4678void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004679 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004680}
4681
4682void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004683 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004684}
4685
4686void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004687 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004688}
4689
4690void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004691 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004692}
4693
Mark Mendellfe57faa2015-09-18 09:26:15 -04004694// Simple implementation of packed switch - generate cascaded compare/jumps.
4695void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4696 LocationSummary* locations =
4697 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4698 locations->SetInAt(0, Location::RequiresRegister());
4699}
4700
Alexey Frunze0960ac52016-12-20 17:24:59 -08004701void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4702 int32_t lower_bound,
4703 uint32_t num_entries,
4704 HBasicBlock* switch_block,
4705 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004706 // Create a set of compare/jumps.
4707 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004708 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004709 // Jump to default if index is negative
4710 // Note: We don't check the case that index is positive while value < lower_bound, because in
4711 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4712 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4713
Alexey Frunze0960ac52016-12-20 17:24:59 -08004714 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004715 // Jump to successors[0] if value == lower_bound.
4716 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4717 int32_t last_index = 0;
4718 for (; num_entries - last_index > 2; last_index += 2) {
4719 __ Addiu(temp_reg, temp_reg, -2);
4720 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4721 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4722 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4723 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4724 }
4725 if (num_entries - last_index == 2) {
4726 // The last missing case_value.
4727 __ Addiu(temp_reg, temp_reg, -1);
4728 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004729 }
4730
4731 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08004732 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004733 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004734 }
4735}
4736
Alexey Frunze0960ac52016-12-20 17:24:59 -08004737void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
4738 int32_t lower_bound,
4739 uint32_t num_entries,
4740 HBasicBlock* switch_block,
4741 HBasicBlock* default_block) {
4742 // Create a jump table.
4743 std::vector<Mips64Label*> labels(num_entries);
4744 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
4745 for (uint32_t i = 0; i < num_entries; i++) {
4746 labels[i] = codegen_->GetLabelOf(successors[i]);
4747 }
4748 JumpTable* table = __ CreateJumpTable(std::move(labels));
4749
4750 // Is the value in range?
4751 __ Addiu32(TMP, value_reg, -lower_bound);
4752 __ LoadConst32(AT, num_entries);
4753 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
4754
4755 // We are in the range of the table.
4756 // Load the target address from the jump table, indexing by the value.
4757 __ LoadLabelAddress(AT, table->GetLabel());
4758 __ Sll(TMP, TMP, 2);
4759 __ Daddu(TMP, TMP, AT);
4760 __ Lw(TMP, TMP, 0);
4761 // Compute the absolute target address by adding the table start address
4762 // (the table contains offsets to targets relative to its start).
4763 __ Daddu(TMP, TMP, AT);
4764 // And jump.
4765 __ Jr(TMP);
4766 __ Nop();
4767}
4768
4769void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4770 int32_t lower_bound = switch_instr->GetStartValue();
4771 uint32_t num_entries = switch_instr->GetNumEntries();
4772 LocationSummary* locations = switch_instr->GetLocations();
4773 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4774 HBasicBlock* switch_block = switch_instr->GetBlock();
4775 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4776
4777 if (num_entries > kPackedSwitchJumpTableThreshold) {
4778 GenTableBasedPackedSwitch(value_reg,
4779 lower_bound,
4780 num_entries,
4781 switch_block,
4782 default_block);
4783 } else {
4784 GenPackedSwitchWithCompares(value_reg,
4785 lower_bound,
4786 num_entries,
4787 switch_block,
4788 default_block);
4789 }
4790}
4791
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004792void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4793 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4794}
4795
4796void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4797 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4798}
4799
Alexey Frunze4dda3372015-06-01 18:31:49 -07004800} // namespace mips64
4801} // namespace art