blob: cf3c42e7a92ba4d561a098a7bac92f5208272125 [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<
Andreas Gampe67409972016-07-19 22:34:53 -0700365 kQuickInstanceofNonTrivial, size_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>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002935 Location receiver = invoke->GetLocations()->InAt(0);
2936 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002937 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938
2939 // Set the hidden argument.
2940 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2941 invoke->GetDexMethodIndex());
2942
2943 // temp = object->GetClass();
2944 if (receiver.IsStackSlot()) {
2945 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2946 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2947 } else {
2948 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2949 }
2950 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002951 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2952 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2953 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2954 invoke->GetImtIndex() % ImTable::kSize, kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 // temp = temp->GetImtEntryAt(method_offset);
2956 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2957 // T9 = temp->GetEntryPoint();
2958 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2959 // T9();
2960 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002961 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002962 DCHECK(!codegen_->IsLeafMethod());
2963 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2964}
2965
2966void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002967 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2968 if (intrinsic.TryDispatch(invoke)) {
2969 return;
2970 }
2971
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972 HandleInvoke(invoke);
2973}
2974
2975void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002976 // Explicit clinit checks triggered by static invokes must have been pruned by
2977 // art::PrepareForRegisterAllocation.
2978 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002979
Chris Larsen3039e382015-08-26 07:54:08 -07002980 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2981 if (intrinsic.TryDispatch(invoke)) {
2982 return;
2983 }
2984
Alexey Frunze4dda3372015-06-01 18:31:49 -07002985 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002986}
2987
Chris Larsen3039e382015-08-26 07:54:08 -07002988static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002989 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002990 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2991 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002992 return true;
2993 }
2994 return false;
2995}
2996
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002997HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2998 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2999 // TODO: Implement other kinds.
3000 return HLoadString::LoadKind::kDexCacheViaMethod;
3001}
3002
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003003HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3004 HLoadClass::LoadKind desired_class_load_kind) {
3005 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3006 // TODO: Implement other kinds.
3007 return HLoadClass::LoadKind::kDexCacheViaMethod;
3008}
3009
Vladimir Markodc151b22015-10-15 18:02:30 +01003010HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3011 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3012 MethodReference target_method ATTRIBUTE_UNUSED) {
3013 switch (desired_dispatch_info.method_load_kind) {
3014 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3015 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3016 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3017 return HInvokeStaticOrDirect::DispatchInfo {
3018 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3019 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3020 0u,
3021 0u
3022 };
3023 default:
3024 break;
3025 }
3026 switch (desired_dispatch_info.code_ptr_location) {
3027 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3028 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3029 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3030 return HInvokeStaticOrDirect::DispatchInfo {
3031 desired_dispatch_info.method_load_kind,
3032 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3033 desired_dispatch_info.method_load_data,
3034 0u
3035 };
3036 default:
3037 return desired_dispatch_info;
3038 }
3039}
3040
Alexey Frunze4dda3372015-06-01 18:31:49 -07003041void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3042 // All registers are assumed to be correctly set up per the calling convention.
3043
Vladimir Marko58155012015-08-19 12:49:41 +00003044 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3045 switch (invoke->GetMethodLoadKind()) {
3046 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3047 // temp = thread->string_init_entrypoint
3048 __ LoadFromOffset(kLoadDoubleword,
3049 temp.AsRegister<GpuRegister>(),
3050 TR,
3051 invoke->GetStringInitOffset());
3052 break;
3053 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003054 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003055 break;
3056 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3057 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3058 break;
3059 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003060 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003061 // TODO: Implement these types.
3062 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3063 LOG(FATAL) << "Unsupported";
3064 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003065 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003066 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003067 GpuRegister reg = temp.AsRegister<GpuRegister>();
3068 GpuRegister method_reg;
3069 if (current_method.IsRegister()) {
3070 method_reg = current_method.AsRegister<GpuRegister>();
3071 } else {
3072 // TODO: use the appropriate DCHECK() here if possible.
3073 // DCHECK(invoke->GetLocations()->Intrinsified());
3074 DCHECK(!current_method.IsValid());
3075 method_reg = reg;
3076 __ Ld(reg, SP, kCurrentMethodStackOffset);
3077 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003078
Vladimir Marko58155012015-08-19 12:49:41 +00003079 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003080 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003081 reg,
3082 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003083 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003084 // temp = temp[index_in_cache];
3085 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3086 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003087 __ LoadFromOffset(kLoadDoubleword,
3088 reg,
3089 reg,
3090 CodeGenerator::GetCachePointerOffset(index_in_cache));
3091 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003092 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003093 }
3094
Vladimir Marko58155012015-08-19 12:49:41 +00003095 switch (invoke->GetCodePtrLocation()) {
3096 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003097 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003098 break;
3099 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3100 // LR = invoke->GetDirectCodePtr();
3101 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3102 // LR()
3103 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003104 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003105 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003106 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003107 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3108 // TODO: Implement these types.
3109 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3110 LOG(FATAL) << "Unsupported";
3111 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003112 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3113 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3114 __ LoadFromOffset(kLoadDoubleword,
3115 T9,
3116 callee_method.AsRegister<GpuRegister>(),
3117 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003118 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003119 // T9()
3120 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003121 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003122 break;
3123 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003124 DCHECK(!IsLeafMethod());
3125}
3126
3127void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003128 // Explicit clinit checks triggered by static invokes must have been pruned by
3129 // art::PrepareForRegisterAllocation.
3130 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003131
3132 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3133 return;
3134 }
3135
3136 LocationSummary* locations = invoke->GetLocations();
3137 codegen_->GenerateStaticOrDirectCall(invoke,
3138 locations->HasTemps()
3139 ? locations->GetTemp(0)
3140 : Location::NoLocation());
3141 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3142}
3143
Alexey Frunze53afca12015-11-05 16:34:23 -08003144void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003145 // Use the calling convention instead of the location of the receiver, as
3146 // intrinsics may have put the receiver in a different register. In the intrinsics
3147 // slow path, the arguments have been moved to the right place, so here we are
3148 // guaranteed that the receiver is the first register of the calling convention.
3149 InvokeDexCallingConvention calling_convention;
3150 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3151
Alexey Frunze53afca12015-11-05 16:34:23 -08003152 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003153 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3154 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3155 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003156 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003157
3158 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003159 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003160 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161 // temp = temp->GetMethodAt(method_offset);
3162 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3163 // T9 = temp->GetEntryPoint();
3164 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3165 // T9();
3166 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003167 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003168}
3169
3170void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3171 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3172 return;
3173 }
3174
3175 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176 DCHECK(!codegen_->IsLeafMethod());
3177 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3178}
3179
3180void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003181 InvokeRuntimeCallingConvention calling_convention;
3182 CodeGenerator::CreateLoadClassLocationSummary(
3183 cls,
3184 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003185 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003186}
3187
3188void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3189 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003190 if (cls->NeedsAccessCheck()) {
3191 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3192 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3193 cls,
3194 cls->GetDexPc(),
3195 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003196 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003197 return;
3198 }
3199
3200 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3201 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3202 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003203 DCHECK(!cls->CanCallRuntime());
3204 DCHECK(!cls->MustGenerateClinitCheck());
3205 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3206 ArtMethod::DeclaringClassOffset().Int32Value());
3207 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003208 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3209 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003210 __ LoadFromOffset(
3211 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003212 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003213 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3214 DCHECK(cls->CanCallRuntime());
3215 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3216 cls,
3217 cls,
3218 cls->GetDexPc(),
3219 cls->MustGenerateClinitCheck());
3220 codegen_->AddSlowPath(slow_path);
3221 if (!cls->IsInDexCache()) {
3222 __ Beqzc(out, slow_path->GetEntryLabel());
3223 }
3224 if (cls->MustGenerateClinitCheck()) {
3225 GenerateClassInitializationCheck(slow_path, out);
3226 } else {
3227 __ Bind(slow_path->GetExitLabel());
3228 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229 }
3230 }
3231}
3232
David Brazdilcb1c0552015-08-04 16:22:25 +01003233static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003234 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003235}
3236
Alexey Frunze4dda3372015-06-01 18:31:49 -07003237void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3238 LocationSummary* locations =
3239 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3240 locations->SetOut(Location::RequiresRegister());
3241}
3242
3243void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3244 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003245 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3246}
3247
3248void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3249 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3250}
3251
3252void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3253 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254}
3255
Alexey Frunze4dda3372015-06-01 18:31:49 -07003256void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003257 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3258 ? LocationSummary::kCallOnSlowPath
3259 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003260 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261 locations->SetInAt(0, Location::RequiresRegister());
3262 locations->SetOut(Location::RequiresRegister());
3263}
3264
3265void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266 LocationSummary* locations = load->GetLocations();
3267 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3268 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3269 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3270 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003271 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003272 __ LoadFromOffset(
3273 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003274 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003275
3276 if (!load->IsInDexCache()) {
3277 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3278 codegen_->AddSlowPath(slow_path);
3279 __ Beqzc(out, slow_path->GetEntryLabel());
3280 __ Bind(slow_path->GetExitLabel());
3281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282}
3283
Alexey Frunze4dda3372015-06-01 18:31:49 -07003284void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3286 locations->SetOut(Location::ConstantLocation(constant));
3287}
3288
3289void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3290 // Will be generated at use site.
3291}
3292
3293void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3294 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003296 InvokeRuntimeCallingConvention calling_convention;
3297 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3298}
3299
3300void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3301 codegen_->InvokeRuntime(instruction->IsEnter()
3302 ? QUICK_ENTRY_POINT(pLockObject)
3303 : QUICK_ENTRY_POINT(pUnlockObject),
3304 instruction,
3305 instruction->GetDexPc(),
3306 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003307 if (instruction->IsEnter()) {
3308 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3309 } else {
3310 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3311 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003312}
3313
3314void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3315 LocationSummary* locations =
3316 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3317 switch (mul->GetResultType()) {
3318 case Primitive::kPrimInt:
3319 case Primitive::kPrimLong:
3320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetInAt(1, Location::RequiresRegister());
3322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3323 break;
3324
3325 case Primitive::kPrimFloat:
3326 case Primitive::kPrimDouble:
3327 locations->SetInAt(0, Location::RequiresFpuRegister());
3328 locations->SetInAt(1, Location::RequiresFpuRegister());
3329 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3330 break;
3331
3332 default:
3333 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3334 }
3335}
3336
3337void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3338 Primitive::Type type = instruction->GetType();
3339 LocationSummary* locations = instruction->GetLocations();
3340
3341 switch (type) {
3342 case Primitive::kPrimInt:
3343 case Primitive::kPrimLong: {
3344 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3345 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3346 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3347 if (type == Primitive::kPrimInt)
3348 __ MulR6(dst, lhs, rhs);
3349 else
3350 __ Dmul(dst, lhs, rhs);
3351 break;
3352 }
3353 case Primitive::kPrimFloat:
3354 case Primitive::kPrimDouble: {
3355 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3356 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3357 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3358 if (type == Primitive::kPrimFloat)
3359 __ MulS(dst, lhs, rhs);
3360 else
3361 __ MulD(dst, lhs, rhs);
3362 break;
3363 }
3364 default:
3365 LOG(FATAL) << "Unexpected mul type " << type;
3366 }
3367}
3368
3369void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3370 LocationSummary* locations =
3371 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3372 switch (neg->GetResultType()) {
3373 case Primitive::kPrimInt:
3374 case Primitive::kPrimLong:
3375 locations->SetInAt(0, Location::RequiresRegister());
3376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3377 break;
3378
3379 case Primitive::kPrimFloat:
3380 case Primitive::kPrimDouble:
3381 locations->SetInAt(0, Location::RequiresFpuRegister());
3382 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3383 break;
3384
3385 default:
3386 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3387 }
3388}
3389
3390void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3391 Primitive::Type type = instruction->GetType();
3392 LocationSummary* locations = instruction->GetLocations();
3393
3394 switch (type) {
3395 case Primitive::kPrimInt:
3396 case Primitive::kPrimLong: {
3397 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3398 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3399 if (type == Primitive::kPrimInt)
3400 __ Subu(dst, ZERO, src);
3401 else
3402 __ Dsubu(dst, ZERO, src);
3403 break;
3404 }
3405 case Primitive::kPrimFloat:
3406 case Primitive::kPrimDouble: {
3407 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3408 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3409 if (type == Primitive::kPrimFloat)
3410 __ NegS(dst, src);
3411 else
3412 __ NegD(dst, src);
3413 break;
3414 }
3415 default:
3416 LOG(FATAL) << "Unexpected neg type " << type;
3417 }
3418}
3419
3420void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3421 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003423 InvokeRuntimeCallingConvention calling_convention;
3424 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3425 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3426 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3427 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3428}
3429
3430void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3431 LocationSummary* locations = instruction->GetLocations();
3432 // Move an uint16_t value to a register.
3433 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003434 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3435 instruction,
3436 instruction->GetDexPc(),
3437 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3439}
3440
3441void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3442 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003444 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003445 if (instruction->IsStringAlloc()) {
3446 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3447 } else {
3448 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3449 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3450 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003451 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3452}
3453
3454void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003455 if (instruction->IsStringAlloc()) {
3456 // String is allocated through StringFactory. Call NewEmptyString entry point.
3457 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003458 MemberOffset code_offset =
3459 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003460 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3461 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3462 __ Jalr(T9);
3463 __ Nop();
3464 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3465 } else {
3466 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3467 instruction,
3468 instruction->GetDexPc(),
3469 nullptr);
3470 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3471 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003472}
3473
3474void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3476 locations->SetInAt(0, Location::RequiresRegister());
3477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3478}
3479
3480void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3481 Primitive::Type type = instruction->GetType();
3482 LocationSummary* locations = instruction->GetLocations();
3483
3484 switch (type) {
3485 case Primitive::kPrimInt:
3486 case Primitive::kPrimLong: {
3487 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3488 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3489 __ Nor(dst, src, ZERO);
3490 break;
3491 }
3492
3493 default:
3494 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3495 }
3496}
3497
3498void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3500 locations->SetInAt(0, Location::RequiresRegister());
3501 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3502}
3503
3504void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3505 LocationSummary* locations = instruction->GetLocations();
3506 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3507 locations->InAt(0).AsRegister<GpuRegister>(),
3508 1);
3509}
3510
3511void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003512 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3513 ? LocationSummary::kCallOnSlowPath
3514 : LocationSummary::kNoCall;
3515 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 locations->SetInAt(0, Location::RequiresRegister());
3517 if (instruction->HasUses()) {
3518 locations->SetOut(Location::SameAsFirstInput());
3519 }
3520}
3521
Calin Juravle2ae48182016-03-16 14:05:09 +00003522void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3523 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003524 return;
3525 }
3526 Location obj = instruction->GetLocations()->InAt(0);
3527
3528 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003529 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003530}
3531
Calin Juravle2ae48182016-03-16 14:05:09 +00003532void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003534 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003535
3536 Location obj = instruction->GetLocations()->InAt(0);
3537
3538 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3539}
3540
3541void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003542 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543}
3544
3545void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3546 HandleBinaryOp(instruction);
3547}
3548
3549void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3550 HandleBinaryOp(instruction);
3551}
3552
3553void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3554 LOG(FATAL) << "Unreachable";
3555}
3556
3557void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3558 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3559}
3560
3561void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3563 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3564 if (location.IsStackSlot()) {
3565 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3566 } else if (location.IsDoubleStackSlot()) {
3567 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3568 }
3569 locations->SetOut(location);
3570}
3571
3572void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3573 ATTRIBUTE_UNUSED) {
3574 // Nothing to do, the parameter is already at its location.
3575}
3576
3577void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3578 LocationSummary* locations =
3579 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3580 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3581}
3582
3583void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3584 ATTRIBUTE_UNUSED) {
3585 // Nothing to do, the method is already at its location.
3586}
3587
3588void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003590 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003591 locations->SetInAt(i, Location::Any());
3592 }
3593 locations->SetOut(Location::Any());
3594}
3595
3596void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3597 LOG(FATAL) << "Unreachable";
3598}
3599
3600void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3601 Primitive::Type type = rem->GetResultType();
3602 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003603 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3604 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3606
3607 switch (type) {
3608 case Primitive::kPrimInt:
3609 case Primitive::kPrimLong:
3610 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003611 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3613 break;
3614
3615 case Primitive::kPrimFloat:
3616 case Primitive::kPrimDouble: {
3617 InvokeRuntimeCallingConvention calling_convention;
3618 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3619 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3620 locations->SetOut(calling_convention.GetReturnLocation(type));
3621 break;
3622 }
3623
3624 default:
3625 LOG(FATAL) << "Unexpected rem type " << type;
3626 }
3627}
3628
3629void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3630 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003631
3632 switch (type) {
3633 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003634 case Primitive::kPrimLong:
3635 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003636 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003637
3638 case Primitive::kPrimFloat:
3639 case Primitive::kPrimDouble: {
3640 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3641 : QUICK_ENTRY_POINT(pFmod);
3642 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003643 if (type == Primitive::kPrimFloat) {
3644 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3645 } else {
3646 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3647 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003648 break;
3649 }
3650 default:
3651 LOG(FATAL) << "Unexpected rem type " << type;
3652 }
3653}
3654
3655void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3656 memory_barrier->SetLocations(nullptr);
3657}
3658
3659void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3660 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3661}
3662
3663void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3665 Primitive::Type return_type = ret->InputAt(0)->GetType();
3666 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3667}
3668
3669void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3670 codegen_->GenerateFrameExit();
3671}
3672
3673void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3674 ret->SetLocations(nullptr);
3675}
3676
3677void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3678 codegen_->GenerateFrameExit();
3679}
3680
Alexey Frunze92d90602015-12-18 18:16:36 -08003681void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3682 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003683}
3684
Alexey Frunze92d90602015-12-18 18:16:36 -08003685void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3686 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003687}
3688
Alexey Frunze4dda3372015-06-01 18:31:49 -07003689void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3690 HandleShift(shl);
3691}
3692
3693void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3694 HandleShift(shl);
3695}
3696
3697void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3698 HandleShift(shr);
3699}
3700
3701void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3702 HandleShift(shr);
3703}
3704
Alexey Frunze4dda3372015-06-01 18:31:49 -07003705void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3706 HandleBinaryOp(instruction);
3707}
3708
3709void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3710 HandleBinaryOp(instruction);
3711}
3712
3713void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3714 HandleFieldGet(instruction, instruction->GetFieldInfo());
3715}
3716
3717void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3718 HandleFieldGet(instruction, instruction->GetFieldInfo());
3719}
3720
3721void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3722 HandleFieldSet(instruction, instruction->GetFieldInfo());
3723}
3724
3725void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003726 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003727}
3728
Calin Juravlee460d1d2015-09-29 04:52:17 +01003729void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3730 HUnresolvedInstanceFieldGet* instruction) {
3731 FieldAccessCallingConventionMIPS64 calling_convention;
3732 codegen_->CreateUnresolvedFieldLocationSummary(
3733 instruction, instruction->GetFieldType(), calling_convention);
3734}
3735
3736void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3737 HUnresolvedInstanceFieldGet* instruction) {
3738 FieldAccessCallingConventionMIPS64 calling_convention;
3739 codegen_->GenerateUnresolvedFieldAccess(instruction,
3740 instruction->GetFieldType(),
3741 instruction->GetFieldIndex(),
3742 instruction->GetDexPc(),
3743 calling_convention);
3744}
3745
3746void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3747 HUnresolvedInstanceFieldSet* instruction) {
3748 FieldAccessCallingConventionMIPS64 calling_convention;
3749 codegen_->CreateUnresolvedFieldLocationSummary(
3750 instruction, instruction->GetFieldType(), calling_convention);
3751}
3752
3753void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3754 HUnresolvedInstanceFieldSet* instruction) {
3755 FieldAccessCallingConventionMIPS64 calling_convention;
3756 codegen_->GenerateUnresolvedFieldAccess(instruction,
3757 instruction->GetFieldType(),
3758 instruction->GetFieldIndex(),
3759 instruction->GetDexPc(),
3760 calling_convention);
3761}
3762
3763void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3764 HUnresolvedStaticFieldGet* instruction) {
3765 FieldAccessCallingConventionMIPS64 calling_convention;
3766 codegen_->CreateUnresolvedFieldLocationSummary(
3767 instruction, instruction->GetFieldType(), calling_convention);
3768}
3769
3770void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3771 HUnresolvedStaticFieldGet* instruction) {
3772 FieldAccessCallingConventionMIPS64 calling_convention;
3773 codegen_->GenerateUnresolvedFieldAccess(instruction,
3774 instruction->GetFieldType(),
3775 instruction->GetFieldIndex(),
3776 instruction->GetDexPc(),
3777 calling_convention);
3778}
3779
3780void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3781 HUnresolvedStaticFieldSet* instruction) {
3782 FieldAccessCallingConventionMIPS64 calling_convention;
3783 codegen_->CreateUnresolvedFieldLocationSummary(
3784 instruction, instruction->GetFieldType(), calling_convention);
3785}
3786
3787void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3788 HUnresolvedStaticFieldSet* instruction) {
3789 FieldAccessCallingConventionMIPS64 calling_convention;
3790 codegen_->GenerateUnresolvedFieldAccess(instruction,
3791 instruction->GetFieldType(),
3792 instruction->GetFieldIndex(),
3793 instruction->GetDexPc(),
3794 calling_convention);
3795}
3796
Alexey Frunze4dda3372015-06-01 18:31:49 -07003797void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3799}
3800
3801void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3802 HBasicBlock* block = instruction->GetBlock();
3803 if (block->GetLoopInformation() != nullptr) {
3804 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3805 // The back edge will generate the suspend check.
3806 return;
3807 }
3808 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3809 // The goto will generate the suspend check.
3810 return;
3811 }
3812 GenerateSuspendCheck(instruction, nullptr);
3813}
3814
Alexey Frunze4dda3372015-06-01 18:31:49 -07003815void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3816 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003818 InvokeRuntimeCallingConvention calling_convention;
3819 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3820}
3821
3822void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3823 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3824 instruction,
3825 instruction->GetDexPc(),
3826 nullptr);
3827 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3828}
3829
3830void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3831 Primitive::Type input_type = conversion->GetInputType();
3832 Primitive::Type result_type = conversion->GetResultType();
3833 DCHECK_NE(input_type, result_type);
3834
3835 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3836 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3837 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3838 }
3839
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3841
3842 if (Primitive::IsFloatingPointType(input_type)) {
3843 locations->SetInAt(0, Location::RequiresFpuRegister());
3844 } else {
3845 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003846 }
3847
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003848 if (Primitive::IsFloatingPointType(result_type)) {
3849 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003850 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003851 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003852 }
3853}
3854
3855void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3856 LocationSummary* locations = conversion->GetLocations();
3857 Primitive::Type result_type = conversion->GetResultType();
3858 Primitive::Type input_type = conversion->GetInputType();
3859
3860 DCHECK_NE(input_type, result_type);
3861
3862 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3863 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3864 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3865
3866 switch (result_type) {
3867 case Primitive::kPrimChar:
3868 __ Andi(dst, src, 0xFFFF);
3869 break;
3870 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003871 if (input_type == Primitive::kPrimLong) {
3872 // Type conversion from long to types narrower than int is a result of code
3873 // transformations. To avoid unpredictable results for SEB and SEH, we first
3874 // need to sign-extend the low 32-bit value into bits 32 through 63.
3875 __ Sll(dst, src, 0);
3876 __ Seb(dst, dst);
3877 } else {
3878 __ Seb(dst, src);
3879 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003880 break;
3881 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003882 if (input_type == Primitive::kPrimLong) {
3883 // Type conversion from long to types narrower than int is a result of code
3884 // transformations. To avoid unpredictable results for SEB and SEH, we first
3885 // need to sign-extend the low 32-bit value into bits 32 through 63.
3886 __ Sll(dst, src, 0);
3887 __ Seh(dst, dst);
3888 } else {
3889 __ Seh(dst, src);
3890 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003891 break;
3892 case Primitive::kPrimInt:
3893 case Primitive::kPrimLong:
3894 // Sign-extend 32-bit int into bits 32 through 63 for
3895 // int-to-long and long-to-int conversions
3896 __ Sll(dst, src, 0);
3897 break;
3898
3899 default:
3900 LOG(FATAL) << "Unexpected type conversion from " << input_type
3901 << " to " << result_type;
3902 }
3903 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003904 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3905 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3906 if (input_type == Primitive::kPrimLong) {
3907 __ Dmtc1(src, FTMP);
3908 if (result_type == Primitive::kPrimFloat) {
3909 __ Cvtsl(dst, FTMP);
3910 } else {
3911 __ Cvtdl(dst, FTMP);
3912 }
3913 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003914 __ Mtc1(src, FTMP);
3915 if (result_type == Primitive::kPrimFloat) {
3916 __ Cvtsw(dst, FTMP);
3917 } else {
3918 __ Cvtdw(dst, FTMP);
3919 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003920 }
3921 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3922 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003923 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3924 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3925 Mips64Label truncate;
3926 Mips64Label done;
3927
3928 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3929 // value when the input is either a NaN or is outside of the range of the output type
3930 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3931 // the same result.
3932 //
3933 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3934 // value of the output type if the input is outside of the range after the truncation or
3935 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3936 // results. This matches the desired float/double-to-int/long conversion exactly.
3937 //
3938 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3939 //
3940 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3941 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3942 // even though it must be NAN2008=1 on R6.
3943 //
3944 // The code takes care of the different behaviors by first comparing the input to the
3945 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3946 // If the input is greater than or equal to the minimum, it procedes to the truncate
3947 // instruction, which will handle such an input the same way irrespective of NAN2008.
3948 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3949 // in order to return either zero or the minimum value.
3950 //
3951 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3952 // truncate instruction for MIPS64R6.
3953 if (input_type == Primitive::kPrimFloat) {
3954 uint32_t min_val = (result_type == Primitive::kPrimLong)
3955 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3956 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3957 __ LoadConst32(TMP, min_val);
3958 __ Mtc1(TMP, FTMP);
3959 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003960 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003961 uint64_t min_val = (result_type == Primitive::kPrimLong)
3962 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3963 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3964 __ LoadConst64(TMP, min_val);
3965 __ Dmtc1(TMP, FTMP);
3966 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003967 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003968
3969 __ Bc1nez(FTMP, &truncate);
3970
3971 if (input_type == Primitive::kPrimFloat) {
3972 __ CmpEqS(FTMP, src, src);
3973 } else {
3974 __ CmpEqD(FTMP, src, src);
3975 }
3976 if (result_type == Primitive::kPrimLong) {
3977 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3978 } else {
3979 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3980 }
3981 __ Mfc1(TMP, FTMP);
3982 __ And(dst, dst, TMP);
3983
3984 __ Bc(&done);
3985
3986 __ Bind(&truncate);
3987
3988 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003989 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003990 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003991 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003992 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003993 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003994 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003995 } else {
3996 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003997 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003998 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003999 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004000 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004001 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004002 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004003
4004 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004005 } else if (Primitive::IsFloatingPointType(result_type) &&
4006 Primitive::IsFloatingPointType(input_type)) {
4007 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4008 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4009 if (result_type == Primitive::kPrimFloat) {
4010 __ Cvtsd(dst, src);
4011 } else {
4012 __ Cvtds(dst, src);
4013 }
4014 } else {
4015 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4016 << " to " << result_type;
4017 }
4018}
4019
4020void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4021 HandleShift(ushr);
4022}
4023
4024void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4025 HandleShift(ushr);
4026}
4027
4028void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4029 HandleBinaryOp(instruction);
4030}
4031
4032void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4033 HandleBinaryOp(instruction);
4034}
4035
4036void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4037 // Nothing to do, this should be removed during prepare for register allocator.
4038 LOG(FATAL) << "Unreachable";
4039}
4040
4041void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4042 // Nothing to do, this should be removed during prepare for register allocator.
4043 LOG(FATAL) << "Unreachable";
4044}
4045
4046void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004047 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004048}
4049
4050void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004051 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004052}
4053
4054void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004055 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004056}
4057
4058void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004059 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004060}
4061
4062void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064}
4065
4066void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004067 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004068}
4069
4070void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004071 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004072}
4073
4074void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004075 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004076}
4077
4078void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004079 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004080}
4081
4082void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004083 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004084}
4085
4086void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004087 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004088}
4089
4090void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004091 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004092}
4093
Aart Bike9f37602015-10-09 11:15:55 -07004094void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004095 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004096}
4097
4098void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004099 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004100}
4101
4102void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004103 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004104}
4105
4106void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004108}
4109
4110void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004112}
4113
4114void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004116}
4117
4118void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004120}
4121
4122void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004124}
4125
Mark Mendellfe57faa2015-09-18 09:26:15 -04004126// Simple implementation of packed switch - generate cascaded compare/jumps.
4127void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4128 LocationSummary* locations =
4129 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4130 locations->SetInAt(0, Location::RequiresRegister());
4131}
4132
4133void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4134 int32_t lower_bound = switch_instr->GetStartValue();
4135 int32_t num_entries = switch_instr->GetNumEntries();
4136 LocationSummary* locations = switch_instr->GetLocations();
4137 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4138 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4139
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004140 // Create a set of compare/jumps.
4141 GpuRegister temp_reg = TMP;
4142 if (IsInt<16>(-lower_bound)) {
4143 __ Addiu(temp_reg, value_reg, -lower_bound);
4144 } else {
4145 __ LoadConst32(AT, -lower_bound);
4146 __ Addu(temp_reg, value_reg, AT);
4147 }
4148 // Jump to default if index is negative
4149 // Note: We don't check the case that index is positive while value < lower_bound, because in
4150 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4151 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4152
Mark Mendellfe57faa2015-09-18 09:26:15 -04004153 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004154 // Jump to successors[0] if value == lower_bound.
4155 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4156 int32_t last_index = 0;
4157 for (; num_entries - last_index > 2; last_index += 2) {
4158 __ Addiu(temp_reg, temp_reg, -2);
4159 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4160 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4161 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4162 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4163 }
4164 if (num_entries - last_index == 2) {
4165 // The last missing case_value.
4166 __ Addiu(temp_reg, temp_reg, -1);
4167 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004168 }
4169
4170 // And the default for any other value.
4171 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004172 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004173 }
4174}
4175
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004176void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4177 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4178}
4179
4180void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4181 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4182}
4183
Alexey Frunze4dda3372015-06-01 18:31:49 -07004184} // namespace mips64
4185} // namespace art