blob: 4814b224ad749b0b626b67a9f45331bb6bfcb77c [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:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800339 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction, bool is_fatal)
340 : SlowPathCodeMIPS64(instruction), is_fatal_(is_fatal) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800344
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100345 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346 DCHECK(instruction_->IsCheckCast()
347 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
348 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
349
350 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800351 if (!is_fatal_) {
352 SaveLiveRegisters(codegen, locations);
353 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354
355 // We're moving two locations to locations that could overlap, so we need a parallel
356 // move resolver.
357 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800358 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
360 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
363 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100365 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800366 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 Primitive::Type ret_type = instruction_->GetType();
368 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
369 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 } else {
371 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800372 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
373 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700374 }
375
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800376 if (!is_fatal_) {
377 RestoreLiveRegisters(codegen, locations);
378 __ Bc(GetExitLabel());
379 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 }
381
Roland Levillain46648892015-06-19 16:07:18 +0100382 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
383
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800384 bool IsFatal() const OVERRIDE { return is_fatal_; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 const bool is_fatal_;
388
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
390};
391
392class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700396
397 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800398 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100400 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000401 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 }
403
Roland Levillain46648892015-06-19 16:07:18 +0100404 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
405
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
408};
409
410CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
411 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100412 const CompilerOptions& compiler_options,
413 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 : CodeGenerator(graph,
415 kNumberOfGpuRegisters,
416 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000417 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700418 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
419 arraysize(kCoreCalleeSaves)),
420 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
421 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100422 compiler_options,
423 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100424 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700425 location_builder_(graph, this),
426 instruction_visitor_(graph, this),
427 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100428 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800429 isa_features_(isa_features),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800430 uint32_literals_(std::less<uint32_t>(),
431 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800432 uint64_literals_(std::less<uint64_t>(),
433 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800434 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzef63f5692016-12-13 17:43:11 -0800435 boot_image_string_patches_(StringReferenceValueComparator(),
436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
437 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
438 boot_image_type_patches_(TypeReferenceValueComparator(),
439 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
440 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +0000441 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -0800442 jit_string_patches_(StringReferenceValueComparator(),
443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
444 jit_class_patches_(TypeReferenceValueComparator(),
445 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700446 // Save RA (containing the return address) to mimic Quick.
447 AddAllocatedRegister(Location::RegisterLocation(RA));
448}
449
450#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100451// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
452#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700453#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700454
455void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700456 // Ensure that we fix up branches.
457 __ FinalizeCode();
458
459 // Adjust native pc offsets in stack maps.
460 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800461 uint32_t old_position =
462 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips64);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700463 uint32_t new_position = __ GetAdjustedPosition(old_position);
464 DCHECK_GE(new_position, old_position);
465 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
466 }
467
468 // Adjust pc offsets for the disassembly information.
469 if (disasm_info_ != nullptr) {
470 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
471 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
472 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
473 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
474 it.second.start = __ GetAdjustedPosition(it.second.start);
475 it.second.end = __ GetAdjustedPosition(it.second.end);
476 }
477 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
478 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
479 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
480 }
481 }
482
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 CodeGenerator::Finalize(allocator);
484}
485
486Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
487 return codegen_->GetAssembler();
488}
489
490void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100491 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700492 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
493}
494
495void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100496 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700497 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
498}
499
500void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
501 // Pop reg
502 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200503 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700504}
505
506void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
507 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200508 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700509 __ Sd(GpuRegister(reg), SP, 0);
510}
511
512void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
513 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
514 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
515 // Allocate a scratch register other than TMP, if available.
516 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
517 // automatically unspilled when the scratch scope object is destroyed).
518 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
519 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200520 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700521 __ LoadFromOffset(load_type,
522 GpuRegister(ensure_scratch.GetRegister()),
523 SP,
524 index1 + stack_offset);
525 __ LoadFromOffset(load_type,
526 TMP,
527 SP,
528 index2 + stack_offset);
529 __ StoreToOffset(store_type,
530 GpuRegister(ensure_scratch.GetRegister()),
531 SP,
532 index2 + stack_offset);
533 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
534}
535
536static dwarf::Reg DWARFReg(GpuRegister reg) {
537 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
538}
539
David Srbeckyba702002016-02-01 18:15:29 +0000540static dwarf::Reg DWARFReg(FpuRegister reg) {
541 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
542}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700543
544void CodeGeneratorMIPS64::GenerateFrameEntry() {
545 __ Bind(&frame_entry_label_);
546
547 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
548
549 if (do_overflow_check) {
550 __ LoadFromOffset(kLoadWord,
551 ZERO,
552 SP,
553 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
554 RecordPcInfo(nullptr, 0);
555 }
556
Alexey Frunze4dda3372015-06-01 18:31:49 -0700557 if (HasEmptyFrame()) {
558 return;
559 }
560
561 // Make sure the frame size isn't unreasonably large. Per the various APIs
562 // it looks like it should always be less than 2GB in size, which allows
563 // us using 32-bit signed offsets from the stack pointer.
564 if (GetFrameSize() > 0x7FFFFFFF)
565 LOG(FATAL) << "Stack frame larger than 2GB";
566
567 // Spill callee-saved registers.
568 // Note that their cumulative size is small and they can be indexed using
569 // 16-bit offsets.
570
571 // TODO: increment/decrement SP in one step instead of two or remove this comment.
572
573 uint32_t ofs = FrameEntrySpillSize();
574 __ IncreaseFrameSize(ofs);
575
576 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
577 GpuRegister reg = kCoreCalleeSaves[i];
578 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200579 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700580 __ Sd(reg, SP, ofs);
581 __ cfi().RelOffset(DWARFReg(reg), ofs);
582 }
583 }
584
585 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
586 FpuRegister reg = kFpuCalleeSaves[i];
587 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200588 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700589 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000590 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700591 }
592 }
593
594 // Allocate the rest of the frame and store the current method pointer
595 // at its end.
596
597 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
598
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100599 // Save the current method if we need it. Note that we do not
600 // do this in HCurrentMethod, as the instruction might have been removed
601 // in the SSA graph.
602 if (RequiresCurrentMethod()) {
603 static_assert(IsInt<16>(kCurrentMethodStackOffset),
604 "kCurrentMethodStackOffset must fit into int16_t");
605 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
606 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100607
608 if (GetGraph()->HasShouldDeoptimizeFlag()) {
609 // Initialize should_deoptimize flag to 0.
610 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
611 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700612}
613
614void CodeGeneratorMIPS64::GenerateFrameExit() {
615 __ cfi().RememberState();
616
Alexey Frunze4dda3372015-06-01 18:31:49 -0700617 if (!HasEmptyFrame()) {
618 // Deallocate the rest of the frame.
619
620 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
621
622 // Restore callee-saved registers.
623 // Note that their cumulative size is small and they can be indexed using
624 // 16-bit offsets.
625
626 // TODO: increment/decrement SP in one step instead of two or remove this comment.
627
628 uint32_t ofs = 0;
629
630 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
631 FpuRegister reg = kFpuCalleeSaves[i];
632 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
633 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200634 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000635 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700636 }
637 }
638
639 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
640 GpuRegister reg = kCoreCalleeSaves[i];
641 if (allocated_registers_.ContainsCoreRegister(reg)) {
642 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200643 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 __ cfi().Restore(DWARFReg(reg));
645 }
646 }
647
648 DCHECK_EQ(ofs, FrameEntrySpillSize());
649 __ DecreaseFrameSize(ofs);
650 }
651
652 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700653 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700654
655 __ cfi().RestoreState();
656 __ cfi().DefCFAOffset(GetFrameSize());
657}
658
659void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
660 __ Bind(GetLabelOf(block));
661}
662
663void CodeGeneratorMIPS64::MoveLocation(Location destination,
664 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100665 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700666 if (source.Equals(destination)) {
667 return;
668 }
669
670 // A valid move can always be inferred from the destination and source
671 // locations. When moving from and to a register, the argument type can be
672 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700674 DCHECK_EQ(unspecified_type, false);
675
676 if (destination.IsRegister() || destination.IsFpuRegister()) {
677 if (unspecified_type) {
678 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
679 if (source.IsStackSlot() ||
680 (src_cst != nullptr && (src_cst->IsIntConstant()
681 || src_cst->IsFloatConstant()
682 || src_cst->IsNullConstant()))) {
683 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100684 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700685 } else {
686 // If the source is a double stack slot or a 64bit constant, a 64bit
687 // type is appropriate. Else the source is a register, and since the
688 // type has not been specified, we chose a 64bit type to force a 64bit
689 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100690 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700691 }
692 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
694 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700695 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
696 // Move to GPR/FPR from stack
697 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100698 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700699 __ LoadFpuFromOffset(load_type,
700 destination.AsFpuRegister<FpuRegister>(),
701 SP,
702 source.GetStackIndex());
703 } else {
704 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
705 __ LoadFromOffset(load_type,
706 destination.AsRegister<GpuRegister>(),
707 SP,
708 source.GetStackIndex());
709 }
710 } else if (source.IsConstant()) {
711 // Move to GPR/FPR from constant
712 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 gpr = destination.AsRegister<GpuRegister>();
715 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100716 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700717 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
718 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
719 gpr = ZERO;
720 } else {
721 __ LoadConst32(gpr, value);
722 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700723 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700724 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
725 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
726 gpr = ZERO;
727 } else {
728 __ LoadConst64(gpr, value);
729 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100733 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
735 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100736 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700737 if (destination.IsRegister()) {
738 // Move to GPR from GPR
739 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
740 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100741 DCHECK(destination.IsFpuRegister());
742 if (Primitive::Is64BitType(dst_type)) {
743 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
744 } else {
745 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
746 }
747 }
748 } else if (source.IsFpuRegister()) {
749 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700750 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100751 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
753 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100754 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700755 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
756 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100757 } else {
758 DCHECK(destination.IsRegister());
759 if (Primitive::Is64BitType(dst_type)) {
760 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
761 } else {
762 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
763 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700764 }
765 }
766 } else { // The destination is not a register. It must be a stack slot.
767 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
768 if (source.IsRegister() || source.IsFpuRegister()) {
769 if (unspecified_type) {
770 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100771 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700772 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100773 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700774 }
775 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100776 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
777 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700778 // Move to stack from GPR/FPR
779 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
780 if (source.IsRegister()) {
781 __ StoreToOffset(store_type,
782 source.AsRegister<GpuRegister>(),
783 SP,
784 destination.GetStackIndex());
785 } else {
786 __ StoreFpuToOffset(store_type,
787 source.AsFpuRegister<FpuRegister>(),
788 SP,
789 destination.GetStackIndex());
790 }
791 } else if (source.IsConstant()) {
792 // Move to stack from constant
793 HConstant* src_cst = source.GetConstant();
794 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700795 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700796 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700797 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
798 if (value != 0) {
799 gpr = TMP;
800 __ LoadConst32(gpr, value);
801 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700802 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700803 DCHECK(destination.IsDoubleStackSlot());
804 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
805 if (value != 0) {
806 gpr = TMP;
807 __ LoadConst64(gpr, value);
808 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700809 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700810 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700811 } else {
812 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
813 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
814 // Move to stack from stack
815 if (destination.IsStackSlot()) {
816 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
817 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
818 } else {
819 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
820 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
821 }
822 }
823 }
824}
825
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700826void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700827 DCHECK(!loc1.IsConstant());
828 DCHECK(!loc2.IsConstant());
829
830 if (loc1.Equals(loc2)) {
831 return;
832 }
833
834 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
835 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
836 bool is_fp_reg1 = loc1.IsFpuRegister();
837 bool is_fp_reg2 = loc2.IsFpuRegister();
838
839 if (loc2.IsRegister() && loc1.IsRegister()) {
840 // Swap 2 GPRs
841 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
842 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
843 __ Move(TMP, r2);
844 __ Move(r2, r1);
845 __ Move(r1, TMP);
846 } else if (is_fp_reg2 && is_fp_reg1) {
847 // Swap 2 FPRs
848 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
849 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700850 if (type == Primitive::kPrimFloat) {
851 __ MovS(FTMP, r1);
852 __ MovS(r1, r2);
853 __ MovS(r2, FTMP);
854 } else {
855 DCHECK_EQ(type, Primitive::kPrimDouble);
856 __ MovD(FTMP, r1);
857 __ MovD(r1, r2);
858 __ MovD(r2, FTMP);
859 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700860 } else if (is_slot1 != is_slot2) {
861 // Swap GPR/FPR and stack slot
862 Location reg_loc = is_slot1 ? loc2 : loc1;
863 Location mem_loc = is_slot1 ? loc1 : loc2;
864 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
865 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
866 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
867 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
868 if (reg_loc.IsFpuRegister()) {
869 __ StoreFpuToOffset(store_type,
870 reg_loc.AsFpuRegister<FpuRegister>(),
871 SP,
872 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700873 if (mem_loc.IsStackSlot()) {
874 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
875 } else {
876 DCHECK(mem_loc.IsDoubleStackSlot());
877 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
878 }
879 } else {
880 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
881 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
882 }
883 } else if (is_slot1 && is_slot2) {
884 move_resolver_.Exchange(loc1.GetStackIndex(),
885 loc2.GetStackIndex(),
886 loc1.IsDoubleStackSlot());
887 } else {
888 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
889 }
890}
891
Calin Juravle175dc732015-08-25 15:42:32 +0100892void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
893 DCHECK(location.IsRegister());
894 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
895}
896
Calin Juravlee460d1d2015-09-29 04:52:17 +0100897void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
898 if (location.IsRegister()) {
899 locations->AddTemp(location);
900 } else {
901 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
902 }
903}
904
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100905void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
906 GpuRegister value,
907 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700908 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700909 GpuRegister card = AT;
910 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100911 if (value_can_be_null) {
912 __ Beqzc(value, &done);
913 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700914 __ LoadFromOffset(kLoadDoubleword,
915 card,
916 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700917 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700918 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
919 __ Daddu(temp, card, temp);
920 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100921 if (value_can_be_null) {
922 __ Bind(&done);
923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700924}
925
Alexey Frunze19f6c692016-11-30 19:19:55 -0800926template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
927inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
928 const ArenaDeque<PcRelativePatchInfo>& infos,
929 ArenaVector<LinkerPatch>* linker_patches) {
930 for (const PcRelativePatchInfo& info : infos) {
931 const DexFile& dex_file = info.target_dex_file;
932 size_t offset_or_index = info.offset_or_index;
933 DCHECK(info.pc_rel_label.IsBound());
934 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
935 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
936 }
937}
938
939void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
940 DCHECK(linker_patches->empty());
941 size_t size =
Alexey Frunze19f6c692016-11-30 19:19:55 -0800942 pc_relative_dex_cache_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800943 pc_relative_string_patches_.size() +
944 pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +0000945 type_bss_entry_patches_.size() +
Alexey Frunzef63f5692016-12-13 17:43:11 -0800946 boot_image_string_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +0000947 boot_image_type_patches_.size();
Alexey Frunze19f6c692016-11-30 19:19:55 -0800948 linker_patches->reserve(size);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800949 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
950 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800951 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +0000952 DCHECK(pc_relative_type_patches_.empty());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800953 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
954 linker_patches);
955 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000956 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
957 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800958 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
959 linker_patches);
960 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000961 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
962 linker_patches);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800963 for (const auto& entry : boot_image_string_patches_) {
964 const StringReference& target_string = entry.first;
965 Literal* literal = entry.second;
966 DCHECK(literal->GetLabel()->IsBound());
967 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
968 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
969 target_string.dex_file,
970 target_string.string_index.index_));
971 }
972 for (const auto& entry : boot_image_type_patches_) {
973 const TypeReference& target_type = entry.first;
974 Literal* literal = entry.second;
975 DCHECK(literal->GetLabel()->IsBound());
976 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
977 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
978 target_type.dex_file,
979 target_type.type_index.index_));
980 }
Vladimir Marko1998cd02017-01-13 13:02:58 +0000981 DCHECK_EQ(size, linker_patches->size());
Alexey Frunzef63f5692016-12-13 17:43:11 -0800982}
983
984CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000985 const DexFile& dex_file, dex::StringIndex string_index) {
986 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Alexey Frunzef63f5692016-12-13 17:43:11 -0800987}
988
989CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeTypePatch(
990 const DexFile& dex_file, dex::TypeIndex type_index) {
991 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunze19f6c692016-11-30 19:19:55 -0800992}
993
Vladimir Marko1998cd02017-01-13 13:02:58 +0000994CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewTypeBssEntryPatch(
995 const DexFile& dex_file, dex::TypeIndex type_index) {
996 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
997}
998
Alexey Frunze19f6c692016-11-30 19:19:55 -0800999CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
1000 const DexFile& dex_file, uint32_t element_offset) {
1001 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1002}
1003
Alexey Frunze19f6c692016-11-30 19:19:55 -08001004CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
1005 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1006 patches->emplace_back(dex_file, offset_or_index);
1007 return &patches->back();
1008}
1009
Alexey Frunzef63f5692016-12-13 17:43:11 -08001010Literal* CodeGeneratorMIPS64::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1011 return map->GetOrCreate(
1012 value,
1013 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1014}
1015
Alexey Frunze19f6c692016-11-30 19:19:55 -08001016Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
1017 return uint64_literals_.GetOrCreate(
1018 value,
1019 [this, value]() { return __ NewLiteral<uint64_t>(value); });
1020}
1021
1022Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
1023 MethodToLiteralMap* map) {
1024 return map->GetOrCreate(
1025 target_method,
1026 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1027}
1028
Alexey Frunzef63f5692016-12-13 17:43:11 -08001029Literal* CodeGeneratorMIPS64::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1030 dex::StringIndex string_index) {
1031 return boot_image_string_patches_.GetOrCreate(
1032 StringReference(&dex_file, string_index),
1033 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1034}
1035
1036Literal* CodeGeneratorMIPS64::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1037 dex::TypeIndex type_index) {
1038 return boot_image_type_patches_.GetOrCreate(
1039 TypeReference(&dex_file, type_index),
1040 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1041}
1042
1043Literal* CodeGeneratorMIPS64::DeduplicateBootImageAddressLiteral(uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001044 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunzef63f5692016-12-13 17:43:11 -08001045}
1046
Alexey Frunze19f6c692016-11-30 19:19:55 -08001047void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1048 GpuRegister out) {
1049 __ Bind(&info->pc_rel_label);
1050 // Add the high half of a 32-bit offset to PC.
1051 __ Auipc(out, /* placeholder */ 0x1234);
1052 // The immediately following instruction will add the sign-extended low half of the 32-bit
Alexey Frunzef63f5692016-12-13 17:43:11 -08001053 // offset to `out` (e.g. ld, jialc, daddiu).
Alexey Frunze19f6c692016-11-30 19:19:55 -08001054}
1055
Alexey Frunze627c1a02017-01-30 19:28:14 -08001056Literal* CodeGeneratorMIPS64::DeduplicateJitStringLiteral(const DexFile& dex_file,
1057 dex::StringIndex string_index,
1058 Handle<mirror::String> handle) {
1059 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
1060 reinterpret_cast64<uint64_t>(handle.GetReference()));
1061 return jit_string_patches_.GetOrCreate(
1062 StringReference(&dex_file, string_index),
1063 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1064}
1065
1066Literal* CodeGeneratorMIPS64::DeduplicateJitClassLiteral(const DexFile& dex_file,
1067 dex::TypeIndex type_index,
1068 Handle<mirror::Class> handle) {
1069 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
1070 reinterpret_cast64<uint64_t>(handle.GetReference()));
1071 return jit_class_patches_.GetOrCreate(
1072 TypeReference(&dex_file, type_index),
1073 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1074}
1075
1076void CodeGeneratorMIPS64::PatchJitRootUse(uint8_t* code,
1077 const uint8_t* roots_data,
1078 const Literal* literal,
1079 uint64_t index_in_table) const {
1080 uint32_t literal_offset = GetAssembler().GetLabelLocation(literal->GetLabel());
1081 uintptr_t address =
1082 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1083 reinterpret_cast<uint32_t*>(code + literal_offset)[0] = dchecked_integral_cast<uint32_t>(address);
1084}
1085
1086void CodeGeneratorMIPS64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1087 for (const auto& entry : jit_string_patches_) {
1088 const auto& it = jit_string_roots_.find(entry.first);
1089 DCHECK(it != jit_string_roots_.end());
1090 PatchJitRootUse(code, roots_data, entry.second, it->second);
1091 }
1092 for (const auto& entry : jit_class_patches_) {
1093 const auto& it = jit_class_roots_.find(entry.first);
1094 DCHECK(it != jit_class_roots_.end());
1095 PatchJitRootUse(code, roots_data, entry.second, it->second);
1096 }
1097}
1098
David Brazdil58282f42016-01-14 12:45:10 +00001099void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001100 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1101 blocked_core_registers_[ZERO] = true;
1102 blocked_core_registers_[K0] = true;
1103 blocked_core_registers_[K1] = true;
1104 blocked_core_registers_[GP] = true;
1105 blocked_core_registers_[SP] = true;
1106 blocked_core_registers_[RA] = true;
1107
Lazar Trsicd9672662015-09-03 17:33:01 +02001108 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
1109 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -07001110 blocked_core_registers_[AT] = true;
1111 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +02001112 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001113 blocked_fpu_registers_[FTMP] = true;
1114
1115 // Reserve suspend and thread registers.
1116 blocked_core_registers_[S0] = true;
1117 blocked_core_registers_[TR] = true;
1118
1119 // Reserve T9 for function calls
1120 blocked_core_registers_[T9] = true;
1121
Goran Jakovljevic782be112016-06-21 12:39:04 +02001122 if (GetGraph()->IsDebuggable()) {
1123 // Stubs do not save callee-save floating point registers. If the graph
1124 // is debuggable, we need to deal with these registers differently. For
1125 // now, just block them.
1126 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1127 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1128 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001129 }
1130}
1131
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1133 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001134 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001135}
1136
1137size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1138 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001139 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001140}
1141
1142size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1143 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001144 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001145}
1146
1147size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1148 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001149 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001150}
1151
1152void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001153 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001154}
1155
1156void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001157 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001158}
1159
Calin Juravle175dc732015-08-25 15:42:32 +01001160void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001161 HInstruction* instruction,
1162 uint32_t dex_pc,
1163 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001164 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001165 __ LoadFromOffset(kLoadDoubleword,
1166 T9,
1167 TR,
1168 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001169 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001170 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001171 if (EntrypointRequiresStackMap(entrypoint)) {
1172 RecordPcInfo(instruction, dex_pc, slow_path);
1173 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001174}
1175
1176void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1177 GpuRegister class_reg) {
1178 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1179 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1180 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1181 // TODO: barrier needed?
1182 __ Bind(slow_path->GetExitLabel());
1183}
1184
1185void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1186 __ Sync(0); // only stype 0 is supported
1187}
1188
1189void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1190 HBasicBlock* successor) {
1191 SuspendCheckSlowPathMIPS64* slow_path =
1192 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1193 codegen_->AddSlowPath(slow_path);
1194
1195 __ LoadFromOffset(kLoadUnsignedHalfword,
1196 TMP,
1197 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001198 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 if (successor == nullptr) {
1200 __ Bnezc(TMP, slow_path->GetEntryLabel());
1201 __ Bind(slow_path->GetReturnLabel());
1202 } else {
1203 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001204 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001205 // slow_path will return to GetLabelOf(successor).
1206 }
1207}
1208
1209InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1210 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001211 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001212 assembler_(codegen->GetAssembler()),
1213 codegen_(codegen) {}
1214
1215void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1216 DCHECK_EQ(instruction->InputCount(), 2U);
1217 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1218 Primitive::Type type = instruction->GetResultType();
1219 switch (type) {
1220 case Primitive::kPrimInt:
1221 case Primitive::kPrimLong: {
1222 locations->SetInAt(0, Location::RequiresRegister());
1223 HInstruction* right = instruction->InputAt(1);
1224 bool can_use_imm = false;
1225 if (right->IsConstant()) {
1226 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1227 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1228 can_use_imm = IsUint<16>(imm);
1229 } else if (instruction->IsAdd()) {
1230 can_use_imm = IsInt<16>(imm);
1231 } else {
1232 DCHECK(instruction->IsSub());
1233 can_use_imm = IsInt<16>(-imm);
1234 }
1235 }
1236 if (can_use_imm)
1237 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1238 else
1239 locations->SetInAt(1, Location::RequiresRegister());
1240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1241 }
1242 break;
1243
1244 case Primitive::kPrimFloat:
1245 case Primitive::kPrimDouble:
1246 locations->SetInAt(0, Location::RequiresFpuRegister());
1247 locations->SetInAt(1, Location::RequiresFpuRegister());
1248 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1249 break;
1250
1251 default:
1252 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1253 }
1254}
1255
1256void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1257 Primitive::Type type = instruction->GetType();
1258 LocationSummary* locations = instruction->GetLocations();
1259
1260 switch (type) {
1261 case Primitive::kPrimInt:
1262 case Primitive::kPrimLong: {
1263 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1264 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1265 Location rhs_location = locations->InAt(1);
1266
1267 GpuRegister rhs_reg = ZERO;
1268 int64_t rhs_imm = 0;
1269 bool use_imm = rhs_location.IsConstant();
1270 if (use_imm) {
1271 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1272 } else {
1273 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1274 }
1275
1276 if (instruction->IsAnd()) {
1277 if (use_imm)
1278 __ Andi(dst, lhs, rhs_imm);
1279 else
1280 __ And(dst, lhs, rhs_reg);
1281 } else if (instruction->IsOr()) {
1282 if (use_imm)
1283 __ Ori(dst, lhs, rhs_imm);
1284 else
1285 __ Or(dst, lhs, rhs_reg);
1286 } else if (instruction->IsXor()) {
1287 if (use_imm)
1288 __ Xori(dst, lhs, rhs_imm);
1289 else
1290 __ Xor(dst, lhs, rhs_reg);
1291 } else if (instruction->IsAdd()) {
1292 if (type == Primitive::kPrimInt) {
1293 if (use_imm)
1294 __ Addiu(dst, lhs, rhs_imm);
1295 else
1296 __ Addu(dst, lhs, rhs_reg);
1297 } else {
1298 if (use_imm)
1299 __ Daddiu(dst, lhs, rhs_imm);
1300 else
1301 __ Daddu(dst, lhs, rhs_reg);
1302 }
1303 } else {
1304 DCHECK(instruction->IsSub());
1305 if (type == Primitive::kPrimInt) {
1306 if (use_imm)
1307 __ Addiu(dst, lhs, -rhs_imm);
1308 else
1309 __ Subu(dst, lhs, rhs_reg);
1310 } else {
1311 if (use_imm)
1312 __ Daddiu(dst, lhs, -rhs_imm);
1313 else
1314 __ Dsubu(dst, lhs, rhs_reg);
1315 }
1316 }
1317 break;
1318 }
1319 case Primitive::kPrimFloat:
1320 case Primitive::kPrimDouble: {
1321 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1322 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1323 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1324 if (instruction->IsAdd()) {
1325 if (type == Primitive::kPrimFloat)
1326 __ AddS(dst, lhs, rhs);
1327 else
1328 __ AddD(dst, lhs, rhs);
1329 } else if (instruction->IsSub()) {
1330 if (type == Primitive::kPrimFloat)
1331 __ SubS(dst, lhs, rhs);
1332 else
1333 __ SubD(dst, lhs, rhs);
1334 } else {
1335 LOG(FATAL) << "Unexpected floating-point binary operation";
1336 }
1337 break;
1338 }
1339 default:
1340 LOG(FATAL) << "Unexpected binary operation type " << type;
1341 }
1342}
1343
1344void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001345 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001346
1347 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1348 Primitive::Type type = instr->GetResultType();
1349 switch (type) {
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimLong: {
1352 locations->SetInAt(0, Location::RequiresRegister());
1353 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 break;
1356 }
1357 default:
1358 LOG(FATAL) << "Unexpected shift type " << type;
1359 }
1360}
1361
1362void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001363 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 LocationSummary* locations = instr->GetLocations();
1365 Primitive::Type type = instr->GetType();
1366
1367 switch (type) {
1368 case Primitive::kPrimInt:
1369 case Primitive::kPrimLong: {
1370 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1371 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1372 Location rhs_location = locations->InAt(1);
1373
1374 GpuRegister rhs_reg = ZERO;
1375 int64_t rhs_imm = 0;
1376 bool use_imm = rhs_location.IsConstant();
1377 if (use_imm) {
1378 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1379 } else {
1380 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1381 }
1382
1383 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001384 uint32_t shift_value = rhs_imm &
1385 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001386
Alexey Frunze92d90602015-12-18 18:16:36 -08001387 if (shift_value == 0) {
1388 if (dst != lhs) {
1389 __ Move(dst, lhs);
1390 }
1391 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001392 if (instr->IsShl()) {
1393 __ Sll(dst, lhs, shift_value);
1394 } else if (instr->IsShr()) {
1395 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001396 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001397 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001398 } else {
1399 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001400 }
1401 } else {
1402 if (shift_value < 32) {
1403 if (instr->IsShl()) {
1404 __ Dsll(dst, lhs, shift_value);
1405 } else if (instr->IsShr()) {
1406 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001407 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001408 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001409 } else {
1410 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001411 }
1412 } else {
1413 shift_value -= 32;
1414 if (instr->IsShl()) {
1415 __ Dsll32(dst, lhs, shift_value);
1416 } else if (instr->IsShr()) {
1417 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001418 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001420 } else {
1421 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001422 }
1423 }
1424 }
1425 } else {
1426 if (type == Primitive::kPrimInt) {
1427 if (instr->IsShl()) {
1428 __ Sllv(dst, lhs, rhs_reg);
1429 } else if (instr->IsShr()) {
1430 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001431 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001432 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001433 } else {
1434 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001435 }
1436 } else {
1437 if (instr->IsShl()) {
1438 __ Dsllv(dst, lhs, rhs_reg);
1439 } else if (instr->IsShr()) {
1440 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001441 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001442 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001443 } else {
1444 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001445 }
1446 }
1447 }
1448 break;
1449 }
1450 default:
1451 LOG(FATAL) << "Unexpected shift operation type " << type;
1452 }
1453}
1454
1455void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1456 HandleBinaryOp(instruction);
1457}
1458
1459void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1460 HandleBinaryOp(instruction);
1461}
1462
1463void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1464 HandleBinaryOp(instruction);
1465}
1466
1467void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1468 HandleBinaryOp(instruction);
1469}
1470
1471void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1474 locations->SetInAt(0, Location::RequiresRegister());
1475 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1476 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1477 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1478 } else {
1479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1480 }
1481}
1482
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001483static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS64* codegen) {
1484 auto null_checker = [codegen, instruction]() {
1485 codegen->MaybeRecordImplicitNullCheck(instruction);
1486 };
1487 return null_checker;
1488}
1489
Alexey Frunze4dda3372015-06-01 18:31:49 -07001490void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1491 LocationSummary* locations = instruction->GetLocations();
1492 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1493 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001494 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001495 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001496
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001497 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001498 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
1499 instruction->IsStringCharAt();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001500 switch (type) {
1501 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001502 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1503 if (index.IsConstant()) {
1504 size_t offset =
1505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001506 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001507 } else {
1508 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001509 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001510 }
1511 break;
1512 }
1513
1514 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001515 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1516 if (index.IsConstant()) {
1517 size_t offset =
1518 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001519 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001520 } else {
1521 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001522 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001523 }
1524 break;
1525 }
1526
1527 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001528 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1529 if (index.IsConstant()) {
1530 size_t offset =
1531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001532 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001533 } else {
1534 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1535 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001536 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001537 }
1538 break;
1539 }
1540
1541 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001542 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001543 if (maybe_compressed_char_at) {
1544 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001545 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001546 __ Dext(TMP, TMP, 0, 1);
1547 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1548 "Expecting 0=compressed, 1=uncompressed");
1549 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001550 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001551 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
1552 if (maybe_compressed_char_at) {
1553 Mips64Label uncompressed_load, done;
1554 __ Bnezc(TMP, &uncompressed_load);
1555 __ LoadFromOffset(kLoadUnsignedByte,
1556 out,
1557 obj,
1558 data_offset + (const_index << TIMES_1));
1559 __ Bc(&done);
1560 __ Bind(&uncompressed_load);
1561 __ LoadFromOffset(kLoadUnsignedHalfword,
1562 out,
1563 obj,
1564 data_offset + (const_index << TIMES_2));
1565 __ Bind(&done);
1566 } else {
1567 __ LoadFromOffset(kLoadUnsignedHalfword,
1568 out,
1569 obj,
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001570 data_offset + (const_index << TIMES_2),
1571 null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001572 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001573 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001574 GpuRegister index_reg = index.AsRegister<GpuRegister>();
1575 if (maybe_compressed_char_at) {
1576 Mips64Label uncompressed_load, done;
1577 __ Bnezc(TMP, &uncompressed_load);
1578 __ Daddu(TMP, obj, index_reg);
1579 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1580 __ Bc(&done);
1581 __ Bind(&uncompressed_load);
1582 __ Dsll(TMP, index_reg, TIMES_2);
1583 __ Daddu(TMP, obj, TMP);
1584 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1585 __ Bind(&done);
1586 } else {
1587 __ Dsll(TMP, index_reg, TIMES_2);
1588 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001589 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001590 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001591 }
1592 break;
1593 }
1594
1595 case Primitive::kPrimInt:
1596 case Primitive::kPrimNot: {
1597 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001598 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1599 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1600 if (index.IsConstant()) {
1601 size_t offset =
1602 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001603 __ LoadFromOffset(load_type, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001604 } else {
1605 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1606 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001607 __ LoadFromOffset(load_type, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001608 }
1609 break;
1610 }
1611
1612 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001613 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1614 if (index.IsConstant()) {
1615 size_t offset =
1616 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001617 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001618 } else {
1619 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1620 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001621 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001622 }
1623 break;
1624 }
1625
1626 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001627 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1628 if (index.IsConstant()) {
1629 size_t offset =
1630 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001631 __ LoadFpuFromOffset(kLoadWord, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001632 } else {
1633 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1634 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001635 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001636 }
1637 break;
1638 }
1639
1640 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1642 if (index.IsConstant()) {
1643 size_t offset =
1644 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001645 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646 } else {
1647 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1648 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001649 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001650 }
1651 break;
1652 }
1653
1654 case Primitive::kPrimVoid:
1655 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1656 UNREACHABLE();
1657 }
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001658
Alexey Frunzec061de12017-02-14 13:27:23 -08001659 if (type == Primitive::kPrimNot) {
1660 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1661 __ MaybeUnpoisonHeapReference(out);
1662 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001663}
1664
1665void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1667 locations->SetInAt(0, Location::RequiresRegister());
1668 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1669}
1670
1671void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1672 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001673 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1675 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1676 __ LoadFromOffset(kLoadWord, out, obj, offset);
1677 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01001678 // Mask out compression flag from String's array length.
1679 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
1680 __ Srl(out, out, 1u);
1681 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001682}
1683
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001684Location LocationsBuilderMIPS64::RegisterOrZeroConstant(HInstruction* instruction) {
1685 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1686 ? Location::ConstantLocation(instruction->AsConstant())
1687 : Location::RequiresRegister();
1688}
1689
1690Location LocationsBuilderMIPS64::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1691 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1692 // We can store a non-zero float or double constant without first loading it into the FPU,
1693 // but we should only prefer this if the constant has a single use.
1694 if (instruction->IsConstant() &&
1695 (instruction->AsConstant()->IsZeroBitPattern() ||
1696 instruction->GetUses().HasExactlyOneElement())) {
1697 return Location::ConstantLocation(instruction->AsConstant());
1698 // Otherwise fall through and require an FPU register for the constant.
1699 }
1700 return Location::RequiresFpuRegister();
1701}
1702
Alexey Frunze4dda3372015-06-01 18:31:49 -07001703void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001704 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1706 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001707 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001708 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709 InvokeRuntimeCallingConvention calling_convention;
1710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1711 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1712 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1713 } else {
1714 locations->SetInAt(0, Location::RequiresRegister());
1715 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1716 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001717 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001718 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001719 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001720 }
1721 }
1722}
1723
1724void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1725 LocationSummary* locations = instruction->GetLocations();
1726 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1727 Location index = locations->InAt(1);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001728 Location value_location = locations->InAt(2);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001729 Primitive::Type value_type = instruction->GetComponentType();
1730 bool needs_runtime_call = locations->WillCall();
1731 bool needs_write_barrier =
1732 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001733 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001734 GpuRegister base_reg = index.IsConstant() ? obj : TMP;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001735
1736 switch (value_type) {
1737 case Primitive::kPrimBoolean:
1738 case Primitive::kPrimByte: {
1739 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001740 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001741 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001742 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001743 __ Daddu(base_reg, obj, index.AsRegister<GpuRegister>());
1744 }
1745 if (value_location.IsConstant()) {
1746 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
1747 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
1748 } else {
1749 GpuRegister value = value_location.AsRegister<GpuRegister>();
1750 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001751 }
1752 break;
1753 }
1754
1755 case Primitive::kPrimShort:
1756 case Primitive::kPrimChar: {
1757 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001758 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001759 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001760 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001761 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_2);
1762 __ Daddu(base_reg, obj, base_reg);
1763 }
1764 if (value_location.IsConstant()) {
1765 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
1766 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
1767 } else {
1768 GpuRegister value = value_location.AsRegister<GpuRegister>();
1769 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770 }
1771 break;
1772 }
1773
1774 case Primitive::kPrimInt:
1775 case Primitive::kPrimNot: {
1776 if (!needs_runtime_call) {
1777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001779 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001780 } else {
1781 DCHECK(index.IsRegister()) << index;
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001782 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
1783 __ Daddu(base_reg, obj, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08001784 }
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001785 if (value_location.IsConstant()) {
1786 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
1787 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
1788 DCHECK(!needs_write_barrier);
Alexey Frunzec061de12017-02-14 13:27:23 -08001789 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001790 GpuRegister value = value_location.AsRegister<GpuRegister>();
1791 if (kPoisonHeapReferences && needs_write_barrier) {
1792 // Note that in the case where `value` is a null reference,
1793 // we do not enter this block, as a null reference does not
1794 // need poisoning.
1795 DCHECK_EQ(value_type, Primitive::kPrimNot);
1796 // Use Sw() instead of StoreToOffset() in order to be able to
1797 // hold the poisoned reference in AT and thus avoid allocating
1798 // yet another temporary register.
1799 if (index.IsConstant()) {
1800 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
1801 int16_t low16 = Low16Bits(data_offset);
1802 // For consistency with StoreToOffset() and such treat data_offset as int32_t.
1803 uint64_t high48 = static_cast<uint64_t>(static_cast<int32_t>(data_offset)) - low16;
1804 int16_t upper16 = High16Bits(high48);
1805 // Allow the full [-2GB,+2GB) range in case `low16` is negative and needs a
1806 // compensatory 64KB added, which may push `high48` above 2GB and require
1807 // the dahi instruction.
1808 int16_t higher16 = High32Bits(high48) + ((upper16 < 0) ? 1 : 0);
1809 __ Daui(TMP, obj, upper16);
1810 if (higher16 != 0) {
1811 __ Dahi(TMP, higher16);
1812 }
1813 base_reg = TMP;
1814 data_offset = low16;
1815 }
1816 } else {
1817 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
1818 }
1819 __ PoisonHeapReference(AT, value);
1820 __ Sw(AT, base_reg, data_offset);
1821 null_checker();
1822 } else {
1823 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
1824 }
1825 if (needs_write_barrier) {
1826 DCHECK_EQ(value_type, Primitive::kPrimNot);
1827 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
1828 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001829 }
1830 } else {
1831 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08001832 // Note: if heap poisoning is enabled, pAputObject takes care
1833 // of poisoning the reference.
Serban Constantinescufc734082016-07-19 17:18:07 +01001834 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001835 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001836 }
1837 break;
1838 }
1839
1840 case Primitive::kPrimLong: {
1841 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001842 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001843 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001845 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_8);
1846 __ Daddu(base_reg, obj, base_reg);
1847 }
1848 if (value_location.IsConstant()) {
1849 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
1850 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
1851 } else {
1852 GpuRegister value = value_location.AsRegister<GpuRegister>();
1853 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001854 }
1855 break;
1856 }
1857
1858 case Primitive::kPrimFloat: {
1859 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001860 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001861 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001862 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001863 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_4);
1864 __ Daddu(base_reg, obj, base_reg);
1865 }
1866 if (value_location.IsConstant()) {
1867 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
1868 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
1869 } else {
1870 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
1871 __ StoreFpuToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001872 }
1873 break;
1874 }
1875
1876 case Primitive::kPrimDouble: {
1877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001878 if (index.IsConstant()) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001879 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01001881 __ Dsll(base_reg, index.AsRegister<GpuRegister>(), TIMES_8);
1882 __ Daddu(base_reg, obj, base_reg);
1883 }
1884 if (value_location.IsConstant()) {
1885 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
1886 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
1887 } else {
1888 FpuRegister value = value_location.AsFpuRegister<FpuRegister>();
1889 __ StoreFpuToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001890 }
1891 break;
1892 }
1893
1894 case Primitive::kPrimVoid:
1895 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1896 UNREACHABLE();
1897 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001898}
1899
1900void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001901 RegisterSet caller_saves = RegisterSet::Empty();
1902 InvokeRuntimeCallingConvention calling_convention;
1903 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1904 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1905 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001906 locations->SetInAt(0, Location::RequiresRegister());
1907 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001908}
1909
1910void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1911 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001912 BoundsCheckSlowPathMIPS64* slow_path =
1913 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001914 codegen_->AddSlowPath(slow_path);
1915
1916 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1917 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1918
1919 // length is limited by the maximum positive signed 32-bit integer.
1920 // Unsigned comparison of length and index checks for index < 0
1921 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001922 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001923}
1924
1925void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001926 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
1927 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
1928
1929 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
1930 switch (type_check_kind) {
1931 case TypeCheckKind::kExactCheck:
1932 case TypeCheckKind::kAbstractClassCheck:
1933 case TypeCheckKind::kClassHierarchyCheck:
1934 case TypeCheckKind::kArrayObjectCheck:
1935 call_kind = throws_into_catch
1936 ? LocationSummary::kCallOnSlowPath
1937 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
1938 break;
1939 case TypeCheckKind::kArrayCheck:
1940 case TypeCheckKind::kUnresolvedCheck:
1941 case TypeCheckKind::kInterfaceCheck:
1942 call_kind = LocationSummary::kCallOnSlowPath;
1943 break;
1944 }
1945
1946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001947 locations->SetInAt(0, Location::RequiresRegister());
1948 locations->SetInAt(1, Location::RequiresRegister());
1949 locations->AddTemp(Location::RequiresRegister());
1950}
1951
1952void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001953 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001954 LocationSummary* locations = instruction->GetLocations();
1955 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1956 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001957 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
1958 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1959 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1960 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1961 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1962 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
1963 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
1964 const uint32_t object_array_data_offset =
1965 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
1966 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001967
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001968 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
1969 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
1970 // read barriers is done for performance and code size reasons.
1971 bool is_type_check_slow_path_fatal = false;
1972 if (!kEmitCompilerReadBarrier) {
1973 is_type_check_slow_path_fatal =
1974 (type_check_kind == TypeCheckKind::kExactCheck ||
1975 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
1976 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
1977 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
1978 !instruction->CanThrowIntoCatchBlock();
1979 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001980 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001981 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
1982 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001983 codegen_->AddSlowPath(slow_path);
1984
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001985 // Avoid this check if we know `obj` is not null.
1986 if (instruction->MustDoNullCheck()) {
1987 __ Beqzc(obj, &done);
1988 }
1989
1990 switch (type_check_kind) {
1991 case TypeCheckKind::kExactCheck:
1992 case TypeCheckKind::kArrayCheck: {
1993 // /* HeapReference<Class> */ temp = obj->klass_
1994 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
1995 __ MaybeUnpoisonHeapReference(temp);
1996 // Jump to slow path for throwing the exception or doing a
1997 // more involved array check.
1998 __ Bnec(temp, cls, slow_path->GetEntryLabel());
1999 break;
2000 }
2001
2002 case TypeCheckKind::kAbstractClassCheck: {
2003 // /* HeapReference<Class> */ temp = obj->klass_
2004 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2005 __ MaybeUnpoisonHeapReference(temp);
2006 // If the class is abstract, we eagerly fetch the super class of the
2007 // object to avoid doing a comparison we know will fail.
2008 Mips64Label loop;
2009 __ Bind(&loop);
2010 // /* HeapReference<Class> */ temp = temp->super_class_
2011 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, super_offset);
2012 __ MaybeUnpoisonHeapReference(temp);
2013 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2014 // exception.
2015 __ Beqzc(temp, slow_path->GetEntryLabel());
2016 // Otherwise, compare the classes.
2017 __ Bnec(temp, cls, &loop);
2018 break;
2019 }
2020
2021 case TypeCheckKind::kClassHierarchyCheck: {
2022 // /* HeapReference<Class> */ temp = obj->klass_
2023 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2024 __ MaybeUnpoisonHeapReference(temp);
2025 // Walk over the class hierarchy to find a match.
2026 Mips64Label loop;
2027 __ Bind(&loop);
2028 __ Beqc(temp, cls, &done);
2029 // /* HeapReference<Class> */ temp = temp->super_class_
2030 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, super_offset);
2031 __ MaybeUnpoisonHeapReference(temp);
2032 // If the class reference currently in `temp` is null, jump to the slow path to throw the
2033 // exception. Otherwise, jump to the beginning of the loop.
2034 __ Bnezc(temp, &loop);
2035 __ Bc(slow_path->GetEntryLabel());
2036 break;
2037 }
2038
2039 case TypeCheckKind::kArrayObjectCheck: {
2040 // /* HeapReference<Class> */ temp = obj->klass_
2041 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2042 __ MaybeUnpoisonHeapReference(temp);
2043 // Do an exact check.
2044 __ Beqc(temp, cls, &done);
2045 // Otherwise, we need to check that the object's class is a non-primitive array.
2046 // /* HeapReference<Class> */ temp = temp->component_type_
2047 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, component_offset);
2048 __ MaybeUnpoisonHeapReference(temp);
2049 // If the component type is null, jump to the slow path to throw the exception.
2050 __ Beqzc(temp, slow_path->GetEntryLabel());
2051 // Otherwise, the object is indeed an array, further check that this component
2052 // type is not a primitive type.
2053 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2054 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2055 __ Bnezc(temp, slow_path->GetEntryLabel());
2056 break;
2057 }
2058
2059 case TypeCheckKind::kUnresolvedCheck:
2060 // We always go into the type check slow path for the unresolved check case.
2061 // We cannot directly call the CheckCast runtime entry point
2062 // without resorting to a type checking slow path here (i.e. by
2063 // calling InvokeRuntime directly), as it would require to
2064 // assign fixed registers for the inputs of this HInstanceOf
2065 // instruction (following the runtime calling convention), which
2066 // might be cluttered by the potential first read barrier
2067 // emission at the beginning of this method.
2068 __ Bc(slow_path->GetEntryLabel());
2069 break;
2070
2071 case TypeCheckKind::kInterfaceCheck: {
2072 // Avoid read barriers to improve performance of the fast path. We can not get false
2073 // positives by doing this.
2074 // /* HeapReference<Class> */ temp = obj->klass_
2075 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2076 __ MaybeUnpoisonHeapReference(temp);
2077 // /* HeapReference<Class> */ temp = temp->iftable_
2078 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, iftable_offset);
2079 __ MaybeUnpoisonHeapReference(temp);
2080 // Iftable is never null.
2081 __ Lw(TMP, temp, array_length_offset);
2082 // Loop through the iftable and check if any class matches.
2083 Mips64Label loop;
2084 __ Bind(&loop);
2085 __ Beqzc(TMP, slow_path->GetEntryLabel());
2086 __ Lwu(AT, temp, object_array_data_offset);
2087 __ MaybeUnpoisonHeapReference(AT);
2088 // Go to next interface.
2089 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2090 __ Addiu(TMP, TMP, -2);
2091 // Compare the classes and continue the loop if they do not match.
2092 __ Bnec(AT, cls, &loop);
2093 break;
2094 }
2095 }
2096
2097 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 __ Bind(slow_path->GetExitLabel());
2099}
2100
2101void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2102 LocationSummary* locations =
2103 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2104 locations->SetInAt(0, Location::RequiresRegister());
2105 if (check->HasUses()) {
2106 locations->SetOut(Location::SameAsFirstInput());
2107 }
2108}
2109
2110void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2111 // We assume the class is not null.
2112 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2113 check->GetLoadClass(),
2114 check,
2115 check->GetDexPc(),
2116 true);
2117 codegen_->AddSlowPath(slow_path);
2118 GenerateClassInitializationCheck(slow_path,
2119 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2120}
2121
2122void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2123 Primitive::Type in_type = compare->InputAt(0)->GetType();
2124
Alexey Frunze299a9392015-12-08 16:08:02 -08002125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002126
2127 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002128 case Primitive::kPrimBoolean:
2129 case Primitive::kPrimByte:
2130 case Primitive::kPrimShort:
2131 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002132 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002133 case Primitive::kPrimLong:
2134 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002135 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2137 break;
2138
2139 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002140 case Primitive::kPrimDouble:
2141 locations->SetInAt(0, Location::RequiresFpuRegister());
2142 locations->SetInAt(1, Location::RequiresFpuRegister());
2143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002144 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002145
2146 default:
2147 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2148 }
2149}
2150
2151void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2152 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002153 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002154 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2155
2156 // 0 if: left == right
2157 // 1 if: left > right
2158 // -1 if: left < right
2159 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002160 case Primitive::kPrimBoolean:
2161 case Primitive::kPrimByte:
2162 case Primitive::kPrimShort:
2163 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002164 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002165 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002167 Location rhs_location = locations->InAt(1);
2168 bool use_imm = rhs_location.IsConstant();
2169 GpuRegister rhs = ZERO;
2170 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002171 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002172 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2173 if (value != 0) {
2174 rhs = AT;
2175 __ LoadConst64(rhs, value);
2176 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002177 } else {
2178 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2179 if (value != 0) {
2180 rhs = AT;
2181 __ LoadConst32(rhs, value);
2182 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002183 }
2184 } else {
2185 rhs = rhs_location.AsRegister<GpuRegister>();
2186 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002187 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002188 __ Slt(res, rhs, lhs);
2189 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 break;
2191 }
2192
Alexey Frunze299a9392015-12-08 16:08:02 -08002193 case Primitive::kPrimFloat: {
2194 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2195 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2196 Mips64Label done;
2197 __ CmpEqS(FTMP, lhs, rhs);
2198 __ LoadConst32(res, 0);
2199 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002200 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002201 __ CmpLtS(FTMP, lhs, rhs);
2202 __ LoadConst32(res, -1);
2203 __ Bc1nez(FTMP, &done);
2204 __ LoadConst32(res, 1);
2205 } else {
2206 __ CmpLtS(FTMP, rhs, lhs);
2207 __ LoadConst32(res, 1);
2208 __ Bc1nez(FTMP, &done);
2209 __ LoadConst32(res, -1);
2210 }
2211 __ Bind(&done);
2212 break;
2213 }
2214
Alexey Frunze4dda3372015-06-01 18:31:49 -07002215 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002216 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2217 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2218 Mips64Label done;
2219 __ CmpEqD(FTMP, lhs, rhs);
2220 __ LoadConst32(res, 0);
2221 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002222 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002223 __ CmpLtD(FTMP, lhs, rhs);
2224 __ LoadConst32(res, -1);
2225 __ Bc1nez(FTMP, &done);
2226 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002227 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002228 __ CmpLtD(FTMP, rhs, lhs);
2229 __ LoadConst32(res, 1);
2230 __ Bc1nez(FTMP, &done);
2231 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002232 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002233 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002234 break;
2235 }
2236
2237 default:
2238 LOG(FATAL) << "Unimplemented compare type " << in_type;
2239 }
2240}
2241
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002242void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002244 switch (instruction->InputAt(0)->GetType()) {
2245 default:
2246 case Primitive::kPrimLong:
2247 locations->SetInAt(0, Location::RequiresRegister());
2248 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2249 break;
2250
2251 case Primitive::kPrimFloat:
2252 case Primitive::kPrimDouble:
2253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetInAt(1, Location::RequiresFpuRegister());
2255 break;
2256 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002257 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 }
2260}
2261
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002262void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002263 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002264 return;
2265 }
2266
Alexey Frunze299a9392015-12-08 16:08:02 -08002267 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002268 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002269 switch (type) {
2270 default:
2271 // Integer case.
2272 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2273 return;
2274 case Primitive::kPrimLong:
2275 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2276 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002277 case Primitive::kPrimFloat:
2278 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002279 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2280 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002281 }
2282}
2283
Alexey Frunzec857c742015-09-23 15:12:39 -07002284void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2285 DCHECK(instruction->IsDiv() || instruction->IsRem());
2286 Primitive::Type type = instruction->GetResultType();
2287
2288 LocationSummary* locations = instruction->GetLocations();
2289 Location second = locations->InAt(1);
2290 DCHECK(second.IsConstant());
2291
2292 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2293 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2294 int64_t imm = Int64FromConstant(second.GetConstant());
2295 DCHECK(imm == 1 || imm == -1);
2296
2297 if (instruction->IsRem()) {
2298 __ Move(out, ZERO);
2299 } else {
2300 if (imm == -1) {
2301 if (type == Primitive::kPrimInt) {
2302 __ Subu(out, ZERO, dividend);
2303 } else {
2304 DCHECK_EQ(type, Primitive::kPrimLong);
2305 __ Dsubu(out, ZERO, dividend);
2306 }
2307 } else if (out != dividend) {
2308 __ Move(out, dividend);
2309 }
2310 }
2311}
2312
2313void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2314 DCHECK(instruction->IsDiv() || instruction->IsRem());
2315 Primitive::Type type = instruction->GetResultType();
2316
2317 LocationSummary* locations = instruction->GetLocations();
2318 Location second = locations->InAt(1);
2319 DCHECK(second.IsConstant());
2320
2321 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2322 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2323 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002324 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002325 int ctz_imm = CTZ(abs_imm);
2326
2327 if (instruction->IsDiv()) {
2328 if (type == Primitive::kPrimInt) {
2329 if (ctz_imm == 1) {
2330 // Fast path for division by +/-2, which is very common.
2331 __ Srl(TMP, dividend, 31);
2332 } else {
2333 __ Sra(TMP, dividend, 31);
2334 __ Srl(TMP, TMP, 32 - ctz_imm);
2335 }
2336 __ Addu(out, dividend, TMP);
2337 __ Sra(out, out, ctz_imm);
2338 if (imm < 0) {
2339 __ Subu(out, ZERO, out);
2340 }
2341 } else {
2342 DCHECK_EQ(type, Primitive::kPrimLong);
2343 if (ctz_imm == 1) {
2344 // Fast path for division by +/-2, which is very common.
2345 __ Dsrl32(TMP, dividend, 31);
2346 } else {
2347 __ Dsra32(TMP, dividend, 31);
2348 if (ctz_imm > 32) {
2349 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2350 } else {
2351 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2352 }
2353 }
2354 __ Daddu(out, dividend, TMP);
2355 if (ctz_imm < 32) {
2356 __ Dsra(out, out, ctz_imm);
2357 } else {
2358 __ Dsra32(out, out, ctz_imm - 32);
2359 }
2360 if (imm < 0) {
2361 __ Dsubu(out, ZERO, out);
2362 }
2363 }
2364 } else {
2365 if (type == Primitive::kPrimInt) {
2366 if (ctz_imm == 1) {
2367 // Fast path for modulo +/-2, which is very common.
2368 __ Sra(TMP, dividend, 31);
2369 __ Subu(out, dividend, TMP);
2370 __ Andi(out, out, 1);
2371 __ Addu(out, out, TMP);
2372 } else {
2373 __ Sra(TMP, dividend, 31);
2374 __ Srl(TMP, TMP, 32 - ctz_imm);
2375 __ Addu(out, dividend, TMP);
2376 if (IsUint<16>(abs_imm - 1)) {
2377 __ Andi(out, out, abs_imm - 1);
2378 } else {
2379 __ Sll(out, out, 32 - ctz_imm);
2380 __ Srl(out, out, 32 - ctz_imm);
2381 }
2382 __ Subu(out, out, TMP);
2383 }
2384 } else {
2385 DCHECK_EQ(type, Primitive::kPrimLong);
2386 if (ctz_imm == 1) {
2387 // Fast path for modulo +/-2, which is very common.
2388 __ Dsra32(TMP, dividend, 31);
2389 __ Dsubu(out, dividend, TMP);
2390 __ Andi(out, out, 1);
2391 __ Daddu(out, out, TMP);
2392 } else {
2393 __ Dsra32(TMP, dividend, 31);
2394 if (ctz_imm > 32) {
2395 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2396 } else {
2397 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2398 }
2399 __ Daddu(out, dividend, TMP);
2400 if (IsUint<16>(abs_imm - 1)) {
2401 __ Andi(out, out, abs_imm - 1);
2402 } else {
2403 if (ctz_imm > 32) {
2404 __ Dsll(out, out, 64 - ctz_imm);
2405 __ Dsrl(out, out, 64 - ctz_imm);
2406 } else {
2407 __ Dsll32(out, out, 32 - ctz_imm);
2408 __ Dsrl32(out, out, 32 - ctz_imm);
2409 }
2410 }
2411 __ Dsubu(out, out, TMP);
2412 }
2413 }
2414 }
2415}
2416
2417void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2418 DCHECK(instruction->IsDiv() || instruction->IsRem());
2419
2420 LocationSummary* locations = instruction->GetLocations();
2421 Location second = locations->InAt(1);
2422 DCHECK(second.IsConstant());
2423
2424 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2425 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2426 int64_t imm = Int64FromConstant(second.GetConstant());
2427
2428 Primitive::Type type = instruction->GetResultType();
2429 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2430
2431 int64_t magic;
2432 int shift;
2433 CalculateMagicAndShiftForDivRem(imm,
2434 (type == Primitive::kPrimLong),
2435 &magic,
2436 &shift);
2437
2438 if (type == Primitive::kPrimInt) {
2439 __ LoadConst32(TMP, magic);
2440 __ MuhR6(TMP, dividend, TMP);
2441
2442 if (imm > 0 && magic < 0) {
2443 __ Addu(TMP, TMP, dividend);
2444 } else if (imm < 0 && magic > 0) {
2445 __ Subu(TMP, TMP, dividend);
2446 }
2447
2448 if (shift != 0) {
2449 __ Sra(TMP, TMP, shift);
2450 }
2451
2452 if (instruction->IsDiv()) {
2453 __ Sra(out, TMP, 31);
2454 __ Subu(out, TMP, out);
2455 } else {
2456 __ Sra(AT, TMP, 31);
2457 __ Subu(AT, TMP, AT);
2458 __ LoadConst32(TMP, imm);
2459 __ MulR6(TMP, AT, TMP);
2460 __ Subu(out, dividend, TMP);
2461 }
2462 } else {
2463 __ LoadConst64(TMP, magic);
2464 __ Dmuh(TMP, dividend, TMP);
2465
2466 if (imm > 0 && magic < 0) {
2467 __ Daddu(TMP, TMP, dividend);
2468 } else if (imm < 0 && magic > 0) {
2469 __ Dsubu(TMP, TMP, dividend);
2470 }
2471
2472 if (shift >= 32) {
2473 __ Dsra32(TMP, TMP, shift - 32);
2474 } else if (shift > 0) {
2475 __ Dsra(TMP, TMP, shift);
2476 }
2477
2478 if (instruction->IsDiv()) {
2479 __ Dsra32(out, TMP, 31);
2480 __ Dsubu(out, TMP, out);
2481 } else {
2482 __ Dsra32(AT, TMP, 31);
2483 __ Dsubu(AT, TMP, AT);
2484 __ LoadConst64(TMP, imm);
2485 __ Dmul(TMP, AT, TMP);
2486 __ Dsubu(out, dividend, TMP);
2487 }
2488 }
2489}
2490
2491void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2492 DCHECK(instruction->IsDiv() || instruction->IsRem());
2493 Primitive::Type type = instruction->GetResultType();
2494 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2495
2496 LocationSummary* locations = instruction->GetLocations();
2497 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2498 Location second = locations->InAt(1);
2499
2500 if (second.IsConstant()) {
2501 int64_t imm = Int64FromConstant(second.GetConstant());
2502 if (imm == 0) {
2503 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2504 } else if (imm == 1 || imm == -1) {
2505 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002506 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002507 DivRemByPowerOfTwo(instruction);
2508 } else {
2509 DCHECK(imm <= -2 || imm >= 2);
2510 GenerateDivRemWithAnyConstant(instruction);
2511 }
2512 } else {
2513 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2514 GpuRegister divisor = second.AsRegister<GpuRegister>();
2515 if (instruction->IsDiv()) {
2516 if (type == Primitive::kPrimInt)
2517 __ DivR6(out, dividend, divisor);
2518 else
2519 __ Ddiv(out, dividend, divisor);
2520 } else {
2521 if (type == Primitive::kPrimInt)
2522 __ ModR6(out, dividend, divisor);
2523 else
2524 __ Dmod(out, dividend, divisor);
2525 }
2526 }
2527}
2528
Alexey Frunze4dda3372015-06-01 18:31:49 -07002529void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2532 switch (div->GetResultType()) {
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimLong:
2535 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002536 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002537 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2538 break;
2539
2540 case Primitive::kPrimFloat:
2541 case Primitive::kPrimDouble:
2542 locations->SetInAt(0, Location::RequiresFpuRegister());
2543 locations->SetInAt(1, Location::RequiresFpuRegister());
2544 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2545 break;
2546
2547 default:
2548 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2549 }
2550}
2551
2552void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2553 Primitive::Type type = instruction->GetType();
2554 LocationSummary* locations = instruction->GetLocations();
2555
2556 switch (type) {
2557 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002558 case Primitive::kPrimLong:
2559 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002561 case Primitive::kPrimFloat:
2562 case Primitive::kPrimDouble: {
2563 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2564 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2565 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2566 if (type == Primitive::kPrimFloat)
2567 __ DivS(dst, lhs, rhs);
2568 else
2569 __ DivD(dst, lhs, rhs);
2570 break;
2571 }
2572 default:
2573 LOG(FATAL) << "Unexpected div type " << type;
2574 }
2575}
2576
2577void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002578 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002579 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002580}
2581
2582void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2583 SlowPathCodeMIPS64* slow_path =
2584 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2585 codegen_->AddSlowPath(slow_path);
2586 Location value = instruction->GetLocations()->InAt(0);
2587
2588 Primitive::Type type = instruction->GetType();
2589
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002590 if (!Primitive::IsIntegralType(type)) {
2591 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002592 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593 }
2594
2595 if (value.IsConstant()) {
2596 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2597 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002598 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 } else {
2600 // A division by a non-null constant is valid. We don't need to perform
2601 // any check, so simply fall through.
2602 }
2603 } else {
2604 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2605 }
2606}
2607
2608void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2609 LocationSummary* locations =
2610 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2611 locations->SetOut(Location::ConstantLocation(constant));
2612}
2613
2614void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2615 // Will be generated at use site.
2616}
2617
2618void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2619 exit->SetLocations(nullptr);
2620}
2621
2622void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2623}
2624
2625void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2626 LocationSummary* locations =
2627 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2628 locations->SetOut(Location::ConstantLocation(constant));
2629}
2630
2631void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2632 // Will be generated at use site.
2633}
2634
David Brazdilfc6a86a2015-06-26 10:33:45 +00002635void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002636 DCHECK(!successor->IsExitBlock());
2637 HBasicBlock* block = got->GetBlock();
2638 HInstruction* previous = got->GetPrevious();
2639 HLoopInformation* info = block->GetLoopInformation();
2640
2641 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2642 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2643 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2644 return;
2645 }
2646 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2647 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2648 }
2649 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002650 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002651 }
2652}
2653
David Brazdilfc6a86a2015-06-26 10:33:45 +00002654void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2655 got->SetLocations(nullptr);
2656}
2657
2658void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2659 HandleGoto(got, got->GetSuccessor());
2660}
2661
2662void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2663 try_boundary->SetLocations(nullptr);
2664}
2665
2666void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2667 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2668 if (!successor->IsExitBlock()) {
2669 HandleGoto(try_boundary, successor);
2670 }
2671}
2672
Alexey Frunze299a9392015-12-08 16:08:02 -08002673void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2674 bool is64bit,
2675 LocationSummary* locations) {
2676 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2677 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2678 Location rhs_location = locations->InAt(1);
2679 GpuRegister rhs_reg = ZERO;
2680 int64_t rhs_imm = 0;
2681 bool use_imm = rhs_location.IsConstant();
2682 if (use_imm) {
2683 if (is64bit) {
2684 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2685 } else {
2686 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2687 }
2688 } else {
2689 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2690 }
2691 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2692
2693 switch (cond) {
2694 case kCondEQ:
2695 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002696 if (use_imm && IsInt<16>(-rhs_imm)) {
2697 if (rhs_imm == 0) {
2698 if (cond == kCondEQ) {
2699 __ Sltiu(dst, lhs, 1);
2700 } else {
2701 __ Sltu(dst, ZERO, lhs);
2702 }
2703 } else {
2704 if (is64bit) {
2705 __ Daddiu(dst, lhs, -rhs_imm);
2706 } else {
2707 __ Addiu(dst, lhs, -rhs_imm);
2708 }
2709 if (cond == kCondEQ) {
2710 __ Sltiu(dst, dst, 1);
2711 } else {
2712 __ Sltu(dst, ZERO, dst);
2713 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002714 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002715 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002716 if (use_imm && IsUint<16>(rhs_imm)) {
2717 __ Xori(dst, lhs, rhs_imm);
2718 } else {
2719 if (use_imm) {
2720 rhs_reg = TMP;
2721 __ LoadConst64(rhs_reg, rhs_imm);
2722 }
2723 __ Xor(dst, lhs, rhs_reg);
2724 }
2725 if (cond == kCondEQ) {
2726 __ Sltiu(dst, dst, 1);
2727 } else {
2728 __ Sltu(dst, ZERO, dst);
2729 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002730 }
2731 break;
2732
2733 case kCondLT:
2734 case kCondGE:
2735 if (use_imm && IsInt<16>(rhs_imm)) {
2736 __ Slti(dst, lhs, rhs_imm);
2737 } else {
2738 if (use_imm) {
2739 rhs_reg = TMP;
2740 __ LoadConst64(rhs_reg, rhs_imm);
2741 }
2742 __ Slt(dst, lhs, rhs_reg);
2743 }
2744 if (cond == kCondGE) {
2745 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2746 // only the slt instruction but no sge.
2747 __ Xori(dst, dst, 1);
2748 }
2749 break;
2750
2751 case kCondLE:
2752 case kCondGT:
2753 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2754 // Simulate lhs <= rhs via lhs < rhs + 1.
2755 __ Slti(dst, lhs, rhs_imm_plus_one);
2756 if (cond == kCondGT) {
2757 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2758 // only the slti instruction but no sgti.
2759 __ Xori(dst, dst, 1);
2760 }
2761 } else {
2762 if (use_imm) {
2763 rhs_reg = TMP;
2764 __ LoadConst64(rhs_reg, rhs_imm);
2765 }
2766 __ Slt(dst, rhs_reg, lhs);
2767 if (cond == kCondLE) {
2768 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2769 // only the slt instruction but no sle.
2770 __ Xori(dst, dst, 1);
2771 }
2772 }
2773 break;
2774
2775 case kCondB:
2776 case kCondAE:
2777 if (use_imm && IsInt<16>(rhs_imm)) {
2778 // Sltiu sign-extends its 16-bit immediate operand before
2779 // the comparison and thus lets us compare directly with
2780 // unsigned values in the ranges [0, 0x7fff] and
2781 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2782 __ Sltiu(dst, lhs, rhs_imm);
2783 } else {
2784 if (use_imm) {
2785 rhs_reg = TMP;
2786 __ LoadConst64(rhs_reg, rhs_imm);
2787 }
2788 __ Sltu(dst, lhs, rhs_reg);
2789 }
2790 if (cond == kCondAE) {
2791 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2792 // only the sltu instruction but no sgeu.
2793 __ Xori(dst, dst, 1);
2794 }
2795 break;
2796
2797 case kCondBE:
2798 case kCondA:
2799 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2800 // Simulate lhs <= rhs via lhs < rhs + 1.
2801 // Note that this only works if rhs + 1 does not overflow
2802 // to 0, hence the check above.
2803 // Sltiu sign-extends its 16-bit immediate operand before
2804 // the comparison and thus lets us compare directly with
2805 // unsigned values in the ranges [0, 0x7fff] and
2806 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2807 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2808 if (cond == kCondA) {
2809 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2810 // only the sltiu instruction but no sgtiu.
2811 __ Xori(dst, dst, 1);
2812 }
2813 } else {
2814 if (use_imm) {
2815 rhs_reg = TMP;
2816 __ LoadConst64(rhs_reg, rhs_imm);
2817 }
2818 __ Sltu(dst, rhs_reg, lhs);
2819 if (cond == kCondBE) {
2820 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2821 // only the sltu instruction but no sleu.
2822 __ Xori(dst, dst, 1);
2823 }
2824 }
2825 break;
2826 }
2827}
2828
2829void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2830 bool is64bit,
2831 LocationSummary* locations,
2832 Mips64Label* label) {
2833 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2834 Location rhs_location = locations->InAt(1);
2835 GpuRegister rhs_reg = ZERO;
2836 int64_t rhs_imm = 0;
2837 bool use_imm = rhs_location.IsConstant();
2838 if (use_imm) {
2839 if (is64bit) {
2840 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2841 } else {
2842 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2843 }
2844 } else {
2845 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2846 }
2847
2848 if (use_imm && rhs_imm == 0) {
2849 switch (cond) {
2850 case kCondEQ:
2851 case kCondBE: // <= 0 if zero
2852 __ Beqzc(lhs, label);
2853 break;
2854 case kCondNE:
2855 case kCondA: // > 0 if non-zero
2856 __ Bnezc(lhs, label);
2857 break;
2858 case kCondLT:
2859 __ Bltzc(lhs, label);
2860 break;
2861 case kCondGE:
2862 __ Bgezc(lhs, label);
2863 break;
2864 case kCondLE:
2865 __ Blezc(lhs, label);
2866 break;
2867 case kCondGT:
2868 __ Bgtzc(lhs, label);
2869 break;
2870 case kCondB: // always false
2871 break;
2872 case kCondAE: // always true
2873 __ Bc(label);
2874 break;
2875 }
2876 } else {
2877 if (use_imm) {
2878 rhs_reg = TMP;
2879 __ LoadConst64(rhs_reg, rhs_imm);
2880 }
2881 switch (cond) {
2882 case kCondEQ:
2883 __ Beqc(lhs, rhs_reg, label);
2884 break;
2885 case kCondNE:
2886 __ Bnec(lhs, rhs_reg, label);
2887 break;
2888 case kCondLT:
2889 __ Bltc(lhs, rhs_reg, label);
2890 break;
2891 case kCondGE:
2892 __ Bgec(lhs, rhs_reg, label);
2893 break;
2894 case kCondLE:
2895 __ Bgec(rhs_reg, lhs, label);
2896 break;
2897 case kCondGT:
2898 __ Bltc(rhs_reg, lhs, label);
2899 break;
2900 case kCondB:
2901 __ Bltuc(lhs, rhs_reg, label);
2902 break;
2903 case kCondAE:
2904 __ Bgeuc(lhs, rhs_reg, label);
2905 break;
2906 case kCondBE:
2907 __ Bgeuc(rhs_reg, lhs, label);
2908 break;
2909 case kCondA:
2910 __ Bltuc(rhs_reg, lhs, label);
2911 break;
2912 }
2913 }
2914}
2915
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002916void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2917 bool gt_bias,
2918 Primitive::Type type,
2919 LocationSummary* locations) {
2920 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2921 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2922 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2923 if (type == Primitive::kPrimFloat) {
2924 switch (cond) {
2925 case kCondEQ:
2926 __ CmpEqS(FTMP, lhs, rhs);
2927 __ Mfc1(dst, FTMP);
2928 __ Andi(dst, dst, 1);
2929 break;
2930 case kCondNE:
2931 __ CmpEqS(FTMP, lhs, rhs);
2932 __ Mfc1(dst, FTMP);
2933 __ Addiu(dst, dst, 1);
2934 break;
2935 case kCondLT:
2936 if (gt_bias) {
2937 __ CmpLtS(FTMP, lhs, rhs);
2938 } else {
2939 __ CmpUltS(FTMP, lhs, rhs);
2940 }
2941 __ Mfc1(dst, FTMP);
2942 __ Andi(dst, dst, 1);
2943 break;
2944 case kCondLE:
2945 if (gt_bias) {
2946 __ CmpLeS(FTMP, lhs, rhs);
2947 } else {
2948 __ CmpUleS(FTMP, lhs, rhs);
2949 }
2950 __ Mfc1(dst, FTMP);
2951 __ Andi(dst, dst, 1);
2952 break;
2953 case kCondGT:
2954 if (gt_bias) {
2955 __ CmpUltS(FTMP, rhs, lhs);
2956 } else {
2957 __ CmpLtS(FTMP, rhs, lhs);
2958 }
2959 __ Mfc1(dst, FTMP);
2960 __ Andi(dst, dst, 1);
2961 break;
2962 case kCondGE:
2963 if (gt_bias) {
2964 __ CmpUleS(FTMP, rhs, lhs);
2965 } else {
2966 __ CmpLeS(FTMP, rhs, lhs);
2967 }
2968 __ Mfc1(dst, FTMP);
2969 __ Andi(dst, dst, 1);
2970 break;
2971 default:
2972 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2973 UNREACHABLE();
2974 }
2975 } else {
2976 DCHECK_EQ(type, Primitive::kPrimDouble);
2977 switch (cond) {
2978 case kCondEQ:
2979 __ CmpEqD(FTMP, lhs, rhs);
2980 __ Mfc1(dst, FTMP);
2981 __ Andi(dst, dst, 1);
2982 break;
2983 case kCondNE:
2984 __ CmpEqD(FTMP, lhs, rhs);
2985 __ Mfc1(dst, FTMP);
2986 __ Addiu(dst, dst, 1);
2987 break;
2988 case kCondLT:
2989 if (gt_bias) {
2990 __ CmpLtD(FTMP, lhs, rhs);
2991 } else {
2992 __ CmpUltD(FTMP, lhs, rhs);
2993 }
2994 __ Mfc1(dst, FTMP);
2995 __ Andi(dst, dst, 1);
2996 break;
2997 case kCondLE:
2998 if (gt_bias) {
2999 __ CmpLeD(FTMP, lhs, rhs);
3000 } else {
3001 __ CmpUleD(FTMP, lhs, rhs);
3002 }
3003 __ Mfc1(dst, FTMP);
3004 __ Andi(dst, dst, 1);
3005 break;
3006 case kCondGT:
3007 if (gt_bias) {
3008 __ CmpUltD(FTMP, rhs, lhs);
3009 } else {
3010 __ CmpLtD(FTMP, rhs, lhs);
3011 }
3012 __ Mfc1(dst, FTMP);
3013 __ Andi(dst, dst, 1);
3014 break;
3015 case kCondGE:
3016 if (gt_bias) {
3017 __ CmpUleD(FTMP, rhs, lhs);
3018 } else {
3019 __ CmpLeD(FTMP, rhs, lhs);
3020 }
3021 __ Mfc1(dst, FTMP);
3022 __ Andi(dst, dst, 1);
3023 break;
3024 default:
3025 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3026 UNREACHABLE();
3027 }
3028 }
3029}
3030
Alexey Frunze299a9392015-12-08 16:08:02 -08003031void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
3032 bool gt_bias,
3033 Primitive::Type type,
3034 LocationSummary* locations,
3035 Mips64Label* label) {
3036 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3037 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3038 if (type == Primitive::kPrimFloat) {
3039 switch (cond) {
3040 case kCondEQ:
3041 __ CmpEqS(FTMP, lhs, rhs);
3042 __ Bc1nez(FTMP, label);
3043 break;
3044 case kCondNE:
3045 __ CmpEqS(FTMP, lhs, rhs);
3046 __ Bc1eqz(FTMP, label);
3047 break;
3048 case kCondLT:
3049 if (gt_bias) {
3050 __ CmpLtS(FTMP, lhs, rhs);
3051 } else {
3052 __ CmpUltS(FTMP, lhs, rhs);
3053 }
3054 __ Bc1nez(FTMP, label);
3055 break;
3056 case kCondLE:
3057 if (gt_bias) {
3058 __ CmpLeS(FTMP, lhs, rhs);
3059 } else {
3060 __ CmpUleS(FTMP, lhs, rhs);
3061 }
3062 __ Bc1nez(FTMP, label);
3063 break;
3064 case kCondGT:
3065 if (gt_bias) {
3066 __ CmpUltS(FTMP, rhs, lhs);
3067 } else {
3068 __ CmpLtS(FTMP, rhs, lhs);
3069 }
3070 __ Bc1nez(FTMP, label);
3071 break;
3072 case kCondGE:
3073 if (gt_bias) {
3074 __ CmpUleS(FTMP, rhs, lhs);
3075 } else {
3076 __ CmpLeS(FTMP, rhs, lhs);
3077 }
3078 __ Bc1nez(FTMP, label);
3079 break;
3080 default:
3081 LOG(FATAL) << "Unexpected non-floating-point condition";
3082 }
3083 } else {
3084 DCHECK_EQ(type, Primitive::kPrimDouble);
3085 switch (cond) {
3086 case kCondEQ:
3087 __ CmpEqD(FTMP, lhs, rhs);
3088 __ Bc1nez(FTMP, label);
3089 break;
3090 case kCondNE:
3091 __ CmpEqD(FTMP, lhs, rhs);
3092 __ Bc1eqz(FTMP, label);
3093 break;
3094 case kCondLT:
3095 if (gt_bias) {
3096 __ CmpLtD(FTMP, lhs, rhs);
3097 } else {
3098 __ CmpUltD(FTMP, lhs, rhs);
3099 }
3100 __ Bc1nez(FTMP, label);
3101 break;
3102 case kCondLE:
3103 if (gt_bias) {
3104 __ CmpLeD(FTMP, lhs, rhs);
3105 } else {
3106 __ CmpUleD(FTMP, lhs, rhs);
3107 }
3108 __ Bc1nez(FTMP, label);
3109 break;
3110 case kCondGT:
3111 if (gt_bias) {
3112 __ CmpUltD(FTMP, rhs, lhs);
3113 } else {
3114 __ CmpLtD(FTMP, rhs, lhs);
3115 }
3116 __ Bc1nez(FTMP, label);
3117 break;
3118 case kCondGE:
3119 if (gt_bias) {
3120 __ CmpUleD(FTMP, rhs, lhs);
3121 } else {
3122 __ CmpLeD(FTMP, rhs, lhs);
3123 }
3124 __ Bc1nez(FTMP, label);
3125 break;
3126 default:
3127 LOG(FATAL) << "Unexpected non-floating-point condition";
3128 }
3129 }
3130}
3131
Alexey Frunze4dda3372015-06-01 18:31:49 -07003132void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003133 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003134 Mips64Label* true_target,
3135 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003136 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003137
David Brazdil0debae72015-11-12 18:37:00 +00003138 if (true_target == nullptr && false_target == nullptr) {
3139 // Nothing to do. The code always falls through.
3140 return;
3141 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003142 // Constant condition, statically compared against "true" (integer value 1).
3143 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003144 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003145 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003146 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003147 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003148 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003149 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003150 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003151 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003152 }
David Brazdil0debae72015-11-12 18:37:00 +00003153 return;
3154 }
3155
3156 // The following code generates these patterns:
3157 // (1) true_target == nullptr && false_target != nullptr
3158 // - opposite condition true => branch to false_target
3159 // (2) true_target != nullptr && false_target == nullptr
3160 // - condition true => branch to true_target
3161 // (3) true_target != nullptr && false_target != nullptr
3162 // - condition true => branch to true_target
3163 // - branch to false_target
3164 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003165 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003166 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003167 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003168 if (true_target == nullptr) {
3169 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3170 } else {
3171 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3172 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003173 } else {
3174 // The condition instruction has not been materialized, use its inputs as
3175 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003176 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003177 Primitive::Type type = condition->InputAt(0)->GetType();
3178 LocationSummary* locations = cond->GetLocations();
3179 IfCondition if_cond = condition->GetCondition();
3180 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003181
David Brazdil0debae72015-11-12 18:37:00 +00003182 if (true_target == nullptr) {
3183 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003184 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003185 }
3186
Alexey Frunze299a9392015-12-08 16:08:02 -08003187 switch (type) {
3188 default:
3189 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3190 break;
3191 case Primitive::kPrimLong:
3192 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3193 break;
3194 case Primitive::kPrimFloat:
3195 case Primitive::kPrimDouble:
3196 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3197 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003198 }
3199 }
David Brazdil0debae72015-11-12 18:37:00 +00003200
3201 // If neither branch falls through (case 3), the conditional branch to `true_target`
3202 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3203 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003204 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003205 }
3206}
3207
3208void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003210 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003211 locations->SetInAt(0, Location::RequiresRegister());
3212 }
3213}
3214
3215void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003216 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3217 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003218 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003219 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003220 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003221 nullptr : codegen_->GetLabelOf(false_successor);
3222 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003223}
3224
3225void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3226 LocationSummary* locations = new (GetGraph()->GetArena())
3227 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003228 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003229 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003230 locations->SetInAt(0, Location::RequiresRegister());
3231 }
3232}
3233
3234void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003235 SlowPathCodeMIPS64* slow_path =
3236 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003237 GenerateTestAndBranch(deoptimize,
3238 /* condition_input_index */ 0,
3239 slow_path->GetEntryLabel(),
3240 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241}
3242
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003243void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3244 LocationSummary* locations = new (GetGraph()->GetArena())
3245 LocationSummary(flag, LocationSummary::kNoCall);
3246 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003247}
3248
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003249void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3250 __ LoadFromOffset(kLoadWord,
3251 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3252 SP,
3253 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003254}
3255
David Brazdil74eb1b22015-12-14 11:44:01 +00003256void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3258 if (Primitive::IsFloatingPointType(select->GetType())) {
3259 locations->SetInAt(0, Location::RequiresFpuRegister());
3260 locations->SetInAt(1, Location::RequiresFpuRegister());
3261 } else {
3262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, Location::RequiresRegister());
3264 }
3265 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3266 locations->SetInAt(2, Location::RequiresRegister());
3267 }
3268 locations->SetOut(Location::SameAsFirstInput());
3269}
3270
3271void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3272 LocationSummary* locations = select->GetLocations();
3273 Mips64Label false_target;
3274 GenerateTestAndBranch(select,
3275 /* condition_input_index */ 2,
3276 /* true_target */ nullptr,
3277 &false_target);
3278 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3279 __ Bind(&false_target);
3280}
3281
David Srbecky0cf44932015-12-09 14:09:59 +00003282void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3283 new (GetGraph()->GetArena()) LocationSummary(info);
3284}
3285
David Srbeckyd28f4a02016-03-14 17:14:24 +00003286void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3287 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003288}
3289
3290void CodeGeneratorMIPS64::GenerateNop() {
3291 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003292}
3293
Alexey Frunze4dda3372015-06-01 18:31:49 -07003294void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3295 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3296 LocationSummary* locations =
3297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3298 locations->SetInAt(0, Location::RequiresRegister());
3299 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3300 locations->SetOut(Location::RequiresFpuRegister());
3301 } else {
3302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3303 }
3304}
3305
3306void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3307 const FieldInfo& field_info) {
3308 Primitive::Type type = field_info.GetFieldType();
3309 LocationSummary* locations = instruction->GetLocations();
3310 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3311 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003312 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003313 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3314
Alexey Frunze4dda3372015-06-01 18:31:49 -07003315 switch (type) {
3316 case Primitive::kPrimBoolean:
3317 load_type = kLoadUnsignedByte;
3318 break;
3319 case Primitive::kPrimByte:
3320 load_type = kLoadSignedByte;
3321 break;
3322 case Primitive::kPrimShort:
3323 load_type = kLoadSignedHalfword;
3324 break;
3325 case Primitive::kPrimChar:
3326 load_type = kLoadUnsignedHalfword;
3327 break;
3328 case Primitive::kPrimInt:
3329 case Primitive::kPrimFloat:
3330 load_type = kLoadWord;
3331 break;
3332 case Primitive::kPrimLong:
3333 case Primitive::kPrimDouble:
3334 load_type = kLoadDoubleword;
3335 break;
3336 case Primitive::kPrimNot:
3337 load_type = kLoadUnsignedWord;
3338 break;
3339 case Primitive::kPrimVoid:
3340 LOG(FATAL) << "Unreachable type " << type;
3341 UNREACHABLE();
3342 }
3343 if (!Primitive::IsFloatingPointType(type)) {
3344 DCHECK(locations->Out().IsRegister());
3345 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003346 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003347 } else {
3348 DCHECK(locations->Out().IsFpuRegister());
3349 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003350 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003351 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003352 // TODO: memory barrier?
Alexey Frunzec061de12017-02-14 13:27:23 -08003353
3354 if (type == Primitive::kPrimNot) {
3355 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3356 __ MaybeUnpoisonHeapReference(dst);
3357 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003358}
3359
3360void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3361 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3362 LocationSummary* locations =
3363 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3364 locations->SetInAt(0, Location::RequiresRegister());
3365 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01003366 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003367 } else {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01003368 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003369 }
3370}
3371
3372void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003373 const FieldInfo& field_info,
3374 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003375 Primitive::Type type = field_info.GetFieldType();
3376 LocationSummary* locations = instruction->GetLocations();
3377 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01003378 Location value_location = locations->InAt(1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003379 StoreOperandType store_type = kStoreByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003380 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3381 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003382 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3383
Alexey Frunze4dda3372015-06-01 18:31:49 -07003384 switch (type) {
3385 case Primitive::kPrimBoolean:
3386 case Primitive::kPrimByte:
3387 store_type = kStoreByte;
3388 break;
3389 case Primitive::kPrimShort:
3390 case Primitive::kPrimChar:
3391 store_type = kStoreHalfword;
3392 break;
3393 case Primitive::kPrimInt:
3394 case Primitive::kPrimFloat:
3395 case Primitive::kPrimNot:
3396 store_type = kStoreWord;
3397 break;
3398 case Primitive::kPrimLong:
3399 case Primitive::kPrimDouble:
3400 store_type = kStoreDoubleword;
3401 break;
3402 case Primitive::kPrimVoid:
3403 LOG(FATAL) << "Unreachable type " << type;
3404 UNREACHABLE();
3405 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01003407 if (value_location.IsConstant()) {
3408 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3409 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
3410 } else {
3411 if (!Primitive::IsFloatingPointType(type)) {
3412 DCHECK(value_location.IsRegister());
3413 GpuRegister src = value_location.AsRegister<GpuRegister>();
3414 if (kPoisonHeapReferences && needs_write_barrier) {
3415 // Note that in the case where `value` is a null reference,
3416 // we do not enter this block, as a null reference does not
3417 // need poisoning.
3418 DCHECK_EQ(type, Primitive::kPrimNot);
3419 __ PoisonHeapReference(TMP, src);
3420 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
3421 } else {
3422 __ StoreToOffset(store_type, src, obj, offset, null_checker);
3423 }
3424 } else {
3425 DCHECK(value_location.IsFpuRegister());
3426 FpuRegister src = value_location.AsFpuRegister<FpuRegister>();
3427 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
3428 }
3429 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003430 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08003431 if (needs_write_barrier) {
Tijana Jakovljevicba89c342017-03-10 13:36:08 +01003432 DCHECK(value_location.IsRegister());
3433 GpuRegister src = value_location.AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003434 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003435 }
3436}
3437
3438void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3439 HandleFieldGet(instruction, instruction->GetFieldInfo());
3440}
3441
3442void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3443 HandleFieldGet(instruction, instruction->GetFieldInfo());
3444}
3445
3446void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3447 HandleFieldSet(instruction, instruction->GetFieldInfo());
3448}
3449
3450void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003451 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003452}
3453
Alexey Frunzef63f5692016-12-13 17:43:11 -08003454void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3455 HInstruction* instruction ATTRIBUTE_UNUSED,
3456 Location root,
3457 GpuRegister obj,
3458 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003459 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3460 if (kEmitCompilerReadBarrier) {
3461 UNIMPLEMENTED(FATAL) << "for read barrier";
3462 } else {
3463 // Plain GC root load with no read barrier.
3464 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3465 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3466 // Note that GC roots are not affected by heap poisoning, thus we
3467 // do not have to unpoison `root_reg` here.
3468 }
3469}
3470
Alexey Frunze4dda3372015-06-01 18:31:49 -07003471void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003472 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3473 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3474 switch (type_check_kind) {
3475 case TypeCheckKind::kExactCheck:
3476 case TypeCheckKind::kAbstractClassCheck:
3477 case TypeCheckKind::kClassHierarchyCheck:
3478 case TypeCheckKind::kArrayObjectCheck:
3479 call_kind = LocationSummary::kNoCall;
3480 break;
3481 case TypeCheckKind::kArrayCheck:
3482 case TypeCheckKind::kUnresolvedCheck:
3483 case TypeCheckKind::kInterfaceCheck:
3484 call_kind = LocationSummary::kCallOnSlowPath;
3485 break;
3486 }
3487
Alexey Frunze4dda3372015-06-01 18:31:49 -07003488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3489 locations->SetInAt(0, Location::RequiresRegister());
3490 locations->SetInAt(1, Location::RequiresRegister());
3491 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003492 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003493 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3494}
3495
3496void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003497 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003498 LocationSummary* locations = instruction->GetLocations();
3499 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3500 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3501 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003502 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3503 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3504 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3505 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003506 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003507 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508
3509 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003510 // Avoid this check if we know `obj` is not null.
3511 if (instruction->MustDoNullCheck()) {
3512 __ Move(out, ZERO);
3513 __ Beqzc(obj, &done);
3514 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003515
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003516 switch (type_check_kind) {
3517 case TypeCheckKind::kExactCheck: {
3518 // /* HeapReference<Class> */ out = obj->klass_
3519 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3520 __ MaybeUnpoisonHeapReference(out);
3521 // Classes must be equal for the instanceof to succeed.
3522 __ Xor(out, out, cls);
3523 __ Sltiu(out, out, 1);
3524 break;
3525 }
3526
3527 case TypeCheckKind::kAbstractClassCheck: {
3528 // /* HeapReference<Class> */ out = obj->klass_
3529 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3530 __ MaybeUnpoisonHeapReference(out);
3531 // If the class is abstract, we eagerly fetch the super class of the
3532 // object to avoid doing a comparison we know will fail.
3533 Mips64Label loop;
3534 __ Bind(&loop);
3535 // /* HeapReference<Class> */ out = out->super_class_
3536 __ LoadFromOffset(kLoadUnsignedWord, out, out, super_offset);
3537 __ MaybeUnpoisonHeapReference(out);
3538 // If `out` is null, we use it for the result, and jump to `done`.
3539 __ Beqzc(out, &done);
3540 __ Bnec(out, cls, &loop);
3541 __ LoadConst32(out, 1);
3542 break;
3543 }
3544
3545 case TypeCheckKind::kClassHierarchyCheck: {
3546 // /* HeapReference<Class> */ out = obj->klass_
3547 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3548 __ MaybeUnpoisonHeapReference(out);
3549 // Walk over the class hierarchy to find a match.
3550 Mips64Label loop, success;
3551 __ Bind(&loop);
3552 __ Beqc(out, cls, &success);
3553 // /* HeapReference<Class> */ out = out->super_class_
3554 __ LoadFromOffset(kLoadUnsignedWord, out, out, super_offset);
3555 __ MaybeUnpoisonHeapReference(out);
3556 __ Bnezc(out, &loop);
3557 // If `out` is null, we use it for the result, and jump to `done`.
3558 __ Bc(&done);
3559 __ Bind(&success);
3560 __ LoadConst32(out, 1);
3561 break;
3562 }
3563
3564 case TypeCheckKind::kArrayObjectCheck: {
3565 // /* HeapReference<Class> */ out = obj->klass_
3566 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3567 __ MaybeUnpoisonHeapReference(out);
3568 // Do an exact check.
3569 Mips64Label success;
3570 __ Beqc(out, cls, &success);
3571 // Otherwise, we need to check that the object's class is a non-primitive array.
3572 // /* HeapReference<Class> */ out = out->component_type_
3573 __ LoadFromOffset(kLoadUnsignedWord, out, out, component_offset);
3574 __ MaybeUnpoisonHeapReference(out);
3575 // If `out` is null, we use it for the result, and jump to `done`.
3576 __ Beqzc(out, &done);
3577 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
3578 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3579 __ Sltiu(out, out, 1);
3580 __ Bc(&done);
3581 __ Bind(&success);
3582 __ LoadConst32(out, 1);
3583 break;
3584 }
3585
3586 case TypeCheckKind::kArrayCheck: {
3587 // No read barrier since the slow path will retry upon failure.
3588 // /* HeapReference<Class> */ out = obj->klass_
3589 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3590 __ MaybeUnpoisonHeapReference(out);
3591 DCHECK(locations->OnlyCallsOnSlowPath());
3592 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
3593 /* is_fatal */ false);
3594 codegen_->AddSlowPath(slow_path);
3595 __ Bnec(out, cls, slow_path->GetEntryLabel());
3596 __ LoadConst32(out, 1);
3597 break;
3598 }
3599
3600 case TypeCheckKind::kUnresolvedCheck:
3601 case TypeCheckKind::kInterfaceCheck: {
3602 // Note that we indeed only call on slow path, but we always go
3603 // into the slow path for the unresolved and interface check
3604 // cases.
3605 //
3606 // We cannot directly call the InstanceofNonTrivial runtime
3607 // entry point without resorting to a type checking slow path
3608 // here (i.e. by calling InvokeRuntime directly), as it would
3609 // require to assign fixed registers for the inputs of this
3610 // HInstanceOf instruction (following the runtime calling
3611 // convention), which might be cluttered by the potential first
3612 // read barrier emission at the beginning of this method.
3613 //
3614 // TODO: Introduce a new runtime entry point taking the object
3615 // to test (instead of its class) as argument, and let it deal
3616 // with the read barrier issues. This will let us refactor this
3617 // case of the `switch` code as it was previously (with a direct
3618 // call to the runtime not using a type checking slow path).
3619 // This should also be beneficial for the other cases above.
3620 DCHECK(locations->OnlyCallsOnSlowPath());
3621 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
3622 /* is_fatal */ false);
3623 codegen_->AddSlowPath(slow_path);
3624 __ Bc(slow_path->GetEntryLabel());
3625 break;
3626 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003627 }
3628
3629 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003630
3631 if (slow_path != nullptr) {
3632 __ Bind(slow_path->GetExitLabel());
3633 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003634}
3635
3636void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3637 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3638 locations->SetOut(Location::ConstantLocation(constant));
3639}
3640
3641void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3642 // Will be generated at use site.
3643}
3644
3645void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3647 locations->SetOut(Location::ConstantLocation(constant));
3648}
3649
3650void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3651 // Will be generated at use site.
3652}
3653
Calin Juravle175dc732015-08-25 15:42:32 +01003654void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3655 // The trampoline uses the same calling convention as dex calling conventions,
3656 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3657 // the method_idx.
3658 HandleInvoke(invoke);
3659}
3660
3661void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3662 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3663}
3664
Alexey Frunze4dda3372015-06-01 18:31:49 -07003665void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3666 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3667 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3668}
3669
3670void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3671 HandleInvoke(invoke);
3672 // The register T0 is required to be used for the hidden argument in
3673 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3674 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3678 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3679 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003680 Location receiver = invoke->GetLocations()->InAt(0);
3681 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003682 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003683
3684 // Set the hidden argument.
3685 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3686 invoke->GetDexMethodIndex());
3687
3688 // temp = object->GetClass();
3689 if (receiver.IsStackSlot()) {
3690 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3691 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3692 } else {
3693 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3694 }
3695 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003696 // Instead of simply (possibly) unpoisoning `temp` here, we should
3697 // emit a read barrier for the previous class reference load.
3698 // However this is not required in practice, as this is an
3699 // intermediate/temporary reference and because the current
3700 // concurrent copying collector keeps the from-space memory
3701 // intact/accessible until the end of the marking phase (the
3702 // concurrent copying collector may not in the future).
3703 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003704 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3705 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3706 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003707 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003708 // temp = temp->GetImtEntryAt(method_offset);
3709 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3710 // T9 = temp->GetEntryPoint();
3711 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3712 // T9();
3713 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003714 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003715 DCHECK(!codegen_->IsLeafMethod());
3716 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3717}
3718
3719void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003720 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3721 if (intrinsic.TryDispatch(invoke)) {
3722 return;
3723 }
3724
Alexey Frunze4dda3372015-06-01 18:31:49 -07003725 HandleInvoke(invoke);
3726}
3727
3728void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003729 // Explicit clinit checks triggered by static invokes must have been pruned by
3730 // art::PrepareForRegisterAllocation.
3731 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003732
Chris Larsen3039e382015-08-26 07:54:08 -07003733 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3734 if (intrinsic.TryDispatch(invoke)) {
3735 return;
3736 }
3737
Alexey Frunze4dda3372015-06-01 18:31:49 -07003738 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003739}
3740
Orion Hodsonac141392017-01-13 11:53:47 +00003741void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3742 HandleInvoke(invoke);
3743}
3744
3745void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3746 codegen_->GenerateInvokePolymorphicCall(invoke);
3747}
3748
Chris Larsen3039e382015-08-26 07:54:08 -07003749static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003750 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003751 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3752 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003753 return true;
3754 }
3755 return false;
3756}
3757
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003758HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003759 HLoadString::LoadKind desired_string_load_kind) {
3760 if (kEmitCompilerReadBarrier) {
3761 UNIMPLEMENTED(FATAL) << "for read barrier";
3762 }
3763 bool fallback_load = false;
3764 switch (desired_string_load_kind) {
3765 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3766 DCHECK(!GetCompilerOptions().GetCompilePic());
3767 break;
3768 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3769 DCHECK(GetCompilerOptions().GetCompilePic());
3770 break;
3771 case HLoadString::LoadKind::kBootImageAddress:
3772 break;
3773 case HLoadString::LoadKind::kBssEntry:
3774 DCHECK(!Runtime::Current()->UseJitCompilation());
3775 break;
3776 case HLoadString::LoadKind::kDexCacheViaMethod:
3777 break;
3778 case HLoadString::LoadKind::kJitTableAddress:
3779 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003780 break;
3781 }
3782 if (fallback_load) {
3783 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3784 }
3785 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003786}
3787
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003788HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3789 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003790 if (kEmitCompilerReadBarrier) {
3791 UNIMPLEMENTED(FATAL) << "for read barrier";
3792 }
3793 bool fallback_load = false;
3794 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003795 case HLoadClass::LoadKind::kInvalid:
3796 LOG(FATAL) << "UNREACHABLE";
3797 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003798 case HLoadClass::LoadKind::kReferrersClass:
3799 break;
3800 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3801 DCHECK(!GetCompilerOptions().GetCompilePic());
3802 break;
3803 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3804 DCHECK(GetCompilerOptions().GetCompilePic());
3805 break;
3806 case HLoadClass::LoadKind::kBootImageAddress:
3807 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003808 case HLoadClass::LoadKind::kBssEntry:
3809 DCHECK(!Runtime::Current()->UseJitCompilation());
3810 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003811 case HLoadClass::LoadKind::kJitTableAddress:
3812 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003813 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003814 case HLoadClass::LoadKind::kDexCacheViaMethod:
3815 break;
3816 }
3817 if (fallback_load) {
3818 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3819 }
3820 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003821}
3822
Vladimir Markodc151b22015-10-15 18:02:30 +01003823HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3824 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003825 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003826 // On MIPS64 we support all dispatch types.
3827 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003828}
3829
Alexey Frunze4dda3372015-06-01 18:31:49 -07003830void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3831 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003832 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003833 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3834 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3835
Alexey Frunze19f6c692016-11-30 19:19:55 -08003836 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003837 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003838 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003839 uint32_t offset =
3840 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003841 __ LoadFromOffset(kLoadDoubleword,
3842 temp.AsRegister<GpuRegister>(),
3843 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003844 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003845 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003846 }
Vladimir Marko58155012015-08-19 12:49:41 +00003847 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003848 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003849 break;
3850 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003851 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3852 kLoadDoubleword,
3853 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003854 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003855 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3856 uint32_t offset = invoke->GetDexCacheArrayOffset();
3857 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003858 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003859 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3860 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3861 break;
3862 }
Vladimir Marko58155012015-08-19 12:49:41 +00003863 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003864 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003865 GpuRegister reg = temp.AsRegister<GpuRegister>();
3866 GpuRegister method_reg;
3867 if (current_method.IsRegister()) {
3868 method_reg = current_method.AsRegister<GpuRegister>();
3869 } else {
3870 // TODO: use the appropriate DCHECK() here if possible.
3871 // DCHECK(invoke->GetLocations()->Intrinsified());
3872 DCHECK(!current_method.IsValid());
3873 method_reg = reg;
3874 __ Ld(reg, SP, kCurrentMethodStackOffset);
3875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003876
Vladimir Marko58155012015-08-19 12:49:41 +00003877 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003878 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003879 reg,
3880 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003881 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003882 // temp = temp[index_in_cache];
3883 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3884 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003885 __ LoadFromOffset(kLoadDoubleword,
3886 reg,
3887 reg,
3888 CodeGenerator::GetCachePointerOffset(index_in_cache));
3889 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003890 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003891 }
3892
Alexey Frunze19f6c692016-11-30 19:19:55 -08003893 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003894 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003895 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003896 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003897 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3898 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3899 __ LoadFromOffset(kLoadDoubleword,
3900 T9,
3901 callee_method.AsRegister<GpuRegister>(),
3902 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003903 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003904 // T9()
3905 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003906 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003907 break;
3908 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003909 DCHECK(!IsLeafMethod());
3910}
3911
3912void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003913 // Explicit clinit checks triggered by static invokes must have been pruned by
3914 // art::PrepareForRegisterAllocation.
3915 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003916
3917 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3918 return;
3919 }
3920
3921 LocationSummary* locations = invoke->GetLocations();
3922 codegen_->GenerateStaticOrDirectCall(invoke,
3923 locations->HasTemps()
3924 ? locations->GetTemp(0)
3925 : Location::NoLocation());
3926 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3927}
3928
Alexey Frunze53afca12015-11-05 16:34:23 -08003929void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003930 // Use the calling convention instead of the location of the receiver, as
3931 // intrinsics may have put the receiver in a different register. In the intrinsics
3932 // slow path, the arguments have been moved to the right place, so here we are
3933 // guaranteed that the receiver is the first register of the calling convention.
3934 InvokeDexCallingConvention calling_convention;
3935 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3936
Alexey Frunze53afca12015-11-05 16:34:23 -08003937 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003938 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3939 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3940 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003941 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003942
3943 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003944 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003945 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003946 // Instead of simply (possibly) unpoisoning `temp` here, we should
3947 // emit a read barrier for the previous class reference load.
3948 // However this is not required in practice, as this is an
3949 // intermediate/temporary reference and because the current
3950 // concurrent copying collector keeps the from-space memory
3951 // intact/accessible until the end of the marking phase (the
3952 // concurrent copying collector may not in the future).
3953 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003954 // temp = temp->GetMethodAt(method_offset);
3955 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3956 // T9 = temp->GetEntryPoint();
3957 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3958 // T9();
3959 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003960 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003961}
3962
3963void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3964 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3965 return;
3966 }
3967
3968 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003969 DCHECK(!codegen_->IsLeafMethod());
3970 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3971}
3972
3973void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003974 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3975 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003976 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003977 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003978 cls,
3979 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003980 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003981 return;
3982 }
Vladimir Marko41559982017-01-06 14:04:23 +00003983 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003984
3985 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3986 ? LocationSummary::kCallOnSlowPath
3987 : LocationSummary::kNoCall;
3988 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003989 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003990 locations->SetInAt(0, Location::RequiresRegister());
3991 }
3992 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003993}
3994
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003995// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3996// move.
3997void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003998 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3999 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4000 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004001 return;
4002 }
Vladimir Marko41559982017-01-06 14:04:23 +00004003 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004004
Vladimir Marko41559982017-01-06 14:04:23 +00004005 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004006 Location out_loc = locations->Out();
4007 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4008 GpuRegister current_method_reg = ZERO;
4009 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4010 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4011 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
4012 }
4013
4014 bool generate_null_check = false;
4015 switch (load_kind) {
4016 case HLoadClass::LoadKind::kReferrersClass:
4017 DCHECK(!cls->CanCallRuntime());
4018 DCHECK(!cls->MustGenerateClinitCheck());
4019 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4020 GenerateGcRootFieldLoad(cls,
4021 out_loc,
4022 current_method_reg,
4023 ArtMethod::DeclaringClassOffset().Int32Value());
4024 break;
4025 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004026 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004027 __ LoadLiteral(out,
4028 kLoadUnsignedWord,
4029 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4030 cls->GetTypeIndex()));
4031 break;
4032 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004033 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004034 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
4035 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
4036 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
4037 __ Daddiu(out, AT, /* placeholder */ 0x5678);
4038 break;
4039 }
4040 case HLoadClass::LoadKind::kBootImageAddress: {
4041 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004042 uint32_t address = dchecked_integral_cast<uint32_t>(
4043 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4044 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004045 __ LoadLiteral(out,
4046 kLoadUnsignedWord,
4047 codegen_->DeduplicateBootImageAddressLiteral(address));
4048 break;
4049 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004050 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004051 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004052 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08004053 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
4054 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004055 generate_null_check = true;
4056 break;
4057 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08004058 case HLoadClass::LoadKind::kJitTableAddress:
4059 __ LoadLiteral(out,
4060 kLoadUnsignedWord,
4061 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4062 cls->GetTypeIndex(),
4063 cls->GetClass()));
4064 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004065 break;
Vladimir Marko41559982017-01-06 14:04:23 +00004066 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004067 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004068 LOG(FATAL) << "UNREACHABLE";
4069 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004070 }
4071
4072 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4073 DCHECK(cls->CanCallRuntime());
4074 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
4075 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4076 codegen_->AddSlowPath(slow_path);
4077 if (generate_null_check) {
4078 __ Beqzc(out, slow_path->GetEntryLabel());
4079 }
4080 if (cls->MustGenerateClinitCheck()) {
4081 GenerateClassInitializationCheck(slow_path, out);
4082 } else {
4083 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004084 }
4085 }
4086}
4087
David Brazdilcb1c0552015-08-04 16:22:25 +01004088static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004089 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01004090}
4091
Alexey Frunze4dda3372015-06-01 18:31:49 -07004092void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
4093 LocationSummary* locations =
4094 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4095 locations->SetOut(Location::RequiresRegister());
4096}
4097
4098void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
4099 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004100 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
4101}
4102
4103void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
4104 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4105}
4106
4107void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4108 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109}
4110
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004112 HLoadString::LoadKind load_kind = load->GetLoadKind();
4113 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004115 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
4116 InvokeRuntimeCallingConvention calling_convention;
4117 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4118 } else {
4119 locations->SetOut(Location::RequiresRegister());
4120 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004121}
4122
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004123// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4124// move.
4125void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004126 HLoadString::LoadKind load_kind = load->GetLoadKind();
4127 LocationSummary* locations = load->GetLocations();
4128 Location out_loc = locations->Out();
4129 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4130
4131 switch (load_kind) {
4132 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004133 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004134 __ LoadLiteral(out,
4135 kLoadUnsignedWord,
4136 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4137 load->GetStringIndex()));
4138 return; // No dex cache slow path.
4139 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4140 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
4141 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004142 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004143 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
4144 __ Daddiu(out, AT, /* placeholder */ 0x5678);
4145 return; // No dex cache slow path.
4146 }
4147 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004148 uint32_t address = dchecked_integral_cast<uint32_t>(
4149 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4150 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004151 __ LoadLiteral(out,
4152 kLoadUnsignedWord,
4153 codegen_->DeduplicateBootImageAddressLiteral(address));
4154 return; // No dex cache slow path.
4155 }
4156 case HLoadString::LoadKind::kBssEntry: {
4157 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4158 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004159 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08004160 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
4161 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004162 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
4163 codegen_->AddSlowPath(slow_path);
4164 __ Beqzc(out, slow_path->GetEntryLabel());
4165 __ Bind(slow_path->GetExitLabel());
4166 return;
4167 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08004168 case HLoadString::LoadKind::kJitTableAddress:
4169 __ LoadLiteral(out,
4170 kLoadUnsignedWord,
4171 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4172 load->GetStringIndex(),
4173 load->GetString()));
4174 GenerateGcRootFieldLoad(load, out_loc, out, 0);
4175 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004176 default:
4177 break;
4178 }
4179
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004180 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08004181 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4182 InvokeRuntimeCallingConvention calling_convention;
4183 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
4184 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4185 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004186}
4187
Alexey Frunze4dda3372015-06-01 18:31:49 -07004188void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
4189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4190 locations->SetOut(Location::ConstantLocation(constant));
4191}
4192
4193void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4194 // Will be generated at use site.
4195}
4196
4197void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
4198 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004199 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200 InvokeRuntimeCallingConvention calling_convention;
4201 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4202}
4203
4204void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004205 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004206 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01004207 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004208 if (instruction->IsEnter()) {
4209 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4210 } else {
4211 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4212 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004213}
4214
4215void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
4216 LocationSummary* locations =
4217 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4218 switch (mul->GetResultType()) {
4219 case Primitive::kPrimInt:
4220 case Primitive::kPrimLong:
4221 locations->SetInAt(0, Location::RequiresRegister());
4222 locations->SetInAt(1, Location::RequiresRegister());
4223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4224 break;
4225
4226 case Primitive::kPrimFloat:
4227 case Primitive::kPrimDouble:
4228 locations->SetInAt(0, Location::RequiresFpuRegister());
4229 locations->SetInAt(1, Location::RequiresFpuRegister());
4230 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4231 break;
4232
4233 default:
4234 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4235 }
4236}
4237
4238void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
4239 Primitive::Type type = instruction->GetType();
4240 LocationSummary* locations = instruction->GetLocations();
4241
4242 switch (type) {
4243 case Primitive::kPrimInt:
4244 case Primitive::kPrimLong: {
4245 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4246 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
4247 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
4248 if (type == Primitive::kPrimInt)
4249 __ MulR6(dst, lhs, rhs);
4250 else
4251 __ Dmul(dst, lhs, rhs);
4252 break;
4253 }
4254 case Primitive::kPrimFloat:
4255 case Primitive::kPrimDouble: {
4256 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4257 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4258 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
4259 if (type == Primitive::kPrimFloat)
4260 __ MulS(dst, lhs, rhs);
4261 else
4262 __ MulD(dst, lhs, rhs);
4263 break;
4264 }
4265 default:
4266 LOG(FATAL) << "Unexpected mul type " << type;
4267 }
4268}
4269
4270void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
4271 LocationSummary* locations =
4272 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4273 switch (neg->GetResultType()) {
4274 case Primitive::kPrimInt:
4275 case Primitive::kPrimLong:
4276 locations->SetInAt(0, Location::RequiresRegister());
4277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4278 break;
4279
4280 case Primitive::kPrimFloat:
4281 case Primitive::kPrimDouble:
4282 locations->SetInAt(0, Location::RequiresFpuRegister());
4283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4284 break;
4285
4286 default:
4287 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4288 }
4289}
4290
4291void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
4292 Primitive::Type type = instruction->GetType();
4293 LocationSummary* locations = instruction->GetLocations();
4294
4295 switch (type) {
4296 case Primitive::kPrimInt:
4297 case Primitive::kPrimLong: {
4298 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4299 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4300 if (type == Primitive::kPrimInt)
4301 __ Subu(dst, ZERO, src);
4302 else
4303 __ Dsubu(dst, ZERO, src);
4304 break;
4305 }
4306 case Primitive::kPrimFloat:
4307 case Primitive::kPrimDouble: {
4308 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4309 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4310 if (type == Primitive::kPrimFloat)
4311 __ NegS(dst, src);
4312 else
4313 __ NegD(dst, src);
4314 break;
4315 }
4316 default:
4317 LOG(FATAL) << "Unexpected neg type " << type;
4318 }
4319}
4320
4321void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
4322 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004324 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004325 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004326 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4327 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004328}
4329
4330void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004331 // Note: if heap poisoning is enabled, the entry point takes care
4332 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004333 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4334 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004335}
4336
4337void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
4338 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004340 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004341 if (instruction->IsStringAlloc()) {
4342 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4343 } else {
4344 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004345 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004346 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4347}
4348
4349void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004350 // Note: if heap poisoning is enabled, the entry point takes care
4351 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004352 if (instruction->IsStringAlloc()) {
4353 // String is allocated through StringFactory. Call NewEmptyString entry point.
4354 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02004355 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07004356 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004357 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4358 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
4359 __ Jalr(T9);
4360 __ Nop();
4361 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4362 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01004363 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004364 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004365 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004366}
4367
4368void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
4369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4372}
4373
4374void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
4375 Primitive::Type type = instruction->GetType();
4376 LocationSummary* locations = instruction->GetLocations();
4377
4378 switch (type) {
4379 case Primitive::kPrimInt:
4380 case Primitive::kPrimLong: {
4381 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4382 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4383 __ Nor(dst, src, ZERO);
4384 break;
4385 }
4386
4387 default:
4388 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4389 }
4390}
4391
4392void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4394 locations->SetInAt(0, Location::RequiresRegister());
4395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4396}
4397
4398void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4399 LocationSummary* locations = instruction->GetLocations();
4400 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4401 locations->InAt(0).AsRegister<GpuRegister>(),
4402 1);
4403}
4404
4405void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004406 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4407 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004408}
4409
Calin Juravle2ae48182016-03-16 14:05:09 +00004410void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4411 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004412 return;
4413 }
4414 Location obj = instruction->GetLocations()->InAt(0);
4415
4416 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004417 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004418}
4419
Calin Juravle2ae48182016-03-16 14:05:09 +00004420void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004421 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004422 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004423
4424 Location obj = instruction->GetLocations()->InAt(0);
4425
4426 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4427}
4428
4429void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004430 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004431}
4432
4433void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4434 HandleBinaryOp(instruction);
4435}
4436
4437void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4438 HandleBinaryOp(instruction);
4439}
4440
4441void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4442 LOG(FATAL) << "Unreachable";
4443}
4444
4445void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4446 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4447}
4448
4449void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4451 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4452 if (location.IsStackSlot()) {
4453 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4454 } else if (location.IsDoubleStackSlot()) {
4455 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4456 }
4457 locations->SetOut(location);
4458}
4459
4460void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4461 ATTRIBUTE_UNUSED) {
4462 // Nothing to do, the parameter is already at its location.
4463}
4464
4465void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4466 LocationSummary* locations =
4467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4468 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4469}
4470
4471void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4472 ATTRIBUTE_UNUSED) {
4473 // Nothing to do, the method is already at its location.
4474}
4475
4476void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004478 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004479 locations->SetInAt(i, Location::Any());
4480 }
4481 locations->SetOut(Location::Any());
4482}
4483
4484void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4485 LOG(FATAL) << "Unreachable";
4486}
4487
4488void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4489 Primitive::Type type = rem->GetResultType();
4490 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004491 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4492 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4494
4495 switch (type) {
4496 case Primitive::kPrimInt:
4497 case Primitive::kPrimLong:
4498 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004499 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4501 break;
4502
4503 case Primitive::kPrimFloat:
4504 case Primitive::kPrimDouble: {
4505 InvokeRuntimeCallingConvention calling_convention;
4506 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4507 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4508 locations->SetOut(calling_convention.GetReturnLocation(type));
4509 break;
4510 }
4511
4512 default:
4513 LOG(FATAL) << "Unexpected rem type " << type;
4514 }
4515}
4516
4517void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4518 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004519
4520 switch (type) {
4521 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004522 case Primitive::kPrimLong:
4523 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004524 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004525
4526 case Primitive::kPrimFloat:
4527 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004528 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4529 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004530 if (type == Primitive::kPrimFloat) {
4531 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4532 } else {
4533 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4534 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004535 break;
4536 }
4537 default:
4538 LOG(FATAL) << "Unexpected rem type " << type;
4539 }
4540}
4541
4542void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4543 memory_barrier->SetLocations(nullptr);
4544}
4545
4546void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4547 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4548}
4549
4550void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4552 Primitive::Type return_type = ret->InputAt(0)->GetType();
4553 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4554}
4555
4556void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4557 codegen_->GenerateFrameExit();
4558}
4559
4560void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4561 ret->SetLocations(nullptr);
4562}
4563
4564void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4565 codegen_->GenerateFrameExit();
4566}
4567
Alexey Frunze92d90602015-12-18 18:16:36 -08004568void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4569 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004570}
4571
Alexey Frunze92d90602015-12-18 18:16:36 -08004572void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4573 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004574}
4575
Alexey Frunze4dda3372015-06-01 18:31:49 -07004576void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4577 HandleShift(shl);
4578}
4579
4580void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4581 HandleShift(shl);
4582}
4583
4584void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4585 HandleShift(shr);
4586}
4587
4588void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4589 HandleShift(shr);
4590}
4591
Alexey Frunze4dda3372015-06-01 18:31:49 -07004592void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4593 HandleBinaryOp(instruction);
4594}
4595
4596void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4597 HandleBinaryOp(instruction);
4598}
4599
4600void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4601 HandleFieldGet(instruction, instruction->GetFieldInfo());
4602}
4603
4604void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4605 HandleFieldGet(instruction, instruction->GetFieldInfo());
4606}
4607
4608void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4609 HandleFieldSet(instruction, instruction->GetFieldInfo());
4610}
4611
4612void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004613 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004614}
4615
Calin Juravlee460d1d2015-09-29 04:52:17 +01004616void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4617 HUnresolvedInstanceFieldGet* instruction) {
4618 FieldAccessCallingConventionMIPS64 calling_convention;
4619 codegen_->CreateUnresolvedFieldLocationSummary(
4620 instruction, instruction->GetFieldType(), calling_convention);
4621}
4622
4623void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4624 HUnresolvedInstanceFieldGet* instruction) {
4625 FieldAccessCallingConventionMIPS64 calling_convention;
4626 codegen_->GenerateUnresolvedFieldAccess(instruction,
4627 instruction->GetFieldType(),
4628 instruction->GetFieldIndex(),
4629 instruction->GetDexPc(),
4630 calling_convention);
4631}
4632
4633void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4634 HUnresolvedInstanceFieldSet* instruction) {
4635 FieldAccessCallingConventionMIPS64 calling_convention;
4636 codegen_->CreateUnresolvedFieldLocationSummary(
4637 instruction, instruction->GetFieldType(), calling_convention);
4638}
4639
4640void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4641 HUnresolvedInstanceFieldSet* instruction) {
4642 FieldAccessCallingConventionMIPS64 calling_convention;
4643 codegen_->GenerateUnresolvedFieldAccess(instruction,
4644 instruction->GetFieldType(),
4645 instruction->GetFieldIndex(),
4646 instruction->GetDexPc(),
4647 calling_convention);
4648}
4649
4650void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4651 HUnresolvedStaticFieldGet* instruction) {
4652 FieldAccessCallingConventionMIPS64 calling_convention;
4653 codegen_->CreateUnresolvedFieldLocationSummary(
4654 instruction, instruction->GetFieldType(), calling_convention);
4655}
4656
4657void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4658 HUnresolvedStaticFieldGet* instruction) {
4659 FieldAccessCallingConventionMIPS64 calling_convention;
4660 codegen_->GenerateUnresolvedFieldAccess(instruction,
4661 instruction->GetFieldType(),
4662 instruction->GetFieldIndex(),
4663 instruction->GetDexPc(),
4664 calling_convention);
4665}
4666
4667void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4668 HUnresolvedStaticFieldSet* instruction) {
4669 FieldAccessCallingConventionMIPS64 calling_convention;
4670 codegen_->CreateUnresolvedFieldLocationSummary(
4671 instruction, instruction->GetFieldType(), calling_convention);
4672}
4673
4674void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4675 HUnresolvedStaticFieldSet* instruction) {
4676 FieldAccessCallingConventionMIPS64 calling_convention;
4677 codegen_->GenerateUnresolvedFieldAccess(instruction,
4678 instruction->GetFieldType(),
4679 instruction->GetFieldIndex(),
4680 instruction->GetDexPc(),
4681 calling_convention);
4682}
4683
Alexey Frunze4dda3372015-06-01 18:31:49 -07004684void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004685 LocationSummary* locations =
4686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004687 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004688}
4689
4690void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4691 HBasicBlock* block = instruction->GetBlock();
4692 if (block->GetLoopInformation() != nullptr) {
4693 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4694 // The back edge will generate the suspend check.
4695 return;
4696 }
4697 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4698 // The goto will generate the suspend check.
4699 return;
4700 }
4701 GenerateSuspendCheck(instruction, nullptr);
4702}
4703
Alexey Frunze4dda3372015-06-01 18:31:49 -07004704void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4705 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004707 InvokeRuntimeCallingConvention calling_convention;
4708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4709}
4710
4711void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004712 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004713 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4714}
4715
4716void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4717 Primitive::Type input_type = conversion->GetInputType();
4718 Primitive::Type result_type = conversion->GetResultType();
4719 DCHECK_NE(input_type, result_type);
4720
4721 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4722 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4723 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4724 }
4725
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4727
4728 if (Primitive::IsFloatingPointType(input_type)) {
4729 locations->SetInAt(0, Location::RequiresFpuRegister());
4730 } else {
4731 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004732 }
4733
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004734 if (Primitive::IsFloatingPointType(result_type)) {
4735 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004736 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004738 }
4739}
4740
4741void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4742 LocationSummary* locations = conversion->GetLocations();
4743 Primitive::Type result_type = conversion->GetResultType();
4744 Primitive::Type input_type = conversion->GetInputType();
4745
4746 DCHECK_NE(input_type, result_type);
4747
4748 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4749 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4750 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4751
4752 switch (result_type) {
4753 case Primitive::kPrimChar:
4754 __ Andi(dst, src, 0xFFFF);
4755 break;
4756 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004757 if (input_type == Primitive::kPrimLong) {
4758 // Type conversion from long to types narrower than int is a result of code
4759 // transformations. To avoid unpredictable results for SEB and SEH, we first
4760 // need to sign-extend the low 32-bit value into bits 32 through 63.
4761 __ Sll(dst, src, 0);
4762 __ Seb(dst, dst);
4763 } else {
4764 __ Seb(dst, src);
4765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 break;
4767 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004768 if (input_type == Primitive::kPrimLong) {
4769 // Type conversion from long to types narrower than int is a result of code
4770 // transformations. To avoid unpredictable results for SEB and SEH, we first
4771 // need to sign-extend the low 32-bit value into bits 32 through 63.
4772 __ Sll(dst, src, 0);
4773 __ Seh(dst, dst);
4774 } else {
4775 __ Seh(dst, src);
4776 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004777 break;
4778 case Primitive::kPrimInt:
4779 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004780 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4781 // conversions, except when the input and output registers are the same and we are not
4782 // converting longs to shorter types. In these cases, do nothing.
4783 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4784 __ Sll(dst, src, 0);
4785 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004786 break;
4787
4788 default:
4789 LOG(FATAL) << "Unexpected type conversion from " << input_type
4790 << " to " << result_type;
4791 }
4792 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004793 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4794 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4795 if (input_type == Primitive::kPrimLong) {
4796 __ Dmtc1(src, FTMP);
4797 if (result_type == Primitive::kPrimFloat) {
4798 __ Cvtsl(dst, FTMP);
4799 } else {
4800 __ Cvtdl(dst, FTMP);
4801 }
4802 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004803 __ Mtc1(src, FTMP);
4804 if (result_type == Primitive::kPrimFloat) {
4805 __ Cvtsw(dst, FTMP);
4806 } else {
4807 __ Cvtdw(dst, FTMP);
4808 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004809 }
4810 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4811 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004812 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4813 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4814 Mips64Label truncate;
4815 Mips64Label done;
4816
4817 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4818 // value when the input is either a NaN or is outside of the range of the output type
4819 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4820 // the same result.
4821 //
4822 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4823 // value of the output type if the input is outside of the range after the truncation or
4824 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4825 // results. This matches the desired float/double-to-int/long conversion exactly.
4826 //
4827 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4828 //
4829 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4830 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4831 // even though it must be NAN2008=1 on R6.
4832 //
4833 // The code takes care of the different behaviors by first comparing the input to the
4834 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4835 // If the input is greater than or equal to the minimum, it procedes to the truncate
4836 // instruction, which will handle such an input the same way irrespective of NAN2008.
4837 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4838 // in order to return either zero or the minimum value.
4839 //
4840 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4841 // truncate instruction for MIPS64R6.
4842 if (input_type == Primitive::kPrimFloat) {
4843 uint32_t min_val = (result_type == Primitive::kPrimLong)
4844 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4845 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4846 __ LoadConst32(TMP, min_val);
4847 __ Mtc1(TMP, FTMP);
4848 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004849 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004850 uint64_t min_val = (result_type == Primitive::kPrimLong)
4851 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4852 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4853 __ LoadConst64(TMP, min_val);
4854 __ Dmtc1(TMP, FTMP);
4855 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004856 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004857
4858 __ Bc1nez(FTMP, &truncate);
4859
4860 if (input_type == Primitive::kPrimFloat) {
4861 __ CmpEqS(FTMP, src, src);
4862 } else {
4863 __ CmpEqD(FTMP, src, src);
4864 }
4865 if (result_type == Primitive::kPrimLong) {
4866 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4867 } else {
4868 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4869 }
4870 __ Mfc1(TMP, FTMP);
4871 __ And(dst, dst, TMP);
4872
4873 __ Bc(&done);
4874
4875 __ Bind(&truncate);
4876
4877 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004878 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004879 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004880 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004881 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004882 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004883 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004884 } else {
4885 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004886 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004887 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004888 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004889 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004890 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004891 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004892
4893 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004894 } else if (Primitive::IsFloatingPointType(result_type) &&
4895 Primitive::IsFloatingPointType(input_type)) {
4896 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4897 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4898 if (result_type == Primitive::kPrimFloat) {
4899 __ Cvtsd(dst, src);
4900 } else {
4901 __ Cvtds(dst, src);
4902 }
4903 } else {
4904 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4905 << " to " << result_type;
4906 }
4907}
4908
4909void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4910 HandleShift(ushr);
4911}
4912
4913void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4914 HandleShift(ushr);
4915}
4916
4917void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4918 HandleBinaryOp(instruction);
4919}
4920
4921void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4922 HandleBinaryOp(instruction);
4923}
4924
4925void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4926 // Nothing to do, this should be removed during prepare for register allocator.
4927 LOG(FATAL) << "Unreachable";
4928}
4929
4930void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4931 // Nothing to do, this should be removed during prepare for register allocator.
4932 LOG(FATAL) << "Unreachable";
4933}
4934
4935void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004936 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004937}
4938
4939void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004940 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004941}
4942
4943void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004944 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004945}
4946
4947void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004948 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004949}
4950
4951void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004952 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004953}
4954
4955void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004956 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004957}
4958
4959void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004960 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004961}
4962
4963void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004964 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004965}
4966
4967void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004968 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004969}
4970
4971void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004972 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004973}
4974
4975void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004976 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004977}
4978
4979void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004980 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004981}
4982
Aart Bike9f37602015-10-09 11:15:55 -07004983void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004984 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004985}
4986
4987void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004988 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004989}
4990
4991void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004993}
4994
4995void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004997}
4998
4999void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07005001}
5002
5003void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07005005}
5006
5007void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07005009}
5010
5011void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07005013}
5014
Mark Mendellfe57faa2015-09-18 09:26:15 -04005015// Simple implementation of packed switch - generate cascaded compare/jumps.
5016void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5017 LocationSummary* locations =
5018 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5019 locations->SetInAt(0, Location::RequiresRegister());
5020}
5021
Alexey Frunze0960ac52016-12-20 17:24:59 -08005022void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
5023 int32_t lower_bound,
5024 uint32_t num_entries,
5025 HBasicBlock* switch_block,
5026 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005027 // Create a set of compare/jumps.
5028 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08005029 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005030 // Jump to default if index is negative
5031 // Note: We don't check the case that index is positive while value < lower_bound, because in
5032 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5033 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
5034
Alexey Frunze0960ac52016-12-20 17:24:59 -08005035 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005036 // Jump to successors[0] if value == lower_bound.
5037 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
5038 int32_t last_index = 0;
5039 for (; num_entries - last_index > 2; last_index += 2) {
5040 __ Addiu(temp_reg, temp_reg, -2);
5041 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5042 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5043 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5044 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5045 }
5046 if (num_entries - last_index == 2) {
5047 // The last missing case_value.
5048 __ Addiu(temp_reg, temp_reg, -1);
5049 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005050 }
5051
5052 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08005053 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005054 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005055 }
5056}
5057
Alexey Frunze0960ac52016-12-20 17:24:59 -08005058void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
5059 int32_t lower_bound,
5060 uint32_t num_entries,
5061 HBasicBlock* switch_block,
5062 HBasicBlock* default_block) {
5063 // Create a jump table.
5064 std::vector<Mips64Label*> labels(num_entries);
5065 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5066 for (uint32_t i = 0; i < num_entries; i++) {
5067 labels[i] = codegen_->GetLabelOf(successors[i]);
5068 }
5069 JumpTable* table = __ CreateJumpTable(std::move(labels));
5070
5071 // Is the value in range?
5072 __ Addiu32(TMP, value_reg, -lower_bound);
5073 __ LoadConst32(AT, num_entries);
5074 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
5075
5076 // We are in the range of the table.
5077 // Load the target address from the jump table, indexing by the value.
5078 __ LoadLabelAddress(AT, table->GetLabel());
5079 __ Sll(TMP, TMP, 2);
5080 __ Daddu(TMP, TMP, AT);
5081 __ Lw(TMP, TMP, 0);
5082 // Compute the absolute target address by adding the table start address
5083 // (the table contains offsets to targets relative to its start).
5084 __ Daddu(TMP, TMP, AT);
5085 // And jump.
5086 __ Jr(TMP);
5087 __ Nop();
5088}
5089
5090void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5091 int32_t lower_bound = switch_instr->GetStartValue();
5092 uint32_t num_entries = switch_instr->GetNumEntries();
5093 LocationSummary* locations = switch_instr->GetLocations();
5094 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
5095 HBasicBlock* switch_block = switch_instr->GetBlock();
5096 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5097
5098 if (num_entries > kPackedSwitchJumpTableThreshold) {
5099 GenTableBasedPackedSwitch(value_reg,
5100 lower_bound,
5101 num_entries,
5102 switch_block,
5103 default_block);
5104 } else {
5105 GenPackedSwitchWithCompares(value_reg,
5106 lower_bound,
5107 num_entries,
5108 switch_block,
5109 default_block);
5110 }
5111}
5112
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005113void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
5114 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
5115}
5116
5117void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
5118 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
5119}
5120
Alexey Frunze4dda3372015-06-01 18:31:49 -07005121} // namespace mips64
5122} // namespace art