blob: 08086820d19f4c825d757e82220c2d68903b34dd [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
94 // TODO: review
95
96 // TODO: shouldn't we use a whole machine word per argument on the stack?
97 // Implicit 4-byte method pointer (and such) will cause misalignment.
98
99 return next_location;
100}
101
102Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
103 return Mips64ReturnLocation(type);
104}
105
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100106// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
107#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700108#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700109
110class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700113
114 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100115 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700116 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
117 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000118 if (instruction_->CanThrowIntoCatchBlock()) {
119 // Live registers will be restored in the catch block if caught.
120 SaveLiveRegisters(codegen, instruction_->GetLocations());
121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700122 // We're moving two locations to locations that could overlap, so we need a parallel
123 // move resolver.
124 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100125 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700126 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
127 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700129 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
130 Primitive::kPrimInt);
Serban Constantinescufc734082016-07-19 17:18:07 +0100131 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
132 ? kQuickThrowStringBounds
133 : kQuickThrowArrayBounds;
134 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100135 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700144 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
145};
146
147class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
148 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000149 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700150
151 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
152 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
153 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100154 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700155 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
156 }
157
Alexandre Rames8158f282015-08-07 10:26:17 +0100158 bool IsFatal() const OVERRIDE { return true; }
159
Roland Levillain46648892015-06-19 16:07:18 +0100160 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
161
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700163 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
164};
165
166class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
167 public:
168 LoadClassSlowPathMIPS64(HLoadClass* cls,
169 HInstruction* at,
170 uint32_t dex_pc,
171 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000172 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700173 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
174 }
175
176 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
177 LocationSummary* locations = at_->GetLocations();
178 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
179
180 __ Bind(GetEntryLabel());
181 SaveLiveRegisters(codegen, locations);
182
183 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800184 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +0100185 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
186 : kQuickInitializeType;
187 mips64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700188 if (do_clinit_) {
189 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
190 } else {
191 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
192 }
193
194 // Move the class to the desired location.
195 Location out = locations->Out();
196 if (out.IsValid()) {
197 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
198 Primitive::Type type = at_->GetType();
199 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
200 }
201
202 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700203 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700204 }
205
Roland Levillain46648892015-06-19 16:07:18 +0100206 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
207
Alexey Frunze4dda3372015-06-01 18:31:49 -0700208 private:
209 // The class this slow path will load.
210 HLoadClass* const cls_;
211
212 // The instruction where this slow path is happening.
213 // (Might be the load class or an initialization check).
214 HInstruction* const at_;
215
216 // The dex PC of `at_`.
217 const uint32_t dex_pc_;
218
219 // Whether to initialize the class.
220 const bool do_clinit_;
221
222 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
223};
224
225class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
226 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000227 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = instruction_->GetLocations();
231 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
232 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800238 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
David Srbecky9cd6d372016-02-09 15:24:47 +0000239 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufc734082016-07-19 17:18:07 +0100240 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700241 instruction_,
242 instruction_->GetDexPc(),
243 this);
244 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
245 Primitive::Type type = instruction_->GetType();
246 mips64_codegen->MoveLocation(locations->Out(),
247 calling_convention.GetReturnLocation(type),
248 type);
249
250 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700251 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700252 }
253
Roland Levillain46648892015-06-19 16:07:18 +0100254 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
255
Alexey Frunze4dda3372015-06-01 18:31:49 -0700256 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700257 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
258};
259
260class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
261 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000262 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700263
264 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
265 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
266 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000267 if (instruction_->CanThrowIntoCatchBlock()) {
268 // Live registers will be restored in the catch block if caught.
269 SaveLiveRegisters(codegen, instruction_->GetLocations());
270 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100271 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272 instruction_,
273 instruction_->GetDexPc(),
274 this);
275 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
276 }
277
Alexandre Rames8158f282015-08-07 10:26:17 +0100278 bool IsFatal() const OVERRIDE { return true; }
279
Roland Levillain46648892015-06-19 16:07:18 +0100280 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
281
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700283 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
284};
285
286class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
287 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100288 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000289 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700290
291 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
292 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
293 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100294 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700296 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700297 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700298 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700299 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700300 }
301 }
302
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700303 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700304 DCHECK(successor_ == nullptr);
305 return &return_label_;
306 }
307
Roland Levillain46648892015-06-19 16:07:18 +0100308 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
309
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700311 // If not null, the block to branch to after the suspend check.
312 HBasicBlock* const successor_;
313
314 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316
317 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
318};
319
320class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
321 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000322 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700323
324 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
325 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800326
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 DCHECK(instruction_->IsCheckCast()
329 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
330 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
331
332 __ Bind(GetEntryLabel());
333 SaveLiveRegisters(codegen, locations);
334
335 // We're moving two locations to locations that could overlap, so we need a parallel
336 // move resolver.
337 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800338 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700339 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
340 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800341 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
343 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700344 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100345 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800346 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700347 Primitive::Type ret_type = instruction_->GetType();
348 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
349 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700350 } else {
351 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800352 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
353 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 }
355
356 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700357 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 }
359
Roland Levillain46648892015-06-19 16:07:18 +0100360 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
361
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
364};
365
366class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
367 public:
Aart Bik42249c32016-01-07 15:33:50 -0800368 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000369 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800372 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700373 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100374 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000375 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 }
377
Roland Levillain46648892015-06-19 16:07:18 +0100378 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
379
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700381 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
382};
383
384CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
385 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100386 const CompilerOptions& compiler_options,
387 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 : CodeGenerator(graph,
389 kNumberOfGpuRegisters,
390 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000391 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700392 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
393 arraysize(kCoreCalleeSaves)),
394 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
395 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100396 compiler_options,
397 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100398 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 location_builder_(graph, this),
400 instruction_visitor_(graph, this),
401 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100402 assembler_(graph->GetArena()),
Alexey Frunze19f6c692016-11-30 19:19:55 -0800403 isa_features_(isa_features),
404 uint64_literals_(std::less<uint64_t>(),
405 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
406 method_patches_(MethodReferenceComparator(),
407 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
408 call_patches_(MethodReferenceComparator(),
409 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
410 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
411 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 // Save RA (containing the return address) to mimic Quick.
413 AddAllocatedRegister(Location::RegisterLocation(RA));
414}
415
416#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100417// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
418#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700419#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700420
421void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700422 // Ensure that we fix up branches.
423 __ FinalizeCode();
424
425 // Adjust native pc offsets in stack maps.
426 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
427 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
428 uint32_t new_position = __ GetAdjustedPosition(old_position);
429 DCHECK_GE(new_position, old_position);
430 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
431 }
432
433 // Adjust pc offsets for the disassembly information.
434 if (disasm_info_ != nullptr) {
435 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
436 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
437 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
438 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
439 it.second.start = __ GetAdjustedPosition(it.second.start);
440 it.second.end = __ GetAdjustedPosition(it.second.end);
441 }
442 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
443 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
444 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
445 }
446 }
447
Alexey Frunze4dda3372015-06-01 18:31:49 -0700448 CodeGenerator::Finalize(allocator);
449}
450
451Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
452 return codegen_->GetAssembler();
453}
454
455void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100456 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700457 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
458}
459
460void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100461 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700462 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
463}
464
465void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
466 // Pop reg
467 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200468 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469}
470
471void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
472 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200473 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700474 __ Sd(GpuRegister(reg), SP, 0);
475}
476
477void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
478 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
479 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
480 // Allocate a scratch register other than TMP, if available.
481 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
482 // automatically unspilled when the scratch scope object is destroyed).
483 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
484 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200485 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700486 __ LoadFromOffset(load_type,
487 GpuRegister(ensure_scratch.GetRegister()),
488 SP,
489 index1 + stack_offset);
490 __ LoadFromOffset(load_type,
491 TMP,
492 SP,
493 index2 + stack_offset);
494 __ StoreToOffset(store_type,
495 GpuRegister(ensure_scratch.GetRegister()),
496 SP,
497 index2 + stack_offset);
498 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
499}
500
501static dwarf::Reg DWARFReg(GpuRegister reg) {
502 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
503}
504
David Srbeckyba702002016-02-01 18:15:29 +0000505static dwarf::Reg DWARFReg(FpuRegister reg) {
506 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
507}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700508
509void CodeGeneratorMIPS64::GenerateFrameEntry() {
510 __ Bind(&frame_entry_label_);
511
512 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
513
514 if (do_overflow_check) {
515 __ LoadFromOffset(kLoadWord,
516 ZERO,
517 SP,
518 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
519 RecordPcInfo(nullptr, 0);
520 }
521
Alexey Frunze4dda3372015-06-01 18:31:49 -0700522 if (HasEmptyFrame()) {
523 return;
524 }
525
526 // Make sure the frame size isn't unreasonably large. Per the various APIs
527 // it looks like it should always be less than 2GB in size, which allows
528 // us using 32-bit signed offsets from the stack pointer.
529 if (GetFrameSize() > 0x7FFFFFFF)
530 LOG(FATAL) << "Stack frame larger than 2GB";
531
532 // Spill callee-saved registers.
533 // Note that their cumulative size is small and they can be indexed using
534 // 16-bit offsets.
535
536 // TODO: increment/decrement SP in one step instead of two or remove this comment.
537
538 uint32_t ofs = FrameEntrySpillSize();
539 __ IncreaseFrameSize(ofs);
540
541 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
542 GpuRegister reg = kCoreCalleeSaves[i];
543 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200544 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700545 __ Sd(reg, SP, ofs);
546 __ cfi().RelOffset(DWARFReg(reg), ofs);
547 }
548 }
549
550 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
551 FpuRegister reg = kFpuCalleeSaves[i];
552 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200553 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700554 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000555 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700556 }
557 }
558
559 // Allocate the rest of the frame and store the current method pointer
560 // at its end.
561
562 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
563
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100564 // Save the current method if we need it. Note that we do not
565 // do this in HCurrentMethod, as the instruction might have been removed
566 // in the SSA graph.
567 if (RequiresCurrentMethod()) {
568 static_assert(IsInt<16>(kCurrentMethodStackOffset),
569 "kCurrentMethodStackOffset must fit into int16_t");
570 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
571 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700572}
573
574void CodeGeneratorMIPS64::GenerateFrameExit() {
575 __ cfi().RememberState();
576
Alexey Frunze4dda3372015-06-01 18:31:49 -0700577 if (!HasEmptyFrame()) {
578 // Deallocate the rest of the frame.
579
580 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
581
582 // Restore callee-saved registers.
583 // Note that their cumulative size is small and they can be indexed using
584 // 16-bit offsets.
585
586 // TODO: increment/decrement SP in one step instead of two or remove this comment.
587
588 uint32_t ofs = 0;
589
590 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
591 FpuRegister reg = kFpuCalleeSaves[i];
592 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
593 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200594 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000595 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700596 }
597 }
598
599 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
600 GpuRegister reg = kCoreCalleeSaves[i];
601 if (allocated_registers_.ContainsCoreRegister(reg)) {
602 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200603 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700604 __ cfi().Restore(DWARFReg(reg));
605 }
606 }
607
608 DCHECK_EQ(ofs, FrameEntrySpillSize());
609 __ DecreaseFrameSize(ofs);
610 }
611
612 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700613 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700614
615 __ cfi().RestoreState();
616 __ cfi().DefCFAOffset(GetFrameSize());
617}
618
619void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
620 __ Bind(GetLabelOf(block));
621}
622
623void CodeGeneratorMIPS64::MoveLocation(Location destination,
624 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100625 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700626 if (source.Equals(destination)) {
627 return;
628 }
629
630 // A valid move can always be inferred from the destination and source
631 // locations. When moving from and to a register, the argument type can be
632 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100633 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634 DCHECK_EQ(unspecified_type, false);
635
636 if (destination.IsRegister() || destination.IsFpuRegister()) {
637 if (unspecified_type) {
638 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
639 if (source.IsStackSlot() ||
640 (src_cst != nullptr && (src_cst->IsIntConstant()
641 || src_cst->IsFloatConstant()
642 || src_cst->IsNullConstant()))) {
643 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100644 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700645 } else {
646 // If the source is a double stack slot or a 64bit constant, a 64bit
647 // type is appropriate. Else the source is a register, and since the
648 // type has not been specified, we chose a 64bit type to force a 64bit
649 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100650 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700651 }
652 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100653 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
654 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700655 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
656 // Move to GPR/FPR from stack
657 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100658 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 __ LoadFpuFromOffset(load_type,
660 destination.AsFpuRegister<FpuRegister>(),
661 SP,
662 source.GetStackIndex());
663 } else {
664 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
665 __ LoadFromOffset(load_type,
666 destination.AsRegister<GpuRegister>(),
667 SP,
668 source.GetStackIndex());
669 }
670 } else if (source.IsConstant()) {
671 // Move to GPR/FPR from constant
672 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700674 gpr = destination.AsRegister<GpuRegister>();
675 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100676 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700677 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
678 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
679 gpr = ZERO;
680 } else {
681 __ LoadConst32(gpr, value);
682 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700683 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700684 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
685 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
686 gpr = ZERO;
687 } else {
688 __ LoadConst64(gpr, value);
689 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700690 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
695 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100696 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700697 if (destination.IsRegister()) {
698 // Move to GPR from GPR
699 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
700 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100701 DCHECK(destination.IsFpuRegister());
702 if (Primitive::Is64BitType(dst_type)) {
703 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
704 } else {
705 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
706 }
707 }
708 } else if (source.IsFpuRegister()) {
709 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
713 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700715 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
716 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100717 } else {
718 DCHECK(destination.IsRegister());
719 if (Primitive::Is64BitType(dst_type)) {
720 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
721 } else {
722 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
723 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700724 }
725 }
726 } else { // The destination is not a register. It must be a stack slot.
727 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
728 if (source.IsRegister() || source.IsFpuRegister()) {
729 if (unspecified_type) {
730 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100733 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 }
735 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100736 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
737 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700738 // Move to stack from GPR/FPR
739 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
740 if (source.IsRegister()) {
741 __ StoreToOffset(store_type,
742 source.AsRegister<GpuRegister>(),
743 SP,
744 destination.GetStackIndex());
745 } else {
746 __ StoreFpuToOffset(store_type,
747 source.AsFpuRegister<FpuRegister>(),
748 SP,
749 destination.GetStackIndex());
750 }
751 } else if (source.IsConstant()) {
752 // Move to stack from constant
753 HConstant* src_cst = source.GetConstant();
754 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700755 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700756 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700757 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
758 if (value != 0) {
759 gpr = TMP;
760 __ LoadConst32(gpr, value);
761 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700762 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700763 DCHECK(destination.IsDoubleStackSlot());
764 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
765 if (value != 0) {
766 gpr = TMP;
767 __ LoadConst64(gpr, value);
768 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700769 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700770 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700771 } else {
772 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
773 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
774 // Move to stack from stack
775 if (destination.IsStackSlot()) {
776 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
777 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
778 } else {
779 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
780 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
781 }
782 }
783 }
784}
785
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700786void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700787 DCHECK(!loc1.IsConstant());
788 DCHECK(!loc2.IsConstant());
789
790 if (loc1.Equals(loc2)) {
791 return;
792 }
793
794 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
795 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
796 bool is_fp_reg1 = loc1.IsFpuRegister();
797 bool is_fp_reg2 = loc2.IsFpuRegister();
798
799 if (loc2.IsRegister() && loc1.IsRegister()) {
800 // Swap 2 GPRs
801 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
802 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
803 __ Move(TMP, r2);
804 __ Move(r2, r1);
805 __ Move(r1, TMP);
806 } else if (is_fp_reg2 && is_fp_reg1) {
807 // Swap 2 FPRs
808 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
809 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700810 if (type == Primitive::kPrimFloat) {
811 __ MovS(FTMP, r1);
812 __ MovS(r1, r2);
813 __ MovS(r2, FTMP);
814 } else {
815 DCHECK_EQ(type, Primitive::kPrimDouble);
816 __ MovD(FTMP, r1);
817 __ MovD(r1, r2);
818 __ MovD(r2, FTMP);
819 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700820 } else if (is_slot1 != is_slot2) {
821 // Swap GPR/FPR and stack slot
822 Location reg_loc = is_slot1 ? loc2 : loc1;
823 Location mem_loc = is_slot1 ? loc1 : loc2;
824 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
825 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
826 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
827 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
828 if (reg_loc.IsFpuRegister()) {
829 __ StoreFpuToOffset(store_type,
830 reg_loc.AsFpuRegister<FpuRegister>(),
831 SP,
832 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700833 if (mem_loc.IsStackSlot()) {
834 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
835 } else {
836 DCHECK(mem_loc.IsDoubleStackSlot());
837 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
838 }
839 } else {
840 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
841 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
842 }
843 } else if (is_slot1 && is_slot2) {
844 move_resolver_.Exchange(loc1.GetStackIndex(),
845 loc2.GetStackIndex(),
846 loc1.IsDoubleStackSlot());
847 } else {
848 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
849 }
850}
851
Calin Juravle175dc732015-08-25 15:42:32 +0100852void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
853 DCHECK(location.IsRegister());
854 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
855}
856
Calin Juravlee460d1d2015-09-29 04:52:17 +0100857void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
858 if (location.IsRegister()) {
859 locations->AddTemp(location);
860 } else {
861 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
862 }
863}
864
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100865void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
866 GpuRegister value,
867 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700868 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700869 GpuRegister card = AT;
870 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100871 if (value_can_be_null) {
872 __ Beqzc(value, &done);
873 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700874 __ LoadFromOffset(kLoadDoubleword,
875 card,
876 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700877 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700878 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
879 __ Daddu(temp, card, temp);
880 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100881 if (value_can_be_null) {
882 __ Bind(&done);
883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700884}
885
Alexey Frunze19f6c692016-11-30 19:19:55 -0800886template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
887inline void CodeGeneratorMIPS64::EmitPcRelativeLinkerPatches(
888 const ArenaDeque<PcRelativePatchInfo>& infos,
889 ArenaVector<LinkerPatch>* linker_patches) {
890 for (const PcRelativePatchInfo& info : infos) {
891 const DexFile& dex_file = info.target_dex_file;
892 size_t offset_or_index = info.offset_or_index;
893 DCHECK(info.pc_rel_label.IsBound());
894 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
895 linker_patches->push_back(Factory(pc_rel_offset, &dex_file, pc_rel_offset, offset_or_index));
896 }
897}
898
899void CodeGeneratorMIPS64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
900 DCHECK(linker_patches->empty());
901 size_t size =
902 method_patches_.size() +
903 call_patches_.size() +
904 pc_relative_dex_cache_patches_.size() +
905 relative_call_patches_.size();
906 linker_patches->reserve(size);
907 for (const auto& entry : method_patches_) {
908 const MethodReference& target_method = entry.first;
909 Literal* literal = entry.second;
910 DCHECK(literal->GetLabel()->IsBound());
911 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
912 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
913 target_method.dex_file,
914 target_method.dex_method_index));
915 }
916 for (const auto& entry : call_patches_) {
917 const MethodReference& target_method = entry.first;
918 Literal* literal = entry.second;
919 DCHECK(literal->GetLabel()->IsBound());
920 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
921 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
922 target_method.dex_file,
923 target_method.dex_method_index));
924 }
925 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
926 linker_patches);
927 for (const PcRelativePatchInfo& info : relative_call_patches_) {
928 const DexFile& dex_file = info.target_dex_file;
929 uint32_t method_index = info.offset_or_index;
930 DCHECK(info.pc_rel_label.IsBound());
931 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
932 linker_patches->push_back(
933 LinkerPatch::RelativeCodePatch(pc_rel_offset, &dex_file, method_index));
934 }
935}
936
937CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeDexCacheArrayPatch(
938 const DexFile& dex_file, uint32_t element_offset) {
939 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
940}
941
942CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativeCallPatch(
943 const DexFile& dex_file, uint32_t method_index) {
944 return NewPcRelativePatch(dex_file, method_index, &relative_call_patches_);
945}
946
947CodeGeneratorMIPS64::PcRelativePatchInfo* CodeGeneratorMIPS64::NewPcRelativePatch(
948 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
949 patches->emplace_back(dex_file, offset_or_index);
950 return &patches->back();
951}
952
953Literal* CodeGeneratorMIPS64::DeduplicateUint64Literal(uint64_t value) {
954 return uint64_literals_.GetOrCreate(
955 value,
956 [this, value]() { return __ NewLiteral<uint64_t>(value); });
957}
958
959Literal* CodeGeneratorMIPS64::DeduplicateMethodLiteral(MethodReference target_method,
960 MethodToLiteralMap* map) {
961 return map->GetOrCreate(
962 target_method,
963 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
964}
965
966Literal* CodeGeneratorMIPS64::DeduplicateMethodAddressLiteral(MethodReference target_method) {
967 return DeduplicateMethodLiteral(target_method, &method_patches_);
968}
969
970Literal* CodeGeneratorMIPS64::DeduplicateMethodCodeLiteral(MethodReference target_method) {
971 return DeduplicateMethodLiteral(target_method, &call_patches_);
972}
973
974void CodeGeneratorMIPS64::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
975 GpuRegister out) {
976 __ Bind(&info->pc_rel_label);
977 // Add the high half of a 32-bit offset to PC.
978 __ Auipc(out, /* placeholder */ 0x1234);
979 // The immediately following instruction will add the sign-extended low half of the 32-bit
980 // offset to `out` (e.g. ld, jialc, addiu).
981}
982
David Brazdil58282f42016-01-14 12:45:10 +0000983void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700984 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
985 blocked_core_registers_[ZERO] = true;
986 blocked_core_registers_[K0] = true;
987 blocked_core_registers_[K1] = true;
988 blocked_core_registers_[GP] = true;
989 blocked_core_registers_[SP] = true;
990 blocked_core_registers_[RA] = true;
991
Lazar Trsicd9672662015-09-03 17:33:01 +0200992 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
993 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700994 blocked_core_registers_[AT] = true;
995 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200996 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700997 blocked_fpu_registers_[FTMP] = true;
998
999 // Reserve suspend and thread registers.
1000 blocked_core_registers_[S0] = true;
1001 blocked_core_registers_[TR] = true;
1002
1003 // Reserve T9 for function calls
1004 blocked_core_registers_[T9] = true;
1005
1006 // TODO: review; anything else?
1007
Goran Jakovljevic782be112016-06-21 12:39:04 +02001008 if (GetGraph()->IsDebuggable()) {
1009 // Stubs do not save callee-save floating point registers. If the graph
1010 // is debuggable, we need to deal with these registers differently. For
1011 // now, just block them.
1012 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1013 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1014 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001015 }
1016}
1017
Alexey Frunze4dda3372015-06-01 18:31:49 -07001018size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1019 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001020 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001021}
1022
1023size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1024 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026}
1027
1028size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1029 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001030 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001031}
1032
1033size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1034 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +02001035 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001036}
1037
1038void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001039 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001040}
1041
1042void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001043 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001044}
1045
Calin Juravle175dc732015-08-25 15:42:32 +01001046void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -07001047 HInstruction* instruction,
1048 uint32_t dex_pc,
1049 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001050 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescufc734082016-07-19 17:18:07 +01001051 __ LoadFromOffset(kLoadDoubleword,
1052 T9,
1053 TR,
1054 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001055 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001056 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +01001057 if (EntrypointRequiresStackMap(entrypoint)) {
1058 RecordPcInfo(instruction, dex_pc, slow_path);
1059 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001060}
1061
1062void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1063 GpuRegister class_reg) {
1064 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1065 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1066 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1067 // TODO: barrier needed?
1068 __ Bind(slow_path->GetExitLabel());
1069}
1070
1071void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1072 __ Sync(0); // only stype 0 is supported
1073}
1074
1075void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1076 HBasicBlock* successor) {
1077 SuspendCheckSlowPathMIPS64* slow_path =
1078 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1079 codegen_->AddSlowPath(slow_path);
1080
1081 __ LoadFromOffset(kLoadUnsignedHalfword,
1082 TMP,
1083 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001084 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001085 if (successor == nullptr) {
1086 __ Bnezc(TMP, slow_path->GetEntryLabel());
1087 __ Bind(slow_path->GetReturnLabel());
1088 } else {
1089 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001090 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001091 // slow_path will return to GetLabelOf(successor).
1092 }
1093}
1094
1095InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1096 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001097 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001098 assembler_(codegen->GetAssembler()),
1099 codegen_(codegen) {}
1100
1101void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1102 DCHECK_EQ(instruction->InputCount(), 2U);
1103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1104 Primitive::Type type = instruction->GetResultType();
1105 switch (type) {
1106 case Primitive::kPrimInt:
1107 case Primitive::kPrimLong: {
1108 locations->SetInAt(0, Location::RequiresRegister());
1109 HInstruction* right = instruction->InputAt(1);
1110 bool can_use_imm = false;
1111 if (right->IsConstant()) {
1112 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1113 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1114 can_use_imm = IsUint<16>(imm);
1115 } else if (instruction->IsAdd()) {
1116 can_use_imm = IsInt<16>(imm);
1117 } else {
1118 DCHECK(instruction->IsSub());
1119 can_use_imm = IsInt<16>(-imm);
1120 }
1121 }
1122 if (can_use_imm)
1123 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1124 else
1125 locations->SetInAt(1, Location::RequiresRegister());
1126 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1127 }
1128 break;
1129
1130 case Primitive::kPrimFloat:
1131 case Primitive::kPrimDouble:
1132 locations->SetInAt(0, Location::RequiresFpuRegister());
1133 locations->SetInAt(1, Location::RequiresFpuRegister());
1134 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1135 break;
1136
1137 default:
1138 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1139 }
1140}
1141
1142void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1143 Primitive::Type type = instruction->GetType();
1144 LocationSummary* locations = instruction->GetLocations();
1145
1146 switch (type) {
1147 case Primitive::kPrimInt:
1148 case Primitive::kPrimLong: {
1149 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1150 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1151 Location rhs_location = locations->InAt(1);
1152
1153 GpuRegister rhs_reg = ZERO;
1154 int64_t rhs_imm = 0;
1155 bool use_imm = rhs_location.IsConstant();
1156 if (use_imm) {
1157 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1158 } else {
1159 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1160 }
1161
1162 if (instruction->IsAnd()) {
1163 if (use_imm)
1164 __ Andi(dst, lhs, rhs_imm);
1165 else
1166 __ And(dst, lhs, rhs_reg);
1167 } else if (instruction->IsOr()) {
1168 if (use_imm)
1169 __ Ori(dst, lhs, rhs_imm);
1170 else
1171 __ Or(dst, lhs, rhs_reg);
1172 } else if (instruction->IsXor()) {
1173 if (use_imm)
1174 __ Xori(dst, lhs, rhs_imm);
1175 else
1176 __ Xor(dst, lhs, rhs_reg);
1177 } else if (instruction->IsAdd()) {
1178 if (type == Primitive::kPrimInt) {
1179 if (use_imm)
1180 __ Addiu(dst, lhs, rhs_imm);
1181 else
1182 __ Addu(dst, lhs, rhs_reg);
1183 } else {
1184 if (use_imm)
1185 __ Daddiu(dst, lhs, rhs_imm);
1186 else
1187 __ Daddu(dst, lhs, rhs_reg);
1188 }
1189 } else {
1190 DCHECK(instruction->IsSub());
1191 if (type == Primitive::kPrimInt) {
1192 if (use_imm)
1193 __ Addiu(dst, lhs, -rhs_imm);
1194 else
1195 __ Subu(dst, lhs, rhs_reg);
1196 } else {
1197 if (use_imm)
1198 __ Daddiu(dst, lhs, -rhs_imm);
1199 else
1200 __ Dsubu(dst, lhs, rhs_reg);
1201 }
1202 }
1203 break;
1204 }
1205 case Primitive::kPrimFloat:
1206 case Primitive::kPrimDouble: {
1207 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1208 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1209 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1210 if (instruction->IsAdd()) {
1211 if (type == Primitive::kPrimFloat)
1212 __ AddS(dst, lhs, rhs);
1213 else
1214 __ AddD(dst, lhs, rhs);
1215 } else if (instruction->IsSub()) {
1216 if (type == Primitive::kPrimFloat)
1217 __ SubS(dst, lhs, rhs);
1218 else
1219 __ SubD(dst, lhs, rhs);
1220 } else {
1221 LOG(FATAL) << "Unexpected floating-point binary operation";
1222 }
1223 break;
1224 }
1225 default:
1226 LOG(FATAL) << "Unexpected binary operation type " << type;
1227 }
1228}
1229
1230void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001231 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232
1233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1234 Primitive::Type type = instr->GetResultType();
1235 switch (type) {
1236 case Primitive::kPrimInt:
1237 case Primitive::kPrimLong: {
1238 locations->SetInAt(0, Location::RequiresRegister());
1239 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001241 break;
1242 }
1243 default:
1244 LOG(FATAL) << "Unexpected shift type " << type;
1245 }
1246}
1247
1248void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001249 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 LocationSummary* locations = instr->GetLocations();
1251 Primitive::Type type = instr->GetType();
1252
1253 switch (type) {
1254 case Primitive::kPrimInt:
1255 case Primitive::kPrimLong: {
1256 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1257 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1258 Location rhs_location = locations->InAt(1);
1259
1260 GpuRegister rhs_reg = ZERO;
1261 int64_t rhs_imm = 0;
1262 bool use_imm = rhs_location.IsConstant();
1263 if (use_imm) {
1264 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1265 } else {
1266 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1267 }
1268
1269 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001270 uint32_t shift_value = rhs_imm &
1271 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001272
Alexey Frunze92d90602015-12-18 18:16:36 -08001273 if (shift_value == 0) {
1274 if (dst != lhs) {
1275 __ Move(dst, lhs);
1276 }
1277 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 if (instr->IsShl()) {
1279 __ Sll(dst, lhs, shift_value);
1280 } else if (instr->IsShr()) {
1281 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001282 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001283 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001284 } else {
1285 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 }
1287 } else {
1288 if (shift_value < 32) {
1289 if (instr->IsShl()) {
1290 __ Dsll(dst, lhs, shift_value);
1291 } else if (instr->IsShr()) {
1292 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001293 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001294 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001295 } else {
1296 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 }
1298 } else {
1299 shift_value -= 32;
1300 if (instr->IsShl()) {
1301 __ Dsll32(dst, lhs, shift_value);
1302 } else if (instr->IsShr()) {
1303 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001304 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001305 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001306 } else {
1307 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001308 }
1309 }
1310 }
1311 } else {
1312 if (type == Primitive::kPrimInt) {
1313 if (instr->IsShl()) {
1314 __ Sllv(dst, lhs, rhs_reg);
1315 } else if (instr->IsShr()) {
1316 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001317 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001318 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001319 } else {
1320 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001321 }
1322 } else {
1323 if (instr->IsShl()) {
1324 __ Dsllv(dst, lhs, rhs_reg);
1325 } else if (instr->IsShr()) {
1326 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001327 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001328 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001329 } else {
1330 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001331 }
1332 }
1333 }
1334 break;
1335 }
1336 default:
1337 LOG(FATAL) << "Unexpected shift operation type " << type;
1338 }
1339}
1340
1341void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1342 HandleBinaryOp(instruction);
1343}
1344
1345void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1346 HandleBinaryOp(instruction);
1347}
1348
1349void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1350 HandleBinaryOp(instruction);
1351}
1352
1353void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1354 HandleBinaryOp(instruction);
1355}
1356
1357void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1358 LocationSummary* locations =
1359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1360 locations->SetInAt(0, Location::RequiresRegister());
1361 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1362 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1364 } else {
1365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1366 }
1367}
1368
1369void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1370 LocationSummary* locations = instruction->GetLocations();
1371 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1372 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001373 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001374
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001375 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001376 switch (type) {
1377 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001378 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1379 if (index.IsConstant()) {
1380 size_t offset =
1381 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1382 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1383 } else {
1384 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1385 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1386 }
1387 break;
1388 }
1389
1390 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001391 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1392 if (index.IsConstant()) {
1393 size_t offset =
1394 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1395 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1396 } else {
1397 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1398 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1399 }
1400 break;
1401 }
1402
1403 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001404 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1405 if (index.IsConstant()) {
1406 size_t offset =
1407 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1408 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1409 } else {
1410 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1411 __ Daddu(TMP, obj, TMP);
1412 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1413 }
1414 break;
1415 }
1416
1417 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1419 if (index.IsConstant()) {
1420 size_t offset =
1421 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1422 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1423 } else {
1424 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1425 __ Daddu(TMP, obj, TMP);
1426 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1427 }
1428 break;
1429 }
1430
1431 case Primitive::kPrimInt:
1432 case Primitive::kPrimNot: {
1433 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001434 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1435 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1436 if (index.IsConstant()) {
1437 size_t offset =
1438 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1439 __ LoadFromOffset(load_type, out, obj, offset);
1440 } else {
1441 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1442 __ Daddu(TMP, obj, TMP);
1443 __ LoadFromOffset(load_type, out, TMP, data_offset);
1444 }
1445 break;
1446 }
1447
1448 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001449 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1450 if (index.IsConstant()) {
1451 size_t offset =
1452 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1453 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1454 } else {
1455 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1456 __ Daddu(TMP, obj, TMP);
1457 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1458 }
1459 break;
1460 }
1461
1462 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001463 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1464 if (index.IsConstant()) {
1465 size_t offset =
1466 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1467 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1468 } else {
1469 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1470 __ Daddu(TMP, obj, TMP);
1471 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1472 }
1473 break;
1474 }
1475
1476 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001477 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1478 if (index.IsConstant()) {
1479 size_t offset =
1480 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1481 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1482 } else {
1483 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1484 __ Daddu(TMP, obj, TMP);
1485 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1486 }
1487 break;
1488 }
1489
1490 case Primitive::kPrimVoid:
1491 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1492 UNREACHABLE();
1493 }
1494 codegen_->MaybeRecordImplicitNullCheck(instruction);
1495}
1496
1497void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1498 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1499 locations->SetInAt(0, Location::RequiresRegister());
1500 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1501}
1502
1503void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1504 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001505 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001506 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1507 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1508 __ LoadFromOffset(kLoadWord, out, obj, offset);
1509 codegen_->MaybeRecordImplicitNullCheck(instruction);
1510}
1511
1512void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001513 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1515 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001516 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001517 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001518 InvokeRuntimeCallingConvention calling_convention;
1519 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1520 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1521 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1522 } else {
1523 locations->SetInAt(0, Location::RequiresRegister());
1524 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1525 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1526 locations->SetInAt(2, Location::RequiresFpuRegister());
1527 } else {
1528 locations->SetInAt(2, Location::RequiresRegister());
1529 }
1530 }
1531}
1532
1533void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1534 LocationSummary* locations = instruction->GetLocations();
1535 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1536 Location index = locations->InAt(1);
1537 Primitive::Type value_type = instruction->GetComponentType();
1538 bool needs_runtime_call = locations->WillCall();
1539 bool needs_write_barrier =
1540 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1541
1542 switch (value_type) {
1543 case Primitive::kPrimBoolean:
1544 case Primitive::kPrimByte: {
1545 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1546 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1547 if (index.IsConstant()) {
1548 size_t offset =
1549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1550 __ StoreToOffset(kStoreByte, value, obj, offset);
1551 } else {
1552 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1553 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1554 }
1555 break;
1556 }
1557
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimChar: {
1560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1561 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1562 if (index.IsConstant()) {
1563 size_t offset =
1564 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1565 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1566 } else {
1567 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1568 __ Daddu(TMP, obj, TMP);
1569 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1570 }
1571 break;
1572 }
1573
1574 case Primitive::kPrimInt:
1575 case Primitive::kPrimNot: {
1576 if (!needs_runtime_call) {
1577 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1578 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1579 if (index.IsConstant()) {
1580 size_t offset =
1581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1582 __ StoreToOffset(kStoreWord, value, obj, offset);
1583 } else {
1584 DCHECK(index.IsRegister()) << index;
1585 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1586 __ Daddu(TMP, obj, TMP);
1587 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1588 }
1589 codegen_->MaybeRecordImplicitNullCheck(instruction);
1590 if (needs_write_barrier) {
1591 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001592 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001593 }
1594 } else {
1595 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001596 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001597 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001598 }
1599 break;
1600 }
1601
1602 case Primitive::kPrimLong: {
1603 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1604 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1605 if (index.IsConstant()) {
1606 size_t offset =
1607 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1608 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1609 } else {
1610 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1611 __ Daddu(TMP, obj, TMP);
1612 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1613 }
1614 break;
1615 }
1616
1617 case Primitive::kPrimFloat: {
1618 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1619 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1620 DCHECK(locations->InAt(2).IsFpuRegister());
1621 if (index.IsConstant()) {
1622 size_t offset =
1623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1624 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1625 } else {
1626 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1627 __ Daddu(TMP, obj, TMP);
1628 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1629 }
1630 break;
1631 }
1632
1633 case Primitive::kPrimDouble: {
1634 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1635 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1636 DCHECK(locations->InAt(2).IsFpuRegister());
1637 if (index.IsConstant()) {
1638 size_t offset =
1639 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1640 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1641 } else {
1642 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1643 __ Daddu(TMP, obj, TMP);
1644 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1645 }
1646 break;
1647 }
1648
1649 case Primitive::kPrimVoid:
1650 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1651 UNREACHABLE();
1652 }
1653
1654 // Ints and objects are handled in the switch.
1655 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1656 codegen_->MaybeRecordImplicitNullCheck(instruction);
1657 }
1658}
1659
1660void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001661 RegisterSet caller_saves = RegisterSet::Empty();
1662 InvokeRuntimeCallingConvention calling_convention;
1663 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1664 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1665 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001666 locations->SetInAt(0, Location::RequiresRegister());
1667 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001668}
1669
1670void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1671 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001672 BoundsCheckSlowPathMIPS64* slow_path =
1673 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 codegen_->AddSlowPath(slow_path);
1675
1676 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1677 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1678
1679 // length is limited by the maximum positive signed 32-bit integer.
1680 // Unsigned comparison of length and index checks for index < 0
1681 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001682 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001683}
1684
1685void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1687 instruction,
1688 LocationSummary::kCallOnSlowPath);
1689 locations->SetInAt(0, Location::RequiresRegister());
1690 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001691 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 locations->AddTemp(Location::RequiresRegister());
1693}
1694
1695void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1696 LocationSummary* locations = instruction->GetLocations();
1697 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1698 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1699 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1700
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001701 SlowPathCodeMIPS64* slow_path =
1702 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001703 codegen_->AddSlowPath(slow_path);
1704
1705 // TODO: avoid this check if we know obj is not null.
1706 __ Beqzc(obj, slow_path->GetExitLabel());
1707 // Compare the class of `obj` with `cls`.
1708 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1709 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1710 __ Bind(slow_path->GetExitLabel());
1711}
1712
1713void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1714 LocationSummary* locations =
1715 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1716 locations->SetInAt(0, Location::RequiresRegister());
1717 if (check->HasUses()) {
1718 locations->SetOut(Location::SameAsFirstInput());
1719 }
1720}
1721
1722void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1723 // We assume the class is not null.
1724 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1725 check->GetLoadClass(),
1726 check,
1727 check->GetDexPc(),
1728 true);
1729 codegen_->AddSlowPath(slow_path);
1730 GenerateClassInitializationCheck(slow_path,
1731 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1732}
1733
1734void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1735 Primitive::Type in_type = compare->InputAt(0)->GetType();
1736
Alexey Frunze299a9392015-12-08 16:08:02 -08001737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738
1739 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001740 case Primitive::kPrimBoolean:
1741 case Primitive::kPrimByte:
1742 case Primitive::kPrimShort:
1743 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001744 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001745 case Primitive::kPrimLong:
1746 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001747 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1749 break;
1750
1751 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001752 case Primitive::kPrimDouble:
1753 locations->SetInAt(0, Location::RequiresFpuRegister());
1754 locations->SetInAt(1, Location::RequiresFpuRegister());
1755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001757
1758 default:
1759 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1760 }
1761}
1762
1763void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1764 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001765 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001766 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1767
1768 // 0 if: left == right
1769 // 1 if: left > right
1770 // -1 if: left < right
1771 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001772 case Primitive::kPrimBoolean:
1773 case Primitive::kPrimByte:
1774 case Primitive::kPrimShort:
1775 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001776 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001777 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001779 Location rhs_location = locations->InAt(1);
1780 bool use_imm = rhs_location.IsConstant();
1781 GpuRegister rhs = ZERO;
1782 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001783 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001784 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1785 if (value != 0) {
1786 rhs = AT;
1787 __ LoadConst64(rhs, value);
1788 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001789 } else {
1790 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1791 if (value != 0) {
1792 rhs = AT;
1793 __ LoadConst32(rhs, value);
1794 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001795 }
1796 } else {
1797 rhs = rhs_location.AsRegister<GpuRegister>();
1798 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001800 __ Slt(res, rhs, lhs);
1801 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802 break;
1803 }
1804
Alexey Frunze299a9392015-12-08 16:08:02 -08001805 case Primitive::kPrimFloat: {
1806 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1807 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1808 Mips64Label done;
1809 __ CmpEqS(FTMP, lhs, rhs);
1810 __ LoadConst32(res, 0);
1811 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001812 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001813 __ CmpLtS(FTMP, lhs, rhs);
1814 __ LoadConst32(res, -1);
1815 __ Bc1nez(FTMP, &done);
1816 __ LoadConst32(res, 1);
1817 } else {
1818 __ CmpLtS(FTMP, rhs, lhs);
1819 __ LoadConst32(res, 1);
1820 __ Bc1nez(FTMP, &done);
1821 __ LoadConst32(res, -1);
1822 }
1823 __ Bind(&done);
1824 break;
1825 }
1826
Alexey Frunze4dda3372015-06-01 18:31:49 -07001827 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001828 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1829 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1830 Mips64Label done;
1831 __ CmpEqD(FTMP, lhs, rhs);
1832 __ LoadConst32(res, 0);
1833 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001834 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001835 __ CmpLtD(FTMP, lhs, rhs);
1836 __ LoadConst32(res, -1);
1837 __ Bc1nez(FTMP, &done);
1838 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001839 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001840 __ CmpLtD(FTMP, rhs, lhs);
1841 __ LoadConst32(res, 1);
1842 __ Bc1nez(FTMP, &done);
1843 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001845 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001846 break;
1847 }
1848
1849 default:
1850 LOG(FATAL) << "Unimplemented compare type " << in_type;
1851 }
1852}
1853
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001856 switch (instruction->InputAt(0)->GetType()) {
1857 default:
1858 case Primitive::kPrimLong:
1859 locations->SetInAt(0, Location::RequiresRegister());
1860 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1861 break;
1862
1863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble:
1865 locations->SetInAt(0, Location::RequiresFpuRegister());
1866 locations->SetInAt(1, Location::RequiresFpuRegister());
1867 break;
1868 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001869 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1871 }
1872}
1873
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001874void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001875 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001876 return;
1877 }
1878
Alexey Frunze299a9392015-12-08 16:08:02 -08001879 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001880 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001881 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001882 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001883
Alexey Frunze299a9392015-12-08 16:08:02 -08001884 switch (type) {
1885 default:
1886 // Integer case.
1887 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1888 return;
1889 case Primitive::kPrimLong:
1890 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1891 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001892
Alexey Frunze299a9392015-12-08 16:08:02 -08001893 case Primitive::kPrimFloat:
1894 case Primitive::kPrimDouble:
1895 // TODO: don't use branches.
1896 GenerateFpCompareAndBranch(instruction->GetCondition(),
1897 instruction->IsGtBias(),
1898 type,
1899 locations,
1900 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001901 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001902 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001903
1904 // Convert the branches into the result.
1905 Mips64Label done;
1906
1907 // False case: result = 0.
1908 __ LoadConst32(dst, 0);
1909 __ Bc(&done);
1910
1911 // True case: result = 1.
1912 __ Bind(&true_label);
1913 __ LoadConst32(dst, 1);
1914 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001915}
1916
Alexey Frunzec857c742015-09-23 15:12:39 -07001917void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1918 DCHECK(instruction->IsDiv() || instruction->IsRem());
1919 Primitive::Type type = instruction->GetResultType();
1920
1921 LocationSummary* locations = instruction->GetLocations();
1922 Location second = locations->InAt(1);
1923 DCHECK(second.IsConstant());
1924
1925 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1926 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1927 int64_t imm = Int64FromConstant(second.GetConstant());
1928 DCHECK(imm == 1 || imm == -1);
1929
1930 if (instruction->IsRem()) {
1931 __ Move(out, ZERO);
1932 } else {
1933 if (imm == -1) {
1934 if (type == Primitive::kPrimInt) {
1935 __ Subu(out, ZERO, dividend);
1936 } else {
1937 DCHECK_EQ(type, Primitive::kPrimLong);
1938 __ Dsubu(out, ZERO, dividend);
1939 }
1940 } else if (out != dividend) {
1941 __ Move(out, dividend);
1942 }
1943 }
1944}
1945
1946void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1947 DCHECK(instruction->IsDiv() || instruction->IsRem());
1948 Primitive::Type type = instruction->GetResultType();
1949
1950 LocationSummary* locations = instruction->GetLocations();
1951 Location second = locations->InAt(1);
1952 DCHECK(second.IsConstant());
1953
1954 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1955 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1956 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001957 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001958 int ctz_imm = CTZ(abs_imm);
1959
1960 if (instruction->IsDiv()) {
1961 if (type == Primitive::kPrimInt) {
1962 if (ctz_imm == 1) {
1963 // Fast path for division by +/-2, which is very common.
1964 __ Srl(TMP, dividend, 31);
1965 } else {
1966 __ Sra(TMP, dividend, 31);
1967 __ Srl(TMP, TMP, 32 - ctz_imm);
1968 }
1969 __ Addu(out, dividend, TMP);
1970 __ Sra(out, out, ctz_imm);
1971 if (imm < 0) {
1972 __ Subu(out, ZERO, out);
1973 }
1974 } else {
1975 DCHECK_EQ(type, Primitive::kPrimLong);
1976 if (ctz_imm == 1) {
1977 // Fast path for division by +/-2, which is very common.
1978 __ Dsrl32(TMP, dividend, 31);
1979 } else {
1980 __ Dsra32(TMP, dividend, 31);
1981 if (ctz_imm > 32) {
1982 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1983 } else {
1984 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1985 }
1986 }
1987 __ Daddu(out, dividend, TMP);
1988 if (ctz_imm < 32) {
1989 __ Dsra(out, out, ctz_imm);
1990 } else {
1991 __ Dsra32(out, out, ctz_imm - 32);
1992 }
1993 if (imm < 0) {
1994 __ Dsubu(out, ZERO, out);
1995 }
1996 }
1997 } else {
1998 if (type == Primitive::kPrimInt) {
1999 if (ctz_imm == 1) {
2000 // Fast path for modulo +/-2, which is very common.
2001 __ Sra(TMP, dividend, 31);
2002 __ Subu(out, dividend, TMP);
2003 __ Andi(out, out, 1);
2004 __ Addu(out, out, TMP);
2005 } else {
2006 __ Sra(TMP, dividend, 31);
2007 __ Srl(TMP, TMP, 32 - ctz_imm);
2008 __ Addu(out, dividend, TMP);
2009 if (IsUint<16>(abs_imm - 1)) {
2010 __ Andi(out, out, abs_imm - 1);
2011 } else {
2012 __ Sll(out, out, 32 - ctz_imm);
2013 __ Srl(out, out, 32 - ctz_imm);
2014 }
2015 __ Subu(out, out, TMP);
2016 }
2017 } else {
2018 DCHECK_EQ(type, Primitive::kPrimLong);
2019 if (ctz_imm == 1) {
2020 // Fast path for modulo +/-2, which is very common.
2021 __ Dsra32(TMP, dividend, 31);
2022 __ Dsubu(out, dividend, TMP);
2023 __ Andi(out, out, 1);
2024 __ Daddu(out, out, TMP);
2025 } else {
2026 __ Dsra32(TMP, dividend, 31);
2027 if (ctz_imm > 32) {
2028 __ Dsrl(TMP, TMP, 64 - ctz_imm);
2029 } else {
2030 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
2031 }
2032 __ Daddu(out, dividend, TMP);
2033 if (IsUint<16>(abs_imm - 1)) {
2034 __ Andi(out, out, abs_imm - 1);
2035 } else {
2036 if (ctz_imm > 32) {
2037 __ Dsll(out, out, 64 - ctz_imm);
2038 __ Dsrl(out, out, 64 - ctz_imm);
2039 } else {
2040 __ Dsll32(out, out, 32 - ctz_imm);
2041 __ Dsrl32(out, out, 32 - ctz_imm);
2042 }
2043 }
2044 __ Dsubu(out, out, TMP);
2045 }
2046 }
2047 }
2048}
2049
2050void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2051 DCHECK(instruction->IsDiv() || instruction->IsRem());
2052
2053 LocationSummary* locations = instruction->GetLocations();
2054 Location second = locations->InAt(1);
2055 DCHECK(second.IsConstant());
2056
2057 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2058 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2059 int64_t imm = Int64FromConstant(second.GetConstant());
2060
2061 Primitive::Type type = instruction->GetResultType();
2062 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2063
2064 int64_t magic;
2065 int shift;
2066 CalculateMagicAndShiftForDivRem(imm,
2067 (type == Primitive::kPrimLong),
2068 &magic,
2069 &shift);
2070
2071 if (type == Primitive::kPrimInt) {
2072 __ LoadConst32(TMP, magic);
2073 __ MuhR6(TMP, dividend, TMP);
2074
2075 if (imm > 0 && magic < 0) {
2076 __ Addu(TMP, TMP, dividend);
2077 } else if (imm < 0 && magic > 0) {
2078 __ Subu(TMP, TMP, dividend);
2079 }
2080
2081 if (shift != 0) {
2082 __ Sra(TMP, TMP, shift);
2083 }
2084
2085 if (instruction->IsDiv()) {
2086 __ Sra(out, TMP, 31);
2087 __ Subu(out, TMP, out);
2088 } else {
2089 __ Sra(AT, TMP, 31);
2090 __ Subu(AT, TMP, AT);
2091 __ LoadConst32(TMP, imm);
2092 __ MulR6(TMP, AT, TMP);
2093 __ Subu(out, dividend, TMP);
2094 }
2095 } else {
2096 __ LoadConst64(TMP, magic);
2097 __ Dmuh(TMP, dividend, TMP);
2098
2099 if (imm > 0 && magic < 0) {
2100 __ Daddu(TMP, TMP, dividend);
2101 } else if (imm < 0 && magic > 0) {
2102 __ Dsubu(TMP, TMP, dividend);
2103 }
2104
2105 if (shift >= 32) {
2106 __ Dsra32(TMP, TMP, shift - 32);
2107 } else if (shift > 0) {
2108 __ Dsra(TMP, TMP, shift);
2109 }
2110
2111 if (instruction->IsDiv()) {
2112 __ Dsra32(out, TMP, 31);
2113 __ Dsubu(out, TMP, out);
2114 } else {
2115 __ Dsra32(AT, TMP, 31);
2116 __ Dsubu(AT, TMP, AT);
2117 __ LoadConst64(TMP, imm);
2118 __ Dmul(TMP, AT, TMP);
2119 __ Dsubu(out, dividend, TMP);
2120 }
2121 }
2122}
2123
2124void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2125 DCHECK(instruction->IsDiv() || instruction->IsRem());
2126 Primitive::Type type = instruction->GetResultType();
2127 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2128
2129 LocationSummary* locations = instruction->GetLocations();
2130 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2131 Location second = locations->InAt(1);
2132
2133 if (second.IsConstant()) {
2134 int64_t imm = Int64FromConstant(second.GetConstant());
2135 if (imm == 0) {
2136 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2137 } else if (imm == 1 || imm == -1) {
2138 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002139 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002140 DivRemByPowerOfTwo(instruction);
2141 } else {
2142 DCHECK(imm <= -2 || imm >= 2);
2143 GenerateDivRemWithAnyConstant(instruction);
2144 }
2145 } else {
2146 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2147 GpuRegister divisor = second.AsRegister<GpuRegister>();
2148 if (instruction->IsDiv()) {
2149 if (type == Primitive::kPrimInt)
2150 __ DivR6(out, dividend, divisor);
2151 else
2152 __ Ddiv(out, dividend, divisor);
2153 } else {
2154 if (type == Primitive::kPrimInt)
2155 __ ModR6(out, dividend, divisor);
2156 else
2157 __ Dmod(out, dividend, divisor);
2158 }
2159 }
2160}
2161
Alexey Frunze4dda3372015-06-01 18:31:49 -07002162void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2163 LocationSummary* locations =
2164 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2165 switch (div->GetResultType()) {
2166 case Primitive::kPrimInt:
2167 case Primitive::kPrimLong:
2168 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002169 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002170 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2171 break;
2172
2173 case Primitive::kPrimFloat:
2174 case Primitive::kPrimDouble:
2175 locations->SetInAt(0, Location::RequiresFpuRegister());
2176 locations->SetInAt(1, Location::RequiresFpuRegister());
2177 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2178 break;
2179
2180 default:
2181 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2182 }
2183}
2184
2185void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2186 Primitive::Type type = instruction->GetType();
2187 LocationSummary* locations = instruction->GetLocations();
2188
2189 switch (type) {
2190 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002191 case Primitive::kPrimLong:
2192 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002194 case Primitive::kPrimFloat:
2195 case Primitive::kPrimDouble: {
2196 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2197 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2198 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2199 if (type == Primitive::kPrimFloat)
2200 __ DivS(dst, lhs, rhs);
2201 else
2202 __ DivD(dst, lhs, rhs);
2203 break;
2204 }
2205 default:
2206 LOG(FATAL) << "Unexpected div type " << type;
2207 }
2208}
2209
2210void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002211 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002212 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002213}
2214
2215void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2216 SlowPathCodeMIPS64* slow_path =
2217 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2218 codegen_->AddSlowPath(slow_path);
2219 Location value = instruction->GetLocations()->InAt(0);
2220
2221 Primitive::Type type = instruction->GetType();
2222
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002223 if (!Primitive::IsIntegralType(type)) {
2224 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002225 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002226 }
2227
2228 if (value.IsConstant()) {
2229 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2230 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002231 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002232 } else {
2233 // A division by a non-null constant is valid. We don't need to perform
2234 // any check, so simply fall through.
2235 }
2236 } else {
2237 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2238 }
2239}
2240
2241void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2242 LocationSummary* locations =
2243 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2244 locations->SetOut(Location::ConstantLocation(constant));
2245}
2246
2247void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2248 // Will be generated at use site.
2249}
2250
2251void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2252 exit->SetLocations(nullptr);
2253}
2254
2255void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2256}
2257
2258void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2259 LocationSummary* locations =
2260 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2261 locations->SetOut(Location::ConstantLocation(constant));
2262}
2263
2264void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2265 // Will be generated at use site.
2266}
2267
David Brazdilfc6a86a2015-06-26 10:33:45 +00002268void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002269 DCHECK(!successor->IsExitBlock());
2270 HBasicBlock* block = got->GetBlock();
2271 HInstruction* previous = got->GetPrevious();
2272 HLoopInformation* info = block->GetLoopInformation();
2273
2274 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2275 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2276 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2277 return;
2278 }
2279 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2280 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2281 }
2282 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002283 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002284 }
2285}
2286
David Brazdilfc6a86a2015-06-26 10:33:45 +00002287void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2288 got->SetLocations(nullptr);
2289}
2290
2291void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2292 HandleGoto(got, got->GetSuccessor());
2293}
2294
2295void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2296 try_boundary->SetLocations(nullptr);
2297}
2298
2299void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2300 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2301 if (!successor->IsExitBlock()) {
2302 HandleGoto(try_boundary, successor);
2303 }
2304}
2305
Alexey Frunze299a9392015-12-08 16:08:02 -08002306void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2307 bool is64bit,
2308 LocationSummary* locations) {
2309 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2310 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2311 Location rhs_location = locations->InAt(1);
2312 GpuRegister rhs_reg = ZERO;
2313 int64_t rhs_imm = 0;
2314 bool use_imm = rhs_location.IsConstant();
2315 if (use_imm) {
2316 if (is64bit) {
2317 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2318 } else {
2319 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2320 }
2321 } else {
2322 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2323 }
2324 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2325
2326 switch (cond) {
2327 case kCondEQ:
2328 case kCondNE:
2329 if (use_imm && IsUint<16>(rhs_imm)) {
2330 __ Xori(dst, lhs, rhs_imm);
2331 } else {
2332 if (use_imm) {
2333 rhs_reg = TMP;
2334 __ LoadConst64(rhs_reg, rhs_imm);
2335 }
2336 __ Xor(dst, lhs, rhs_reg);
2337 }
2338 if (cond == kCondEQ) {
2339 __ Sltiu(dst, dst, 1);
2340 } else {
2341 __ Sltu(dst, ZERO, dst);
2342 }
2343 break;
2344
2345 case kCondLT:
2346 case kCondGE:
2347 if (use_imm && IsInt<16>(rhs_imm)) {
2348 __ Slti(dst, lhs, rhs_imm);
2349 } else {
2350 if (use_imm) {
2351 rhs_reg = TMP;
2352 __ LoadConst64(rhs_reg, rhs_imm);
2353 }
2354 __ Slt(dst, lhs, rhs_reg);
2355 }
2356 if (cond == kCondGE) {
2357 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2358 // only the slt instruction but no sge.
2359 __ Xori(dst, dst, 1);
2360 }
2361 break;
2362
2363 case kCondLE:
2364 case kCondGT:
2365 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2366 // Simulate lhs <= rhs via lhs < rhs + 1.
2367 __ Slti(dst, lhs, rhs_imm_plus_one);
2368 if (cond == kCondGT) {
2369 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2370 // only the slti instruction but no sgti.
2371 __ Xori(dst, dst, 1);
2372 }
2373 } else {
2374 if (use_imm) {
2375 rhs_reg = TMP;
2376 __ LoadConst64(rhs_reg, rhs_imm);
2377 }
2378 __ Slt(dst, rhs_reg, lhs);
2379 if (cond == kCondLE) {
2380 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2381 // only the slt instruction but no sle.
2382 __ Xori(dst, dst, 1);
2383 }
2384 }
2385 break;
2386
2387 case kCondB:
2388 case kCondAE:
2389 if (use_imm && IsInt<16>(rhs_imm)) {
2390 // Sltiu sign-extends its 16-bit immediate operand before
2391 // the comparison and thus lets us compare directly with
2392 // unsigned values in the ranges [0, 0x7fff] and
2393 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2394 __ Sltiu(dst, lhs, rhs_imm);
2395 } else {
2396 if (use_imm) {
2397 rhs_reg = TMP;
2398 __ LoadConst64(rhs_reg, rhs_imm);
2399 }
2400 __ Sltu(dst, lhs, rhs_reg);
2401 }
2402 if (cond == kCondAE) {
2403 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2404 // only the sltu instruction but no sgeu.
2405 __ Xori(dst, dst, 1);
2406 }
2407 break;
2408
2409 case kCondBE:
2410 case kCondA:
2411 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2412 // Simulate lhs <= rhs via lhs < rhs + 1.
2413 // Note that this only works if rhs + 1 does not overflow
2414 // to 0, hence the check above.
2415 // Sltiu sign-extends its 16-bit immediate operand before
2416 // the comparison and thus lets us compare directly with
2417 // unsigned values in the ranges [0, 0x7fff] and
2418 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2419 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2420 if (cond == kCondA) {
2421 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2422 // only the sltiu instruction but no sgtiu.
2423 __ Xori(dst, dst, 1);
2424 }
2425 } else {
2426 if (use_imm) {
2427 rhs_reg = TMP;
2428 __ LoadConst64(rhs_reg, rhs_imm);
2429 }
2430 __ Sltu(dst, rhs_reg, lhs);
2431 if (cond == kCondBE) {
2432 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2433 // only the sltu instruction but no sleu.
2434 __ Xori(dst, dst, 1);
2435 }
2436 }
2437 break;
2438 }
2439}
2440
2441void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2442 bool is64bit,
2443 LocationSummary* locations,
2444 Mips64Label* label) {
2445 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2446 Location rhs_location = locations->InAt(1);
2447 GpuRegister rhs_reg = ZERO;
2448 int64_t rhs_imm = 0;
2449 bool use_imm = rhs_location.IsConstant();
2450 if (use_imm) {
2451 if (is64bit) {
2452 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2453 } else {
2454 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2455 }
2456 } else {
2457 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2458 }
2459
2460 if (use_imm && rhs_imm == 0) {
2461 switch (cond) {
2462 case kCondEQ:
2463 case kCondBE: // <= 0 if zero
2464 __ Beqzc(lhs, label);
2465 break;
2466 case kCondNE:
2467 case kCondA: // > 0 if non-zero
2468 __ Bnezc(lhs, label);
2469 break;
2470 case kCondLT:
2471 __ Bltzc(lhs, label);
2472 break;
2473 case kCondGE:
2474 __ Bgezc(lhs, label);
2475 break;
2476 case kCondLE:
2477 __ Blezc(lhs, label);
2478 break;
2479 case kCondGT:
2480 __ Bgtzc(lhs, label);
2481 break;
2482 case kCondB: // always false
2483 break;
2484 case kCondAE: // always true
2485 __ Bc(label);
2486 break;
2487 }
2488 } else {
2489 if (use_imm) {
2490 rhs_reg = TMP;
2491 __ LoadConst64(rhs_reg, rhs_imm);
2492 }
2493 switch (cond) {
2494 case kCondEQ:
2495 __ Beqc(lhs, rhs_reg, label);
2496 break;
2497 case kCondNE:
2498 __ Bnec(lhs, rhs_reg, label);
2499 break;
2500 case kCondLT:
2501 __ Bltc(lhs, rhs_reg, label);
2502 break;
2503 case kCondGE:
2504 __ Bgec(lhs, rhs_reg, label);
2505 break;
2506 case kCondLE:
2507 __ Bgec(rhs_reg, lhs, label);
2508 break;
2509 case kCondGT:
2510 __ Bltc(rhs_reg, lhs, label);
2511 break;
2512 case kCondB:
2513 __ Bltuc(lhs, rhs_reg, label);
2514 break;
2515 case kCondAE:
2516 __ Bgeuc(lhs, rhs_reg, label);
2517 break;
2518 case kCondBE:
2519 __ Bgeuc(rhs_reg, lhs, label);
2520 break;
2521 case kCondA:
2522 __ Bltuc(rhs_reg, lhs, label);
2523 break;
2524 }
2525 }
2526}
2527
2528void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2529 bool gt_bias,
2530 Primitive::Type type,
2531 LocationSummary* locations,
2532 Mips64Label* label) {
2533 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2534 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2535 if (type == Primitive::kPrimFloat) {
2536 switch (cond) {
2537 case kCondEQ:
2538 __ CmpEqS(FTMP, lhs, rhs);
2539 __ Bc1nez(FTMP, label);
2540 break;
2541 case kCondNE:
2542 __ CmpEqS(FTMP, lhs, rhs);
2543 __ Bc1eqz(FTMP, label);
2544 break;
2545 case kCondLT:
2546 if (gt_bias) {
2547 __ CmpLtS(FTMP, lhs, rhs);
2548 } else {
2549 __ CmpUltS(FTMP, lhs, rhs);
2550 }
2551 __ Bc1nez(FTMP, label);
2552 break;
2553 case kCondLE:
2554 if (gt_bias) {
2555 __ CmpLeS(FTMP, lhs, rhs);
2556 } else {
2557 __ CmpUleS(FTMP, lhs, rhs);
2558 }
2559 __ Bc1nez(FTMP, label);
2560 break;
2561 case kCondGT:
2562 if (gt_bias) {
2563 __ CmpUltS(FTMP, rhs, lhs);
2564 } else {
2565 __ CmpLtS(FTMP, rhs, lhs);
2566 }
2567 __ Bc1nez(FTMP, label);
2568 break;
2569 case kCondGE:
2570 if (gt_bias) {
2571 __ CmpUleS(FTMP, rhs, lhs);
2572 } else {
2573 __ CmpLeS(FTMP, rhs, lhs);
2574 }
2575 __ Bc1nez(FTMP, label);
2576 break;
2577 default:
2578 LOG(FATAL) << "Unexpected non-floating-point condition";
2579 }
2580 } else {
2581 DCHECK_EQ(type, Primitive::kPrimDouble);
2582 switch (cond) {
2583 case kCondEQ:
2584 __ CmpEqD(FTMP, lhs, rhs);
2585 __ Bc1nez(FTMP, label);
2586 break;
2587 case kCondNE:
2588 __ CmpEqD(FTMP, lhs, rhs);
2589 __ Bc1eqz(FTMP, label);
2590 break;
2591 case kCondLT:
2592 if (gt_bias) {
2593 __ CmpLtD(FTMP, lhs, rhs);
2594 } else {
2595 __ CmpUltD(FTMP, lhs, rhs);
2596 }
2597 __ Bc1nez(FTMP, label);
2598 break;
2599 case kCondLE:
2600 if (gt_bias) {
2601 __ CmpLeD(FTMP, lhs, rhs);
2602 } else {
2603 __ CmpUleD(FTMP, lhs, rhs);
2604 }
2605 __ Bc1nez(FTMP, label);
2606 break;
2607 case kCondGT:
2608 if (gt_bias) {
2609 __ CmpUltD(FTMP, rhs, lhs);
2610 } else {
2611 __ CmpLtD(FTMP, rhs, lhs);
2612 }
2613 __ Bc1nez(FTMP, label);
2614 break;
2615 case kCondGE:
2616 if (gt_bias) {
2617 __ CmpUleD(FTMP, rhs, lhs);
2618 } else {
2619 __ CmpLeD(FTMP, rhs, lhs);
2620 }
2621 __ Bc1nez(FTMP, label);
2622 break;
2623 default:
2624 LOG(FATAL) << "Unexpected non-floating-point condition";
2625 }
2626 }
2627}
2628
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002630 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002631 Mips64Label* true_target,
2632 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002633 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634
David Brazdil0debae72015-11-12 18:37:00 +00002635 if (true_target == nullptr && false_target == nullptr) {
2636 // Nothing to do. The code always falls through.
2637 return;
2638 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002639 // Constant condition, statically compared against "true" (integer value 1).
2640 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002641 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002642 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002643 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002644 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002645 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002646 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002647 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002648 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002649 }
David Brazdil0debae72015-11-12 18:37:00 +00002650 return;
2651 }
2652
2653 // The following code generates these patterns:
2654 // (1) true_target == nullptr && false_target != nullptr
2655 // - opposite condition true => branch to false_target
2656 // (2) true_target != nullptr && false_target == nullptr
2657 // - condition true => branch to true_target
2658 // (3) true_target != nullptr && false_target != nullptr
2659 // - condition true => branch to true_target
2660 // - branch to false_target
2661 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002662 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002663 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002664 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002665 if (true_target == nullptr) {
2666 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2667 } else {
2668 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2669 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002670 } else {
2671 // The condition instruction has not been materialized, use its inputs as
2672 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002673 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002674 Primitive::Type type = condition->InputAt(0)->GetType();
2675 LocationSummary* locations = cond->GetLocations();
2676 IfCondition if_cond = condition->GetCondition();
2677 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002678
David Brazdil0debae72015-11-12 18:37:00 +00002679 if (true_target == nullptr) {
2680 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002681 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002682 }
2683
Alexey Frunze299a9392015-12-08 16:08:02 -08002684 switch (type) {
2685 default:
2686 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2687 break;
2688 case Primitive::kPrimLong:
2689 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2690 break;
2691 case Primitive::kPrimFloat:
2692 case Primitive::kPrimDouble:
2693 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2694 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002695 }
2696 }
David Brazdil0debae72015-11-12 18:37:00 +00002697
2698 // If neither branch falls through (case 3), the conditional branch to `true_target`
2699 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2700 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002701 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002702 }
2703}
2704
2705void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002707 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002708 locations->SetInAt(0, Location::RequiresRegister());
2709 }
2710}
2711
2712void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002713 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2714 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002715 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002716 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002717 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002718 nullptr : codegen_->GetLabelOf(false_successor);
2719 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002720}
2721
2722void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2723 LocationSummary* locations = new (GetGraph()->GetArena())
2724 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002725 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002726 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002727 locations->SetInAt(0, Location::RequiresRegister());
2728 }
2729}
2730
2731void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002732 SlowPathCodeMIPS64* slow_path =
2733 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002734 GenerateTestAndBranch(deoptimize,
2735 /* condition_input_index */ 0,
2736 slow_path->GetEntryLabel(),
2737 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002738}
2739
Mingyao Yang063fc772016-08-02 11:02:54 -07002740void LocationsBuilderMIPS64::VisitShouldDeoptimizeFlag(
2741 HShouldDeoptimizeFlag* flag ATTRIBUTE_UNUSED) {
2742 // TODO: to be implemented.
2743}
2744
2745void InstructionCodeGeneratorMIPS64::VisitShouldDeoptimizeFlag(
2746 HShouldDeoptimizeFlag* flag ATTRIBUTE_UNUSED) {
2747 // TODO: to be implemented.
2748}
2749
David Brazdil74eb1b22015-12-14 11:44:01 +00002750void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2752 if (Primitive::IsFloatingPointType(select->GetType())) {
2753 locations->SetInAt(0, Location::RequiresFpuRegister());
2754 locations->SetInAt(1, Location::RequiresFpuRegister());
2755 } else {
2756 locations->SetInAt(0, Location::RequiresRegister());
2757 locations->SetInAt(1, Location::RequiresRegister());
2758 }
2759 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2760 locations->SetInAt(2, Location::RequiresRegister());
2761 }
2762 locations->SetOut(Location::SameAsFirstInput());
2763}
2764
2765void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2766 LocationSummary* locations = select->GetLocations();
2767 Mips64Label false_target;
2768 GenerateTestAndBranch(select,
2769 /* condition_input_index */ 2,
2770 /* true_target */ nullptr,
2771 &false_target);
2772 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2773 __ Bind(&false_target);
2774}
2775
David Srbecky0cf44932015-12-09 14:09:59 +00002776void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2777 new (GetGraph()->GetArena()) LocationSummary(info);
2778}
2779
David Srbeckyd28f4a02016-03-14 17:14:24 +00002780void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2781 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002782}
2783
2784void CodeGeneratorMIPS64::GenerateNop() {
2785 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002786}
2787
Alexey Frunze4dda3372015-06-01 18:31:49 -07002788void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2789 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2790 LocationSummary* locations =
2791 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2792 locations->SetInAt(0, Location::RequiresRegister());
2793 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2794 locations->SetOut(Location::RequiresFpuRegister());
2795 } else {
2796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2797 }
2798}
2799
2800void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2801 const FieldInfo& field_info) {
2802 Primitive::Type type = field_info.GetFieldType();
2803 LocationSummary* locations = instruction->GetLocations();
2804 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2805 LoadOperandType load_type = kLoadUnsignedByte;
2806 switch (type) {
2807 case Primitive::kPrimBoolean:
2808 load_type = kLoadUnsignedByte;
2809 break;
2810 case Primitive::kPrimByte:
2811 load_type = kLoadSignedByte;
2812 break;
2813 case Primitive::kPrimShort:
2814 load_type = kLoadSignedHalfword;
2815 break;
2816 case Primitive::kPrimChar:
2817 load_type = kLoadUnsignedHalfword;
2818 break;
2819 case Primitive::kPrimInt:
2820 case Primitive::kPrimFloat:
2821 load_type = kLoadWord;
2822 break;
2823 case Primitive::kPrimLong:
2824 case Primitive::kPrimDouble:
2825 load_type = kLoadDoubleword;
2826 break;
2827 case Primitive::kPrimNot:
2828 load_type = kLoadUnsignedWord;
2829 break;
2830 case Primitive::kPrimVoid:
2831 LOG(FATAL) << "Unreachable type " << type;
2832 UNREACHABLE();
2833 }
2834 if (!Primitive::IsFloatingPointType(type)) {
2835 DCHECK(locations->Out().IsRegister());
2836 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2837 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2838 } else {
2839 DCHECK(locations->Out().IsFpuRegister());
2840 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2841 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2842 }
2843
2844 codegen_->MaybeRecordImplicitNullCheck(instruction);
2845 // TODO: memory barrier?
2846}
2847
2848void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2849 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2850 LocationSummary* locations =
2851 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2852 locations->SetInAt(0, Location::RequiresRegister());
2853 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2854 locations->SetInAt(1, Location::RequiresFpuRegister());
2855 } else {
2856 locations->SetInAt(1, Location::RequiresRegister());
2857 }
2858}
2859
2860void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002861 const FieldInfo& field_info,
2862 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863 Primitive::Type type = field_info.GetFieldType();
2864 LocationSummary* locations = instruction->GetLocations();
2865 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2866 StoreOperandType store_type = kStoreByte;
2867 switch (type) {
2868 case Primitive::kPrimBoolean:
2869 case Primitive::kPrimByte:
2870 store_type = kStoreByte;
2871 break;
2872 case Primitive::kPrimShort:
2873 case Primitive::kPrimChar:
2874 store_type = kStoreHalfword;
2875 break;
2876 case Primitive::kPrimInt:
2877 case Primitive::kPrimFloat:
2878 case Primitive::kPrimNot:
2879 store_type = kStoreWord;
2880 break;
2881 case Primitive::kPrimLong:
2882 case Primitive::kPrimDouble:
2883 store_type = kStoreDoubleword;
2884 break;
2885 case Primitive::kPrimVoid:
2886 LOG(FATAL) << "Unreachable type " << type;
2887 UNREACHABLE();
2888 }
2889 if (!Primitive::IsFloatingPointType(type)) {
2890 DCHECK(locations->InAt(1).IsRegister());
2891 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2892 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2893 } else {
2894 DCHECK(locations->InAt(1).IsFpuRegister());
2895 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2896 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2897 }
2898
2899 codegen_->MaybeRecordImplicitNullCheck(instruction);
2900 // TODO: memory barriers?
2901 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2902 DCHECK(locations->InAt(1).IsRegister());
2903 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002904 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002905 }
2906}
2907
2908void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2909 HandleFieldGet(instruction, instruction->GetFieldInfo());
2910}
2911
2912void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2913 HandleFieldGet(instruction, instruction->GetFieldInfo());
2914}
2915
2916void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2917 HandleFieldSet(instruction, instruction->GetFieldInfo());
2918}
2919
2920void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002921 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002922}
2923
2924void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2925 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002926 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2928 locations->SetInAt(0, Location::RequiresRegister());
2929 locations->SetInAt(1, Location::RequiresRegister());
2930 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002931 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002932 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2933}
2934
2935void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2936 LocationSummary* locations = instruction->GetLocations();
2937 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2938 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2939 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2940
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002941 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002942
2943 // Return 0 if `obj` is null.
2944 // TODO: Avoid this check if we know `obj` is not null.
2945 __ Move(out, ZERO);
2946 __ Beqzc(obj, &done);
2947
2948 // Compare the class of `obj` with `cls`.
2949 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002950 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002951 // Classes must be equal for the instanceof to succeed.
2952 __ Xor(out, out, cls);
2953 __ Sltiu(out, out, 1);
2954 } else {
2955 // If the classes are not equal, we go into a slow path.
2956 DCHECK(locations->OnlyCallsOnSlowPath());
2957 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002958 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002959 codegen_->AddSlowPath(slow_path);
2960 __ Bnec(out, cls, slow_path->GetEntryLabel());
2961 __ LoadConst32(out, 1);
2962 __ Bind(slow_path->GetExitLabel());
2963 }
2964
2965 __ Bind(&done);
2966}
2967
2968void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2970 locations->SetOut(Location::ConstantLocation(constant));
2971}
2972
2973void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2974 // Will be generated at use site.
2975}
2976
2977void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2978 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2979 locations->SetOut(Location::ConstantLocation(constant));
2980}
2981
2982void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2983 // Will be generated at use site.
2984}
2985
Calin Juravle175dc732015-08-25 15:42:32 +01002986void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2987 // The trampoline uses the same calling convention as dex calling conventions,
2988 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2989 // the method_idx.
2990 HandleInvoke(invoke);
2991}
2992
2993void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2994 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2995}
2996
Alexey Frunze4dda3372015-06-01 18:31:49 -07002997void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2998 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2999 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3000}
3001
3002void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3003 HandleInvoke(invoke);
3004 // The register T0 is required to be used for the hidden argument in
3005 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3006 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3007}
3008
3009void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
3010 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3011 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003012 Location receiver = invoke->GetLocations()->InAt(0);
3013 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003014 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003015
3016 // Set the hidden argument.
3017 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
3018 invoke->GetDexMethodIndex());
3019
3020 // temp = object->GetClass();
3021 if (receiver.IsStackSlot()) {
3022 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
3023 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
3024 } else {
3025 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
3026 }
3027 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003028 __ LoadFromOffset(kLoadDoubleword, temp, temp,
3029 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
3030 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003031 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003032 // temp = temp->GetImtEntryAt(method_offset);
3033 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3034 // T9 = temp->GetEntryPoint();
3035 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3036 // T9();
3037 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003038 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039 DCHECK(!codegen_->IsLeafMethod());
3040 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3041}
3042
3043void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07003044 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3045 if (intrinsic.TryDispatch(invoke)) {
3046 return;
3047 }
3048
Alexey Frunze4dda3372015-06-01 18:31:49 -07003049 HandleInvoke(invoke);
3050}
3051
3052void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003053 // Explicit clinit checks triggered by static invokes must have been pruned by
3054 // art::PrepareForRegisterAllocation.
3055 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003056
Chris Larsen3039e382015-08-26 07:54:08 -07003057 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3058 if (intrinsic.TryDispatch(invoke)) {
3059 return;
3060 }
3061
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003063}
3064
Chris Larsen3039e382015-08-26 07:54:08 -07003065static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003066 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003067 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3068 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003069 return true;
3070 }
3071 return false;
3072}
3073
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003074HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3075 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3076 // TODO: Implement other kinds.
3077 return HLoadString::LoadKind::kDexCacheViaMethod;
3078}
3079
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003080HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3081 HLoadClass::LoadKind desired_class_load_kind) {
3082 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3083 // TODO: Implement other kinds.
3084 return HLoadClass::LoadKind::kDexCacheViaMethod;
3085}
3086
Vladimir Markodc151b22015-10-15 18:02:30 +01003087HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3088 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003089 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunze19f6c692016-11-30 19:19:55 -08003090 // On MIPS64 we support all dispatch types.
3091 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003092}
3093
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3095 // All registers are assumed to be correctly set up per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003096 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunze19f6c692016-11-30 19:19:55 -08003097 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3098 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3099
3100 // For better instruction scheduling we load the direct code pointer before the method pointer.
3101 switch (code_ptr_location) {
3102 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3103 // T9 = invoke->GetDirectCodePtr();
3104 __ LoadLiteral(T9, kLoadDoubleword, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3105 break;
3106 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3107 // T9 = code address from literal pool with link-time patch.
3108 __ LoadLiteral(T9,
3109 kLoadUnsignedWord,
3110 DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3111 break;
3112 default:
3113 break;
3114 }
3115
3116 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003117 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003118 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003119 uint32_t offset =
3120 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003121 __ LoadFromOffset(kLoadDoubleword,
3122 temp.AsRegister<GpuRegister>(),
3123 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003124 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003125 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003126 }
Vladimir Marko58155012015-08-19 12:49:41 +00003127 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003128 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003129 break;
3130 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003131 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3132 kLoadDoubleword,
3133 DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003134 break;
3135 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003136 __ LoadLiteral(temp.AsRegister<GpuRegister>(),
3137 kLoadUnsignedWord,
3138 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3139 break;
3140 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3141 uint32_t offset = invoke->GetDexCacheArrayOffset();
3142 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3143 NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset);
3144 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3145 __ Ld(temp.AsRegister<GpuRegister>(), AT, /* placeholder */ 0x5678);
3146 break;
3147 }
Vladimir Marko58155012015-08-19 12:49:41 +00003148 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003149 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003150 GpuRegister reg = temp.AsRegister<GpuRegister>();
3151 GpuRegister method_reg;
3152 if (current_method.IsRegister()) {
3153 method_reg = current_method.AsRegister<GpuRegister>();
3154 } else {
3155 // TODO: use the appropriate DCHECK() here if possible.
3156 // DCHECK(invoke->GetLocations()->Intrinsified());
3157 DCHECK(!current_method.IsValid());
3158 method_reg = reg;
3159 __ Ld(reg, SP, kCurrentMethodStackOffset);
3160 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161
Vladimir Marko58155012015-08-19 12:49:41 +00003162 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003163 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003164 reg,
3165 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003166 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003167 // temp = temp[index_in_cache];
3168 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3169 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003170 __ LoadFromOffset(kLoadDoubleword,
3171 reg,
3172 reg,
3173 CodeGenerator::GetCachePointerOffset(index_in_cache));
3174 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176 }
3177
Alexey Frunze19f6c692016-11-30 19:19:55 -08003178 switch (code_ptr_location) {
Vladimir Marko58155012015-08-19 12:49:41 +00003179 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003180 __ Balc(&frame_entry_label_);
Vladimir Marko58155012015-08-19 12:49:41 +00003181 break;
3182 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunze19f6c692016-11-30 19:19:55 -08003183 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3184 // T9 prepared above for better instruction scheduling.
3185 // T9()
Vladimir Marko58155012015-08-19 12:49:41 +00003186 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003187 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003188 break;
Alexey Frunze19f6c692016-11-30 19:19:55 -08003189 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3190 CodeGeneratorMIPS64::PcRelativePatchInfo* info =
3191 NewPcRelativeCallPatch(*invoke->GetTargetMethod().dex_file,
3192 invoke->GetTargetMethod().dex_method_index);
3193 EmitPcRelativeAddressPlaceholderHigh(info, AT);
3194 __ Jialc(AT, /* placeholder */ 0x5678);
3195 break;
3196 }
Vladimir Marko58155012015-08-19 12:49:41 +00003197 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3198 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3199 __ LoadFromOffset(kLoadDoubleword,
3200 T9,
3201 callee_method.AsRegister<GpuRegister>(),
3202 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003203 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003204 // T9()
3205 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003206 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003207 break;
3208 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003209 DCHECK(!IsLeafMethod());
3210}
3211
3212void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003213 // Explicit clinit checks triggered by static invokes must have been pruned by
3214 // art::PrepareForRegisterAllocation.
3215 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003216
3217 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3218 return;
3219 }
3220
3221 LocationSummary* locations = invoke->GetLocations();
3222 codegen_->GenerateStaticOrDirectCall(invoke,
3223 locations->HasTemps()
3224 ? locations->GetTemp(0)
3225 : Location::NoLocation());
3226 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3227}
3228
Alexey Frunze53afca12015-11-05 16:34:23 -08003229void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003230 // Use the calling convention instead of the location of the receiver, as
3231 // intrinsics may have put the receiver in a different register. In the intrinsics
3232 // slow path, the arguments have been moved to the right place, so here we are
3233 // guaranteed that the receiver is the first register of the calling convention.
3234 InvokeDexCallingConvention calling_convention;
3235 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3236
Alexey Frunze53afca12015-11-05 16:34:23 -08003237 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003238 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3239 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3240 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003241 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003242
3243 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003244 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003245 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003246 // temp = temp->GetMethodAt(method_offset);
3247 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3248 // T9 = temp->GetEntryPoint();
3249 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3250 // T9();
3251 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003252 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003253}
3254
3255void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3256 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3257 return;
3258 }
3259
3260 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261 DCHECK(!codegen_->IsLeafMethod());
3262 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3263}
3264
3265void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003266 InvokeRuntimeCallingConvention calling_convention;
3267 CodeGenerator::CreateLoadClassLocationSummary(
3268 cls,
3269 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003270 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003271}
3272
3273void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3274 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003275 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003276 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003277 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003278 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003279 return;
3280 }
3281
3282 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3283 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3284 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003285 DCHECK(!cls->CanCallRuntime());
3286 DCHECK(!cls->MustGenerateClinitCheck());
3287 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3288 ArtMethod::DeclaringClassOffset().Int32Value());
3289 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003290 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3291 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003292 __ LoadFromOffset(
Andreas Gampea5b09a62016-11-17 15:21:22 -08003293 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_));
Vladimir Marko05792b92015-08-03 11:56:49 +01003294 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003295 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3296 DCHECK(cls->CanCallRuntime());
3297 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3298 cls,
3299 cls,
3300 cls->GetDexPc(),
3301 cls->MustGenerateClinitCheck());
3302 codegen_->AddSlowPath(slow_path);
3303 if (!cls->IsInDexCache()) {
3304 __ Beqzc(out, slow_path->GetEntryLabel());
3305 }
3306 if (cls->MustGenerateClinitCheck()) {
3307 GenerateClassInitializationCheck(slow_path, out);
3308 } else {
3309 __ Bind(slow_path->GetExitLabel());
3310 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003311 }
3312 }
3313}
3314
David Brazdilcb1c0552015-08-04 16:22:25 +01003315static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003316 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003317}
3318
Alexey Frunze4dda3372015-06-01 18:31:49 -07003319void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3320 LocationSummary* locations =
3321 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3322 locations->SetOut(Location::RequiresRegister());
3323}
3324
3325void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3326 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003327 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3328}
3329
3330void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3331 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3332}
3333
3334void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3335 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003336}
3337
Alexey Frunze4dda3372015-06-01 18:31:49 -07003338void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003339 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3340 ? LocationSummary::kCallOnSlowPath
3341 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003343 locations->SetInAt(0, Location::RequiresRegister());
3344 locations->SetOut(Location::RequiresRegister());
3345}
3346
3347void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003348 // TODO: Re-add the compiler code to do string dex cache lookup again.
3349 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3350 codegen_->AddSlowPath(slow_path);
3351 __ Bc(slow_path->GetEntryLabel());
3352 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003353}
3354
Alexey Frunze4dda3372015-06-01 18:31:49 -07003355void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3357 locations->SetOut(Location::ConstantLocation(constant));
3358}
3359
3360void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3361 // Will be generated at use site.
3362}
3363
3364void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3365 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003367 InvokeRuntimeCallingConvention calling_convention;
3368 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3369}
3370
3371void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003372 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003373 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003374 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003375 if (instruction->IsEnter()) {
3376 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3377 } else {
3378 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3379 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003380}
3381
3382void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3383 LocationSummary* locations =
3384 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3385 switch (mul->GetResultType()) {
3386 case Primitive::kPrimInt:
3387 case Primitive::kPrimLong:
3388 locations->SetInAt(0, Location::RequiresRegister());
3389 locations->SetInAt(1, Location::RequiresRegister());
3390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3391 break;
3392
3393 case Primitive::kPrimFloat:
3394 case Primitive::kPrimDouble:
3395 locations->SetInAt(0, Location::RequiresFpuRegister());
3396 locations->SetInAt(1, Location::RequiresFpuRegister());
3397 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3398 break;
3399
3400 default:
3401 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3402 }
3403}
3404
3405void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3406 Primitive::Type type = instruction->GetType();
3407 LocationSummary* locations = instruction->GetLocations();
3408
3409 switch (type) {
3410 case Primitive::kPrimInt:
3411 case Primitive::kPrimLong: {
3412 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3413 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3414 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3415 if (type == Primitive::kPrimInt)
3416 __ MulR6(dst, lhs, rhs);
3417 else
3418 __ Dmul(dst, lhs, rhs);
3419 break;
3420 }
3421 case Primitive::kPrimFloat:
3422 case Primitive::kPrimDouble: {
3423 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3424 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3425 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3426 if (type == Primitive::kPrimFloat)
3427 __ MulS(dst, lhs, rhs);
3428 else
3429 __ MulD(dst, lhs, rhs);
3430 break;
3431 }
3432 default:
3433 LOG(FATAL) << "Unexpected mul type " << type;
3434 }
3435}
3436
3437void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3438 LocationSummary* locations =
3439 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3440 switch (neg->GetResultType()) {
3441 case Primitive::kPrimInt:
3442 case Primitive::kPrimLong:
3443 locations->SetInAt(0, Location::RequiresRegister());
3444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3445 break;
3446
3447 case Primitive::kPrimFloat:
3448 case Primitive::kPrimDouble:
3449 locations->SetInAt(0, Location::RequiresFpuRegister());
3450 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3451 break;
3452
3453 default:
3454 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3455 }
3456}
3457
3458void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3459 Primitive::Type type = instruction->GetType();
3460 LocationSummary* locations = instruction->GetLocations();
3461
3462 switch (type) {
3463 case Primitive::kPrimInt:
3464 case Primitive::kPrimLong: {
3465 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3466 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3467 if (type == Primitive::kPrimInt)
3468 __ Subu(dst, ZERO, src);
3469 else
3470 __ Dsubu(dst, ZERO, src);
3471 break;
3472 }
3473 case Primitive::kPrimFloat:
3474 case Primitive::kPrimDouble: {
3475 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3476 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3477 if (type == Primitive::kPrimFloat)
3478 __ NegS(dst, src);
3479 else
3480 __ NegD(dst, src);
3481 break;
3482 }
3483 default:
3484 LOG(FATAL) << "Unexpected neg type " << type;
3485 }
3486}
3487
3488void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3489 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003491 InvokeRuntimeCallingConvention calling_convention;
3492 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3493 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3494 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3495 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3499 LocationSummary* locations = instruction->GetLocations();
3500 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08003501 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(),
3502 instruction->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003503 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003504 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3505}
3506
3507void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3508 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003509 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003510 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003511 if (instruction->IsStringAlloc()) {
3512 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3513 } else {
3514 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3515 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3516 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003517 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3518}
3519
3520void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003521 if (instruction->IsStringAlloc()) {
3522 // String is allocated through StringFactory. Call NewEmptyString entry point.
3523 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003524 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003525 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003526 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3527 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3528 __ Jalr(T9);
3529 __ Nop();
3530 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3531 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003532 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003533 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3534 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003535}
3536
3537void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3539 locations->SetInAt(0, Location::RequiresRegister());
3540 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3541}
3542
3543void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3544 Primitive::Type type = instruction->GetType();
3545 LocationSummary* locations = instruction->GetLocations();
3546
3547 switch (type) {
3548 case Primitive::kPrimInt:
3549 case Primitive::kPrimLong: {
3550 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3551 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3552 __ Nor(dst, src, ZERO);
3553 break;
3554 }
3555
3556 default:
3557 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3558 }
3559}
3560
3561void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3563 locations->SetInAt(0, Location::RequiresRegister());
3564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3565}
3566
3567void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3568 LocationSummary* locations = instruction->GetLocations();
3569 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3570 locations->InAt(0).AsRegister<GpuRegister>(),
3571 1);
3572}
3573
3574void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003575 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3576 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003577}
3578
Calin Juravle2ae48182016-03-16 14:05:09 +00003579void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3580 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003581 return;
3582 }
3583 Location obj = instruction->GetLocations()->InAt(0);
3584
3585 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003586 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587}
3588
Calin Juravle2ae48182016-03-16 14:05:09 +00003589void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003590 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003591 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003592
3593 Location obj = instruction->GetLocations()->InAt(0);
3594
3595 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3596}
3597
3598void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003599 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003600}
3601
3602void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3603 HandleBinaryOp(instruction);
3604}
3605
3606void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3607 HandleBinaryOp(instruction);
3608}
3609
3610void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3611 LOG(FATAL) << "Unreachable";
3612}
3613
3614void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3615 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3616}
3617
3618void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3620 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3621 if (location.IsStackSlot()) {
3622 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3623 } else if (location.IsDoubleStackSlot()) {
3624 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3625 }
3626 locations->SetOut(location);
3627}
3628
3629void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3630 ATTRIBUTE_UNUSED) {
3631 // Nothing to do, the parameter is already at its location.
3632}
3633
3634void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3637 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3638}
3639
3640void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3641 ATTRIBUTE_UNUSED) {
3642 // Nothing to do, the method is already at its location.
3643}
3644
3645void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003647 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003648 locations->SetInAt(i, Location::Any());
3649 }
3650 locations->SetOut(Location::Any());
3651}
3652
3653void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3654 LOG(FATAL) << "Unreachable";
3655}
3656
3657void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3658 Primitive::Type type = rem->GetResultType();
3659 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003660 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3661 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3663
3664 switch (type) {
3665 case Primitive::kPrimInt:
3666 case Primitive::kPrimLong:
3667 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003668 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003669 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3670 break;
3671
3672 case Primitive::kPrimFloat:
3673 case Primitive::kPrimDouble: {
3674 InvokeRuntimeCallingConvention calling_convention;
3675 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3676 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3677 locations->SetOut(calling_convention.GetReturnLocation(type));
3678 break;
3679 }
3680
3681 default:
3682 LOG(FATAL) << "Unexpected rem type " << type;
3683 }
3684}
3685
3686void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3687 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003688
3689 switch (type) {
3690 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003691 case Primitive::kPrimLong:
3692 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003693 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003694
3695 case Primitive::kPrimFloat:
3696 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003697 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3698 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003699 if (type == Primitive::kPrimFloat) {
3700 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3701 } else {
3702 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3703 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003704 break;
3705 }
3706 default:
3707 LOG(FATAL) << "Unexpected rem type " << type;
3708 }
3709}
3710
3711void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3712 memory_barrier->SetLocations(nullptr);
3713}
3714
3715void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3716 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3717}
3718
3719void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3721 Primitive::Type return_type = ret->InputAt(0)->GetType();
3722 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3723}
3724
3725void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3726 codegen_->GenerateFrameExit();
3727}
3728
3729void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3730 ret->SetLocations(nullptr);
3731}
3732
3733void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3734 codegen_->GenerateFrameExit();
3735}
3736
Alexey Frunze92d90602015-12-18 18:16:36 -08003737void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3738 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003739}
3740
Alexey Frunze92d90602015-12-18 18:16:36 -08003741void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3742 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003743}
3744
Alexey Frunze4dda3372015-06-01 18:31:49 -07003745void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3746 HandleShift(shl);
3747}
3748
3749void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3750 HandleShift(shl);
3751}
3752
3753void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3754 HandleShift(shr);
3755}
3756
3757void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3758 HandleShift(shr);
3759}
3760
Alexey Frunze4dda3372015-06-01 18:31:49 -07003761void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3762 HandleBinaryOp(instruction);
3763}
3764
3765void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3766 HandleBinaryOp(instruction);
3767}
3768
3769void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3770 HandleFieldGet(instruction, instruction->GetFieldInfo());
3771}
3772
3773void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3774 HandleFieldGet(instruction, instruction->GetFieldInfo());
3775}
3776
3777void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3778 HandleFieldSet(instruction, instruction->GetFieldInfo());
3779}
3780
3781void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003782 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003783}
3784
Calin Juravlee460d1d2015-09-29 04:52:17 +01003785void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3786 HUnresolvedInstanceFieldGet* instruction) {
3787 FieldAccessCallingConventionMIPS64 calling_convention;
3788 codegen_->CreateUnresolvedFieldLocationSummary(
3789 instruction, instruction->GetFieldType(), calling_convention);
3790}
3791
3792void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3793 HUnresolvedInstanceFieldGet* instruction) {
3794 FieldAccessCallingConventionMIPS64 calling_convention;
3795 codegen_->GenerateUnresolvedFieldAccess(instruction,
3796 instruction->GetFieldType(),
3797 instruction->GetFieldIndex(),
3798 instruction->GetDexPc(),
3799 calling_convention);
3800}
3801
3802void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3803 HUnresolvedInstanceFieldSet* instruction) {
3804 FieldAccessCallingConventionMIPS64 calling_convention;
3805 codegen_->CreateUnresolvedFieldLocationSummary(
3806 instruction, instruction->GetFieldType(), calling_convention);
3807}
3808
3809void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3810 HUnresolvedInstanceFieldSet* instruction) {
3811 FieldAccessCallingConventionMIPS64 calling_convention;
3812 codegen_->GenerateUnresolvedFieldAccess(instruction,
3813 instruction->GetFieldType(),
3814 instruction->GetFieldIndex(),
3815 instruction->GetDexPc(),
3816 calling_convention);
3817}
3818
3819void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3820 HUnresolvedStaticFieldGet* instruction) {
3821 FieldAccessCallingConventionMIPS64 calling_convention;
3822 codegen_->CreateUnresolvedFieldLocationSummary(
3823 instruction, instruction->GetFieldType(), calling_convention);
3824}
3825
3826void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3827 HUnresolvedStaticFieldGet* instruction) {
3828 FieldAccessCallingConventionMIPS64 calling_convention;
3829 codegen_->GenerateUnresolvedFieldAccess(instruction,
3830 instruction->GetFieldType(),
3831 instruction->GetFieldIndex(),
3832 instruction->GetDexPc(),
3833 calling_convention);
3834}
3835
3836void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3837 HUnresolvedStaticFieldSet* instruction) {
3838 FieldAccessCallingConventionMIPS64 calling_convention;
3839 codegen_->CreateUnresolvedFieldLocationSummary(
3840 instruction, instruction->GetFieldType(), calling_convention);
3841}
3842
3843void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3844 HUnresolvedStaticFieldSet* instruction) {
3845 FieldAccessCallingConventionMIPS64 calling_convention;
3846 codegen_->GenerateUnresolvedFieldAccess(instruction,
3847 instruction->GetFieldType(),
3848 instruction->GetFieldIndex(),
3849 instruction->GetDexPc(),
3850 calling_convention);
3851}
3852
Alexey Frunze4dda3372015-06-01 18:31:49 -07003853void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003854 LocationSummary* locations =
3855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003856 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003857}
3858
3859void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3860 HBasicBlock* block = instruction->GetBlock();
3861 if (block->GetLoopInformation() != nullptr) {
3862 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3863 // The back edge will generate the suspend check.
3864 return;
3865 }
3866 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3867 // The goto will generate the suspend check.
3868 return;
3869 }
3870 GenerateSuspendCheck(instruction, nullptr);
3871}
3872
Alexey Frunze4dda3372015-06-01 18:31:49 -07003873void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3874 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003875 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003876 InvokeRuntimeCallingConvention calling_convention;
3877 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3878}
3879
3880void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003881 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003882 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3883}
3884
3885void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3886 Primitive::Type input_type = conversion->GetInputType();
3887 Primitive::Type result_type = conversion->GetResultType();
3888 DCHECK_NE(input_type, result_type);
3889
3890 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3891 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3892 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3893 }
3894
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3896
3897 if (Primitive::IsFloatingPointType(input_type)) {
3898 locations->SetInAt(0, Location::RequiresFpuRegister());
3899 } else {
3900 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003901 }
3902
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003903 if (Primitive::IsFloatingPointType(result_type)) {
3904 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003905 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003907 }
3908}
3909
3910void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3911 LocationSummary* locations = conversion->GetLocations();
3912 Primitive::Type result_type = conversion->GetResultType();
3913 Primitive::Type input_type = conversion->GetInputType();
3914
3915 DCHECK_NE(input_type, result_type);
3916
3917 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3918 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3919 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3920
3921 switch (result_type) {
3922 case Primitive::kPrimChar:
3923 __ Andi(dst, src, 0xFFFF);
3924 break;
3925 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003926 if (input_type == Primitive::kPrimLong) {
3927 // Type conversion from long to types narrower than int is a result of code
3928 // transformations. To avoid unpredictable results for SEB and SEH, we first
3929 // need to sign-extend the low 32-bit value into bits 32 through 63.
3930 __ Sll(dst, src, 0);
3931 __ Seb(dst, dst);
3932 } else {
3933 __ Seb(dst, src);
3934 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003935 break;
3936 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003937 if (input_type == Primitive::kPrimLong) {
3938 // Type conversion from long to types narrower than int is a result of code
3939 // transformations. To avoid unpredictable results for SEB and SEH, we first
3940 // need to sign-extend the low 32-bit value into bits 32 through 63.
3941 __ Sll(dst, src, 0);
3942 __ Seh(dst, dst);
3943 } else {
3944 __ Seh(dst, src);
3945 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003946 break;
3947 case Primitive::kPrimInt:
3948 case Primitive::kPrimLong:
3949 // Sign-extend 32-bit int into bits 32 through 63 for
3950 // int-to-long and long-to-int conversions
3951 __ Sll(dst, src, 0);
3952 break;
3953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003959 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3960 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3961 if (input_type == Primitive::kPrimLong) {
3962 __ Dmtc1(src, FTMP);
3963 if (result_type == Primitive::kPrimFloat) {
3964 __ Cvtsl(dst, FTMP);
3965 } else {
3966 __ Cvtdl(dst, FTMP);
3967 }
3968 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003969 __ Mtc1(src, FTMP);
3970 if (result_type == Primitive::kPrimFloat) {
3971 __ Cvtsw(dst, FTMP);
3972 } else {
3973 __ Cvtdw(dst, FTMP);
3974 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003975 }
3976 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3977 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003978 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3979 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3980 Mips64Label truncate;
3981 Mips64Label done;
3982
3983 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3984 // value when the input is either a NaN or is outside of the range of the output type
3985 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3986 // the same result.
3987 //
3988 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3989 // value of the output type if the input is outside of the range after the truncation or
3990 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3991 // results. This matches the desired float/double-to-int/long conversion exactly.
3992 //
3993 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3994 //
3995 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3996 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3997 // even though it must be NAN2008=1 on R6.
3998 //
3999 // The code takes care of the different behaviors by first comparing the input to the
4000 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4001 // If the input is greater than or equal to the minimum, it procedes to the truncate
4002 // instruction, which will handle such an input the same way irrespective of NAN2008.
4003 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4004 // in order to return either zero or the minimum value.
4005 //
4006 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4007 // truncate instruction for MIPS64R6.
4008 if (input_type == Primitive::kPrimFloat) {
4009 uint32_t min_val = (result_type == Primitive::kPrimLong)
4010 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4011 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4012 __ LoadConst32(TMP, min_val);
4013 __ Mtc1(TMP, FTMP);
4014 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004016 uint64_t min_val = (result_type == Primitive::kPrimLong)
4017 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4018 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4019 __ LoadConst64(TMP, min_val);
4020 __ Dmtc1(TMP, FTMP);
4021 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004022 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004023
4024 __ Bc1nez(FTMP, &truncate);
4025
4026 if (input_type == Primitive::kPrimFloat) {
4027 __ CmpEqS(FTMP, src, src);
4028 } else {
4029 __ CmpEqD(FTMP, src, src);
4030 }
4031 if (result_type == Primitive::kPrimLong) {
4032 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4033 } else {
4034 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4035 }
4036 __ Mfc1(TMP, FTMP);
4037 __ And(dst, dst, TMP);
4038
4039 __ Bc(&done);
4040
4041 __ Bind(&truncate);
4042
4043 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004044 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004045 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004046 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004047 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004048 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004049 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004050 } else {
4051 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004052 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004053 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004054 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004055 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004056 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004057 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004058
4059 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004060 } else if (Primitive::IsFloatingPointType(result_type) &&
4061 Primitive::IsFloatingPointType(input_type)) {
4062 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4063 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4064 if (result_type == Primitive::kPrimFloat) {
4065 __ Cvtsd(dst, src);
4066 } else {
4067 __ Cvtds(dst, src);
4068 }
4069 } else {
4070 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4071 << " to " << result_type;
4072 }
4073}
4074
4075void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4076 HandleShift(ushr);
4077}
4078
4079void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4080 HandleShift(ushr);
4081}
4082
4083void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4084 HandleBinaryOp(instruction);
4085}
4086
4087void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4088 HandleBinaryOp(instruction);
4089}
4090
4091void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4092 // Nothing to do, this should be removed during prepare for register allocator.
4093 LOG(FATAL) << "Unreachable";
4094}
4095
4096void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4097 // Nothing to do, this should be removed during prepare for register allocator.
4098 LOG(FATAL) << "Unreachable";
4099}
4100
4101void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004102 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004103}
4104
4105void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004106 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004107}
4108
4109void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004110 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111}
4112
4113void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004114 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004115}
4116
4117void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004118 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004119}
4120
4121void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004122 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123}
4124
4125void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004126 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127}
4128
4129void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004130 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131}
4132
4133void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004134 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004135}
4136
4137void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004138 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004139}
4140
4141void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004142 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004143}
4144
4145void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004146 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004147}
4148
Aart Bike9f37602015-10-09 11:15:55 -07004149void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004150 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004151}
4152
4153void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004154 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004155}
4156
4157void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004158 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004159}
4160
4161void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004162 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004163}
4164
4165void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004166 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004167}
4168
4169void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004170 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004171}
4172
4173void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004174 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004175}
4176
4177void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004178 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004179}
4180
Mark Mendellfe57faa2015-09-18 09:26:15 -04004181// Simple implementation of packed switch - generate cascaded compare/jumps.
4182void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4183 LocationSummary* locations =
4184 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4185 locations->SetInAt(0, Location::RequiresRegister());
4186}
4187
4188void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4189 int32_t lower_bound = switch_instr->GetStartValue();
4190 int32_t num_entries = switch_instr->GetNumEntries();
4191 LocationSummary* locations = switch_instr->GetLocations();
4192 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4193 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4194
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004195 // Create a set of compare/jumps.
4196 GpuRegister temp_reg = TMP;
4197 if (IsInt<16>(-lower_bound)) {
4198 __ Addiu(temp_reg, value_reg, -lower_bound);
4199 } else {
4200 __ LoadConst32(AT, -lower_bound);
4201 __ Addu(temp_reg, value_reg, AT);
4202 }
4203 // Jump to default if index is negative
4204 // Note: We don't check the case that index is positive while value < lower_bound, because in
4205 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4206 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4207
Mark Mendellfe57faa2015-09-18 09:26:15 -04004208 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004209 // Jump to successors[0] if value == lower_bound.
4210 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4211 int32_t last_index = 0;
4212 for (; num_entries - last_index > 2; last_index += 2) {
4213 __ Addiu(temp_reg, temp_reg, -2);
4214 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4215 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4216 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4217 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4218 }
4219 if (num_entries - last_index == 2) {
4220 // The last missing case_value.
4221 __ Addiu(temp_reg, temp_reg, -1);
4222 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004223 }
4224
4225 // And the default for any other value.
4226 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004227 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004228 }
4229}
4230
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004231void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4232 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4233}
4234
4235void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4236 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4237}
4238
Alexey Frunze4dda3372015-06-01 18:31:49 -07004239} // namespace mips64
4240} // namespace art