blob: 5be0da4011668e5a12134fa719152dc9d0c6d843 [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();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001493 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1494 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001495 switch (type) {
1496 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001497 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1498 if (index.IsConstant()) {
1499 size_t offset =
1500 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1501 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1502 } else {
1503 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1504 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1505 }
1506 break;
1507 }
1508
1509 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001510 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1511 if (index.IsConstant()) {
1512 size_t offset =
1513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1514 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1515 } else {
1516 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1517 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1518 }
1519 break;
1520 }
1521
1522 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001523 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1524 if (index.IsConstant()) {
1525 size_t offset =
1526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1527 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1528 } else {
1529 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1530 __ Daddu(TMP, obj, TMP);
1531 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1532 }
1533 break;
1534 }
1535
1536 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001537 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001538 if (maybe_compressed_char_at) {
1539 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1540 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset);
1541 codegen_->MaybeRecordImplicitNullCheck(instruction);
1542 __ Dext(TMP, TMP, 0, 1);
1543 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1544 "Expecting 0=compressed, 1=uncompressed");
1545 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001546 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001547 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1548 if (maybe_compressed_char_at) {
1549 Mips64Label uncompressed_load, done;
1550 __ Bnezc(TMP, &uncompressed_load);
1551 __ LoadFromOffset(kLoadUnsignedByte,
1552 out,
1553 obj,
1554 data_offset + (const_index << TIMES_1));
1555 __ Bc(&done);
1556 __ Bind(&uncompressed_load);
1557 __ LoadFromOffset(kLoadUnsignedHalfword,
1558 out,
1559 obj,
1560 data_offset + (const_index << TIMES_2));
1561 __ Bind(&done);
1562 } else {
1563 __ LoadFromOffset(kLoadUnsignedHalfword,
1564 out,
1565 obj,
1566 data_offset + (const_index << TIMES_2));
1567 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001568 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001569 GpuRegister index_reg = index.AsRegister<GpuRegister>();
1570 if (maybe_compressed_char_at) {
1571 Mips64Label uncompressed_load, done;
1572 __ Bnezc(TMP, &uncompressed_load);
1573 __ Daddu(TMP, obj, index_reg);
1574 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1575 __ Bc(&done);
1576 __ Bind(&uncompressed_load);
1577 __ Dsll(TMP, index_reg, TIMES_2);
1578 __ Daddu(TMP, obj, TMP);
1579 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1580 __ Bind(&done);
1581 } else {
1582 __ Dsll(TMP, index_reg, TIMES_2);
1583 __ Daddu(TMP, obj, TMP);
1584 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1585 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001586 }
1587 break;
1588 }
1589
1590 case Primitive::kPrimInt:
1591 case Primitive::kPrimNot: {
1592 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001593 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1594 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1595 if (index.IsConstant()) {
1596 size_t offset =
1597 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1598 __ LoadFromOffset(load_type, out, obj, offset);
1599 } else {
1600 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1601 __ Daddu(TMP, obj, TMP);
1602 __ LoadFromOffset(load_type, out, TMP, data_offset);
1603 }
1604 break;
1605 }
1606
1607 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001608 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1609 if (index.IsConstant()) {
1610 size_t offset =
1611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1612 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1613 } else {
1614 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1615 __ Daddu(TMP, obj, TMP);
1616 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1617 }
1618 break;
1619 }
1620
1621 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001622 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1623 if (index.IsConstant()) {
1624 size_t offset =
1625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1626 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1627 } else {
1628 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1629 __ Daddu(TMP, obj, TMP);
1630 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1631 }
1632 break;
1633 }
1634
1635 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001636 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1637 if (index.IsConstant()) {
1638 size_t offset =
1639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1640 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1641 } else {
1642 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1643 __ Daddu(TMP, obj, TMP);
1644 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1645 }
1646 break;
1647 }
1648
1649 case Primitive::kPrimVoid:
1650 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1651 UNREACHABLE();
1652 }
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001653 if (!maybe_compressed_char_at) {
1654 codegen_->MaybeRecordImplicitNullCheck(instruction);
1655 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001656}
1657
1658void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1659 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1660 locations->SetInAt(0, Location::RequiresRegister());
1661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1662}
1663
1664void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1665 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001666 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1668 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1669 __ LoadFromOffset(kLoadWord, out, obj, offset);
1670 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001671 // Mask out compression flag from String's array length.
1672 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
1673 __ Srl(out, out, 1u);
1674 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001675}
1676
1677void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001678 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1680 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001681 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001682 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001683 InvokeRuntimeCallingConvention calling_convention;
1684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1685 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1686 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1687 } else {
1688 locations->SetInAt(0, Location::RequiresRegister());
1689 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1690 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1691 locations->SetInAt(2, Location::RequiresFpuRegister());
1692 } else {
1693 locations->SetInAt(2, Location::RequiresRegister());
1694 }
1695 }
1696}
1697
1698void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1699 LocationSummary* locations = instruction->GetLocations();
1700 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1701 Location index = locations->InAt(1);
1702 Primitive::Type value_type = instruction->GetComponentType();
1703 bool needs_runtime_call = locations->WillCall();
1704 bool needs_write_barrier =
1705 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1706
1707 switch (value_type) {
1708 case Primitive::kPrimBoolean:
1709 case Primitive::kPrimByte: {
1710 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1711 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1712 if (index.IsConstant()) {
1713 size_t offset =
1714 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1715 __ StoreToOffset(kStoreByte, value, obj, offset);
1716 } else {
1717 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1718 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1719 }
1720 break;
1721 }
1722
1723 case Primitive::kPrimShort:
1724 case Primitive::kPrimChar: {
1725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1726 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1727 if (index.IsConstant()) {
1728 size_t offset =
1729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1730 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1731 } else {
1732 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1733 __ Daddu(TMP, obj, TMP);
1734 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1735 }
1736 break;
1737 }
1738
1739 case Primitive::kPrimInt:
1740 case Primitive::kPrimNot: {
1741 if (!needs_runtime_call) {
1742 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1743 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1744 if (index.IsConstant()) {
1745 size_t offset =
1746 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1747 __ StoreToOffset(kStoreWord, value, obj, offset);
1748 } else {
1749 DCHECK(index.IsRegister()) << index;
1750 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1751 __ Daddu(TMP, obj, TMP);
1752 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1753 }
1754 codegen_->MaybeRecordImplicitNullCheck(instruction);
1755 if (needs_write_barrier) {
1756 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001757 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001758 }
1759 } else {
1760 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001761 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001762 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001763 }
1764 break;
1765 }
1766
1767 case Primitive::kPrimLong: {
1768 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1769 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1770 if (index.IsConstant()) {
1771 size_t offset =
1772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1773 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1774 } else {
1775 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1776 __ Daddu(TMP, obj, TMP);
1777 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1778 }
1779 break;
1780 }
1781
1782 case Primitive::kPrimFloat: {
1783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1784 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1785 DCHECK(locations->InAt(2).IsFpuRegister());
1786 if (index.IsConstant()) {
1787 size_t offset =
1788 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1789 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1790 } else {
1791 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1792 __ Daddu(TMP, obj, TMP);
1793 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1794 }
1795 break;
1796 }
1797
1798 case Primitive::kPrimDouble: {
1799 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1800 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1801 DCHECK(locations->InAt(2).IsFpuRegister());
1802 if (index.IsConstant()) {
1803 size_t offset =
1804 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1805 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1806 } else {
1807 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1808 __ Daddu(TMP, obj, TMP);
1809 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1810 }
1811 break;
1812 }
1813
1814 case Primitive::kPrimVoid:
1815 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1816 UNREACHABLE();
1817 }
1818
1819 // Ints and objects are handled in the switch.
1820 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1821 codegen_->MaybeRecordImplicitNullCheck(instruction);
1822 }
1823}
1824
1825void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001826 RegisterSet caller_saves = RegisterSet::Empty();
1827 InvokeRuntimeCallingConvention calling_convention;
1828 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1829 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1830 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001831 locations->SetInAt(0, Location::RequiresRegister());
1832 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833}
1834
1835void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1836 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001837 BoundsCheckSlowPathMIPS64* slow_path =
1838 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001839 codegen_->AddSlowPath(slow_path);
1840
1841 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1842 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1843
1844 // length is limited by the maximum positive signed 32-bit integer.
1845 // Unsigned comparison of length and index checks for index < 0
1846 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001847 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001848}
1849
1850void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1852 instruction,
1853 LocationSummary::kCallOnSlowPath);
1854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001856 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001857 locations->AddTemp(Location::RequiresRegister());
1858}
1859
1860void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1861 LocationSummary* locations = instruction->GetLocations();
1862 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1863 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1864 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1865
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001866 SlowPathCodeMIPS64* slow_path =
1867 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001868 codegen_->AddSlowPath(slow_path);
1869
1870 // TODO: avoid this check if we know obj is not null.
1871 __ Beqzc(obj, slow_path->GetExitLabel());
1872 // Compare the class of `obj` with `cls`.
1873 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1874 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1875 __ Bind(slow_path->GetExitLabel());
1876}
1877
1878void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1881 locations->SetInAt(0, Location::RequiresRegister());
1882 if (check->HasUses()) {
1883 locations->SetOut(Location::SameAsFirstInput());
1884 }
1885}
1886
1887void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1888 // We assume the class is not null.
1889 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1890 check->GetLoadClass(),
1891 check,
1892 check->GetDexPc(),
1893 true);
1894 codegen_->AddSlowPath(slow_path);
1895 GenerateClassInitializationCheck(slow_path,
1896 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1897}
1898
1899void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1900 Primitive::Type in_type = compare->InputAt(0)->GetType();
1901
Alexey Frunze299a9392015-12-08 16:08:02 -08001902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903
1904 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001905 case Primitive::kPrimBoolean:
1906 case Primitive::kPrimByte:
1907 case Primitive::kPrimShort:
1908 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001909 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001910 case Primitive::kPrimLong:
1911 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001912 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1914 break;
1915
1916 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001917 case Primitive::kPrimDouble:
1918 locations->SetInAt(0, Location::RequiresFpuRegister());
1919 locations->SetInAt(1, Location::RequiresFpuRegister());
1920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001921 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001922
1923 default:
1924 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1925 }
1926}
1927
1928void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1929 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001930 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001931 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1932
1933 // 0 if: left == right
1934 // 1 if: left > right
1935 // -1 if: left < right
1936 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001937 case Primitive::kPrimBoolean:
1938 case Primitive::kPrimByte:
1939 case Primitive::kPrimShort:
1940 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001941 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001942 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001943 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001944 Location rhs_location = locations->InAt(1);
1945 bool use_imm = rhs_location.IsConstant();
1946 GpuRegister rhs = ZERO;
1947 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001948 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001949 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1950 if (value != 0) {
1951 rhs = AT;
1952 __ LoadConst64(rhs, value);
1953 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001954 } else {
1955 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1956 if (value != 0) {
1957 rhs = AT;
1958 __ LoadConst32(rhs, value);
1959 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001960 }
1961 } else {
1962 rhs = rhs_location.AsRegister<GpuRegister>();
1963 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001964 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001965 __ Slt(res, rhs, lhs);
1966 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967 break;
1968 }
1969
Alexey Frunze299a9392015-12-08 16:08:02 -08001970 case Primitive::kPrimFloat: {
1971 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1972 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1973 Mips64Label done;
1974 __ CmpEqS(FTMP, lhs, rhs);
1975 __ LoadConst32(res, 0);
1976 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001977 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001978 __ CmpLtS(FTMP, lhs, rhs);
1979 __ LoadConst32(res, -1);
1980 __ Bc1nez(FTMP, &done);
1981 __ LoadConst32(res, 1);
1982 } else {
1983 __ CmpLtS(FTMP, rhs, lhs);
1984 __ LoadConst32(res, 1);
1985 __ Bc1nez(FTMP, &done);
1986 __ LoadConst32(res, -1);
1987 }
1988 __ Bind(&done);
1989 break;
1990 }
1991
Alexey Frunze4dda3372015-06-01 18:31:49 -07001992 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001993 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1994 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1995 Mips64Label done;
1996 __ CmpEqD(FTMP, lhs, rhs);
1997 __ LoadConst32(res, 0);
1998 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001999 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002000 __ CmpLtD(FTMP, lhs, rhs);
2001 __ LoadConst32(res, -1);
2002 __ Bc1nez(FTMP, &done);
2003 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002004 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002005 __ CmpLtD(FTMP, rhs, lhs);
2006 __ LoadConst32(res, 1);
2007 __ Bc1nez(FTMP, &done);
2008 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002009 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002010 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002011 break;
2012 }
2013
2014 default:
2015 LOG(FATAL) << "Unimplemented compare type " << in_type;
2016 }
2017}
2018
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002021 switch (instruction->InputAt(0)->GetType()) {
2022 default:
2023 case Primitive::kPrimLong:
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2026 break;
2027
2028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble:
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 break;
2033 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002034 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2036 }
2037}
2038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002040 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002041 return;
2042 }
2043
Alexey Frunze299a9392015-12-08 16:08:02 -08002044 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002045 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002046 switch (type) {
2047 default:
2048 // Integer case.
2049 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2050 return;
2051 case Primitive::kPrimLong:
2052 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2053 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002054 case Primitive::kPrimFloat:
2055 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002056 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2057 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002058 }
2059}
2060
Alexey Frunzec857c742015-09-23 15:12:39 -07002061void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2062 DCHECK(instruction->IsDiv() || instruction->IsRem());
2063 Primitive::Type type = instruction->GetResultType();
2064
2065 LocationSummary* locations = instruction->GetLocations();
2066 Location second = locations->InAt(1);
2067 DCHECK(second.IsConstant());
2068
2069 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2070 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2071 int64_t imm = Int64FromConstant(second.GetConstant());
2072 DCHECK(imm == 1 || imm == -1);
2073
2074 if (instruction->IsRem()) {
2075 __ Move(out, ZERO);
2076 } else {
2077 if (imm == -1) {
2078 if (type == Primitive::kPrimInt) {
2079 __ Subu(out, ZERO, dividend);
2080 } else {
2081 DCHECK_EQ(type, Primitive::kPrimLong);
2082 __ Dsubu(out, ZERO, dividend);
2083 }
2084 } else if (out != dividend) {
2085 __ Move(out, dividend);
2086 }
2087 }
2088}
2089
2090void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2091 DCHECK(instruction->IsDiv() || instruction->IsRem());
2092 Primitive::Type type = instruction->GetResultType();
2093
2094 LocationSummary* locations = instruction->GetLocations();
2095 Location second = locations->InAt(1);
2096 DCHECK(second.IsConstant());
2097
2098 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2099 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2100 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002101 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002102 int ctz_imm = CTZ(abs_imm);
2103
2104 if (instruction->IsDiv()) {
2105 if (type == Primitive::kPrimInt) {
2106 if (ctz_imm == 1) {
2107 // Fast path for division by +/-2, which is very common.
2108 __ Srl(TMP, dividend, 31);
2109 } else {
2110 __ Sra(TMP, dividend, 31);
2111 __ Srl(TMP, TMP, 32 - ctz_imm);
2112 }
2113 __ Addu(out, dividend, TMP);
2114 __ Sra(out, out, ctz_imm);
2115 if (imm < 0) {
2116 __ Subu(out, ZERO, out);
2117 }
2118 } else {
2119 DCHECK_EQ(type, Primitive::kPrimLong);
2120 if (ctz_imm == 1) {
2121 // Fast path for division by +/-2, which is very common.
2122 __ Dsrl32(TMP, dividend, 31);
2123 } else {
2124 __ Dsra32(TMP, dividend, 31);
2125 if (ctz_imm > 32) {
2126 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2127 } else {
2128 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2129 }
2130 }
2131 __ Daddu(out, dividend, TMP);
2132 if (ctz_imm < 32) {
2133 __ Dsra(out, out, ctz_imm);
2134 } else {
2135 __ Dsra32(out, out, ctz_imm - 32);
2136 }
2137 if (imm < 0) {
2138 __ Dsubu(out, ZERO, out);
2139 }
2140 }
2141 } else {
2142 if (type == Primitive::kPrimInt) {
2143 if (ctz_imm == 1) {
2144 // Fast path for modulo +/-2, which is very common.
2145 __ Sra(TMP, dividend, 31);
2146 __ Subu(out, dividend, TMP);
2147 __ Andi(out, out, 1);
2148 __ Addu(out, out, TMP);
2149 } else {
2150 __ Sra(TMP, dividend, 31);
2151 __ Srl(TMP, TMP, 32 - ctz_imm);
2152 __ Addu(out, dividend, TMP);
2153 if (IsUint<16>(abs_imm - 1)) {
2154 __ Andi(out, out, abs_imm - 1);
2155 } else {
2156 __ Sll(out, out, 32 - ctz_imm);
2157 __ Srl(out, out, 32 - ctz_imm);
2158 }
2159 __ Subu(out, out, TMP);
2160 }
2161 } else {
2162 DCHECK_EQ(type, Primitive::kPrimLong);
2163 if (ctz_imm == 1) {
2164 // Fast path for modulo +/-2, which is very common.
2165 __ Dsra32(TMP, dividend, 31);
2166 __ Dsubu(out, dividend, TMP);
2167 __ Andi(out, out, 1);
2168 __ Daddu(out, out, TMP);
2169 } else {
2170 __ Dsra32(TMP, dividend, 31);
2171 if (ctz_imm > 32) {
2172 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2173 } else {
2174 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2175 }
2176 __ Daddu(out, dividend, TMP);
2177 if (IsUint<16>(abs_imm - 1)) {
2178 __ Andi(out, out, abs_imm - 1);
2179 } else {
2180 if (ctz_imm > 32) {
2181 __ Dsll(out, out, 64 - ctz_imm);
2182 __ Dsrl(out, out, 64 - ctz_imm);
2183 } else {
2184 __ Dsll32(out, out, 32 - ctz_imm);
2185 __ Dsrl32(out, out, 32 - ctz_imm);
2186 }
2187 }
2188 __ Dsubu(out, out, TMP);
2189 }
2190 }
2191 }
2192}
2193
2194void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2195 DCHECK(instruction->IsDiv() || instruction->IsRem());
2196
2197 LocationSummary* locations = instruction->GetLocations();
2198 Location second = locations->InAt(1);
2199 DCHECK(second.IsConstant());
2200
2201 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2202 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2203 int64_t imm = Int64FromConstant(second.GetConstant());
2204
2205 Primitive::Type type = instruction->GetResultType();
2206 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2207
2208 int64_t magic;
2209 int shift;
2210 CalculateMagicAndShiftForDivRem(imm,
2211 (type == Primitive::kPrimLong),
2212 &magic,
2213 &shift);
2214
2215 if (type == Primitive::kPrimInt) {
2216 __ LoadConst32(TMP, magic);
2217 __ MuhR6(TMP, dividend, TMP);
2218
2219 if (imm > 0 && magic < 0) {
2220 __ Addu(TMP, TMP, dividend);
2221 } else if (imm < 0 && magic > 0) {
2222 __ Subu(TMP, TMP, dividend);
2223 }
2224
2225 if (shift != 0) {
2226 __ Sra(TMP, TMP, shift);
2227 }
2228
2229 if (instruction->IsDiv()) {
2230 __ Sra(out, TMP, 31);
2231 __ Subu(out, TMP, out);
2232 } else {
2233 __ Sra(AT, TMP, 31);
2234 __ Subu(AT, TMP, AT);
2235 __ LoadConst32(TMP, imm);
2236 __ MulR6(TMP, AT, TMP);
2237 __ Subu(out, dividend, TMP);
2238 }
2239 } else {
2240 __ LoadConst64(TMP, magic);
2241 __ Dmuh(TMP, dividend, TMP);
2242
2243 if (imm > 0 && magic < 0) {
2244 __ Daddu(TMP, TMP, dividend);
2245 } else if (imm < 0 && magic > 0) {
2246 __ Dsubu(TMP, TMP, dividend);
2247 }
2248
2249 if (shift >= 32) {
2250 __ Dsra32(TMP, TMP, shift - 32);
2251 } else if (shift > 0) {
2252 __ Dsra(TMP, TMP, shift);
2253 }
2254
2255 if (instruction->IsDiv()) {
2256 __ Dsra32(out, TMP, 31);
2257 __ Dsubu(out, TMP, out);
2258 } else {
2259 __ Dsra32(AT, TMP, 31);
2260 __ Dsubu(AT, TMP, AT);
2261 __ LoadConst64(TMP, imm);
2262 __ Dmul(TMP, AT, TMP);
2263 __ Dsubu(out, dividend, TMP);
2264 }
2265 }
2266}
2267
2268void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2269 DCHECK(instruction->IsDiv() || instruction->IsRem());
2270 Primitive::Type type = instruction->GetResultType();
2271 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2272
2273 LocationSummary* locations = instruction->GetLocations();
2274 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2275 Location second = locations->InAt(1);
2276
2277 if (second.IsConstant()) {
2278 int64_t imm = Int64FromConstant(second.GetConstant());
2279 if (imm == 0) {
2280 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2281 } else if (imm == 1 || imm == -1) {
2282 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002283 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002284 DivRemByPowerOfTwo(instruction);
2285 } else {
2286 DCHECK(imm <= -2 || imm >= 2);
2287 GenerateDivRemWithAnyConstant(instruction);
2288 }
2289 } else {
2290 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2291 GpuRegister divisor = second.AsRegister<GpuRegister>();
2292 if (instruction->IsDiv()) {
2293 if (type == Primitive::kPrimInt)
2294 __ DivR6(out, dividend, divisor);
2295 else
2296 __ Ddiv(out, dividend, divisor);
2297 } else {
2298 if (type == Primitive::kPrimInt)
2299 __ ModR6(out, dividend, divisor);
2300 else
2301 __ Dmod(out, dividend, divisor);
2302 }
2303 }
2304}
2305
Alexey Frunze4dda3372015-06-01 18:31:49 -07002306void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2307 LocationSummary* locations =
2308 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2309 switch (div->GetResultType()) {
2310 case Primitive::kPrimInt:
2311 case Primitive::kPrimLong:
2312 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002313 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2315 break;
2316
2317 case Primitive::kPrimFloat:
2318 case Primitive::kPrimDouble:
2319 locations->SetInAt(0, Location::RequiresFpuRegister());
2320 locations->SetInAt(1, Location::RequiresFpuRegister());
2321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2322 break;
2323
2324 default:
2325 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2326 }
2327}
2328
2329void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2330 Primitive::Type type = instruction->GetType();
2331 LocationSummary* locations = instruction->GetLocations();
2332
2333 switch (type) {
2334 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002335 case Primitive::kPrimLong:
2336 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002337 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002338 case Primitive::kPrimFloat:
2339 case Primitive::kPrimDouble: {
2340 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2341 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2342 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2343 if (type == Primitive::kPrimFloat)
2344 __ DivS(dst, lhs, rhs);
2345 else
2346 __ DivD(dst, lhs, rhs);
2347 break;
2348 }
2349 default:
2350 LOG(FATAL) << "Unexpected div type " << type;
2351 }
2352}
2353
2354void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002355 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002356 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002357}
2358
2359void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2360 SlowPathCodeMIPS64* slow_path =
2361 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2362 codegen_->AddSlowPath(slow_path);
2363 Location value = instruction->GetLocations()->InAt(0);
2364
2365 Primitive::Type type = instruction->GetType();
2366
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002367 if (!Primitive::IsIntegralType(type)) {
2368 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002369 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002370 }
2371
2372 if (value.IsConstant()) {
2373 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2374 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002375 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002376 } else {
2377 // A division by a non-null constant is valid. We don't need to perform
2378 // any check, so simply fall through.
2379 }
2380 } else {
2381 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2382 }
2383}
2384
2385void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2386 LocationSummary* locations =
2387 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2388 locations->SetOut(Location::ConstantLocation(constant));
2389}
2390
2391void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2392 // Will be generated at use site.
2393}
2394
2395void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2396 exit->SetLocations(nullptr);
2397}
2398
2399void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2400}
2401
2402void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2403 LocationSummary* locations =
2404 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2405 locations->SetOut(Location::ConstantLocation(constant));
2406}
2407
2408void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2409 // Will be generated at use site.
2410}
2411
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002413 DCHECK(!successor->IsExitBlock());
2414 HBasicBlock* block = got->GetBlock();
2415 HInstruction* previous = got->GetPrevious();
2416 HLoopInformation* info = block->GetLoopInformation();
2417
2418 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2419 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2420 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2421 return;
2422 }
2423 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2424 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2425 }
2426 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002427 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002428 }
2429}
2430
David Brazdilfc6a86a2015-06-26 10:33:45 +00002431void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2432 got->SetLocations(nullptr);
2433}
2434
2435void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2436 HandleGoto(got, got->GetSuccessor());
2437}
2438
2439void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2440 try_boundary->SetLocations(nullptr);
2441}
2442
2443void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2444 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2445 if (!successor->IsExitBlock()) {
2446 HandleGoto(try_boundary, successor);
2447 }
2448}
2449
Alexey Frunze299a9392015-12-08 16:08:02 -08002450void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2451 bool is64bit,
2452 LocationSummary* locations) {
2453 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2454 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2455 Location rhs_location = locations->InAt(1);
2456 GpuRegister rhs_reg = ZERO;
2457 int64_t rhs_imm = 0;
2458 bool use_imm = rhs_location.IsConstant();
2459 if (use_imm) {
2460 if (is64bit) {
2461 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2462 } else {
2463 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2464 }
2465 } else {
2466 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2467 }
2468 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2469
2470 switch (cond) {
2471 case kCondEQ:
2472 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002473 if (use_imm && IsInt<16>(-rhs_imm)) {
2474 if (rhs_imm == 0) {
2475 if (cond == kCondEQ) {
2476 __ Sltiu(dst, lhs, 1);
2477 } else {
2478 __ Sltu(dst, ZERO, lhs);
2479 }
2480 } else {
2481 if (is64bit) {
2482 __ Daddiu(dst, lhs, -rhs_imm);
2483 } else {
2484 __ Addiu(dst, lhs, -rhs_imm);
2485 }
2486 if (cond == kCondEQ) {
2487 __ Sltiu(dst, dst, 1);
2488 } else {
2489 __ Sltu(dst, ZERO, dst);
2490 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002491 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002492 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002493 if (use_imm && IsUint<16>(rhs_imm)) {
2494 __ Xori(dst, lhs, rhs_imm);
2495 } else {
2496 if (use_imm) {
2497 rhs_reg = TMP;
2498 __ LoadConst64(rhs_reg, rhs_imm);
2499 }
2500 __ Xor(dst, lhs, rhs_reg);
2501 }
2502 if (cond == kCondEQ) {
2503 __ Sltiu(dst, dst, 1);
2504 } else {
2505 __ Sltu(dst, ZERO, dst);
2506 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002507 }
2508 break;
2509
2510 case kCondLT:
2511 case kCondGE:
2512 if (use_imm && IsInt<16>(rhs_imm)) {
2513 __ Slti(dst, lhs, rhs_imm);
2514 } else {
2515 if (use_imm) {
2516 rhs_reg = TMP;
2517 __ LoadConst64(rhs_reg, rhs_imm);
2518 }
2519 __ Slt(dst, lhs, rhs_reg);
2520 }
2521 if (cond == kCondGE) {
2522 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2523 // only the slt instruction but no sge.
2524 __ Xori(dst, dst, 1);
2525 }
2526 break;
2527
2528 case kCondLE:
2529 case kCondGT:
2530 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2531 // Simulate lhs <= rhs via lhs < rhs + 1.
2532 __ Slti(dst, lhs, rhs_imm_plus_one);
2533 if (cond == kCondGT) {
2534 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2535 // only the slti instruction but no sgti.
2536 __ Xori(dst, dst, 1);
2537 }
2538 } else {
2539 if (use_imm) {
2540 rhs_reg = TMP;
2541 __ LoadConst64(rhs_reg, rhs_imm);
2542 }
2543 __ Slt(dst, rhs_reg, lhs);
2544 if (cond == kCondLE) {
2545 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2546 // only the slt instruction but no sle.
2547 __ Xori(dst, dst, 1);
2548 }
2549 }
2550 break;
2551
2552 case kCondB:
2553 case kCondAE:
2554 if (use_imm && IsInt<16>(rhs_imm)) {
2555 // Sltiu sign-extends its 16-bit immediate operand before
2556 // the comparison and thus lets us compare directly with
2557 // unsigned values in the ranges [0, 0x7fff] and
2558 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2559 __ Sltiu(dst, lhs, rhs_imm);
2560 } else {
2561 if (use_imm) {
2562 rhs_reg = TMP;
2563 __ LoadConst64(rhs_reg, rhs_imm);
2564 }
2565 __ Sltu(dst, lhs, rhs_reg);
2566 }
2567 if (cond == kCondAE) {
2568 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2569 // only the sltu instruction but no sgeu.
2570 __ Xori(dst, dst, 1);
2571 }
2572 break;
2573
2574 case kCondBE:
2575 case kCondA:
2576 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2577 // Simulate lhs <= rhs via lhs < rhs + 1.
2578 // Note that this only works if rhs + 1 does not overflow
2579 // to 0, hence the check above.
2580 // Sltiu sign-extends its 16-bit immediate operand before
2581 // the comparison and thus lets us compare directly with
2582 // unsigned values in the ranges [0, 0x7fff] and
2583 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2584 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2585 if (cond == kCondA) {
2586 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2587 // only the sltiu instruction but no sgtiu.
2588 __ Xori(dst, dst, 1);
2589 }
2590 } else {
2591 if (use_imm) {
2592 rhs_reg = TMP;
2593 __ LoadConst64(rhs_reg, rhs_imm);
2594 }
2595 __ Sltu(dst, rhs_reg, lhs);
2596 if (cond == kCondBE) {
2597 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2598 // only the sltu instruction but no sleu.
2599 __ Xori(dst, dst, 1);
2600 }
2601 }
2602 break;
2603 }
2604}
2605
2606void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2607 bool is64bit,
2608 LocationSummary* locations,
2609 Mips64Label* label) {
2610 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2611 Location rhs_location = locations->InAt(1);
2612 GpuRegister rhs_reg = ZERO;
2613 int64_t rhs_imm = 0;
2614 bool use_imm = rhs_location.IsConstant();
2615 if (use_imm) {
2616 if (is64bit) {
2617 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2618 } else {
2619 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2620 }
2621 } else {
2622 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2623 }
2624
2625 if (use_imm && rhs_imm == 0) {
2626 switch (cond) {
2627 case kCondEQ:
2628 case kCondBE: // <= 0 if zero
2629 __ Beqzc(lhs, label);
2630 break;
2631 case kCondNE:
2632 case kCondA: // > 0 if non-zero
2633 __ Bnezc(lhs, label);
2634 break;
2635 case kCondLT:
2636 __ Bltzc(lhs, label);
2637 break;
2638 case kCondGE:
2639 __ Bgezc(lhs, label);
2640 break;
2641 case kCondLE:
2642 __ Blezc(lhs, label);
2643 break;
2644 case kCondGT:
2645 __ Bgtzc(lhs, label);
2646 break;
2647 case kCondB: // always false
2648 break;
2649 case kCondAE: // always true
2650 __ Bc(label);
2651 break;
2652 }
2653 } else {
2654 if (use_imm) {
2655 rhs_reg = TMP;
2656 __ LoadConst64(rhs_reg, rhs_imm);
2657 }
2658 switch (cond) {
2659 case kCondEQ:
2660 __ Beqc(lhs, rhs_reg, label);
2661 break;
2662 case kCondNE:
2663 __ Bnec(lhs, rhs_reg, label);
2664 break;
2665 case kCondLT:
2666 __ Bltc(lhs, rhs_reg, label);
2667 break;
2668 case kCondGE:
2669 __ Bgec(lhs, rhs_reg, label);
2670 break;
2671 case kCondLE:
2672 __ Bgec(rhs_reg, lhs, label);
2673 break;
2674 case kCondGT:
2675 __ Bltc(rhs_reg, lhs, label);
2676 break;
2677 case kCondB:
2678 __ Bltuc(lhs, rhs_reg, label);
2679 break;
2680 case kCondAE:
2681 __ Bgeuc(lhs, rhs_reg, label);
2682 break;
2683 case kCondBE:
2684 __ Bgeuc(rhs_reg, lhs, label);
2685 break;
2686 case kCondA:
2687 __ Bltuc(rhs_reg, lhs, label);
2688 break;
2689 }
2690 }
2691}
2692
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002693void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2694 bool gt_bias,
2695 Primitive::Type type,
2696 LocationSummary* locations) {
2697 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2698 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2699 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2700 if (type == Primitive::kPrimFloat) {
2701 switch (cond) {
2702 case kCondEQ:
2703 __ CmpEqS(FTMP, lhs, rhs);
2704 __ Mfc1(dst, FTMP);
2705 __ Andi(dst, dst, 1);
2706 break;
2707 case kCondNE:
2708 __ CmpEqS(FTMP, lhs, rhs);
2709 __ Mfc1(dst, FTMP);
2710 __ Addiu(dst, dst, 1);
2711 break;
2712 case kCondLT:
2713 if (gt_bias) {
2714 __ CmpLtS(FTMP, lhs, rhs);
2715 } else {
2716 __ CmpUltS(FTMP, lhs, rhs);
2717 }
2718 __ Mfc1(dst, FTMP);
2719 __ Andi(dst, dst, 1);
2720 break;
2721 case kCondLE:
2722 if (gt_bias) {
2723 __ CmpLeS(FTMP, lhs, rhs);
2724 } else {
2725 __ CmpUleS(FTMP, lhs, rhs);
2726 }
2727 __ Mfc1(dst, FTMP);
2728 __ Andi(dst, dst, 1);
2729 break;
2730 case kCondGT:
2731 if (gt_bias) {
2732 __ CmpUltS(FTMP, rhs, lhs);
2733 } else {
2734 __ CmpLtS(FTMP, rhs, lhs);
2735 }
2736 __ Mfc1(dst, FTMP);
2737 __ Andi(dst, dst, 1);
2738 break;
2739 case kCondGE:
2740 if (gt_bias) {
2741 __ CmpUleS(FTMP, rhs, lhs);
2742 } else {
2743 __ CmpLeS(FTMP, rhs, lhs);
2744 }
2745 __ Mfc1(dst, FTMP);
2746 __ Andi(dst, dst, 1);
2747 break;
2748 default:
2749 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2750 UNREACHABLE();
2751 }
2752 } else {
2753 DCHECK_EQ(type, Primitive::kPrimDouble);
2754 switch (cond) {
2755 case kCondEQ:
2756 __ CmpEqD(FTMP, lhs, rhs);
2757 __ Mfc1(dst, FTMP);
2758 __ Andi(dst, dst, 1);
2759 break;
2760 case kCondNE:
2761 __ CmpEqD(FTMP, lhs, rhs);
2762 __ Mfc1(dst, FTMP);
2763 __ Addiu(dst, dst, 1);
2764 break;
2765 case kCondLT:
2766 if (gt_bias) {
2767 __ CmpLtD(FTMP, lhs, rhs);
2768 } else {
2769 __ CmpUltD(FTMP, lhs, rhs);
2770 }
2771 __ Mfc1(dst, FTMP);
2772 __ Andi(dst, dst, 1);
2773 break;
2774 case kCondLE:
2775 if (gt_bias) {
2776 __ CmpLeD(FTMP, lhs, rhs);
2777 } else {
2778 __ CmpUleD(FTMP, lhs, rhs);
2779 }
2780 __ Mfc1(dst, FTMP);
2781 __ Andi(dst, dst, 1);
2782 break;
2783 case kCondGT:
2784 if (gt_bias) {
2785 __ CmpUltD(FTMP, rhs, lhs);
2786 } else {
2787 __ CmpLtD(FTMP, rhs, lhs);
2788 }
2789 __ Mfc1(dst, FTMP);
2790 __ Andi(dst, dst, 1);
2791 break;
2792 case kCondGE:
2793 if (gt_bias) {
2794 __ CmpUleD(FTMP, rhs, lhs);
2795 } else {
2796 __ CmpLeD(FTMP, rhs, lhs);
2797 }
2798 __ Mfc1(dst, FTMP);
2799 __ Andi(dst, dst, 1);
2800 break;
2801 default:
2802 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2803 UNREACHABLE();
2804 }
2805 }
2806}
2807
Alexey Frunze299a9392015-12-08 16:08:02 -08002808void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2809 bool gt_bias,
2810 Primitive::Type type,
2811 LocationSummary* locations,
2812 Mips64Label* label) {
2813 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2814 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2815 if (type == Primitive::kPrimFloat) {
2816 switch (cond) {
2817 case kCondEQ:
2818 __ CmpEqS(FTMP, lhs, rhs);
2819 __ Bc1nez(FTMP, label);
2820 break;
2821 case kCondNE:
2822 __ CmpEqS(FTMP, lhs, rhs);
2823 __ Bc1eqz(FTMP, label);
2824 break;
2825 case kCondLT:
2826 if (gt_bias) {
2827 __ CmpLtS(FTMP, lhs, rhs);
2828 } else {
2829 __ CmpUltS(FTMP, lhs, rhs);
2830 }
2831 __ Bc1nez(FTMP, label);
2832 break;
2833 case kCondLE:
2834 if (gt_bias) {
2835 __ CmpLeS(FTMP, lhs, rhs);
2836 } else {
2837 __ CmpUleS(FTMP, lhs, rhs);
2838 }
2839 __ Bc1nez(FTMP, label);
2840 break;
2841 case kCondGT:
2842 if (gt_bias) {
2843 __ CmpUltS(FTMP, rhs, lhs);
2844 } else {
2845 __ CmpLtS(FTMP, rhs, lhs);
2846 }
2847 __ Bc1nez(FTMP, label);
2848 break;
2849 case kCondGE:
2850 if (gt_bias) {
2851 __ CmpUleS(FTMP, rhs, lhs);
2852 } else {
2853 __ CmpLeS(FTMP, rhs, lhs);
2854 }
2855 __ Bc1nez(FTMP, label);
2856 break;
2857 default:
2858 LOG(FATAL) << "Unexpected non-floating-point condition";
2859 }
2860 } else {
2861 DCHECK_EQ(type, Primitive::kPrimDouble);
2862 switch (cond) {
2863 case kCondEQ:
2864 __ CmpEqD(FTMP, lhs, rhs);
2865 __ Bc1nez(FTMP, label);
2866 break;
2867 case kCondNE:
2868 __ CmpEqD(FTMP, lhs, rhs);
2869 __ Bc1eqz(FTMP, label);
2870 break;
2871 case kCondLT:
2872 if (gt_bias) {
2873 __ CmpLtD(FTMP, lhs, rhs);
2874 } else {
2875 __ CmpUltD(FTMP, lhs, rhs);
2876 }
2877 __ Bc1nez(FTMP, label);
2878 break;
2879 case kCondLE:
2880 if (gt_bias) {
2881 __ CmpLeD(FTMP, lhs, rhs);
2882 } else {
2883 __ CmpUleD(FTMP, lhs, rhs);
2884 }
2885 __ Bc1nez(FTMP, label);
2886 break;
2887 case kCondGT:
2888 if (gt_bias) {
2889 __ CmpUltD(FTMP, rhs, lhs);
2890 } else {
2891 __ CmpLtD(FTMP, rhs, lhs);
2892 }
2893 __ Bc1nez(FTMP, label);
2894 break;
2895 case kCondGE:
2896 if (gt_bias) {
2897 __ CmpUleD(FTMP, rhs, lhs);
2898 } else {
2899 __ CmpLeD(FTMP, rhs, lhs);
2900 }
2901 __ Bc1nez(FTMP, label);
2902 break;
2903 default:
2904 LOG(FATAL) << "Unexpected non-floating-point condition";
2905 }
2906 }
2907}
2908
Alexey Frunze4dda3372015-06-01 18:31:49 -07002909void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002910 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002911 Mips64Label* true_target,
2912 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002913 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002914
David Brazdil0debae72015-11-12 18:37:00 +00002915 if (true_target == nullptr && false_target == nullptr) {
2916 // Nothing to do. The code always falls through.
2917 return;
2918 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002919 // Constant condition, statically compared against "true" (integer value 1).
2920 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002921 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002922 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002924 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002925 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002926 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002927 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002928 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002929 }
David Brazdil0debae72015-11-12 18:37:00 +00002930 return;
2931 }
2932
2933 // The following code generates these patterns:
2934 // (1) true_target == nullptr && false_target != nullptr
2935 // - opposite condition true => branch to false_target
2936 // (2) true_target != nullptr && false_target == nullptr
2937 // - condition true => branch to true_target
2938 // (3) true_target != nullptr && false_target != nullptr
2939 // - condition true => branch to true_target
2940 // - branch to false_target
2941 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002942 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002943 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002944 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002945 if (true_target == nullptr) {
2946 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2947 } else {
2948 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2949 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002950 } else {
2951 // The condition instruction has not been materialized, use its inputs as
2952 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002953 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002954 Primitive::Type type = condition->InputAt(0)->GetType();
2955 LocationSummary* locations = cond->GetLocations();
2956 IfCondition if_cond = condition->GetCondition();
2957 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002958
David Brazdil0debae72015-11-12 18:37:00 +00002959 if (true_target == nullptr) {
2960 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002961 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002962 }
2963
Alexey Frunze299a9392015-12-08 16:08:02 -08002964 switch (type) {
2965 default:
2966 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2967 break;
2968 case Primitive::kPrimLong:
2969 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2970 break;
2971 case Primitive::kPrimFloat:
2972 case Primitive::kPrimDouble:
2973 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2974 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002975 }
2976 }
David Brazdil0debae72015-11-12 18:37:00 +00002977
2978 // If neither branch falls through (case 3), the conditional branch to `true_target`
2979 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2980 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002981 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002982 }
2983}
2984
2985void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2986 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002987 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 locations->SetInAt(0, Location::RequiresRegister());
2989 }
2990}
2991
2992void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002993 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2994 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002995 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002996 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002997 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002998 nullptr : codegen_->GetLabelOf(false_successor);
2999 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003000}
3001
3002void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3003 LocationSummary* locations = new (GetGraph()->GetArena())
3004 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003005 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003006 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003007 locations->SetInAt(0, Location::RequiresRegister());
3008 }
3009}
3010
3011void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003012 SlowPathCodeMIPS64* slow_path =
3013 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003014 GenerateTestAndBranch(deoptimize,
3015 /* condition_input_index */ 0,
3016 slow_path->GetEntryLabel(),
3017 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003018}
3019
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003020void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3021 LocationSummary* locations = new (GetGraph()->GetArena())
3022 LocationSummary(flag, LocationSummary::kNoCall);
3023 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003024}
3025
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003026void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3027 __ LoadFromOffset(kLoadWord,
3028 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3029 SP,
3030 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003031}
3032
David Brazdil74eb1b22015-12-14 11:44:01 +00003033void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3034 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3035 if (Primitive::IsFloatingPointType(select->GetType())) {
3036 locations->SetInAt(0, Location::RequiresFpuRegister());
3037 locations->SetInAt(1, Location::RequiresFpuRegister());
3038 } else {
3039 locations->SetInAt(0, Location::RequiresRegister());
3040 locations->SetInAt(1, Location::RequiresRegister());
3041 }
3042 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3043 locations->SetInAt(2, Location::RequiresRegister());
3044 }
3045 locations->SetOut(Location::SameAsFirstInput());
3046}
3047
3048void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3049 LocationSummary* locations = select->GetLocations();
3050 Mips64Label false_target;
3051 GenerateTestAndBranch(select,
3052 /* condition_input_index */ 2,
3053 /* true_target */ nullptr,
3054 &false_target);
3055 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3056 __ Bind(&false_target);
3057}
3058
David Srbecky0cf44932015-12-09 14:09:59 +00003059void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3060 new (GetGraph()->GetArena()) LocationSummary(info);
3061}
3062
David Srbeckyd28f4a02016-03-14 17:14:24 +00003063void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3064 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003065}
3066
3067void CodeGeneratorMIPS64::GenerateNop() {
3068 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003069}
3070
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3072 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3073 LocationSummary* locations =
3074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3075 locations->SetInAt(0, Location::RequiresRegister());
3076 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3077 locations->SetOut(Location::RequiresFpuRegister());
3078 } else {
3079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3080 }
3081}
3082
3083void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3084 const FieldInfo& field_info) {
3085 Primitive::Type type = field_info.GetFieldType();
3086 LocationSummary* locations = instruction->GetLocations();
3087 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3088 LoadOperandType load_type = kLoadUnsignedByte;
3089 switch (type) {
3090 case Primitive::kPrimBoolean:
3091 load_type = kLoadUnsignedByte;
3092 break;
3093 case Primitive::kPrimByte:
3094 load_type = kLoadSignedByte;
3095 break;
3096 case Primitive::kPrimShort:
3097 load_type = kLoadSignedHalfword;
3098 break;
3099 case Primitive::kPrimChar:
3100 load_type = kLoadUnsignedHalfword;
3101 break;
3102 case Primitive::kPrimInt:
3103 case Primitive::kPrimFloat:
3104 load_type = kLoadWord;
3105 break;
3106 case Primitive::kPrimLong:
3107 case Primitive::kPrimDouble:
3108 load_type = kLoadDoubleword;
3109 break;
3110 case Primitive::kPrimNot:
3111 load_type = kLoadUnsignedWord;
3112 break;
3113 case Primitive::kPrimVoid:
3114 LOG(FATAL) << "Unreachable type " << type;
3115 UNREACHABLE();
3116 }
3117 if (!Primitive::IsFloatingPointType(type)) {
3118 DCHECK(locations->Out().IsRegister());
3119 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3120 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
3121 } else {
3122 DCHECK(locations->Out().IsFpuRegister());
3123 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3124 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
3125 }
3126
3127 codegen_->MaybeRecordImplicitNullCheck(instruction);
3128 // TODO: memory barrier?
3129}
3130
3131void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3132 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3135 locations->SetInAt(0, Location::RequiresRegister());
3136 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3137 locations->SetInAt(1, Location::RequiresFpuRegister());
3138 } else {
3139 locations->SetInAt(1, Location::RequiresRegister());
3140 }
3141}
3142
3143void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003144 const FieldInfo& field_info,
3145 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003146 Primitive::Type type = field_info.GetFieldType();
3147 LocationSummary* locations = instruction->GetLocations();
3148 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3149 StoreOperandType store_type = kStoreByte;
3150 switch (type) {
3151 case Primitive::kPrimBoolean:
3152 case Primitive::kPrimByte:
3153 store_type = kStoreByte;
3154 break;
3155 case Primitive::kPrimShort:
3156 case Primitive::kPrimChar:
3157 store_type = kStoreHalfword;
3158 break;
3159 case Primitive::kPrimInt:
3160 case Primitive::kPrimFloat:
3161 case Primitive::kPrimNot:
3162 store_type = kStoreWord;
3163 break;
3164 case Primitive::kPrimLong:
3165 case Primitive::kPrimDouble:
3166 store_type = kStoreDoubleword;
3167 break;
3168 case Primitive::kPrimVoid:
3169 LOG(FATAL) << "Unreachable type " << type;
3170 UNREACHABLE();
3171 }
3172 if (!Primitive::IsFloatingPointType(type)) {
3173 DCHECK(locations->InAt(1).IsRegister());
3174 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
3175 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
3176 } else {
3177 DCHECK(locations->InAt(1).IsFpuRegister());
3178 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
3179 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
3180 }
3181
3182 codegen_->MaybeRecordImplicitNullCheck(instruction);
3183 // TODO: memory barriers?
3184 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3185 DCHECK(locations->InAt(1).IsRegister());
3186 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003187 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003188 }
3189}
3190
3191void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3192 HandleFieldGet(instruction, instruction->GetFieldInfo());
3193}
3194
3195void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3196 HandleFieldGet(instruction, instruction->GetFieldInfo());
3197}
3198
3199void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3200 HandleFieldSet(instruction, instruction->GetFieldInfo());
3201}
3202
3203void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003204 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003205}
3206
Alexey Frunzef63f5692016-12-13 17:43:11 -08003207void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3208 HInstruction* instruction ATTRIBUTE_UNUSED,
3209 Location root,
3210 GpuRegister obj,
3211 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003212 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3213 if (kEmitCompilerReadBarrier) {
3214 UNIMPLEMENTED(FATAL) << "for read barrier";
3215 } else {
3216 // Plain GC root load with no read barrier.
3217 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3218 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3219 // Note that GC roots are not affected by heap poisoning, thus we
3220 // do not have to unpoison `root_reg` here.
3221 }
3222}
3223
Alexey Frunze4dda3372015-06-01 18:31:49 -07003224void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3225 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003226 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, Location::RequiresRegister());
3230 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003231 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003232 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3233}
3234
3235void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3236 LocationSummary* locations = instruction->GetLocations();
3237 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3238 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3239 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3240
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003241 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003242
3243 // Return 0 if `obj` is null.
3244 // TODO: Avoid this check if we know `obj` is not null.
3245 __ Move(out, ZERO);
3246 __ Beqzc(obj, &done);
3247
3248 // Compare the class of `obj` with `cls`.
3249 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003250 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003251 // Classes must be equal for the instanceof to succeed.
3252 __ Xor(out, out, cls);
3253 __ Sltiu(out, out, 1);
3254 } else {
3255 // If the classes are not equal, we go into a slow path.
3256 DCHECK(locations->OnlyCallsOnSlowPath());
3257 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003258 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003259 codegen_->AddSlowPath(slow_path);
3260 __ Bnec(out, cls, slow_path->GetEntryLabel());
3261 __ LoadConst32(out, 1);
3262 __ Bind(slow_path->GetExitLabel());
3263 }
3264
3265 __ Bind(&done);
3266}
3267
3268void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3269 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3270 locations->SetOut(Location::ConstantLocation(constant));
3271}
3272
3273void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3274 // Will be generated at use site.
3275}
3276
3277void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3279 locations->SetOut(Location::ConstantLocation(constant));
3280}
3281
3282void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3283 // Will be generated at use site.
3284}
3285
Calin Juravle175dc732015-08-25 15:42:32 +01003286void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3287 // The trampoline uses the same calling convention as dex calling conventions,
3288 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3289 // the method_idx.
3290 HandleInvoke(invoke);
3291}
3292
3293void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3294 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3295}
3296
Alexey Frunze4dda3372015-06-01 18:31:49 -07003297void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3298 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3299 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3300}
3301
3302void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3303 HandleInvoke(invoke);
3304 // The register T0 is required to be used for the hidden argument in
3305 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3306 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3307}
3308
3309void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3310 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3311 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003312 Location receiver = invoke->GetLocations()->InAt(0);
3313 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003314 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003315
3316 // Set the hidden argument.
3317 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3318 invoke->GetDexMethodIndex());
3319
3320 // temp = object->GetClass();
3321 if (receiver.IsStackSlot()) {
3322 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3323 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3324 } else {
3325 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3326 }
3327 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003328 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3329 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3330 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003331 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003332 // temp = temp->GetImtEntryAt(method_offset);
3333 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3334 // T9 = temp->GetEntryPoint();
3335 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3336 // T9();
3337 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003338 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003339 DCHECK(!codegen_->IsLeafMethod());
3340 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3341}
3342
3343void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003344 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3345 if (intrinsic.TryDispatch(invoke)) {
3346 return;
3347 }
3348
Alexey Frunze4dda3372015-06-01 18:31:49 -07003349 HandleInvoke(invoke);
3350}
3351
3352void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003353 // Explicit clinit checks triggered by static invokes must have been pruned by
3354 // art::PrepareForRegisterAllocation.
3355 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003356
Chris Larsen3039e382015-08-26 07:54:08 -07003357 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3358 if (intrinsic.TryDispatch(invoke)) {
3359 return;
3360 }
3361
Alexey Frunze4dda3372015-06-01 18:31:49 -07003362 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003363}
3364
Orion Hodsonac141392017-01-13 11:53:47 +00003365void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3366 HandleInvoke(invoke);
3367}
3368
3369void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3370 codegen_->GenerateInvokePolymorphicCall(invoke);
3371}
3372
Chris Larsen3039e382015-08-26 07:54:08 -07003373static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003374 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003375 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3376 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003377 return true;
3378 }
3379 return false;
3380}
3381
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003382HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003383 HLoadString::LoadKind desired_string_load_kind) {
3384 if (kEmitCompilerReadBarrier) {
3385 UNIMPLEMENTED(FATAL) << "for read barrier";
3386 }
3387 bool fallback_load = false;
3388 switch (desired_string_load_kind) {
3389 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3390 DCHECK(!GetCompilerOptions().GetCompilePic());
3391 break;
3392 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3393 DCHECK(GetCompilerOptions().GetCompilePic());
3394 break;
3395 case HLoadString::LoadKind::kBootImageAddress:
3396 break;
3397 case HLoadString::LoadKind::kBssEntry:
3398 DCHECK(!Runtime::Current()->UseJitCompilation());
3399 break;
3400 case HLoadString::LoadKind::kDexCacheViaMethod:
3401 break;
3402 case HLoadString::LoadKind::kJitTableAddress:
3403 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003404 break;
3405 }
3406 if (fallback_load) {
3407 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3408 }
3409 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003410}
3411
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003412HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3413 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003414 if (kEmitCompilerReadBarrier) {
3415 UNIMPLEMENTED(FATAL) << "for read barrier";
3416 }
3417 bool fallback_load = false;
3418 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003419 case HLoadClass::LoadKind::kInvalid:
3420 LOG(FATAL) << "UNREACHABLE";
3421 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003422 case HLoadClass::LoadKind::kReferrersClass:
3423 break;
3424 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3425 DCHECK(!GetCompilerOptions().GetCompilePic());
3426 break;
3427 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3428 DCHECK(GetCompilerOptions().GetCompilePic());
3429 break;
3430 case HLoadClass::LoadKind::kBootImageAddress:
3431 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003432 case HLoadClass::LoadKind::kBssEntry:
3433 DCHECK(!Runtime::Current()->UseJitCompilation());
3434 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003435 case HLoadClass::LoadKind::kJitTableAddress:
3436 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003437 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003438 case HLoadClass::LoadKind::kDexCacheViaMethod:
3439 break;
3440 }
3441 if (fallback_load) {
3442 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3443 }
3444 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003445}
3446
Vladimir Markodc151b22015-10-15 18:02:30 +01003447HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3448 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003449 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003450 // On MIPS64 we support all dispatch types.
3451 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003452}
3453
Alexey Frunze4dda3372015-06-01 18:31:49 -07003454void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3455 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003456 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003457 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3458 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3459
Alexey Frunze19f6c692016-11-30 19:19:55 -08003460 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003461 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003462 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003463 uint32_t offset =
3464 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003465 __ LoadFromOffset(kLoadDoubleword,
3466 temp.AsRegister<GpuRegister>(),
3467 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003468 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003469 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003470 }
Vladimir Marko58155012015-08-19 12:49:41 +00003471 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003472 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003473 break;
3474 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003475 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3476 kLoadDoubleword,
3477 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003478 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003479 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3480 uint32_t offset = invoke->GetDexCacheArrayOffset();
3481 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003482 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003483 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3484 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3485 break;
3486 }
Vladimir Marko58155012015-08-19 12:49:41 +00003487 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003488 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003489 GpuRegister reg = temp.AsRegister<GpuRegister>();
3490 GpuRegister method_reg;
3491 if (current_method.IsRegister()) {
3492 method_reg = current_method.AsRegister<GpuRegister>();
3493 } else {
3494 // TODO: use the appropriate DCHECK() here if possible.
3495 // DCHECK(invoke->GetLocations()->Intrinsified());
3496 DCHECK(!current_method.IsValid());
3497 method_reg = reg;
3498 __ Ld(reg, SP, kCurrentMethodStackOffset);
3499 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003500
Vladimir Marko58155012015-08-19 12:49:41 +00003501 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003502 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003503 reg,
3504 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003505 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003506 // temp = temp[index_in_cache];
3507 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3508 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003509 __ LoadFromOffset(kLoadDoubleword,
3510 reg,
3511 reg,
3512 CodeGenerator::GetCachePointerOffset(index_in_cache));
3513 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003515 }
3516
Alexey Frunze19f6c692016-11-30 19:19:55 -08003517 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003518 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003519 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003520 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003521 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3522 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3523 __ LoadFromOffset(kLoadDoubleword,
3524 T9,
3525 callee_method.AsRegister<GpuRegister>(),
3526 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003527 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003528 // T9()
3529 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003530 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003531 break;
3532 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533 DCHECK(!IsLeafMethod());
3534}
3535
3536void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003537 // Explicit clinit checks triggered by static invokes must have been pruned by
3538 // art::PrepareForRegisterAllocation.
3539 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003540
3541 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3542 return;
3543 }
3544
3545 LocationSummary* locations = invoke->GetLocations();
3546 codegen_->GenerateStaticOrDirectCall(invoke,
3547 locations->HasTemps()
3548 ? locations->GetTemp(0)
3549 : Location::NoLocation());
3550 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3551}
3552
Alexey Frunze53afca12015-11-05 16:34:23 -08003553void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003554 // Use the calling convention instead of the location of the receiver, as
3555 // intrinsics may have put the receiver in a different register. In the intrinsics
3556 // slow path, the arguments have been moved to the right place, so here we are
3557 // guaranteed that the receiver is the first register of the calling convention.
3558 InvokeDexCallingConvention calling_convention;
3559 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3560
Alexey Frunze53afca12015-11-05 16:34:23 -08003561 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003562 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3563 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003565 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003566
3567 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003568 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003569 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003570 // temp = temp->GetMethodAt(method_offset);
3571 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3572 // T9 = temp->GetEntryPoint();
3573 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3574 // T9();
3575 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003576 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003577}
3578
3579void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3580 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3581 return;
3582 }
3583
3584 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003585 DCHECK(!codegen_->IsLeafMethod());
3586 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3587}
3588
3589void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003590 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3591 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003592 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003593 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003594 cls,
3595 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003596 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003597 return;
3598 }
Vladimir Marko41559982017-01-06 14:04:23 +00003599 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003600
3601 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3602 ? LocationSummary::kCallOnSlowPath
3603 : LocationSummary::kNoCall;
3604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003605 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003606 locations->SetInAt(0, Location::RequiresRegister());
3607 }
3608 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003609}
3610
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003611// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3612// move.
3613void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003614 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3615 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3616 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003617 return;
3618 }
Vladimir Marko41559982017-01-06 14:04:23 +00003619 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003620
Vladimir Marko41559982017-01-06 14:04:23 +00003621 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003622 Location out_loc = locations->Out();
3623 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3624 GpuRegister current_method_reg = ZERO;
3625 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3626 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3627 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3628 }
3629
3630 bool generate_null_check = false;
3631 switch (load_kind) {
3632 case HLoadClass::LoadKind::kReferrersClass:
3633 DCHECK(!cls->CanCallRuntime());
3634 DCHECK(!cls->MustGenerateClinitCheck());
3635 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3636 GenerateGcRootFieldLoad(cls,
3637 out_loc,
3638 current_method_reg,
3639 ArtMethod::DeclaringClassOffset().Int32Value());
3640 break;
3641 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003642 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003643 __ LoadLiteral(out,
3644 kLoadUnsignedWord,
3645 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3646 cls->GetTypeIndex()));
3647 break;
3648 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003649 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003650 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3651 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3652 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3653 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3654 break;
3655 }
3656 case HLoadClass::LoadKind::kBootImageAddress: {
3657 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003658 uint32_t address = dchecked_integral_cast<uint32_t>(
3659 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
3660 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003661 __ LoadLiteral(out,
3662 kLoadUnsignedWord,
3663 codegen_->DeduplicateBootImageAddressLiteral(address));
3664 break;
3665 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003666 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003667 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00003668 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003669 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3670 __ Lwu(out, AT, /* placeholder */ 0x5678);
3671 generate_null_check = true;
3672 break;
3673 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003674 case HLoadClass::LoadKind::kJitTableAddress:
3675 __ LoadLiteral(out,
3676 kLoadUnsignedWord,
3677 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
3678 cls->GetTypeIndex(),
3679 cls->GetClass()));
3680 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003681 break;
Vladimir Marko41559982017-01-06 14:04:23 +00003682 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003683 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00003684 LOG(FATAL) << "UNREACHABLE";
3685 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003686 }
3687
3688 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3689 DCHECK(cls->CanCallRuntime());
3690 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3691 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3692 codegen_->AddSlowPath(slow_path);
3693 if (generate_null_check) {
3694 __ Beqzc(out, slow_path->GetEntryLabel());
3695 }
3696 if (cls->MustGenerateClinitCheck()) {
3697 GenerateClassInitializationCheck(slow_path, out);
3698 } else {
3699 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003700 }
3701 }
3702}
3703
David Brazdilcb1c0552015-08-04 16:22:25 +01003704static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003705 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003706}
3707
Alexey Frunze4dda3372015-06-01 18:31:49 -07003708void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3709 LocationSummary* locations =
3710 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3711 locations->SetOut(Location::RequiresRegister());
3712}
3713
3714void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3715 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003716 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3717}
3718
3719void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3720 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3721}
3722
3723void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3724 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003725}
3726
Alexey Frunze4dda3372015-06-01 18:31:49 -07003727void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003728 HLoadString::LoadKind load_kind = load->GetLoadKind();
3729 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003730 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003731 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3732 InvokeRuntimeCallingConvention calling_convention;
3733 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3734 } else {
3735 locations->SetOut(Location::RequiresRegister());
3736 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003737}
3738
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003739// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3740// move.
3741void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003742 HLoadString::LoadKind load_kind = load->GetLoadKind();
3743 LocationSummary* locations = load->GetLocations();
3744 Location out_loc = locations->Out();
3745 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3746
3747 switch (load_kind) {
3748 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003749 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003750 __ LoadLiteral(out,
3751 kLoadUnsignedWord,
3752 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3753 load->GetStringIndex()));
3754 return; // No dex cache slow path.
3755 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3756 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3757 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003758 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003759 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3760 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3761 return; // No dex cache slow path.
3762 }
3763 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003764 uint32_t address = dchecked_integral_cast<uint32_t>(
3765 reinterpret_cast<uintptr_t>(load->GetString().Get()));
3766 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003767 __ LoadLiteral(out,
3768 kLoadUnsignedWord,
3769 codegen_->DeduplicateBootImageAddressLiteral(address));
3770 return; // No dex cache slow path.
3771 }
3772 case HLoadString::LoadKind::kBssEntry: {
3773 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3774 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003775 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003776 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3777 __ Lwu(out, AT, /* placeholder */ 0x5678);
3778 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3779 codegen_->AddSlowPath(slow_path);
3780 __ Beqzc(out, slow_path->GetEntryLabel());
3781 __ Bind(slow_path->GetExitLabel());
3782 return;
3783 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003784 case HLoadString::LoadKind::kJitTableAddress:
3785 __ LoadLiteral(out,
3786 kLoadUnsignedWord,
3787 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
3788 load->GetStringIndex(),
3789 load->GetString()));
3790 GenerateGcRootFieldLoad(load, out_loc, out, 0);
3791 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003792 default:
3793 break;
3794 }
3795
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003796 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003797 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3798 InvokeRuntimeCallingConvention calling_convention;
3799 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3800 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3801 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802}
3803
Alexey Frunze4dda3372015-06-01 18:31:49 -07003804void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3806 locations->SetOut(Location::ConstantLocation(constant));
3807}
3808
3809void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3810 // Will be generated at use site.
3811}
3812
3813void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3814 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 InvokeRuntimeCallingConvention calling_convention;
3817 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3818}
3819
3820void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003821 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003822 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003823 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003824 if (instruction->IsEnter()) {
3825 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3826 } else {
3827 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3828 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003829}
3830
3831void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3832 LocationSummary* locations =
3833 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3834 switch (mul->GetResultType()) {
3835 case Primitive::kPrimInt:
3836 case Primitive::kPrimLong:
3837 locations->SetInAt(0, Location::RequiresRegister());
3838 locations->SetInAt(1, Location::RequiresRegister());
3839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3840 break;
3841
3842 case Primitive::kPrimFloat:
3843 case Primitive::kPrimDouble:
3844 locations->SetInAt(0, Location::RequiresFpuRegister());
3845 locations->SetInAt(1, Location::RequiresFpuRegister());
3846 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3847 break;
3848
3849 default:
3850 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3851 }
3852}
3853
3854void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3855 Primitive::Type type = instruction->GetType();
3856 LocationSummary* locations = instruction->GetLocations();
3857
3858 switch (type) {
3859 case Primitive::kPrimInt:
3860 case Primitive::kPrimLong: {
3861 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3862 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3863 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3864 if (type == Primitive::kPrimInt)
3865 __ MulR6(dst, lhs, rhs);
3866 else
3867 __ Dmul(dst, lhs, rhs);
3868 break;
3869 }
3870 case Primitive::kPrimFloat:
3871 case Primitive::kPrimDouble: {
3872 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3873 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3874 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3875 if (type == Primitive::kPrimFloat)
3876 __ MulS(dst, lhs, rhs);
3877 else
3878 __ MulD(dst, lhs, rhs);
3879 break;
3880 }
3881 default:
3882 LOG(FATAL) << "Unexpected mul type " << type;
3883 }
3884}
3885
3886void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3887 LocationSummary* locations =
3888 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3889 switch (neg->GetResultType()) {
3890 case Primitive::kPrimInt:
3891 case Primitive::kPrimLong:
3892 locations->SetInAt(0, Location::RequiresRegister());
3893 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3894 break;
3895
3896 case Primitive::kPrimFloat:
3897 case Primitive::kPrimDouble:
3898 locations->SetInAt(0, Location::RequiresFpuRegister());
3899 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3900 break;
3901
3902 default:
3903 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3904 }
3905}
3906
3907void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3908 Primitive::Type type = instruction->GetType();
3909 LocationSummary* locations = instruction->GetLocations();
3910
3911 switch (type) {
3912 case Primitive::kPrimInt:
3913 case Primitive::kPrimLong: {
3914 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3915 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3916 if (type == Primitive::kPrimInt)
3917 __ Subu(dst, ZERO, src);
3918 else
3919 __ Dsubu(dst, ZERO, src);
3920 break;
3921 }
3922 case Primitive::kPrimFloat:
3923 case Primitive::kPrimDouble: {
3924 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3925 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3926 if (type == Primitive::kPrimFloat)
3927 __ NegS(dst, src);
3928 else
3929 __ NegD(dst, src);
3930 break;
3931 }
3932 default:
3933 LOG(FATAL) << "Unexpected neg type " << type;
3934 }
3935}
3936
3937void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3938 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003939 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003940 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003941 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003942 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3943 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003944}
3945
3946void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003947 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
3948 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003949}
3950
3951void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3952 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003954 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003955 if (instruction->IsStringAlloc()) {
3956 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3957 } else {
3958 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003959 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003960 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3961}
3962
3963void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003964 if (instruction->IsStringAlloc()) {
3965 // String is allocated through StringFactory. Call NewEmptyString entry point.
3966 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003967 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003968 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003969 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3970 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3971 __ Jalr(T9);
3972 __ Nop();
3973 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3974 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003975 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003976 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003977 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003978}
3979
3980void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3982 locations->SetInAt(0, Location::RequiresRegister());
3983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3984}
3985
3986void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3987 Primitive::Type type = instruction->GetType();
3988 LocationSummary* locations = instruction->GetLocations();
3989
3990 switch (type) {
3991 case Primitive::kPrimInt:
3992 case Primitive::kPrimLong: {
3993 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3994 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3995 __ Nor(dst, src, ZERO);
3996 break;
3997 }
3998
3999 default:
4000 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4001 }
4002}
4003
4004void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4006 locations->SetInAt(0, Location::RequiresRegister());
4007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4008}
4009
4010void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4011 LocationSummary* locations = instruction->GetLocations();
4012 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4013 locations->InAt(0).AsRegister<GpuRegister>(),
4014 1);
4015}
4016
4017void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004018 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4019 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004020}
4021
Calin Juravle2ae48182016-03-16 14:05:09 +00004022void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4023 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004024 return;
4025 }
4026 Location obj = instruction->GetLocations()->InAt(0);
4027
4028 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004029 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004030}
4031
Calin Juravle2ae48182016-03-16 14:05:09 +00004032void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004033 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004034 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004035
4036 Location obj = instruction->GetLocations()->InAt(0);
4037
4038 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4039}
4040
4041void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004042 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004043}
4044
4045void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4046 HandleBinaryOp(instruction);
4047}
4048
4049void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4050 HandleBinaryOp(instruction);
4051}
4052
4053void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4054 LOG(FATAL) << "Unreachable";
4055}
4056
4057void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4058 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4059}
4060
4061void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4063 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4064 if (location.IsStackSlot()) {
4065 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4066 } else if (location.IsDoubleStackSlot()) {
4067 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4068 }
4069 locations->SetOut(location);
4070}
4071
4072void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4073 ATTRIBUTE_UNUSED) {
4074 // Nothing to do, the parameter is already at its location.
4075}
4076
4077void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4080 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4081}
4082
4083void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4084 ATTRIBUTE_UNUSED) {
4085 // Nothing to do, the method is already at its location.
4086}
4087
4088void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004090 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091 locations->SetInAt(i, Location::Any());
4092 }
4093 locations->SetOut(Location::Any());
4094}
4095
4096void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4097 LOG(FATAL) << "Unreachable";
4098}
4099
4100void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4101 Primitive::Type type = rem->GetResultType();
4102 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004103 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4104 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4106
4107 switch (type) {
4108 case Primitive::kPrimInt:
4109 case Primitive::kPrimLong:
4110 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004111 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4113 break;
4114
4115 case Primitive::kPrimFloat:
4116 case Primitive::kPrimDouble: {
4117 InvokeRuntimeCallingConvention calling_convention;
4118 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4119 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4120 locations->SetOut(calling_convention.GetReturnLocation(type));
4121 break;
4122 }
4123
4124 default:
4125 LOG(FATAL) << "Unexpected rem type " << type;
4126 }
4127}
4128
4129void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4130 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131
4132 switch (type) {
4133 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004134 case Primitive::kPrimLong:
4135 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004136 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004137
4138 case Primitive::kPrimFloat:
4139 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004140 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4141 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004142 if (type == Primitive::kPrimFloat) {
4143 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4144 } else {
4145 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4146 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004147 break;
4148 }
4149 default:
4150 LOG(FATAL) << "Unexpected rem type " << type;
4151 }
4152}
4153
4154void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4155 memory_barrier->SetLocations(nullptr);
4156}
4157
4158void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4159 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4160}
4161
4162void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4164 Primitive::Type return_type = ret->InputAt(0)->GetType();
4165 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4166}
4167
4168void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4169 codegen_->GenerateFrameExit();
4170}
4171
4172void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4173 ret->SetLocations(nullptr);
4174}
4175
4176void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4177 codegen_->GenerateFrameExit();
4178}
4179
Alexey Frunze92d90602015-12-18 18:16:36 -08004180void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4181 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004182}
4183
Alexey Frunze92d90602015-12-18 18:16:36 -08004184void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4185 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004186}
4187
Alexey Frunze4dda3372015-06-01 18:31:49 -07004188void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4189 HandleShift(shl);
4190}
4191
4192void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4193 HandleShift(shl);
4194}
4195
4196void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4197 HandleShift(shr);
4198}
4199
4200void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4201 HandleShift(shr);
4202}
4203
Alexey Frunze4dda3372015-06-01 18:31:49 -07004204void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4205 HandleBinaryOp(instruction);
4206}
4207
4208void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4209 HandleBinaryOp(instruction);
4210}
4211
4212void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4213 HandleFieldGet(instruction, instruction->GetFieldInfo());
4214}
4215
4216void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4217 HandleFieldGet(instruction, instruction->GetFieldInfo());
4218}
4219
4220void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4221 HandleFieldSet(instruction, instruction->GetFieldInfo());
4222}
4223
4224void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004225 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004226}
4227
Calin Juravlee460d1d2015-09-29 04:52:17 +01004228void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4229 HUnresolvedInstanceFieldGet* instruction) {
4230 FieldAccessCallingConventionMIPS64 calling_convention;
4231 codegen_->CreateUnresolvedFieldLocationSummary(
4232 instruction, instruction->GetFieldType(), calling_convention);
4233}
4234
4235void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4236 HUnresolvedInstanceFieldGet* instruction) {
4237 FieldAccessCallingConventionMIPS64 calling_convention;
4238 codegen_->GenerateUnresolvedFieldAccess(instruction,
4239 instruction->GetFieldType(),
4240 instruction->GetFieldIndex(),
4241 instruction->GetDexPc(),
4242 calling_convention);
4243}
4244
4245void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4246 HUnresolvedInstanceFieldSet* instruction) {
4247 FieldAccessCallingConventionMIPS64 calling_convention;
4248 codegen_->CreateUnresolvedFieldLocationSummary(
4249 instruction, instruction->GetFieldType(), calling_convention);
4250}
4251
4252void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4253 HUnresolvedInstanceFieldSet* instruction) {
4254 FieldAccessCallingConventionMIPS64 calling_convention;
4255 codegen_->GenerateUnresolvedFieldAccess(instruction,
4256 instruction->GetFieldType(),
4257 instruction->GetFieldIndex(),
4258 instruction->GetDexPc(),
4259 calling_convention);
4260}
4261
4262void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4263 HUnresolvedStaticFieldGet* instruction) {
4264 FieldAccessCallingConventionMIPS64 calling_convention;
4265 codegen_->CreateUnresolvedFieldLocationSummary(
4266 instruction, instruction->GetFieldType(), calling_convention);
4267}
4268
4269void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4270 HUnresolvedStaticFieldGet* instruction) {
4271 FieldAccessCallingConventionMIPS64 calling_convention;
4272 codegen_->GenerateUnresolvedFieldAccess(instruction,
4273 instruction->GetFieldType(),
4274 instruction->GetFieldIndex(),
4275 instruction->GetDexPc(),
4276 calling_convention);
4277}
4278
4279void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4280 HUnresolvedStaticFieldSet* instruction) {
4281 FieldAccessCallingConventionMIPS64 calling_convention;
4282 codegen_->CreateUnresolvedFieldLocationSummary(
4283 instruction, instruction->GetFieldType(), calling_convention);
4284}
4285
4286void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4287 HUnresolvedStaticFieldSet* instruction) {
4288 FieldAccessCallingConventionMIPS64 calling_convention;
4289 codegen_->GenerateUnresolvedFieldAccess(instruction,
4290 instruction->GetFieldType(),
4291 instruction->GetFieldIndex(),
4292 instruction->GetDexPc(),
4293 calling_convention);
4294}
4295
Alexey Frunze4dda3372015-06-01 18:31:49 -07004296void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004297 LocationSummary* locations =
4298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004299 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004300}
4301
4302void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4303 HBasicBlock* block = instruction->GetBlock();
4304 if (block->GetLoopInformation() != nullptr) {
4305 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4306 // The back edge will generate the suspend check.
4307 return;
4308 }
4309 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4310 // The goto will generate the suspend check.
4311 return;
4312 }
4313 GenerateSuspendCheck(instruction, nullptr);
4314}
4315
Alexey Frunze4dda3372015-06-01 18:31:49 -07004316void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4317 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004319 InvokeRuntimeCallingConvention calling_convention;
4320 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4321}
4322
4323void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004324 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004325 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4326}
4327
4328void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4329 Primitive::Type input_type = conversion->GetInputType();
4330 Primitive::Type result_type = conversion->GetResultType();
4331 DCHECK_NE(input_type, result_type);
4332
4333 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4334 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4335 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4336 }
4337
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4339
4340 if (Primitive::IsFloatingPointType(input_type)) {
4341 locations->SetInAt(0, Location::RequiresFpuRegister());
4342 } else {
4343 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004344 }
4345
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004346 if (Primitive::IsFloatingPointType(result_type)) {
4347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004348 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004350 }
4351}
4352
4353void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4354 LocationSummary* locations = conversion->GetLocations();
4355 Primitive::Type result_type = conversion->GetResultType();
4356 Primitive::Type input_type = conversion->GetInputType();
4357
4358 DCHECK_NE(input_type, result_type);
4359
4360 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4361 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4362 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4363
4364 switch (result_type) {
4365 case Primitive::kPrimChar:
4366 __ Andi(dst, src, 0xFFFF);
4367 break;
4368 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004369 if (input_type == Primitive::kPrimLong) {
4370 // Type conversion from long to types narrower than int is a result of code
4371 // transformations. To avoid unpredictable results for SEB and SEH, we first
4372 // need to sign-extend the low 32-bit value into bits 32 through 63.
4373 __ Sll(dst, src, 0);
4374 __ Seb(dst, dst);
4375 } else {
4376 __ Seb(dst, src);
4377 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004378 break;
4379 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004380 if (input_type == Primitive::kPrimLong) {
4381 // Type conversion from long to types narrower than int is a result of code
4382 // transformations. To avoid unpredictable results for SEB and SEH, we first
4383 // need to sign-extend the low 32-bit value into bits 32 through 63.
4384 __ Sll(dst, src, 0);
4385 __ Seh(dst, dst);
4386 } else {
4387 __ Seh(dst, src);
4388 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004389 break;
4390 case Primitive::kPrimInt:
4391 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004392 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4393 // conversions, except when the input and output registers are the same and we are not
4394 // converting longs to shorter types. In these cases, do nothing.
4395 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4396 __ Sll(dst, src, 0);
4397 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004398 break;
4399
4400 default:
4401 LOG(FATAL) << "Unexpected type conversion from " << input_type
4402 << " to " << result_type;
4403 }
4404 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004405 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4406 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4407 if (input_type == Primitive::kPrimLong) {
4408 __ Dmtc1(src, FTMP);
4409 if (result_type == Primitive::kPrimFloat) {
4410 __ Cvtsl(dst, FTMP);
4411 } else {
4412 __ Cvtdl(dst, FTMP);
4413 }
4414 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004415 __ Mtc1(src, FTMP);
4416 if (result_type == Primitive::kPrimFloat) {
4417 __ Cvtsw(dst, FTMP);
4418 } else {
4419 __ Cvtdw(dst, FTMP);
4420 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004421 }
4422 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4423 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004424 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4425 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4426 Mips64Label truncate;
4427 Mips64Label done;
4428
4429 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4430 // value when the input is either a NaN or is outside of the range of the output type
4431 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4432 // the same result.
4433 //
4434 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4435 // value of the output type if the input is outside of the range after the truncation or
4436 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4437 // results. This matches the desired float/double-to-int/long conversion exactly.
4438 //
4439 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4440 //
4441 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4442 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4443 // even though it must be NAN2008=1 on R6.
4444 //
4445 // The code takes care of the different behaviors by first comparing the input to the
4446 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4447 // If the input is greater than or equal to the minimum, it procedes to the truncate
4448 // instruction, which will handle such an input the same way irrespective of NAN2008.
4449 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4450 // in order to return either zero or the minimum value.
4451 //
4452 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4453 // truncate instruction for MIPS64R6.
4454 if (input_type == Primitive::kPrimFloat) {
4455 uint32_t min_val = (result_type == Primitive::kPrimLong)
4456 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4457 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4458 __ LoadConst32(TMP, min_val);
4459 __ Mtc1(TMP, FTMP);
4460 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004461 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004462 uint64_t min_val = (result_type == Primitive::kPrimLong)
4463 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4464 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4465 __ LoadConst64(TMP, min_val);
4466 __ Dmtc1(TMP, FTMP);
4467 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004468 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004469
4470 __ Bc1nez(FTMP, &truncate);
4471
4472 if (input_type == Primitive::kPrimFloat) {
4473 __ CmpEqS(FTMP, src, src);
4474 } else {
4475 __ CmpEqD(FTMP, src, src);
4476 }
4477 if (result_type == Primitive::kPrimLong) {
4478 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4479 } else {
4480 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4481 }
4482 __ Mfc1(TMP, FTMP);
4483 __ And(dst, dst, TMP);
4484
4485 __ Bc(&done);
4486
4487 __ Bind(&truncate);
4488
4489 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004490 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004491 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004492 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004493 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004494 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004495 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004496 } else {
4497 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004498 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004499 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004500 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004501 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004502 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004503 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004504
4505 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004506 } else if (Primitive::IsFloatingPointType(result_type) &&
4507 Primitive::IsFloatingPointType(input_type)) {
4508 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4509 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4510 if (result_type == Primitive::kPrimFloat) {
4511 __ Cvtsd(dst, src);
4512 } else {
4513 __ Cvtds(dst, src);
4514 }
4515 } else {
4516 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4517 << " to " << result_type;
4518 }
4519}
4520
4521void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4522 HandleShift(ushr);
4523}
4524
4525void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4526 HandleShift(ushr);
4527}
4528
4529void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4530 HandleBinaryOp(instruction);
4531}
4532
4533void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4534 HandleBinaryOp(instruction);
4535}
4536
4537void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4538 // Nothing to do, this should be removed during prepare for register allocator.
4539 LOG(FATAL) << "Unreachable";
4540}
4541
4542void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4543 // Nothing to do, this should be removed during prepare for register allocator.
4544 LOG(FATAL) << "Unreachable";
4545}
4546
4547void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004548 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004549}
4550
4551void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004552 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004553}
4554
4555void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004556 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004557}
4558
4559void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004560 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004561}
4562
4563void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004564 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004565}
4566
4567void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004568 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004569}
4570
4571void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004572 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004573}
4574
4575void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004576 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004577}
4578
4579void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004580 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004581}
4582
4583void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004584 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004585}
4586
4587void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004588 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004589}
4590
4591void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004592 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004593}
4594
Aart Bike9f37602015-10-09 11:15:55 -07004595void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004596 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004597}
4598
4599void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004600 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004601}
4602
4603void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004604 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004605}
4606
4607void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004608 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004609}
4610
4611void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004612 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004613}
4614
4615void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004616 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004617}
4618
4619void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004620 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004621}
4622
4623void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004624 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004625}
4626
Mark Mendellfe57faa2015-09-18 09:26:15 -04004627// Simple implementation of packed switch - generate cascaded compare/jumps.
4628void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4629 LocationSummary* locations =
4630 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4631 locations->SetInAt(0, Location::RequiresRegister());
4632}
4633
Alexey Frunze0960ac52016-12-20 17:24:59 -08004634void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4635 int32_t lower_bound,
4636 uint32_t num_entries,
4637 HBasicBlock* switch_block,
4638 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004639 // Create a set of compare/jumps.
4640 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004641 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004642 // Jump to default if index is negative
4643 // Note: We don't check the case that index is positive while value < lower_bound, because in
4644 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4645 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4646
Alexey Frunze0960ac52016-12-20 17:24:59 -08004647 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004648 // Jump to successors[0] if value == lower_bound.
4649 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4650 int32_t last_index = 0;
4651 for (; num_entries - last_index > 2; last_index += 2) {
4652 __ Addiu(temp_reg, temp_reg, -2);
4653 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4654 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4655 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4656 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4657 }
4658 if (num_entries - last_index == 2) {
4659 // The last missing case_value.
4660 __ Addiu(temp_reg, temp_reg, -1);
4661 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004662 }
4663
4664 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08004665 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004666 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004667 }
4668}
4669
Alexey Frunze0960ac52016-12-20 17:24:59 -08004670void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
4671 int32_t lower_bound,
4672 uint32_t num_entries,
4673 HBasicBlock* switch_block,
4674 HBasicBlock* default_block) {
4675 // Create a jump table.
4676 std::vector<Mips64Label*> labels(num_entries);
4677 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
4678 for (uint32_t i = 0; i < num_entries; i++) {
4679 labels[i] = codegen_->GetLabelOf(successors[i]);
4680 }
4681 JumpTable* table = __ CreateJumpTable(std::move(labels));
4682
4683 // Is the value in range?
4684 __ Addiu32(TMP, value_reg, -lower_bound);
4685 __ LoadConst32(AT, num_entries);
4686 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
4687
4688 // We are in the range of the table.
4689 // Load the target address from the jump table, indexing by the value.
4690 __ LoadLabelAddress(AT, table->GetLabel());
4691 __ Sll(TMP, TMP, 2);
4692 __ Daddu(TMP, TMP, AT);
4693 __ Lw(TMP, TMP, 0);
4694 // Compute the absolute target address by adding the table start address
4695 // (the table contains offsets to targets relative to its start).
4696 __ Daddu(TMP, TMP, AT);
4697 // And jump.
4698 __ Jr(TMP);
4699 __ Nop();
4700}
4701
4702void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4703 int32_t lower_bound = switch_instr->GetStartValue();
4704 uint32_t num_entries = switch_instr->GetNumEntries();
4705 LocationSummary* locations = switch_instr->GetLocations();
4706 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4707 HBasicBlock* switch_block = switch_instr->GetBlock();
4708 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4709
4710 if (num_entries > kPackedSwitchJumpTableThreshold) {
4711 GenTableBasedPackedSwitch(value_reg,
4712 lower_bound,
4713 num_entries,
4714 switch_block,
4715 default_block);
4716 } else {
4717 GenPackedSwitchWithCompares(value_reg,
4718 lower_bound,
4719 num_entries,
4720 switch_block,
4721 default_block);
4722 }
4723}
4724
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004725void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4726 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4727}
4728
4729void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4730 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4731}
4732
Alexey Frunze4dda3372015-06-01 18:31:49 -07004733} // namespace mips64
4734} // namespace art