blob: 817854b50761d75fc89f6001ffe46507eaa3fea8 [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 Frunzec061de12017-02-14 13:27:23 -08001656
1657 if (type == Primitive::kPrimNot) {
1658 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1659 __ MaybeUnpoisonHeapReference(out);
1660 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001661}
1662
1663void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1665 locations->SetInAt(0, Location::RequiresRegister());
1666 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1667}
1668
1669void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1670 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001671 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001672 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1673 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1674 __ LoadFromOffset(kLoadWord, out, obj, offset);
1675 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001676 // Mask out compression flag from String's array length.
1677 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
1678 __ Srl(out, out, 1u);
1679 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001680}
1681
1682void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001683 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1685 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001686 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001687 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001688 InvokeRuntimeCallingConvention calling_convention;
1689 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1690 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1691 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1692 } else {
1693 locations->SetInAt(0, Location::RequiresRegister());
1694 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1695 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1696 locations->SetInAt(2, Location::RequiresFpuRegister());
1697 } else {
1698 locations->SetInAt(2, Location::RequiresRegister());
1699 }
1700 }
1701}
1702
1703void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1704 LocationSummary* locations = instruction->GetLocations();
1705 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1706 Location index = locations->InAt(1);
1707 Primitive::Type value_type = instruction->GetComponentType();
1708 bool needs_runtime_call = locations->WillCall();
1709 bool needs_write_barrier =
1710 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1711
1712 switch (value_type) {
1713 case Primitive::kPrimBoolean:
1714 case Primitive::kPrimByte: {
1715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1716 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1717 if (index.IsConstant()) {
1718 size_t offset =
1719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1720 __ StoreToOffset(kStoreByte, value, obj, offset);
1721 } else {
1722 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1723 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1724 }
1725 break;
1726 }
1727
1728 case Primitive::kPrimShort:
1729 case Primitive::kPrimChar: {
1730 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1731 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1732 if (index.IsConstant()) {
1733 size_t offset =
1734 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1735 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1736 } else {
1737 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1738 __ Daddu(TMP, obj, TMP);
1739 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1740 }
1741 break;
1742 }
1743
1744 case Primitive::kPrimInt:
1745 case Primitive::kPrimNot: {
1746 if (!needs_runtime_call) {
1747 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08001748 GpuRegister base_reg;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001749 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1750 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001751 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
1752 base_reg = obj;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001753 } else {
1754 DCHECK(index.IsRegister()) << index;
1755 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1756 __ Daddu(TMP, obj, TMP);
Alexey Frunzec061de12017-02-14 13:27:23 -08001757 base_reg = TMP;
1758 }
1759 if (kPoisonHeapReferences && needs_write_barrier) {
1760 // Note that in the case where `value` is a null reference,
1761 // we do not enter this block, as a null reference does not
1762 // need poisoning.
1763 DCHECK_EQ(value_type, Primitive::kPrimNot);
1764 // Use Sw() instead of StoreToOffset() in order to be able to
1765 // hold the poisoned reference in AT and thus avoid allocating
1766 // yet another temporary register.
1767 if (index.IsConstant()) {
1768 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
1769 int16_t low16 = Low16Bits(data_offset);
1770 // For consistency with StoreToOffset() and such treat data_offset as int32_t.
1771 uint64_t high48 = static_cast<uint64_t>(static_cast<int32_t>(data_offset)) - low16;
1772 int16_t upper16 = High16Bits(high48);
1773 // Allow the full [-2GB,+2GB) range in case `low16` is negative and needs a
1774 // compensatory 64KB added, which may push `high48` above 2GB and require
1775 // the dahi instruction.
1776 int16_t higher16 = High32Bits(high48) + ((upper16 < 0) ? 1 : 0);
1777 __ Daui(TMP, obj, upper16);
1778 if (higher16 != 0) {
1779 __ Dahi(TMP, higher16);
1780 }
1781 base_reg = TMP;
1782 data_offset = low16;
1783 }
1784 } else {
1785 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
1786 }
1787 __ PoisonHeapReference(AT, value);
1788 __ Sw(AT, base_reg, data_offset);
1789 } else {
1790 __ StoreToOffset(kStoreWord, value, base_reg, data_offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791 }
1792 codegen_->MaybeRecordImplicitNullCheck(instruction);
1793 if (needs_write_barrier) {
1794 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001795 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 }
1797 } else {
1798 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08001799 // Note: if heap poisoning is enabled, pAputObject takes care
1800 // of poisoning the reference.
Serban Constantinescufc734082016-07-19 17:18:07 +01001801 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001802 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001803 }
1804 break;
1805 }
1806
1807 case Primitive::kPrimLong: {
1808 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1809 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1810 if (index.IsConstant()) {
1811 size_t offset =
1812 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1813 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1814 } else {
1815 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1816 __ Daddu(TMP, obj, TMP);
1817 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1818 }
1819 break;
1820 }
1821
1822 case Primitive::kPrimFloat: {
1823 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1824 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1825 DCHECK(locations->InAt(2).IsFpuRegister());
1826 if (index.IsConstant()) {
1827 size_t offset =
1828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1829 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1830 } else {
1831 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1832 __ Daddu(TMP, obj, TMP);
1833 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimDouble: {
1839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1840 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1841 DCHECK(locations->InAt(2).IsFpuRegister());
1842 if (index.IsConstant()) {
1843 size_t offset =
1844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1845 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1846 } else {
1847 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1848 __ Daddu(TMP, obj, TMP);
1849 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1850 }
1851 break;
1852 }
1853
1854 case Primitive::kPrimVoid:
1855 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1856 UNREACHABLE();
1857 }
1858
1859 // Ints and objects are handled in the switch.
1860 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1861 codegen_->MaybeRecordImplicitNullCheck(instruction);
1862 }
1863}
1864
1865void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001866 RegisterSet caller_saves = RegisterSet::Empty();
1867 InvokeRuntimeCallingConvention calling_convention;
1868 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1869 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1870 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001871 locations->SetInAt(0, Location::RequiresRegister());
1872 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001873}
1874
1875void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1876 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001877 BoundsCheckSlowPathMIPS64* slow_path =
1878 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001879 codegen_->AddSlowPath(slow_path);
1880
1881 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1882 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1883
1884 // length is limited by the maximum positive signed 32-bit integer.
1885 // Unsigned comparison of length and index checks for index < 0
1886 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001887 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001888}
1889
1890void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1891 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1892 instruction,
1893 LocationSummary::kCallOnSlowPath);
1894 locations->SetInAt(0, Location::RequiresRegister());
1895 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001896 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001897 locations->AddTemp(Location::RequiresRegister());
1898}
1899
1900void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1901 LocationSummary* locations = instruction->GetLocations();
1902 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1903 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1904 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1905
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001906 SlowPathCodeMIPS64* slow_path =
1907 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001908 codegen_->AddSlowPath(slow_path);
1909
1910 // TODO: avoid this check if we know obj is not null.
1911 __ Beqzc(obj, slow_path->GetExitLabel());
1912 // Compare the class of `obj` with `cls`.
1913 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08001914 __ MaybeUnpoisonHeapReference(obj_cls);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001915 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1916 __ Bind(slow_path->GetExitLabel());
1917}
1918
1919void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1922 locations->SetInAt(0, Location::RequiresRegister());
1923 if (check->HasUses()) {
1924 locations->SetOut(Location::SameAsFirstInput());
1925 }
1926}
1927
1928void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1929 // We assume the class is not null.
1930 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1931 check->GetLoadClass(),
1932 check,
1933 check->GetDexPc(),
1934 true);
1935 codegen_->AddSlowPath(slow_path);
1936 GenerateClassInitializationCheck(slow_path,
1937 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1938}
1939
1940void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1941 Primitive::Type in_type = compare->InputAt(0)->GetType();
1942
Alexey Frunze299a9392015-12-08 16:08:02 -08001943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001944
1945 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001946 case Primitive::kPrimBoolean:
1947 case Primitive::kPrimByte:
1948 case Primitive::kPrimShort:
1949 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001950 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001951 case Primitive::kPrimLong:
1952 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001953 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1955 break;
1956
1957 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001958 case Primitive::kPrimDouble:
1959 locations->SetInAt(0, Location::RequiresFpuRegister());
1960 locations->SetInAt(1, Location::RequiresFpuRegister());
1961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001962 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001963
1964 default:
1965 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1966 }
1967}
1968
1969void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1970 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001971 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001972 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1973
1974 // 0 if: left == right
1975 // 1 if: left > right
1976 // -1 if: left < right
1977 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001978 case Primitive::kPrimBoolean:
1979 case Primitive::kPrimByte:
1980 case Primitive::kPrimShort:
1981 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001982 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001983 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001984 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001985 Location rhs_location = locations->InAt(1);
1986 bool use_imm = rhs_location.IsConstant();
1987 GpuRegister rhs = ZERO;
1988 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001989 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001990 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1991 if (value != 0) {
1992 rhs = AT;
1993 __ LoadConst64(rhs, value);
1994 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001995 } else {
1996 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1997 if (value != 0) {
1998 rhs = AT;
1999 __ LoadConst32(rhs, value);
2000 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002001 }
2002 } else {
2003 rhs = rhs_location.AsRegister<GpuRegister>();
2004 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002005 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002006 __ Slt(res, rhs, lhs);
2007 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002008 break;
2009 }
2010
Alexey Frunze299a9392015-12-08 16:08:02 -08002011 case Primitive::kPrimFloat: {
2012 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2013 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2014 Mips64Label done;
2015 __ CmpEqS(FTMP, lhs, rhs);
2016 __ LoadConst32(res, 0);
2017 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002018 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002019 __ CmpLtS(FTMP, lhs, rhs);
2020 __ LoadConst32(res, -1);
2021 __ Bc1nez(FTMP, &done);
2022 __ LoadConst32(res, 1);
2023 } else {
2024 __ CmpLtS(FTMP, rhs, lhs);
2025 __ LoadConst32(res, 1);
2026 __ Bc1nez(FTMP, &done);
2027 __ LoadConst32(res, -1);
2028 }
2029 __ Bind(&done);
2030 break;
2031 }
2032
Alexey Frunze4dda3372015-06-01 18:31:49 -07002033 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002034 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2035 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2036 Mips64Label done;
2037 __ CmpEqD(FTMP, lhs, rhs);
2038 __ LoadConst32(res, 0);
2039 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002040 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002041 __ CmpLtD(FTMP, lhs, rhs);
2042 __ LoadConst32(res, -1);
2043 __ Bc1nez(FTMP, &done);
2044 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002045 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002046 __ CmpLtD(FTMP, rhs, lhs);
2047 __ LoadConst32(res, 1);
2048 __ Bc1nez(FTMP, &done);
2049 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002050 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002051 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002052 break;
2053 }
2054
2055 default:
2056 LOG(FATAL) << "Unimplemented compare type " << in_type;
2057 }
2058}
2059
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002060void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002062 switch (instruction->InputAt(0)->GetType()) {
2063 default:
2064 case Primitive::kPrimLong:
2065 locations->SetInAt(0, Location::RequiresRegister());
2066 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2067 break;
2068
2069 case Primitive::kPrimFloat:
2070 case Primitive::kPrimDouble:
2071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetInAt(1, Location::RequiresFpuRegister());
2073 break;
2074 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002075 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002076 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2077 }
2078}
2079
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002081 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002082 return;
2083 }
2084
Alexey Frunze299a9392015-12-08 16:08:02 -08002085 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002086 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002087 switch (type) {
2088 default:
2089 // Integer case.
2090 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2091 return;
2092 case Primitive::kPrimLong:
2093 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2094 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002095 case Primitive::kPrimFloat:
2096 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002097 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2098 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 }
2100}
2101
Alexey Frunzec857c742015-09-23 15:12:39 -07002102void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2103 DCHECK(instruction->IsDiv() || instruction->IsRem());
2104 Primitive::Type type = instruction->GetResultType();
2105
2106 LocationSummary* locations = instruction->GetLocations();
2107 Location second = locations->InAt(1);
2108 DCHECK(second.IsConstant());
2109
2110 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2111 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2112 int64_t imm = Int64FromConstant(second.GetConstant());
2113 DCHECK(imm == 1 || imm == -1);
2114
2115 if (instruction->IsRem()) {
2116 __ Move(out, ZERO);
2117 } else {
2118 if (imm == -1) {
2119 if (type == Primitive::kPrimInt) {
2120 __ Subu(out, ZERO, dividend);
2121 } else {
2122 DCHECK_EQ(type, Primitive::kPrimLong);
2123 __ Dsubu(out, ZERO, dividend);
2124 }
2125 } else if (out != dividend) {
2126 __ Move(out, dividend);
2127 }
2128 }
2129}
2130
2131void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2132 DCHECK(instruction->IsDiv() || instruction->IsRem());
2133 Primitive::Type type = instruction->GetResultType();
2134
2135 LocationSummary* locations = instruction->GetLocations();
2136 Location second = locations->InAt(1);
2137 DCHECK(second.IsConstant());
2138
2139 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2140 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2141 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002142 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002143 int ctz_imm = CTZ(abs_imm);
2144
2145 if (instruction->IsDiv()) {
2146 if (type == Primitive::kPrimInt) {
2147 if (ctz_imm == 1) {
2148 // Fast path for division by +/-2, which is very common.
2149 __ Srl(TMP, dividend, 31);
2150 } else {
2151 __ Sra(TMP, dividend, 31);
2152 __ Srl(TMP, TMP, 32 - ctz_imm);
2153 }
2154 __ Addu(out, dividend, TMP);
2155 __ Sra(out, out, ctz_imm);
2156 if (imm < 0) {
2157 __ Subu(out, ZERO, out);
2158 }
2159 } else {
2160 DCHECK_EQ(type, Primitive::kPrimLong);
2161 if (ctz_imm == 1) {
2162 // Fast path for division by +/-2, which is very common.
2163 __ Dsrl32(TMP, dividend, 31);
2164 } else {
2165 __ Dsra32(TMP, dividend, 31);
2166 if (ctz_imm > 32) {
2167 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2168 } else {
2169 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2170 }
2171 }
2172 __ Daddu(out, dividend, TMP);
2173 if (ctz_imm < 32) {
2174 __ Dsra(out, out, ctz_imm);
2175 } else {
2176 __ Dsra32(out, out, ctz_imm - 32);
2177 }
2178 if (imm < 0) {
2179 __ Dsubu(out, ZERO, out);
2180 }
2181 }
2182 } else {
2183 if (type == Primitive::kPrimInt) {
2184 if (ctz_imm == 1) {
2185 // Fast path for modulo +/-2, which is very common.
2186 __ Sra(TMP, dividend, 31);
2187 __ Subu(out, dividend, TMP);
2188 __ Andi(out, out, 1);
2189 __ Addu(out, out, TMP);
2190 } else {
2191 __ Sra(TMP, dividend, 31);
2192 __ Srl(TMP, TMP, 32 - ctz_imm);
2193 __ Addu(out, dividend, TMP);
2194 if (IsUint<16>(abs_imm - 1)) {
2195 __ Andi(out, out, abs_imm - 1);
2196 } else {
2197 __ Sll(out, out, 32 - ctz_imm);
2198 __ Srl(out, out, 32 - ctz_imm);
2199 }
2200 __ Subu(out, out, TMP);
2201 }
2202 } else {
2203 DCHECK_EQ(type, Primitive::kPrimLong);
2204 if (ctz_imm == 1) {
2205 // Fast path for modulo +/-2, which is very common.
2206 __ Dsra32(TMP, dividend, 31);
2207 __ Dsubu(out, dividend, TMP);
2208 __ Andi(out, out, 1);
2209 __ Daddu(out, out, TMP);
2210 } else {
2211 __ Dsra32(TMP, dividend, 31);
2212 if (ctz_imm > 32) {
2213 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2214 } else {
2215 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2216 }
2217 __ Daddu(out, dividend, TMP);
2218 if (IsUint<16>(abs_imm - 1)) {
2219 __ Andi(out, out, abs_imm - 1);
2220 } else {
2221 if (ctz_imm > 32) {
2222 __ Dsll(out, out, 64 - ctz_imm);
2223 __ Dsrl(out, out, 64 - ctz_imm);
2224 } else {
2225 __ Dsll32(out, out, 32 - ctz_imm);
2226 __ Dsrl32(out, out, 32 - ctz_imm);
2227 }
2228 }
2229 __ Dsubu(out, out, TMP);
2230 }
2231 }
2232 }
2233}
2234
2235void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2236 DCHECK(instruction->IsDiv() || instruction->IsRem());
2237
2238 LocationSummary* locations = instruction->GetLocations();
2239 Location second = locations->InAt(1);
2240 DCHECK(second.IsConstant());
2241
2242 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2243 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2244 int64_t imm = Int64FromConstant(second.GetConstant());
2245
2246 Primitive::Type type = instruction->GetResultType();
2247 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2248
2249 int64_t magic;
2250 int shift;
2251 CalculateMagicAndShiftForDivRem(imm,
2252 (type == Primitive::kPrimLong),
2253 &magic,
2254 &shift);
2255
2256 if (type == Primitive::kPrimInt) {
2257 __ LoadConst32(TMP, magic);
2258 __ MuhR6(TMP, dividend, TMP);
2259
2260 if (imm > 0 && magic < 0) {
2261 __ Addu(TMP, TMP, dividend);
2262 } else if (imm < 0 && magic > 0) {
2263 __ Subu(TMP, TMP, dividend);
2264 }
2265
2266 if (shift != 0) {
2267 __ Sra(TMP, TMP, shift);
2268 }
2269
2270 if (instruction->IsDiv()) {
2271 __ Sra(out, TMP, 31);
2272 __ Subu(out, TMP, out);
2273 } else {
2274 __ Sra(AT, TMP, 31);
2275 __ Subu(AT, TMP, AT);
2276 __ LoadConst32(TMP, imm);
2277 __ MulR6(TMP, AT, TMP);
2278 __ Subu(out, dividend, TMP);
2279 }
2280 } else {
2281 __ LoadConst64(TMP, magic);
2282 __ Dmuh(TMP, dividend, TMP);
2283
2284 if (imm > 0 && magic < 0) {
2285 __ Daddu(TMP, TMP, dividend);
2286 } else if (imm < 0 && magic > 0) {
2287 __ Dsubu(TMP, TMP, dividend);
2288 }
2289
2290 if (shift >= 32) {
2291 __ Dsra32(TMP, TMP, shift - 32);
2292 } else if (shift > 0) {
2293 __ Dsra(TMP, TMP, shift);
2294 }
2295
2296 if (instruction->IsDiv()) {
2297 __ Dsra32(out, TMP, 31);
2298 __ Dsubu(out, TMP, out);
2299 } else {
2300 __ Dsra32(AT, TMP, 31);
2301 __ Dsubu(AT, TMP, AT);
2302 __ LoadConst64(TMP, imm);
2303 __ Dmul(TMP, AT, TMP);
2304 __ Dsubu(out, dividend, TMP);
2305 }
2306 }
2307}
2308
2309void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2310 DCHECK(instruction->IsDiv() || instruction->IsRem());
2311 Primitive::Type type = instruction->GetResultType();
2312 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2313
2314 LocationSummary* locations = instruction->GetLocations();
2315 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2316 Location second = locations->InAt(1);
2317
2318 if (second.IsConstant()) {
2319 int64_t imm = Int64FromConstant(second.GetConstant());
2320 if (imm == 0) {
2321 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2322 } else if (imm == 1 || imm == -1) {
2323 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002324 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002325 DivRemByPowerOfTwo(instruction);
2326 } else {
2327 DCHECK(imm <= -2 || imm >= 2);
2328 GenerateDivRemWithAnyConstant(instruction);
2329 }
2330 } else {
2331 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2332 GpuRegister divisor = second.AsRegister<GpuRegister>();
2333 if (instruction->IsDiv()) {
2334 if (type == Primitive::kPrimInt)
2335 __ DivR6(out, dividend, divisor);
2336 else
2337 __ Ddiv(out, dividend, divisor);
2338 } else {
2339 if (type == Primitive::kPrimInt)
2340 __ ModR6(out, dividend, divisor);
2341 else
2342 __ Dmod(out, dividend, divisor);
2343 }
2344 }
2345}
2346
Alexey Frunze4dda3372015-06-01 18:31:49 -07002347void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2348 LocationSummary* locations =
2349 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2350 switch (div->GetResultType()) {
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimLong:
2353 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002354 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2356 break;
2357
2358 case Primitive::kPrimFloat:
2359 case Primitive::kPrimDouble:
2360 locations->SetInAt(0, Location::RequiresFpuRegister());
2361 locations->SetInAt(1, Location::RequiresFpuRegister());
2362 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2363 break;
2364
2365 default:
2366 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2367 }
2368}
2369
2370void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2371 Primitive::Type type = instruction->GetType();
2372 LocationSummary* locations = instruction->GetLocations();
2373
2374 switch (type) {
2375 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002376 case Primitive::kPrimLong:
2377 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002379 case Primitive::kPrimFloat:
2380 case Primitive::kPrimDouble: {
2381 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2382 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2383 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2384 if (type == Primitive::kPrimFloat)
2385 __ DivS(dst, lhs, rhs);
2386 else
2387 __ DivD(dst, lhs, rhs);
2388 break;
2389 }
2390 default:
2391 LOG(FATAL) << "Unexpected div type " << type;
2392 }
2393}
2394
2395void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002396 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002397 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002398}
2399
2400void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2401 SlowPathCodeMIPS64* slow_path =
2402 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2403 codegen_->AddSlowPath(slow_path);
2404 Location value = instruction->GetLocations()->InAt(0);
2405
2406 Primitive::Type type = instruction->GetType();
2407
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002408 if (!Primitive::IsIntegralType(type)) {
2409 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002410 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002411 }
2412
2413 if (value.IsConstant()) {
2414 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2415 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002416 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002417 } else {
2418 // A division by a non-null constant is valid. We don't need to perform
2419 // any check, so simply fall through.
2420 }
2421 } else {
2422 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2423 }
2424}
2425
2426void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2427 LocationSummary* locations =
2428 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2429 locations->SetOut(Location::ConstantLocation(constant));
2430}
2431
2432void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2433 // Will be generated at use site.
2434}
2435
2436void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2437 exit->SetLocations(nullptr);
2438}
2439
2440void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2441}
2442
2443void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2444 LocationSummary* locations =
2445 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2446 locations->SetOut(Location::ConstantLocation(constant));
2447}
2448
2449void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2450 // Will be generated at use site.
2451}
2452
David Brazdilfc6a86a2015-06-26 10:33:45 +00002453void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002454 DCHECK(!successor->IsExitBlock());
2455 HBasicBlock* block = got->GetBlock();
2456 HInstruction* previous = got->GetPrevious();
2457 HLoopInformation* info = block->GetLoopInformation();
2458
2459 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2460 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2461 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2462 return;
2463 }
2464 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2465 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2466 }
2467 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002468 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002469 }
2470}
2471
David Brazdilfc6a86a2015-06-26 10:33:45 +00002472void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2473 got->SetLocations(nullptr);
2474}
2475
2476void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2477 HandleGoto(got, got->GetSuccessor());
2478}
2479
2480void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2481 try_boundary->SetLocations(nullptr);
2482}
2483
2484void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2485 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2486 if (!successor->IsExitBlock()) {
2487 HandleGoto(try_boundary, successor);
2488 }
2489}
2490
Alexey Frunze299a9392015-12-08 16:08:02 -08002491void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2492 bool is64bit,
2493 LocationSummary* locations) {
2494 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2495 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2496 Location rhs_location = locations->InAt(1);
2497 GpuRegister rhs_reg = ZERO;
2498 int64_t rhs_imm = 0;
2499 bool use_imm = rhs_location.IsConstant();
2500 if (use_imm) {
2501 if (is64bit) {
2502 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2503 } else {
2504 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2505 }
2506 } else {
2507 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2508 }
2509 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2510
2511 switch (cond) {
2512 case kCondEQ:
2513 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002514 if (use_imm && IsInt<16>(-rhs_imm)) {
2515 if (rhs_imm == 0) {
2516 if (cond == kCondEQ) {
2517 __ Sltiu(dst, lhs, 1);
2518 } else {
2519 __ Sltu(dst, ZERO, lhs);
2520 }
2521 } else {
2522 if (is64bit) {
2523 __ Daddiu(dst, lhs, -rhs_imm);
2524 } else {
2525 __ Addiu(dst, lhs, -rhs_imm);
2526 }
2527 if (cond == kCondEQ) {
2528 __ Sltiu(dst, dst, 1);
2529 } else {
2530 __ Sltu(dst, ZERO, dst);
2531 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002532 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002533 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002534 if (use_imm && IsUint<16>(rhs_imm)) {
2535 __ Xori(dst, lhs, rhs_imm);
2536 } else {
2537 if (use_imm) {
2538 rhs_reg = TMP;
2539 __ LoadConst64(rhs_reg, rhs_imm);
2540 }
2541 __ Xor(dst, lhs, rhs_reg);
2542 }
2543 if (cond == kCondEQ) {
2544 __ Sltiu(dst, dst, 1);
2545 } else {
2546 __ Sltu(dst, ZERO, dst);
2547 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002548 }
2549 break;
2550
2551 case kCondLT:
2552 case kCondGE:
2553 if (use_imm && IsInt<16>(rhs_imm)) {
2554 __ Slti(dst, lhs, rhs_imm);
2555 } else {
2556 if (use_imm) {
2557 rhs_reg = TMP;
2558 __ LoadConst64(rhs_reg, rhs_imm);
2559 }
2560 __ Slt(dst, lhs, rhs_reg);
2561 }
2562 if (cond == kCondGE) {
2563 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2564 // only the slt instruction but no sge.
2565 __ Xori(dst, dst, 1);
2566 }
2567 break;
2568
2569 case kCondLE:
2570 case kCondGT:
2571 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2572 // Simulate lhs <= rhs via lhs < rhs + 1.
2573 __ Slti(dst, lhs, rhs_imm_plus_one);
2574 if (cond == kCondGT) {
2575 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2576 // only the slti instruction but no sgti.
2577 __ Xori(dst, dst, 1);
2578 }
2579 } else {
2580 if (use_imm) {
2581 rhs_reg = TMP;
2582 __ LoadConst64(rhs_reg, rhs_imm);
2583 }
2584 __ Slt(dst, rhs_reg, lhs);
2585 if (cond == kCondLE) {
2586 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2587 // only the slt instruction but no sle.
2588 __ Xori(dst, dst, 1);
2589 }
2590 }
2591 break;
2592
2593 case kCondB:
2594 case kCondAE:
2595 if (use_imm && IsInt<16>(rhs_imm)) {
2596 // Sltiu sign-extends its 16-bit immediate operand before
2597 // the comparison and thus lets us compare directly with
2598 // unsigned values in the ranges [0, 0x7fff] and
2599 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2600 __ Sltiu(dst, lhs, rhs_imm);
2601 } else {
2602 if (use_imm) {
2603 rhs_reg = TMP;
2604 __ LoadConst64(rhs_reg, rhs_imm);
2605 }
2606 __ Sltu(dst, lhs, rhs_reg);
2607 }
2608 if (cond == kCondAE) {
2609 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2610 // only the sltu instruction but no sgeu.
2611 __ Xori(dst, dst, 1);
2612 }
2613 break;
2614
2615 case kCondBE:
2616 case kCondA:
2617 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2618 // Simulate lhs <= rhs via lhs < rhs + 1.
2619 // Note that this only works if rhs + 1 does not overflow
2620 // to 0, hence the check above.
2621 // Sltiu sign-extends its 16-bit immediate operand before
2622 // the comparison and thus lets us compare directly with
2623 // unsigned values in the ranges [0, 0x7fff] and
2624 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2625 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2626 if (cond == kCondA) {
2627 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2628 // only the sltiu instruction but no sgtiu.
2629 __ Xori(dst, dst, 1);
2630 }
2631 } else {
2632 if (use_imm) {
2633 rhs_reg = TMP;
2634 __ LoadConst64(rhs_reg, rhs_imm);
2635 }
2636 __ Sltu(dst, rhs_reg, lhs);
2637 if (cond == kCondBE) {
2638 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2639 // only the sltu instruction but no sleu.
2640 __ Xori(dst, dst, 1);
2641 }
2642 }
2643 break;
2644 }
2645}
2646
2647void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2648 bool is64bit,
2649 LocationSummary* locations,
2650 Mips64Label* label) {
2651 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2652 Location rhs_location = locations->InAt(1);
2653 GpuRegister rhs_reg = ZERO;
2654 int64_t rhs_imm = 0;
2655 bool use_imm = rhs_location.IsConstant();
2656 if (use_imm) {
2657 if (is64bit) {
2658 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2659 } else {
2660 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2661 }
2662 } else {
2663 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2664 }
2665
2666 if (use_imm && rhs_imm == 0) {
2667 switch (cond) {
2668 case kCondEQ:
2669 case kCondBE: // <= 0 if zero
2670 __ Beqzc(lhs, label);
2671 break;
2672 case kCondNE:
2673 case kCondA: // > 0 if non-zero
2674 __ Bnezc(lhs, label);
2675 break;
2676 case kCondLT:
2677 __ Bltzc(lhs, label);
2678 break;
2679 case kCondGE:
2680 __ Bgezc(lhs, label);
2681 break;
2682 case kCondLE:
2683 __ Blezc(lhs, label);
2684 break;
2685 case kCondGT:
2686 __ Bgtzc(lhs, label);
2687 break;
2688 case kCondB: // always false
2689 break;
2690 case kCondAE: // always true
2691 __ Bc(label);
2692 break;
2693 }
2694 } else {
2695 if (use_imm) {
2696 rhs_reg = TMP;
2697 __ LoadConst64(rhs_reg, rhs_imm);
2698 }
2699 switch (cond) {
2700 case kCondEQ:
2701 __ Beqc(lhs, rhs_reg, label);
2702 break;
2703 case kCondNE:
2704 __ Bnec(lhs, rhs_reg, label);
2705 break;
2706 case kCondLT:
2707 __ Bltc(lhs, rhs_reg, label);
2708 break;
2709 case kCondGE:
2710 __ Bgec(lhs, rhs_reg, label);
2711 break;
2712 case kCondLE:
2713 __ Bgec(rhs_reg, lhs, label);
2714 break;
2715 case kCondGT:
2716 __ Bltc(rhs_reg, lhs, label);
2717 break;
2718 case kCondB:
2719 __ Bltuc(lhs, rhs_reg, label);
2720 break;
2721 case kCondAE:
2722 __ Bgeuc(lhs, rhs_reg, label);
2723 break;
2724 case kCondBE:
2725 __ Bgeuc(rhs_reg, lhs, label);
2726 break;
2727 case kCondA:
2728 __ Bltuc(rhs_reg, lhs, label);
2729 break;
2730 }
2731 }
2732}
2733
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002734void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2735 bool gt_bias,
2736 Primitive::Type type,
2737 LocationSummary* locations) {
2738 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2739 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2740 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2741 if (type == Primitive::kPrimFloat) {
2742 switch (cond) {
2743 case kCondEQ:
2744 __ CmpEqS(FTMP, lhs, rhs);
2745 __ Mfc1(dst, FTMP);
2746 __ Andi(dst, dst, 1);
2747 break;
2748 case kCondNE:
2749 __ CmpEqS(FTMP, lhs, rhs);
2750 __ Mfc1(dst, FTMP);
2751 __ Addiu(dst, dst, 1);
2752 break;
2753 case kCondLT:
2754 if (gt_bias) {
2755 __ CmpLtS(FTMP, lhs, rhs);
2756 } else {
2757 __ CmpUltS(FTMP, lhs, rhs);
2758 }
2759 __ Mfc1(dst, FTMP);
2760 __ Andi(dst, dst, 1);
2761 break;
2762 case kCondLE:
2763 if (gt_bias) {
2764 __ CmpLeS(FTMP, lhs, rhs);
2765 } else {
2766 __ CmpUleS(FTMP, lhs, rhs);
2767 }
2768 __ Mfc1(dst, FTMP);
2769 __ Andi(dst, dst, 1);
2770 break;
2771 case kCondGT:
2772 if (gt_bias) {
2773 __ CmpUltS(FTMP, rhs, lhs);
2774 } else {
2775 __ CmpLtS(FTMP, rhs, lhs);
2776 }
2777 __ Mfc1(dst, FTMP);
2778 __ Andi(dst, dst, 1);
2779 break;
2780 case kCondGE:
2781 if (gt_bias) {
2782 __ CmpUleS(FTMP, rhs, lhs);
2783 } else {
2784 __ CmpLeS(FTMP, rhs, lhs);
2785 }
2786 __ Mfc1(dst, FTMP);
2787 __ Andi(dst, dst, 1);
2788 break;
2789 default:
2790 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2791 UNREACHABLE();
2792 }
2793 } else {
2794 DCHECK_EQ(type, Primitive::kPrimDouble);
2795 switch (cond) {
2796 case kCondEQ:
2797 __ CmpEqD(FTMP, lhs, rhs);
2798 __ Mfc1(dst, FTMP);
2799 __ Andi(dst, dst, 1);
2800 break;
2801 case kCondNE:
2802 __ CmpEqD(FTMP, lhs, rhs);
2803 __ Mfc1(dst, FTMP);
2804 __ Addiu(dst, dst, 1);
2805 break;
2806 case kCondLT:
2807 if (gt_bias) {
2808 __ CmpLtD(FTMP, lhs, rhs);
2809 } else {
2810 __ CmpUltD(FTMP, lhs, rhs);
2811 }
2812 __ Mfc1(dst, FTMP);
2813 __ Andi(dst, dst, 1);
2814 break;
2815 case kCondLE:
2816 if (gt_bias) {
2817 __ CmpLeD(FTMP, lhs, rhs);
2818 } else {
2819 __ CmpUleD(FTMP, lhs, rhs);
2820 }
2821 __ Mfc1(dst, FTMP);
2822 __ Andi(dst, dst, 1);
2823 break;
2824 case kCondGT:
2825 if (gt_bias) {
2826 __ CmpUltD(FTMP, rhs, lhs);
2827 } else {
2828 __ CmpLtD(FTMP, rhs, lhs);
2829 }
2830 __ Mfc1(dst, FTMP);
2831 __ Andi(dst, dst, 1);
2832 break;
2833 case kCondGE:
2834 if (gt_bias) {
2835 __ CmpUleD(FTMP, rhs, lhs);
2836 } else {
2837 __ CmpLeD(FTMP, rhs, lhs);
2838 }
2839 __ Mfc1(dst, FTMP);
2840 __ Andi(dst, dst, 1);
2841 break;
2842 default:
2843 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2844 UNREACHABLE();
2845 }
2846 }
2847}
2848
Alexey Frunze299a9392015-12-08 16:08:02 -08002849void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2850 bool gt_bias,
2851 Primitive::Type type,
2852 LocationSummary* locations,
2853 Mips64Label* label) {
2854 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2855 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2856 if (type == Primitive::kPrimFloat) {
2857 switch (cond) {
2858 case kCondEQ:
2859 __ CmpEqS(FTMP, lhs, rhs);
2860 __ Bc1nez(FTMP, label);
2861 break;
2862 case kCondNE:
2863 __ CmpEqS(FTMP, lhs, rhs);
2864 __ Bc1eqz(FTMP, label);
2865 break;
2866 case kCondLT:
2867 if (gt_bias) {
2868 __ CmpLtS(FTMP, lhs, rhs);
2869 } else {
2870 __ CmpUltS(FTMP, lhs, rhs);
2871 }
2872 __ Bc1nez(FTMP, label);
2873 break;
2874 case kCondLE:
2875 if (gt_bias) {
2876 __ CmpLeS(FTMP, lhs, rhs);
2877 } else {
2878 __ CmpUleS(FTMP, lhs, rhs);
2879 }
2880 __ Bc1nez(FTMP, label);
2881 break;
2882 case kCondGT:
2883 if (gt_bias) {
2884 __ CmpUltS(FTMP, rhs, lhs);
2885 } else {
2886 __ CmpLtS(FTMP, rhs, lhs);
2887 }
2888 __ Bc1nez(FTMP, label);
2889 break;
2890 case kCondGE:
2891 if (gt_bias) {
2892 __ CmpUleS(FTMP, rhs, lhs);
2893 } else {
2894 __ CmpLeS(FTMP, rhs, lhs);
2895 }
2896 __ Bc1nez(FTMP, label);
2897 break;
2898 default:
2899 LOG(FATAL) << "Unexpected non-floating-point condition";
2900 }
2901 } else {
2902 DCHECK_EQ(type, Primitive::kPrimDouble);
2903 switch (cond) {
2904 case kCondEQ:
2905 __ CmpEqD(FTMP, lhs, rhs);
2906 __ Bc1nez(FTMP, label);
2907 break;
2908 case kCondNE:
2909 __ CmpEqD(FTMP, lhs, rhs);
2910 __ Bc1eqz(FTMP, label);
2911 break;
2912 case kCondLT:
2913 if (gt_bias) {
2914 __ CmpLtD(FTMP, lhs, rhs);
2915 } else {
2916 __ CmpUltD(FTMP, lhs, rhs);
2917 }
2918 __ Bc1nez(FTMP, label);
2919 break;
2920 case kCondLE:
2921 if (gt_bias) {
2922 __ CmpLeD(FTMP, lhs, rhs);
2923 } else {
2924 __ CmpUleD(FTMP, lhs, rhs);
2925 }
2926 __ Bc1nez(FTMP, label);
2927 break;
2928 case kCondGT:
2929 if (gt_bias) {
2930 __ CmpUltD(FTMP, rhs, lhs);
2931 } else {
2932 __ CmpLtD(FTMP, rhs, lhs);
2933 }
2934 __ Bc1nez(FTMP, label);
2935 break;
2936 case kCondGE:
2937 if (gt_bias) {
2938 __ CmpUleD(FTMP, rhs, lhs);
2939 } else {
2940 __ CmpLeD(FTMP, rhs, lhs);
2941 }
2942 __ Bc1nez(FTMP, label);
2943 break;
2944 default:
2945 LOG(FATAL) << "Unexpected non-floating-point condition";
2946 }
2947 }
2948}
2949
Alexey Frunze4dda3372015-06-01 18:31:49 -07002950void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002951 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002952 Mips64Label* true_target,
2953 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002954 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955
David Brazdil0debae72015-11-12 18:37:00 +00002956 if (true_target == nullptr && false_target == nullptr) {
2957 // Nothing to do. The code always falls through.
2958 return;
2959 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002960 // Constant condition, statically compared against "true" (integer value 1).
2961 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002962 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002963 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002964 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002965 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002966 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002967 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002968 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002969 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 }
David Brazdil0debae72015-11-12 18:37:00 +00002971 return;
2972 }
2973
2974 // The following code generates these patterns:
2975 // (1) true_target == nullptr && false_target != nullptr
2976 // - opposite condition true => branch to false_target
2977 // (2) true_target != nullptr && false_target == nullptr
2978 // - condition true => branch to true_target
2979 // (3) true_target != nullptr && false_target != nullptr
2980 // - condition true => branch to true_target
2981 // - branch to false_target
2982 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002984 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002985 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002986 if (true_target == nullptr) {
2987 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2988 } else {
2989 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2990 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002991 } else {
2992 // The condition instruction has not been materialized, use its inputs as
2993 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002994 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002995 Primitive::Type type = condition->InputAt(0)->GetType();
2996 LocationSummary* locations = cond->GetLocations();
2997 IfCondition if_cond = condition->GetCondition();
2998 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002999
David Brazdil0debae72015-11-12 18:37:00 +00003000 if (true_target == nullptr) {
3001 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003002 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003003 }
3004
Alexey Frunze299a9392015-12-08 16:08:02 -08003005 switch (type) {
3006 default:
3007 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3008 break;
3009 case Primitive::kPrimLong:
3010 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3011 break;
3012 case Primitive::kPrimFloat:
3013 case Primitive::kPrimDouble:
3014 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3015 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003016 }
3017 }
David Brazdil0debae72015-11-12 18:37:00 +00003018
3019 // If neither branch falls through (case 3), the conditional branch to `true_target`
3020 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3021 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003022 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003023 }
3024}
3025
3026void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003028 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003029 locations->SetInAt(0, Location::RequiresRegister());
3030 }
3031}
3032
3033void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003034 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3035 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003036 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003037 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003038 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003039 nullptr : codegen_->GetLabelOf(false_successor);
3040 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003041}
3042
3043void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3044 LocationSummary* locations = new (GetGraph()->GetArena())
3045 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003046 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003047 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003048 locations->SetInAt(0, Location::RequiresRegister());
3049 }
3050}
3051
3052void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003053 SlowPathCodeMIPS64* slow_path =
3054 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003055 GenerateTestAndBranch(deoptimize,
3056 /* condition_input_index */ 0,
3057 slow_path->GetEntryLabel(),
3058 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003059}
3060
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003061void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3062 LocationSummary* locations = new (GetGraph()->GetArena())
3063 LocationSummary(flag, LocationSummary::kNoCall);
3064 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003065}
3066
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003067void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3068 __ LoadFromOffset(kLoadWord,
3069 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3070 SP,
3071 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003072}
3073
David Brazdil74eb1b22015-12-14 11:44:01 +00003074void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3076 if (Primitive::IsFloatingPointType(select->GetType())) {
3077 locations->SetInAt(0, Location::RequiresFpuRegister());
3078 locations->SetInAt(1, Location::RequiresFpuRegister());
3079 } else {
3080 locations->SetInAt(0, Location::RequiresRegister());
3081 locations->SetInAt(1, Location::RequiresRegister());
3082 }
3083 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3084 locations->SetInAt(2, Location::RequiresRegister());
3085 }
3086 locations->SetOut(Location::SameAsFirstInput());
3087}
3088
3089void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3090 LocationSummary* locations = select->GetLocations();
3091 Mips64Label false_target;
3092 GenerateTestAndBranch(select,
3093 /* condition_input_index */ 2,
3094 /* true_target */ nullptr,
3095 &false_target);
3096 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3097 __ Bind(&false_target);
3098}
3099
David Srbecky0cf44932015-12-09 14:09:59 +00003100void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3101 new (GetGraph()->GetArena()) LocationSummary(info);
3102}
3103
David Srbeckyd28f4a02016-03-14 17:14:24 +00003104void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3105 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003106}
3107
3108void CodeGeneratorMIPS64::GenerateNop() {
3109 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003110}
3111
Alexey Frunze4dda3372015-06-01 18:31:49 -07003112void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3113 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3114 LocationSummary* locations =
3115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3116 locations->SetInAt(0, Location::RequiresRegister());
3117 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3118 locations->SetOut(Location::RequiresFpuRegister());
3119 } else {
3120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3121 }
3122}
3123
3124void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3125 const FieldInfo& field_info) {
3126 Primitive::Type type = field_info.GetFieldType();
3127 LocationSummary* locations = instruction->GetLocations();
3128 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3129 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003130 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003131 switch (type) {
3132 case Primitive::kPrimBoolean:
3133 load_type = kLoadUnsignedByte;
3134 break;
3135 case Primitive::kPrimByte:
3136 load_type = kLoadSignedByte;
3137 break;
3138 case Primitive::kPrimShort:
3139 load_type = kLoadSignedHalfword;
3140 break;
3141 case Primitive::kPrimChar:
3142 load_type = kLoadUnsignedHalfword;
3143 break;
3144 case Primitive::kPrimInt:
3145 case Primitive::kPrimFloat:
3146 load_type = kLoadWord;
3147 break;
3148 case Primitive::kPrimLong:
3149 case Primitive::kPrimDouble:
3150 load_type = kLoadDoubleword;
3151 break;
3152 case Primitive::kPrimNot:
3153 load_type = kLoadUnsignedWord;
3154 break;
3155 case Primitive::kPrimVoid:
3156 LOG(FATAL) << "Unreachable type " << type;
3157 UNREACHABLE();
3158 }
3159 if (!Primitive::IsFloatingPointType(type)) {
3160 DCHECK(locations->Out().IsRegister());
3161 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003162 __ LoadFromOffset(load_type, dst, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003163 } else {
3164 DCHECK(locations->Out().IsFpuRegister());
3165 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003166 __ LoadFpuFromOffset(load_type, dst, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003167 }
3168
3169 codegen_->MaybeRecordImplicitNullCheck(instruction);
3170 // TODO: memory barrier?
Alexey Frunzec061de12017-02-14 13:27:23 -08003171
3172 if (type == Primitive::kPrimNot) {
3173 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3174 __ MaybeUnpoisonHeapReference(dst);
3175 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176}
3177
3178void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3179 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3180 LocationSummary* locations =
3181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3182 locations->SetInAt(0, Location::RequiresRegister());
3183 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3184 locations->SetInAt(1, Location::RequiresFpuRegister());
3185 } else {
3186 locations->SetInAt(1, Location::RequiresRegister());
3187 }
3188}
3189
3190void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003191 const FieldInfo& field_info,
3192 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003193 Primitive::Type type = field_info.GetFieldType();
3194 LocationSummary* locations = instruction->GetLocations();
3195 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3196 StoreOperandType store_type = kStoreByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003197 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3198 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003199 switch (type) {
3200 case Primitive::kPrimBoolean:
3201 case Primitive::kPrimByte:
3202 store_type = kStoreByte;
3203 break;
3204 case Primitive::kPrimShort:
3205 case Primitive::kPrimChar:
3206 store_type = kStoreHalfword;
3207 break;
3208 case Primitive::kPrimInt:
3209 case Primitive::kPrimFloat:
3210 case Primitive::kPrimNot:
3211 store_type = kStoreWord;
3212 break;
3213 case Primitive::kPrimLong:
3214 case Primitive::kPrimDouble:
3215 store_type = kStoreDoubleword;
3216 break;
3217 case Primitive::kPrimVoid:
3218 LOG(FATAL) << "Unreachable type " << type;
3219 UNREACHABLE();
3220 }
3221 if (!Primitive::IsFloatingPointType(type)) {
3222 DCHECK(locations->InAt(1).IsRegister());
3223 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003224 if (kPoisonHeapReferences && needs_write_barrier) {
3225 // Note that in the case where `value` is a null reference,
3226 // we do not enter this block, as a null reference does not
3227 // need poisoning.
3228 DCHECK_EQ(type, Primitive::kPrimNot);
3229 __ PoisonHeapReference(TMP, src);
3230 __ StoreToOffset(store_type, TMP, obj, offset);
3231 } else {
3232 __ StoreToOffset(store_type, src, obj, offset);
3233 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 } else {
3235 DCHECK(locations->InAt(1).IsFpuRegister());
3236 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003237 __ StoreFpuToOffset(store_type, src, obj, offset);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003238 }
3239
3240 codegen_->MaybeRecordImplicitNullCheck(instruction);
3241 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08003242 if (needs_write_barrier) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003243 DCHECK(locations->InAt(1).IsRegister());
3244 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003245 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003246 }
3247}
3248
3249void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3250 HandleFieldGet(instruction, instruction->GetFieldInfo());
3251}
3252
3253void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3254 HandleFieldGet(instruction, instruction->GetFieldInfo());
3255}
3256
3257void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3258 HandleFieldSet(instruction, instruction->GetFieldInfo());
3259}
3260
3261void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003262 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003263}
3264
Alexey Frunzef63f5692016-12-13 17:43:11 -08003265void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3266 HInstruction* instruction ATTRIBUTE_UNUSED,
3267 Location root,
3268 GpuRegister obj,
3269 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003270 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3271 if (kEmitCompilerReadBarrier) {
3272 UNIMPLEMENTED(FATAL) << "for read barrier";
3273 } else {
3274 // Plain GC root load with no read barrier.
3275 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3276 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3277 // Note that GC roots are not affected by heap poisoning, thus we
3278 // do not have to unpoison `root_reg` here.
3279 }
3280}
3281
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3283 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003284 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3286 locations->SetInAt(0, Location::RequiresRegister());
3287 locations->SetInAt(1, Location::RequiresRegister());
3288 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003289 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003290 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3291}
3292
3293void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
3294 LocationSummary* locations = instruction->GetLocations();
3295 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3296 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3297 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3298
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003299 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003300
3301 // Return 0 if `obj` is null.
3302 // TODO: Avoid this check if we know `obj` is not null.
3303 __ Move(out, ZERO);
3304 __ Beqzc(obj, &done);
3305
3306 // Compare the class of `obj` with `cls`.
3307 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Alexey Frunzec061de12017-02-14 13:27:23 -08003308 __ MaybeUnpoisonHeapReference(out);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003309 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003310 // Classes must be equal for the instanceof to succeed.
3311 __ Xor(out, out, cls);
3312 __ Sltiu(out, out, 1);
3313 } else {
3314 // If the classes are not equal, we go into a slow path.
3315 DCHECK(locations->OnlyCallsOnSlowPath());
3316 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003317 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003318 codegen_->AddSlowPath(slow_path);
3319 __ Bnec(out, cls, slow_path->GetEntryLabel());
3320 __ LoadConst32(out, 1);
3321 __ Bind(slow_path->GetExitLabel());
3322 }
3323
3324 __ Bind(&done);
3325}
3326
3327void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3329 locations->SetOut(Location::ConstantLocation(constant));
3330}
3331
3332void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3333 // Will be generated at use site.
3334}
3335
3336void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3338 locations->SetOut(Location::ConstantLocation(constant));
3339}
3340
3341void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3342 // Will be generated at use site.
3343}
3344
Calin Juravle175dc732015-08-25 15:42:32 +01003345void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3346 // The trampoline uses the same calling convention as dex calling conventions,
3347 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3348 // the method_idx.
3349 HandleInvoke(invoke);
3350}
3351
3352void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3353 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3354}
3355
Alexey Frunze4dda3372015-06-01 18:31:49 -07003356void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3357 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3358 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3359}
3360
3361void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3362 HandleInvoke(invoke);
3363 // The register T0 is required to be used for the hidden argument in
3364 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3365 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3366}
3367
3368void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3369 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3370 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003371 Location receiver = invoke->GetLocations()->InAt(0);
3372 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003373 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003374
3375 // Set the hidden argument.
3376 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3377 invoke->GetDexMethodIndex());
3378
3379 // temp = object->GetClass();
3380 if (receiver.IsStackSlot()) {
3381 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3382 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3383 } else {
3384 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3385 }
3386 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003387 // Instead of simply (possibly) unpoisoning `temp` here, we should
3388 // emit a read barrier for the previous class reference load.
3389 // However this is not required in practice, as this is an
3390 // intermediate/temporary reference and because the current
3391 // concurrent copying collector keeps the from-space memory
3392 // intact/accessible until the end of the marking phase (the
3393 // concurrent copying collector may not in the future).
3394 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003395 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3396 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3397 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003398 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003399 // temp = temp->GetImtEntryAt(method_offset);
3400 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3401 // T9 = temp->GetEntryPoint();
3402 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3403 // T9();
3404 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003405 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406 DCHECK(!codegen_->IsLeafMethod());
3407 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3408}
3409
3410void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003411 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3412 if (intrinsic.TryDispatch(invoke)) {
3413 return;
3414 }
3415
Alexey Frunze4dda3372015-06-01 18:31:49 -07003416 HandleInvoke(invoke);
3417}
3418
3419void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003420 // Explicit clinit checks triggered by static invokes must have been pruned by
3421 // art::PrepareForRegisterAllocation.
3422 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003423
Chris Larsen3039e382015-08-26 07:54:08 -07003424 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3425 if (intrinsic.TryDispatch(invoke)) {
3426 return;
3427 }
3428
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003430}
3431
Orion Hodsonac141392017-01-13 11:53:47 +00003432void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3433 HandleInvoke(invoke);
3434}
3435
3436void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3437 codegen_->GenerateInvokePolymorphicCall(invoke);
3438}
3439
Chris Larsen3039e382015-08-26 07:54:08 -07003440static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003441 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003442 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3443 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 return true;
3445 }
3446 return false;
3447}
3448
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003449HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003450 HLoadString::LoadKind desired_string_load_kind) {
3451 if (kEmitCompilerReadBarrier) {
3452 UNIMPLEMENTED(FATAL) << "for read barrier";
3453 }
3454 bool fallback_load = false;
3455 switch (desired_string_load_kind) {
3456 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3457 DCHECK(!GetCompilerOptions().GetCompilePic());
3458 break;
3459 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3460 DCHECK(GetCompilerOptions().GetCompilePic());
3461 break;
3462 case HLoadString::LoadKind::kBootImageAddress:
3463 break;
3464 case HLoadString::LoadKind::kBssEntry:
3465 DCHECK(!Runtime::Current()->UseJitCompilation());
3466 break;
3467 case HLoadString::LoadKind::kDexCacheViaMethod:
3468 break;
3469 case HLoadString::LoadKind::kJitTableAddress:
3470 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003471 break;
3472 }
3473 if (fallback_load) {
3474 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3475 }
3476 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003477}
3478
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003479HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3480 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003481 if (kEmitCompilerReadBarrier) {
3482 UNIMPLEMENTED(FATAL) << "for read barrier";
3483 }
3484 bool fallback_load = false;
3485 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003486 case HLoadClass::LoadKind::kInvalid:
3487 LOG(FATAL) << "UNREACHABLE";
3488 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003489 case HLoadClass::LoadKind::kReferrersClass:
3490 break;
3491 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3492 DCHECK(!GetCompilerOptions().GetCompilePic());
3493 break;
3494 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3495 DCHECK(GetCompilerOptions().GetCompilePic());
3496 break;
3497 case HLoadClass::LoadKind::kBootImageAddress:
3498 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003499 case HLoadClass::LoadKind::kBssEntry:
3500 DCHECK(!Runtime::Current()->UseJitCompilation());
3501 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003502 case HLoadClass::LoadKind::kJitTableAddress:
3503 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003504 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003505 case HLoadClass::LoadKind::kDexCacheViaMethod:
3506 break;
3507 }
3508 if (fallback_load) {
3509 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3510 }
3511 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003512}
3513
Vladimir Markodc151b22015-10-15 18:02:30 +01003514HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3515 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003516 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003517 // On MIPS64 we support all dispatch types.
3518 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003519}
3520
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3522 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003523 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003524 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3525 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3526
Alexey Frunze19f6c692016-11-30 19:19:55 -08003527 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003528 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003529 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003530 uint32_t offset =
3531 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003532 __ LoadFromOffset(kLoadDoubleword,
3533 temp.AsRegister<GpuRegister>(),
3534 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003535 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003536 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003537 }
Vladimir Marko58155012015-08-19 12:49:41 +00003538 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003539 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003540 break;
3541 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003542 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3543 kLoadDoubleword,
3544 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003545 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003546 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3547 uint32_t offset = invoke->GetDexCacheArrayOffset();
3548 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003549 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003550 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3551 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3552 break;
3553 }
Vladimir Marko58155012015-08-19 12:49:41 +00003554 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003555 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003556 GpuRegister reg = temp.AsRegister<GpuRegister>();
3557 GpuRegister method_reg;
3558 if (current_method.IsRegister()) {
3559 method_reg = current_method.AsRegister<GpuRegister>();
3560 } else {
3561 // TODO: use the appropriate DCHECK() here if possible.
3562 // DCHECK(invoke->GetLocations()->Intrinsified());
3563 DCHECK(!current_method.IsValid());
3564 method_reg = reg;
3565 __ Ld(reg, SP, kCurrentMethodStackOffset);
3566 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003567
Vladimir Marko58155012015-08-19 12:49:41 +00003568 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003569 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003570 reg,
3571 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003572 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003573 // temp = temp[index_in_cache];
3574 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3575 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003576 __ LoadFromOffset(kLoadDoubleword,
3577 reg,
3578 reg,
3579 CodeGenerator::GetCachePointerOffset(index_in_cache));
3580 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003581 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003582 }
3583
Alexey Frunze19f6c692016-11-30 19:19:55 -08003584 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003585 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003586 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003587 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003588 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3589 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3590 __ LoadFromOffset(kLoadDoubleword,
3591 T9,
3592 callee_method.AsRegister<GpuRegister>(),
3593 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003594 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003595 // T9()
3596 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003597 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003598 break;
3599 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003600 DCHECK(!IsLeafMethod());
3601}
3602
3603void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003604 // Explicit clinit checks triggered by static invokes must have been pruned by
3605 // art::PrepareForRegisterAllocation.
3606 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003607
3608 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3609 return;
3610 }
3611
3612 LocationSummary* locations = invoke->GetLocations();
3613 codegen_->GenerateStaticOrDirectCall(invoke,
3614 locations->HasTemps()
3615 ? locations->GetTemp(0)
3616 : Location::NoLocation());
3617 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3618}
3619
Alexey Frunze53afca12015-11-05 16:34:23 -08003620void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003621 // Use the calling convention instead of the location of the receiver, as
3622 // intrinsics may have put the receiver in a different register. In the intrinsics
3623 // slow path, the arguments have been moved to the right place, so here we are
3624 // guaranteed that the receiver is the first register of the calling convention.
3625 InvokeDexCallingConvention calling_convention;
3626 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3627
Alexey Frunze53afca12015-11-05 16:34:23 -08003628 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3630 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3631 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003632 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003633
3634 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003635 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003636 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003637 // Instead of simply (possibly) unpoisoning `temp` here, we should
3638 // emit a read barrier for the previous class reference load.
3639 // However this is not required in practice, as this is an
3640 // intermediate/temporary reference and because the current
3641 // concurrent copying collector keeps the from-space memory
3642 // intact/accessible until the end of the marking phase (the
3643 // concurrent copying collector may not in the future).
3644 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003645 // temp = temp->GetMethodAt(method_offset);
3646 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3647 // T9 = temp->GetEntryPoint();
3648 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3649 // T9();
3650 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003651 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003652}
3653
3654void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3655 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3656 return;
3657 }
3658
3659 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003660 DCHECK(!codegen_->IsLeafMethod());
3661 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3662}
3663
3664void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003665 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3666 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003667 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003668 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003669 cls,
3670 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003671 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003672 return;
3673 }
Vladimir Marko41559982017-01-06 14:04:23 +00003674 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003675
3676 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3677 ? LocationSummary::kCallOnSlowPath
3678 : LocationSummary::kNoCall;
3679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003680 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003681 locations->SetInAt(0, Location::RequiresRegister());
3682 }
3683 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003684}
3685
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003686// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3687// move.
3688void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003689 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3690 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3691 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003692 return;
3693 }
Vladimir Marko41559982017-01-06 14:04:23 +00003694 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003695
Vladimir Marko41559982017-01-06 14:04:23 +00003696 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003697 Location out_loc = locations->Out();
3698 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3699 GpuRegister current_method_reg = ZERO;
3700 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3701 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3702 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3703 }
3704
3705 bool generate_null_check = false;
3706 switch (load_kind) {
3707 case HLoadClass::LoadKind::kReferrersClass:
3708 DCHECK(!cls->CanCallRuntime());
3709 DCHECK(!cls->MustGenerateClinitCheck());
3710 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3711 GenerateGcRootFieldLoad(cls,
3712 out_loc,
3713 current_method_reg,
3714 ArtMethod::DeclaringClassOffset().Int32Value());
3715 break;
3716 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003717 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003718 __ LoadLiteral(out,
3719 kLoadUnsignedWord,
3720 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3721 cls->GetTypeIndex()));
3722 break;
3723 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003724 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003725 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3726 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3727 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3728 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3729 break;
3730 }
3731 case HLoadClass::LoadKind::kBootImageAddress: {
3732 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003733 uint32_t address = dchecked_integral_cast<uint32_t>(
3734 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
3735 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003736 __ LoadLiteral(out,
3737 kLoadUnsignedWord,
3738 codegen_->DeduplicateBootImageAddressLiteral(address));
3739 break;
3740 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003741 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003742 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00003743 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003744 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3745 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003746 generate_null_check = true;
3747 break;
3748 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003749 case HLoadClass::LoadKind::kJitTableAddress:
3750 __ LoadLiteral(out,
3751 kLoadUnsignedWord,
3752 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
3753 cls->GetTypeIndex(),
3754 cls->GetClass()));
3755 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003756 break;
Vladimir Marko41559982017-01-06 14:04:23 +00003757 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003758 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00003759 LOG(FATAL) << "UNREACHABLE";
3760 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003761 }
3762
3763 if (generate_null_check || cls->MustGenerateClinitCheck()) {
3764 DCHECK(cls->CanCallRuntime());
3765 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3766 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3767 codegen_->AddSlowPath(slow_path);
3768 if (generate_null_check) {
3769 __ Beqzc(out, slow_path->GetEntryLabel());
3770 }
3771 if (cls->MustGenerateClinitCheck()) {
3772 GenerateClassInitializationCheck(slow_path, out);
3773 } else {
3774 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003775 }
3776 }
3777}
3778
David Brazdilcb1c0552015-08-04 16:22:25 +01003779static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003780 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003781}
3782
Alexey Frunze4dda3372015-06-01 18:31:49 -07003783void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3784 LocationSummary* locations =
3785 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3786 locations->SetOut(Location::RequiresRegister());
3787}
3788
3789void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3790 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003791 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3792}
3793
3794void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3795 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3796}
3797
3798void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3799 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003800}
3801
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003803 HLoadString::LoadKind load_kind = load->GetLoadKind();
3804 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003806 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
3807 InvokeRuntimeCallingConvention calling_convention;
3808 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
3809 } else {
3810 locations->SetOut(Location::RequiresRegister());
3811 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003812}
3813
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003814// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3815// move.
3816void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003817 HLoadString::LoadKind load_kind = load->GetLoadKind();
3818 LocationSummary* locations = load->GetLocations();
3819 Location out_loc = locations->Out();
3820 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3821
3822 switch (load_kind) {
3823 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003824 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003825 __ LoadLiteral(out,
3826 kLoadUnsignedWord,
3827 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
3828 load->GetStringIndex()));
3829 return; // No dex cache slow path.
3830 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
3831 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
3832 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003833 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003834 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3835 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3836 return; // No dex cache slow path.
3837 }
3838 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00003839 uint32_t address = dchecked_integral_cast<uint32_t>(
3840 reinterpret_cast<uintptr_t>(load->GetString().Get()));
3841 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003842 __ LoadLiteral(out,
3843 kLoadUnsignedWord,
3844 codegen_->DeduplicateBootImageAddressLiteral(address));
3845 return; // No dex cache slow path.
3846 }
3847 case HLoadString::LoadKind::kBssEntry: {
3848 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
3849 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003850 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08003851 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
3852 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunzef63f5692016-12-13 17:43:11 -08003853 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3854 codegen_->AddSlowPath(slow_path);
3855 __ Beqzc(out, slow_path->GetEntryLabel());
3856 __ Bind(slow_path->GetExitLabel());
3857 return;
3858 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08003859 case HLoadString::LoadKind::kJitTableAddress:
3860 __ LoadLiteral(out,
3861 kLoadUnsignedWord,
3862 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
3863 load->GetStringIndex(),
3864 load->GetString()));
3865 GenerateGcRootFieldLoad(load, out_loc, out, 0);
3866 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003867 default:
3868 break;
3869 }
3870
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003871 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08003872 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
3873 InvokeRuntimeCallingConvention calling_convention;
3874 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
3875 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
3876 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003877}
3878
Alexey Frunze4dda3372015-06-01 18:31:49 -07003879void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3880 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3881 locations->SetOut(Location::ConstantLocation(constant));
3882}
3883
3884void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3885 // Will be generated at use site.
3886}
3887
3888void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3889 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003890 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003891 InvokeRuntimeCallingConvention calling_convention;
3892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3893}
3894
3895void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003896 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003897 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003898 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003899 if (instruction->IsEnter()) {
3900 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3901 } else {
3902 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3903 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003904}
3905
3906void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3907 LocationSummary* locations =
3908 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3909 switch (mul->GetResultType()) {
3910 case Primitive::kPrimInt:
3911 case Primitive::kPrimLong:
3912 locations->SetInAt(0, Location::RequiresRegister());
3913 locations->SetInAt(1, Location::RequiresRegister());
3914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3915 break;
3916
3917 case Primitive::kPrimFloat:
3918 case Primitive::kPrimDouble:
3919 locations->SetInAt(0, Location::RequiresFpuRegister());
3920 locations->SetInAt(1, Location::RequiresFpuRegister());
3921 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3922 break;
3923
3924 default:
3925 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3926 }
3927}
3928
3929void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3930 Primitive::Type type = instruction->GetType();
3931 LocationSummary* locations = instruction->GetLocations();
3932
3933 switch (type) {
3934 case Primitive::kPrimInt:
3935 case Primitive::kPrimLong: {
3936 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3937 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3938 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3939 if (type == Primitive::kPrimInt)
3940 __ MulR6(dst, lhs, rhs);
3941 else
3942 __ Dmul(dst, lhs, rhs);
3943 break;
3944 }
3945 case Primitive::kPrimFloat:
3946 case Primitive::kPrimDouble: {
3947 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3948 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3949 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3950 if (type == Primitive::kPrimFloat)
3951 __ MulS(dst, lhs, rhs);
3952 else
3953 __ MulD(dst, lhs, rhs);
3954 break;
3955 }
3956 default:
3957 LOG(FATAL) << "Unexpected mul type " << type;
3958 }
3959}
3960
3961void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3962 LocationSummary* locations =
3963 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3964 switch (neg->GetResultType()) {
3965 case Primitive::kPrimInt:
3966 case Primitive::kPrimLong:
3967 locations->SetInAt(0, Location::RequiresRegister());
3968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3969 break;
3970
3971 case Primitive::kPrimFloat:
3972 case Primitive::kPrimDouble:
3973 locations->SetInAt(0, Location::RequiresFpuRegister());
3974 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3975 break;
3976
3977 default:
3978 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3979 }
3980}
3981
3982void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3983 Primitive::Type type = instruction->GetType();
3984 LocationSummary* locations = instruction->GetLocations();
3985
3986 switch (type) {
3987 case Primitive::kPrimInt:
3988 case Primitive::kPrimLong: {
3989 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3990 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3991 if (type == Primitive::kPrimInt)
3992 __ Subu(dst, ZERO, src);
3993 else
3994 __ Dsubu(dst, ZERO, src);
3995 break;
3996 }
3997 case Primitive::kPrimFloat:
3998 case Primitive::kPrimDouble: {
3999 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4000 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4001 if (type == Primitive::kPrimFloat)
4002 __ NegS(dst, src);
4003 else
4004 __ NegD(dst, src);
4005 break;
4006 }
4007 default:
4008 LOG(FATAL) << "Unexpected neg type " << type;
4009 }
4010}
4011
4012void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
4013 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004016 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004017 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4018 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004022 // Note: if heap poisoning is enabled, the entry point takes care
4023 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004024 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4025 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004026}
4027
4028void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
4029 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004032 if (instruction->IsStringAlloc()) {
4033 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4034 } else {
4035 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004036 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004037 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4038}
4039
4040void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004041 // Note: if heap poisoning is enabled, the entry point takes care
4042 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004043 if (instruction->IsStringAlloc()) {
4044 // String is allocated through StringFactory. Call NewEmptyString entry point.
4045 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02004046 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07004047 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004048 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4049 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
4050 __ Jalr(T9);
4051 __ Nop();
4052 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4053 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01004054 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004055 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004056 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004057}
4058
4059void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
4060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4061 locations->SetInAt(0, Location::RequiresRegister());
4062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4063}
4064
4065void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
4066 Primitive::Type type = instruction->GetType();
4067 LocationSummary* locations = instruction->GetLocations();
4068
4069 switch (type) {
4070 case Primitive::kPrimInt:
4071 case Primitive::kPrimLong: {
4072 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4073 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4074 __ Nor(dst, src, ZERO);
4075 break;
4076 }
4077
4078 default:
4079 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4080 }
4081}
4082
4083void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4085 locations->SetInAt(0, Location::RequiresRegister());
4086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4087}
4088
4089void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4090 LocationSummary* locations = instruction->GetLocations();
4091 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4092 locations->InAt(0).AsRegister<GpuRegister>(),
4093 1);
4094}
4095
4096void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004097 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4098 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099}
4100
Calin Juravle2ae48182016-03-16 14:05:09 +00004101void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4102 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004103 return;
4104 }
4105 Location obj = instruction->GetLocations()->InAt(0);
4106
4107 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004108 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109}
4110
Calin Juravle2ae48182016-03-16 14:05:09 +00004111void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004112 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004113 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004114
4115 Location obj = instruction->GetLocations()->InAt(0);
4116
4117 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004121 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004122}
4123
4124void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4125 HandleBinaryOp(instruction);
4126}
4127
4128void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4129 HandleBinaryOp(instruction);
4130}
4131
4132void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4133 LOG(FATAL) << "Unreachable";
4134}
4135
4136void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4137 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4138}
4139
4140void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4142 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4143 if (location.IsStackSlot()) {
4144 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4145 } else if (location.IsDoubleStackSlot()) {
4146 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4147 }
4148 locations->SetOut(location);
4149}
4150
4151void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4152 ATTRIBUTE_UNUSED) {
4153 // Nothing to do, the parameter is already at its location.
4154}
4155
4156void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4159 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4160}
4161
4162void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4163 ATTRIBUTE_UNUSED) {
4164 // Nothing to do, the method is already at its location.
4165}
4166
4167void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4168 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004169 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170 locations->SetInAt(i, Location::Any());
4171 }
4172 locations->SetOut(Location::Any());
4173}
4174
4175void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4176 LOG(FATAL) << "Unreachable";
4177}
4178
4179void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4180 Primitive::Type type = rem->GetResultType();
4181 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004182 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4183 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4185
4186 switch (type) {
4187 case Primitive::kPrimInt:
4188 case Primitive::kPrimLong:
4189 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004190 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4192 break;
4193
4194 case Primitive::kPrimFloat:
4195 case Primitive::kPrimDouble: {
4196 InvokeRuntimeCallingConvention calling_convention;
4197 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4198 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4199 locations->SetOut(calling_convention.GetReturnLocation(type));
4200 break;
4201 }
4202
4203 default:
4204 LOG(FATAL) << "Unexpected rem type " << type;
4205 }
4206}
4207
4208void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4209 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004210
4211 switch (type) {
4212 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004213 case Primitive::kPrimLong:
4214 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004215 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004216
4217 case Primitive::kPrimFloat:
4218 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004219 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4220 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004221 if (type == Primitive::kPrimFloat) {
4222 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4223 } else {
4224 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4225 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004226 break;
4227 }
4228 default:
4229 LOG(FATAL) << "Unexpected rem type " << type;
4230 }
4231}
4232
4233void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4234 memory_barrier->SetLocations(nullptr);
4235}
4236
4237void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4238 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4239}
4240
4241void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4243 Primitive::Type return_type = ret->InputAt(0)->GetType();
4244 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4245}
4246
4247void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4248 codegen_->GenerateFrameExit();
4249}
4250
4251void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4252 ret->SetLocations(nullptr);
4253}
4254
4255void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4256 codegen_->GenerateFrameExit();
4257}
4258
Alexey Frunze92d90602015-12-18 18:16:36 -08004259void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4260 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004261}
4262
Alexey Frunze92d90602015-12-18 18:16:36 -08004263void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4264 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004265}
4266
Alexey Frunze4dda3372015-06-01 18:31:49 -07004267void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4268 HandleShift(shl);
4269}
4270
4271void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4272 HandleShift(shl);
4273}
4274
4275void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4276 HandleShift(shr);
4277}
4278
4279void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4280 HandleShift(shr);
4281}
4282
Alexey Frunze4dda3372015-06-01 18:31:49 -07004283void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4284 HandleBinaryOp(instruction);
4285}
4286
4287void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4288 HandleBinaryOp(instruction);
4289}
4290
4291void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4292 HandleFieldGet(instruction, instruction->GetFieldInfo());
4293}
4294
4295void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4296 HandleFieldGet(instruction, instruction->GetFieldInfo());
4297}
4298
4299void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4300 HandleFieldSet(instruction, instruction->GetFieldInfo());
4301}
4302
4303void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004304 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004305}
4306
Calin Juravlee460d1d2015-09-29 04:52:17 +01004307void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4308 HUnresolvedInstanceFieldGet* instruction) {
4309 FieldAccessCallingConventionMIPS64 calling_convention;
4310 codegen_->CreateUnresolvedFieldLocationSummary(
4311 instruction, instruction->GetFieldType(), calling_convention);
4312}
4313
4314void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4315 HUnresolvedInstanceFieldGet* instruction) {
4316 FieldAccessCallingConventionMIPS64 calling_convention;
4317 codegen_->GenerateUnresolvedFieldAccess(instruction,
4318 instruction->GetFieldType(),
4319 instruction->GetFieldIndex(),
4320 instruction->GetDexPc(),
4321 calling_convention);
4322}
4323
4324void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4325 HUnresolvedInstanceFieldSet* instruction) {
4326 FieldAccessCallingConventionMIPS64 calling_convention;
4327 codegen_->CreateUnresolvedFieldLocationSummary(
4328 instruction, instruction->GetFieldType(), calling_convention);
4329}
4330
4331void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4332 HUnresolvedInstanceFieldSet* instruction) {
4333 FieldAccessCallingConventionMIPS64 calling_convention;
4334 codegen_->GenerateUnresolvedFieldAccess(instruction,
4335 instruction->GetFieldType(),
4336 instruction->GetFieldIndex(),
4337 instruction->GetDexPc(),
4338 calling_convention);
4339}
4340
4341void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4342 HUnresolvedStaticFieldGet* instruction) {
4343 FieldAccessCallingConventionMIPS64 calling_convention;
4344 codegen_->CreateUnresolvedFieldLocationSummary(
4345 instruction, instruction->GetFieldType(), calling_convention);
4346}
4347
4348void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4349 HUnresolvedStaticFieldGet* instruction) {
4350 FieldAccessCallingConventionMIPS64 calling_convention;
4351 codegen_->GenerateUnresolvedFieldAccess(instruction,
4352 instruction->GetFieldType(),
4353 instruction->GetFieldIndex(),
4354 instruction->GetDexPc(),
4355 calling_convention);
4356}
4357
4358void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4359 HUnresolvedStaticFieldSet* instruction) {
4360 FieldAccessCallingConventionMIPS64 calling_convention;
4361 codegen_->CreateUnresolvedFieldLocationSummary(
4362 instruction, instruction->GetFieldType(), calling_convention);
4363}
4364
4365void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4366 HUnresolvedStaticFieldSet* instruction) {
4367 FieldAccessCallingConventionMIPS64 calling_convention;
4368 codegen_->GenerateUnresolvedFieldAccess(instruction,
4369 instruction->GetFieldType(),
4370 instruction->GetFieldIndex(),
4371 instruction->GetDexPc(),
4372 calling_convention);
4373}
4374
Alexey Frunze4dda3372015-06-01 18:31:49 -07004375void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004376 LocationSummary* locations =
4377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004378 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004379}
4380
4381void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4382 HBasicBlock* block = instruction->GetBlock();
4383 if (block->GetLoopInformation() != nullptr) {
4384 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4385 // The back edge will generate the suspend check.
4386 return;
4387 }
4388 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4389 // The goto will generate the suspend check.
4390 return;
4391 }
4392 GenerateSuspendCheck(instruction, nullptr);
4393}
4394
Alexey Frunze4dda3372015-06-01 18:31:49 -07004395void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4396 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004397 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004398 InvokeRuntimeCallingConvention calling_convention;
4399 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4400}
4401
4402void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004403 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004404 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4405}
4406
4407void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4408 Primitive::Type input_type = conversion->GetInputType();
4409 Primitive::Type result_type = conversion->GetResultType();
4410 DCHECK_NE(input_type, result_type);
4411
4412 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4413 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4414 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4415 }
4416
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4418
4419 if (Primitive::IsFloatingPointType(input_type)) {
4420 locations->SetInAt(0, Location::RequiresFpuRegister());
4421 } else {
4422 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004423 }
4424
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004425 if (Primitive::IsFloatingPointType(result_type)) {
4426 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004427 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004428 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004429 }
4430}
4431
4432void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4433 LocationSummary* locations = conversion->GetLocations();
4434 Primitive::Type result_type = conversion->GetResultType();
4435 Primitive::Type input_type = conversion->GetInputType();
4436
4437 DCHECK_NE(input_type, result_type);
4438
4439 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4440 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4441 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4442
4443 switch (result_type) {
4444 case Primitive::kPrimChar:
4445 __ Andi(dst, src, 0xFFFF);
4446 break;
4447 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004448 if (input_type == Primitive::kPrimLong) {
4449 // Type conversion from long to types narrower than int is a result of code
4450 // transformations. To avoid unpredictable results for SEB and SEH, we first
4451 // need to sign-extend the low 32-bit value into bits 32 through 63.
4452 __ Sll(dst, src, 0);
4453 __ Seb(dst, dst);
4454 } else {
4455 __ Seb(dst, src);
4456 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004457 break;
4458 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004459 if (input_type == Primitive::kPrimLong) {
4460 // Type conversion from long to types narrower than int is a result of code
4461 // transformations. To avoid unpredictable results for SEB and SEH, we first
4462 // need to sign-extend the low 32-bit value into bits 32 through 63.
4463 __ Sll(dst, src, 0);
4464 __ Seh(dst, dst);
4465 } else {
4466 __ Seh(dst, src);
4467 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004468 break;
4469 case Primitive::kPrimInt:
4470 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004471 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4472 // conversions, except when the input and output registers are the same and we are not
4473 // converting longs to shorter types. In these cases, do nothing.
4474 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4475 __ Sll(dst, src, 0);
4476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004477 break;
4478
4479 default:
4480 LOG(FATAL) << "Unexpected type conversion from " << input_type
4481 << " to " << result_type;
4482 }
4483 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004484 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4485 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4486 if (input_type == Primitive::kPrimLong) {
4487 __ Dmtc1(src, FTMP);
4488 if (result_type == Primitive::kPrimFloat) {
4489 __ Cvtsl(dst, FTMP);
4490 } else {
4491 __ Cvtdl(dst, FTMP);
4492 }
4493 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004494 __ Mtc1(src, FTMP);
4495 if (result_type == Primitive::kPrimFloat) {
4496 __ Cvtsw(dst, FTMP);
4497 } else {
4498 __ Cvtdw(dst, FTMP);
4499 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004500 }
4501 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4502 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004503 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4504 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4505 Mips64Label truncate;
4506 Mips64Label done;
4507
4508 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4509 // value when the input is either a NaN or is outside of the range of the output type
4510 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4511 // the same result.
4512 //
4513 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4514 // value of the output type if the input is outside of the range after the truncation or
4515 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4516 // results. This matches the desired float/double-to-int/long conversion exactly.
4517 //
4518 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4519 //
4520 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4521 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4522 // even though it must be NAN2008=1 on R6.
4523 //
4524 // The code takes care of the different behaviors by first comparing the input to the
4525 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4526 // If the input is greater than or equal to the minimum, it procedes to the truncate
4527 // instruction, which will handle such an input the same way irrespective of NAN2008.
4528 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4529 // in order to return either zero or the minimum value.
4530 //
4531 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4532 // truncate instruction for MIPS64R6.
4533 if (input_type == Primitive::kPrimFloat) {
4534 uint32_t min_val = (result_type == Primitive::kPrimLong)
4535 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4536 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4537 __ LoadConst32(TMP, min_val);
4538 __ Mtc1(TMP, FTMP);
4539 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004540 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004541 uint64_t min_val = (result_type == Primitive::kPrimLong)
4542 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4543 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4544 __ LoadConst64(TMP, min_val);
4545 __ Dmtc1(TMP, FTMP);
4546 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004547 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004548
4549 __ Bc1nez(FTMP, &truncate);
4550
4551 if (input_type == Primitive::kPrimFloat) {
4552 __ CmpEqS(FTMP, src, src);
4553 } else {
4554 __ CmpEqD(FTMP, src, src);
4555 }
4556 if (result_type == Primitive::kPrimLong) {
4557 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4558 } else {
4559 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4560 }
4561 __ Mfc1(TMP, FTMP);
4562 __ And(dst, dst, TMP);
4563
4564 __ Bc(&done);
4565
4566 __ Bind(&truncate);
4567
4568 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004569 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004570 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004571 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004572 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004573 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004574 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004575 } else {
4576 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004577 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004578 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004579 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004580 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004581 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004582 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004583
4584 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004585 } else if (Primitive::IsFloatingPointType(result_type) &&
4586 Primitive::IsFloatingPointType(input_type)) {
4587 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4588 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4589 if (result_type == Primitive::kPrimFloat) {
4590 __ Cvtsd(dst, src);
4591 } else {
4592 __ Cvtds(dst, src);
4593 }
4594 } else {
4595 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4596 << " to " << result_type;
4597 }
4598}
4599
4600void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4601 HandleShift(ushr);
4602}
4603
4604void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4605 HandleShift(ushr);
4606}
4607
4608void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4609 HandleBinaryOp(instruction);
4610}
4611
4612void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4613 HandleBinaryOp(instruction);
4614}
4615
4616void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4617 // Nothing to do, this should be removed during prepare for register allocator.
4618 LOG(FATAL) << "Unreachable";
4619}
4620
4621void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4622 // Nothing to do, this should be removed during prepare for register allocator.
4623 LOG(FATAL) << "Unreachable";
4624}
4625
4626void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004627 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004628}
4629
4630void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004631 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004632}
4633
4634void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004635 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004636}
4637
4638void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004639 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004640}
4641
4642void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004643 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004644}
4645
4646void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004647 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004648}
4649
4650void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004651 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004652}
4653
4654void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004655 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004656}
4657
4658void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004659 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004660}
4661
4662void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004663 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004664}
4665
4666void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004667 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004668}
4669
4670void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004671 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004672}
4673
Aart Bike9f37602015-10-09 11:15:55 -07004674void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004675 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004676}
4677
4678void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004679 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004680}
4681
4682void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004683 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004684}
4685
4686void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004687 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004688}
4689
4690void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004691 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004692}
4693
4694void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004695 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004696}
4697
4698void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004699 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004700}
4701
4702void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004703 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004704}
4705
Mark Mendellfe57faa2015-09-18 09:26:15 -04004706// Simple implementation of packed switch - generate cascaded compare/jumps.
4707void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4708 LocationSummary* locations =
4709 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4710 locations->SetInAt(0, Location::RequiresRegister());
4711}
4712
Alexey Frunze0960ac52016-12-20 17:24:59 -08004713void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4714 int32_t lower_bound,
4715 uint32_t num_entries,
4716 HBasicBlock* switch_block,
4717 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004718 // Create a set of compare/jumps.
4719 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004720 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004721 // Jump to default if index is negative
4722 // Note: We don't check the case that index is positive while value < lower_bound, because in
4723 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4724 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4725
Alexey Frunze0960ac52016-12-20 17:24:59 -08004726 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004727 // Jump to successors[0] if value == lower_bound.
4728 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4729 int32_t last_index = 0;
4730 for (; num_entries - last_index > 2; last_index += 2) {
4731 __ Addiu(temp_reg, temp_reg, -2);
4732 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4733 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4734 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4735 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4736 }
4737 if (num_entries - last_index == 2) {
4738 // The last missing case_value.
4739 __ Addiu(temp_reg, temp_reg, -1);
4740 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004741 }
4742
4743 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08004744 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004745 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004746 }
4747}
4748
Alexey Frunze0960ac52016-12-20 17:24:59 -08004749void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
4750 int32_t lower_bound,
4751 uint32_t num_entries,
4752 HBasicBlock* switch_block,
4753 HBasicBlock* default_block) {
4754 // Create a jump table.
4755 std::vector<Mips64Label*> labels(num_entries);
4756 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
4757 for (uint32_t i = 0; i < num_entries; i++) {
4758 labels[i] = codegen_->GetLabelOf(successors[i]);
4759 }
4760 JumpTable* table = __ CreateJumpTable(std::move(labels));
4761
4762 // Is the value in range?
4763 __ Addiu32(TMP, value_reg, -lower_bound);
4764 __ LoadConst32(AT, num_entries);
4765 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
4766
4767 // We are in the range of the table.
4768 // Load the target address from the jump table, indexing by the value.
4769 __ LoadLabelAddress(AT, table->GetLabel());
4770 __ Sll(TMP, TMP, 2);
4771 __ Daddu(TMP, TMP, AT);
4772 __ Lw(TMP, TMP, 0);
4773 // Compute the absolute target address by adding the table start address
4774 // (the table contains offsets to targets relative to its start).
4775 __ Daddu(TMP, TMP, AT);
4776 // And jump.
4777 __ Jr(TMP);
4778 __ Nop();
4779}
4780
4781void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4782 int32_t lower_bound = switch_instr->GetStartValue();
4783 uint32_t num_entries = switch_instr->GetNumEntries();
4784 LocationSummary* locations = switch_instr->GetLocations();
4785 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4786 HBasicBlock* switch_block = switch_instr->GetBlock();
4787 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4788
4789 if (num_entries > kPackedSwitchJumpTableThreshold) {
4790 GenTableBasedPackedSwitch(value_reg,
4791 lower_bound,
4792 num_entries,
4793 switch_block,
4794 default_block);
4795 } else {
4796 GenPackedSwitchWithCompares(value_reg,
4797 lower_bound,
4798 num_entries,
4799 switch_block,
4800 default_block);
4801 }
4802}
4803
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004804void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4805 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4806}
4807
4808void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4809 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4810}
4811
Alexey Frunze4dda3372015-06-01 18:31:49 -07004812} // namespace mips64
4813} // namespace art