blob: 621cd1ebb8123c8dfe87a8a2720f457859bd62e4 [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 Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
Alexey Frunze4dda3372015-06-01 18:31:49 -070040Location Mips64ReturnLocation(Primitive::Type return_type) {
41 switch (return_type) {
42 case Primitive::kPrimBoolean:
43 case Primitive::kPrimByte:
44 case Primitive::kPrimChar:
45 case Primitive::kPrimShort:
46 case Primitive::kPrimInt:
47 case Primitive::kPrimNot:
48 case Primitive::kPrimLong:
49 return Location::RegisterLocation(V0);
50
51 case Primitive::kPrimFloat:
52 case Primitive::kPrimDouble:
53 return Location::FpuRegisterLocation(F0);
54
55 case Primitive::kPrimVoid:
56 return Location();
57 }
58 UNREACHABLE();
59}
60
61Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
62 return Mips64ReturnLocation(type);
63}
64
65Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
66 return Location::RegisterLocation(kMethodRegisterArgument);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
70 Location next_location;
71 if (type == Primitive::kPrimVoid) {
72 LOG(FATAL) << "Unexpected parameter type " << type;
73 }
74
75 if (Primitive::IsFloatingPointType(type) &&
76 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
77 next_location = Location::FpuRegisterLocation(
78 calling_convention.GetFpuRegisterAt(float_index_++));
79 gp_index_++;
80 } else if (!Primitive::IsFloatingPointType(type) &&
81 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
82 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
83 float_index_++;
84 } else {
85 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
86 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
87 : Location::StackSlot(stack_offset);
88 }
89
90 // Space on the stack is reserved for all arguments.
91 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
92
93 // TODO: review
94
95 // TODO: shouldn't we use a whole machine word per argument on the stack?
96 // Implicit 4-byte method pointer (and such) will cause misalignment.
97
98 return next_location;
99}
100
101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
102 return Mips64ReturnLocation(type);
103}
104
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700105// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
106#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Lazar Trsicd9672662015-09-03 17:33:01 +0200107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700108
109class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
110 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700112
113 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100114 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700115 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
116 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000117 if (instruction_->CanThrowIntoCatchBlock()) {
118 // Live registers will be restored in the catch block if caught.
119 SaveLiveRegisters(codegen, instruction_->GetLocations());
120 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700121 // We're moving two locations to locations that could overlap, so we need a parallel
122 // move resolver.
123 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100124 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
126 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700128 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
129 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100130 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
131 ? QUICK_ENTRY_POINT(pThrowStringBounds)
132 : QUICK_ENTRY_POINT(pThrowArrayBounds);
133 mips64_codegen->InvokeRuntime(entry_point_offset,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700134 instruction_,
135 instruction_->GetDexPc(),
136 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100137 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700138 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
139 }
140
Alexandre Rames8158f282015-08-07 10:26:17 +0100141 bool IsFatal() const OVERRIDE { return true; }
142
Roland Levillain46648892015-06-19 16:07:18 +0100143 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
144
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
173};
174
175class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
176 public:
177 LoadClassSlowPathMIPS64(HLoadClass* cls,
178 HInstruction* at,
179 uint32_t dex_pc,
180 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000181 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700182 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
183 }
184
185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
186 LocationSummary* locations = at_->GetLocations();
187 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
188
189 __ Bind(GetEntryLabel());
190 SaveLiveRegisters(codegen, locations);
191
192 InvokeRuntimeCallingConvention calling_convention;
193 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
194 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
195 : QUICK_ENTRY_POINT(pInitializeType);
196 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
197 if (do_clinit_) {
198 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
199 } else {
200 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
201 }
202
203 // Move the class to the desired location.
204 Location out = locations->Out();
205 if (out.IsValid()) {
206 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
207 Primitive::Type type = at_->GetType();
208 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
209 }
210
211 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700212 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700213 }
214
Roland Levillain46648892015-06-19 16:07:18 +0100215 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
216
Alexey Frunze4dda3372015-06-01 18:31:49 -0700217 private:
218 // The class this slow path will load.
219 HLoadClass* const cls_;
220
221 // The instruction where this slow path is happening.
222 // (Might be the load class or an initialization check).
223 HInstruction* const at_;
224
225 // The dex PC of `at_`.
226 const uint32_t dex_pc_;
227
228 // Whether to initialize the class.
229 const bool do_clinit_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
232};
233
234class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
235 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000236 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700237
238 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
239 LocationSummary* locations = instruction_->GetLocations();
240 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
241 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
242
243 __ Bind(GetEntryLabel());
244 SaveLiveRegisters(codegen, locations);
245
246 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000247 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
248 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700260 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700266 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
267};
268
269class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
275 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000276 if (instruction_->CanThrowIntoCatchBlock()) {
277 // Live registers will be restored in the catch block if caught.
278 SaveLiveRegisters(codegen, instruction_->GetLocations());
279 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700280 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
281 instruction_,
282 instruction_->GetDexPc(),
283 this);
284 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
285 }
286
Alexandre Rames8158f282015-08-07 10:26:17 +0100287 bool IsFatal() const OVERRIDE { return true; }
288
Roland Levillain46648892015-06-19 16:07:18 +0100289 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
290
Alexey Frunze4dda3372015-06-01 18:31:49 -0700291 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700292 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
293};
294
295class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
296 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100297 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000298 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299
300 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
301 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
302 __ Bind(GetEntryLabel());
303 SaveLiveRegisters(codegen, instruction_->GetLocations());
304 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
305 instruction_,
306 instruction_->GetDexPc(),
307 this);
308 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
309 RestoreLiveRegisters(codegen, instruction_->GetLocations());
310 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700311 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700313 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 }
315 }
316
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700317 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700318 DCHECK(successor_ == nullptr);
319 return &return_label_;
320 }
321
Roland Levillain46648892015-06-19 16:07:18 +0100322 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
323
Alexey Frunze4dda3372015-06-01 18:31:49 -0700324 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700325 // If not null, the block to branch to after the suspend check.
326 HBasicBlock* const successor_;
327
328 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700329 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330
331 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
332};
333
334class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
335 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000336 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337
338 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
339 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200340 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342 DCHECK(instruction_->IsCheckCast()
343 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
344 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
345
346 __ Bind(GetEntryLabel());
347 SaveLiveRegisters(codegen, locations);
348
349 // We're moving two locations to locations that could overlap, so we need a parallel
350 // move resolver.
351 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100352 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
354 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100355 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700356 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
357 Primitive::kPrimNot);
358
359 if (instruction_->IsInstanceOf()) {
360 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
361 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100362 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000364 CheckEntrypointTypes<
365 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 Primitive::Type ret_type = instruction_->GetType();
367 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
368 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 } else {
370 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100371 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
373 }
374
375 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700376 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 }
378
Roland Levillain46648892015-06-19 16:07:18 +0100379 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
380
Alexey Frunze4dda3372015-06-01 18:31:49 -0700381 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
383};
384
385class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
386 public:
Aart Bik42249c32016-01-07 15:33:50 -0800387 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800391 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800394 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
395 instruction_,
396 instruction_->GetDexPc(),
397 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000398 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 }
400
Roland Levillain46648892015-06-19 16:07:18 +0100401 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
402
Alexey Frunze4dda3372015-06-01 18:31:49 -0700403 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700404 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
405};
406
407CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
408 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100409 const CompilerOptions& compiler_options,
410 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 : CodeGenerator(graph,
412 kNumberOfGpuRegisters,
413 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000414 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700415 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
416 arraysize(kCoreCalleeSaves)),
417 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
418 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100419 compiler_options,
420 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100421 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700422 location_builder_(graph, this),
423 instruction_visitor_(graph, this),
424 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100425 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700426 isa_features_(isa_features) {
427 // Save RA (containing the return address) to mimic Quick.
428 AddAllocatedRegister(Location::RegisterLocation(RA));
429}
430
431#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700432// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
433#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Lazar Trsicd9672662015-09-03 17:33:01 +0200434#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700435
436void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700437 // Ensure that we fix up branches.
438 __ FinalizeCode();
439
440 // Adjust native pc offsets in stack maps.
441 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
442 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
443 uint32_t new_position = __ GetAdjustedPosition(old_position);
444 DCHECK_GE(new_position, old_position);
445 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
446 }
447
448 // Adjust pc offsets for the disassembly information.
449 if (disasm_info_ != nullptr) {
450 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
451 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
452 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
453 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
454 it.second.start = __ GetAdjustedPosition(it.second.start);
455 it.second.end = __ GetAdjustedPosition(it.second.end);
456 }
457 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
458 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
459 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
460 }
461 }
462
Alexey Frunze4dda3372015-06-01 18:31:49 -0700463 CodeGenerator::Finalize(allocator);
464}
465
466Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
467 return codegen_->GetAssembler();
468}
469
470void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100471 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700472 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
473}
474
475void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100476 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
478}
479
480void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
481 // Pop reg
482 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200483 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700484}
485
486void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
487 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200488 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700489 __ Sd(GpuRegister(reg), SP, 0);
490}
491
492void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
493 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
494 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
495 // Allocate a scratch register other than TMP, if available.
496 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
497 // automatically unspilled when the scratch scope object is destroyed).
498 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
499 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200500 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700501 __ LoadFromOffset(load_type,
502 GpuRegister(ensure_scratch.GetRegister()),
503 SP,
504 index1 + stack_offset);
505 __ LoadFromOffset(load_type,
506 TMP,
507 SP,
508 index2 + stack_offset);
509 __ StoreToOffset(store_type,
510 GpuRegister(ensure_scratch.GetRegister()),
511 SP,
512 index2 + stack_offset);
513 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
514}
515
516static dwarf::Reg DWARFReg(GpuRegister reg) {
517 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
518}
519
David Srbeckyba702002016-02-01 18:15:29 +0000520static dwarf::Reg DWARFReg(FpuRegister reg) {
521 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
522}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700523
524void CodeGeneratorMIPS64::GenerateFrameEntry() {
525 __ Bind(&frame_entry_label_);
526
527 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
528
529 if (do_overflow_check) {
530 __ LoadFromOffset(kLoadWord,
531 ZERO,
532 SP,
533 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
534 RecordPcInfo(nullptr, 0);
535 }
536
537 // TODO: anything related to T9/GP/GOT/PIC/.so's?
538
539 if (HasEmptyFrame()) {
540 return;
541 }
542
543 // Make sure the frame size isn't unreasonably large. Per the various APIs
544 // it looks like it should always be less than 2GB in size, which allows
545 // us using 32-bit signed offsets from the stack pointer.
546 if (GetFrameSize() > 0x7FFFFFFF)
547 LOG(FATAL) << "Stack frame larger than 2GB";
548
549 // Spill callee-saved registers.
550 // Note that their cumulative size is small and they can be indexed using
551 // 16-bit offsets.
552
553 // TODO: increment/decrement SP in one step instead of two or remove this comment.
554
555 uint32_t ofs = FrameEntrySpillSize();
556 __ IncreaseFrameSize(ofs);
557
558 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
559 GpuRegister reg = kCoreCalleeSaves[i];
560 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200561 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700562 __ Sd(reg, SP, ofs);
563 __ cfi().RelOffset(DWARFReg(reg), ofs);
564 }
565 }
566
567 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
568 FpuRegister reg = kFpuCalleeSaves[i];
569 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200570 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000572 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700573 }
574 }
575
576 // Allocate the rest of the frame and store the current method pointer
577 // at its end.
578
579 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
580
581 static_assert(IsInt<16>(kCurrentMethodStackOffset),
582 "kCurrentMethodStackOffset must fit into int16_t");
583 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
584}
585
586void CodeGeneratorMIPS64::GenerateFrameExit() {
587 __ cfi().RememberState();
588
589 // TODO: anything related to T9/GP/GOT/PIC/.so's?
590
591 if (!HasEmptyFrame()) {
592 // Deallocate the rest of the frame.
593
594 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
595
596 // Restore callee-saved registers.
597 // Note that their cumulative size is small and they can be indexed using
598 // 16-bit offsets.
599
600 // TODO: increment/decrement SP in one step instead of two or remove this comment.
601
602 uint32_t ofs = 0;
603
604 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
605 FpuRegister reg = kFpuCalleeSaves[i];
606 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
607 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200608 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000609 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700610 }
611 }
612
613 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
614 GpuRegister reg = kCoreCalleeSaves[i];
615 if (allocated_registers_.ContainsCoreRegister(reg)) {
616 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200617 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700618 __ cfi().Restore(DWARFReg(reg));
619 }
620 }
621
622 DCHECK_EQ(ofs, FrameEntrySpillSize());
623 __ DecreaseFrameSize(ofs);
624 }
625
626 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700627 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700628
629 __ cfi().RestoreState();
630 __ cfi().DefCFAOffset(GetFrameSize());
631}
632
633void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
634 __ Bind(GetLabelOf(block));
635}
636
637void CodeGeneratorMIPS64::MoveLocation(Location destination,
638 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100639 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700640 if (source.Equals(destination)) {
641 return;
642 }
643
644 // A valid move can always be inferred from the destination and source
645 // locations. When moving from and to a register, the argument type can be
646 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100647 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700648 DCHECK_EQ(unspecified_type, false);
649
650 if (destination.IsRegister() || destination.IsFpuRegister()) {
651 if (unspecified_type) {
652 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
653 if (source.IsStackSlot() ||
654 (src_cst != nullptr && (src_cst->IsIntConstant()
655 || src_cst->IsFloatConstant()
656 || src_cst->IsNullConstant()))) {
657 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100658 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 } else {
660 // If the source is a double stack slot or a 64bit constant, a 64bit
661 // type is appropriate. Else the source is a register, and since the
662 // type has not been specified, we chose a 64bit type to force a 64bit
663 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100664 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700665 }
666 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100667 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
668 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
670 // Move to GPR/FPR from stack
671 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100672 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700673 __ LoadFpuFromOffset(load_type,
674 destination.AsFpuRegister<FpuRegister>(),
675 SP,
676 source.GetStackIndex());
677 } else {
678 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
679 __ LoadFromOffset(load_type,
680 destination.AsRegister<GpuRegister>(),
681 SP,
682 source.GetStackIndex());
683 }
684 } else if (source.IsConstant()) {
685 // Move to GPR/FPR from constant
686 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100687 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700688 gpr = destination.AsRegister<GpuRegister>();
689 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100690 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700691 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
692 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
693 gpr = ZERO;
694 } else {
695 __ LoadConst32(gpr, value);
696 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700697 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700698 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
699 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
700 gpr = ZERO;
701 } else {
702 __ LoadConst64(gpr, value);
703 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100705 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700706 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100707 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
709 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100710 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700711 if (destination.IsRegister()) {
712 // Move to GPR from GPR
713 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
714 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100715 DCHECK(destination.IsFpuRegister());
716 if (Primitive::Is64BitType(dst_type)) {
717 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
718 } else {
719 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
720 }
721 }
722 } else if (source.IsFpuRegister()) {
723 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700724 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100725 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700726 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
727 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100728 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700729 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
730 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 } else {
732 DCHECK(destination.IsRegister());
733 if (Primitive::Is64BitType(dst_type)) {
734 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
735 } else {
736 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
737 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700738 }
739 }
740 } else { // The destination is not a register. It must be a stack slot.
741 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
742 if (source.IsRegister() || source.IsFpuRegister()) {
743 if (unspecified_type) {
744 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100745 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700746 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100747 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700748 }
749 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100750 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
751 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 // Move to stack from GPR/FPR
753 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
754 if (source.IsRegister()) {
755 __ StoreToOffset(store_type,
756 source.AsRegister<GpuRegister>(),
757 SP,
758 destination.GetStackIndex());
759 } else {
760 __ StoreFpuToOffset(store_type,
761 source.AsFpuRegister<FpuRegister>(),
762 SP,
763 destination.GetStackIndex());
764 }
765 } else if (source.IsConstant()) {
766 // Move to stack from constant
767 HConstant* src_cst = source.GetConstant();
768 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700769 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700770 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700771 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
772 if (value != 0) {
773 gpr = TMP;
774 __ LoadConst32(gpr, value);
775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 DCHECK(destination.IsDoubleStackSlot());
778 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
779 if (value != 0) {
780 gpr = TMP;
781 __ LoadConst64(gpr, value);
782 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700783 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700784 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700785 } else {
786 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
787 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
788 // Move to stack from stack
789 if (destination.IsStackSlot()) {
790 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
791 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
792 } else {
793 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
794 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
795 }
796 }
797 }
798}
799
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700800void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700801 DCHECK(!loc1.IsConstant());
802 DCHECK(!loc2.IsConstant());
803
804 if (loc1.Equals(loc2)) {
805 return;
806 }
807
808 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
809 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
810 bool is_fp_reg1 = loc1.IsFpuRegister();
811 bool is_fp_reg2 = loc2.IsFpuRegister();
812
813 if (loc2.IsRegister() && loc1.IsRegister()) {
814 // Swap 2 GPRs
815 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
816 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
817 __ Move(TMP, r2);
818 __ Move(r2, r1);
819 __ Move(r1, TMP);
820 } else if (is_fp_reg2 && is_fp_reg1) {
821 // Swap 2 FPRs
822 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
823 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700824 if (type == Primitive::kPrimFloat) {
825 __ MovS(FTMP, r1);
826 __ MovS(r1, r2);
827 __ MovS(r2, FTMP);
828 } else {
829 DCHECK_EQ(type, Primitive::kPrimDouble);
830 __ MovD(FTMP, r1);
831 __ MovD(r1, r2);
832 __ MovD(r2, FTMP);
833 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700834 } else if (is_slot1 != is_slot2) {
835 // Swap GPR/FPR and stack slot
836 Location reg_loc = is_slot1 ? loc2 : loc1;
837 Location mem_loc = is_slot1 ? loc1 : loc2;
838 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
839 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
840 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
841 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
842 if (reg_loc.IsFpuRegister()) {
843 __ StoreFpuToOffset(store_type,
844 reg_loc.AsFpuRegister<FpuRegister>(),
845 SP,
846 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700847 if (mem_loc.IsStackSlot()) {
848 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
849 } else {
850 DCHECK(mem_loc.IsDoubleStackSlot());
851 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
852 }
853 } else {
854 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
855 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
856 }
857 } else if (is_slot1 && is_slot2) {
858 move_resolver_.Exchange(loc1.GetStackIndex(),
859 loc2.GetStackIndex(),
860 loc1.IsDoubleStackSlot());
861 } else {
862 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
863 }
864}
865
Calin Juravle175dc732015-08-25 15:42:32 +0100866void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
867 DCHECK(location.IsRegister());
868 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
869}
870
Calin Juravlee460d1d2015-09-29 04:52:17 +0100871void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
872 if (location.IsRegister()) {
873 locations->AddTemp(location);
874 } else {
875 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
876 }
877}
878
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100879void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
880 GpuRegister value,
881 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700882 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700883 GpuRegister card = AT;
884 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100885 if (value_can_be_null) {
886 __ Beqzc(value, &done);
887 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700888 __ LoadFromOffset(kLoadDoubleword,
889 card,
890 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200891 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700892 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
893 __ Daddu(temp, card, temp);
894 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100895 if (value_can_be_null) {
896 __ Bind(&done);
897 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700898}
899
David Brazdil58282f42016-01-14 12:45:10 +0000900void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700901 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
902 blocked_core_registers_[ZERO] = true;
903 blocked_core_registers_[K0] = true;
904 blocked_core_registers_[K1] = true;
905 blocked_core_registers_[GP] = true;
906 blocked_core_registers_[SP] = true;
907 blocked_core_registers_[RA] = true;
908
Lazar Trsicd9672662015-09-03 17:33:01 +0200909 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
910 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700911 blocked_core_registers_[AT] = true;
912 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200913 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700914 blocked_fpu_registers_[FTMP] = true;
915
916 // Reserve suspend and thread registers.
917 blocked_core_registers_[S0] = true;
918 blocked_core_registers_[TR] = true;
919
920 // Reserve T9 for function calls
921 blocked_core_registers_[T9] = true;
922
923 // TODO: review; anything else?
924
Goran Jakovljevic782be112016-06-21 12:39:04 +0200925 if (GetGraph()->IsDebuggable()) {
926 // Stubs do not save callee-save floating point registers. If the graph
927 // is debuggable, we need to deal with these registers differently. For
928 // now, just block them.
929 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
930 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
931 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932 }
933}
934
Alexey Frunze4dda3372015-06-01 18:31:49 -0700935size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
936 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200937 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700938}
939
940size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
941 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200942 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943}
944
945size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
946 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200947 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700948}
949
950size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
951 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200952 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700953}
954
955void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100956 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700957}
958
959void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100960 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700961}
962
Calin Juravle175dc732015-08-25 15:42:32 +0100963void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
964 HInstruction* instruction,
965 uint32_t dex_pc,
966 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200967 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100968 instruction,
969 dex_pc,
970 slow_path);
971}
972
Alexey Frunze4dda3372015-06-01 18:31:49 -0700973void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
974 HInstruction* instruction,
975 uint32_t dex_pc,
976 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100977 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700978 // TODO: anything related to T9/GP/GOT/PIC/.so's?
979 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
980 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700981 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700982 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700983}
984
985void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
986 GpuRegister class_reg) {
987 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
988 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
989 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
990 // TODO: barrier needed?
991 __ Bind(slow_path->GetExitLabel());
992}
993
994void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
995 __ Sync(0); // only stype 0 is supported
996}
997
998void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
999 HBasicBlock* successor) {
1000 SuspendCheckSlowPathMIPS64* slow_path =
1001 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1002 codegen_->AddSlowPath(slow_path);
1003
1004 __ LoadFromOffset(kLoadUnsignedHalfword,
1005 TMP,
1006 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001007 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001008 if (successor == nullptr) {
1009 __ Bnezc(TMP, slow_path->GetEntryLabel());
1010 __ Bind(slow_path->GetReturnLabel());
1011 } else {
1012 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001013 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014 // slow_path will return to GetLabelOf(successor).
1015 }
1016}
1017
1018InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1019 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001020 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001021 assembler_(codegen->GetAssembler()),
1022 codegen_(codegen) {}
1023
1024void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1025 DCHECK_EQ(instruction->InputCount(), 2U);
1026 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1027 Primitive::Type type = instruction->GetResultType();
1028 switch (type) {
1029 case Primitive::kPrimInt:
1030 case Primitive::kPrimLong: {
1031 locations->SetInAt(0, Location::RequiresRegister());
1032 HInstruction* right = instruction->InputAt(1);
1033 bool can_use_imm = false;
1034 if (right->IsConstant()) {
1035 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1036 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1037 can_use_imm = IsUint<16>(imm);
1038 } else if (instruction->IsAdd()) {
1039 can_use_imm = IsInt<16>(imm);
1040 } else {
1041 DCHECK(instruction->IsSub());
1042 can_use_imm = IsInt<16>(-imm);
1043 }
1044 }
1045 if (can_use_imm)
1046 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1047 else
1048 locations->SetInAt(1, Location::RequiresRegister());
1049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1050 }
1051 break;
1052
1053 case Primitive::kPrimFloat:
1054 case Primitive::kPrimDouble:
1055 locations->SetInAt(0, Location::RequiresFpuRegister());
1056 locations->SetInAt(1, Location::RequiresFpuRegister());
1057 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1058 break;
1059
1060 default:
1061 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1062 }
1063}
1064
1065void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1066 Primitive::Type type = instruction->GetType();
1067 LocationSummary* locations = instruction->GetLocations();
1068
1069 switch (type) {
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimLong: {
1072 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1073 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1074 Location rhs_location = locations->InAt(1);
1075
1076 GpuRegister rhs_reg = ZERO;
1077 int64_t rhs_imm = 0;
1078 bool use_imm = rhs_location.IsConstant();
1079 if (use_imm) {
1080 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1081 } else {
1082 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1083 }
1084
1085 if (instruction->IsAnd()) {
1086 if (use_imm)
1087 __ Andi(dst, lhs, rhs_imm);
1088 else
1089 __ And(dst, lhs, rhs_reg);
1090 } else if (instruction->IsOr()) {
1091 if (use_imm)
1092 __ Ori(dst, lhs, rhs_imm);
1093 else
1094 __ Or(dst, lhs, rhs_reg);
1095 } else if (instruction->IsXor()) {
1096 if (use_imm)
1097 __ Xori(dst, lhs, rhs_imm);
1098 else
1099 __ Xor(dst, lhs, rhs_reg);
1100 } else if (instruction->IsAdd()) {
1101 if (type == Primitive::kPrimInt) {
1102 if (use_imm)
1103 __ Addiu(dst, lhs, rhs_imm);
1104 else
1105 __ Addu(dst, lhs, rhs_reg);
1106 } else {
1107 if (use_imm)
1108 __ Daddiu(dst, lhs, rhs_imm);
1109 else
1110 __ Daddu(dst, lhs, rhs_reg);
1111 }
1112 } else {
1113 DCHECK(instruction->IsSub());
1114 if (type == Primitive::kPrimInt) {
1115 if (use_imm)
1116 __ Addiu(dst, lhs, -rhs_imm);
1117 else
1118 __ Subu(dst, lhs, rhs_reg);
1119 } else {
1120 if (use_imm)
1121 __ Daddiu(dst, lhs, -rhs_imm);
1122 else
1123 __ Dsubu(dst, lhs, rhs_reg);
1124 }
1125 }
1126 break;
1127 }
1128 case Primitive::kPrimFloat:
1129 case Primitive::kPrimDouble: {
1130 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1131 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1132 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1133 if (instruction->IsAdd()) {
1134 if (type == Primitive::kPrimFloat)
1135 __ AddS(dst, lhs, rhs);
1136 else
1137 __ AddD(dst, lhs, rhs);
1138 } else if (instruction->IsSub()) {
1139 if (type == Primitive::kPrimFloat)
1140 __ SubS(dst, lhs, rhs);
1141 else
1142 __ SubD(dst, lhs, rhs);
1143 } else {
1144 LOG(FATAL) << "Unexpected floating-point binary operation";
1145 }
1146 break;
1147 }
1148 default:
1149 LOG(FATAL) << "Unexpected binary operation type " << type;
1150 }
1151}
1152
1153void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001154 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001155
1156 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1157 Primitive::Type type = instr->GetResultType();
1158 switch (type) {
1159 case Primitive::kPrimInt:
1160 case Primitive::kPrimLong: {
1161 locations->SetInAt(0, Location::RequiresRegister());
1162 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001164 break;
1165 }
1166 default:
1167 LOG(FATAL) << "Unexpected shift type " << type;
1168 }
1169}
1170
1171void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001172 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173 LocationSummary* locations = instr->GetLocations();
1174 Primitive::Type type = instr->GetType();
1175
1176 switch (type) {
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimLong: {
1179 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1180 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1181 Location rhs_location = locations->InAt(1);
1182
1183 GpuRegister rhs_reg = ZERO;
1184 int64_t rhs_imm = 0;
1185 bool use_imm = rhs_location.IsConstant();
1186 if (use_imm) {
1187 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1188 } else {
1189 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1190 }
1191
1192 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001193 uint32_t shift_value = rhs_imm &
1194 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195
Alexey Frunze92d90602015-12-18 18:16:36 -08001196 if (shift_value == 0) {
1197 if (dst != lhs) {
1198 __ Move(dst, lhs);
1199 }
1200 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001201 if (instr->IsShl()) {
1202 __ Sll(dst, lhs, shift_value);
1203 } else if (instr->IsShr()) {
1204 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001205 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001206 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001207 } else {
1208 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 }
1210 } else {
1211 if (shift_value < 32) {
1212 if (instr->IsShl()) {
1213 __ Dsll(dst, lhs, shift_value);
1214 } else if (instr->IsShr()) {
1215 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001216 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001218 } else {
1219 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001220 }
1221 } else {
1222 shift_value -= 32;
1223 if (instr->IsShl()) {
1224 __ Dsll32(dst, lhs, shift_value);
1225 } else if (instr->IsShr()) {
1226 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001227 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001229 } else {
1230 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 }
1232 }
1233 }
1234 } else {
1235 if (type == Primitive::kPrimInt) {
1236 if (instr->IsShl()) {
1237 __ Sllv(dst, lhs, rhs_reg);
1238 } else if (instr->IsShr()) {
1239 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001240 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001241 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001242 } else {
1243 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 }
1245 } else {
1246 if (instr->IsShl()) {
1247 __ Dsllv(dst, lhs, rhs_reg);
1248 } else if (instr->IsShr()) {
1249 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001250 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001251 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001252 } else {
1253 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001254 }
1255 }
1256 }
1257 break;
1258 }
1259 default:
1260 LOG(FATAL) << "Unexpected shift operation type " << type;
1261 }
1262}
1263
1264void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1265 HandleBinaryOp(instruction);
1266}
1267
1268void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1269 HandleBinaryOp(instruction);
1270}
1271
1272void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1273 HandleBinaryOp(instruction);
1274}
1275
1276void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1277 HandleBinaryOp(instruction);
1278}
1279
1280void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1281 LocationSummary* locations =
1282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1283 locations->SetInAt(0, Location::RequiresRegister());
1284 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1285 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1286 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1287 } else {
1288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1289 }
1290}
1291
1292void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1293 LocationSummary* locations = instruction->GetLocations();
1294 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1295 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001296 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001298 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001299 switch (type) {
1300 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001301 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1302 if (index.IsConstant()) {
1303 size_t offset =
1304 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1305 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1306 } else {
1307 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1308 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1309 }
1310 break;
1311 }
1312
1313 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001314 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1315 if (index.IsConstant()) {
1316 size_t offset =
1317 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1318 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1319 } else {
1320 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1321 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1322 }
1323 break;
1324 }
1325
1326 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001327 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1328 if (index.IsConstant()) {
1329 size_t offset =
1330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1331 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1332 } else {
1333 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1334 __ Daddu(TMP, obj, TMP);
1335 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1336 }
1337 break;
1338 }
1339
1340 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001341 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1342 if (index.IsConstant()) {
1343 size_t offset =
1344 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1345 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1346 } else {
1347 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1348 __ Daddu(TMP, obj, TMP);
1349 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1350 }
1351 break;
1352 }
1353
1354 case Primitive::kPrimInt:
1355 case Primitive::kPrimNot: {
1356 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001357 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1358 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1359 if (index.IsConstant()) {
1360 size_t offset =
1361 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1362 __ LoadFromOffset(load_type, out, obj, offset);
1363 } else {
1364 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1365 __ Daddu(TMP, obj, TMP);
1366 __ LoadFromOffset(load_type, out, TMP, data_offset);
1367 }
1368 break;
1369 }
1370
1371 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001372 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1373 if (index.IsConstant()) {
1374 size_t offset =
1375 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1376 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1377 } else {
1378 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1379 __ Daddu(TMP, obj, TMP);
1380 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1381 }
1382 break;
1383 }
1384
1385 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001386 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1387 if (index.IsConstant()) {
1388 size_t offset =
1389 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1390 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1391 } else {
1392 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1393 __ Daddu(TMP, obj, TMP);
1394 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1395 }
1396 break;
1397 }
1398
1399 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001400 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1401 if (index.IsConstant()) {
1402 size_t offset =
1403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1404 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1405 } else {
1406 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1407 __ Daddu(TMP, obj, TMP);
1408 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1409 }
1410 break;
1411 }
1412
1413 case Primitive::kPrimVoid:
1414 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1415 UNREACHABLE();
1416 }
1417 codegen_->MaybeRecordImplicitNullCheck(instruction);
1418}
1419
1420void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1422 locations->SetInAt(0, Location::RequiresRegister());
1423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1424}
1425
1426void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1427 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001428 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001429 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1430 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1431 __ LoadFromOffset(kLoadWord, out, obj, offset);
1432 codegen_->MaybeRecordImplicitNullCheck(instruction);
1433}
1434
1435void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001436 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1438 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001439 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001440 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001441 InvokeRuntimeCallingConvention calling_convention;
1442 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1443 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1444 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1445 } else {
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1448 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1449 locations->SetInAt(2, Location::RequiresFpuRegister());
1450 } else {
1451 locations->SetInAt(2, Location::RequiresRegister());
1452 }
1453 }
1454}
1455
1456void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1457 LocationSummary* locations = instruction->GetLocations();
1458 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1459 Location index = locations->InAt(1);
1460 Primitive::Type value_type = instruction->GetComponentType();
1461 bool needs_runtime_call = locations->WillCall();
1462 bool needs_write_barrier =
1463 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1464
1465 switch (value_type) {
1466 case Primitive::kPrimBoolean:
1467 case Primitive::kPrimByte: {
1468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1469 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1470 if (index.IsConstant()) {
1471 size_t offset =
1472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1473 __ StoreToOffset(kStoreByte, value, obj, offset);
1474 } else {
1475 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1476 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1477 }
1478 break;
1479 }
1480
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimChar: {
1483 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1484 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1485 if (index.IsConstant()) {
1486 size_t offset =
1487 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1488 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1489 } else {
1490 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1491 __ Daddu(TMP, obj, TMP);
1492 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1493 }
1494 break;
1495 }
1496
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimNot: {
1499 if (!needs_runtime_call) {
1500 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1501 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1502 if (index.IsConstant()) {
1503 size_t offset =
1504 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1505 __ StoreToOffset(kStoreWord, value, obj, offset);
1506 } else {
1507 DCHECK(index.IsRegister()) << index;
1508 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1509 __ Daddu(TMP, obj, TMP);
1510 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1511 }
1512 codegen_->MaybeRecordImplicitNullCheck(instruction);
1513 if (needs_write_barrier) {
1514 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001515 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001516 }
1517 } else {
1518 DCHECK_EQ(value_type, Primitive::kPrimNot);
1519 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1520 instruction,
1521 instruction->GetDexPc(),
1522 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001523 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001524 }
1525 break;
1526 }
1527
1528 case Primitive::kPrimLong: {
1529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1530 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1531 if (index.IsConstant()) {
1532 size_t offset =
1533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1534 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1535 } else {
1536 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1537 __ Daddu(TMP, obj, TMP);
1538 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1539 }
1540 break;
1541 }
1542
1543 case Primitive::kPrimFloat: {
1544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1545 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1546 DCHECK(locations->InAt(2).IsFpuRegister());
1547 if (index.IsConstant()) {
1548 size_t offset =
1549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1550 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1551 } else {
1552 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1553 __ Daddu(TMP, obj, TMP);
1554 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1555 }
1556 break;
1557 }
1558
1559 case Primitive::kPrimDouble: {
1560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1561 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1562 DCHECK(locations->InAt(2).IsFpuRegister());
1563 if (index.IsConstant()) {
1564 size_t offset =
1565 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1566 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1567 } else {
1568 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1569 __ Daddu(TMP, obj, TMP);
1570 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1571 }
1572 break;
1573 }
1574
1575 case Primitive::kPrimVoid:
1576 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1577 UNREACHABLE();
1578 }
1579
1580 // Ints and objects are handled in the switch.
1581 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1582 codegen_->MaybeRecordImplicitNullCheck(instruction);
1583 }
1584}
1585
1586void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001587 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1588 ? LocationSummary::kCallOnSlowPath
1589 : LocationSummary::kNoCall;
1590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetInAt(1, Location::RequiresRegister());
1593 if (instruction->HasUses()) {
1594 locations->SetOut(Location::SameAsFirstInput());
1595 }
1596}
1597
1598void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1599 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001600 BoundsCheckSlowPathMIPS64* slow_path =
1601 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001602 codegen_->AddSlowPath(slow_path);
1603
1604 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1605 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1606
1607 // length is limited by the maximum positive signed 32-bit integer.
1608 // Unsigned comparison of length and index checks for index < 0
1609 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001610 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611}
1612
1613void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1615 instruction,
1616 LocationSummary::kCallOnSlowPath);
1617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001619 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 locations->AddTemp(Location::RequiresRegister());
1621}
1622
1623void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1624 LocationSummary* locations = instruction->GetLocations();
1625 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1626 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1627 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1628
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001629 SlowPathCodeMIPS64* slow_path =
1630 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001631 codegen_->AddSlowPath(slow_path);
1632
1633 // TODO: avoid this check if we know obj is not null.
1634 __ Beqzc(obj, slow_path->GetExitLabel());
1635 // Compare the class of `obj` with `cls`.
1636 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1637 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1638 __ Bind(slow_path->GetExitLabel());
1639}
1640
1641void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1642 LocationSummary* locations =
1643 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1644 locations->SetInAt(0, Location::RequiresRegister());
1645 if (check->HasUses()) {
1646 locations->SetOut(Location::SameAsFirstInput());
1647 }
1648}
1649
1650void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1651 // We assume the class is not null.
1652 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1653 check->GetLoadClass(),
1654 check,
1655 check->GetDexPc(),
1656 true);
1657 codegen_->AddSlowPath(slow_path);
1658 GenerateClassInitializationCheck(slow_path,
1659 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1660}
1661
1662void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1663 Primitive::Type in_type = compare->InputAt(0)->GetType();
1664
Alexey Frunze299a9392015-12-08 16:08:02 -08001665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001666
1667 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001668 case Primitive::kPrimBoolean:
1669 case Primitive::kPrimByte:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001672 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001673 case Primitive::kPrimLong:
1674 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001675 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1677 break;
1678
1679 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001680 case Primitive::kPrimDouble:
1681 locations->SetInAt(0, Location::RequiresFpuRegister());
1682 locations->SetInAt(1, Location::RequiresFpuRegister());
1683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001684 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685
1686 default:
1687 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1688 }
1689}
1690
1691void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1692 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001693 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001694 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1695
1696 // 0 if: left == right
1697 // 1 if: left > right
1698 // -1 if: left < right
1699 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001700 case Primitive::kPrimBoolean:
1701 case Primitive::kPrimByte:
1702 case Primitive::kPrimShort:
1703 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001704 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001707 Location rhs_location = locations->InAt(1);
1708 bool use_imm = rhs_location.IsConstant();
1709 GpuRegister rhs = ZERO;
1710 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001711 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001712 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1713 if (value != 0) {
1714 rhs = AT;
1715 __ LoadConst64(rhs, value);
1716 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001717 } else {
1718 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1719 if (value != 0) {
1720 rhs = AT;
1721 __ LoadConst32(rhs, value);
1722 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001723 }
1724 } else {
1725 rhs = rhs_location.AsRegister<GpuRegister>();
1726 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001728 __ Slt(res, rhs, lhs);
1729 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 break;
1731 }
1732
Alexey Frunze299a9392015-12-08 16:08:02 -08001733 case Primitive::kPrimFloat: {
1734 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1735 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1736 Mips64Label done;
1737 __ CmpEqS(FTMP, lhs, rhs);
1738 __ LoadConst32(res, 0);
1739 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001740 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001741 __ CmpLtS(FTMP, lhs, rhs);
1742 __ LoadConst32(res, -1);
1743 __ Bc1nez(FTMP, &done);
1744 __ LoadConst32(res, 1);
1745 } else {
1746 __ CmpLtS(FTMP, rhs, lhs);
1747 __ LoadConst32(res, 1);
1748 __ Bc1nez(FTMP, &done);
1749 __ LoadConst32(res, -1);
1750 }
1751 __ Bind(&done);
1752 break;
1753 }
1754
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001756 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1757 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1758 Mips64Label done;
1759 __ CmpEqD(FTMP, lhs, rhs);
1760 __ LoadConst32(res, 0);
1761 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001762 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001763 __ CmpLtD(FTMP, lhs, rhs);
1764 __ LoadConst32(res, -1);
1765 __ Bc1nez(FTMP, &done);
1766 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001768 __ CmpLtD(FTMP, rhs, lhs);
1769 __ LoadConst32(res, 1);
1770 __ Bc1nez(FTMP, &done);
1771 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001773 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001774 break;
1775 }
1776
1777 default:
1778 LOG(FATAL) << "Unimplemented compare type " << in_type;
1779 }
1780}
1781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001784 switch (instruction->InputAt(0)->GetType()) {
1785 default:
1786 case Primitive::kPrimLong:
1787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1789 break;
1790
1791 case Primitive::kPrimFloat:
1792 case Primitive::kPrimDouble:
1793 locations->SetInAt(0, Location::RequiresFpuRegister());
1794 locations->SetInAt(1, Location::RequiresFpuRegister());
1795 break;
1796 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001797 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1799 }
1800}
1801
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001803 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804 return;
1805 }
1806
Alexey Frunze299a9392015-12-08 16:08:02 -08001807 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001810 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811
Alexey Frunze299a9392015-12-08 16:08:02 -08001812 switch (type) {
1813 default:
1814 // Integer case.
1815 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1816 return;
1817 case Primitive::kPrimLong:
1818 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1819 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820
Alexey Frunze299a9392015-12-08 16:08:02 -08001821 case Primitive::kPrimFloat:
1822 case Primitive::kPrimDouble:
1823 // TODO: don't use branches.
1824 GenerateFpCompareAndBranch(instruction->GetCondition(),
1825 instruction->IsGtBias(),
1826 type,
1827 locations,
1828 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001829 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001831
1832 // Convert the branches into the result.
1833 Mips64Label done;
1834
1835 // False case: result = 0.
1836 __ LoadConst32(dst, 0);
1837 __ Bc(&done);
1838
1839 // True case: result = 1.
1840 __ Bind(&true_label);
1841 __ LoadConst32(dst, 1);
1842 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001843}
1844
Alexey Frunzec857c742015-09-23 15:12:39 -07001845void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1846 DCHECK(instruction->IsDiv() || instruction->IsRem());
1847 Primitive::Type type = instruction->GetResultType();
1848
1849 LocationSummary* locations = instruction->GetLocations();
1850 Location second = locations->InAt(1);
1851 DCHECK(second.IsConstant());
1852
1853 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1854 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1855 int64_t imm = Int64FromConstant(second.GetConstant());
1856 DCHECK(imm == 1 || imm == -1);
1857
1858 if (instruction->IsRem()) {
1859 __ Move(out, ZERO);
1860 } else {
1861 if (imm == -1) {
1862 if (type == Primitive::kPrimInt) {
1863 __ Subu(out, ZERO, dividend);
1864 } else {
1865 DCHECK_EQ(type, Primitive::kPrimLong);
1866 __ Dsubu(out, ZERO, dividend);
1867 }
1868 } else if (out != dividend) {
1869 __ Move(out, dividend);
1870 }
1871 }
1872}
1873
1874void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1875 DCHECK(instruction->IsDiv() || instruction->IsRem());
1876 Primitive::Type type = instruction->GetResultType();
1877
1878 LocationSummary* locations = instruction->GetLocations();
1879 Location second = locations->InAt(1);
1880 DCHECK(second.IsConstant());
1881
1882 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1883 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1884 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001885 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001886 int ctz_imm = CTZ(abs_imm);
1887
1888 if (instruction->IsDiv()) {
1889 if (type == Primitive::kPrimInt) {
1890 if (ctz_imm == 1) {
1891 // Fast path for division by +/-2, which is very common.
1892 __ Srl(TMP, dividend, 31);
1893 } else {
1894 __ Sra(TMP, dividend, 31);
1895 __ Srl(TMP, TMP, 32 - ctz_imm);
1896 }
1897 __ Addu(out, dividend, TMP);
1898 __ Sra(out, out, ctz_imm);
1899 if (imm < 0) {
1900 __ Subu(out, ZERO, out);
1901 }
1902 } else {
1903 DCHECK_EQ(type, Primitive::kPrimLong);
1904 if (ctz_imm == 1) {
1905 // Fast path for division by +/-2, which is very common.
1906 __ Dsrl32(TMP, dividend, 31);
1907 } else {
1908 __ Dsra32(TMP, dividend, 31);
1909 if (ctz_imm > 32) {
1910 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1911 } else {
1912 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1913 }
1914 }
1915 __ Daddu(out, dividend, TMP);
1916 if (ctz_imm < 32) {
1917 __ Dsra(out, out, ctz_imm);
1918 } else {
1919 __ Dsra32(out, out, ctz_imm - 32);
1920 }
1921 if (imm < 0) {
1922 __ Dsubu(out, ZERO, out);
1923 }
1924 }
1925 } else {
1926 if (type == Primitive::kPrimInt) {
1927 if (ctz_imm == 1) {
1928 // Fast path for modulo +/-2, which is very common.
1929 __ Sra(TMP, dividend, 31);
1930 __ Subu(out, dividend, TMP);
1931 __ Andi(out, out, 1);
1932 __ Addu(out, out, TMP);
1933 } else {
1934 __ Sra(TMP, dividend, 31);
1935 __ Srl(TMP, TMP, 32 - ctz_imm);
1936 __ Addu(out, dividend, TMP);
1937 if (IsUint<16>(abs_imm - 1)) {
1938 __ Andi(out, out, abs_imm - 1);
1939 } else {
1940 __ Sll(out, out, 32 - ctz_imm);
1941 __ Srl(out, out, 32 - ctz_imm);
1942 }
1943 __ Subu(out, out, TMP);
1944 }
1945 } else {
1946 DCHECK_EQ(type, Primitive::kPrimLong);
1947 if (ctz_imm == 1) {
1948 // Fast path for modulo +/-2, which is very common.
1949 __ Dsra32(TMP, dividend, 31);
1950 __ Dsubu(out, dividend, TMP);
1951 __ Andi(out, out, 1);
1952 __ Daddu(out, out, TMP);
1953 } else {
1954 __ Dsra32(TMP, dividend, 31);
1955 if (ctz_imm > 32) {
1956 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1957 } else {
1958 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1959 }
1960 __ Daddu(out, dividend, TMP);
1961 if (IsUint<16>(abs_imm - 1)) {
1962 __ Andi(out, out, abs_imm - 1);
1963 } else {
1964 if (ctz_imm > 32) {
1965 __ Dsll(out, out, 64 - ctz_imm);
1966 __ Dsrl(out, out, 64 - ctz_imm);
1967 } else {
1968 __ Dsll32(out, out, 32 - ctz_imm);
1969 __ Dsrl32(out, out, 32 - ctz_imm);
1970 }
1971 }
1972 __ Dsubu(out, out, TMP);
1973 }
1974 }
1975 }
1976}
1977
1978void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1979 DCHECK(instruction->IsDiv() || instruction->IsRem());
1980
1981 LocationSummary* locations = instruction->GetLocations();
1982 Location second = locations->InAt(1);
1983 DCHECK(second.IsConstant());
1984
1985 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1986 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1987 int64_t imm = Int64FromConstant(second.GetConstant());
1988
1989 Primitive::Type type = instruction->GetResultType();
1990 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1991
1992 int64_t magic;
1993 int shift;
1994 CalculateMagicAndShiftForDivRem(imm,
1995 (type == Primitive::kPrimLong),
1996 &magic,
1997 &shift);
1998
1999 if (type == Primitive::kPrimInt) {
2000 __ LoadConst32(TMP, magic);
2001 __ MuhR6(TMP, dividend, TMP);
2002
2003 if (imm > 0 && magic < 0) {
2004 __ Addu(TMP, TMP, dividend);
2005 } else if (imm < 0 && magic > 0) {
2006 __ Subu(TMP, TMP, dividend);
2007 }
2008
2009 if (shift != 0) {
2010 __ Sra(TMP, TMP, shift);
2011 }
2012
2013 if (instruction->IsDiv()) {
2014 __ Sra(out, TMP, 31);
2015 __ Subu(out, TMP, out);
2016 } else {
2017 __ Sra(AT, TMP, 31);
2018 __ Subu(AT, TMP, AT);
2019 __ LoadConst32(TMP, imm);
2020 __ MulR6(TMP, AT, TMP);
2021 __ Subu(out, dividend, TMP);
2022 }
2023 } else {
2024 __ LoadConst64(TMP, magic);
2025 __ Dmuh(TMP, dividend, TMP);
2026
2027 if (imm > 0 && magic < 0) {
2028 __ Daddu(TMP, TMP, dividend);
2029 } else if (imm < 0 && magic > 0) {
2030 __ Dsubu(TMP, TMP, dividend);
2031 }
2032
2033 if (shift >= 32) {
2034 __ Dsra32(TMP, TMP, shift - 32);
2035 } else if (shift > 0) {
2036 __ Dsra(TMP, TMP, shift);
2037 }
2038
2039 if (instruction->IsDiv()) {
2040 __ Dsra32(out, TMP, 31);
2041 __ Dsubu(out, TMP, out);
2042 } else {
2043 __ Dsra32(AT, TMP, 31);
2044 __ Dsubu(AT, TMP, AT);
2045 __ LoadConst64(TMP, imm);
2046 __ Dmul(TMP, AT, TMP);
2047 __ Dsubu(out, dividend, TMP);
2048 }
2049 }
2050}
2051
2052void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2053 DCHECK(instruction->IsDiv() || instruction->IsRem());
2054 Primitive::Type type = instruction->GetResultType();
2055 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2056
2057 LocationSummary* locations = instruction->GetLocations();
2058 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2059 Location second = locations->InAt(1);
2060
2061 if (second.IsConstant()) {
2062 int64_t imm = Int64FromConstant(second.GetConstant());
2063 if (imm == 0) {
2064 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2065 } else if (imm == 1 || imm == -1) {
2066 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002067 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002068 DivRemByPowerOfTwo(instruction);
2069 } else {
2070 DCHECK(imm <= -2 || imm >= 2);
2071 GenerateDivRemWithAnyConstant(instruction);
2072 }
2073 } else {
2074 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2075 GpuRegister divisor = second.AsRegister<GpuRegister>();
2076 if (instruction->IsDiv()) {
2077 if (type == Primitive::kPrimInt)
2078 __ DivR6(out, dividend, divisor);
2079 else
2080 __ Ddiv(out, dividend, divisor);
2081 } else {
2082 if (type == Primitive::kPrimInt)
2083 __ ModR6(out, dividend, divisor);
2084 else
2085 __ Dmod(out, dividend, divisor);
2086 }
2087 }
2088}
2089
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2093 switch (div->GetResultType()) {
2094 case Primitive::kPrimInt:
2095 case Primitive::kPrimLong:
2096 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002097 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2099 break;
2100
2101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble:
2103 locations->SetInAt(0, Location::RequiresFpuRegister());
2104 locations->SetInAt(1, Location::RequiresFpuRegister());
2105 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2106 break;
2107
2108 default:
2109 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2110 }
2111}
2112
2113void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2114 Primitive::Type type = instruction->GetType();
2115 LocationSummary* locations = instruction->GetLocations();
2116
2117 switch (type) {
2118 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002119 case Primitive::kPrimLong:
2120 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble: {
2124 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2125 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2126 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2127 if (type == Primitive::kPrimFloat)
2128 __ DivS(dst, lhs, rhs);
2129 else
2130 __ DivD(dst, lhs, rhs);
2131 break;
2132 }
2133 default:
2134 LOG(FATAL) << "Unexpected div type " << type;
2135 }
2136}
2137
2138void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002139 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2140 ? LocationSummary::kCallOnSlowPath
2141 : LocationSummary::kNoCall;
2142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002143 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2144 if (instruction->HasUses()) {
2145 locations->SetOut(Location::SameAsFirstInput());
2146 }
2147}
2148
2149void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2150 SlowPathCodeMIPS64* slow_path =
2151 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2152 codegen_->AddSlowPath(slow_path);
2153 Location value = instruction->GetLocations()->InAt(0);
2154
2155 Primitive::Type type = instruction->GetType();
2156
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002157 if (!Primitive::IsIntegralType(type)) {
2158 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002159 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002160 }
2161
2162 if (value.IsConstant()) {
2163 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2164 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002165 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 } else {
2167 // A division by a non-null constant is valid. We don't need to perform
2168 // any check, so simply fall through.
2169 }
2170 } else {
2171 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2172 }
2173}
2174
2175void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2178 locations->SetOut(Location::ConstantLocation(constant));
2179}
2180
2181void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2182 // Will be generated at use site.
2183}
2184
2185void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2186 exit->SetLocations(nullptr);
2187}
2188
2189void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2190}
2191
2192void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2195 locations->SetOut(Location::ConstantLocation(constant));
2196}
2197
2198void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2199 // Will be generated at use site.
2200}
2201
David Brazdilfc6a86a2015-06-26 10:33:45 +00002202void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 DCHECK(!successor->IsExitBlock());
2204 HBasicBlock* block = got->GetBlock();
2205 HInstruction* previous = got->GetPrevious();
2206 HLoopInformation* info = block->GetLoopInformation();
2207
2208 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2209 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2210 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2211 return;
2212 }
2213 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2214 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2215 }
2216 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002217 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002218 }
2219}
2220
David Brazdilfc6a86a2015-06-26 10:33:45 +00002221void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2222 got->SetLocations(nullptr);
2223}
2224
2225void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2226 HandleGoto(got, got->GetSuccessor());
2227}
2228
2229void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2230 try_boundary->SetLocations(nullptr);
2231}
2232
2233void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2234 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2235 if (!successor->IsExitBlock()) {
2236 HandleGoto(try_boundary, successor);
2237 }
2238}
2239
Alexey Frunze299a9392015-12-08 16:08:02 -08002240void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2241 bool is64bit,
2242 LocationSummary* locations) {
2243 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2244 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2245 Location rhs_location = locations->InAt(1);
2246 GpuRegister rhs_reg = ZERO;
2247 int64_t rhs_imm = 0;
2248 bool use_imm = rhs_location.IsConstant();
2249 if (use_imm) {
2250 if (is64bit) {
2251 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2252 } else {
2253 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2254 }
2255 } else {
2256 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2257 }
2258 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2259
2260 switch (cond) {
2261 case kCondEQ:
2262 case kCondNE:
2263 if (use_imm && IsUint<16>(rhs_imm)) {
2264 __ Xori(dst, lhs, rhs_imm);
2265 } else {
2266 if (use_imm) {
2267 rhs_reg = TMP;
2268 __ LoadConst64(rhs_reg, rhs_imm);
2269 }
2270 __ Xor(dst, lhs, rhs_reg);
2271 }
2272 if (cond == kCondEQ) {
2273 __ Sltiu(dst, dst, 1);
2274 } else {
2275 __ Sltu(dst, ZERO, dst);
2276 }
2277 break;
2278
2279 case kCondLT:
2280 case kCondGE:
2281 if (use_imm && IsInt<16>(rhs_imm)) {
2282 __ Slti(dst, lhs, rhs_imm);
2283 } else {
2284 if (use_imm) {
2285 rhs_reg = TMP;
2286 __ LoadConst64(rhs_reg, rhs_imm);
2287 }
2288 __ Slt(dst, lhs, rhs_reg);
2289 }
2290 if (cond == kCondGE) {
2291 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2292 // only the slt instruction but no sge.
2293 __ Xori(dst, dst, 1);
2294 }
2295 break;
2296
2297 case kCondLE:
2298 case kCondGT:
2299 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2300 // Simulate lhs <= rhs via lhs < rhs + 1.
2301 __ Slti(dst, lhs, rhs_imm_plus_one);
2302 if (cond == kCondGT) {
2303 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2304 // only the slti instruction but no sgti.
2305 __ Xori(dst, dst, 1);
2306 }
2307 } else {
2308 if (use_imm) {
2309 rhs_reg = TMP;
2310 __ LoadConst64(rhs_reg, rhs_imm);
2311 }
2312 __ Slt(dst, rhs_reg, lhs);
2313 if (cond == kCondLE) {
2314 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2315 // only the slt instruction but no sle.
2316 __ Xori(dst, dst, 1);
2317 }
2318 }
2319 break;
2320
2321 case kCondB:
2322 case kCondAE:
2323 if (use_imm && IsInt<16>(rhs_imm)) {
2324 // Sltiu sign-extends its 16-bit immediate operand before
2325 // the comparison and thus lets us compare directly with
2326 // unsigned values in the ranges [0, 0x7fff] and
2327 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2328 __ Sltiu(dst, lhs, rhs_imm);
2329 } else {
2330 if (use_imm) {
2331 rhs_reg = TMP;
2332 __ LoadConst64(rhs_reg, rhs_imm);
2333 }
2334 __ Sltu(dst, lhs, rhs_reg);
2335 }
2336 if (cond == kCondAE) {
2337 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2338 // only the sltu instruction but no sgeu.
2339 __ Xori(dst, dst, 1);
2340 }
2341 break;
2342
2343 case kCondBE:
2344 case kCondA:
2345 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2346 // Simulate lhs <= rhs via lhs < rhs + 1.
2347 // Note that this only works if rhs + 1 does not overflow
2348 // to 0, hence the check above.
2349 // Sltiu sign-extends its 16-bit immediate operand before
2350 // the comparison and thus lets us compare directly with
2351 // unsigned values in the ranges [0, 0x7fff] and
2352 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2353 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2354 if (cond == kCondA) {
2355 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2356 // only the sltiu instruction but no sgtiu.
2357 __ Xori(dst, dst, 1);
2358 }
2359 } else {
2360 if (use_imm) {
2361 rhs_reg = TMP;
2362 __ LoadConst64(rhs_reg, rhs_imm);
2363 }
2364 __ Sltu(dst, rhs_reg, lhs);
2365 if (cond == kCondBE) {
2366 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2367 // only the sltu instruction but no sleu.
2368 __ Xori(dst, dst, 1);
2369 }
2370 }
2371 break;
2372 }
2373}
2374
2375void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2376 bool is64bit,
2377 LocationSummary* locations,
2378 Mips64Label* label) {
2379 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2380 Location rhs_location = locations->InAt(1);
2381 GpuRegister rhs_reg = ZERO;
2382 int64_t rhs_imm = 0;
2383 bool use_imm = rhs_location.IsConstant();
2384 if (use_imm) {
2385 if (is64bit) {
2386 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2387 } else {
2388 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2389 }
2390 } else {
2391 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2392 }
2393
2394 if (use_imm && rhs_imm == 0) {
2395 switch (cond) {
2396 case kCondEQ:
2397 case kCondBE: // <= 0 if zero
2398 __ Beqzc(lhs, label);
2399 break;
2400 case kCondNE:
2401 case kCondA: // > 0 if non-zero
2402 __ Bnezc(lhs, label);
2403 break;
2404 case kCondLT:
2405 __ Bltzc(lhs, label);
2406 break;
2407 case kCondGE:
2408 __ Bgezc(lhs, label);
2409 break;
2410 case kCondLE:
2411 __ Blezc(lhs, label);
2412 break;
2413 case kCondGT:
2414 __ Bgtzc(lhs, label);
2415 break;
2416 case kCondB: // always false
2417 break;
2418 case kCondAE: // always true
2419 __ Bc(label);
2420 break;
2421 }
2422 } else {
2423 if (use_imm) {
2424 rhs_reg = TMP;
2425 __ LoadConst64(rhs_reg, rhs_imm);
2426 }
2427 switch (cond) {
2428 case kCondEQ:
2429 __ Beqc(lhs, rhs_reg, label);
2430 break;
2431 case kCondNE:
2432 __ Bnec(lhs, rhs_reg, label);
2433 break;
2434 case kCondLT:
2435 __ Bltc(lhs, rhs_reg, label);
2436 break;
2437 case kCondGE:
2438 __ Bgec(lhs, rhs_reg, label);
2439 break;
2440 case kCondLE:
2441 __ Bgec(rhs_reg, lhs, label);
2442 break;
2443 case kCondGT:
2444 __ Bltc(rhs_reg, lhs, label);
2445 break;
2446 case kCondB:
2447 __ Bltuc(lhs, rhs_reg, label);
2448 break;
2449 case kCondAE:
2450 __ Bgeuc(lhs, rhs_reg, label);
2451 break;
2452 case kCondBE:
2453 __ Bgeuc(rhs_reg, lhs, label);
2454 break;
2455 case kCondA:
2456 __ Bltuc(rhs_reg, lhs, label);
2457 break;
2458 }
2459 }
2460}
2461
2462void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2463 bool gt_bias,
2464 Primitive::Type type,
2465 LocationSummary* locations,
2466 Mips64Label* label) {
2467 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2468 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2469 if (type == Primitive::kPrimFloat) {
2470 switch (cond) {
2471 case kCondEQ:
2472 __ CmpEqS(FTMP, lhs, rhs);
2473 __ Bc1nez(FTMP, label);
2474 break;
2475 case kCondNE:
2476 __ CmpEqS(FTMP, lhs, rhs);
2477 __ Bc1eqz(FTMP, label);
2478 break;
2479 case kCondLT:
2480 if (gt_bias) {
2481 __ CmpLtS(FTMP, lhs, rhs);
2482 } else {
2483 __ CmpUltS(FTMP, lhs, rhs);
2484 }
2485 __ Bc1nez(FTMP, label);
2486 break;
2487 case kCondLE:
2488 if (gt_bias) {
2489 __ CmpLeS(FTMP, lhs, rhs);
2490 } else {
2491 __ CmpUleS(FTMP, lhs, rhs);
2492 }
2493 __ Bc1nez(FTMP, label);
2494 break;
2495 case kCondGT:
2496 if (gt_bias) {
2497 __ CmpUltS(FTMP, rhs, lhs);
2498 } else {
2499 __ CmpLtS(FTMP, rhs, lhs);
2500 }
2501 __ Bc1nez(FTMP, label);
2502 break;
2503 case kCondGE:
2504 if (gt_bias) {
2505 __ CmpUleS(FTMP, rhs, lhs);
2506 } else {
2507 __ CmpLeS(FTMP, rhs, lhs);
2508 }
2509 __ Bc1nez(FTMP, label);
2510 break;
2511 default:
2512 LOG(FATAL) << "Unexpected non-floating-point condition";
2513 }
2514 } else {
2515 DCHECK_EQ(type, Primitive::kPrimDouble);
2516 switch (cond) {
2517 case kCondEQ:
2518 __ CmpEqD(FTMP, lhs, rhs);
2519 __ Bc1nez(FTMP, label);
2520 break;
2521 case kCondNE:
2522 __ CmpEqD(FTMP, lhs, rhs);
2523 __ Bc1eqz(FTMP, label);
2524 break;
2525 case kCondLT:
2526 if (gt_bias) {
2527 __ CmpLtD(FTMP, lhs, rhs);
2528 } else {
2529 __ CmpUltD(FTMP, lhs, rhs);
2530 }
2531 __ Bc1nez(FTMP, label);
2532 break;
2533 case kCondLE:
2534 if (gt_bias) {
2535 __ CmpLeD(FTMP, lhs, rhs);
2536 } else {
2537 __ CmpUleD(FTMP, lhs, rhs);
2538 }
2539 __ Bc1nez(FTMP, label);
2540 break;
2541 case kCondGT:
2542 if (gt_bias) {
2543 __ CmpUltD(FTMP, rhs, lhs);
2544 } else {
2545 __ CmpLtD(FTMP, rhs, lhs);
2546 }
2547 __ Bc1nez(FTMP, label);
2548 break;
2549 case kCondGE:
2550 if (gt_bias) {
2551 __ CmpUleD(FTMP, rhs, lhs);
2552 } else {
2553 __ CmpLeD(FTMP, rhs, lhs);
2554 }
2555 __ Bc1nez(FTMP, label);
2556 break;
2557 default:
2558 LOG(FATAL) << "Unexpected non-floating-point condition";
2559 }
2560 }
2561}
2562
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002564 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002565 Mips64Label* true_target,
2566 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002567 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002568
David Brazdil0debae72015-11-12 18:37:00 +00002569 if (true_target == nullptr && false_target == nullptr) {
2570 // Nothing to do. The code always falls through.
2571 return;
2572 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002573 // Constant condition, statically compared against "true" (integer value 1).
2574 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002575 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002576 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002577 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002579 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002580 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002581 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002582 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002583 }
David Brazdil0debae72015-11-12 18:37:00 +00002584 return;
2585 }
2586
2587 // The following code generates these patterns:
2588 // (1) true_target == nullptr && false_target != nullptr
2589 // - opposite condition true => branch to false_target
2590 // (2) true_target != nullptr && false_target == nullptr
2591 // - condition true => branch to true_target
2592 // (3) true_target != nullptr && false_target != nullptr
2593 // - condition true => branch to true_target
2594 // - branch to false_target
2595 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002597 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002598 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002599 if (true_target == nullptr) {
2600 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2601 } else {
2602 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2603 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002604 } else {
2605 // The condition instruction has not been materialized, use its inputs as
2606 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002607 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002608 Primitive::Type type = condition->InputAt(0)->GetType();
2609 LocationSummary* locations = cond->GetLocations();
2610 IfCondition if_cond = condition->GetCondition();
2611 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002612
David Brazdil0debae72015-11-12 18:37:00 +00002613 if (true_target == nullptr) {
2614 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002615 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002616 }
2617
Alexey Frunze299a9392015-12-08 16:08:02 -08002618 switch (type) {
2619 default:
2620 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2621 break;
2622 case Primitive::kPrimLong:
2623 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2624 break;
2625 case Primitive::kPrimFloat:
2626 case Primitive::kPrimDouble:
2627 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2628 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629 }
2630 }
David Brazdil0debae72015-11-12 18:37:00 +00002631
2632 // If neither branch falls through (case 3), the conditional branch to `true_target`
2633 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2634 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002635 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002636 }
2637}
2638
2639void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002641 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642 locations->SetInAt(0, Location::RequiresRegister());
2643 }
2644}
2645
2646void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002647 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2648 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002649 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002650 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002651 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002652 nullptr : codegen_->GetLabelOf(false_successor);
2653 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654}
2655
2656void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2657 LocationSummary* locations = new (GetGraph()->GetArena())
2658 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002659 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 locations->SetInAt(0, Location::RequiresRegister());
2661 }
2662}
2663
2664void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002665 SlowPathCodeMIPS64* slow_path =
2666 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002667 GenerateTestAndBranch(deoptimize,
2668 /* condition_input_index */ 0,
2669 slow_path->GetEntryLabel(),
2670 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671}
2672
David Brazdil74eb1b22015-12-14 11:44:01 +00002673void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2675 if (Primitive::IsFloatingPointType(select->GetType())) {
2676 locations->SetInAt(0, Location::RequiresFpuRegister());
2677 locations->SetInAt(1, Location::RequiresFpuRegister());
2678 } else {
2679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetInAt(1, Location::RequiresRegister());
2681 }
2682 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2683 locations->SetInAt(2, Location::RequiresRegister());
2684 }
2685 locations->SetOut(Location::SameAsFirstInput());
2686}
2687
2688void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2689 LocationSummary* locations = select->GetLocations();
2690 Mips64Label false_target;
2691 GenerateTestAndBranch(select,
2692 /* condition_input_index */ 2,
2693 /* true_target */ nullptr,
2694 &false_target);
2695 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2696 __ Bind(&false_target);
2697}
2698
David Srbecky0cf44932015-12-09 14:09:59 +00002699void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2700 new (GetGraph()->GetArena()) LocationSummary(info);
2701}
2702
David Srbeckyd28f4a02016-03-14 17:14:24 +00002703void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2704 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002705}
2706
2707void CodeGeneratorMIPS64::GenerateNop() {
2708 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002709}
2710
Alexey Frunze4dda3372015-06-01 18:31:49 -07002711void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2712 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2715 locations->SetInAt(0, Location::RequiresRegister());
2716 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2717 locations->SetOut(Location::RequiresFpuRegister());
2718 } else {
2719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2720 }
2721}
2722
2723void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2724 const FieldInfo& field_info) {
2725 Primitive::Type type = field_info.GetFieldType();
2726 LocationSummary* locations = instruction->GetLocations();
2727 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2728 LoadOperandType load_type = kLoadUnsignedByte;
2729 switch (type) {
2730 case Primitive::kPrimBoolean:
2731 load_type = kLoadUnsignedByte;
2732 break;
2733 case Primitive::kPrimByte:
2734 load_type = kLoadSignedByte;
2735 break;
2736 case Primitive::kPrimShort:
2737 load_type = kLoadSignedHalfword;
2738 break;
2739 case Primitive::kPrimChar:
2740 load_type = kLoadUnsignedHalfword;
2741 break;
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimFloat:
2744 load_type = kLoadWord;
2745 break;
2746 case Primitive::kPrimLong:
2747 case Primitive::kPrimDouble:
2748 load_type = kLoadDoubleword;
2749 break;
2750 case Primitive::kPrimNot:
2751 load_type = kLoadUnsignedWord;
2752 break;
2753 case Primitive::kPrimVoid:
2754 LOG(FATAL) << "Unreachable type " << type;
2755 UNREACHABLE();
2756 }
2757 if (!Primitive::IsFloatingPointType(type)) {
2758 DCHECK(locations->Out().IsRegister());
2759 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2760 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2761 } else {
2762 DCHECK(locations->Out().IsFpuRegister());
2763 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2764 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2765 }
2766
2767 codegen_->MaybeRecordImplicitNullCheck(instruction);
2768 // TODO: memory barrier?
2769}
2770
2771void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2772 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2773 LocationSummary* locations =
2774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2777 locations->SetInAt(1, Location::RequiresFpuRegister());
2778 } else {
2779 locations->SetInAt(1, Location::RequiresRegister());
2780 }
2781}
2782
2783void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002784 const FieldInfo& field_info,
2785 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002786 Primitive::Type type = field_info.GetFieldType();
2787 LocationSummary* locations = instruction->GetLocations();
2788 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2789 StoreOperandType store_type = kStoreByte;
2790 switch (type) {
2791 case Primitive::kPrimBoolean:
2792 case Primitive::kPrimByte:
2793 store_type = kStoreByte;
2794 break;
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimChar:
2797 store_type = kStoreHalfword;
2798 break;
2799 case Primitive::kPrimInt:
2800 case Primitive::kPrimFloat:
2801 case Primitive::kPrimNot:
2802 store_type = kStoreWord;
2803 break;
2804 case Primitive::kPrimLong:
2805 case Primitive::kPrimDouble:
2806 store_type = kStoreDoubleword;
2807 break;
2808 case Primitive::kPrimVoid:
2809 LOG(FATAL) << "Unreachable type " << type;
2810 UNREACHABLE();
2811 }
2812 if (!Primitive::IsFloatingPointType(type)) {
2813 DCHECK(locations->InAt(1).IsRegister());
2814 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2815 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2816 } else {
2817 DCHECK(locations->InAt(1).IsFpuRegister());
2818 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2819 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2820 }
2821
2822 codegen_->MaybeRecordImplicitNullCheck(instruction);
2823 // TODO: memory barriers?
2824 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2825 DCHECK(locations->InAt(1).IsRegister());
2826 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002827 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002828 }
2829}
2830
2831void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2832 HandleFieldGet(instruction, instruction->GetFieldInfo());
2833}
2834
2835void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2836 HandleFieldGet(instruction, instruction->GetFieldInfo());
2837}
2838
2839void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2840 HandleFieldSet(instruction, instruction->GetFieldInfo());
2841}
2842
2843void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002844 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845}
2846
2847void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2848 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002849 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::RequiresRegister());
2853 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002854 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002855 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2856}
2857
2858void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2859 LocationSummary* locations = instruction->GetLocations();
2860 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2861 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2862 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2863
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002864 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002865
2866 // Return 0 if `obj` is null.
2867 // TODO: Avoid this check if we know `obj` is not null.
2868 __ Move(out, ZERO);
2869 __ Beqzc(obj, &done);
2870
2871 // Compare the class of `obj` with `cls`.
2872 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002873 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002874 // Classes must be equal for the instanceof to succeed.
2875 __ Xor(out, out, cls);
2876 __ Sltiu(out, out, 1);
2877 } else {
2878 // If the classes are not equal, we go into a slow path.
2879 DCHECK(locations->OnlyCallsOnSlowPath());
2880 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002881 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002882 codegen_->AddSlowPath(slow_path);
2883 __ Bnec(out, cls, slow_path->GetEntryLabel());
2884 __ LoadConst32(out, 1);
2885 __ Bind(slow_path->GetExitLabel());
2886 }
2887
2888 __ Bind(&done);
2889}
2890
2891void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2893 locations->SetOut(Location::ConstantLocation(constant));
2894}
2895
2896void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2897 // Will be generated at use site.
2898}
2899
2900void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2902 locations->SetOut(Location::ConstantLocation(constant));
2903}
2904
2905void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2906 // Will be generated at use site.
2907}
2908
Calin Juravle175dc732015-08-25 15:42:32 +01002909void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2910 // The trampoline uses the same calling convention as dex calling conventions,
2911 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2912 // the method_idx.
2913 HandleInvoke(invoke);
2914}
2915
2916void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2917 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2918}
2919
Alexey Frunze4dda3372015-06-01 18:31:49 -07002920void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2921 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2922 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2923}
2924
2925void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2926 HandleInvoke(invoke);
2927 // The register T0 is required to be used for the hidden argument in
2928 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2929 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2930}
2931
2932void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2933 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2934 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00002935 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2936 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002937 Location receiver = invoke->GetLocations()->InAt(0);
2938 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002939 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940
2941 // Set the hidden argument.
2942 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2943 invoke->GetDexMethodIndex());
2944
2945 // temp = object->GetClass();
2946 if (receiver.IsStackSlot()) {
2947 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2948 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2949 } else {
2950 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2951 }
2952 codegen_->MaybeRecordImplicitNullCheck(invoke);
2953 // temp = temp->GetImtEntryAt(method_offset);
2954 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2955 // T9 = temp->GetEntryPoint();
2956 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2957 // T9();
2958 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002959 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 DCHECK(!codegen_->IsLeafMethod());
2961 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2962}
2963
2964void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002965 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2966 if (intrinsic.TryDispatch(invoke)) {
2967 return;
2968 }
2969
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 HandleInvoke(invoke);
2971}
2972
2973void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002974 // Explicit clinit checks triggered by static invokes must have been pruned by
2975 // art::PrepareForRegisterAllocation.
2976 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002977
Chris Larsen3039e382015-08-26 07:54:08 -07002978 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2979 if (intrinsic.TryDispatch(invoke)) {
2980 return;
2981 }
2982
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002984}
2985
Chris Larsen3039e382015-08-26 07:54:08 -07002986static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002987 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002988 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2989 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002990 return true;
2991 }
2992 return false;
2993}
2994
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002995HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2996 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2997 // TODO: Implement other kinds.
2998 return HLoadString::LoadKind::kDexCacheViaMethod;
2999}
3000
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003001HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3002 HLoadClass::LoadKind desired_class_load_kind) {
3003 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3004 // TODO: Implement other kinds.
3005 return HLoadClass::LoadKind::kDexCacheViaMethod;
3006}
3007
Vladimir Markodc151b22015-10-15 18:02:30 +01003008HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3009 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3010 MethodReference target_method ATTRIBUTE_UNUSED) {
3011 switch (desired_dispatch_info.method_load_kind) {
3012 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3013 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3014 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3015 return HInvokeStaticOrDirect::DispatchInfo {
3016 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3017 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3018 0u,
3019 0u
3020 };
3021 default:
3022 break;
3023 }
3024 switch (desired_dispatch_info.code_ptr_location) {
3025 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3026 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3027 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3028 return HInvokeStaticOrDirect::DispatchInfo {
3029 desired_dispatch_info.method_load_kind,
3030 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3031 desired_dispatch_info.method_load_data,
3032 0u
3033 };
3034 default:
3035 return desired_dispatch_info;
3036 }
3037}
3038
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3040 // All registers are assumed to be correctly set up per the calling convention.
3041
Vladimir Marko58155012015-08-19 12:49:41 +00003042 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3043 switch (invoke->GetMethodLoadKind()) {
3044 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3045 // temp = thread->string_init_entrypoint
3046 __ LoadFromOffset(kLoadDoubleword,
3047 temp.AsRegister<GpuRegister>(),
3048 TR,
3049 invoke->GetStringInitOffset());
3050 break;
3051 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003052 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003053 break;
3054 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3055 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3056 break;
3057 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003058 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003059 // TODO: Implement these types.
3060 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3061 LOG(FATAL) << "Unsupported";
3062 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003063 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003064 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003065 GpuRegister reg = temp.AsRegister<GpuRegister>();
3066 GpuRegister method_reg;
3067 if (current_method.IsRegister()) {
3068 method_reg = current_method.AsRegister<GpuRegister>();
3069 } else {
3070 // TODO: use the appropriate DCHECK() here if possible.
3071 // DCHECK(invoke->GetLocations()->Intrinsified());
3072 DCHECK(!current_method.IsValid());
3073 method_reg = reg;
3074 __ Ld(reg, SP, kCurrentMethodStackOffset);
3075 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003076
Vladimir Marko58155012015-08-19 12:49:41 +00003077 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003078 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003079 reg,
3080 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003081 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003082 // temp = temp[index_in_cache];
3083 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3084 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003085 __ LoadFromOffset(kLoadDoubleword,
3086 reg,
3087 reg,
3088 CodeGenerator::GetCachePointerOffset(index_in_cache));
3089 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 }
3092
Vladimir Marko58155012015-08-19 12:49:41 +00003093 switch (invoke->GetCodePtrLocation()) {
3094 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003095 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003096 break;
3097 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3098 // LR = invoke->GetDirectCodePtr();
3099 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3100 // LR()
3101 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003102 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003103 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003104 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003105 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3106 // TODO: Implement these types.
3107 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3108 LOG(FATAL) << "Unsupported";
3109 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003110 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3111 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3112 __ LoadFromOffset(kLoadDoubleword,
3113 T9,
3114 callee_method.AsRegister<GpuRegister>(),
3115 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003116 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003117 // T9()
3118 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003119 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003120 break;
3121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122 DCHECK(!IsLeafMethod());
3123}
3124
3125void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003126 // Explicit clinit checks triggered by static invokes must have been pruned by
3127 // art::PrepareForRegisterAllocation.
3128 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129
3130 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3131 return;
3132 }
3133
3134 LocationSummary* locations = invoke->GetLocations();
3135 codegen_->GenerateStaticOrDirectCall(invoke,
3136 locations->HasTemps()
3137 ? locations->GetTemp(0)
3138 : Location::NoLocation());
3139 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3140}
3141
Alexey Frunze53afca12015-11-05 16:34:23 -08003142void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003143 // Use the calling convention instead of the location of the receiver, as
3144 // intrinsics may have put the receiver in a different register. In the intrinsics
3145 // slow path, the arguments have been moved to the right place, so here we are
3146 // guaranteed that the receiver is the first register of the calling convention.
3147 InvokeDexCallingConvention calling_convention;
3148 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3149
Alexey Frunze53afca12015-11-05 16:34:23 -08003150 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3152 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3153 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003154 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003155
3156 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003157 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003158 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003159 // temp = temp->GetMethodAt(method_offset);
3160 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3161 // T9 = temp->GetEntryPoint();
3162 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3163 // T9();
3164 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003165 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003166}
3167
3168void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3169 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3170 return;
3171 }
3172
3173 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003174 DCHECK(!codegen_->IsLeafMethod());
3175 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3176}
3177
3178void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003179 InvokeRuntimeCallingConvention calling_convention;
3180 CodeGenerator::CreateLoadClassLocationSummary(
3181 cls,
3182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003183 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003184}
3185
3186void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3187 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003188 if (cls->NeedsAccessCheck()) {
3189 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3190 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3191 cls,
3192 cls->GetDexPc(),
3193 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003194 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003195 return;
3196 }
3197
3198 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3199 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3200 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003201 DCHECK(!cls->CanCallRuntime());
3202 DCHECK(!cls->MustGenerateClinitCheck());
3203 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3204 ArtMethod::DeclaringClassOffset().Int32Value());
3205 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003206 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3207 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003208 __ LoadFromOffset(
3209 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003210 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003211 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3212 DCHECK(cls->CanCallRuntime());
3213 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3214 cls,
3215 cls,
3216 cls->GetDexPc(),
3217 cls->MustGenerateClinitCheck());
3218 codegen_->AddSlowPath(slow_path);
3219 if (!cls->IsInDexCache()) {
3220 __ Beqzc(out, slow_path->GetEntryLabel());
3221 }
3222 if (cls->MustGenerateClinitCheck()) {
3223 GenerateClassInitializationCheck(slow_path, out);
3224 } else {
3225 __ Bind(slow_path->GetExitLabel());
3226 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003227 }
3228 }
3229}
3230
David Brazdilcb1c0552015-08-04 16:22:25 +01003231static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003232 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003233}
3234
Alexey Frunze4dda3372015-06-01 18:31:49 -07003235void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3236 LocationSummary* locations =
3237 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3238 locations->SetOut(Location::RequiresRegister());
3239}
3240
3241void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3242 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003243 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3244}
3245
3246void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3247 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3248}
3249
3250void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3251 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252}
3253
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003255 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3256 ? LocationSummary::kCallOnSlowPath
3257 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetOut(Location::RequiresRegister());
3261}
3262
3263void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264 LocationSummary* locations = load->GetLocations();
3265 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3266 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3267 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3268 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003269 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003270 __ LoadFromOffset(
3271 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003272 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003273
3274 if (!load->IsInDexCache()) {
3275 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3276 codegen_->AddSlowPath(slow_path);
3277 __ Beqzc(out, slow_path->GetEntryLabel());
3278 __ Bind(slow_path->GetExitLabel());
3279 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003280}
3281
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3284 locations->SetOut(Location::ConstantLocation(constant));
3285}
3286
3287void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3288 // Will be generated at use site.
3289}
3290
3291void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3292 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003294 InvokeRuntimeCallingConvention calling_convention;
3295 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3296}
3297
3298void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3299 codegen_->InvokeRuntime(instruction->IsEnter()
3300 ? QUICK_ENTRY_POINT(pLockObject)
3301 : QUICK_ENTRY_POINT(pUnlockObject),
3302 instruction,
3303 instruction->GetDexPc(),
3304 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003305 if (instruction->IsEnter()) {
3306 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3307 } else {
3308 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003310}
3311
3312void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3313 LocationSummary* locations =
3314 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3315 switch (mul->GetResultType()) {
3316 case Primitive::kPrimInt:
3317 case Primitive::kPrimLong:
3318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetInAt(1, Location::RequiresRegister());
3320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3321 break;
3322
3323 case Primitive::kPrimFloat:
3324 case Primitive::kPrimDouble:
3325 locations->SetInAt(0, Location::RequiresFpuRegister());
3326 locations->SetInAt(1, Location::RequiresFpuRegister());
3327 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3328 break;
3329
3330 default:
3331 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3332 }
3333}
3334
3335void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3336 Primitive::Type type = instruction->GetType();
3337 LocationSummary* locations = instruction->GetLocations();
3338
3339 switch (type) {
3340 case Primitive::kPrimInt:
3341 case Primitive::kPrimLong: {
3342 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3343 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3344 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3345 if (type == Primitive::kPrimInt)
3346 __ MulR6(dst, lhs, rhs);
3347 else
3348 __ Dmul(dst, lhs, rhs);
3349 break;
3350 }
3351 case Primitive::kPrimFloat:
3352 case Primitive::kPrimDouble: {
3353 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3354 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3355 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3356 if (type == Primitive::kPrimFloat)
3357 __ MulS(dst, lhs, rhs);
3358 else
3359 __ MulD(dst, lhs, rhs);
3360 break;
3361 }
3362 default:
3363 LOG(FATAL) << "Unexpected mul type " << type;
3364 }
3365}
3366
3367void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3370 switch (neg->GetResultType()) {
3371 case Primitive::kPrimInt:
3372 case Primitive::kPrimLong:
3373 locations->SetInAt(0, Location::RequiresRegister());
3374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3375 break;
3376
3377 case Primitive::kPrimFloat:
3378 case Primitive::kPrimDouble:
3379 locations->SetInAt(0, Location::RequiresFpuRegister());
3380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3381 break;
3382
3383 default:
3384 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3385 }
3386}
3387
3388void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3389 Primitive::Type type = instruction->GetType();
3390 LocationSummary* locations = instruction->GetLocations();
3391
3392 switch (type) {
3393 case Primitive::kPrimInt:
3394 case Primitive::kPrimLong: {
3395 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3396 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3397 if (type == Primitive::kPrimInt)
3398 __ Subu(dst, ZERO, src);
3399 else
3400 __ Dsubu(dst, ZERO, src);
3401 break;
3402 }
3403 case Primitive::kPrimFloat:
3404 case Primitive::kPrimDouble: {
3405 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3406 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3407 if (type == Primitive::kPrimFloat)
3408 __ NegS(dst, src);
3409 else
3410 __ NegD(dst, src);
3411 break;
3412 }
3413 default:
3414 LOG(FATAL) << "Unexpected neg type " << type;
3415 }
3416}
3417
3418void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3419 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003421 InvokeRuntimeCallingConvention calling_convention;
3422 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3423 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3424 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3425 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3426}
3427
3428void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3429 LocationSummary* locations = instruction->GetLocations();
3430 // Move an uint16_t value to a register.
3431 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003432 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3433 instruction,
3434 instruction->GetDexPc(),
3435 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003436 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3437}
3438
3439void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3440 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003443 if (instruction->IsStringAlloc()) {
3444 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3445 } else {
3446 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3447 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3448 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003449 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3450}
3451
3452void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003453 if (instruction->IsStringAlloc()) {
3454 // String is allocated through StringFactory. Call NewEmptyString entry point.
3455 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003456 MemberOffset code_offset =
3457 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003458 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3459 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3460 __ Jalr(T9);
3461 __ Nop();
3462 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3463 } else {
3464 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3465 instruction,
3466 instruction->GetDexPc(),
3467 nullptr);
3468 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3469 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470}
3471
3472void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3474 locations->SetInAt(0, Location::RequiresRegister());
3475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3476}
3477
3478void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3479 Primitive::Type type = instruction->GetType();
3480 LocationSummary* locations = instruction->GetLocations();
3481
3482 switch (type) {
3483 case Primitive::kPrimInt:
3484 case Primitive::kPrimLong: {
3485 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3486 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3487 __ Nor(dst, src, ZERO);
3488 break;
3489 }
3490
3491 default:
3492 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3493 }
3494}
3495
3496void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3498 locations->SetInAt(0, Location::RequiresRegister());
3499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3500}
3501
3502void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3503 LocationSummary* locations = instruction->GetLocations();
3504 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3505 locations->InAt(0).AsRegister<GpuRegister>(),
3506 1);
3507}
3508
3509void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003510 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3511 ? LocationSummary::kCallOnSlowPath
3512 : LocationSummary::kNoCall;
3513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 locations->SetInAt(0, Location::RequiresRegister());
3515 if (instruction->HasUses()) {
3516 locations->SetOut(Location::SameAsFirstInput());
3517 }
3518}
3519
Calin Juravle2ae48182016-03-16 14:05:09 +00003520void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3521 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003522 return;
3523 }
3524 Location obj = instruction->GetLocations()->InAt(0);
3525
3526 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003527 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003528}
3529
Calin Juravle2ae48182016-03-16 14:05:09 +00003530void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003531 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003532 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533
3534 Location obj = instruction->GetLocations()->InAt(0);
3535
3536 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3537}
3538
3539void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003540 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003541}
3542
3543void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3544 HandleBinaryOp(instruction);
3545}
3546
3547void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3548 HandleBinaryOp(instruction);
3549}
3550
3551void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3552 LOG(FATAL) << "Unreachable";
3553}
3554
3555void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3556 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3557}
3558
3559void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3561 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3562 if (location.IsStackSlot()) {
3563 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3564 } else if (location.IsDoubleStackSlot()) {
3565 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3566 }
3567 locations->SetOut(location);
3568}
3569
3570void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3571 ATTRIBUTE_UNUSED) {
3572 // Nothing to do, the parameter is already at its location.
3573}
3574
3575void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3576 LocationSummary* locations =
3577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3578 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3579}
3580
3581void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3582 ATTRIBUTE_UNUSED) {
3583 // Nothing to do, the method is already at its location.
3584}
3585
3586void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003588 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589 locations->SetInAt(i, Location::Any());
3590 }
3591 locations->SetOut(Location::Any());
3592}
3593
3594void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3595 LOG(FATAL) << "Unreachable";
3596}
3597
3598void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3599 Primitive::Type type = rem->GetResultType();
3600 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003601 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3602 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3604
3605 switch (type) {
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimLong:
3608 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003609 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3611 break;
3612
3613 case Primitive::kPrimFloat:
3614 case Primitive::kPrimDouble: {
3615 InvokeRuntimeCallingConvention calling_convention;
3616 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3617 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3618 locations->SetOut(calling_convention.GetReturnLocation(type));
3619 break;
3620 }
3621
3622 default:
3623 LOG(FATAL) << "Unexpected rem type " << type;
3624 }
3625}
3626
3627void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3628 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629
3630 switch (type) {
3631 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003632 case Primitive::kPrimLong:
3633 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003634 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635
3636 case Primitive::kPrimFloat:
3637 case Primitive::kPrimDouble: {
3638 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3639 : QUICK_ENTRY_POINT(pFmod);
3640 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003641 if (type == Primitive::kPrimFloat) {
3642 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3643 } else {
3644 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3645 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003646 break;
3647 }
3648 default:
3649 LOG(FATAL) << "Unexpected rem type " << type;
3650 }
3651}
3652
3653void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3654 memory_barrier->SetLocations(nullptr);
3655}
3656
3657void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3658 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3659}
3660
3661void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3663 Primitive::Type return_type = ret->InputAt(0)->GetType();
3664 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3665}
3666
3667void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3668 codegen_->GenerateFrameExit();
3669}
3670
3671void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3672 ret->SetLocations(nullptr);
3673}
3674
3675void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3676 codegen_->GenerateFrameExit();
3677}
3678
Alexey Frunze92d90602015-12-18 18:16:36 -08003679void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3680 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003681}
3682
Alexey Frunze92d90602015-12-18 18:16:36 -08003683void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3684 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003685}
3686
Alexey Frunze4dda3372015-06-01 18:31:49 -07003687void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3688 HandleShift(shl);
3689}
3690
3691void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3692 HandleShift(shl);
3693}
3694
3695void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3696 HandleShift(shr);
3697}
3698
3699void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3700 HandleShift(shr);
3701}
3702
Alexey Frunze4dda3372015-06-01 18:31:49 -07003703void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3704 HandleBinaryOp(instruction);
3705}
3706
3707void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3708 HandleBinaryOp(instruction);
3709}
3710
3711void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3712 HandleFieldGet(instruction, instruction->GetFieldInfo());
3713}
3714
3715void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3716 HandleFieldGet(instruction, instruction->GetFieldInfo());
3717}
3718
3719void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3720 HandleFieldSet(instruction, instruction->GetFieldInfo());
3721}
3722
3723void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003724 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003725}
3726
Calin Juravlee460d1d2015-09-29 04:52:17 +01003727void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3728 HUnresolvedInstanceFieldGet* instruction) {
3729 FieldAccessCallingConventionMIPS64 calling_convention;
3730 codegen_->CreateUnresolvedFieldLocationSummary(
3731 instruction, instruction->GetFieldType(), calling_convention);
3732}
3733
3734void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3735 HUnresolvedInstanceFieldGet* instruction) {
3736 FieldAccessCallingConventionMIPS64 calling_convention;
3737 codegen_->GenerateUnresolvedFieldAccess(instruction,
3738 instruction->GetFieldType(),
3739 instruction->GetFieldIndex(),
3740 instruction->GetDexPc(),
3741 calling_convention);
3742}
3743
3744void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3745 HUnresolvedInstanceFieldSet* instruction) {
3746 FieldAccessCallingConventionMIPS64 calling_convention;
3747 codegen_->CreateUnresolvedFieldLocationSummary(
3748 instruction, instruction->GetFieldType(), calling_convention);
3749}
3750
3751void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3752 HUnresolvedInstanceFieldSet* instruction) {
3753 FieldAccessCallingConventionMIPS64 calling_convention;
3754 codegen_->GenerateUnresolvedFieldAccess(instruction,
3755 instruction->GetFieldType(),
3756 instruction->GetFieldIndex(),
3757 instruction->GetDexPc(),
3758 calling_convention);
3759}
3760
3761void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3762 HUnresolvedStaticFieldGet* instruction) {
3763 FieldAccessCallingConventionMIPS64 calling_convention;
3764 codegen_->CreateUnresolvedFieldLocationSummary(
3765 instruction, instruction->GetFieldType(), calling_convention);
3766}
3767
3768void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3769 HUnresolvedStaticFieldGet* instruction) {
3770 FieldAccessCallingConventionMIPS64 calling_convention;
3771 codegen_->GenerateUnresolvedFieldAccess(instruction,
3772 instruction->GetFieldType(),
3773 instruction->GetFieldIndex(),
3774 instruction->GetDexPc(),
3775 calling_convention);
3776}
3777
3778void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3779 HUnresolvedStaticFieldSet* instruction) {
3780 FieldAccessCallingConventionMIPS64 calling_convention;
3781 codegen_->CreateUnresolvedFieldLocationSummary(
3782 instruction, instruction->GetFieldType(), calling_convention);
3783}
3784
3785void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3786 HUnresolvedStaticFieldSet* instruction) {
3787 FieldAccessCallingConventionMIPS64 calling_convention;
3788 codegen_->GenerateUnresolvedFieldAccess(instruction,
3789 instruction->GetFieldType(),
3790 instruction->GetFieldIndex(),
3791 instruction->GetDexPc(),
3792 calling_convention);
3793}
3794
Alexey Frunze4dda3372015-06-01 18:31:49 -07003795void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3797}
3798
3799void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3800 HBasicBlock* block = instruction->GetBlock();
3801 if (block->GetLoopInformation() != nullptr) {
3802 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3803 // The back edge will generate the suspend check.
3804 return;
3805 }
3806 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3807 // The goto will generate the suspend check.
3808 return;
3809 }
3810 GenerateSuspendCheck(instruction, nullptr);
3811}
3812
Alexey Frunze4dda3372015-06-01 18:31:49 -07003813void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3814 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 InvokeRuntimeCallingConvention calling_convention;
3817 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3818}
3819
3820void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3821 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3822 instruction,
3823 instruction->GetDexPc(),
3824 nullptr);
3825 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3826}
3827
3828void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3829 Primitive::Type input_type = conversion->GetInputType();
3830 Primitive::Type result_type = conversion->GetResultType();
3831 DCHECK_NE(input_type, result_type);
3832
3833 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3834 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3835 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3836 }
3837
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003838 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3839
3840 if (Primitive::IsFloatingPointType(input_type)) {
3841 locations->SetInAt(0, Location::RequiresFpuRegister());
3842 } else {
3843 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003844 }
3845
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003846 if (Primitive::IsFloatingPointType(result_type)) {
3847 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003848 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003850 }
3851}
3852
3853void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3854 LocationSummary* locations = conversion->GetLocations();
3855 Primitive::Type result_type = conversion->GetResultType();
3856 Primitive::Type input_type = conversion->GetInputType();
3857
3858 DCHECK_NE(input_type, result_type);
3859
3860 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3861 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3862 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3863
3864 switch (result_type) {
3865 case Primitive::kPrimChar:
3866 __ Andi(dst, src, 0xFFFF);
3867 break;
3868 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003869 if (input_type == Primitive::kPrimLong) {
3870 // Type conversion from long to types narrower than int is a result of code
3871 // transformations. To avoid unpredictable results for SEB and SEH, we first
3872 // need to sign-extend the low 32-bit value into bits 32 through 63.
3873 __ Sll(dst, src, 0);
3874 __ Seb(dst, dst);
3875 } else {
3876 __ Seb(dst, src);
3877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003878 break;
3879 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003880 if (input_type == Primitive::kPrimLong) {
3881 // Type conversion from long to types narrower than int is a result of code
3882 // transformations. To avoid unpredictable results for SEB and SEH, we first
3883 // need to sign-extend the low 32-bit value into bits 32 through 63.
3884 __ Sll(dst, src, 0);
3885 __ Seh(dst, dst);
3886 } else {
3887 __ Seh(dst, src);
3888 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003889 break;
3890 case Primitive::kPrimInt:
3891 case Primitive::kPrimLong:
3892 // Sign-extend 32-bit int into bits 32 through 63 for
3893 // int-to-long and long-to-int conversions
3894 __ Sll(dst, src, 0);
3895 break;
3896
3897 default:
3898 LOG(FATAL) << "Unexpected type conversion from " << input_type
3899 << " to " << result_type;
3900 }
3901 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003902 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3903 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3904 if (input_type == Primitive::kPrimLong) {
3905 __ Dmtc1(src, FTMP);
3906 if (result_type == Primitive::kPrimFloat) {
3907 __ Cvtsl(dst, FTMP);
3908 } else {
3909 __ Cvtdl(dst, FTMP);
3910 }
3911 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003912 __ Mtc1(src, FTMP);
3913 if (result_type == Primitive::kPrimFloat) {
3914 __ Cvtsw(dst, FTMP);
3915 } else {
3916 __ Cvtdw(dst, FTMP);
3917 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003918 }
3919 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3920 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003921 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3922 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3923 Mips64Label truncate;
3924 Mips64Label done;
3925
3926 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3927 // value when the input is either a NaN or is outside of the range of the output type
3928 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3929 // the same result.
3930 //
3931 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3932 // value of the output type if the input is outside of the range after the truncation or
3933 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3934 // results. This matches the desired float/double-to-int/long conversion exactly.
3935 //
3936 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3937 //
3938 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3939 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3940 // even though it must be NAN2008=1 on R6.
3941 //
3942 // The code takes care of the different behaviors by first comparing the input to the
3943 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3944 // If the input is greater than or equal to the minimum, it procedes to the truncate
3945 // instruction, which will handle such an input the same way irrespective of NAN2008.
3946 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3947 // in order to return either zero or the minimum value.
3948 //
3949 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3950 // truncate instruction for MIPS64R6.
3951 if (input_type == Primitive::kPrimFloat) {
3952 uint32_t min_val = (result_type == Primitive::kPrimLong)
3953 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3954 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3955 __ LoadConst32(TMP, min_val);
3956 __ Mtc1(TMP, FTMP);
3957 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003958 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003959 uint64_t min_val = (result_type == Primitive::kPrimLong)
3960 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3961 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3962 __ LoadConst64(TMP, min_val);
3963 __ Dmtc1(TMP, FTMP);
3964 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003965 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003966
3967 __ Bc1nez(FTMP, &truncate);
3968
3969 if (input_type == Primitive::kPrimFloat) {
3970 __ CmpEqS(FTMP, src, src);
3971 } else {
3972 __ CmpEqD(FTMP, src, src);
3973 }
3974 if (result_type == Primitive::kPrimLong) {
3975 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3976 } else {
3977 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3978 }
3979 __ Mfc1(TMP, FTMP);
3980 __ And(dst, dst, TMP);
3981
3982 __ Bc(&done);
3983
3984 __ Bind(&truncate);
3985
3986 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003987 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003988 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003989 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003990 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003991 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003992 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003993 } else {
3994 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003995 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003996 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003997 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003998 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003999 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004000 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004001
4002 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003 } else if (Primitive::IsFloatingPointType(result_type) &&
4004 Primitive::IsFloatingPointType(input_type)) {
4005 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4006 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4007 if (result_type == Primitive::kPrimFloat) {
4008 __ Cvtsd(dst, src);
4009 } else {
4010 __ Cvtds(dst, src);
4011 }
4012 } else {
4013 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4014 << " to " << result_type;
4015 }
4016}
4017
4018void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4019 HandleShift(ushr);
4020}
4021
4022void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4023 HandleShift(ushr);
4024}
4025
4026void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4027 HandleBinaryOp(instruction);
4028}
4029
4030void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4031 HandleBinaryOp(instruction);
4032}
4033
4034void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4035 // Nothing to do, this should be removed during prepare for register allocator.
4036 LOG(FATAL) << "Unreachable";
4037}
4038
4039void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4040 // Nothing to do, this should be removed during prepare for register allocator.
4041 LOG(FATAL) << "Unreachable";
4042}
4043
4044void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004045 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004046}
4047
4048void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004049 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004050}
4051
4052void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004053 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004054}
4055
4056void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004057 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004058}
4059
4060void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004061 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004062}
4063
4064void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004065 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066}
4067
4068void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004069 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070}
4071
4072void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004073 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004074}
4075
4076void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004077 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004078}
4079
4080void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004081 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004082}
4083
4084void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004085 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004086}
4087
4088void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004089 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004090}
4091
Aart Bike9f37602015-10-09 11:15:55 -07004092void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004093 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004094}
4095
4096void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004097 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004098}
4099
4100void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004101 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004102}
4103
4104void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004105 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004106}
4107
4108void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004109 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004110}
4111
4112void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004114}
4115
4116void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004122}
4123
Mark Mendellfe57faa2015-09-18 09:26:15 -04004124// Simple implementation of packed switch - generate cascaded compare/jumps.
4125void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4126 LocationSummary* locations =
4127 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4128 locations->SetInAt(0, Location::RequiresRegister());
4129}
4130
4131void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4132 int32_t lower_bound = switch_instr->GetStartValue();
4133 int32_t num_entries = switch_instr->GetNumEntries();
4134 LocationSummary* locations = switch_instr->GetLocations();
4135 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4136 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4137
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004138 // Create a set of compare/jumps.
4139 GpuRegister temp_reg = TMP;
4140 if (IsInt<16>(-lower_bound)) {
4141 __ Addiu(temp_reg, value_reg, -lower_bound);
4142 } else {
4143 __ LoadConst32(AT, -lower_bound);
4144 __ Addu(temp_reg, value_reg, AT);
4145 }
4146 // Jump to default if index is negative
4147 // Note: We don't check the case that index is positive while value < lower_bound, because in
4148 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4149 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4150
Mark Mendellfe57faa2015-09-18 09:26:15 -04004151 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004152 // Jump to successors[0] if value == lower_bound.
4153 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4154 int32_t last_index = 0;
4155 for (; num_entries - last_index > 2; last_index += 2) {
4156 __ Addiu(temp_reg, temp_reg, -2);
4157 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4158 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4159 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4160 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4161 }
4162 if (num_entries - last_index == 2) {
4163 // The last missing case_value.
4164 __ Addiu(temp_reg, temp_reg, -1);
4165 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004166 }
4167
4168 // And the default for any other value.
4169 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004170 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004171 }
4172}
4173
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004174void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4175 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4176}
4177
4178void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4179 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4180}
4181
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182} // namespace mips64
4183} // namespace art