blob: 4c8dabfedec75c7bcbcdd15e08654fe0ec2e04f3 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze19f6c692016-11-30 19:19:55 -080021#include "compiled_method.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070022#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070026#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070027#include "mirror/array-inl.h"
28#include "mirror/class-inl.h"
29#include "offsets.h"
30#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070031#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070032#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070033#include "utils/stack_checks.h"
34
35namespace art {
36namespace mips64 {
37
38static constexpr int kCurrentMethodStackOffset = 0;
39static constexpr GpuRegister kMethodRegisterArgument = A0;
40
Alexey Frunze4dda3372015-06-01 18:31:49 -070041Location Mips64ReturnLocation(Primitive::Type return_type) {
42 switch (return_type) {
43 case Primitive::kPrimBoolean:
44 case Primitive::kPrimByte:
45 case Primitive::kPrimChar:
46 case Primitive::kPrimShort:
47 case Primitive::kPrimInt:
48 case Primitive::kPrimNot:
49 case Primitive::kPrimLong:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimFloat:
53 case Primitive::kPrimDouble:
54 return Location::FpuRegisterLocation(F0);
55
56 case Primitive::kPrimVoid:
57 return Location();
58 }
59 UNREACHABLE();
60}
61
62Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
63 return Mips64ReturnLocation(type);
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
67 return Location::RegisterLocation(kMethodRegisterArgument);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
71 Location next_location;
72 if (type == Primitive::kPrimVoid) {
73 LOG(FATAL) << "Unexpected parameter type " << type;
74 }
75
76 if (Primitive::IsFloatingPointType(type) &&
77 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
78 next_location = Location::FpuRegisterLocation(
79 calling_convention.GetFpuRegisterAt(float_index_++));
80 gp_index_++;
81 } else if (!Primitive::IsFloatingPointType(type) &&
82 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
83 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
84 float_index_++;
85 } else {
86 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
87 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
88 : Location::StackSlot(stack_offset);
89 }
90
91 // Space on the stack is reserved for all arguments.
92 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
93
Alexey Frunze4dda3372015-06-01 18:31:49 -070094 return next_location;
95}
96
97Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
98 return Mips64ReturnLocation(type);
99}
100
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100101// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
102#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700103#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700104
105class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100110 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
112 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000113 if (instruction_->CanThrowIntoCatchBlock()) {
114 // Live registers will be restored in the catch block if caught.
115 SaveLiveRegisters(codegen, instruction_->GetLocations());
116 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 // We're moving two locations to locations that could overlap, so we need a parallel
118 // move resolver.
119 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100120 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
122 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
125 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100126 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
127 ? kQuickThrowStringBounds
128 : kQuickThrowArrayBounds;
129 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700131 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
132 }
133
Alexandre Rames8158f282015-08-07 10:26:17 +0100134 bool IsFatal() const OVERRIDE { return true; }
135
Roland Levillain46648892015-06-19 16:07:18 +0100136 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
137
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700139 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
140};
141
142class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
143 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
147 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
148 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100149 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
151 }
152
Alexandre Rames8158f282015-08-07 10:26:17 +0100153 bool IsFatal() const OVERRIDE { return true; }
154
Roland Levillain46648892015-06-19 16:07:18 +0100155 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
156
Alexey Frunze4dda3372015-06-01 18:31:49 -0700157 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
159};
160
161class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
162 public:
163 LoadClassSlowPathMIPS64(HLoadClass* cls,
164 HInstruction* at,
165 uint32_t dex_pc,
166 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000167 : SlowPathCodeMIPS64(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700168 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
169 }
170
171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000172 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700173 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
174
175 __ Bind(GetEntryLabel());
176 SaveLiveRegisters(codegen, locations);
177
178 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000179 dex::TypeIndex type_index = cls_->GetTypeIndex();
180 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100181 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
182 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000183 mips64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700184 if (do_clinit_) {
185 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
186 } else {
187 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
188 }
189
190 // Move the class to the desired location.
191 Location out = locations->Out();
192 if (out.IsValid()) {
193 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000194 Primitive::Type type = instruction_->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700195 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
196 }
197
198 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000199 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
200 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
201 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
202 DCHECK(out.IsValid());
203 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
204 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
205 // so that we can avoid another calculation here.
206 DCHECK_NE(out.AsRegister<GpuRegister>(), AT);
207 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000208 mips64_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000209 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
210 __ Sw(out.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
211 }
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700212 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
214
Roland Levillain46648892015-06-19 16:07:18 +0100215 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
216
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 private:
218 // The class this slow path will load.
219 HLoadClass* const cls_;
220
Alexey Frunze4dda3372015-06-01 18:31:49 -0700221 // The dex PC of `at_`.
222 const uint32_t dex_pc_;
223
224 // Whether to initialize the class.
225 const bool do_clinit_;
226
227 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
228};
229
230class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
231 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000232 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700233
234 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
235 LocationSummary* locations = instruction_->GetLocations();
236 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
237 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
238
239 __ Bind(GetEntryLabel());
240 SaveLiveRegisters(codegen, locations);
241
242 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzef63f5692016-12-13 17:43:11 -0800243 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100246 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700247 instruction_,
248 instruction_->GetDexPc(),
249 this);
250 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
251 Primitive::Type type = instruction_->GetType();
252 mips64_codegen->MoveLocation(locations->Out(),
253 calling_convention.GetReturnLocation(type),
254 type);
255
256 RestoreLiveRegisters(codegen, locations);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800257
258 // Store the resolved String to the BSS entry.
259 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
260 // .bss entry address in the fast path, so that we can avoid another calculation here.
261 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
262 DCHECK_NE(out, AT);
263 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
264 mips64_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
265 mips64_codegen->EmitPcRelativeAddressPlaceholderHigh(info, AT);
266 __ Sw(out, AT, /* placeholder */ 0x5678);
267
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700268 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700269 }
270
Roland Levillain46648892015-06-19 16:07:18 +0100271 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
272
Alexey Frunze4dda3372015-06-01 18:31:49 -0700273 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700274 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
275};
276
277class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
278 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000279 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700280
281 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
282 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
283 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000284 if (instruction_->CanThrowIntoCatchBlock()) {
285 // Live registers will be restored in the catch block if caught.
286 SaveLiveRegisters(codegen, instruction_->GetLocations());
287 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100288 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289 instruction_,
290 instruction_->GetDexPc(),
291 this);
292 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
293 }
294
Alexandre Rames8158f282015-08-07 10:26:17 +0100295 bool IsFatal() const OVERRIDE { return true; }
296
Roland Levillain46648892015-06-19 16:07:18 +0100297 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
298
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
301};
302
303class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
304 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100305 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000306 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307
308 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
309 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
310 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100311 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700316 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700317 }
318 }
319
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700320 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700321 DCHECK(successor_ == nullptr);
322 return &return_label_;
323 }
324
Roland Levillain46648892015-06-19 16:07:18 +0100325 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
326
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 // If not null, the block to branch to after the suspend check.
329 HBasicBlock* const successor_;
330
331 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700332 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700333
334 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
335};
336
337class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
338 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000339 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340
341 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
342 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 DCHECK(instruction_->IsCheckCast()
346 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
347 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
348
349 __ Bind(GetEntryLabel());
350 SaveLiveRegisters(codegen, locations);
351
352 // We're moving two locations to locations that could overlap, so we need a parallel
353 // move resolver.
354 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800355 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
357 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
360 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100362 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Primitive::Type ret_type = instruction_->GetType();
365 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
366 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 } else {
368 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800369 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
370 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 }
372
373 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700374 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 }
376
Roland Levillain46648892015-06-19 16:07:18 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
378
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
381};
382
383class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800389 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100391 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000392 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 }
394
Roland Levillain46648892015-06-19 16:07:18 +0100395 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
396
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
399};
400
401CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
402 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100403 const CompilerOptions& compiler_options,
404 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 : CodeGenerator(graph,
406 kNumberOfGpuRegisters,
407 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000408 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
410 arraysize(kCoreCalleeSaves)),
411 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
412 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100413 compiler_options,
414 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100415 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700416 location_builder_(graph, this),
417 instruction_visitor_(graph, this),
418 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100419 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800420 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800421 uint32_literals_(std::less<uint32_t>(),
422 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800423 uint64_literals_(std::less<uint64_t>(),
424 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800425 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800426 boot_image_string_patches_(StringReferenceValueComparator(),
427 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
428 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
429 boot_image_type_patches_(TypeReferenceValueComparator(),
430 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
431 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000432 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800433 boot_image_address_patches_(std::less<uint32_t>(),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800434 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
435 jit_string_patches_(StringReferenceValueComparator(),
436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
437 jit_class_patches_(TypeReferenceValueComparator(),
438 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700439 // Save RA (containing the return address) to mimic Quick.
440 AddAllocatedRegister(Location::RegisterLocation(RA));
441}
442
443#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100444// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
445#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700446#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700447
448void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700449 // Ensure that we fix up branches.
450 __ FinalizeCode();
451
452 // Adjust native pc offsets in stack maps.
453 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800454 uint32_t old_position =
455 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700456 uint32_t new_position = __ GetAdjustedPosition(old_position);
457 DCHECK_GE(new_position, old_position);
458 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
459 }
460
461 // Adjust pc offsets for the disassembly information.
462 if (disasm_info_ != nullptr) {
463 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
464 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
465 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
466 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
467 it.second.start = __ GetAdjustedPosition(it.second.start);
468 it.second.end = __ GetAdjustedPosition(it.second.end);
469 }
470 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
471 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
472 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
473 }
474 }
475
Alexey Frunze4dda3372015-06-01 18:31:49 -0700476 CodeGenerator::Finalize(allocator);
477}
478
479Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
480 return codegen_->GetAssembler();
481}
482
483void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100484 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700485 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
486}
487
488void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100489 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700490 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
491}
492
493void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
494 // Pop reg
495 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200496 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497}
498
499void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
500 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200501 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700502 __ Sd(GpuRegister(reg), SP, 0);
503}
504
505void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
506 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
507 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
508 // Allocate a scratch register other than TMP, if available.
509 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
510 // automatically unspilled when the scratch scope object is destroyed).
511 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
512 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200513 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700514 __ LoadFromOffset(load_type,
515 GpuRegister(ensure_scratch.GetRegister()),
516 SP,
517 index1 + stack_offset);
518 __ LoadFromOffset(load_type,
519 TMP,
520 SP,
521 index2 + stack_offset);
522 __ StoreToOffset(store_type,
523 GpuRegister(ensure_scratch.GetRegister()),
524 SP,
525 index2 + stack_offset);
526 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
527}
528
529static dwarf::Reg DWARFReg(GpuRegister reg) {
530 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
531}
532
David Srbeckyba702002016-02-01 18:15:29 +0000533static dwarf::Reg DWARFReg(FpuRegister reg) {
534 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
535}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700536
537void CodeGeneratorMIPS64::GenerateFrameEntry() {
538 __ Bind(&frame_entry_label_);
539
540 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
541
542 if (do_overflow_check) {
543 __ LoadFromOffset(kLoadWord,
544 ZERO,
545 SP,
546 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
547 RecordPcInfo(nullptr, 0);
548 }
549
Alexey Frunze4dda3372015-06-01 18:31:49 -0700550 if (HasEmptyFrame()) {
551 return;
552 }
553
554 // Make sure the frame size isn't unreasonably large. Per the various APIs
555 // it looks like it should always be less than 2GB in size, which allows
556 // us using 32-bit signed offsets from the stack pointer.
557 if (GetFrameSize() > 0x7FFFFFFF)
558 LOG(FATAL) << "Stack frame larger than 2GB";
559
560 // Spill callee-saved registers.
561 // Note that their cumulative size is small and they can be indexed using
562 // 16-bit offsets.
563
564 // TODO: increment/decrement SP in one step instead of two or remove this comment.
565
566 uint32_t ofs = FrameEntrySpillSize();
567 __ IncreaseFrameSize(ofs);
568
569 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
570 GpuRegister reg = kCoreCalleeSaves[i];
571 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200572 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700573 __ Sd(reg, SP, ofs);
574 __ cfi().RelOffset(DWARFReg(reg), ofs);
575 }
576 }
577
578 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
579 FpuRegister reg = kFpuCalleeSaves[i];
580 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200581 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700582 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000583 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700584 }
585 }
586
587 // Allocate the rest of the frame and store the current method pointer
588 // at its end.
589
590 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
591
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100592 // Save the current method if we need it. Note that we do not
593 // do this in HCurrentMethod, as the instruction might have been removed
594 // in the SSA graph.
595 if (RequiresCurrentMethod()) {
596 static_assert(IsInt<16>(kCurrentMethodStackOffset),
597 "kCurrentMethodStackOffset must fit into int16_t");
598 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
599 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100600
601 if (GetGraph()->HasShouldDeoptimizeFlag()) {
602 // Initialize should_deoptimize flag to 0.
603 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
604 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700605}
606
607void CodeGeneratorMIPS64::GenerateFrameExit() {
608 __ cfi().RememberState();
609
Alexey Frunze4dda3372015-06-01 18:31:49 -0700610 if (!HasEmptyFrame()) {
611 // Deallocate the rest of the frame.
612
613 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
614
615 // Restore callee-saved registers.
616 // Note that their cumulative size is small and they can be indexed using
617 // 16-bit offsets.
618
619 // TODO: increment/decrement SP in one step instead of two or remove this comment.
620
621 uint32_t ofs = 0;
622
623 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
624 FpuRegister reg = kFpuCalleeSaves[i];
625 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
626 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200627 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000628 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700629 }
630 }
631
632 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
633 GpuRegister reg = kCoreCalleeSaves[i];
634 if (allocated_registers_.ContainsCoreRegister(reg)) {
635 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200636 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700637 __ cfi().Restore(DWARFReg(reg));
638 }
639 }
640
641 DCHECK_EQ(ofs, FrameEntrySpillSize());
642 __ DecreaseFrameSize(ofs);
643 }
644
645 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700646 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700647
648 __ cfi().RestoreState();
649 __ cfi().DefCFAOffset(GetFrameSize());
650}
651
652void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
653 __ Bind(GetLabelOf(block));
654}
655
656void CodeGeneratorMIPS64::MoveLocation(Location destination,
657 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100658 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 if (source.Equals(destination)) {
660 return;
661 }
662
663 // A valid move can always be inferred from the destination and source
664 // locations. When moving from and to a register, the argument type can be
665 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100666 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700667 DCHECK_EQ(unspecified_type, false);
668
669 if (destination.IsRegister() || destination.IsFpuRegister()) {
670 if (unspecified_type) {
671 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
672 if (source.IsStackSlot() ||
673 (src_cst != nullptr && (src_cst->IsIntConstant()
674 || src_cst->IsFloatConstant()
675 || src_cst->IsNullConstant()))) {
676 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100677 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 } else {
679 // If the source is a double stack slot or a 64bit constant, a 64bit
680 // type is appropriate. Else the source is a register, and since the
681 // type has not been specified, we chose a 64bit type to force a 64bit
682 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700684 }
685 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100686 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
687 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700688 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
689 // Move to GPR/FPR from stack
690 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 __ LoadFpuFromOffset(load_type,
693 destination.AsFpuRegister<FpuRegister>(),
694 SP,
695 source.GetStackIndex());
696 } else {
697 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
698 __ LoadFromOffset(load_type,
699 destination.AsRegister<GpuRegister>(),
700 SP,
701 source.GetStackIndex());
702 }
703 } else if (source.IsConstant()) {
704 // Move to GPR/FPR from constant
705 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 gpr = destination.AsRegister<GpuRegister>();
708 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700710 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
711 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
712 gpr = ZERO;
713 } else {
714 __ LoadConst32(gpr, value);
715 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700717 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
718 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
719 gpr = ZERO;
720 } else {
721 __ LoadConst64(gpr, value);
722 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700723 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700725 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100726 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700727 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
728 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 if (destination.IsRegister()) {
731 // Move to GPR from GPR
732 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
733 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100734 DCHECK(destination.IsFpuRegister());
735 if (Primitive::Is64BitType(dst_type)) {
736 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
737 } else {
738 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
739 }
740 }
741 } else if (source.IsFpuRegister()) {
742 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700743 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100744 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700745 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
746 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100747 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
749 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100750 } else {
751 DCHECK(destination.IsRegister());
752 if (Primitive::Is64BitType(dst_type)) {
753 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
754 } else {
755 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 }
758 }
759 } else { // The destination is not a register. It must be a stack slot.
760 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
761 if (source.IsRegister() || source.IsFpuRegister()) {
762 if (unspecified_type) {
763 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100764 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700765 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100766 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700767 }
768 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100769 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
770 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700771 // Move to stack from GPR/FPR
772 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
773 if (source.IsRegister()) {
774 __ StoreToOffset(store_type,
775 source.AsRegister<GpuRegister>(),
776 SP,
777 destination.GetStackIndex());
778 } else {
779 __ StoreFpuToOffset(store_type,
780 source.AsFpuRegister<FpuRegister>(),
781 SP,
782 destination.GetStackIndex());
783 }
784 } else if (source.IsConstant()) {
785 // Move to stack from constant
786 HConstant* src_cst = source.GetConstant();
787 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700788 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700789 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
791 if (value != 0) {
792 gpr = TMP;
793 __ LoadConst32(gpr, value);
794 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700795 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700796 DCHECK(destination.IsDoubleStackSlot());
797 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
798 if (value != 0) {
799 gpr = TMP;
800 __ LoadConst64(gpr, value);
801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700802 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700803 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700804 } else {
805 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
806 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
807 // Move to stack from stack
808 if (destination.IsStackSlot()) {
809 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
810 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
811 } else {
812 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
813 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
814 }
815 }
816 }
817}
818
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700819void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700820 DCHECK(!loc1.IsConstant());
821 DCHECK(!loc2.IsConstant());
822
823 if (loc1.Equals(loc2)) {
824 return;
825 }
826
827 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
828 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
829 bool is_fp_reg1 = loc1.IsFpuRegister();
830 bool is_fp_reg2 = loc2.IsFpuRegister();
831
832 if (loc2.IsRegister() && loc1.IsRegister()) {
833 // Swap 2 GPRs
834 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
835 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
836 __ Move(TMP, r2);
837 __ Move(r2, r1);
838 __ Move(r1, TMP);
839 } else if (is_fp_reg2 && is_fp_reg1) {
840 // Swap 2 FPRs
841 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
842 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700843 if (type == Primitive::kPrimFloat) {
844 __ MovS(FTMP, r1);
845 __ MovS(r1, r2);
846 __ MovS(r2, FTMP);
847 } else {
848 DCHECK_EQ(type, Primitive::kPrimDouble);
849 __ MovD(FTMP, r1);
850 __ MovD(r1, r2);
851 __ MovD(r2, FTMP);
852 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700853 } else if (is_slot1 != is_slot2) {
854 // Swap GPR/FPR and stack slot
855 Location reg_loc = is_slot1 ? loc2 : loc1;
856 Location mem_loc = is_slot1 ? loc1 : loc2;
857 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
858 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
859 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
860 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
861 if (reg_loc.IsFpuRegister()) {
862 __ StoreFpuToOffset(store_type,
863 reg_loc.AsFpuRegister<FpuRegister>(),
864 SP,
865 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700866 if (mem_loc.IsStackSlot()) {
867 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
868 } else {
869 DCHECK(mem_loc.IsDoubleStackSlot());
870 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
871 }
872 } else {
873 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
874 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
875 }
876 } else if (is_slot1 && is_slot2) {
877 move_resolver_.Exchange(loc1.GetStackIndex(),
878 loc2.GetStackIndex(),
879 loc1.IsDoubleStackSlot());
880 } else {
881 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
882 }
883}
884
Calin Juravle175dc732015-08-25 15:42:32 +0100885void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
886 DCHECK(location.IsRegister());
887 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
888}
889
Calin Juravlee460d1d2015-09-29 04:52:17 +0100890void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
891 if (location.IsRegister()) {
892 locations->AddTemp(location);
893 } else {
894 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
895 }
896}
897
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100898void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
899 GpuRegister value,
900 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700901 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700902 GpuRegister card = AT;
903 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100904 if (value_can_be_null) {
905 __ Beqzc(value, &done);
906 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700907 __ LoadFromOffset(kLoadDoubleword,
908 card,
909 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700910 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
912 __ Daddu(temp, card, temp);
913 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100914 if (value_can_be_null) {
915 __ Bind(&done);
916 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700917}
918
Alexey Frunze19f6c692016-11-30 19:19:55 -0800919template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
920inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
921 const ArenaDeque<PcRelativePatchInfo>& infos,
922 ArenaVector<LinkerPatch>* linker_patches) {
923 for (const PcRelativePatchInfo& info : infos) {
924 const DexFile& dex_file = info.target_dex_file;
925 size_t offset_or_index = info.offset_or_index;
926 DCHECK(info.pc_rel_label.IsBound());
927 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
928 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
929 }
930}
931
932void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
933 DCHECK(linker_patches->empty());
934 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -0800935 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800936 pc_relative_string_patches_.size() +
937 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +0000938 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800939 boot_image_string_patches_.size() +
940 boot_image_type_patches_.size() +
941 boot_image_address_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -0800942 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800943 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
944 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800945 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +0000946 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800947 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
948 linker_patches);
949 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000950 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
951 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800952 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
953 linker_patches);
954 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000955 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
956 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800957 for (const auto& entry : boot_image_string_patches_) {
958 const StringReference& target_string = entry.first;
959 Literal* literal = entry.second;
960 DCHECK(literal->GetLabel()->IsBound());
961 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
962 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
963 target_string.dex_file,
964 target_string.string_index.index_));
965 }
966 for (const auto& entry : boot_image_type_patches_) {
967 const TypeReference& target_type = entry.first;
968 Literal* literal = entry.second;
969 DCHECK(literal->GetLabel()->IsBound());
970 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
971 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
972 target_type.dex_file,
973 target_type.type_index.index_));
974 }
975 for (const auto& entry : boot_image_address_patches_) {
976 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
977 Literal* literal = entry.second;
978 DCHECK(literal->GetLabel()->IsBound());
979 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
980 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
981 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000982 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800983}
984
985CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000986 const DexFile& dex_file, dex::StringIndex string_index) {
987 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800988}
989
990CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
991 const DexFile& dex_file, dex::TypeIndex type_index) {
992 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800993}
994
Vladimir Marko1998cd02017-01-13 13:02:58 +0000995CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
996 const DexFile& dex_file, dex::TypeIndex type_index) {
997 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
998}
999
Alexey Frunze19f6c692016-11-30 19:19:55 -08001000CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1001 const DexFile& dex_file, uint32_t element_offset) {
1002 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1003}
1004
Alexey Frunze19f6c692016-11-30 19:19:55 -08001005CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1006 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1007 patches->emplace_back(dex_file, offset_or_index);
1008 return &patches->back();
1009}
1010
Alexey Frunzef63f5692016-12-13 17:43:11 -08001011Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1012 return map->GetOrCreate(
1013 value,
1014 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1015}
1016
Alexey Frunze19f6c692016-11-30 19:19:55 -08001017Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1018 return uint64_literals_.GetOrCreate(
1019 value,
1020 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1021}
1022
1023Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1024 MethodToLiteralMap* map) {
1025 return map->GetOrCreate(
1026 target_method,
1027 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1028}
1029
Alexey Frunzef63f5692016-12-13 17:43:11 -08001030Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1031 dex::StringIndex string_index) {
1032 return boot_image_string_patches_.GetOrCreate(
1033 StringReference(&dex_file, string_index),
1034 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1035}
1036
1037Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1038 dex::TypeIndex type_index) {
1039 return boot_image_type_patches_.GetOrCreate(
1040 TypeReference(&dex_file, type_index),
1041 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1042}
1043
1044Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
1045 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1046 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1047 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1048}
1049
Alexey Frunze19f6c692016-11-30 19:19:55 -08001050void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1051 GpuRegister out) {
1052 __ Bind(&info->pc_rel_label);
1053 // Add the high half of a 32-bit offset to PC.
1054 __ Auipc(out, /* placeholder */ 0x1234);
1055 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001056 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001057}
1058
Alexey Frunze627c1a02017-01-30 19:28:14 -08001059Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1060 dex::StringIndex string_index,
1061 Handle<mirror::String> handle) {
1062 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1063 reinterpret_cast64<uint64_t>(handle.GetReference()));
1064 return jit_string_patches_.GetOrCreate(
1065 StringReference(&dex_file, string_index),
1066 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1067}
1068
1069Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1070 dex::TypeIndex type_index,
1071 Handle<mirror::Class> handle) {
1072 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1073 reinterpret_cast64<uint64_t>(handle.GetReference()));
1074 return jit_class_patches_.GetOrCreate(
1075 TypeReference(&dex_file, type_index),
1076 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1077}
1078
1079void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1080 const uint8_t* roots_data,
1081 const Literal* literal,
1082 uint64_t index_in_table) const {
1083 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1084 uintptr_t address =
1085 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1086 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1087}
1088
1089void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1090 for (const auto& entry : jit_string_patches_) {
1091 const auto& it = jit_string_roots_.find(entry.first);
1092 DCHECK(it != jit_string_roots_.end());
1093 PatchJitRootUse(code, roots_data, entry.second, it->second);
1094 }
1095 for (const auto& entry : jit_class_patches_) {
1096 const auto& it = jit_class_roots_.find(entry.first);
1097 DCHECK(it != jit_class_roots_.end());
1098 PatchJitRootUse(code, roots_data, entry.second, it->second);
1099 }
1100}
1101
David Brazdil58282f42016-01-14 12:45:10 +00001102void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001103 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1104 blocked_core_registers_[ZERO] = true;
1105 blocked_core_registers_[K0] = true;
1106 blocked_core_registers_[K1] = true;
1107 blocked_core_registers_[GP] = true;
1108 blocked_core_registers_[SP] = true;
1109 blocked_core_registers_[RA] = true;
1110
Lazar Trsicd9672662015-09-03 17:33:01 +02001111 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1112 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113 blocked_core_registers_[AT] = true;
1114 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001115 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001116 blocked_fpu_registers_[FTMP] = true;
1117
1118 // Reserve suspend and thread registers.
1119 blocked_core_registers_[S0] = true;
1120 blocked_core_registers_[TR] = true;
1121
1122 // Reserve T9 for function calls
1123 blocked_core_registers_[T9] = true;
1124
Goran Jakovljevic782be112016-06-21 12:39:04 +02001125 if (GetGraph()->IsDebuggable()) {
1126 // Stubs do not save callee-save floating point registers. If the graph
1127 // is debuggable, we need to deal with these registers differently. For
1128 // now, just block them.
1129 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1130 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1131 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132 }
1133}
1134
Alexey Frunze4dda3372015-06-01 18:31:49 -07001135size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1136 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001137 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001138}
1139
1140size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1141 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001142 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001143}
1144
1145size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1146 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001147 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001148}
1149
1150size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1151 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001152 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153}
1154
1155void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001156 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157}
1158
1159void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001160 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161}
1162
Calin Juravle175dc732015-08-25 15:42:32 +01001163void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001164 HInstruction* instruction,
1165 uint32_t dex_pc,
1166 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001167 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001168 __ LoadFromOffset(kLoadDoubleword,
1169 T9,
1170 TR,
1171 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001172 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001173 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001174 if (EntrypointRequiresStackMap(entrypoint)) {
1175 RecordPcInfo(instruction, dex_pc, slow_path);
1176 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001177}
1178
1179void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1180 GpuRegister class_reg) {
1181 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1182 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1183 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1184 // TODO: barrier needed?
1185 __ Bind(slow_path->GetExitLabel());
1186}
1187
1188void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1189 __ Sync(0); // only stype 0 is supported
1190}
1191
1192void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1193 HBasicBlock* successor) {
1194 SuspendCheckSlowPathMIPS64* slow_path =
1195 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1196 codegen_->AddSlowPath(slow_path);
1197
1198 __ LoadFromOffset(kLoadUnsignedHalfword,
1199 TMP,
1200 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001201 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001202 if (successor == nullptr) {
1203 __ Bnezc(TMP, slow_path->GetEntryLabel());
1204 __ Bind(slow_path->GetReturnLabel());
1205 } else {
1206 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001207 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 // slow_path will return to GetLabelOf(successor).
1209 }
1210}
1211
1212InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1213 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001214 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 assembler_(codegen->GetAssembler()),
1216 codegen_(codegen) {}
1217
1218void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1219 DCHECK_EQ(instruction->InputCount(), 2U);
1220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1221 Primitive::Type type = instruction->GetResultType();
1222 switch (type) {
1223 case Primitive::kPrimInt:
1224 case Primitive::kPrimLong: {
1225 locations->SetInAt(0, Location::RequiresRegister());
1226 HInstruction* right = instruction->InputAt(1);
1227 bool can_use_imm = false;
1228 if (right->IsConstant()) {
1229 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1230 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1231 can_use_imm = IsUint<16>(imm);
1232 } else if (instruction->IsAdd()) {
1233 can_use_imm = IsInt<16>(imm);
1234 } else {
1235 DCHECK(instruction->IsSub());
1236 can_use_imm = IsInt<16>(-imm);
1237 }
1238 }
1239 if (can_use_imm)
1240 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1241 else
1242 locations->SetInAt(1, Location::RequiresRegister());
1243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1244 }
1245 break;
1246
1247 case Primitive::kPrimFloat:
1248 case Primitive::kPrimDouble:
1249 locations->SetInAt(0, Location::RequiresFpuRegister());
1250 locations->SetInAt(1, Location::RequiresFpuRegister());
1251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1252 break;
1253
1254 default:
1255 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1256 }
1257}
1258
1259void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1260 Primitive::Type type = instruction->GetType();
1261 LocationSummary* locations = instruction->GetLocations();
1262
1263 switch (type) {
1264 case Primitive::kPrimInt:
1265 case Primitive::kPrimLong: {
1266 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1267 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1268 Location rhs_location = locations->InAt(1);
1269
1270 GpuRegister rhs_reg = ZERO;
1271 int64_t rhs_imm = 0;
1272 bool use_imm = rhs_location.IsConstant();
1273 if (use_imm) {
1274 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1275 } else {
1276 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1277 }
1278
1279 if (instruction->IsAnd()) {
1280 if (use_imm)
1281 __ Andi(dst, lhs, rhs_imm);
1282 else
1283 __ And(dst, lhs, rhs_reg);
1284 } else if (instruction->IsOr()) {
1285 if (use_imm)
1286 __ Ori(dst, lhs, rhs_imm);
1287 else
1288 __ Or(dst, lhs, rhs_reg);
1289 } else if (instruction->IsXor()) {
1290 if (use_imm)
1291 __ Xori(dst, lhs, rhs_imm);
1292 else
1293 __ Xor(dst, lhs, rhs_reg);
1294 } else if (instruction->IsAdd()) {
1295 if (type == Primitive::kPrimInt) {
1296 if (use_imm)
1297 __ Addiu(dst, lhs, rhs_imm);
1298 else
1299 __ Addu(dst, lhs, rhs_reg);
1300 } else {
1301 if (use_imm)
1302 __ Daddiu(dst, lhs, rhs_imm);
1303 else
1304 __ Daddu(dst, lhs, rhs_reg);
1305 }
1306 } else {
1307 DCHECK(instruction->IsSub());
1308 if (type == Primitive::kPrimInt) {
1309 if (use_imm)
1310 __ Addiu(dst, lhs, -rhs_imm);
1311 else
1312 __ Subu(dst, lhs, rhs_reg);
1313 } else {
1314 if (use_imm)
1315 __ Daddiu(dst, lhs, -rhs_imm);
1316 else
1317 __ Dsubu(dst, lhs, rhs_reg);
1318 }
1319 }
1320 break;
1321 }
1322 case Primitive::kPrimFloat:
1323 case Primitive::kPrimDouble: {
1324 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1325 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1326 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1327 if (instruction->IsAdd()) {
1328 if (type == Primitive::kPrimFloat)
1329 __ AddS(dst, lhs, rhs);
1330 else
1331 __ AddD(dst, lhs, rhs);
1332 } else if (instruction->IsSub()) {
1333 if (type == Primitive::kPrimFloat)
1334 __ SubS(dst, lhs, rhs);
1335 else
1336 __ SubD(dst, lhs, rhs);
1337 } else {
1338 LOG(FATAL) << "Unexpected floating-point binary operation";
1339 }
1340 break;
1341 }
1342 default:
1343 LOG(FATAL) << "Unexpected binary operation type " << type;
1344 }
1345}
1346
1347void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001348 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001349
1350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1351 Primitive::Type type = instr->GetResultType();
1352 switch (type) {
1353 case Primitive::kPrimInt:
1354 case Primitive::kPrimLong: {
1355 locations->SetInAt(0, Location::RequiresRegister());
1356 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001358 break;
1359 }
1360 default:
1361 LOG(FATAL) << "Unexpected shift type " << type;
1362 }
1363}
1364
1365void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001366 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001367 LocationSummary* locations = instr->GetLocations();
1368 Primitive::Type type = instr->GetType();
1369
1370 switch (type) {
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimLong: {
1373 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1374 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1375 Location rhs_location = locations->InAt(1);
1376
1377 GpuRegister rhs_reg = ZERO;
1378 int64_t rhs_imm = 0;
1379 bool use_imm = rhs_location.IsConstant();
1380 if (use_imm) {
1381 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1382 } else {
1383 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1384 }
1385
1386 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001387 uint32_t shift_value = rhs_imm &
1388 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001389
Alexey Frunze92d90602015-12-18 18:16:36 -08001390 if (shift_value == 0) {
1391 if (dst != lhs) {
1392 __ Move(dst, lhs);
1393 }
1394 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001395 if (instr->IsShl()) {
1396 __ Sll(dst, lhs, shift_value);
1397 } else if (instr->IsShr()) {
1398 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001399 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001400 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001401 } else {
1402 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001403 }
1404 } else {
1405 if (shift_value < 32) {
1406 if (instr->IsShl()) {
1407 __ Dsll(dst, lhs, shift_value);
1408 } else if (instr->IsShr()) {
1409 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001410 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001411 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001412 } else {
1413 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 }
1415 } else {
1416 shift_value -= 32;
1417 if (instr->IsShl()) {
1418 __ Dsll32(dst, lhs, shift_value);
1419 } else if (instr->IsShr()) {
1420 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001421 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001422 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001423 } else {
1424 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001425 }
1426 }
1427 }
1428 } else {
1429 if (type == Primitive::kPrimInt) {
1430 if (instr->IsShl()) {
1431 __ Sllv(dst, lhs, rhs_reg);
1432 } else if (instr->IsShr()) {
1433 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001434 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001435 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001436 } else {
1437 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001438 }
1439 } else {
1440 if (instr->IsShl()) {
1441 __ Dsllv(dst, lhs, rhs_reg);
1442 } else if (instr->IsShr()) {
1443 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001444 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001445 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001446 } else {
1447 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001448 }
1449 }
1450 }
1451 break;
1452 }
1453 default:
1454 LOG(FATAL) << "Unexpected shift operation type " << type;
1455 }
1456}
1457
1458void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1459 HandleBinaryOp(instruction);
1460}
1461
1462void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1463 HandleBinaryOp(instruction);
1464}
1465
1466void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1467 HandleBinaryOp(instruction);
1468}
1469
1470void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1471 HandleBinaryOp(instruction);
1472}
1473
1474void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1475 LocationSummary* locations =
1476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1479 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1480 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1481 } else {
1482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1483 }
1484}
1485
1486void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1487 LocationSummary* locations = instruction->GetLocations();
1488 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1489 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001490 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001491
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001492 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001493 switch (type) {
1494 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001495 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1496 if (index.IsConstant()) {
1497 size_t offset =
1498 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1499 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1500 } else {
1501 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1502 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1503 }
1504 break;
1505 }
1506
1507 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001508 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1509 if (index.IsConstant()) {
1510 size_t offset =
1511 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1512 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1513 } else {
1514 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1515 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1516 }
1517 break;
1518 }
1519
1520 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001521 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1522 if (index.IsConstant()) {
1523 size_t offset =
1524 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1525 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1526 } else {
1527 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1528 __ Daddu(TMP, obj, TMP);
1529 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1530 }
1531 break;
1532 }
1533
1534 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001535 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1536 if (index.IsConstant()) {
1537 size_t offset =
1538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1539 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1540 } else {
1541 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1542 __ Daddu(TMP, obj, TMP);
1543 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1544 }
1545 break;
1546 }
1547
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimNot: {
1550 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001551 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1552 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1553 if (index.IsConstant()) {
1554 size_t offset =
1555 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1556 __ LoadFromOffset(load_type, out, obj, offset);
1557 } else {
1558 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1559 __ Daddu(TMP, obj, TMP);
1560 __ LoadFromOffset(load_type, out, TMP, data_offset);
1561 }
1562 break;
1563 }
1564
1565 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001566 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1567 if (index.IsConstant()) {
1568 size_t offset =
1569 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1570 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1571 } else {
1572 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1573 __ Daddu(TMP, obj, TMP);
1574 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1575 }
1576 break;
1577 }
1578
1579 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001580 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1581 if (index.IsConstant()) {
1582 size_t offset =
1583 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1584 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1585 } else {
1586 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1587 __ Daddu(TMP, obj, TMP);
1588 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1589 }
1590 break;
1591 }
1592
1593 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001594 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1595 if (index.IsConstant()) {
1596 size_t offset =
1597 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1598 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1599 } else {
1600 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1601 __ Daddu(TMP, obj, TMP);
1602 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1603 }
1604 break;
1605 }
1606
1607 case Primitive::kPrimVoid:
1608 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1609 UNREACHABLE();
1610 }
1611 codegen_->MaybeRecordImplicitNullCheck(instruction);
1612}
1613
1614void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1616 locations->SetInAt(0, Location::RequiresRegister());
1617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1618}
1619
1620void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1621 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001622 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001623 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1624 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1625 __ LoadFromOffset(kLoadWord, out, obj, offset);
1626 codegen_->MaybeRecordImplicitNullCheck(instruction);
1627}
1628
1629void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001630 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001631 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1632 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001633 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001634 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001635 InvokeRuntimeCallingConvention calling_convention;
1636 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1637 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1638 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1639 } else {
1640 locations->SetInAt(0, Location::RequiresRegister());
1641 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1642 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1643 locations->SetInAt(2, Location::RequiresFpuRegister());
1644 } else {
1645 locations->SetInAt(2, Location::RequiresRegister());
1646 }
1647 }
1648}
1649
1650void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1651 LocationSummary* locations = instruction->GetLocations();
1652 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1653 Location index = locations->InAt(1);
1654 Primitive::Type value_type = instruction->GetComponentType();
1655 bool needs_runtime_call = locations->WillCall();
1656 bool needs_write_barrier =
1657 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1658
1659 switch (value_type) {
1660 case Primitive::kPrimBoolean:
1661 case Primitive::kPrimByte: {
1662 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1663 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1664 if (index.IsConstant()) {
1665 size_t offset =
1666 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1667 __ StoreToOffset(kStoreByte, value, obj, offset);
1668 } else {
1669 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1670 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1671 }
1672 break;
1673 }
1674
1675 case Primitive::kPrimShort:
1676 case Primitive::kPrimChar: {
1677 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1678 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1679 if (index.IsConstant()) {
1680 size_t offset =
1681 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1682 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1683 } else {
1684 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1685 __ Daddu(TMP, obj, TMP);
1686 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1687 }
1688 break;
1689 }
1690
1691 case Primitive::kPrimInt:
1692 case Primitive::kPrimNot: {
1693 if (!needs_runtime_call) {
1694 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1695 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1696 if (index.IsConstant()) {
1697 size_t offset =
1698 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1699 __ StoreToOffset(kStoreWord, value, obj, offset);
1700 } else {
1701 DCHECK(index.IsRegister()) << index;
1702 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1703 __ Daddu(TMP, obj, TMP);
1704 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1705 }
1706 codegen_->MaybeRecordImplicitNullCheck(instruction);
1707 if (needs_write_barrier) {
1708 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001709 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710 }
1711 } else {
1712 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001713 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001714 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001715 }
1716 break;
1717 }
1718
1719 case Primitive::kPrimLong: {
1720 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1721 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1722 if (index.IsConstant()) {
1723 size_t offset =
1724 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1725 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1726 } else {
1727 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1728 __ Daddu(TMP, obj, TMP);
1729 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1730 }
1731 break;
1732 }
1733
1734 case Primitive::kPrimFloat: {
1735 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1736 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1737 DCHECK(locations->InAt(2).IsFpuRegister());
1738 if (index.IsConstant()) {
1739 size_t offset =
1740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1741 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1742 } else {
1743 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1744 __ Daddu(TMP, obj, TMP);
1745 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1746 }
1747 break;
1748 }
1749
1750 case Primitive::kPrimDouble: {
1751 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1752 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1753 DCHECK(locations->InAt(2).IsFpuRegister());
1754 if (index.IsConstant()) {
1755 size_t offset =
1756 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1757 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1758 } else {
1759 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1760 __ Daddu(TMP, obj, TMP);
1761 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1762 }
1763 break;
1764 }
1765
1766 case Primitive::kPrimVoid:
1767 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1768 UNREACHABLE();
1769 }
1770
1771 // Ints and objects are handled in the switch.
1772 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1773 codegen_->MaybeRecordImplicitNullCheck(instruction);
1774 }
1775}
1776
1777void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001778 RegisterSet caller_saves = RegisterSet::Empty();
1779 InvokeRuntimeCallingConvention calling_convention;
1780 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1781 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1782 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 locations->SetInAt(0, Location::RequiresRegister());
1784 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001785}
1786
1787void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1788 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001789 BoundsCheckSlowPathMIPS64* slow_path =
1790 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791 codegen_->AddSlowPath(slow_path);
1792
1793 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1794 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1795
1796 // length is limited by the maximum positive signed 32-bit integer.
1797 // Unsigned comparison of length and index checks for index < 0
1798 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001799 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001800}
1801
1802void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1804 instruction,
1805 LocationSummary::kCallOnSlowPath);
1806 locations->SetInAt(0, Location::RequiresRegister());
1807 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001808 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 locations->AddTemp(Location::RequiresRegister());
1810}
1811
1812void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1813 LocationSummary* locations = instruction->GetLocations();
1814 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1815 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1816 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1817
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001818 SlowPathCodeMIPS64* slow_path =
1819 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820 codegen_->AddSlowPath(slow_path);
1821
1822 // TODO: avoid this check if we know obj is not null.
1823 __ Beqzc(obj, slow_path->GetExitLabel());
1824 // Compare the class of `obj` with `cls`.
1825 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1826 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1827 __ Bind(slow_path->GetExitLabel());
1828}
1829
1830void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1833 locations->SetInAt(0, Location::RequiresRegister());
1834 if (check->HasUses()) {
1835 locations->SetOut(Location::SameAsFirstInput());
1836 }
1837}
1838
1839void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1840 // We assume the class is not null.
1841 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1842 check->GetLoadClass(),
1843 check,
1844 check->GetDexPc(),
1845 true);
1846 codegen_->AddSlowPath(slow_path);
1847 GenerateClassInitializationCheck(slow_path,
1848 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1849}
1850
1851void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1852 Primitive::Type in_type = compare->InputAt(0)->GetType();
1853
Alexey Frunze299a9392015-12-08 16:08:02 -08001854 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855
1856 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001857 case Primitive::kPrimBoolean:
1858 case Primitive::kPrimByte:
1859 case Primitive::kPrimShort:
1860 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001861 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862 case Primitive::kPrimLong:
1863 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001864 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1866 break;
1867
1868 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001869 case Primitive::kPrimDouble:
1870 locations->SetInAt(0, Location::RequiresFpuRegister());
1871 locations->SetInAt(1, Location::RequiresFpuRegister());
1872 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001874
1875 default:
1876 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1877 }
1878}
1879
1880void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1881 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001882 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001883 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1884
1885 // 0 if: left == right
1886 // 1 if: left > right
1887 // -1 if: left < right
1888 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001889 case Primitive::kPrimBoolean:
1890 case Primitive::kPrimByte:
1891 case Primitive::kPrimShort:
1892 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001893 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001894 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001895 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001896 Location rhs_location = locations->InAt(1);
1897 bool use_imm = rhs_location.IsConstant();
1898 GpuRegister rhs = ZERO;
1899 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001900 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001901 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1902 if (value != 0) {
1903 rhs = AT;
1904 __ LoadConst64(rhs, value);
1905 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001906 } else {
1907 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1908 if (value != 0) {
1909 rhs = AT;
1910 __ LoadConst32(rhs, value);
1911 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001912 }
1913 } else {
1914 rhs = rhs_location.AsRegister<GpuRegister>();
1915 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001916 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001917 __ Slt(res, rhs, lhs);
1918 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001919 break;
1920 }
1921
Alexey Frunze299a9392015-12-08 16:08:02 -08001922 case Primitive::kPrimFloat: {
1923 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1924 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1925 Mips64Label done;
1926 __ CmpEqS(FTMP, lhs, rhs);
1927 __ LoadConst32(res, 0);
1928 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001929 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001930 __ CmpLtS(FTMP, lhs, rhs);
1931 __ LoadConst32(res, -1);
1932 __ Bc1nez(FTMP, &done);
1933 __ LoadConst32(res, 1);
1934 } else {
1935 __ CmpLtS(FTMP, rhs, lhs);
1936 __ LoadConst32(res, 1);
1937 __ Bc1nez(FTMP, &done);
1938 __ LoadConst32(res, -1);
1939 }
1940 __ Bind(&done);
1941 break;
1942 }
1943
Alexey Frunze4dda3372015-06-01 18:31:49 -07001944 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001945 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1946 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1947 Mips64Label done;
1948 __ CmpEqD(FTMP, lhs, rhs);
1949 __ LoadConst32(res, 0);
1950 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001951 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001952 __ CmpLtD(FTMP, lhs, rhs);
1953 __ LoadConst32(res, -1);
1954 __ Bc1nez(FTMP, &done);
1955 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001956 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001957 __ CmpLtD(FTMP, rhs, lhs);
1958 __ LoadConst32(res, 1);
1959 __ Bc1nez(FTMP, &done);
1960 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001961 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001962 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001963 break;
1964 }
1965
1966 default:
1967 LOG(FATAL) << "Unimplemented compare type " << in_type;
1968 }
1969}
1970
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001973 switch (instruction->InputAt(0)->GetType()) {
1974 default:
1975 case Primitive::kPrimLong:
1976 locations->SetInAt(0, Location::RequiresRegister());
1977 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1978 break;
1979
1980 case Primitive::kPrimFloat:
1981 case Primitive::kPrimDouble:
1982 locations->SetInAt(0, Location::RequiresFpuRegister());
1983 locations->SetInAt(1, Location::RequiresFpuRegister());
1984 break;
1985 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001986 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1988 }
1989}
1990
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001992 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001993 return;
1994 }
1995
Alexey Frunze299a9392015-12-08 16:08:02 -08001996 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001997 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001998 switch (type) {
1999 default:
2000 // Integer case.
2001 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2002 return;
2003 case Primitive::kPrimLong:
2004 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2005 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002006 case Primitive::kPrimFloat:
2007 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002008 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2009 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002010 }
2011}
2012
Alexey Frunzec857c742015-09-23 15:12:39 -07002013void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2014 DCHECK(instruction->IsDiv() || instruction->IsRem());
2015 Primitive::Type type = instruction->GetResultType();
2016
2017 LocationSummary* locations = instruction->GetLocations();
2018 Location second = locations->InAt(1);
2019 DCHECK(second.IsConstant());
2020
2021 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2022 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2023 int64_t imm = Int64FromConstant(second.GetConstant());
2024 DCHECK(imm == 1 || imm == -1);
2025
2026 if (instruction->IsRem()) {
2027 __ Move(out, ZERO);
2028 } else {
2029 if (imm == -1) {
2030 if (type == Primitive::kPrimInt) {
2031 __ Subu(out, ZERO, dividend);
2032 } else {
2033 DCHECK_EQ(type, Primitive::kPrimLong);
2034 __ Dsubu(out, ZERO, dividend);
2035 }
2036 } else if (out != dividend) {
2037 __ Move(out, dividend);
2038 }
2039 }
2040}
2041
2042void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2043 DCHECK(instruction->IsDiv() || instruction->IsRem());
2044 Primitive::Type type = instruction->GetResultType();
2045
2046 LocationSummary* locations = instruction->GetLocations();
2047 Location second = locations->InAt(1);
2048 DCHECK(second.IsConstant());
2049
2050 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2051 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2052 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002053 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002054 int ctz_imm = CTZ(abs_imm);
2055
2056 if (instruction->IsDiv()) {
2057 if (type == Primitive::kPrimInt) {
2058 if (ctz_imm == 1) {
2059 // Fast path for division by +/-2, which is very common.
2060 __ Srl(TMP, dividend, 31);
2061 } else {
2062 __ Sra(TMP, dividend, 31);
2063 __ Srl(TMP, TMP, 32 - ctz_imm);
2064 }
2065 __ Addu(out, dividend, TMP);
2066 __ Sra(out, out, ctz_imm);
2067 if (imm < 0) {
2068 __ Subu(out, ZERO, out);
2069 }
2070 } else {
2071 DCHECK_EQ(type, Primitive::kPrimLong);
2072 if (ctz_imm == 1) {
2073 // Fast path for division by +/-2, which is very common.
2074 __ Dsrl32(TMP, dividend, 31);
2075 } else {
2076 __ Dsra32(TMP, dividend, 31);
2077 if (ctz_imm > 32) {
2078 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2079 } else {
2080 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2081 }
2082 }
2083 __ Daddu(out, dividend, TMP);
2084 if (ctz_imm < 32) {
2085 __ Dsra(out, out, ctz_imm);
2086 } else {
2087 __ Dsra32(out, out, ctz_imm - 32);
2088 }
2089 if (imm < 0) {
2090 __ Dsubu(out, ZERO, out);
2091 }
2092 }
2093 } else {
2094 if (type == Primitive::kPrimInt) {
2095 if (ctz_imm == 1) {
2096 // Fast path for modulo +/-2, which is very common.
2097 __ Sra(TMP, dividend, 31);
2098 __ Subu(out, dividend, TMP);
2099 __ Andi(out, out, 1);
2100 __ Addu(out, out, TMP);
2101 } else {
2102 __ Sra(TMP, dividend, 31);
2103 __ Srl(TMP, TMP, 32 - ctz_imm);
2104 __ Addu(out, dividend, TMP);
2105 if (IsUint<16>(abs_imm - 1)) {
2106 __ Andi(out, out, abs_imm - 1);
2107 } else {
2108 __ Sll(out, out, 32 - ctz_imm);
2109 __ Srl(out, out, 32 - ctz_imm);
2110 }
2111 __ Subu(out, out, TMP);
2112 }
2113 } else {
2114 DCHECK_EQ(type, Primitive::kPrimLong);
2115 if (ctz_imm == 1) {
2116 // Fast path for modulo +/-2, which is very common.
2117 __ Dsra32(TMP, dividend, 31);
2118 __ Dsubu(out, dividend, TMP);
2119 __ Andi(out, out, 1);
2120 __ Daddu(out, out, TMP);
2121 } else {
2122 __ Dsra32(TMP, dividend, 31);
2123 if (ctz_imm > 32) {
2124 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2125 } else {
2126 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2127 }
2128 __ Daddu(out, dividend, TMP);
2129 if (IsUint<16>(abs_imm - 1)) {
2130 __ Andi(out, out, abs_imm - 1);
2131 } else {
2132 if (ctz_imm > 32) {
2133 __ Dsll(out, out, 64 - ctz_imm);
2134 __ Dsrl(out, out, 64 - ctz_imm);
2135 } else {
2136 __ Dsll32(out, out, 32 - ctz_imm);
2137 __ Dsrl32(out, out, 32 - ctz_imm);
2138 }
2139 }
2140 __ Dsubu(out, out, TMP);
2141 }
2142 }
2143 }
2144}
2145
2146void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2147 DCHECK(instruction->IsDiv() || instruction->IsRem());
2148
2149 LocationSummary* locations = instruction->GetLocations();
2150 Location second = locations->InAt(1);
2151 DCHECK(second.IsConstant());
2152
2153 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2154 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2155 int64_t imm = Int64FromConstant(second.GetConstant());
2156
2157 Primitive::Type type = instruction->GetResultType();
2158 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2159
2160 int64_t magic;
2161 int shift;
2162 CalculateMagicAndShiftForDivRem(imm,
2163 (type == Primitive::kPrimLong),
2164 &magic,
2165 &shift);
2166
2167 if (type == Primitive::kPrimInt) {
2168 __ LoadConst32(TMP, magic);
2169 __ MuhR6(TMP, dividend, TMP);
2170
2171 if (imm > 0 && magic < 0) {
2172 __ Addu(TMP, TMP, dividend);
2173 } else if (imm < 0 && magic > 0) {
2174 __ Subu(TMP, TMP, dividend);
2175 }
2176
2177 if (shift != 0) {
2178 __ Sra(TMP, TMP, shift);
2179 }
2180
2181 if (instruction->IsDiv()) {
2182 __ Sra(out, TMP, 31);
2183 __ Subu(out, TMP, out);
2184 } else {
2185 __ Sra(AT, TMP, 31);
2186 __ Subu(AT, TMP, AT);
2187 __ LoadConst32(TMP, imm);
2188 __ MulR6(TMP, AT, TMP);
2189 __ Subu(out, dividend, TMP);
2190 }
2191 } else {
2192 __ LoadConst64(TMP, magic);
2193 __ Dmuh(TMP, dividend, TMP);
2194
2195 if (imm > 0 && magic < 0) {
2196 __ Daddu(TMP, TMP, dividend);
2197 } else if (imm < 0 && magic > 0) {
2198 __ Dsubu(TMP, TMP, dividend);
2199 }
2200
2201 if (shift >= 32) {
2202 __ Dsra32(TMP, TMP, shift - 32);
2203 } else if (shift > 0) {
2204 __ Dsra(TMP, TMP, shift);
2205 }
2206
2207 if (instruction->IsDiv()) {
2208 __ Dsra32(out, TMP, 31);
2209 __ Dsubu(out, TMP, out);
2210 } else {
2211 __ Dsra32(AT, TMP, 31);
2212 __ Dsubu(AT, TMP, AT);
2213 __ LoadConst64(TMP, imm);
2214 __ Dmul(TMP, AT, TMP);
2215 __ Dsubu(out, dividend, TMP);
2216 }
2217 }
2218}
2219
2220void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2221 DCHECK(instruction->IsDiv() || instruction->IsRem());
2222 Primitive::Type type = instruction->GetResultType();
2223 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2224
2225 LocationSummary* locations = instruction->GetLocations();
2226 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2227 Location second = locations->InAt(1);
2228
2229 if (second.IsConstant()) {
2230 int64_t imm = Int64FromConstant(second.GetConstant());
2231 if (imm == 0) {
2232 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2233 } else if (imm == 1 || imm == -1) {
2234 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002235 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002236 DivRemByPowerOfTwo(instruction);
2237 } else {
2238 DCHECK(imm <= -2 || imm >= 2);
2239 GenerateDivRemWithAnyConstant(instruction);
2240 }
2241 } else {
2242 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2243 GpuRegister divisor = second.AsRegister<GpuRegister>();
2244 if (instruction->IsDiv()) {
2245 if (type == Primitive::kPrimInt)
2246 __ DivR6(out, dividend, divisor);
2247 else
2248 __ Ddiv(out, dividend, divisor);
2249 } else {
2250 if (type == Primitive::kPrimInt)
2251 __ ModR6(out, dividend, divisor);
2252 else
2253 __ Dmod(out, dividend, divisor);
2254 }
2255 }
2256}
2257
Alexey Frunze4dda3372015-06-01 18:31:49 -07002258void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2259 LocationSummary* locations =
2260 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2261 switch (div->GetResultType()) {
2262 case Primitive::kPrimInt:
2263 case Primitive::kPrimLong:
2264 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002265 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2267 break;
2268
2269 case Primitive::kPrimFloat:
2270 case Primitive::kPrimDouble:
2271 locations->SetInAt(0, Location::RequiresFpuRegister());
2272 locations->SetInAt(1, Location::RequiresFpuRegister());
2273 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2274 break;
2275
2276 default:
2277 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2278 }
2279}
2280
2281void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2282 Primitive::Type type = instruction->GetType();
2283 LocationSummary* locations = instruction->GetLocations();
2284
2285 switch (type) {
2286 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002287 case Primitive::kPrimLong:
2288 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002289 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002290 case Primitive::kPrimFloat:
2291 case Primitive::kPrimDouble: {
2292 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2293 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2294 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2295 if (type == Primitive::kPrimFloat)
2296 __ DivS(dst, lhs, rhs);
2297 else
2298 __ DivD(dst, lhs, rhs);
2299 break;
2300 }
2301 default:
2302 LOG(FATAL) << "Unexpected div type " << type;
2303 }
2304}
2305
2306void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002307 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002308 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002309}
2310
2311void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2312 SlowPathCodeMIPS64* slow_path =
2313 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2314 codegen_->AddSlowPath(slow_path);
2315 Location value = instruction->GetLocations()->InAt(0);
2316
2317 Primitive::Type type = instruction->GetType();
2318
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002319 if (!Primitive::IsIntegralType(type)) {
2320 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002321 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002322 }
2323
2324 if (value.IsConstant()) {
2325 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2326 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002327 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002328 } else {
2329 // A division by a non-null constant is valid. We don't need to perform
2330 // any check, so simply fall through.
2331 }
2332 } else {
2333 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2334 }
2335}
2336
2337void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2338 LocationSummary* locations =
2339 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2340 locations->SetOut(Location::ConstantLocation(constant));
2341}
2342
2343void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2344 // Will be generated at use site.
2345}
2346
2347void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2348 exit->SetLocations(nullptr);
2349}
2350
2351void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2352}
2353
2354void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2355 LocationSummary* locations =
2356 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2357 locations->SetOut(Location::ConstantLocation(constant));
2358}
2359
2360void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2361 // Will be generated at use site.
2362}
2363
David Brazdilfc6a86a2015-06-26 10:33:45 +00002364void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002365 DCHECK(!successor->IsExitBlock());
2366 HBasicBlock* block = got->GetBlock();
2367 HInstruction* previous = got->GetPrevious();
2368 HLoopInformation* info = block->GetLoopInformation();
2369
2370 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2371 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2372 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2373 return;
2374 }
2375 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2376 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2377 }
2378 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002379 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002380 }
2381}
2382
David Brazdilfc6a86a2015-06-26 10:33:45 +00002383void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2384 got->SetLocations(nullptr);
2385}
2386
2387void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2388 HandleGoto(got, got->GetSuccessor());
2389}
2390
2391void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2392 try_boundary->SetLocations(nullptr);
2393}
2394
2395void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2396 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2397 if (!successor->IsExitBlock()) {
2398 HandleGoto(try_boundary, successor);
2399 }
2400}
2401
Alexey Frunze299a9392015-12-08 16:08:02 -08002402void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2403 bool is64bit,
2404 LocationSummary* locations) {
2405 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2406 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2407 Location rhs_location = locations->InAt(1);
2408 GpuRegister rhs_reg = ZERO;
2409 int64_t rhs_imm = 0;
2410 bool use_imm = rhs_location.IsConstant();
2411 if (use_imm) {
2412 if (is64bit) {
2413 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2414 } else {
2415 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2416 }
2417 } else {
2418 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2419 }
2420 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2421
2422 switch (cond) {
2423 case kCondEQ:
2424 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002425 if (use_imm && IsInt<16>(-rhs_imm)) {
2426 if (rhs_imm == 0) {
2427 if (cond == kCondEQ) {
2428 __ Sltiu(dst, lhs, 1);
2429 } else {
2430 __ Sltu(dst, ZERO, lhs);
2431 }
2432 } else {
2433 if (is64bit) {
2434 __ Daddiu(dst, lhs, -rhs_imm);
2435 } else {
2436 __ Addiu(dst, lhs, -rhs_imm);
2437 }
2438 if (cond == kCondEQ) {
2439 __ Sltiu(dst, dst, 1);
2440 } else {
2441 __ Sltu(dst, ZERO, dst);
2442 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002443 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002444 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002445 if (use_imm && IsUint<16>(rhs_imm)) {
2446 __ Xori(dst, lhs, rhs_imm);
2447 } else {
2448 if (use_imm) {
2449 rhs_reg = TMP;
2450 __ LoadConst64(rhs_reg, rhs_imm);
2451 }
2452 __ Xor(dst, lhs, rhs_reg);
2453 }
2454 if (cond == kCondEQ) {
2455 __ Sltiu(dst, dst, 1);
2456 } else {
2457 __ Sltu(dst, ZERO, dst);
2458 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002459 }
2460 break;
2461
2462 case kCondLT:
2463 case kCondGE:
2464 if (use_imm && IsInt<16>(rhs_imm)) {
2465 __ Slti(dst, lhs, rhs_imm);
2466 } else {
2467 if (use_imm) {
2468 rhs_reg = TMP;
2469 __ LoadConst64(rhs_reg, rhs_imm);
2470 }
2471 __ Slt(dst, lhs, rhs_reg);
2472 }
2473 if (cond == kCondGE) {
2474 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2475 // only the slt instruction but no sge.
2476 __ Xori(dst, dst, 1);
2477 }
2478 break;
2479
2480 case kCondLE:
2481 case kCondGT:
2482 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2483 // Simulate lhs <= rhs via lhs < rhs + 1.
2484 __ Slti(dst, lhs, rhs_imm_plus_one);
2485 if (cond == kCondGT) {
2486 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2487 // only the slti instruction but no sgti.
2488 __ Xori(dst, dst, 1);
2489 }
2490 } else {
2491 if (use_imm) {
2492 rhs_reg = TMP;
2493 __ LoadConst64(rhs_reg, rhs_imm);
2494 }
2495 __ Slt(dst, rhs_reg, lhs);
2496 if (cond == kCondLE) {
2497 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2498 // only the slt instruction but no sle.
2499 __ Xori(dst, dst, 1);
2500 }
2501 }
2502 break;
2503
2504 case kCondB:
2505 case kCondAE:
2506 if (use_imm && IsInt<16>(rhs_imm)) {
2507 // Sltiu sign-extends its 16-bit immediate operand before
2508 // the comparison and thus lets us compare directly with
2509 // unsigned values in the ranges [0, 0x7fff] and
2510 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2511 __ Sltiu(dst, lhs, rhs_imm);
2512 } else {
2513 if (use_imm) {
2514 rhs_reg = TMP;
2515 __ LoadConst64(rhs_reg, rhs_imm);
2516 }
2517 __ Sltu(dst, lhs, rhs_reg);
2518 }
2519 if (cond == kCondAE) {
2520 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2521 // only the sltu instruction but no sgeu.
2522 __ Xori(dst, dst, 1);
2523 }
2524 break;
2525
2526 case kCondBE:
2527 case kCondA:
2528 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2529 // Simulate lhs <= rhs via lhs < rhs + 1.
2530 // Note that this only works if rhs + 1 does not overflow
2531 // to 0, hence the check above.
2532 // Sltiu sign-extends its 16-bit immediate operand before
2533 // the comparison and thus lets us compare directly with
2534 // unsigned values in the ranges [0, 0x7fff] and
2535 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2536 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2537 if (cond == kCondA) {
2538 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2539 // only the sltiu instruction but no sgtiu.
2540 __ Xori(dst, dst, 1);
2541 }
2542 } else {
2543 if (use_imm) {
2544 rhs_reg = TMP;
2545 __ LoadConst64(rhs_reg, rhs_imm);
2546 }
2547 __ Sltu(dst, rhs_reg, lhs);
2548 if (cond == kCondBE) {
2549 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2550 // only the sltu instruction but no sleu.
2551 __ Xori(dst, dst, 1);
2552 }
2553 }
2554 break;
2555 }
2556}
2557
2558void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2559 bool is64bit,
2560 LocationSummary* locations,
2561 Mips64Label* label) {
2562 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2563 Location rhs_location = locations->InAt(1);
2564 GpuRegister rhs_reg = ZERO;
2565 int64_t rhs_imm = 0;
2566 bool use_imm = rhs_location.IsConstant();
2567 if (use_imm) {
2568 if (is64bit) {
2569 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2570 } else {
2571 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2572 }
2573 } else {
2574 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2575 }
2576
2577 if (use_imm && rhs_imm == 0) {
2578 switch (cond) {
2579 case kCondEQ:
2580 case kCondBE: // <= 0 if zero
2581 __ Beqzc(lhs, label);
2582 break;
2583 case kCondNE:
2584 case kCondA: // > 0 if non-zero
2585 __ Bnezc(lhs, label);
2586 break;
2587 case kCondLT:
2588 __ Bltzc(lhs, label);
2589 break;
2590 case kCondGE:
2591 __ Bgezc(lhs, label);
2592 break;
2593 case kCondLE:
2594 __ Blezc(lhs, label);
2595 break;
2596 case kCondGT:
2597 __ Bgtzc(lhs, label);
2598 break;
2599 case kCondB: // always false
2600 break;
2601 case kCondAE: // always true
2602 __ Bc(label);
2603 break;
2604 }
2605 } else {
2606 if (use_imm) {
2607 rhs_reg = TMP;
2608 __ LoadConst64(rhs_reg, rhs_imm);
2609 }
2610 switch (cond) {
2611 case kCondEQ:
2612 __ Beqc(lhs, rhs_reg, label);
2613 break;
2614 case kCondNE:
2615 __ Bnec(lhs, rhs_reg, label);
2616 break;
2617 case kCondLT:
2618 __ Bltc(lhs, rhs_reg, label);
2619 break;
2620 case kCondGE:
2621 __ Bgec(lhs, rhs_reg, label);
2622 break;
2623 case kCondLE:
2624 __ Bgec(rhs_reg, lhs, label);
2625 break;
2626 case kCondGT:
2627 __ Bltc(rhs_reg, lhs, label);
2628 break;
2629 case kCondB:
2630 __ Bltuc(lhs, rhs_reg, label);
2631 break;
2632 case kCondAE:
2633 __ Bgeuc(lhs, rhs_reg, label);
2634 break;
2635 case kCondBE:
2636 __ Bgeuc(rhs_reg, lhs, label);
2637 break;
2638 case kCondA:
2639 __ Bltuc(rhs_reg, lhs, label);
2640 break;
2641 }
2642 }
2643}
2644
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002645void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2646 bool gt_bias,
2647 Primitive::Type type,
2648 LocationSummary* locations) {
2649 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2650 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2651 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2652 if (type == Primitive::kPrimFloat) {
2653 switch (cond) {
2654 case kCondEQ:
2655 __ CmpEqS(FTMP, lhs, rhs);
2656 __ Mfc1(dst, FTMP);
2657 __ Andi(dst, dst, 1);
2658 break;
2659 case kCondNE:
2660 __ CmpEqS(FTMP, lhs, rhs);
2661 __ Mfc1(dst, FTMP);
2662 __ Addiu(dst, dst, 1);
2663 break;
2664 case kCondLT:
2665 if (gt_bias) {
2666 __ CmpLtS(FTMP, lhs, rhs);
2667 } else {
2668 __ CmpUltS(FTMP, lhs, rhs);
2669 }
2670 __ Mfc1(dst, FTMP);
2671 __ Andi(dst, dst, 1);
2672 break;
2673 case kCondLE:
2674 if (gt_bias) {
2675 __ CmpLeS(FTMP, lhs, rhs);
2676 } else {
2677 __ CmpUleS(FTMP, lhs, rhs);
2678 }
2679 __ Mfc1(dst, FTMP);
2680 __ Andi(dst, dst, 1);
2681 break;
2682 case kCondGT:
2683 if (gt_bias) {
2684 __ CmpUltS(FTMP, rhs, lhs);
2685 } else {
2686 __ CmpLtS(FTMP, rhs, lhs);
2687 }
2688 __ Mfc1(dst, FTMP);
2689 __ Andi(dst, dst, 1);
2690 break;
2691 case kCondGE:
2692 if (gt_bias) {
2693 __ CmpUleS(FTMP, rhs, lhs);
2694 } else {
2695 __ CmpLeS(FTMP, rhs, lhs);
2696 }
2697 __ Mfc1(dst, FTMP);
2698 __ Andi(dst, dst, 1);
2699 break;
2700 default:
2701 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2702 UNREACHABLE();
2703 }
2704 } else {
2705 DCHECK_EQ(type, Primitive::kPrimDouble);
2706 switch (cond) {
2707 case kCondEQ:
2708 __ CmpEqD(FTMP, lhs, rhs);
2709 __ Mfc1(dst, FTMP);
2710 __ Andi(dst, dst, 1);
2711 break;
2712 case kCondNE:
2713 __ CmpEqD(FTMP, lhs, rhs);
2714 __ Mfc1(dst, FTMP);
2715 __ Addiu(dst, dst, 1);
2716 break;
2717 case kCondLT:
2718 if (gt_bias) {
2719 __ CmpLtD(FTMP, lhs, rhs);
2720 } else {
2721 __ CmpUltD(FTMP, lhs, rhs);
2722 }
2723 __ Mfc1(dst, FTMP);
2724 __ Andi(dst, dst, 1);
2725 break;
2726 case kCondLE:
2727 if (gt_bias) {
2728 __ CmpLeD(FTMP, lhs, rhs);
2729 } else {
2730 __ CmpUleD(FTMP, lhs, rhs);
2731 }
2732 __ Mfc1(dst, FTMP);
2733 __ Andi(dst, dst, 1);
2734 break;
2735 case kCondGT:
2736 if (gt_bias) {
2737 __ CmpUltD(FTMP, rhs, lhs);
2738 } else {
2739 __ CmpLtD(FTMP, rhs, lhs);
2740 }
2741 __ Mfc1(dst, FTMP);
2742 __ Andi(dst, dst, 1);
2743 break;
2744 case kCondGE:
2745 if (gt_bias) {
2746 __ CmpUleD(FTMP, rhs, lhs);
2747 } else {
2748 __ CmpLeD(FTMP, rhs, lhs);
2749 }
2750 __ Mfc1(dst, FTMP);
2751 __ Andi(dst, dst, 1);
2752 break;
2753 default:
2754 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2755 UNREACHABLE();
2756 }
2757 }
2758}
2759
Alexey Frunze299a9392015-12-08 16:08:02 -08002760void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2761 bool gt_bias,
2762 Primitive::Type type,
2763 LocationSummary* locations,
2764 Mips64Label* label) {
2765 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2766 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2767 if (type == Primitive::kPrimFloat) {
2768 switch (cond) {
2769 case kCondEQ:
2770 __ CmpEqS(FTMP, lhs, rhs);
2771 __ Bc1nez(FTMP, label);
2772 break;
2773 case kCondNE:
2774 __ CmpEqS(FTMP, lhs, rhs);
2775 __ Bc1eqz(FTMP, label);
2776 break;
2777 case kCondLT:
2778 if (gt_bias) {
2779 __ CmpLtS(FTMP, lhs, rhs);
2780 } else {
2781 __ CmpUltS(FTMP, lhs, rhs);
2782 }
2783 __ Bc1nez(FTMP, label);
2784 break;
2785 case kCondLE:
2786 if (gt_bias) {
2787 __ CmpLeS(FTMP, lhs, rhs);
2788 } else {
2789 __ CmpUleS(FTMP, lhs, rhs);
2790 }
2791 __ Bc1nez(FTMP, label);
2792 break;
2793 case kCondGT:
2794 if (gt_bias) {
2795 __ CmpUltS(FTMP, rhs, lhs);
2796 } else {
2797 __ CmpLtS(FTMP, rhs, lhs);
2798 }
2799 __ Bc1nez(FTMP, label);
2800 break;
2801 case kCondGE:
2802 if (gt_bias) {
2803 __ CmpUleS(FTMP, rhs, lhs);
2804 } else {
2805 __ CmpLeS(FTMP, rhs, lhs);
2806 }
2807 __ Bc1nez(FTMP, label);
2808 break;
2809 default:
2810 LOG(FATAL) << "Unexpected non-floating-point condition";
2811 }
2812 } else {
2813 DCHECK_EQ(type, Primitive::kPrimDouble);
2814 switch (cond) {
2815 case kCondEQ:
2816 __ CmpEqD(FTMP, lhs, rhs);
2817 __ Bc1nez(FTMP, label);
2818 break;
2819 case kCondNE:
2820 __ CmpEqD(FTMP, lhs, rhs);
2821 __ Bc1eqz(FTMP, label);
2822 break;
2823 case kCondLT:
2824 if (gt_bias) {
2825 __ CmpLtD(FTMP, lhs, rhs);
2826 } else {
2827 __ CmpUltD(FTMP, lhs, rhs);
2828 }
2829 __ Bc1nez(FTMP, label);
2830 break;
2831 case kCondLE:
2832 if (gt_bias) {
2833 __ CmpLeD(FTMP, lhs, rhs);
2834 } else {
2835 __ CmpUleD(FTMP, lhs, rhs);
2836 }
2837 __ Bc1nez(FTMP, label);
2838 break;
2839 case kCondGT:
2840 if (gt_bias) {
2841 __ CmpUltD(FTMP, rhs, lhs);
2842 } else {
2843 __ CmpLtD(FTMP, rhs, lhs);
2844 }
2845 __ Bc1nez(FTMP, label);
2846 break;
2847 case kCondGE:
2848 if (gt_bias) {
2849 __ CmpUleD(FTMP, rhs, lhs);
2850 } else {
2851 __ CmpLeD(FTMP, rhs, lhs);
2852 }
2853 __ Bc1nez(FTMP, label);
2854 break;
2855 default:
2856 LOG(FATAL) << "Unexpected non-floating-point condition";
2857 }
2858 }
2859}
2860
Alexey Frunze4dda3372015-06-01 18:31:49 -07002861void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002862 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002863 Mips64Label* true_target,
2864 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002865 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002866
David Brazdil0debae72015-11-12 18:37:00 +00002867 if (true_target == nullptr && false_target == nullptr) {
2868 // Nothing to do. The code always falls through.
2869 return;
2870 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002871 // Constant condition, statically compared against "true" (integer value 1).
2872 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002873 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002874 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002876 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002877 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002878 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002879 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002881 }
David Brazdil0debae72015-11-12 18:37:00 +00002882 return;
2883 }
2884
2885 // The following code generates these patterns:
2886 // (1) true_target == nullptr && false_target != nullptr
2887 // - opposite condition true => branch to false_target
2888 // (2) true_target != nullptr && false_target == nullptr
2889 // - condition true => branch to true_target
2890 // (3) true_target != nullptr && false_target != nullptr
2891 // - condition true => branch to true_target
2892 // - branch to false_target
2893 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002894 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002895 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002896 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002897 if (true_target == nullptr) {
2898 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2899 } else {
2900 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2901 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002902 } else {
2903 // The condition instruction has not been materialized, use its inputs as
2904 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002905 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002906 Primitive::Type type = condition->InputAt(0)->GetType();
2907 LocationSummary* locations = cond->GetLocations();
2908 IfCondition if_cond = condition->GetCondition();
2909 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002910
David Brazdil0debae72015-11-12 18:37:00 +00002911 if (true_target == nullptr) {
2912 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002913 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002914 }
2915
Alexey Frunze299a9392015-12-08 16:08:02 -08002916 switch (type) {
2917 default:
2918 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2919 break;
2920 case Primitive::kPrimLong:
2921 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2922 break;
2923 case Primitive::kPrimFloat:
2924 case Primitive::kPrimDouble:
2925 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2926 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002927 }
2928 }
David Brazdil0debae72015-11-12 18:37:00 +00002929
2930 // If neither branch falls through (case 3), the conditional branch to `true_target`
2931 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2932 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002933 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002934 }
2935}
2936
2937void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002939 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940 locations->SetInAt(0, Location::RequiresRegister());
2941 }
2942}
2943
2944void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002945 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2946 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002947 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002948 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002949 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002950 nullptr : codegen_->GetLabelOf(false_successor);
2951 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952}
2953
2954void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2955 LocationSummary* locations = new (GetGraph()->GetArena())
2956 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002957 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002958 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002959 locations->SetInAt(0, Location::RequiresRegister());
2960 }
2961}
2962
2963void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002964 SlowPathCodeMIPS64* slow_path =
2965 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002966 GenerateTestAndBranch(deoptimize,
2967 /* condition_input_index */ 0,
2968 slow_path->GetEntryLabel(),
2969 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970}
2971
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002972void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2973 LocationSummary* locations = new (GetGraph()->GetArena())
2974 LocationSummary(flag, LocationSummary::kNoCall);
2975 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07002976}
2977
Goran Jakovljevicc6418422016-12-05 16:31:55 +01002978void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2979 __ LoadFromOffset(kLoadWord,
2980 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
2981 SP,
2982 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07002983}
2984
David Brazdil74eb1b22015-12-14 11:44:01 +00002985void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2986 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2987 if (Primitive::IsFloatingPointType(select->GetType())) {
2988 locations->SetInAt(0, Location::RequiresFpuRegister());
2989 locations->SetInAt(1, Location::RequiresFpuRegister());
2990 } else {
2991 locations->SetInAt(0, Location::RequiresRegister());
2992 locations->SetInAt(1, Location::RequiresRegister());
2993 }
2994 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2995 locations->SetInAt(2, Location::RequiresRegister());
2996 }
2997 locations->SetOut(Location::SameAsFirstInput());
2998}
2999
3000void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3001 LocationSummary* locations = select->GetLocations();
3002 Mips64Label false_target;
3003 GenerateTestAndBranch(select,
3004 /* condition_input_index */ 2,
3005 /* true_target */ nullptr,
3006 &false_target);
3007 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3008 __ Bind(&false_target);
3009}
3010
David Srbecky0cf44932015-12-09 14:09:59 +00003011void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3012 new (GetGraph()->GetArena()) LocationSummary(info);
3013}
3014
David Srbeckyd28f4a02016-03-14 17:14:24 +00003015void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3016 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003017}
3018
3019void CodeGeneratorMIPS64::GenerateNop() {
3020 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003021}
3022
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3024 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3025 LocationSummary* locations =
3026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3027 locations->SetInAt(0, Location::RequiresRegister());
3028 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3029 locations->SetOut(Location::RequiresFpuRegister());
3030 } else {
3031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3032 }
3033}
3034
3035void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3036 const FieldInfo& field_info) {
3037 Primitive::Type type = field_info.GetFieldType();
3038 LocationSummary* locations = instruction->GetLocations();
3039 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3040 LoadOperandType load_type = kLoadUnsignedByte;
3041 switch (type) {
3042 case Primitive::kPrimBoolean:
3043 load_type = kLoadUnsignedByte;
3044 break;
3045 case Primitive::kPrimByte:
3046 load_type = kLoadSignedByte;
3047 break;
3048 case Primitive::kPrimShort:
3049 load_type = kLoadSignedHalfword;
3050 break;
3051 case Primitive::kPrimChar:
3052 load_type = kLoadUnsignedHalfword;
3053 break;
3054 case Primitive::kPrimInt:
3055 case Primitive::kPrimFloat:
3056 load_type = kLoadWord;
3057 break;
3058 case Primitive::kPrimLong:
3059 case Primitive::kPrimDouble:
3060 load_type = kLoadDoubleword;
3061 break;
3062 case Primitive::kPrimNot:
3063 load_type = kLoadUnsignedWord;
3064 break;
3065 case Primitive::kPrimVoid:
3066 LOG(FATAL) << "Unreachable type " << type;
3067 UNREACHABLE();
3068 }
3069 if (!Primitive::IsFloatingPointType(type)) {
3070 DCHECK(locations->Out().IsRegister());
3071 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3072 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
3073 } else {
3074 DCHECK(locations->Out().IsFpuRegister());
3075 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3076 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
3077 }
3078
3079 codegen_->MaybeRecordImplicitNullCheck(instruction);
3080 // TODO: memory barrier?
3081}
3082
3083void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3084 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3087 locations->SetInAt(0, Location::RequiresRegister());
3088 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3089 locations->SetInAt(1, Location::RequiresFpuRegister());
3090 } else {
3091 locations->SetInAt(1, Location::RequiresRegister());
3092 }
3093}
3094
3095void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003096 const FieldInfo& field_info,
3097 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003098 Primitive::Type type = field_info.GetFieldType();
3099 LocationSummary* locations = instruction->GetLocations();
3100 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3101 StoreOperandType store_type = kStoreByte;
3102 switch (type) {
3103 case Primitive::kPrimBoolean:
3104 case Primitive::kPrimByte:
3105 store_type = kStoreByte;
3106 break;
3107 case Primitive::kPrimShort:
3108 case Primitive::kPrimChar:
3109 store_type = kStoreHalfword;
3110 break;
3111 case Primitive::kPrimInt:
3112 case Primitive::kPrimFloat:
3113 case Primitive::kPrimNot:
3114 store_type = kStoreWord;
3115 break;
3116 case Primitive::kPrimLong:
3117 case Primitive::kPrimDouble:
3118 store_type = kStoreDoubleword;
3119 break;
3120 case Primitive::kPrimVoid:
3121 LOG(FATAL) << "Unreachable type " << type;
3122 UNREACHABLE();
3123 }
3124 if (!Primitive::IsFloatingPointType(type)) {
3125 DCHECK(locations->InAt(1).IsRegister());
3126 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
3127 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
3128 } else {
3129 DCHECK(locations->InAt(1).IsFpuRegister());
3130 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
3131 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
3132 }
3133
3134 codegen_->MaybeRecordImplicitNullCheck(instruction);
3135 // TODO: memory barriers?
3136 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3137 DCHECK(locations->InAt(1).IsRegister());
3138 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003139 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003140 }
3141}
3142
3143void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3144 HandleFieldGet(instruction, instruction->GetFieldInfo());
3145}
3146
3147void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3148 HandleFieldGet(instruction, instruction->GetFieldInfo());
3149}
3150
3151void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3152 HandleFieldSet(instruction, instruction->GetFieldInfo());
3153}
3154
3155void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003156 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003157}
3158
Alexey Frunzef63f5692016-12-13 17:43:11 -08003159void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3160 HInstruction* instruction ATTRIBUTE_UNUSED,
3161 Location root,
3162 GpuRegister obj,
3163 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003164 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3165 if (kEmitCompilerReadBarrier) {
3166 UNIMPLEMENTED(FATAL) << "for read barrier";
3167 } else {
3168 // Plain GC root load with no read barrier.
3169 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3170 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3171 // Note that GC roots are not affected by heap poisoning, thus we
3172 // do not have to unpoison `root_reg` here.
3173 }
3174}
3175
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3177 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003179 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3180 locations->SetInAt(0, Location::RequiresRegister());
3181 locations->SetInAt(1, Location::RequiresRegister());
3182 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003183 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003184 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3185}
3186
3187void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3188 LocationSummary* locations = instruction->GetLocations();
3189 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3190 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3191 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3192
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003193 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003194
3195 // Return 0 if `obj` is null.
3196 // TODO: Avoid this check if we know `obj` is not null.
3197 __ Move(out, ZERO);
3198 __ Beqzc(obj, &done);
3199
3200 // Compare the class of `obj` with `cls`.
3201 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003202 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003203 // Classes must be equal for the instanceof to succeed.
3204 __ Xor(out, out, cls);
3205 __ Sltiu(out, out, 1);
3206 } else {
3207 // If the classes are not equal, we go into a slow path.
3208 DCHECK(locations->OnlyCallsOnSlowPath());
3209 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003210 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003211 codegen_->AddSlowPath(slow_path);
3212 __ Bnec(out, cls, slow_path->GetEntryLabel());
3213 __ LoadConst32(out, 1);
3214 __ Bind(slow_path->GetExitLabel());
3215 }
3216
3217 __ Bind(&done);
3218}
3219
3220void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3221 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3222 locations->SetOut(Location::ConstantLocation(constant));
3223}
3224
3225void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3226 // Will be generated at use site.
3227}
3228
3229void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3231 locations->SetOut(Location::ConstantLocation(constant));
3232}
3233
3234void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3235 // Will be generated at use site.
3236}
3237
Calin Juravle175dc732015-08-25 15:42:32 +01003238void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3239 // The trampoline uses the same calling convention as dex calling conventions,
3240 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3241 // the method_idx.
3242 HandleInvoke(invoke);
3243}
3244
3245void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3246 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3247}
3248
Alexey Frunze4dda3372015-06-01 18:31:49 -07003249void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3250 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3251 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3252}
3253
3254void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3255 HandleInvoke(invoke);
3256 // The register T0 is required to be used for the hidden argument in
3257 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3258 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3259}
3260
3261void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3262 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3263 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264 Location receiver = invoke->GetLocations()->InAt(0);
3265 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003266 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003267
3268 // Set the hidden argument.
3269 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3270 invoke->GetDexMethodIndex());
3271
3272 // temp = object->GetClass();
3273 if (receiver.IsStackSlot()) {
3274 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3275 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3276 } else {
3277 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3278 }
3279 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003280 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3281 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3282 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003283 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003284 // temp = temp->GetImtEntryAt(method_offset);
3285 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3286 // T9 = temp->GetEntryPoint();
3287 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3288 // T9();
3289 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003290 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003291 DCHECK(!codegen_->IsLeafMethod());
3292 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3293}
3294
3295void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003296 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3297 if (intrinsic.TryDispatch(invoke)) {
3298 return;
3299 }
3300
Alexey Frunze4dda3372015-06-01 18:31:49 -07003301 HandleInvoke(invoke);
3302}
3303
3304void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003305 // Explicit clinit checks triggered by static invokes must have been pruned by
3306 // art::PrepareForRegisterAllocation.
3307 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003308
Chris Larsen3039e382015-08-26 07:54:08 -07003309 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3310 if (intrinsic.TryDispatch(invoke)) {
3311 return;
3312 }
3313
Alexey Frunze4dda3372015-06-01 18:31:49 -07003314 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003315}
3316
Orion Hodsonac141392017-01-13 11:53:47 +00003317void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3318 HandleInvoke(invoke);
3319}
3320
3321void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3322 codegen_->GenerateInvokePolymorphicCall(invoke);
3323}
3324
Chris Larsen3039e382015-08-26 07:54:08 -07003325static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003326 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003327 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3328 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003329 return true;
3330 }
3331 return false;
3332}
3333
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003334HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003335 HLoadString::LoadKind desired_string_load_kind) {
3336 if (kEmitCompilerReadBarrier) {
3337 UNIMPLEMENTED(FATAL) << "for read barrier";
3338 }
3339 bool fallback_load = false;
3340 switch (desired_string_load_kind) {
3341 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3342 DCHECK(!GetCompilerOptions().GetCompilePic());
3343 break;
3344 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3345 DCHECK(GetCompilerOptions().GetCompilePic());
3346 break;
3347 case HLoadString::LoadKind::kBootImageAddress:
3348 break;
3349 case HLoadString::LoadKind::kBssEntry:
3350 DCHECK(!Runtime::Current()->UseJitCompilation());
3351 break;
3352 case HLoadString::LoadKind::kDexCacheViaMethod:
3353 break;
3354 case HLoadString::LoadKind::kJitTableAddress:
3355 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003356 break;
3357 }
3358 if (fallback_load) {
3359 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3360 }
3361 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003362}
3363
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003364HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3365 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003366 if (kEmitCompilerReadBarrier) {
3367 UNIMPLEMENTED(FATAL) << "for read barrier";
3368 }
3369 bool fallback_load = false;
3370 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003371 case HLoadClass::LoadKind::kInvalid:
3372 LOG(FATAL) << "UNREACHABLE";
3373 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003374 case HLoadClass::LoadKind::kReferrersClass:
3375 break;
3376 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3377 DCHECK(!GetCompilerOptions().GetCompilePic());
3378 break;
3379 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3380 DCHECK(GetCompilerOptions().GetCompilePic());
3381 break;
3382 case HLoadClass::LoadKind::kBootImageAddress:
3383 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003384 case HLoadClass::LoadKind::kBssEntry:
3385 DCHECK(!Runtime::Current()->UseJitCompilation());
3386 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003387 case HLoadClass::LoadKind::kJitTableAddress:
3388 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003389 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003390 case HLoadClass::LoadKind::kDexCacheViaMethod:
3391 break;
3392 }
3393 if (fallback_load) {
3394 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3395 }
3396 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003397}
3398
Vladimir Markodc151b22015-10-15 18:02:30 +01003399HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3400 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003401 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003402 // On MIPS64 we support all dispatch types.
3403 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003404}
3405
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3407 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003408 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003409 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3410 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3411
Alexey Frunze19f6c692016-11-30 19:19:55 -08003412 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003413 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003414 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003415 uint32_t offset =
3416 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003417 __ LoadFromOffset(kLoadDoubleword,
3418 temp.AsRegister<GpuRegister>(),
3419 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003420 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003421 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003422 }
Vladimir Marko58155012015-08-19 12:49:41 +00003423 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003424 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003425 break;
3426 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003427 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3428 kLoadDoubleword,
3429 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003430 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003431 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3432 uint32_t offset = invoke->GetDexCacheArrayOffset();
3433 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003434 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003435 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3436 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3437 break;
3438 }
Vladimir Marko58155012015-08-19 12:49:41 +00003439 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003440 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003441 GpuRegister reg = temp.AsRegister<GpuRegister>();
3442 GpuRegister method_reg;
3443 if (current_method.IsRegister()) {
3444 method_reg = current_method.AsRegister<GpuRegister>();
3445 } else {
3446 // TODO: use the appropriate DCHECK() here if possible.
3447 // DCHECK(invoke->GetLocations()->Intrinsified());
3448 DCHECK(!current_method.IsValid());
3449 method_reg = reg;
3450 __ Ld(reg, SP, kCurrentMethodStackOffset);
3451 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003452
Vladimir Marko58155012015-08-19 12:49:41 +00003453 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003454 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003455 reg,
3456 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003457 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003458 // temp = temp[index_in_cache];
3459 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3460 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003461 __ LoadFromOffset(kLoadDoubleword,
3462 reg,
3463 reg,
3464 CodeGenerator::GetCachePointerOffset(index_in_cache));
3465 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003466 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003467 }
3468
Alexey Frunze19f6c692016-11-30 19:19:55 -08003469 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003470 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003471 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003472 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003473 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3474 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3475 __ LoadFromOffset(kLoadDoubleword,
3476 T9,
3477 callee_method.AsRegister<GpuRegister>(),
3478 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003479 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003480 // T9()
3481 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003482 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003483 break;
3484 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485 DCHECK(!IsLeafMethod());
3486}
3487
3488void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003489 // Explicit clinit checks triggered by static invokes must have been pruned by
3490 // art::PrepareForRegisterAllocation.
3491 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492
3493 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3494 return;
3495 }
3496
3497 LocationSummary* locations = invoke->GetLocations();
3498 codegen_->GenerateStaticOrDirectCall(invoke,
3499 locations->HasTemps()
3500 ? locations->GetTemp(0)
3501 : Location::NoLocation());
3502 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3503}
3504
Alexey Frunze53afca12015-11-05 16:34:23 -08003505void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003506 // Use the calling convention instead of the location of the receiver, as
3507 // intrinsics may have put the receiver in a different register. In the intrinsics
3508 // slow path, the arguments have been moved to the right place, so here we are
3509 // guaranteed that the receiver is the first register of the calling convention.
3510 InvokeDexCallingConvention calling_convention;
3511 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3512
Alexey Frunze53afca12015-11-05 16:34:23 -08003513 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3515 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3516 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003517 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003518
3519 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003520 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003521 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003522 // temp = temp->GetMethodAt(method_offset);
3523 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3524 // T9 = temp->GetEntryPoint();
3525 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3526 // T9();
3527 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003528 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003529}
3530
3531void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3532 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3533 return;
3534 }
3535
3536 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003537 DCHECK(!codegen_->IsLeafMethod());
3538 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3539}
3540
3541void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003542 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3543 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003544 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003545 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003546 cls,
3547 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003548 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003549 return;
3550 }
Vladimir Marko41559982017-01-06 14:04:23 +00003551 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003552
3553 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3554 ? LocationSummary::kCallOnSlowPath
3555 : LocationSummary::kNoCall;
3556 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003557 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003558 locations->SetInAt(0, Location::RequiresRegister());
3559 }
3560 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561}
3562
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003563// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3564// move.
3565void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003566 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3567 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3568 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003569 return;
3570 }
Vladimir Marko41559982017-01-06 14:04:23 +00003571 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003572
Vladimir Marko41559982017-01-06 14:04:23 +00003573 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003574 Location out_loc = locations->Out();
3575 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3576 GpuRegister current_method_reg = ZERO;
3577 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3578 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3579 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3580 }
3581
3582 bool generate_null_check = false;
3583 switch (load_kind) {
3584 case HLoadClass::LoadKind::kReferrersClass:
3585 DCHECK(!cls->CanCallRuntime());
3586 DCHECK(!cls->MustGenerateClinitCheck());
3587 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3588 GenerateGcRootFieldLoad(cls,
3589 out_loc,
3590 current_method_reg,
3591 ArtMethod::DeclaringClassOffset().Int32Value());
3592 break;
3593 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003594 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003595 __ LoadLiteral(out,
3596 kLoadUnsignedWord,
3597 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3598 cls->GetTypeIndex()));
3599 break;
3600 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003601 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003602 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3603 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3604 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3605 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3606 break;
3607 }
3608 case HLoadClass::LoadKind::kBootImageAddress: {
3609 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003610 uint32_t address = dchecked_integral_cast<uint32_t>(
3611 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
3612 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003613 __ LoadLiteral(out,
3614 kLoadUnsignedWord,
3615 codegen_->DeduplicateBootImageAddressLiteral(address));
3616 break;
3617 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003618 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003619 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00003620 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003621 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3622 __ Lwu(out, AT, /* placeholder */ 0x5678);
3623 generate_null_check = true;
3624 break;
3625 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003626 case HLoadClass::LoadKind::kJitTableAddress:
3627 __ LoadLiteral(out,
3628 kLoadUnsignedWord,
3629 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
3630 cls->GetTypeIndex(),
3631 cls->GetClass()));
3632 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003633 break;
Vladimir Marko41559982017-01-06 14:04:23 +00003634 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003635 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00003636 LOG(FATAL) << "UNREACHABLE";
3637 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003638 }
3639
3640 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3641 DCHECK(cls->CanCallRuntime());
3642 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3643 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3644 codegen_->AddSlowPath(slow_path);
3645 if (generate_null_check) {
3646 __ Beqzc(out, slow_path->GetEntryLabel());
3647 }
3648 if (cls->MustGenerateClinitCheck()) {
3649 GenerateClassInitializationCheck(slow_path, out);
3650 } else {
3651 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003652 }
3653 }
3654}
3655
David Brazdilcb1c0552015-08-04 16:22:25 +01003656static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003657 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003658}
3659
Alexey Frunze4dda3372015-06-01 18:31:49 -07003660void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3661 LocationSummary* locations =
3662 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3663 locations->SetOut(Location::RequiresRegister());
3664}
3665
3666void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3667 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003668 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3669}
3670
3671void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3672 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3673}
3674
3675void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3676 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003677}
3678
Alexey Frunze4dda3372015-06-01 18:31:49 -07003679void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003680 HLoadString::LoadKind load_kind = load->GetLoadKind();
3681 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003683 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3684 InvokeRuntimeCallingConvention calling_convention;
3685 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3686 } else {
3687 locations->SetOut(Location::RequiresRegister());
3688 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003689}
3690
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003691// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3692// move.
3693void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003694 HLoadString::LoadKind load_kind = load->GetLoadKind();
3695 LocationSummary* locations = load->GetLocations();
3696 Location out_loc = locations->Out();
3697 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3698
3699 switch (load_kind) {
3700 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003701 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003702 __ LoadLiteral(out,
3703 kLoadUnsignedWord,
3704 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3705 load->GetStringIndex()));
3706 return; // No dex cache slow path.
3707 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3708 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3709 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003710 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003711 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3712 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3713 return; // No dex cache slow path.
3714 }
3715 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003716 uint32_t address = dchecked_integral_cast<uint32_t>(
3717 reinterpret_cast<uintptr_t>(load->GetString().Get()));
3718 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003719 __ LoadLiteral(out,
3720 kLoadUnsignedWord,
3721 codegen_->DeduplicateBootImageAddressLiteral(address));
3722 return; // No dex cache slow path.
3723 }
3724 case HLoadString::LoadKind::kBssEntry: {
3725 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3726 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003727 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003728 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3729 __ Lwu(out, AT, /* placeholder */ 0x5678);
3730 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3731 codegen_->AddSlowPath(slow_path);
3732 __ Beqzc(out, slow_path->GetEntryLabel());
3733 __ Bind(slow_path->GetExitLabel());
3734 return;
3735 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003736 case HLoadString::LoadKind::kJitTableAddress:
3737 __ LoadLiteral(out,
3738 kLoadUnsignedWord,
3739 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
3740 load->GetStringIndex(),
3741 load->GetString()));
3742 GenerateGcRootFieldLoad(load, out_loc, out, 0);
3743 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003744 default:
3745 break;
3746 }
3747
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003748 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003749 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3750 InvokeRuntimeCallingConvention calling_convention;
3751 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3752 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3753 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003754}
3755
Alexey Frunze4dda3372015-06-01 18:31:49 -07003756void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3758 locations->SetOut(Location::ConstantLocation(constant));
3759}
3760
3761void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3762 // Will be generated at use site.
3763}
3764
3765void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3766 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003768 InvokeRuntimeCallingConvention calling_convention;
3769 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3770}
3771
3772void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003773 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003774 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003775 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003776 if (instruction->IsEnter()) {
3777 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3778 } else {
3779 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3780 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003781}
3782
3783void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3784 LocationSummary* locations =
3785 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3786 switch (mul->GetResultType()) {
3787 case Primitive::kPrimInt:
3788 case Primitive::kPrimLong:
3789 locations->SetInAt(0, Location::RequiresRegister());
3790 locations->SetInAt(1, Location::RequiresRegister());
3791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3792 break;
3793
3794 case Primitive::kPrimFloat:
3795 case Primitive::kPrimDouble:
3796 locations->SetInAt(0, Location::RequiresFpuRegister());
3797 locations->SetInAt(1, Location::RequiresFpuRegister());
3798 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3799 break;
3800
3801 default:
3802 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3803 }
3804}
3805
3806void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3807 Primitive::Type type = instruction->GetType();
3808 LocationSummary* locations = instruction->GetLocations();
3809
3810 switch (type) {
3811 case Primitive::kPrimInt:
3812 case Primitive::kPrimLong: {
3813 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3814 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3815 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3816 if (type == Primitive::kPrimInt)
3817 __ MulR6(dst, lhs, rhs);
3818 else
3819 __ Dmul(dst, lhs, rhs);
3820 break;
3821 }
3822 case Primitive::kPrimFloat:
3823 case Primitive::kPrimDouble: {
3824 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3825 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3826 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3827 if (type == Primitive::kPrimFloat)
3828 __ MulS(dst, lhs, rhs);
3829 else
3830 __ MulD(dst, lhs, rhs);
3831 break;
3832 }
3833 default:
3834 LOG(FATAL) << "Unexpected mul type " << type;
3835 }
3836}
3837
3838void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3839 LocationSummary* locations =
3840 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3841 switch (neg->GetResultType()) {
3842 case Primitive::kPrimInt:
3843 case Primitive::kPrimLong:
3844 locations->SetInAt(0, Location::RequiresRegister());
3845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3846 break;
3847
3848 case Primitive::kPrimFloat:
3849 case Primitive::kPrimDouble:
3850 locations->SetInAt(0, Location::RequiresFpuRegister());
3851 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3852 break;
3853
3854 default:
3855 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3856 }
3857}
3858
3859void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3860 Primitive::Type type = instruction->GetType();
3861 LocationSummary* locations = instruction->GetLocations();
3862
3863 switch (type) {
3864 case Primitive::kPrimInt:
3865 case Primitive::kPrimLong: {
3866 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3867 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3868 if (type == Primitive::kPrimInt)
3869 __ Subu(dst, ZERO, src);
3870 else
3871 __ Dsubu(dst, ZERO, src);
3872 break;
3873 }
3874 case Primitive::kPrimFloat:
3875 case Primitive::kPrimDouble: {
3876 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3877 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3878 if (type == Primitive::kPrimFloat)
3879 __ NegS(dst, src);
3880 else
3881 __ NegD(dst, src);
3882 break;
3883 }
3884 default:
3885 LOG(FATAL) << "Unexpected neg type " << type;
3886 }
3887}
3888
3889void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3890 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003892 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003893 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003894 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3895 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003896}
3897
3898void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003899 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
3900 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003901}
3902
3903void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3904 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003906 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003907 if (instruction->IsStringAlloc()) {
3908 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3909 } else {
3910 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003911 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003912 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3913}
3914
3915void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003916 if (instruction->IsStringAlloc()) {
3917 // String is allocated through StringFactory. Call NewEmptyString entry point.
3918 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003919 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003920 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003921 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3922 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3923 __ Jalr(T9);
3924 __ Nop();
3925 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3926 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003927 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003928 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003929 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003930}
3931
3932void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3934 locations->SetInAt(0, Location::RequiresRegister());
3935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3936}
3937
3938void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3939 Primitive::Type type = instruction->GetType();
3940 LocationSummary* locations = instruction->GetLocations();
3941
3942 switch (type) {
3943 case Primitive::kPrimInt:
3944 case Primitive::kPrimLong: {
3945 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3946 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3947 __ Nor(dst, src, ZERO);
3948 break;
3949 }
3950
3951 default:
3952 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3953 }
3954}
3955
3956void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3957 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3958 locations->SetInAt(0, Location::RequiresRegister());
3959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3960}
3961
3962void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3963 LocationSummary* locations = instruction->GetLocations();
3964 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3965 locations->InAt(0).AsRegister<GpuRegister>(),
3966 1);
3967}
3968
3969void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003970 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3971 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003972}
3973
Calin Juravle2ae48182016-03-16 14:05:09 +00003974void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3975 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003976 return;
3977 }
3978 Location obj = instruction->GetLocations()->InAt(0);
3979
3980 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003981 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003982}
3983
Calin Juravle2ae48182016-03-16 14:05:09 +00003984void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003985 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003986 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003987
3988 Location obj = instruction->GetLocations()->InAt(0);
3989
3990 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3991}
3992
3993void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003994 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995}
3996
3997void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3998 HandleBinaryOp(instruction);
3999}
4000
4001void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4002 HandleBinaryOp(instruction);
4003}
4004
4005void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4006 LOG(FATAL) << "Unreachable";
4007}
4008
4009void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4010 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4011}
4012
4013void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4015 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4016 if (location.IsStackSlot()) {
4017 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4018 } else if (location.IsDoubleStackSlot()) {
4019 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4020 }
4021 locations->SetOut(location);
4022}
4023
4024void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4025 ATTRIBUTE_UNUSED) {
4026 // Nothing to do, the parameter is already at its location.
4027}
4028
4029void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4032 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4033}
4034
4035void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4036 ATTRIBUTE_UNUSED) {
4037 // Nothing to do, the method is already at its location.
4038}
4039
4040void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4041 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004042 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004043 locations->SetInAt(i, Location::Any());
4044 }
4045 locations->SetOut(Location::Any());
4046}
4047
4048void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4049 LOG(FATAL) << "Unreachable";
4050}
4051
4052void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4053 Primitive::Type type = rem->GetResultType();
4054 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004055 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4056 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4058
4059 switch (type) {
4060 case Primitive::kPrimInt:
4061 case Primitive::kPrimLong:
4062 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004063 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4065 break;
4066
4067 case Primitive::kPrimFloat:
4068 case Primitive::kPrimDouble: {
4069 InvokeRuntimeCallingConvention calling_convention;
4070 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4071 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4072 locations->SetOut(calling_convention.GetReturnLocation(type));
4073 break;
4074 }
4075
4076 default:
4077 LOG(FATAL) << "Unexpected rem type " << type;
4078 }
4079}
4080
4081void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4082 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004083
4084 switch (type) {
4085 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004086 case Primitive::kPrimLong:
4087 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004088 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004089
4090 case Primitive::kPrimFloat:
4091 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004092 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4093 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004094 if (type == Primitive::kPrimFloat) {
4095 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4096 } else {
4097 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4098 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099 break;
4100 }
4101 default:
4102 LOG(FATAL) << "Unexpected rem type " << type;
4103 }
4104}
4105
4106void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4107 memory_barrier->SetLocations(nullptr);
4108}
4109
4110void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4111 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4112}
4113
4114void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4115 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4116 Primitive::Type return_type = ret->InputAt(0)->GetType();
4117 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4121 codegen_->GenerateFrameExit();
4122}
4123
4124void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4125 ret->SetLocations(nullptr);
4126}
4127
4128void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4129 codegen_->GenerateFrameExit();
4130}
4131
Alexey Frunze92d90602015-12-18 18:16:36 -08004132void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4133 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004134}
4135
Alexey Frunze92d90602015-12-18 18:16:36 -08004136void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4137 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004138}
4139
Alexey Frunze4dda3372015-06-01 18:31:49 -07004140void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4141 HandleShift(shl);
4142}
4143
4144void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4145 HandleShift(shl);
4146}
4147
4148void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4149 HandleShift(shr);
4150}
4151
4152void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4153 HandleShift(shr);
4154}
4155
Alexey Frunze4dda3372015-06-01 18:31:49 -07004156void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4157 HandleBinaryOp(instruction);
4158}
4159
4160void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4161 HandleBinaryOp(instruction);
4162}
4163
4164void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4165 HandleFieldGet(instruction, instruction->GetFieldInfo());
4166}
4167
4168void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4169 HandleFieldGet(instruction, instruction->GetFieldInfo());
4170}
4171
4172void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4173 HandleFieldSet(instruction, instruction->GetFieldInfo());
4174}
4175
4176void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004177 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004178}
4179
Calin Juravlee460d1d2015-09-29 04:52:17 +01004180void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4181 HUnresolvedInstanceFieldGet* instruction) {
4182 FieldAccessCallingConventionMIPS64 calling_convention;
4183 codegen_->CreateUnresolvedFieldLocationSummary(
4184 instruction, instruction->GetFieldType(), calling_convention);
4185}
4186
4187void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4188 HUnresolvedInstanceFieldGet* instruction) {
4189 FieldAccessCallingConventionMIPS64 calling_convention;
4190 codegen_->GenerateUnresolvedFieldAccess(instruction,
4191 instruction->GetFieldType(),
4192 instruction->GetFieldIndex(),
4193 instruction->GetDexPc(),
4194 calling_convention);
4195}
4196
4197void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4198 HUnresolvedInstanceFieldSet* instruction) {
4199 FieldAccessCallingConventionMIPS64 calling_convention;
4200 codegen_->CreateUnresolvedFieldLocationSummary(
4201 instruction, instruction->GetFieldType(), calling_convention);
4202}
4203
4204void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4205 HUnresolvedInstanceFieldSet* instruction) {
4206 FieldAccessCallingConventionMIPS64 calling_convention;
4207 codegen_->GenerateUnresolvedFieldAccess(instruction,
4208 instruction->GetFieldType(),
4209 instruction->GetFieldIndex(),
4210 instruction->GetDexPc(),
4211 calling_convention);
4212}
4213
4214void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4215 HUnresolvedStaticFieldGet* instruction) {
4216 FieldAccessCallingConventionMIPS64 calling_convention;
4217 codegen_->CreateUnresolvedFieldLocationSummary(
4218 instruction, instruction->GetFieldType(), calling_convention);
4219}
4220
4221void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4222 HUnresolvedStaticFieldGet* instruction) {
4223 FieldAccessCallingConventionMIPS64 calling_convention;
4224 codegen_->GenerateUnresolvedFieldAccess(instruction,
4225 instruction->GetFieldType(),
4226 instruction->GetFieldIndex(),
4227 instruction->GetDexPc(),
4228 calling_convention);
4229}
4230
4231void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4232 HUnresolvedStaticFieldSet* instruction) {
4233 FieldAccessCallingConventionMIPS64 calling_convention;
4234 codegen_->CreateUnresolvedFieldLocationSummary(
4235 instruction, instruction->GetFieldType(), calling_convention);
4236}
4237
4238void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4239 HUnresolvedStaticFieldSet* instruction) {
4240 FieldAccessCallingConventionMIPS64 calling_convention;
4241 codegen_->GenerateUnresolvedFieldAccess(instruction,
4242 instruction->GetFieldType(),
4243 instruction->GetFieldIndex(),
4244 instruction->GetDexPc(),
4245 calling_convention);
4246}
4247
Alexey Frunze4dda3372015-06-01 18:31:49 -07004248void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004249 LocationSummary* locations =
4250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004251 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004252}
4253
4254void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4255 HBasicBlock* block = instruction->GetBlock();
4256 if (block->GetLoopInformation() != nullptr) {
4257 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4258 // The back edge will generate the suspend check.
4259 return;
4260 }
4261 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4262 // The goto will generate the suspend check.
4263 return;
4264 }
4265 GenerateSuspendCheck(instruction, nullptr);
4266}
4267
Alexey Frunze4dda3372015-06-01 18:31:49 -07004268void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4269 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004271 InvokeRuntimeCallingConvention calling_convention;
4272 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4273}
4274
4275void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004276 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004277 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4278}
4279
4280void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4281 Primitive::Type input_type = conversion->GetInputType();
4282 Primitive::Type result_type = conversion->GetResultType();
4283 DCHECK_NE(input_type, result_type);
4284
4285 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4286 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4287 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4288 }
4289
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4291
4292 if (Primitive::IsFloatingPointType(input_type)) {
4293 locations->SetInAt(0, Location::RequiresFpuRegister());
4294 } else {
4295 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004296 }
4297
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004298 if (Primitive::IsFloatingPointType(result_type)) {
4299 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004300 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004302 }
4303}
4304
4305void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4306 LocationSummary* locations = conversion->GetLocations();
4307 Primitive::Type result_type = conversion->GetResultType();
4308 Primitive::Type input_type = conversion->GetInputType();
4309
4310 DCHECK_NE(input_type, result_type);
4311
4312 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4313 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4314 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4315
4316 switch (result_type) {
4317 case Primitive::kPrimChar:
4318 __ Andi(dst, src, 0xFFFF);
4319 break;
4320 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004321 if (input_type == Primitive::kPrimLong) {
4322 // Type conversion from long to types narrower than int is a result of code
4323 // transformations. To avoid unpredictable results for SEB and SEH, we first
4324 // need to sign-extend the low 32-bit value into bits 32 through 63.
4325 __ Sll(dst, src, 0);
4326 __ Seb(dst, dst);
4327 } else {
4328 __ Seb(dst, src);
4329 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004330 break;
4331 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004332 if (input_type == Primitive::kPrimLong) {
4333 // Type conversion from long to types narrower than int is a result of code
4334 // transformations. To avoid unpredictable results for SEB and SEH, we first
4335 // need to sign-extend the low 32-bit value into bits 32 through 63.
4336 __ Sll(dst, src, 0);
4337 __ Seh(dst, dst);
4338 } else {
4339 __ Seh(dst, src);
4340 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004341 break;
4342 case Primitive::kPrimInt:
4343 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004344 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4345 // conversions, except when the input and output registers are the same and we are not
4346 // converting longs to shorter types. In these cases, do nothing.
4347 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4348 __ Sll(dst, src, 0);
4349 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004350 break;
4351
4352 default:
4353 LOG(FATAL) << "Unexpected type conversion from " << input_type
4354 << " to " << result_type;
4355 }
4356 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004357 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4358 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4359 if (input_type == Primitive::kPrimLong) {
4360 __ Dmtc1(src, FTMP);
4361 if (result_type == Primitive::kPrimFloat) {
4362 __ Cvtsl(dst, FTMP);
4363 } else {
4364 __ Cvtdl(dst, FTMP);
4365 }
4366 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004367 __ Mtc1(src, FTMP);
4368 if (result_type == Primitive::kPrimFloat) {
4369 __ Cvtsw(dst, FTMP);
4370 } else {
4371 __ Cvtdw(dst, FTMP);
4372 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004373 }
4374 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4375 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004376 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4377 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4378 Mips64Label truncate;
4379 Mips64Label done;
4380
4381 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4382 // value when the input is either a NaN or is outside of the range of the output type
4383 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4384 // the same result.
4385 //
4386 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4387 // value of the output type if the input is outside of the range after the truncation or
4388 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4389 // results. This matches the desired float/double-to-int/long conversion exactly.
4390 //
4391 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4392 //
4393 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4394 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4395 // even though it must be NAN2008=1 on R6.
4396 //
4397 // The code takes care of the different behaviors by first comparing the input to the
4398 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4399 // If the input is greater than or equal to the minimum, it procedes to the truncate
4400 // instruction, which will handle such an input the same way irrespective of NAN2008.
4401 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4402 // in order to return either zero or the minimum value.
4403 //
4404 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4405 // truncate instruction for MIPS64R6.
4406 if (input_type == Primitive::kPrimFloat) {
4407 uint32_t min_val = (result_type == Primitive::kPrimLong)
4408 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4409 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4410 __ LoadConst32(TMP, min_val);
4411 __ Mtc1(TMP, FTMP);
4412 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004413 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004414 uint64_t min_val = (result_type == Primitive::kPrimLong)
4415 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4416 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4417 __ LoadConst64(TMP, min_val);
4418 __ Dmtc1(TMP, FTMP);
4419 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004420 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004421
4422 __ Bc1nez(FTMP, &truncate);
4423
4424 if (input_type == Primitive::kPrimFloat) {
4425 __ CmpEqS(FTMP, src, src);
4426 } else {
4427 __ CmpEqD(FTMP, src, src);
4428 }
4429 if (result_type == Primitive::kPrimLong) {
4430 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4431 } else {
4432 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4433 }
4434 __ Mfc1(TMP, FTMP);
4435 __ And(dst, dst, TMP);
4436
4437 __ Bc(&done);
4438
4439 __ Bind(&truncate);
4440
4441 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004442 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004443 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004444 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004445 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004446 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004447 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004448 } else {
4449 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004450 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004451 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004452 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004453 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004454 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004455 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004456
4457 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004458 } else if (Primitive::IsFloatingPointType(result_type) &&
4459 Primitive::IsFloatingPointType(input_type)) {
4460 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4461 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4462 if (result_type == Primitive::kPrimFloat) {
4463 __ Cvtsd(dst, src);
4464 } else {
4465 __ Cvtds(dst, src);
4466 }
4467 } else {
4468 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4469 << " to " << result_type;
4470 }
4471}
4472
4473void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4474 HandleShift(ushr);
4475}
4476
4477void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4478 HandleShift(ushr);
4479}
4480
4481void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4482 HandleBinaryOp(instruction);
4483}
4484
4485void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4486 HandleBinaryOp(instruction);
4487}
4488
4489void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4490 // Nothing to do, this should be removed during prepare for register allocator.
4491 LOG(FATAL) << "Unreachable";
4492}
4493
4494void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4495 // Nothing to do, this should be removed during prepare for register allocator.
4496 LOG(FATAL) << "Unreachable";
4497}
4498
4499void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004500 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004501}
4502
4503void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004504 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004505}
4506
4507void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004508 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004509}
4510
4511void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004512 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004513}
4514
4515void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004516 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004517}
4518
4519void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004520 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004521}
4522
4523void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004524 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004525}
4526
4527void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004528 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004529}
4530
4531void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004532 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004533}
4534
4535void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004536 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004537}
4538
4539void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004540 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004541}
4542
4543void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004544 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004545}
4546
Aart Bike9f37602015-10-09 11:15:55 -07004547void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004548 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004549}
4550
4551void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004552 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004553}
4554
4555void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004556 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004557}
4558
4559void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004560 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004561}
4562
4563void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004564 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004565}
4566
4567void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004568 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004569}
4570
4571void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004572 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004573}
4574
4575void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004576 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004577}
4578
Mark Mendellfe57faa2015-09-18 09:26:15 -04004579// Simple implementation of packed switch - generate cascaded compare/jumps.
4580void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4581 LocationSummary* locations =
4582 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4583 locations->SetInAt(0, Location::RequiresRegister());
4584}
4585
Alexey Frunze0960ac52016-12-20 17:24:59 -08004586void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4587 int32_t lower_bound,
4588 uint32_t num_entries,
4589 HBasicBlock* switch_block,
4590 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004591 // Create a set of compare/jumps.
4592 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004593 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004594 // Jump to default if index is negative
4595 // Note: We don't check the case that index is positive while value < lower_bound, because in
4596 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4597 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4598
Alexey Frunze0960ac52016-12-20 17:24:59 -08004599 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004600 // Jump to successors[0] if value == lower_bound.
4601 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4602 int32_t last_index = 0;
4603 for (; num_entries - last_index > 2; last_index += 2) {
4604 __ Addiu(temp_reg, temp_reg, -2);
4605 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4606 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4607 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4608 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4609 }
4610 if (num_entries - last_index == 2) {
4611 // The last missing case_value.
4612 __ Addiu(temp_reg, temp_reg, -1);
4613 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004614 }
4615
4616 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08004617 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004618 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004619 }
4620}
4621
Alexey Frunze0960ac52016-12-20 17:24:59 -08004622void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
4623 int32_t lower_bound,
4624 uint32_t num_entries,
4625 HBasicBlock* switch_block,
4626 HBasicBlock* default_block) {
4627 // Create a jump table.
4628 std::vector<Mips64Label*> labels(num_entries);
4629 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
4630 for (uint32_t i = 0; i < num_entries; i++) {
4631 labels[i] = codegen_->GetLabelOf(successors[i]);
4632 }
4633 JumpTable* table = __ CreateJumpTable(std::move(labels));
4634
4635 // Is the value in range?
4636 __ Addiu32(TMP, value_reg, -lower_bound);
4637 __ LoadConst32(AT, num_entries);
4638 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
4639
4640 // We are in the range of the table.
4641 // Load the target address from the jump table, indexing by the value.
4642 __ LoadLabelAddress(AT, table->GetLabel());
4643 __ Sll(TMP, TMP, 2);
4644 __ Daddu(TMP, TMP, AT);
4645 __ Lw(TMP, TMP, 0);
4646 // Compute the absolute target address by adding the table start address
4647 // (the table contains offsets to targets relative to its start).
4648 __ Daddu(TMP, TMP, AT);
4649 // And jump.
4650 __ Jr(TMP);
4651 __ Nop();
4652}
4653
4654void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4655 int32_t lower_bound = switch_instr->GetStartValue();
4656 uint32_t num_entries = switch_instr->GetNumEntries();
4657 LocationSummary* locations = switch_instr->GetLocations();
4658 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4659 HBasicBlock* switch_block = switch_instr->GetBlock();
4660 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4661
4662 if (num_entries > kPackedSwitchJumpTableThreshold) {
4663 GenTableBasedPackedSwitch(value_reg,
4664 lower_bound,
4665 num_entries,
4666 switch_block,
4667 default_block);
4668 } else {
4669 GenPackedSwitchWithCompares(value_reg,
4670 lower_bound,
4671 num_entries,
4672 switch_block,
4673 default_block);
4674 }
4675}
4676
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004677void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4678 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4679}
4680
4681void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4682 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4683}
4684
Alexey Frunze4dda3372015-06-01 18:31:49 -07004685} // namespace mips64
4686} // namespace art