blob: 02c3ad6e39eab657baf017371bd982d4bd6b0c44 [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
1684void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001685 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1687 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001688 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001689 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001690 InvokeRuntimeCallingConvention calling_convention;
1691 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1692 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1693 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1694 } else {
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1697 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1698 locations->SetInAt(2, Location::RequiresFpuRegister());
1699 } else {
1700 locations->SetInAt(2, Location::RequiresRegister());
1701 }
1702 }
1703}
1704
1705void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1706 LocationSummary* locations = instruction->GetLocations();
1707 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1708 Location index = locations->InAt(1);
1709 Primitive::Type value_type = instruction->GetComponentType();
1710 bool needs_runtime_call = locations->WillCall();
1711 bool needs_write_barrier =
1712 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001713 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001714
1715 switch (value_type) {
1716 case Primitive::kPrimBoolean:
1717 case Primitive::kPrimByte: {
1718 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1719 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1720 if (index.IsConstant()) {
1721 size_t offset =
1722 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001723 __ StoreToOffset(kStoreByte, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 } else {
1725 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001726 __ StoreToOffset(kStoreByte, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 }
1728 break;
1729 }
1730
1731 case Primitive::kPrimShort:
1732 case Primitive::kPrimChar: {
1733 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1734 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1735 if (index.IsConstant()) {
1736 size_t offset =
1737 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001738 __ StoreToOffset(kStoreHalfword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001739 } else {
1740 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1741 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001742 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743 }
1744 break;
1745 }
1746
1747 case Primitive::kPrimInt:
1748 case Primitive::kPrimNot: {
1749 if (!needs_runtime_call) {
1750 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08001751 GpuRegister base_reg;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001752 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1753 if (index.IsConstant()) {
Alexey Frunzec061de12017-02-14 13:27:23 -08001754 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
1755 base_reg = obj;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756 } else {
1757 DCHECK(index.IsRegister()) << index;
1758 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1759 __ Daddu(TMP, obj, TMP);
Alexey Frunzec061de12017-02-14 13:27:23 -08001760 base_reg = TMP;
1761 }
1762 if (kPoisonHeapReferences && needs_write_barrier) {
1763 // Note that in the case where `value` is a null reference,
1764 // we do not enter this block, as a null reference does not
1765 // need poisoning.
1766 DCHECK_EQ(value_type, Primitive::kPrimNot);
1767 // Use Sw() instead of StoreToOffset() in order to be able to
1768 // hold the poisoned reference in AT and thus avoid allocating
1769 // yet another temporary register.
1770 if (index.IsConstant()) {
1771 if (!IsInt<16>(static_cast<int32_t>(data_offset))) {
1772 int16_t low16 = Low16Bits(data_offset);
1773 // For consistency with StoreToOffset() and such treat data_offset as int32_t.
1774 uint64_t high48 = static_cast<uint64_t>(static_cast<int32_t>(data_offset)) - low16;
1775 int16_t upper16 = High16Bits(high48);
1776 // Allow the full [-2GB,+2GB) range in case `low16` is negative and needs a
1777 // compensatory 64KB added, which may push `high48` above 2GB and require
1778 // the dahi instruction.
1779 int16_t higher16 = High32Bits(high48) + ((upper16 < 0) ? 1 : 0);
1780 __ Daui(TMP, obj, upper16);
1781 if (higher16 != 0) {
1782 __ Dahi(TMP, higher16);
1783 }
1784 base_reg = TMP;
1785 data_offset = low16;
1786 }
1787 } else {
1788 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset)));
1789 }
1790 __ PoisonHeapReference(AT, value);
1791 __ Sw(AT, base_reg, data_offset);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001792 null_checker();
Alexey Frunzec061de12017-02-14 13:27:23 -08001793 } else {
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001794 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 if (needs_write_barrier) {
1797 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001798 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 }
1800 } else {
1801 DCHECK_EQ(value_type, Primitive::kPrimNot);
Alexey Frunzec061de12017-02-14 13:27:23 -08001802 // Note: if heap poisoning is enabled, pAputObject takes care
1803 // of poisoning the reference.
Serban Constantinescufc734082016-07-19 17:18:07 +01001804 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001805 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806 }
1807 break;
1808 }
1809
1810 case Primitive::kPrimLong: {
1811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1812 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1813 if (index.IsConstant()) {
1814 size_t offset =
1815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001816 __ StoreToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817 } else {
1818 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1819 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001820 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821 }
1822 break;
1823 }
1824
1825 case Primitive::kPrimFloat: {
1826 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1827 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1828 DCHECK(locations->InAt(2).IsFpuRegister());
1829 if (index.IsConstant()) {
1830 size_t offset =
1831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001832 __ StoreFpuToOffset(kStoreWord, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 } else {
1834 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1835 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001836 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001837 }
1838 break;
1839 }
1840
1841 case Primitive::kPrimDouble: {
1842 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1843 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1844 DCHECK(locations->InAt(2).IsFpuRegister());
1845 if (index.IsConstant()) {
1846 size_t offset =
1847 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001848 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001849 } else {
1850 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1851 __ Daddu(TMP, obj, TMP);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01001852 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001853 }
1854 break;
1855 }
1856
1857 case Primitive::kPrimVoid:
1858 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1859 UNREACHABLE();
1860 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861}
1862
1863void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001864 RegisterSet caller_saves = RegisterSet::Empty();
1865 InvokeRuntimeCallingConvention calling_convention;
1866 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1867 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1868 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001869 locations->SetInAt(0, Location::RequiresRegister());
1870 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001871}
1872
1873void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1874 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001875 BoundsCheckSlowPathMIPS64* slow_path =
1876 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001877 codegen_->AddSlowPath(slow_path);
1878
1879 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1880 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1881
1882 // length is limited by the maximum positive signed 32-bit integer.
1883 // Unsigned comparison of length and index checks for index < 0
1884 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001885 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001886}
1887
1888void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001889 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
1890 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
1891
1892 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
1893 switch (type_check_kind) {
1894 case TypeCheckKind::kExactCheck:
1895 case TypeCheckKind::kAbstractClassCheck:
1896 case TypeCheckKind::kClassHierarchyCheck:
1897 case TypeCheckKind::kArrayObjectCheck:
1898 call_kind = throws_into_catch
1899 ? LocationSummary::kCallOnSlowPath
1900 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
1901 break;
1902 case TypeCheckKind::kArrayCheck:
1903 case TypeCheckKind::kUnresolvedCheck:
1904 case TypeCheckKind::kInterfaceCheck:
1905 call_kind = LocationSummary::kCallOnSlowPath;
1906 break;
1907 }
1908
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetInAt(1, Location::RequiresRegister());
1912 locations->AddTemp(Location::RequiresRegister());
1913}
1914
1915void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001916 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001917 LocationSummary* locations = instruction->GetLocations();
1918 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1919 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001920 GpuRegister temp = locations->GetTemp(0).AsRegister<GpuRegister>();
1921 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1922 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1923 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1924 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1925 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
1926 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
1927 const uint32_t object_array_data_offset =
1928 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
1929 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001930
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001931 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
1932 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
1933 // read barriers is done for performance and code size reasons.
1934 bool is_type_check_slow_path_fatal = false;
1935 if (!kEmitCompilerReadBarrier) {
1936 is_type_check_slow_path_fatal =
1937 (type_check_kind == TypeCheckKind::kExactCheck ||
1938 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
1939 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
1940 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
1941 !instruction->CanThrowIntoCatchBlock();
1942 }
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001943 SlowPathCodeMIPS64* slow_path =
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001944 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
1945 is_type_check_slow_path_fatal);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001946 codegen_->AddSlowPath(slow_path);
1947
Alexey Frunze66b69ad2017-02-24 00:51:44 -08001948 // Avoid this check if we know `obj` is not null.
1949 if (instruction->MustDoNullCheck()) {
1950 __ Beqzc(obj, &done);
1951 }
1952
1953 switch (type_check_kind) {
1954 case TypeCheckKind::kExactCheck:
1955 case TypeCheckKind::kArrayCheck: {
1956 // /* HeapReference<Class> */ temp = obj->klass_
1957 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
1958 __ MaybeUnpoisonHeapReference(temp);
1959 // Jump to slow path for throwing the exception or doing a
1960 // more involved array check.
1961 __ Bnec(temp, cls, slow_path->GetEntryLabel());
1962 break;
1963 }
1964
1965 case TypeCheckKind::kAbstractClassCheck: {
1966 // /* HeapReference<Class> */ temp = obj->klass_
1967 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
1968 __ MaybeUnpoisonHeapReference(temp);
1969 // If the class is abstract, we eagerly fetch the super class of the
1970 // object to avoid doing a comparison we know will fail.
1971 Mips64Label loop;
1972 __ Bind(&loop);
1973 // /* HeapReference<Class> */ temp = temp->super_class_
1974 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, super_offset);
1975 __ MaybeUnpoisonHeapReference(temp);
1976 // If the class reference currently in `temp` is null, jump to the slow path to throw the
1977 // exception.
1978 __ Beqzc(temp, slow_path->GetEntryLabel());
1979 // Otherwise, compare the classes.
1980 __ Bnec(temp, cls, &loop);
1981 break;
1982 }
1983
1984 case TypeCheckKind::kClassHierarchyCheck: {
1985 // /* HeapReference<Class> */ temp = obj->klass_
1986 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
1987 __ MaybeUnpoisonHeapReference(temp);
1988 // Walk over the class hierarchy to find a match.
1989 Mips64Label loop;
1990 __ Bind(&loop);
1991 __ Beqc(temp, cls, &done);
1992 // /* HeapReference<Class> */ temp = temp->super_class_
1993 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, super_offset);
1994 __ MaybeUnpoisonHeapReference(temp);
1995 // If the class reference currently in `temp` is null, jump to the slow path to throw the
1996 // exception. Otherwise, jump to the beginning of the loop.
1997 __ Bnezc(temp, &loop);
1998 __ Bc(slow_path->GetEntryLabel());
1999 break;
2000 }
2001
2002 case TypeCheckKind::kArrayObjectCheck: {
2003 // /* HeapReference<Class> */ temp = obj->klass_
2004 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2005 __ MaybeUnpoisonHeapReference(temp);
2006 // Do an exact check.
2007 __ Beqc(temp, cls, &done);
2008 // Otherwise, we need to check that the object's class is a non-primitive array.
2009 // /* HeapReference<Class> */ temp = temp->component_type_
2010 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, component_offset);
2011 __ MaybeUnpoisonHeapReference(temp);
2012 // If the component type is null, jump to the slow path to throw the exception.
2013 __ Beqzc(temp, slow_path->GetEntryLabel());
2014 // Otherwise, the object is indeed an array, further check that this component
2015 // type is not a primitive type.
2016 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
2017 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2018 __ Bnezc(temp, slow_path->GetEntryLabel());
2019 break;
2020 }
2021
2022 case TypeCheckKind::kUnresolvedCheck:
2023 // We always go into the type check slow path for the unresolved check case.
2024 // We cannot directly call the CheckCast runtime entry point
2025 // without resorting to a type checking slow path here (i.e. by
2026 // calling InvokeRuntime directly), as it would require to
2027 // assign fixed registers for the inputs of this HInstanceOf
2028 // instruction (following the runtime calling convention), which
2029 // might be cluttered by the potential first read barrier
2030 // emission at the beginning of this method.
2031 __ Bc(slow_path->GetEntryLabel());
2032 break;
2033
2034 case TypeCheckKind::kInterfaceCheck: {
2035 // Avoid read barriers to improve performance of the fast path. We can not get false
2036 // positives by doing this.
2037 // /* HeapReference<Class> */ temp = obj->klass_
2038 __ LoadFromOffset(kLoadUnsignedWord, temp, obj, class_offset);
2039 __ MaybeUnpoisonHeapReference(temp);
2040 // /* HeapReference<Class> */ temp = temp->iftable_
2041 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, iftable_offset);
2042 __ MaybeUnpoisonHeapReference(temp);
2043 // Iftable is never null.
2044 __ Lw(TMP, temp, array_length_offset);
2045 // Loop through the iftable and check if any class matches.
2046 Mips64Label loop;
2047 __ Bind(&loop);
2048 __ Beqzc(TMP, slow_path->GetEntryLabel());
2049 __ Lwu(AT, temp, object_array_data_offset);
2050 __ MaybeUnpoisonHeapReference(AT);
2051 // Go to next interface.
2052 __ Daddiu(temp, temp, 2 * kHeapReferenceSize);
2053 __ Addiu(TMP, TMP, -2);
2054 // Compare the classes and continue the loop if they do not match.
2055 __ Bnec(AT, cls, &loop);
2056 break;
2057 }
2058 }
2059
2060 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061 __ Bind(slow_path->GetExitLabel());
2062}
2063
2064void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2067 locations->SetInAt(0, Location::RequiresRegister());
2068 if (check->HasUses()) {
2069 locations->SetOut(Location::SameAsFirstInput());
2070 }
2071}
2072
2073void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
2074 // We assume the class is not null.
2075 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2076 check->GetLoadClass(),
2077 check,
2078 check->GetDexPc(),
2079 true);
2080 codegen_->AddSlowPath(slow_path);
2081 GenerateClassInitializationCheck(slow_path,
2082 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
2083}
2084
2085void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
2086 Primitive::Type in_type = compare->InputAt(0)->GetType();
2087
Alexey Frunze299a9392015-12-08 16:08:02 -08002088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002089
2090 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002091 case Primitive::kPrimBoolean:
2092 case Primitive::kPrimByte:
2093 case Primitive::kPrimShort:
2094 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002095 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 case Primitive::kPrimLong:
2097 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002098 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2100 break;
2101
2102 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08002103 case Primitive::kPrimDouble:
2104 locations->SetInAt(0, Location::RequiresFpuRegister());
2105 locations->SetInAt(1, Location::RequiresFpuRegister());
2106 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002107 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108
2109 default:
2110 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2111 }
2112}
2113
2114void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
2115 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002116 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002117 Primitive::Type in_type = instruction->InputAt(0)->GetType();
2118
2119 // 0 if: left == right
2120 // 1 if: left > right
2121 // -1 if: left < right
2122 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002123 case Primitive::kPrimBoolean:
2124 case Primitive::kPrimByte:
2125 case Primitive::kPrimShort:
2126 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002127 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002128 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002129 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002130 Location rhs_location = locations->InAt(1);
2131 bool use_imm = rhs_location.IsConstant();
2132 GpuRegister rhs = ZERO;
2133 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002134 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08002135 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2136 if (value != 0) {
2137 rhs = AT;
2138 __ LoadConst64(rhs, value);
2139 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00002140 } else {
2141 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
2142 if (value != 0) {
2143 rhs = AT;
2144 __ LoadConst32(rhs, value);
2145 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07002146 }
2147 } else {
2148 rhs = rhs_location.AsRegister<GpuRegister>();
2149 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002150 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08002151 __ Slt(res, rhs, lhs);
2152 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002153 break;
2154 }
2155
Alexey Frunze299a9392015-12-08 16:08:02 -08002156 case Primitive::kPrimFloat: {
2157 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2158 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2159 Mips64Label done;
2160 __ CmpEqS(FTMP, lhs, rhs);
2161 __ LoadConst32(res, 0);
2162 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002163 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002164 __ CmpLtS(FTMP, lhs, rhs);
2165 __ LoadConst32(res, -1);
2166 __ Bc1nez(FTMP, &done);
2167 __ LoadConst32(res, 1);
2168 } else {
2169 __ CmpLtS(FTMP, rhs, lhs);
2170 __ LoadConst32(res, 1);
2171 __ Bc1nez(FTMP, &done);
2172 __ LoadConst32(res, -1);
2173 }
2174 __ Bind(&done);
2175 break;
2176 }
2177
Alexey Frunze4dda3372015-06-01 18:31:49 -07002178 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08002179 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2180 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2181 Mips64Label done;
2182 __ CmpEqD(FTMP, lhs, rhs);
2183 __ LoadConst32(res, 0);
2184 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00002185 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08002186 __ CmpLtD(FTMP, lhs, rhs);
2187 __ LoadConst32(res, -1);
2188 __ Bc1nez(FTMP, &done);
2189 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08002191 __ CmpLtD(FTMP, rhs, lhs);
2192 __ LoadConst32(res, 1);
2193 __ Bc1nez(FTMP, &done);
2194 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002195 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002196 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002197 break;
2198 }
2199
2200 default:
2201 LOG(FATAL) << "Unimplemented compare type " << in_type;
2202 }
2203}
2204
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002205void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08002207 switch (instruction->InputAt(0)->GetType()) {
2208 default:
2209 case Primitive::kPrimLong:
2210 locations->SetInAt(0, Location::RequiresRegister());
2211 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2212 break;
2213
2214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble:
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
2217 locations->SetInAt(1, Location::RequiresFpuRegister());
2218 break;
2219 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002220 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2222 }
2223}
2224
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002225void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002226 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002227 return;
2228 }
2229
Alexey Frunze299a9392015-12-08 16:08:02 -08002230 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002231 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08002232 switch (type) {
2233 default:
2234 // Integer case.
2235 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
2236 return;
2237 case Primitive::kPrimLong:
2238 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
2239 return;
Alexey Frunze299a9392015-12-08 16:08:02 -08002240 case Primitive::kPrimFloat:
2241 case Primitive::kPrimDouble:
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002242 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2243 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002244 }
2245}
2246
Alexey Frunzec857c742015-09-23 15:12:39 -07002247void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2248 DCHECK(instruction->IsDiv() || instruction->IsRem());
2249 Primitive::Type type = instruction->GetResultType();
2250
2251 LocationSummary* locations = instruction->GetLocations();
2252 Location second = locations->InAt(1);
2253 DCHECK(second.IsConstant());
2254
2255 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2256 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2257 int64_t imm = Int64FromConstant(second.GetConstant());
2258 DCHECK(imm == 1 || imm == -1);
2259
2260 if (instruction->IsRem()) {
2261 __ Move(out, ZERO);
2262 } else {
2263 if (imm == -1) {
2264 if (type == Primitive::kPrimInt) {
2265 __ Subu(out, ZERO, dividend);
2266 } else {
2267 DCHECK_EQ(type, Primitive::kPrimLong);
2268 __ Dsubu(out, ZERO, dividend);
2269 }
2270 } else if (out != dividend) {
2271 __ Move(out, dividend);
2272 }
2273 }
2274}
2275
2276void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2277 DCHECK(instruction->IsDiv() || instruction->IsRem());
2278 Primitive::Type type = instruction->GetResultType();
2279
2280 LocationSummary* locations = instruction->GetLocations();
2281 Location second = locations->InAt(1);
2282 DCHECK(second.IsConstant());
2283
2284 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2285 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2286 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002287 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07002288 int ctz_imm = CTZ(abs_imm);
2289
2290 if (instruction->IsDiv()) {
2291 if (type == Primitive::kPrimInt) {
2292 if (ctz_imm == 1) {
2293 // Fast path for division by +/-2, which is very common.
2294 __ Srl(TMP, dividend, 31);
2295 } else {
2296 __ Sra(TMP, dividend, 31);
2297 __ Srl(TMP, TMP, 32 - ctz_imm);
2298 }
2299 __ Addu(out, dividend, TMP);
2300 __ Sra(out, out, ctz_imm);
2301 if (imm < 0) {
2302 __ Subu(out, ZERO, out);
2303 }
2304 } else {
2305 DCHECK_EQ(type, Primitive::kPrimLong);
2306 if (ctz_imm == 1) {
2307 // Fast path for division by +/-2, which is very common.
2308 __ Dsrl32(TMP, dividend, 31);
2309 } else {
2310 __ Dsra32(TMP, dividend, 31);
2311 if (ctz_imm > 32) {
2312 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2313 } else {
2314 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2315 }
2316 }
2317 __ Daddu(out, dividend, TMP);
2318 if (ctz_imm < 32) {
2319 __ Dsra(out, out, ctz_imm);
2320 } else {
2321 __ Dsra32(out, out, ctz_imm - 32);
2322 }
2323 if (imm < 0) {
2324 __ Dsubu(out, ZERO, out);
2325 }
2326 }
2327 } else {
2328 if (type == Primitive::kPrimInt) {
2329 if (ctz_imm == 1) {
2330 // Fast path for modulo +/-2, which is very common.
2331 __ Sra(TMP, dividend, 31);
2332 __ Subu(out, dividend, TMP);
2333 __ Andi(out, out, 1);
2334 __ Addu(out, out, TMP);
2335 } else {
2336 __ Sra(TMP, dividend, 31);
2337 __ Srl(TMP, TMP, 32 - ctz_imm);
2338 __ Addu(out, dividend, TMP);
2339 if (IsUint<16>(abs_imm - 1)) {
2340 __ Andi(out, out, abs_imm - 1);
2341 } else {
2342 __ Sll(out, out, 32 - ctz_imm);
2343 __ Srl(out, out, 32 - ctz_imm);
2344 }
2345 __ Subu(out, out, TMP);
2346 }
2347 } else {
2348 DCHECK_EQ(type, Primitive::kPrimLong);
2349 if (ctz_imm == 1) {
2350 // Fast path for modulo +/-2, which is very common.
2351 __ Dsra32(TMP, dividend, 31);
2352 __ Dsubu(out, dividend, TMP);
2353 __ Andi(out, out, 1);
2354 __ Daddu(out, out, TMP);
2355 } else {
2356 __ Dsra32(TMP, dividend, 31);
2357 if (ctz_imm > 32) {
2358 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2359 } else {
2360 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2361 }
2362 __ Daddu(out, dividend, TMP);
2363 if (IsUint<16>(abs_imm - 1)) {
2364 __ Andi(out, out, abs_imm - 1);
2365 } else {
2366 if (ctz_imm > 32) {
2367 __ Dsll(out, out, 64 - ctz_imm);
2368 __ Dsrl(out, out, 64 - ctz_imm);
2369 } else {
2370 __ Dsll32(out, out, 32 - ctz_imm);
2371 __ Dsrl32(out, out, 32 - ctz_imm);
2372 }
2373 }
2374 __ Dsubu(out, out, TMP);
2375 }
2376 }
2377 }
2378}
2379
2380void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2381 DCHECK(instruction->IsDiv() || instruction->IsRem());
2382
2383 LocationSummary* locations = instruction->GetLocations();
2384 Location second = locations->InAt(1);
2385 DCHECK(second.IsConstant());
2386
2387 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2388 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2389 int64_t imm = Int64FromConstant(second.GetConstant());
2390
2391 Primitive::Type type = instruction->GetResultType();
2392 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2393
2394 int64_t magic;
2395 int shift;
2396 CalculateMagicAndShiftForDivRem(imm,
2397 (type == Primitive::kPrimLong),
2398 &magic,
2399 &shift);
2400
2401 if (type == Primitive::kPrimInt) {
2402 __ LoadConst32(TMP, magic);
2403 __ MuhR6(TMP, dividend, TMP);
2404
2405 if (imm > 0 && magic < 0) {
2406 __ Addu(TMP, TMP, dividend);
2407 } else if (imm < 0 && magic > 0) {
2408 __ Subu(TMP, TMP, dividend);
2409 }
2410
2411 if (shift != 0) {
2412 __ Sra(TMP, TMP, shift);
2413 }
2414
2415 if (instruction->IsDiv()) {
2416 __ Sra(out, TMP, 31);
2417 __ Subu(out, TMP, out);
2418 } else {
2419 __ Sra(AT, TMP, 31);
2420 __ Subu(AT, TMP, AT);
2421 __ LoadConst32(TMP, imm);
2422 __ MulR6(TMP, AT, TMP);
2423 __ Subu(out, dividend, TMP);
2424 }
2425 } else {
2426 __ LoadConst64(TMP, magic);
2427 __ Dmuh(TMP, dividend, TMP);
2428
2429 if (imm > 0 && magic < 0) {
2430 __ Daddu(TMP, TMP, dividend);
2431 } else if (imm < 0 && magic > 0) {
2432 __ Dsubu(TMP, TMP, dividend);
2433 }
2434
2435 if (shift >= 32) {
2436 __ Dsra32(TMP, TMP, shift - 32);
2437 } else if (shift > 0) {
2438 __ Dsra(TMP, TMP, shift);
2439 }
2440
2441 if (instruction->IsDiv()) {
2442 __ Dsra32(out, TMP, 31);
2443 __ Dsubu(out, TMP, out);
2444 } else {
2445 __ Dsra32(AT, TMP, 31);
2446 __ Dsubu(AT, TMP, AT);
2447 __ LoadConst64(TMP, imm);
2448 __ Dmul(TMP, AT, TMP);
2449 __ Dsubu(out, dividend, TMP);
2450 }
2451 }
2452}
2453
2454void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2455 DCHECK(instruction->IsDiv() || instruction->IsRem());
2456 Primitive::Type type = instruction->GetResultType();
2457 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2458
2459 LocationSummary* locations = instruction->GetLocations();
2460 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2461 Location second = locations->InAt(1);
2462
2463 if (second.IsConstant()) {
2464 int64_t imm = Int64FromConstant(second.GetConstant());
2465 if (imm == 0) {
2466 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2467 } else if (imm == 1 || imm == -1) {
2468 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002469 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002470 DivRemByPowerOfTwo(instruction);
2471 } else {
2472 DCHECK(imm <= -2 || imm >= 2);
2473 GenerateDivRemWithAnyConstant(instruction);
2474 }
2475 } else {
2476 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2477 GpuRegister divisor = second.AsRegister<GpuRegister>();
2478 if (instruction->IsDiv()) {
2479 if (type == Primitive::kPrimInt)
2480 __ DivR6(out, dividend, divisor);
2481 else
2482 __ Ddiv(out, dividend, divisor);
2483 } else {
2484 if (type == Primitive::kPrimInt)
2485 __ ModR6(out, dividend, divisor);
2486 else
2487 __ Dmod(out, dividend, divisor);
2488 }
2489 }
2490}
2491
Alexey Frunze4dda3372015-06-01 18:31:49 -07002492void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2493 LocationSummary* locations =
2494 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2495 switch (div->GetResultType()) {
2496 case Primitive::kPrimInt:
2497 case Primitive::kPrimLong:
2498 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002499 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2501 break;
2502
2503 case Primitive::kPrimFloat:
2504 case Primitive::kPrimDouble:
2505 locations->SetInAt(0, Location::RequiresFpuRegister());
2506 locations->SetInAt(1, Location::RequiresFpuRegister());
2507 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2508 break;
2509
2510 default:
2511 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2512 }
2513}
2514
2515void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2516 Primitive::Type type = instruction->GetType();
2517 LocationSummary* locations = instruction->GetLocations();
2518
2519 switch (type) {
2520 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002521 case Primitive::kPrimLong:
2522 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002523 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002524 case Primitive::kPrimFloat:
2525 case Primitive::kPrimDouble: {
2526 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2527 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2528 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2529 if (type == Primitive::kPrimFloat)
2530 __ DivS(dst, lhs, rhs);
2531 else
2532 __ DivD(dst, lhs, rhs);
2533 break;
2534 }
2535 default:
2536 LOG(FATAL) << "Unexpected div type " << type;
2537 }
2538}
2539
2540void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002541 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002543}
2544
2545void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2546 SlowPathCodeMIPS64* slow_path =
2547 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2548 codegen_->AddSlowPath(slow_path);
2549 Location value = instruction->GetLocations()->InAt(0);
2550
2551 Primitive::Type type = instruction->GetType();
2552
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002553 if (!Primitive::IsIntegralType(type)) {
2554 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002555 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002556 }
2557
2558 if (value.IsConstant()) {
2559 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2560 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002561 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002562 } else {
2563 // A division by a non-null constant is valid. We don't need to perform
2564 // any check, so simply fall through.
2565 }
2566 } else {
2567 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2568 }
2569}
2570
2571void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2572 LocationSummary* locations =
2573 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2574 locations->SetOut(Location::ConstantLocation(constant));
2575}
2576
2577void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2578 // Will be generated at use site.
2579}
2580
2581void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2582 exit->SetLocations(nullptr);
2583}
2584
2585void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2586}
2587
2588void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2591 locations->SetOut(Location::ConstantLocation(constant));
2592}
2593
2594void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2595 // Will be generated at use site.
2596}
2597
David Brazdilfc6a86a2015-06-26 10:33:45 +00002598void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 DCHECK(!successor->IsExitBlock());
2600 HBasicBlock* block = got->GetBlock();
2601 HInstruction* previous = got->GetPrevious();
2602 HLoopInformation* info = block->GetLoopInformation();
2603
2604 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2605 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2606 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2607 return;
2608 }
2609 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2610 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2611 }
2612 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002613 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 }
2615}
2616
David Brazdilfc6a86a2015-06-26 10:33:45 +00002617void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2618 got->SetLocations(nullptr);
2619}
2620
2621void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2622 HandleGoto(got, got->GetSuccessor());
2623}
2624
2625void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2626 try_boundary->SetLocations(nullptr);
2627}
2628
2629void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2630 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2631 if (!successor->IsExitBlock()) {
2632 HandleGoto(try_boundary, successor);
2633 }
2634}
2635
Alexey Frunze299a9392015-12-08 16:08:02 -08002636void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2637 bool is64bit,
2638 LocationSummary* locations) {
2639 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2640 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2641 Location rhs_location = locations->InAt(1);
2642 GpuRegister rhs_reg = ZERO;
2643 int64_t rhs_imm = 0;
2644 bool use_imm = rhs_location.IsConstant();
2645 if (use_imm) {
2646 if (is64bit) {
2647 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2648 } else {
2649 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2650 }
2651 } else {
2652 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2653 }
2654 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2655
2656 switch (cond) {
2657 case kCondEQ:
2658 case kCondNE:
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002659 if (use_imm && IsInt<16>(-rhs_imm)) {
2660 if (rhs_imm == 0) {
2661 if (cond == kCondEQ) {
2662 __ Sltiu(dst, lhs, 1);
2663 } else {
2664 __ Sltu(dst, ZERO, lhs);
2665 }
2666 } else {
2667 if (is64bit) {
2668 __ Daddiu(dst, lhs, -rhs_imm);
2669 } else {
2670 __ Addiu(dst, lhs, -rhs_imm);
2671 }
2672 if (cond == kCondEQ) {
2673 __ Sltiu(dst, dst, 1);
2674 } else {
2675 __ Sltu(dst, ZERO, dst);
2676 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002677 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002678 } else {
Goran Jakovljevicdb3deee2016-12-28 14:33:21 +01002679 if (use_imm && IsUint<16>(rhs_imm)) {
2680 __ Xori(dst, lhs, rhs_imm);
2681 } else {
2682 if (use_imm) {
2683 rhs_reg = TMP;
2684 __ LoadConst64(rhs_reg, rhs_imm);
2685 }
2686 __ Xor(dst, lhs, rhs_reg);
2687 }
2688 if (cond == kCondEQ) {
2689 __ Sltiu(dst, dst, 1);
2690 } else {
2691 __ Sltu(dst, ZERO, dst);
2692 }
Alexey Frunze299a9392015-12-08 16:08:02 -08002693 }
2694 break;
2695
2696 case kCondLT:
2697 case kCondGE:
2698 if (use_imm && IsInt<16>(rhs_imm)) {
2699 __ Slti(dst, lhs, rhs_imm);
2700 } else {
2701 if (use_imm) {
2702 rhs_reg = TMP;
2703 __ LoadConst64(rhs_reg, rhs_imm);
2704 }
2705 __ Slt(dst, lhs, rhs_reg);
2706 }
2707 if (cond == kCondGE) {
2708 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2709 // only the slt instruction but no sge.
2710 __ Xori(dst, dst, 1);
2711 }
2712 break;
2713
2714 case kCondLE:
2715 case kCondGT:
2716 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2717 // Simulate lhs <= rhs via lhs < rhs + 1.
2718 __ Slti(dst, lhs, rhs_imm_plus_one);
2719 if (cond == kCondGT) {
2720 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2721 // only the slti instruction but no sgti.
2722 __ Xori(dst, dst, 1);
2723 }
2724 } else {
2725 if (use_imm) {
2726 rhs_reg = TMP;
2727 __ LoadConst64(rhs_reg, rhs_imm);
2728 }
2729 __ Slt(dst, rhs_reg, lhs);
2730 if (cond == kCondLE) {
2731 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2732 // only the slt instruction but no sle.
2733 __ Xori(dst, dst, 1);
2734 }
2735 }
2736 break;
2737
2738 case kCondB:
2739 case kCondAE:
2740 if (use_imm && IsInt<16>(rhs_imm)) {
2741 // Sltiu sign-extends its 16-bit immediate operand before
2742 // the comparison and thus lets us compare directly with
2743 // unsigned values in the ranges [0, 0x7fff] and
2744 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2745 __ Sltiu(dst, lhs, rhs_imm);
2746 } else {
2747 if (use_imm) {
2748 rhs_reg = TMP;
2749 __ LoadConst64(rhs_reg, rhs_imm);
2750 }
2751 __ Sltu(dst, lhs, rhs_reg);
2752 }
2753 if (cond == kCondAE) {
2754 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2755 // only the sltu instruction but no sgeu.
2756 __ Xori(dst, dst, 1);
2757 }
2758 break;
2759
2760 case kCondBE:
2761 case kCondA:
2762 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2763 // Simulate lhs <= rhs via lhs < rhs + 1.
2764 // Note that this only works if rhs + 1 does not overflow
2765 // to 0, hence the check above.
2766 // Sltiu sign-extends its 16-bit immediate operand before
2767 // the comparison and thus lets us compare directly with
2768 // unsigned values in the ranges [0, 0x7fff] and
2769 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2770 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2771 if (cond == kCondA) {
2772 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2773 // only the sltiu instruction but no sgtiu.
2774 __ Xori(dst, dst, 1);
2775 }
2776 } else {
2777 if (use_imm) {
2778 rhs_reg = TMP;
2779 __ LoadConst64(rhs_reg, rhs_imm);
2780 }
2781 __ Sltu(dst, rhs_reg, lhs);
2782 if (cond == kCondBE) {
2783 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2784 // only the sltu instruction but no sleu.
2785 __ Xori(dst, dst, 1);
2786 }
2787 }
2788 break;
2789 }
2790}
2791
2792void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2793 bool is64bit,
2794 LocationSummary* locations,
2795 Mips64Label* label) {
2796 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2797 Location rhs_location = locations->InAt(1);
2798 GpuRegister rhs_reg = ZERO;
2799 int64_t rhs_imm = 0;
2800 bool use_imm = rhs_location.IsConstant();
2801 if (use_imm) {
2802 if (is64bit) {
2803 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2804 } else {
2805 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2806 }
2807 } else {
2808 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2809 }
2810
2811 if (use_imm && rhs_imm == 0) {
2812 switch (cond) {
2813 case kCondEQ:
2814 case kCondBE: // <= 0 if zero
2815 __ Beqzc(lhs, label);
2816 break;
2817 case kCondNE:
2818 case kCondA: // > 0 if non-zero
2819 __ Bnezc(lhs, label);
2820 break;
2821 case kCondLT:
2822 __ Bltzc(lhs, label);
2823 break;
2824 case kCondGE:
2825 __ Bgezc(lhs, label);
2826 break;
2827 case kCondLE:
2828 __ Blezc(lhs, label);
2829 break;
2830 case kCondGT:
2831 __ Bgtzc(lhs, label);
2832 break;
2833 case kCondB: // always false
2834 break;
2835 case kCondAE: // always true
2836 __ Bc(label);
2837 break;
2838 }
2839 } else {
2840 if (use_imm) {
2841 rhs_reg = TMP;
2842 __ LoadConst64(rhs_reg, rhs_imm);
2843 }
2844 switch (cond) {
2845 case kCondEQ:
2846 __ Beqc(lhs, rhs_reg, label);
2847 break;
2848 case kCondNE:
2849 __ Bnec(lhs, rhs_reg, label);
2850 break;
2851 case kCondLT:
2852 __ Bltc(lhs, rhs_reg, label);
2853 break;
2854 case kCondGE:
2855 __ Bgec(lhs, rhs_reg, label);
2856 break;
2857 case kCondLE:
2858 __ Bgec(rhs_reg, lhs, label);
2859 break;
2860 case kCondGT:
2861 __ Bltc(rhs_reg, lhs, label);
2862 break;
2863 case kCondB:
2864 __ Bltuc(lhs, rhs_reg, label);
2865 break;
2866 case kCondAE:
2867 __ Bgeuc(lhs, rhs_reg, label);
2868 break;
2869 case kCondBE:
2870 __ Bgeuc(rhs_reg, lhs, label);
2871 break;
2872 case kCondA:
2873 __ Bltuc(rhs_reg, lhs, label);
2874 break;
2875 }
2876 }
2877}
2878
Tijana Jakovljevic43758192016-12-30 09:23:01 +01002879void InstructionCodeGeneratorMIPS64::GenerateFpCompare(IfCondition cond,
2880 bool gt_bias,
2881 Primitive::Type type,
2882 LocationSummary* locations) {
2883 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2884 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2885 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2886 if (type == Primitive::kPrimFloat) {
2887 switch (cond) {
2888 case kCondEQ:
2889 __ CmpEqS(FTMP, lhs, rhs);
2890 __ Mfc1(dst, FTMP);
2891 __ Andi(dst, dst, 1);
2892 break;
2893 case kCondNE:
2894 __ CmpEqS(FTMP, lhs, rhs);
2895 __ Mfc1(dst, FTMP);
2896 __ Addiu(dst, dst, 1);
2897 break;
2898 case kCondLT:
2899 if (gt_bias) {
2900 __ CmpLtS(FTMP, lhs, rhs);
2901 } else {
2902 __ CmpUltS(FTMP, lhs, rhs);
2903 }
2904 __ Mfc1(dst, FTMP);
2905 __ Andi(dst, dst, 1);
2906 break;
2907 case kCondLE:
2908 if (gt_bias) {
2909 __ CmpLeS(FTMP, lhs, rhs);
2910 } else {
2911 __ CmpUleS(FTMP, lhs, rhs);
2912 }
2913 __ Mfc1(dst, FTMP);
2914 __ Andi(dst, dst, 1);
2915 break;
2916 case kCondGT:
2917 if (gt_bias) {
2918 __ CmpUltS(FTMP, rhs, lhs);
2919 } else {
2920 __ CmpLtS(FTMP, rhs, lhs);
2921 }
2922 __ Mfc1(dst, FTMP);
2923 __ Andi(dst, dst, 1);
2924 break;
2925 case kCondGE:
2926 if (gt_bias) {
2927 __ CmpUleS(FTMP, rhs, lhs);
2928 } else {
2929 __ CmpLeS(FTMP, rhs, lhs);
2930 }
2931 __ Mfc1(dst, FTMP);
2932 __ Andi(dst, dst, 1);
2933 break;
2934 default:
2935 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2936 UNREACHABLE();
2937 }
2938 } else {
2939 DCHECK_EQ(type, Primitive::kPrimDouble);
2940 switch (cond) {
2941 case kCondEQ:
2942 __ CmpEqD(FTMP, lhs, rhs);
2943 __ Mfc1(dst, FTMP);
2944 __ Andi(dst, dst, 1);
2945 break;
2946 case kCondNE:
2947 __ CmpEqD(FTMP, lhs, rhs);
2948 __ Mfc1(dst, FTMP);
2949 __ Addiu(dst, dst, 1);
2950 break;
2951 case kCondLT:
2952 if (gt_bias) {
2953 __ CmpLtD(FTMP, lhs, rhs);
2954 } else {
2955 __ CmpUltD(FTMP, lhs, rhs);
2956 }
2957 __ Mfc1(dst, FTMP);
2958 __ Andi(dst, dst, 1);
2959 break;
2960 case kCondLE:
2961 if (gt_bias) {
2962 __ CmpLeD(FTMP, lhs, rhs);
2963 } else {
2964 __ CmpUleD(FTMP, lhs, rhs);
2965 }
2966 __ Mfc1(dst, FTMP);
2967 __ Andi(dst, dst, 1);
2968 break;
2969 case kCondGT:
2970 if (gt_bias) {
2971 __ CmpUltD(FTMP, rhs, lhs);
2972 } else {
2973 __ CmpLtD(FTMP, rhs, lhs);
2974 }
2975 __ Mfc1(dst, FTMP);
2976 __ Andi(dst, dst, 1);
2977 break;
2978 case kCondGE:
2979 if (gt_bias) {
2980 __ CmpUleD(FTMP, rhs, lhs);
2981 } else {
2982 __ CmpLeD(FTMP, rhs, lhs);
2983 }
2984 __ Mfc1(dst, FTMP);
2985 __ Andi(dst, dst, 1);
2986 break;
2987 default:
2988 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
2989 UNREACHABLE();
2990 }
2991 }
2992}
2993
Alexey Frunze299a9392015-12-08 16:08:02 -08002994void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2995 bool gt_bias,
2996 Primitive::Type type,
2997 LocationSummary* locations,
2998 Mips64Label* label) {
2999 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3000 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3001 if (type == Primitive::kPrimFloat) {
3002 switch (cond) {
3003 case kCondEQ:
3004 __ CmpEqS(FTMP, lhs, rhs);
3005 __ Bc1nez(FTMP, label);
3006 break;
3007 case kCondNE:
3008 __ CmpEqS(FTMP, lhs, rhs);
3009 __ Bc1eqz(FTMP, label);
3010 break;
3011 case kCondLT:
3012 if (gt_bias) {
3013 __ CmpLtS(FTMP, lhs, rhs);
3014 } else {
3015 __ CmpUltS(FTMP, lhs, rhs);
3016 }
3017 __ Bc1nez(FTMP, label);
3018 break;
3019 case kCondLE:
3020 if (gt_bias) {
3021 __ CmpLeS(FTMP, lhs, rhs);
3022 } else {
3023 __ CmpUleS(FTMP, lhs, rhs);
3024 }
3025 __ Bc1nez(FTMP, label);
3026 break;
3027 case kCondGT:
3028 if (gt_bias) {
3029 __ CmpUltS(FTMP, rhs, lhs);
3030 } else {
3031 __ CmpLtS(FTMP, rhs, lhs);
3032 }
3033 __ Bc1nez(FTMP, label);
3034 break;
3035 case kCondGE:
3036 if (gt_bias) {
3037 __ CmpUleS(FTMP, rhs, lhs);
3038 } else {
3039 __ CmpLeS(FTMP, rhs, lhs);
3040 }
3041 __ Bc1nez(FTMP, label);
3042 break;
3043 default:
3044 LOG(FATAL) << "Unexpected non-floating-point condition";
3045 }
3046 } else {
3047 DCHECK_EQ(type, Primitive::kPrimDouble);
3048 switch (cond) {
3049 case kCondEQ:
3050 __ CmpEqD(FTMP, lhs, rhs);
3051 __ Bc1nez(FTMP, label);
3052 break;
3053 case kCondNE:
3054 __ CmpEqD(FTMP, lhs, rhs);
3055 __ Bc1eqz(FTMP, label);
3056 break;
3057 case kCondLT:
3058 if (gt_bias) {
3059 __ CmpLtD(FTMP, lhs, rhs);
3060 } else {
3061 __ CmpUltD(FTMP, lhs, rhs);
3062 }
3063 __ Bc1nez(FTMP, label);
3064 break;
3065 case kCondLE:
3066 if (gt_bias) {
3067 __ CmpLeD(FTMP, lhs, rhs);
3068 } else {
3069 __ CmpUleD(FTMP, lhs, rhs);
3070 }
3071 __ Bc1nez(FTMP, label);
3072 break;
3073 case kCondGT:
3074 if (gt_bias) {
3075 __ CmpUltD(FTMP, rhs, lhs);
3076 } else {
3077 __ CmpLtD(FTMP, rhs, lhs);
3078 }
3079 __ Bc1nez(FTMP, label);
3080 break;
3081 case kCondGE:
3082 if (gt_bias) {
3083 __ CmpUleD(FTMP, rhs, lhs);
3084 } else {
3085 __ CmpLeD(FTMP, rhs, lhs);
3086 }
3087 __ Bc1nez(FTMP, label);
3088 break;
3089 default:
3090 LOG(FATAL) << "Unexpected non-floating-point condition";
3091 }
3092 }
3093}
3094
Alexey Frunze4dda3372015-06-01 18:31:49 -07003095void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003096 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003097 Mips64Label* true_target,
3098 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003099 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003100
David Brazdil0debae72015-11-12 18:37:00 +00003101 if (true_target == nullptr && false_target == nullptr) {
3102 // Nothing to do. The code always falls through.
3103 return;
3104 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003105 // Constant condition, statically compared against "true" (integer value 1).
3106 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003107 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003108 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003109 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003110 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003111 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003112 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003113 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003114 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003115 }
David Brazdil0debae72015-11-12 18:37:00 +00003116 return;
3117 }
3118
3119 // The following code generates these patterns:
3120 // (1) true_target == nullptr && false_target != nullptr
3121 // - opposite condition true => branch to false_target
3122 // (2) true_target != nullptr && false_target == nullptr
3123 // - condition true => branch to true_target
3124 // (3) true_target != nullptr && false_target != nullptr
3125 // - condition true => branch to true_target
3126 // - branch to false_target
3127 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003129 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003130 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003131 if (true_target == nullptr) {
3132 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
3133 } else {
3134 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
3135 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003136 } else {
3137 // The condition instruction has not been materialized, use its inputs as
3138 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003139 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003140 Primitive::Type type = condition->InputAt(0)->GetType();
3141 LocationSummary* locations = cond->GetLocations();
3142 IfCondition if_cond = condition->GetCondition();
3143 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003144
David Brazdil0debae72015-11-12 18:37:00 +00003145 if (true_target == nullptr) {
3146 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08003147 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003148 }
3149
Alexey Frunze299a9392015-12-08 16:08:02 -08003150 switch (type) {
3151 default:
3152 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
3153 break;
3154 case Primitive::kPrimLong:
3155 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
3156 break;
3157 case Primitive::kPrimFloat:
3158 case Primitive::kPrimDouble:
3159 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3160 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161 }
3162 }
David Brazdil0debae72015-11-12 18:37:00 +00003163
3164 // If neither branch falls through (case 3), the conditional branch to `true_target`
3165 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3166 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003167 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003168 }
3169}
3170
3171void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
3172 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003173 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003174 locations->SetInAt(0, Location::RequiresRegister());
3175 }
3176}
3177
3178void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003179 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3180 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003181 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003182 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003183 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00003184 nullptr : codegen_->GetLabelOf(false_successor);
3185 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003186}
3187
3188void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
3189 LocationSummary* locations = new (GetGraph()->GetArena())
3190 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003191 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003192 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003193 locations->SetInAt(0, Location::RequiresRegister());
3194 }
3195}
3196
3197void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003198 SlowPathCodeMIPS64* slow_path =
3199 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003200 GenerateTestAndBranch(deoptimize,
3201 /* condition_input_index */ 0,
3202 slow_path->GetEntryLabel(),
3203 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003204}
3205
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003206void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3207 LocationSummary* locations = new (GetGraph()->GetArena())
3208 LocationSummary(flag, LocationSummary::kNoCall);
3209 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07003210}
3211
Goran Jakovljevicc6418422016-12-05 16:31:55 +01003212void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3213 __ LoadFromOffset(kLoadWord,
3214 flag->GetLocations()->Out().AsRegister<GpuRegister>(),
3215 SP,
3216 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07003217}
3218
David Brazdil74eb1b22015-12-14 11:44:01 +00003219void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
3220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3221 if (Primitive::IsFloatingPointType(select->GetType())) {
3222 locations->SetInAt(0, Location::RequiresFpuRegister());
3223 locations->SetInAt(1, Location::RequiresFpuRegister());
3224 } else {
3225 locations->SetInAt(0, Location::RequiresRegister());
3226 locations->SetInAt(1, Location::RequiresRegister());
3227 }
3228 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3229 locations->SetInAt(2, Location::RequiresRegister());
3230 }
3231 locations->SetOut(Location::SameAsFirstInput());
3232}
3233
3234void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
3235 LocationSummary* locations = select->GetLocations();
3236 Mips64Label false_target;
3237 GenerateTestAndBranch(select,
3238 /* condition_input_index */ 2,
3239 /* true_target */ nullptr,
3240 &false_target);
3241 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3242 __ Bind(&false_target);
3243}
3244
David Srbecky0cf44932015-12-09 14:09:59 +00003245void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3246 new (GetGraph()->GetArena()) LocationSummary(info);
3247}
3248
David Srbeckyd28f4a02016-03-14 17:14:24 +00003249void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3250 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003251}
3252
3253void CodeGeneratorMIPS64::GenerateNop() {
3254 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003255}
3256
Alexey Frunze4dda3372015-06-01 18:31:49 -07003257void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
3258 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3259 LocationSummary* locations =
3260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3261 locations->SetInAt(0, Location::RequiresRegister());
3262 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3263 locations->SetOut(Location::RequiresFpuRegister());
3264 } else {
3265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3266 }
3267}
3268
3269void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
3270 const FieldInfo& field_info) {
3271 Primitive::Type type = field_info.GetFieldType();
3272 LocationSummary* locations = instruction->GetLocations();
3273 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3274 LoadOperandType load_type = kLoadUnsignedByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003275 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003276 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3277
Alexey Frunze4dda3372015-06-01 18:31:49 -07003278 switch (type) {
3279 case Primitive::kPrimBoolean:
3280 load_type = kLoadUnsignedByte;
3281 break;
3282 case Primitive::kPrimByte:
3283 load_type = kLoadSignedByte;
3284 break;
3285 case Primitive::kPrimShort:
3286 load_type = kLoadSignedHalfword;
3287 break;
3288 case Primitive::kPrimChar:
3289 load_type = kLoadUnsignedHalfword;
3290 break;
3291 case Primitive::kPrimInt:
3292 case Primitive::kPrimFloat:
3293 load_type = kLoadWord;
3294 break;
3295 case Primitive::kPrimLong:
3296 case Primitive::kPrimDouble:
3297 load_type = kLoadDoubleword;
3298 break;
3299 case Primitive::kPrimNot:
3300 load_type = kLoadUnsignedWord;
3301 break;
3302 case Primitive::kPrimVoid:
3303 LOG(FATAL) << "Unreachable type " << type;
3304 UNREACHABLE();
3305 }
3306 if (!Primitive::IsFloatingPointType(type)) {
3307 DCHECK(locations->Out().IsRegister());
3308 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003309 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003310 } else {
3311 DCHECK(locations->Out().IsFpuRegister());
3312 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003313 __ LoadFpuFromOffset(load_type, dst, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003314 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003315 // TODO: memory barrier?
Alexey Frunzec061de12017-02-14 13:27:23 -08003316
3317 if (type == Primitive::kPrimNot) {
3318 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3319 __ MaybeUnpoisonHeapReference(dst);
3320 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003321}
3322
3323void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
3324 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
3325 LocationSummary* locations =
3326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3327 locations->SetInAt(0, Location::RequiresRegister());
3328 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
3329 locations->SetInAt(1, Location::RequiresFpuRegister());
3330 } else {
3331 locations->SetInAt(1, Location::RequiresRegister());
3332 }
3333}
3334
3335void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003336 const FieldInfo& field_info,
3337 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003338 Primitive::Type type = field_info.GetFieldType();
3339 LocationSummary* locations = instruction->GetLocations();
3340 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3341 StoreOperandType store_type = kStoreByte;
Alexey Frunzec061de12017-02-14 13:27:23 -08003342 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3343 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003344 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
3345
Alexey Frunze4dda3372015-06-01 18:31:49 -07003346 switch (type) {
3347 case Primitive::kPrimBoolean:
3348 case Primitive::kPrimByte:
3349 store_type = kStoreByte;
3350 break;
3351 case Primitive::kPrimShort:
3352 case Primitive::kPrimChar:
3353 store_type = kStoreHalfword;
3354 break;
3355 case Primitive::kPrimInt:
3356 case Primitive::kPrimFloat:
3357 case Primitive::kPrimNot:
3358 store_type = kStoreWord;
3359 break;
3360 case Primitive::kPrimLong:
3361 case Primitive::kPrimDouble:
3362 store_type = kStoreDoubleword;
3363 break;
3364 case Primitive::kPrimVoid:
3365 LOG(FATAL) << "Unreachable type " << type;
3366 UNREACHABLE();
3367 }
3368 if (!Primitive::IsFloatingPointType(type)) {
3369 DCHECK(locations->InAt(1).IsRegister());
3370 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Alexey Frunzec061de12017-02-14 13:27:23 -08003371 if (kPoisonHeapReferences && needs_write_barrier) {
3372 // Note that in the case where `value` is a null reference,
3373 // we do not enter this block, as a null reference does not
3374 // need poisoning.
3375 DCHECK_EQ(type, Primitive::kPrimNot);
3376 __ PoisonHeapReference(TMP, src);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003377 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08003378 } else {
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003379 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Alexey Frunzec061de12017-02-14 13:27:23 -08003380 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003381 } else {
3382 DCHECK(locations->InAt(1).IsFpuRegister());
3383 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003384 __ StoreFpuToOffset(store_type, src, obj, offset, null_checker);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003385 }
3386
Alexey Frunze4dda3372015-06-01 18:31:49 -07003387 // TODO: memory barriers?
Alexey Frunzec061de12017-02-14 13:27:23 -08003388 if (needs_write_barrier) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003389 DCHECK(locations->InAt(1).IsRegister());
3390 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003391 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003392 }
3393}
3394
3395void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3396 HandleFieldGet(instruction, instruction->GetFieldInfo());
3397}
3398
3399void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3400 HandleFieldGet(instruction, instruction->GetFieldInfo());
3401}
3402
3403void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3404 HandleFieldSet(instruction, instruction->GetFieldInfo());
3405}
3406
3407void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003408 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003409}
3410
Alexey Frunzef63f5692016-12-13 17:43:11 -08003411void InstructionCodeGeneratorMIPS64::GenerateGcRootFieldLoad(
3412 HInstruction* instruction ATTRIBUTE_UNUSED,
3413 Location root,
3414 GpuRegister obj,
3415 uint32_t offset) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003416 GpuRegister root_reg = root.AsRegister<GpuRegister>();
3417 if (kEmitCompilerReadBarrier) {
3418 UNIMPLEMENTED(FATAL) << "for read barrier";
3419 } else {
3420 // Plain GC root load with no read barrier.
3421 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
3422 __ LoadFromOffset(kLoadUnsignedWord, root_reg, obj, offset);
3423 // Note that GC roots are not affected by heap poisoning, thus we
3424 // do not have to unpoison `root_reg` here.
3425 }
3426}
3427
Alexey Frunze4dda3372015-06-01 18:31:49 -07003428void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003429 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3430 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3431 switch (type_check_kind) {
3432 case TypeCheckKind::kExactCheck:
3433 case TypeCheckKind::kAbstractClassCheck:
3434 case TypeCheckKind::kClassHierarchyCheck:
3435 case TypeCheckKind::kArrayObjectCheck:
3436 call_kind = LocationSummary::kNoCall;
3437 break;
3438 case TypeCheckKind::kArrayCheck:
3439 case TypeCheckKind::kUnresolvedCheck:
3440 case TypeCheckKind::kInterfaceCheck:
3441 call_kind = LocationSummary::kCallOnSlowPath;
3442 break;
3443 }
3444
Alexey Frunze4dda3372015-06-01 18:31:49 -07003445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3446 locations->SetInAt(0, Location::RequiresRegister());
3447 locations->SetInAt(1, Location::RequiresRegister());
3448 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003449 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3451}
3452
3453void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003454 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003455 LocationSummary* locations = instruction->GetLocations();
3456 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
3457 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
3458 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003459 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3460 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3461 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3462 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003463 Mips64Label done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003464 SlowPathCodeMIPS64* slow_path = nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003465
3466 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003467 // Avoid this check if we know `obj` is not null.
3468 if (instruction->MustDoNullCheck()) {
3469 __ Move(out, ZERO);
3470 __ Beqzc(obj, &done);
3471 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003473 switch (type_check_kind) {
3474 case TypeCheckKind::kExactCheck: {
3475 // /* HeapReference<Class> */ out = obj->klass_
3476 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3477 __ MaybeUnpoisonHeapReference(out);
3478 // Classes must be equal for the instanceof to succeed.
3479 __ Xor(out, out, cls);
3480 __ Sltiu(out, out, 1);
3481 break;
3482 }
3483
3484 case TypeCheckKind::kAbstractClassCheck: {
3485 // /* HeapReference<Class> */ out = obj->klass_
3486 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3487 __ MaybeUnpoisonHeapReference(out);
3488 // If the class is abstract, we eagerly fetch the super class of the
3489 // object to avoid doing a comparison we know will fail.
3490 Mips64Label loop;
3491 __ Bind(&loop);
3492 // /* HeapReference<Class> */ out = out->super_class_
3493 __ LoadFromOffset(kLoadUnsignedWord, out, out, super_offset);
3494 __ MaybeUnpoisonHeapReference(out);
3495 // If `out` is null, we use it for the result, and jump to `done`.
3496 __ Beqzc(out, &done);
3497 __ Bnec(out, cls, &loop);
3498 __ LoadConst32(out, 1);
3499 break;
3500 }
3501
3502 case TypeCheckKind::kClassHierarchyCheck: {
3503 // /* HeapReference<Class> */ out = obj->klass_
3504 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3505 __ MaybeUnpoisonHeapReference(out);
3506 // Walk over the class hierarchy to find a match.
3507 Mips64Label loop, success;
3508 __ Bind(&loop);
3509 __ Beqc(out, cls, &success);
3510 // /* HeapReference<Class> */ out = out->super_class_
3511 __ LoadFromOffset(kLoadUnsignedWord, out, out, super_offset);
3512 __ MaybeUnpoisonHeapReference(out);
3513 __ Bnezc(out, &loop);
3514 // If `out` is null, we use it for the result, and jump to `done`.
3515 __ Bc(&done);
3516 __ Bind(&success);
3517 __ LoadConst32(out, 1);
3518 break;
3519 }
3520
3521 case TypeCheckKind::kArrayObjectCheck: {
3522 // /* HeapReference<Class> */ out = obj->klass_
3523 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3524 __ MaybeUnpoisonHeapReference(out);
3525 // Do an exact check.
3526 Mips64Label success;
3527 __ Beqc(out, cls, &success);
3528 // Otherwise, we need to check that the object's class is a non-primitive array.
3529 // /* HeapReference<Class> */ out = out->component_type_
3530 __ LoadFromOffset(kLoadUnsignedWord, out, out, component_offset);
3531 __ MaybeUnpoisonHeapReference(out);
3532 // If `out` is null, we use it for the result, and jump to `done`.
3533 __ Beqzc(out, &done);
3534 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
3535 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3536 __ Sltiu(out, out, 1);
3537 __ Bc(&done);
3538 __ Bind(&success);
3539 __ LoadConst32(out, 1);
3540 break;
3541 }
3542
3543 case TypeCheckKind::kArrayCheck: {
3544 // No read barrier since the slow path will retry upon failure.
3545 // /* HeapReference<Class> */ out = obj->klass_
3546 __ LoadFromOffset(kLoadUnsignedWord, out, obj, class_offset);
3547 __ MaybeUnpoisonHeapReference(out);
3548 DCHECK(locations->OnlyCallsOnSlowPath());
3549 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
3550 /* is_fatal */ false);
3551 codegen_->AddSlowPath(slow_path);
3552 __ Bnec(out, cls, slow_path->GetEntryLabel());
3553 __ LoadConst32(out, 1);
3554 break;
3555 }
3556
3557 case TypeCheckKind::kUnresolvedCheck:
3558 case TypeCheckKind::kInterfaceCheck: {
3559 // Note that we indeed only call on slow path, but we always go
3560 // into the slow path for the unresolved and interface check
3561 // cases.
3562 //
3563 // We cannot directly call the InstanceofNonTrivial runtime
3564 // entry point without resorting to a type checking slow path
3565 // here (i.e. by calling InvokeRuntime directly), as it would
3566 // require to assign fixed registers for the inputs of this
3567 // HInstanceOf instruction (following the runtime calling
3568 // convention), which might be cluttered by the potential first
3569 // read barrier emission at the beginning of this method.
3570 //
3571 // TODO: Introduce a new runtime entry point taking the object
3572 // to test (instead of its class) as argument, and let it deal
3573 // with the read barrier issues. This will let us refactor this
3574 // case of the `switch` code as it was previously (with a direct
3575 // call to the runtime not using a type checking slow path).
3576 // This should also be beneficial for the other cases above.
3577 DCHECK(locations->OnlyCallsOnSlowPath());
3578 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction,
3579 /* is_fatal */ false);
3580 codegen_->AddSlowPath(slow_path);
3581 __ Bc(slow_path->GetEntryLabel());
3582 break;
3583 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003584 }
3585
3586 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003587
3588 if (slow_path != nullptr) {
3589 __ Bind(slow_path->GetExitLabel());
3590 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003591}
3592
3593void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
3594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3595 locations->SetOut(Location::ConstantLocation(constant));
3596}
3597
3598void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3599 // Will be generated at use site.
3600}
3601
3602void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
3603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3604 locations->SetOut(Location::ConstantLocation(constant));
3605}
3606
3607void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3608 // Will be generated at use site.
3609}
3610
Calin Juravle175dc732015-08-25 15:42:32 +01003611void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3612 // The trampoline uses the same calling convention as dex calling conventions,
3613 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3614 // the method_idx.
3615 HandleInvoke(invoke);
3616}
3617
3618void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3619 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3620}
3621
Alexey Frunze4dda3372015-06-01 18:31:49 -07003622void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
3623 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
3624 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3625}
3626
3627void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3628 HandleInvoke(invoke);
3629 // The register T0 is required to be used for the hidden argument in
3630 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3631 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3632}
3633
3634void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3635 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3636 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003637 Location receiver = invoke->GetLocations()->InAt(0);
3638 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003639 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003640
3641 // Set the hidden argument.
3642 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3643 invoke->GetDexMethodIndex());
3644
3645 // temp = object->GetClass();
3646 if (receiver.IsStackSlot()) {
3647 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3648 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3649 } else {
3650 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3651 }
3652 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003653 // Instead of simply (possibly) unpoisoning `temp` here, we should
3654 // emit a read barrier for the previous class reference load.
3655 // However this is not required in practice, as this is an
3656 // intermediate/temporary reference and because the current
3657 // concurrent copying collector keeps the from-space memory
3658 // intact/accessible until the end of the marking phase (the
3659 // concurrent copying collector may not in the future).
3660 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003661 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3662 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3663 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003664 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003665 // temp = temp->GetImtEntryAt(method_offset);
3666 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3667 // T9 = temp->GetEntryPoint();
3668 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3669 // T9();
3670 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003671 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003672 DCHECK(!codegen_->IsLeafMethod());
3673 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3674}
3675
3676void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003677 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3678 if (intrinsic.TryDispatch(invoke)) {
3679 return;
3680 }
3681
Alexey Frunze4dda3372015-06-01 18:31:49 -07003682 HandleInvoke(invoke);
3683}
3684
3685void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003686 // Explicit clinit checks triggered by static invokes must have been pruned by
3687 // art::PrepareForRegisterAllocation.
3688 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003689
Chris Larsen3039e382015-08-26 07:54:08 -07003690 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3691 if (intrinsic.TryDispatch(invoke)) {
3692 return;
3693 }
3694
Alexey Frunze4dda3372015-06-01 18:31:49 -07003695 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003696}
3697
Orion Hodsonac141392017-01-13 11:53:47 +00003698void LocationsBuilderMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3699 HandleInvoke(invoke);
3700}
3701
3702void InstructionCodeGeneratorMIPS64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3703 codegen_->GenerateInvokePolymorphicCall(invoke);
3704}
3705
Chris Larsen3039e382015-08-26 07:54:08 -07003706static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003707 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003708 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3709 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003710 return true;
3711 }
3712 return false;
3713}
3714
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003715HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003716 HLoadString::LoadKind desired_string_load_kind) {
3717 if (kEmitCompilerReadBarrier) {
3718 UNIMPLEMENTED(FATAL) << "for read barrier";
3719 }
3720 bool fallback_load = false;
3721 switch (desired_string_load_kind) {
3722 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3723 DCHECK(!GetCompilerOptions().GetCompilePic());
3724 break;
3725 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3726 DCHECK(GetCompilerOptions().GetCompilePic());
3727 break;
3728 case HLoadString::LoadKind::kBootImageAddress:
3729 break;
3730 case HLoadString::LoadKind::kBssEntry:
3731 DCHECK(!Runtime::Current()->UseJitCompilation());
3732 break;
3733 case HLoadString::LoadKind::kDexCacheViaMethod:
3734 break;
3735 case HLoadString::LoadKind::kJitTableAddress:
3736 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003737 break;
3738 }
3739 if (fallback_load) {
3740 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
3741 }
3742 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003743}
3744
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003745HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3746 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003747 if (kEmitCompilerReadBarrier) {
3748 UNIMPLEMENTED(FATAL) << "for read barrier";
3749 }
3750 bool fallback_load = false;
3751 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00003752 case HLoadClass::LoadKind::kInvalid:
3753 LOG(FATAL) << "UNREACHABLE";
3754 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003755 case HLoadClass::LoadKind::kReferrersClass:
3756 break;
3757 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3758 DCHECK(!GetCompilerOptions().GetCompilePic());
3759 break;
3760 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3761 DCHECK(GetCompilerOptions().GetCompilePic());
3762 break;
3763 case HLoadClass::LoadKind::kBootImageAddress:
3764 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003765 case HLoadClass::LoadKind::kBssEntry:
3766 DCHECK(!Runtime::Current()->UseJitCompilation());
3767 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003768 case HLoadClass::LoadKind::kJitTableAddress:
3769 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003770 break;
Alexey Frunzef63f5692016-12-13 17:43:11 -08003771 case HLoadClass::LoadKind::kDexCacheViaMethod:
3772 break;
3773 }
3774 if (fallback_load) {
3775 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
3776 }
3777 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003778}
3779
Vladimir Markodc151b22015-10-15 18:02:30 +01003780HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3781 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003782 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003783 // On MIPS64 we support all dispatch types.
3784 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003785}
3786
Alexey Frunze4dda3372015-06-01 18:31:49 -07003787void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3788 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003789 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003790 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3791 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3792
Alexey Frunze19f6c692016-11-30 19:19:55 -08003793 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003794 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003795 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003796 uint32_t offset =
3797 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003798 __ LoadFromOffset(kLoadDoubleword,
3799 temp.AsRegister<GpuRegister>(),
3800 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003801 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003802 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003803 }
Vladimir Marko58155012015-08-19 12:49:41 +00003804 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003805 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003806 break;
3807 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003808 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3809 kLoadDoubleword,
3810 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003811 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003812 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3813 uint32_t offset = invoke->GetDexCacheArrayOffset();
3814 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00003815 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFileForPcRelativeDexCache(), offset);
Alexey Frunze19f6c692016-11-30 19:19:55 -08003816 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3817 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3818 break;
3819 }
Vladimir Marko58155012015-08-19 12:49:41 +00003820 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003821 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003822 GpuRegister reg = temp.AsRegister<GpuRegister>();
3823 GpuRegister method_reg;
3824 if (current_method.IsRegister()) {
3825 method_reg = current_method.AsRegister<GpuRegister>();
3826 } else {
3827 // TODO: use the appropriate DCHECK() here if possible.
3828 // DCHECK(invoke->GetLocations()->Intrinsified());
3829 DCHECK(!current_method.IsValid());
3830 method_reg = reg;
3831 __ Ld(reg, SP, kCurrentMethodStackOffset);
3832 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003833
Vladimir Marko58155012015-08-19 12:49:41 +00003834 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003835 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003836 reg,
3837 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003838 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003839 // temp = temp[index_in_cache];
3840 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3841 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003842 __ LoadFromOffset(kLoadDoubleword,
3843 reg,
3844 reg,
3845 CodeGenerator::GetCachePointerOffset(index_in_cache));
3846 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003847 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003848 }
3849
Alexey Frunze19f6c692016-11-30 19:19:55 -08003850 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003851 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003852 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003853 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003854 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3855 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3856 __ LoadFromOffset(kLoadDoubleword,
3857 T9,
3858 callee_method.AsRegister<GpuRegister>(),
3859 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003860 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003861 // T9()
3862 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003863 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003864 break;
3865 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003866 DCHECK(!IsLeafMethod());
3867}
3868
3869void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003870 // Explicit clinit checks triggered by static invokes must have been pruned by
3871 // art::PrepareForRegisterAllocation.
3872 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003873
3874 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3875 return;
3876 }
3877
3878 LocationSummary* locations = invoke->GetLocations();
3879 codegen_->GenerateStaticOrDirectCall(invoke,
3880 locations->HasTemps()
3881 ? locations->GetTemp(0)
3882 : Location::NoLocation());
3883 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3884}
3885
Alexey Frunze53afca12015-11-05 16:34:23 -08003886void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003887 // Use the calling convention instead of the location of the receiver, as
3888 // intrinsics may have put the receiver in a different register. In the intrinsics
3889 // slow path, the arguments have been moved to the right place, so here we are
3890 // guaranteed that the receiver is the first register of the calling convention.
3891 InvokeDexCallingConvention calling_convention;
3892 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3893
Alexey Frunze53afca12015-11-05 16:34:23 -08003894 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003895 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3896 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3897 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003898 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003899
3900 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003901 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003902 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08003903 // Instead of simply (possibly) unpoisoning `temp` here, we should
3904 // emit a read barrier for the previous class reference load.
3905 // However this is not required in practice, as this is an
3906 // intermediate/temporary reference and because the current
3907 // concurrent copying collector keeps the from-space memory
3908 // intact/accessible until the end of the marking phase (the
3909 // concurrent copying collector may not in the future).
3910 __ MaybeUnpoisonHeapReference(temp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003911 // temp = temp->GetMethodAt(method_offset);
3912 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3913 // T9 = temp->GetEntryPoint();
3914 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3915 // T9();
3916 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003917 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003918}
3919
3920void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3921 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3922 return;
3923 }
3924
3925 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003926 DCHECK(!codegen_->IsLeafMethod());
3927 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3928}
3929
3930void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00003931 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3932 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003933 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00003934 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Alexey Frunzef63f5692016-12-13 17:43:11 -08003935 cls,
3936 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00003937 calling_convention.GetReturnLocation(Primitive::kPrimNot));
Alexey Frunzef63f5692016-12-13 17:43:11 -08003938 return;
3939 }
Vladimir Marko41559982017-01-06 14:04:23 +00003940 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003941
3942 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3943 ? LocationSummary::kCallOnSlowPath
3944 : LocationSummary::kNoCall;
3945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Vladimir Marko41559982017-01-06 14:04:23 +00003946 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08003947 locations->SetInAt(0, Location::RequiresRegister());
3948 }
3949 locations->SetOut(Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003950}
3951
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003952// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
3953// move.
3954void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00003955 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3956 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3957 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01003958 return;
3959 }
Vladimir Marko41559982017-01-06 14:04:23 +00003960 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01003961
Vladimir Marko41559982017-01-06 14:04:23 +00003962 LocationSummary* locations = cls->GetLocations();
Alexey Frunzef63f5692016-12-13 17:43:11 -08003963 Location out_loc = locations->Out();
3964 GpuRegister out = out_loc.AsRegister<GpuRegister>();
3965 GpuRegister current_method_reg = ZERO;
3966 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3967 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
3968 current_method_reg = locations->InAt(0).AsRegister<GpuRegister>();
3969 }
3970
3971 bool generate_null_check = false;
3972 switch (load_kind) {
3973 case HLoadClass::LoadKind::kReferrersClass:
3974 DCHECK(!cls->CanCallRuntime());
3975 DCHECK(!cls->MustGenerateClinitCheck());
3976 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3977 GenerateGcRootFieldLoad(cls,
3978 out_loc,
3979 current_method_reg,
3980 ArtMethod::DeclaringClassOffset().Int32Value());
3981 break;
3982 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003983 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003984 __ LoadLiteral(out,
3985 kLoadUnsignedWord,
3986 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
3987 cls->GetTypeIndex()));
3988 break;
3989 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00003990 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08003991 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3992 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
3993 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
3994 __ Daddiu(out, AT, /* placeholder */ 0x5678);
3995 break;
3996 }
3997 case HLoadClass::LoadKind::kBootImageAddress: {
3998 DCHECK(!kEmitCompilerReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003999 uint32_t address = dchecked_integral_cast<uint32_t>(
4000 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4001 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004002 __ LoadLiteral(out,
4003 kLoadUnsignedWord,
4004 codegen_->DeduplicateBootImageAddressLiteral(address));
4005 break;
4006 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004007 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004008 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00004009 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08004010 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
4011 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004012 generate_null_check = true;
4013 break;
4014 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08004015 case HLoadClass::LoadKind::kJitTableAddress:
4016 __ LoadLiteral(out,
4017 kLoadUnsignedWord,
4018 codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4019 cls->GetTypeIndex(),
4020 cls->GetClass()));
4021 GenerateGcRootFieldLoad(cls, out_loc, out, 0);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004022 break;
Vladimir Marko41559982017-01-06 14:04:23 +00004023 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004024 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004025 LOG(FATAL) << "UNREACHABLE";
4026 UNREACHABLE();
Alexey Frunzef63f5692016-12-13 17:43:11 -08004027 }
4028
4029 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4030 DCHECK(cls->CanCallRuntime());
4031 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
4032 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4033 codegen_->AddSlowPath(slow_path);
4034 if (generate_null_check) {
4035 __ Beqzc(out, slow_path->GetEntryLabel());
4036 }
4037 if (cls->MustGenerateClinitCheck()) {
4038 GenerateClassInitializationCheck(slow_path, out);
4039 } else {
4040 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004041 }
4042 }
4043}
4044
David Brazdilcb1c0552015-08-04 16:22:25 +01004045static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004046 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01004047}
4048
Alexey Frunze4dda3372015-06-01 18:31:49 -07004049void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
4050 LocationSummary* locations =
4051 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4052 locations->SetOut(Location::RequiresRegister());
4053}
4054
4055void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
4056 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01004057 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
4058}
4059
4060void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
4061 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4062}
4063
4064void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4065 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066}
4067
Alexey Frunze4dda3372015-06-01 18:31:49 -07004068void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004069 HLoadString::LoadKind load_kind = load->GetLoadKind();
4070 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004071 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004072 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
4073 InvokeRuntimeCallingConvention calling_convention;
4074 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
4075 } else {
4076 locations->SetOut(Location::RequiresRegister());
4077 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004078}
4079
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004080// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4081// move.
4082void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunzef63f5692016-12-13 17:43:11 -08004083 HLoadString::LoadKind load_kind = load->GetLoadKind();
4084 LocationSummary* locations = load->GetLocations();
4085 Location out_loc = locations->Out();
4086 GpuRegister out = out_loc.AsRegister<GpuRegister>();
4087
4088 switch (load_kind) {
4089 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004090 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004091 __ LoadLiteral(out,
4092 kLoadUnsignedWord,
4093 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4094 load->GetStringIndex()));
4095 return; // No dex cache slow path.
4096 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4097 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
4098 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004099 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzef63f5692016-12-13 17:43:11 -08004100 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, AT);
4101 __ Daddiu(out, AT, /* placeholder */ 0x5678);
4102 return; // No dex cache slow path.
4103 }
4104 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004105 uint32_t address = dchecked_integral_cast<uint32_t>(
4106 reinterpret_cast<uintptr_t>(load->GetString().Get()));
4107 DCHECK_NE(address, 0u);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004108 __ LoadLiteral(out,
4109 kLoadUnsignedWord,
4110 codegen_->DeduplicateBootImageAddressLiteral(address));
4111 return; // No dex cache slow path.
4112 }
4113 case HLoadString::LoadKind::kBssEntry: {
4114 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4115 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004116 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec061de12017-02-14 13:27:23 -08004117 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out);
4118 GenerateGcRootFieldLoad(load, out_loc, out, /* placeholder */ 0x5678);
Alexey Frunzef63f5692016-12-13 17:43:11 -08004119 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
4120 codegen_->AddSlowPath(slow_path);
4121 __ Beqzc(out, slow_path->GetEntryLabel());
4122 __ Bind(slow_path->GetExitLabel());
4123 return;
4124 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08004125 case HLoadString::LoadKind::kJitTableAddress:
4126 __ LoadLiteral(out,
4127 kLoadUnsignedWord,
4128 codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
4129 load->GetStringIndex(),
4130 load->GetString()));
4131 GenerateGcRootFieldLoad(load, out_loc, out, 0);
4132 return;
Alexey Frunzef63f5692016-12-13 17:43:11 -08004133 default:
4134 break;
4135 }
4136
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004137 // TODO: Re-add the compiler code to do string dex cache lookup again.
Alexey Frunzef63f5692016-12-13 17:43:11 -08004138 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
4139 InvokeRuntimeCallingConvention calling_convention;
4140 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
4141 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
4142 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004143}
4144
Alexey Frunze4dda3372015-06-01 18:31:49 -07004145void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
4146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4147 locations->SetOut(Location::ConstantLocation(constant));
4148}
4149
4150void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4151 // Will be generated at use site.
4152}
4153
4154void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
4155 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004156 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004157 InvokeRuntimeCallingConvention calling_convention;
4158 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4159}
4160
4161void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004162 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004163 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01004164 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004165 if (instruction->IsEnter()) {
4166 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4167 } else {
4168 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4169 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004170}
4171
4172void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
4173 LocationSummary* locations =
4174 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4175 switch (mul->GetResultType()) {
4176 case Primitive::kPrimInt:
4177 case Primitive::kPrimLong:
4178 locations->SetInAt(0, Location::RequiresRegister());
4179 locations->SetInAt(1, Location::RequiresRegister());
4180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4181 break;
4182
4183 case Primitive::kPrimFloat:
4184 case Primitive::kPrimDouble:
4185 locations->SetInAt(0, Location::RequiresFpuRegister());
4186 locations->SetInAt(1, Location::RequiresFpuRegister());
4187 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4188 break;
4189
4190 default:
4191 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4192 }
4193}
4194
4195void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
4196 Primitive::Type type = instruction->GetType();
4197 LocationSummary* locations = instruction->GetLocations();
4198
4199 switch (type) {
4200 case Primitive::kPrimInt:
4201 case Primitive::kPrimLong: {
4202 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4203 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
4204 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
4205 if (type == Primitive::kPrimInt)
4206 __ MulR6(dst, lhs, rhs);
4207 else
4208 __ Dmul(dst, lhs, rhs);
4209 break;
4210 }
4211 case Primitive::kPrimFloat:
4212 case Primitive::kPrimDouble: {
4213 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4214 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
4215 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
4216 if (type == Primitive::kPrimFloat)
4217 __ MulS(dst, lhs, rhs);
4218 else
4219 __ MulD(dst, lhs, rhs);
4220 break;
4221 }
4222 default:
4223 LOG(FATAL) << "Unexpected mul type " << type;
4224 }
4225}
4226
4227void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
4228 LocationSummary* locations =
4229 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4230 switch (neg->GetResultType()) {
4231 case Primitive::kPrimInt:
4232 case Primitive::kPrimLong:
4233 locations->SetInAt(0, Location::RequiresRegister());
4234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4235 break;
4236
4237 case Primitive::kPrimFloat:
4238 case Primitive::kPrimDouble:
4239 locations->SetInAt(0, Location::RequiresFpuRegister());
4240 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4241 break;
4242
4243 default:
4244 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4245 }
4246}
4247
4248void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
4249 Primitive::Type type = instruction->GetType();
4250 LocationSummary* locations = instruction->GetLocations();
4251
4252 switch (type) {
4253 case Primitive::kPrimInt:
4254 case Primitive::kPrimLong: {
4255 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4256 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4257 if (type == Primitive::kPrimInt)
4258 __ Subu(dst, ZERO, src);
4259 else
4260 __ Dsubu(dst, ZERO, src);
4261 break;
4262 }
4263 case Primitive::kPrimFloat:
4264 case Primitive::kPrimDouble: {
4265 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4266 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4267 if (type == Primitive::kPrimFloat)
4268 __ NegS(dst, src);
4269 else
4270 __ NegD(dst, src);
4271 break;
4272 }
4273 default:
4274 LOG(FATAL) << "Unexpected neg type " << type;
4275 }
4276}
4277
4278void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
4279 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004281 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004282 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004283 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4284 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004285}
4286
4287void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004288 // Note: if heap poisoning is enabled, the entry point takes care
4289 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004290 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
4291 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004292}
4293
4294void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
4295 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004297 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004298 if (instruction->IsStringAlloc()) {
4299 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4300 } else {
4301 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004302 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004303 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4304}
4305
4306void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08004307 // Note: if heap poisoning is enabled, the entry point takes care
4308 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004309 if (instruction->IsStringAlloc()) {
4310 // String is allocated through StringFactory. Call NewEmptyString entry point.
4311 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02004312 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07004313 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004314 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4315 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
4316 __ Jalr(T9);
4317 __ Nop();
4318 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4319 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01004320 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004321 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004322 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004323}
4324
4325void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
4326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4327 locations->SetInAt(0, Location::RequiresRegister());
4328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4329}
4330
4331void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
4332 Primitive::Type type = instruction->GetType();
4333 LocationSummary* locations = instruction->GetLocations();
4334
4335 switch (type) {
4336 case Primitive::kPrimInt:
4337 case Primitive::kPrimLong: {
4338 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4339 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4340 __ Nor(dst, src, ZERO);
4341 break;
4342 }
4343
4344 default:
4345 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4346 }
4347}
4348
4349void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4351 locations->SetInAt(0, Location::RequiresRegister());
4352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4353}
4354
4355void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
4356 LocationSummary* locations = instruction->GetLocations();
4357 __ Xori(locations->Out().AsRegister<GpuRegister>(),
4358 locations->InAt(0).AsRegister<GpuRegister>(),
4359 1);
4360}
4361
4362void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004363 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4364 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004365}
4366
Calin Juravle2ae48182016-03-16 14:05:09 +00004367void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4368 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004369 return;
4370 }
4371 Location obj = instruction->GetLocations()->InAt(0);
4372
4373 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004374 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004375}
4376
Calin Juravle2ae48182016-03-16 14:05:09 +00004377void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004378 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004379 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004380
4381 Location obj = instruction->GetLocations()->InAt(0);
4382
4383 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
4384}
4385
4386void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004387 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004388}
4389
4390void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
4391 HandleBinaryOp(instruction);
4392}
4393
4394void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
4395 HandleBinaryOp(instruction);
4396}
4397
4398void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4399 LOG(FATAL) << "Unreachable";
4400}
4401
4402void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
4403 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4404}
4405
4406void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
4407 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4408 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4409 if (location.IsStackSlot()) {
4410 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4411 } else if (location.IsDoubleStackSlot()) {
4412 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4413 }
4414 locations->SetOut(location);
4415}
4416
4417void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
4418 ATTRIBUTE_UNUSED) {
4419 // Nothing to do, the parameter is already at its location.
4420}
4421
4422void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
4423 LocationSummary* locations =
4424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4425 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4426}
4427
4428void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
4429 ATTRIBUTE_UNUSED) {
4430 // Nothing to do, the method is already at its location.
4431}
4432
4433void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
4434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004435 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004436 locations->SetInAt(i, Location::Any());
4437 }
4438 locations->SetOut(Location::Any());
4439}
4440
4441void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4442 LOG(FATAL) << "Unreachable";
4443}
4444
4445void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
4446 Primitive::Type type = rem->GetResultType();
4447 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004448 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4449 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4451
4452 switch (type) {
4453 case Primitive::kPrimInt:
4454 case Primitive::kPrimLong:
4455 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07004456 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07004457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4458 break;
4459
4460 case Primitive::kPrimFloat:
4461 case Primitive::kPrimDouble: {
4462 InvokeRuntimeCallingConvention calling_convention;
4463 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4464 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4465 locations->SetOut(calling_convention.GetReturnLocation(type));
4466 break;
4467 }
4468
4469 default:
4470 LOG(FATAL) << "Unexpected rem type " << type;
4471 }
4472}
4473
4474void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
4475 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07004476
4477 switch (type) {
4478 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07004479 case Primitive::kPrimLong:
4480 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004481 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07004482
4483 case Primitive::kPrimFloat:
4484 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01004485 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
4486 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004487 if (type == Primitive::kPrimFloat) {
4488 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4489 } else {
4490 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4491 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004492 break;
4493 }
4494 default:
4495 LOG(FATAL) << "Unexpected rem type " << type;
4496 }
4497}
4498
4499void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4500 memory_barrier->SetLocations(nullptr);
4501}
4502
4503void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4504 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4505}
4506
4507void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
4508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4509 Primitive::Type return_type = ret->InputAt(0)->GetType();
4510 locations->SetInAt(0, Mips64ReturnLocation(return_type));
4511}
4512
4513void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4514 codegen_->GenerateFrameExit();
4515}
4516
4517void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
4518 ret->SetLocations(nullptr);
4519}
4520
4521void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4522 codegen_->GenerateFrameExit();
4523}
4524
Alexey Frunze92d90602015-12-18 18:16:36 -08004525void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
4526 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004527}
4528
Alexey Frunze92d90602015-12-18 18:16:36 -08004529void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
4530 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004531}
4532
Alexey Frunze4dda3372015-06-01 18:31:49 -07004533void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
4534 HandleShift(shl);
4535}
4536
4537void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
4538 HandleShift(shl);
4539}
4540
4541void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
4542 HandleShift(shr);
4543}
4544
4545void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
4546 HandleShift(shr);
4547}
4548
Alexey Frunze4dda3372015-06-01 18:31:49 -07004549void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
4550 HandleBinaryOp(instruction);
4551}
4552
4553void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
4554 HandleBinaryOp(instruction);
4555}
4556
4557void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4558 HandleFieldGet(instruction, instruction->GetFieldInfo());
4559}
4560
4561void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4562 HandleFieldGet(instruction, instruction->GetFieldInfo());
4563}
4564
4565void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4566 HandleFieldSet(instruction, instruction->GetFieldInfo());
4567}
4568
4569void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01004570 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004571}
4572
Calin Juravlee460d1d2015-09-29 04:52:17 +01004573void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
4574 HUnresolvedInstanceFieldGet* instruction) {
4575 FieldAccessCallingConventionMIPS64 calling_convention;
4576 codegen_->CreateUnresolvedFieldLocationSummary(
4577 instruction, instruction->GetFieldType(), calling_convention);
4578}
4579
4580void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
4581 HUnresolvedInstanceFieldGet* instruction) {
4582 FieldAccessCallingConventionMIPS64 calling_convention;
4583 codegen_->GenerateUnresolvedFieldAccess(instruction,
4584 instruction->GetFieldType(),
4585 instruction->GetFieldIndex(),
4586 instruction->GetDexPc(),
4587 calling_convention);
4588}
4589
4590void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
4591 HUnresolvedInstanceFieldSet* instruction) {
4592 FieldAccessCallingConventionMIPS64 calling_convention;
4593 codegen_->CreateUnresolvedFieldLocationSummary(
4594 instruction, instruction->GetFieldType(), calling_convention);
4595}
4596
4597void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
4598 HUnresolvedInstanceFieldSet* instruction) {
4599 FieldAccessCallingConventionMIPS64 calling_convention;
4600 codegen_->GenerateUnresolvedFieldAccess(instruction,
4601 instruction->GetFieldType(),
4602 instruction->GetFieldIndex(),
4603 instruction->GetDexPc(),
4604 calling_convention);
4605}
4606
4607void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
4608 HUnresolvedStaticFieldGet* instruction) {
4609 FieldAccessCallingConventionMIPS64 calling_convention;
4610 codegen_->CreateUnresolvedFieldLocationSummary(
4611 instruction, instruction->GetFieldType(), calling_convention);
4612}
4613
4614void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
4615 HUnresolvedStaticFieldGet* instruction) {
4616 FieldAccessCallingConventionMIPS64 calling_convention;
4617 codegen_->GenerateUnresolvedFieldAccess(instruction,
4618 instruction->GetFieldType(),
4619 instruction->GetFieldIndex(),
4620 instruction->GetDexPc(),
4621 calling_convention);
4622}
4623
4624void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
4625 HUnresolvedStaticFieldSet* instruction) {
4626 FieldAccessCallingConventionMIPS64 calling_convention;
4627 codegen_->CreateUnresolvedFieldLocationSummary(
4628 instruction, instruction->GetFieldType(), calling_convention);
4629}
4630
4631void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
4632 HUnresolvedStaticFieldSet* instruction) {
4633 FieldAccessCallingConventionMIPS64 calling_convention;
4634 codegen_->GenerateUnresolvedFieldAccess(instruction,
4635 instruction->GetFieldType(),
4636 instruction->GetFieldIndex(),
4637 instruction->GetDexPc(),
4638 calling_convention);
4639}
4640
Alexey Frunze4dda3372015-06-01 18:31:49 -07004641void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01004642 LocationSummary* locations =
4643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004644 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07004645}
4646
4647void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
4648 HBasicBlock* block = instruction->GetBlock();
4649 if (block->GetLoopInformation() != nullptr) {
4650 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4651 // The back edge will generate the suspend check.
4652 return;
4653 }
4654 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4655 // The goto will generate the suspend check.
4656 return;
4657 }
4658 GenerateSuspendCheck(instruction, nullptr);
4659}
4660
Alexey Frunze4dda3372015-06-01 18:31:49 -07004661void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
4662 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004664 InvokeRuntimeCallingConvention calling_convention;
4665 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4666}
4667
4668void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01004669 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004670 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4671}
4672
4673void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4674 Primitive::Type input_type = conversion->GetInputType();
4675 Primitive::Type result_type = conversion->GetResultType();
4676 DCHECK_NE(input_type, result_type);
4677
4678 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4679 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4680 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4681 }
4682
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004683 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
4684
4685 if (Primitive::IsFloatingPointType(input_type)) {
4686 locations->SetInAt(0, Location::RequiresFpuRegister());
4687 } else {
4688 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07004689 }
4690
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004691 if (Primitive::IsFloatingPointType(result_type)) {
4692 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004693 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004695 }
4696}
4697
4698void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
4699 LocationSummary* locations = conversion->GetLocations();
4700 Primitive::Type result_type = conversion->GetResultType();
4701 Primitive::Type input_type = conversion->GetInputType();
4702
4703 DCHECK_NE(input_type, result_type);
4704
4705 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4706 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4707 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4708
4709 switch (result_type) {
4710 case Primitive::kPrimChar:
4711 __ Andi(dst, src, 0xFFFF);
4712 break;
4713 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004714 if (input_type == Primitive::kPrimLong) {
4715 // Type conversion from long to types narrower than int is a result of code
4716 // transformations. To avoid unpredictable results for SEB and SEH, we first
4717 // need to sign-extend the low 32-bit value into bits 32 through 63.
4718 __ Sll(dst, src, 0);
4719 __ Seb(dst, dst);
4720 } else {
4721 __ Seb(dst, src);
4722 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004723 break;
4724 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00004725 if (input_type == Primitive::kPrimLong) {
4726 // Type conversion from long to types narrower than int is a result of code
4727 // transformations. To avoid unpredictable results for SEB and SEH, we first
4728 // need to sign-extend the low 32-bit value into bits 32 through 63.
4729 __ Sll(dst, src, 0);
4730 __ Seh(dst, dst);
4731 } else {
4732 __ Seh(dst, src);
4733 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004734 break;
4735 case Primitive::kPrimInt:
4736 case Primitive::kPrimLong:
Goran Jakovljevic992bdb92016-12-28 16:21:48 +01004737 // Sign-extend 32-bit int into bits 32 through 63 for int-to-long and long-to-int
4738 // conversions, except when the input and output registers are the same and we are not
4739 // converting longs to shorter types. In these cases, do nothing.
4740 if ((input_type == Primitive::kPrimLong) || (dst != src)) {
4741 __ Sll(dst, src, 0);
4742 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004743 break;
4744
4745 default:
4746 LOG(FATAL) << "Unexpected type conversion from " << input_type
4747 << " to " << result_type;
4748 }
4749 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004750 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4751 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
4752 if (input_type == Primitive::kPrimLong) {
4753 __ Dmtc1(src, FTMP);
4754 if (result_type == Primitive::kPrimFloat) {
4755 __ Cvtsl(dst, FTMP);
4756 } else {
4757 __ Cvtdl(dst, FTMP);
4758 }
4759 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07004760 __ Mtc1(src, FTMP);
4761 if (result_type == Primitive::kPrimFloat) {
4762 __ Cvtsw(dst, FTMP);
4763 } else {
4764 __ Cvtdw(dst, FTMP);
4765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004766 }
4767 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4768 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004769 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
4770 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4771 Mips64Label truncate;
4772 Mips64Label done;
4773
4774 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4775 // value when the input is either a NaN or is outside of the range of the output type
4776 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4777 // the same result.
4778 //
4779 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4780 // value of the output type if the input is outside of the range after the truncation or
4781 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4782 // results. This matches the desired float/double-to-int/long conversion exactly.
4783 //
4784 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4785 //
4786 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4787 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4788 // even though it must be NAN2008=1 on R6.
4789 //
4790 // The code takes care of the different behaviors by first comparing the input to the
4791 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4792 // If the input is greater than or equal to the minimum, it procedes to the truncate
4793 // instruction, which will handle such an input the same way irrespective of NAN2008.
4794 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4795 // in order to return either zero or the minimum value.
4796 //
4797 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4798 // truncate instruction for MIPS64R6.
4799 if (input_type == Primitive::kPrimFloat) {
4800 uint32_t min_val = (result_type == Primitive::kPrimLong)
4801 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4802 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4803 __ LoadConst32(TMP, min_val);
4804 __ Mtc1(TMP, FTMP);
4805 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004806 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004807 uint64_t min_val = (result_type == Primitive::kPrimLong)
4808 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4809 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4810 __ LoadConst64(TMP, min_val);
4811 __ Dmtc1(TMP, FTMP);
4812 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004813 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004814
4815 __ Bc1nez(FTMP, &truncate);
4816
4817 if (input_type == Primitive::kPrimFloat) {
4818 __ CmpEqS(FTMP, src, src);
4819 } else {
4820 __ CmpEqD(FTMP, src, src);
4821 }
4822 if (result_type == Primitive::kPrimLong) {
4823 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4824 } else {
4825 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4826 }
4827 __ Mfc1(TMP, FTMP);
4828 __ And(dst, dst, TMP);
4829
4830 __ Bc(&done);
4831
4832 __ Bind(&truncate);
4833
4834 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004835 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004836 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004837 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004838 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004839 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004840 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004841 } else {
4842 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004843 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004844 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004845 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004846 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004847 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004848 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004849
4850 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004851 } else if (Primitive::IsFloatingPointType(result_type) &&
4852 Primitive::IsFloatingPointType(input_type)) {
4853 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4854 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4855 if (result_type == Primitive::kPrimFloat) {
4856 __ Cvtsd(dst, src);
4857 } else {
4858 __ Cvtds(dst, src);
4859 }
4860 } else {
4861 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4862 << " to " << result_type;
4863 }
4864}
4865
4866void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4867 HandleShift(ushr);
4868}
4869
4870void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4871 HandleShift(ushr);
4872}
4873
4874void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4875 HandleBinaryOp(instruction);
4876}
4877
4878void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4879 HandleBinaryOp(instruction);
4880}
4881
4882void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4883 // Nothing to do, this should be removed during prepare for register allocator.
4884 LOG(FATAL) << "Unreachable";
4885}
4886
4887void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4888 // Nothing to do, this should be removed during prepare for register allocator.
4889 LOG(FATAL) << "Unreachable";
4890}
4891
4892void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004893 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004894}
4895
4896void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004897 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004898}
4899
4900void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004901 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004902}
4903
4904void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004905 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004906}
4907
4908void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004909 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004910}
4911
4912void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004913 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004914}
4915
4916void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004917 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004918}
4919
4920void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004921 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004922}
4923
4924void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004925 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004926}
4927
4928void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004929 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004930}
4931
4932void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004933 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004934}
4935
4936void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004937 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004938}
4939
Aart Bike9f37602015-10-09 11:15:55 -07004940void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004941 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004942}
4943
4944void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004945 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004946}
4947
4948void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004949 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004950}
4951
4952void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004953 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004954}
4955
4956void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004957 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004958}
4959
4960void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004961 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004962}
4963
4964void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004965 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004966}
4967
4968void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004969 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004970}
4971
Mark Mendellfe57faa2015-09-18 09:26:15 -04004972// Simple implementation of packed switch - generate cascaded compare/jumps.
4973void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4974 LocationSummary* locations =
4975 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4976 locations->SetInAt(0, Location::RequiresRegister());
4977}
4978
Alexey Frunze0960ac52016-12-20 17:24:59 -08004979void InstructionCodeGeneratorMIPS64::GenPackedSwitchWithCompares(GpuRegister value_reg,
4980 int32_t lower_bound,
4981 uint32_t num_entries,
4982 HBasicBlock* switch_block,
4983 HBasicBlock* default_block) {
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004984 // Create a set of compare/jumps.
4985 GpuRegister temp_reg = TMP;
Alexey Frunze0960ac52016-12-20 17:24:59 -08004986 __ Addiu32(temp_reg, value_reg, -lower_bound);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004987 // Jump to default if index is negative
4988 // Note: We don't check the case that index is positive while value < lower_bound, because in
4989 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4990 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4991
Alexey Frunze0960ac52016-12-20 17:24:59 -08004992 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004993 // Jump to successors[0] if value == lower_bound.
4994 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4995 int32_t last_index = 0;
4996 for (; num_entries - last_index > 2; last_index += 2) {
4997 __ Addiu(temp_reg, temp_reg, -2);
4998 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4999 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5000 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5001 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5002 }
5003 if (num_entries - last_index == 2) {
5004 // The last missing case_value.
5005 __ Addiu(temp_reg, temp_reg, -1);
5006 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005007 }
5008
5009 // And the default for any other value.
Alexey Frunze0960ac52016-12-20 17:24:59 -08005010 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07005011 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005012 }
5013}
5014
Alexey Frunze0960ac52016-12-20 17:24:59 -08005015void InstructionCodeGeneratorMIPS64::GenTableBasedPackedSwitch(GpuRegister value_reg,
5016 int32_t lower_bound,
5017 uint32_t num_entries,
5018 HBasicBlock* switch_block,
5019 HBasicBlock* default_block) {
5020 // Create a jump table.
5021 std::vector<Mips64Label*> labels(num_entries);
5022 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5023 for (uint32_t i = 0; i < num_entries; i++) {
5024 labels[i] = codegen_->GetLabelOf(successors[i]);
5025 }
5026 JumpTable* table = __ CreateJumpTable(std::move(labels));
5027
5028 // Is the value in range?
5029 __ Addiu32(TMP, value_reg, -lower_bound);
5030 __ LoadConst32(AT, num_entries);
5031 __ Bgeuc(TMP, AT, codegen_->GetLabelOf(default_block));
5032
5033 // We are in the range of the table.
5034 // Load the target address from the jump table, indexing by the value.
5035 __ LoadLabelAddress(AT, table->GetLabel());
5036 __ Sll(TMP, TMP, 2);
5037 __ Daddu(TMP, TMP, AT);
5038 __ Lw(TMP, TMP, 0);
5039 // Compute the absolute target address by adding the table start address
5040 // (the table contains offsets to targets relative to its start).
5041 __ Daddu(TMP, TMP, AT);
5042 // And jump.
5043 __ Jr(TMP);
5044 __ Nop();
5045}
5046
5047void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5048 int32_t lower_bound = switch_instr->GetStartValue();
5049 uint32_t num_entries = switch_instr->GetNumEntries();
5050 LocationSummary* locations = switch_instr->GetLocations();
5051 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
5052 HBasicBlock* switch_block = switch_instr->GetBlock();
5053 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5054
5055 if (num_entries > kPackedSwitchJumpTableThreshold) {
5056 GenTableBasedPackedSwitch(value_reg,
5057 lower_bound,
5058 num_entries,
5059 switch_block,
5060 default_block);
5061 } else {
5062 GenPackedSwitchWithCompares(value_reg,
5063 lower_bound,
5064 num_entries,
5065 switch_block,
5066 default_block);
5067 }
5068}
5069
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005070void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
5071 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
5072}
5073
5074void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
5075 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
5076}
5077
Alexey Frunze4dda3372015-06-01 18:31:49 -07005078} // namespace mips64
5079} // namespace art