blob: 4e7a2728b1c568af6409067b895ca01eca77b5f5 [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
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100105// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
106#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700107#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, 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());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
304 instruction_,
305 instruction_->GetDexPc(),
306 this);
307 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700308 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700309 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700311 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700312 }
313 }
314
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700315 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700316 DCHECK(successor_ == nullptr);
317 return &return_label_;
318 }
319
Roland Levillain46648892015-06-19 16:07:18 +0100320 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
321
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700323 // If not null, the block to branch to after the suspend check.
324 HBasicBlock* const successor_;
325
326 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700327 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328
329 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
330};
331
332class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
333 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000334 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335
336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
337 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100339 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700340 DCHECK(instruction_->IsCheckCast()
341 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
342 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
343
344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, locations);
346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
352 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100353 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
355 Primitive::kPrimNot);
356
357 if (instruction_->IsInstanceOf()) {
358 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
359 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000362 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700363 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Primitive::Type ret_type = instruction_->GetType();
365 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
366 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 } else {
368 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100369 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
371 }
372
373 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700374 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 }
376
Roland Levillain46648892015-06-19 16:07:18 +0100377 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
378
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
381};
382
383class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800389 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700390 __ Bind(GetEntryLabel());
391 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800392 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
393 instruction_,
394 instruction_->GetDexPc(),
395 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000396 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 }
398
Roland Levillain46648892015-06-19 16:07:18 +0100399 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
400
Alexey Frunze4dda3372015-06-01 18:31:49 -0700401 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
403};
404
405CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
406 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100407 const CompilerOptions& compiler_options,
408 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 : CodeGenerator(graph,
410 kNumberOfGpuRegisters,
411 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000412 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700413 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
414 arraysize(kCoreCalleeSaves)),
415 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
416 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100417 compiler_options,
418 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100419 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700420 location_builder_(graph, this),
421 instruction_visitor_(graph, this),
422 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100423 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700424 isa_features_(isa_features) {
425 // Save RA (containing the return address) to mimic Quick.
426 AddAllocatedRegister(Location::RegisterLocation(RA));
427}
428
429#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100430// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
431#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700432#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700433
434void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700435 // Ensure that we fix up branches.
436 __ FinalizeCode();
437
438 // Adjust native pc offsets in stack maps.
439 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
440 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
441 uint32_t new_position = __ GetAdjustedPosition(old_position);
442 DCHECK_GE(new_position, old_position);
443 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
444 }
445
446 // Adjust pc offsets for the disassembly information.
447 if (disasm_info_ != nullptr) {
448 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
449 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
450 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
451 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
452 it.second.start = __ GetAdjustedPosition(it.second.start);
453 it.second.end = __ GetAdjustedPosition(it.second.end);
454 }
455 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
456 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
457 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
458 }
459 }
460
Alexey Frunze4dda3372015-06-01 18:31:49 -0700461 CodeGenerator::Finalize(allocator);
462}
463
464Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
465 return codegen_->GetAssembler();
466}
467
468void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100469 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700470 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
471}
472
473void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100474 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700475 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
476}
477
478void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
479 // Pop reg
480 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200481 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700482}
483
484void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
485 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200486 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700487 __ Sd(GpuRegister(reg), SP, 0);
488}
489
490void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
491 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
492 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
493 // Allocate a scratch register other than TMP, if available.
494 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
495 // automatically unspilled when the scratch scope object is destroyed).
496 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
497 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200498 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700499 __ LoadFromOffset(load_type,
500 GpuRegister(ensure_scratch.GetRegister()),
501 SP,
502 index1 + stack_offset);
503 __ LoadFromOffset(load_type,
504 TMP,
505 SP,
506 index2 + stack_offset);
507 __ StoreToOffset(store_type,
508 GpuRegister(ensure_scratch.GetRegister()),
509 SP,
510 index2 + stack_offset);
511 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
512}
513
514static dwarf::Reg DWARFReg(GpuRegister reg) {
515 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
516}
517
David Srbeckyba702002016-02-01 18:15:29 +0000518static dwarf::Reg DWARFReg(FpuRegister reg) {
519 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
520}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700521
522void CodeGeneratorMIPS64::GenerateFrameEntry() {
523 __ Bind(&frame_entry_label_);
524
525 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
526
527 if (do_overflow_check) {
528 __ LoadFromOffset(kLoadWord,
529 ZERO,
530 SP,
531 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
532 RecordPcInfo(nullptr, 0);
533 }
534
535 // TODO: anything related to T9/GP/GOT/PIC/.so's?
536
537 if (HasEmptyFrame()) {
538 return;
539 }
540
541 // Make sure the frame size isn't unreasonably large. Per the various APIs
542 // it looks like it should always be less than 2GB in size, which allows
543 // us using 32-bit signed offsets from the stack pointer.
544 if (GetFrameSize() > 0x7FFFFFFF)
545 LOG(FATAL) << "Stack frame larger than 2GB";
546
547 // Spill callee-saved registers.
548 // Note that their cumulative size is small and they can be indexed using
549 // 16-bit offsets.
550
551 // TODO: increment/decrement SP in one step instead of two or remove this comment.
552
553 uint32_t ofs = FrameEntrySpillSize();
554 __ IncreaseFrameSize(ofs);
555
556 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
557 GpuRegister reg = kCoreCalleeSaves[i];
558 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200559 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700560 __ Sd(reg, SP, ofs);
561 __ cfi().RelOffset(DWARFReg(reg), ofs);
562 }
563 }
564
565 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
566 FpuRegister reg = kFpuCalleeSaves[i];
567 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200568 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700569 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000570 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 }
572 }
573
574 // Allocate the rest of the frame and store the current method pointer
575 // at its end.
576
577 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
578
579 static_assert(IsInt<16>(kCurrentMethodStackOffset),
580 "kCurrentMethodStackOffset must fit into int16_t");
581 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
582}
583
584void CodeGeneratorMIPS64::GenerateFrameExit() {
585 __ cfi().RememberState();
586
587 // TODO: anything related to T9/GP/GOT/PIC/.so's?
588
589 if (!HasEmptyFrame()) {
590 // Deallocate the rest of the frame.
591
592 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
593
594 // Restore callee-saved registers.
595 // Note that their cumulative size is small and they can be indexed using
596 // 16-bit offsets.
597
598 // TODO: increment/decrement SP in one step instead of two or remove this comment.
599
600 uint32_t ofs = 0;
601
602 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
603 FpuRegister reg = kFpuCalleeSaves[i];
604 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
605 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200606 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000607 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700608 }
609 }
610
611 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
612 GpuRegister reg = kCoreCalleeSaves[i];
613 if (allocated_registers_.ContainsCoreRegister(reg)) {
614 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200615 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700616 __ cfi().Restore(DWARFReg(reg));
617 }
618 }
619
620 DCHECK_EQ(ofs, FrameEntrySpillSize());
621 __ DecreaseFrameSize(ofs);
622 }
623
624 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700625 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700626
627 __ cfi().RestoreState();
628 __ cfi().DefCFAOffset(GetFrameSize());
629}
630
631void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
632 __ Bind(GetLabelOf(block));
633}
634
635void CodeGeneratorMIPS64::MoveLocation(Location destination,
636 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100637 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700638 if (source.Equals(destination)) {
639 return;
640 }
641
642 // A valid move can always be inferred from the destination and source
643 // locations. When moving from and to a register, the argument type can be
644 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 DCHECK_EQ(unspecified_type, false);
647
648 if (destination.IsRegister() || destination.IsFpuRegister()) {
649 if (unspecified_type) {
650 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
651 if (source.IsStackSlot() ||
652 (src_cst != nullptr && (src_cst->IsIntConstant()
653 || src_cst->IsFloatConstant()
654 || src_cst->IsNullConstant()))) {
655 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100656 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700657 } else {
658 // If the source is a double stack slot or a 64bit constant, a 64bit
659 // type is appropriate. Else the source is a register, and since the
660 // type has not been specified, we chose a 64bit type to force a 64bit
661 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 }
664 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100665 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
666 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700667 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
668 // Move to GPR/FPR from stack
669 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 __ LoadFpuFromOffset(load_type,
672 destination.AsFpuRegister<FpuRegister>(),
673 SP,
674 source.GetStackIndex());
675 } else {
676 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
677 __ LoadFromOffset(load_type,
678 destination.AsRegister<GpuRegister>(),
679 SP,
680 source.GetStackIndex());
681 }
682 } else if (source.IsConstant()) {
683 // Move to GPR/FPR from constant
684 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100685 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 gpr = destination.AsRegister<GpuRegister>();
687 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100688 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700689 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
690 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
691 gpr = ZERO;
692 } else {
693 __ LoadConst32(gpr, value);
694 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700695 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700696 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
697 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
698 gpr = ZERO;
699 } else {
700 __ LoadConst64(gpr, value);
701 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100705 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700706 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
707 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100708 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700709 if (destination.IsRegister()) {
710 // Move to GPR from GPR
711 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
712 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 DCHECK(destination.IsFpuRegister());
714 if (Primitive::Is64BitType(dst_type)) {
715 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
716 } else {
717 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
718 }
719 }
720 } else if (source.IsFpuRegister()) {
721 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700722 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100723 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700724 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
725 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100726 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700727 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
728 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 } else {
730 DCHECK(destination.IsRegister());
731 if (Primitive::Is64BitType(dst_type)) {
732 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
733 } else {
734 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
735 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700736 }
737 }
738 } else { // The destination is not a register. It must be a stack slot.
739 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
740 if (source.IsRegister() || source.IsFpuRegister()) {
741 if (unspecified_type) {
742 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100743 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100745 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700746 }
747 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100748 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
749 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700750 // Move to stack from GPR/FPR
751 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
752 if (source.IsRegister()) {
753 __ StoreToOffset(store_type,
754 source.AsRegister<GpuRegister>(),
755 SP,
756 destination.GetStackIndex());
757 } else {
758 __ StoreFpuToOffset(store_type,
759 source.AsFpuRegister<FpuRegister>(),
760 SP,
761 destination.GetStackIndex());
762 }
763 } else if (source.IsConstant()) {
764 // Move to stack from constant
765 HConstant* src_cst = source.GetConstant();
766 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700768 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700769 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
770 if (value != 0) {
771 gpr = TMP;
772 __ LoadConst32(gpr, value);
773 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700774 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700775 DCHECK(destination.IsDoubleStackSlot());
776 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
777 if (value != 0) {
778 gpr = TMP;
779 __ LoadConst64(gpr, value);
780 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700781 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700782 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700783 } else {
784 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
785 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
786 // Move to stack from stack
787 if (destination.IsStackSlot()) {
788 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
789 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
790 } else {
791 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
792 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
793 }
794 }
795 }
796}
797
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700798void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700799 DCHECK(!loc1.IsConstant());
800 DCHECK(!loc2.IsConstant());
801
802 if (loc1.Equals(loc2)) {
803 return;
804 }
805
806 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
807 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
808 bool is_fp_reg1 = loc1.IsFpuRegister();
809 bool is_fp_reg2 = loc2.IsFpuRegister();
810
811 if (loc2.IsRegister() && loc1.IsRegister()) {
812 // Swap 2 GPRs
813 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
814 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
815 __ Move(TMP, r2);
816 __ Move(r2, r1);
817 __ Move(r1, TMP);
818 } else if (is_fp_reg2 && is_fp_reg1) {
819 // Swap 2 FPRs
820 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
821 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700822 if (type == Primitive::kPrimFloat) {
823 __ MovS(FTMP, r1);
824 __ MovS(r1, r2);
825 __ MovS(r2, FTMP);
826 } else {
827 DCHECK_EQ(type, Primitive::kPrimDouble);
828 __ MovD(FTMP, r1);
829 __ MovD(r1, r2);
830 __ MovD(r2, FTMP);
831 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700832 } else if (is_slot1 != is_slot2) {
833 // Swap GPR/FPR and stack slot
834 Location reg_loc = is_slot1 ? loc2 : loc1;
835 Location mem_loc = is_slot1 ? loc1 : loc2;
836 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
837 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
838 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
839 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
840 if (reg_loc.IsFpuRegister()) {
841 __ StoreFpuToOffset(store_type,
842 reg_loc.AsFpuRegister<FpuRegister>(),
843 SP,
844 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700845 if (mem_loc.IsStackSlot()) {
846 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
847 } else {
848 DCHECK(mem_loc.IsDoubleStackSlot());
849 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
850 }
851 } else {
852 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
853 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
854 }
855 } else if (is_slot1 && is_slot2) {
856 move_resolver_.Exchange(loc1.GetStackIndex(),
857 loc2.GetStackIndex(),
858 loc1.IsDoubleStackSlot());
859 } else {
860 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
861 }
862}
863
Calin Juravle175dc732015-08-25 15:42:32 +0100864void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
865 DCHECK(location.IsRegister());
866 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
867}
868
Calin Juravlee460d1d2015-09-29 04:52:17 +0100869void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
870 if (location.IsRegister()) {
871 locations->AddTemp(location);
872 } else {
873 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
874 }
875}
876
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100877void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
878 GpuRegister value,
879 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700880 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700881 GpuRegister card = AT;
882 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100883 if (value_can_be_null) {
884 __ Beqzc(value, &done);
885 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700886 __ LoadFromOffset(kLoadDoubleword,
887 card,
888 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700889 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700890 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
891 __ Daddu(temp, card, temp);
892 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100893 if (value_can_be_null) {
894 __ Bind(&done);
895 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700896}
897
David Brazdil58282f42016-01-14 12:45:10 +0000898void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700899 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
900 blocked_core_registers_[ZERO] = true;
901 blocked_core_registers_[K0] = true;
902 blocked_core_registers_[K1] = true;
903 blocked_core_registers_[GP] = true;
904 blocked_core_registers_[SP] = true;
905 blocked_core_registers_[RA] = true;
906
Lazar Trsicd9672662015-09-03 17:33:01 +0200907 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
908 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700909 blocked_core_registers_[AT] = true;
910 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200911 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700912 blocked_fpu_registers_[FTMP] = true;
913
914 // Reserve suspend and thread registers.
915 blocked_core_registers_[S0] = true;
916 blocked_core_registers_[TR] = true;
917
918 // Reserve T9 for function calls
919 blocked_core_registers_[T9] = true;
920
921 // TODO: review; anything else?
922
Goran Jakovljevic782be112016-06-21 12:39:04 +0200923 if (GetGraph()->IsDebuggable()) {
924 // Stubs do not save callee-save floating point registers. If the graph
925 // is debuggable, we need to deal with these registers differently. For
926 // now, just block them.
927 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
928 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
929 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700930 }
931}
932
Alexey Frunze4dda3372015-06-01 18:31:49 -0700933size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
934 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200935 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936}
937
938size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
939 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200940 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941}
942
943size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
944 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200945 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946}
947
948size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
949 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200950 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951}
952
953void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100954 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700955}
956
957void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100958 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700959}
960
Calin Juravle175dc732015-08-25 15:42:32 +0100961void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
962 HInstruction* instruction,
963 uint32_t dex_pc,
964 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700965 InvokeRuntime(GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100966 instruction,
967 dex_pc,
968 slow_path);
969}
970
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
972 HInstruction* instruction,
973 uint32_t dex_pc,
974 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100975 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 // TODO: anything related to T9/GP/GOT/PIC/.so's?
977 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
978 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700979 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700980 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700981}
982
983void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
984 GpuRegister class_reg) {
985 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
986 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
987 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
988 // TODO: barrier needed?
989 __ Bind(slow_path->GetExitLabel());
990}
991
992void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
993 __ Sync(0); // only stype 0 is supported
994}
995
996void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
997 HBasicBlock* successor) {
998 SuspendCheckSlowPathMIPS64* slow_path =
999 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1000 codegen_->AddSlowPath(slow_path);
1001
1002 __ LoadFromOffset(kLoadUnsignedHalfword,
1003 TMP,
1004 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001005 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001006 if (successor == nullptr) {
1007 __ Bnezc(TMP, slow_path->GetEntryLabel());
1008 __ Bind(slow_path->GetReturnLabel());
1009 } else {
1010 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001011 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001012 // slow_path will return to GetLabelOf(successor).
1013 }
1014}
1015
1016InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1017 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001018 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001019 assembler_(codegen->GetAssembler()),
1020 codegen_(codegen) {}
1021
1022void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1023 DCHECK_EQ(instruction->InputCount(), 2U);
1024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1025 Primitive::Type type = instruction->GetResultType();
1026 switch (type) {
1027 case Primitive::kPrimInt:
1028 case Primitive::kPrimLong: {
1029 locations->SetInAt(0, Location::RequiresRegister());
1030 HInstruction* right = instruction->InputAt(1);
1031 bool can_use_imm = false;
1032 if (right->IsConstant()) {
1033 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1034 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1035 can_use_imm = IsUint<16>(imm);
1036 } else if (instruction->IsAdd()) {
1037 can_use_imm = IsInt<16>(imm);
1038 } else {
1039 DCHECK(instruction->IsSub());
1040 can_use_imm = IsInt<16>(-imm);
1041 }
1042 }
1043 if (can_use_imm)
1044 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1045 else
1046 locations->SetInAt(1, Location::RequiresRegister());
1047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1048 }
1049 break;
1050
1051 case Primitive::kPrimFloat:
1052 case Primitive::kPrimDouble:
1053 locations->SetInAt(0, Location::RequiresFpuRegister());
1054 locations->SetInAt(1, Location::RequiresFpuRegister());
1055 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1056 break;
1057
1058 default:
1059 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1060 }
1061}
1062
1063void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1064 Primitive::Type type = instruction->GetType();
1065 LocationSummary* locations = instruction->GetLocations();
1066
1067 switch (type) {
1068 case Primitive::kPrimInt:
1069 case Primitive::kPrimLong: {
1070 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1071 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1072 Location rhs_location = locations->InAt(1);
1073
1074 GpuRegister rhs_reg = ZERO;
1075 int64_t rhs_imm = 0;
1076 bool use_imm = rhs_location.IsConstant();
1077 if (use_imm) {
1078 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1079 } else {
1080 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1081 }
1082
1083 if (instruction->IsAnd()) {
1084 if (use_imm)
1085 __ Andi(dst, lhs, rhs_imm);
1086 else
1087 __ And(dst, lhs, rhs_reg);
1088 } else if (instruction->IsOr()) {
1089 if (use_imm)
1090 __ Ori(dst, lhs, rhs_imm);
1091 else
1092 __ Or(dst, lhs, rhs_reg);
1093 } else if (instruction->IsXor()) {
1094 if (use_imm)
1095 __ Xori(dst, lhs, rhs_imm);
1096 else
1097 __ Xor(dst, lhs, rhs_reg);
1098 } else if (instruction->IsAdd()) {
1099 if (type == Primitive::kPrimInt) {
1100 if (use_imm)
1101 __ Addiu(dst, lhs, rhs_imm);
1102 else
1103 __ Addu(dst, lhs, rhs_reg);
1104 } else {
1105 if (use_imm)
1106 __ Daddiu(dst, lhs, rhs_imm);
1107 else
1108 __ Daddu(dst, lhs, rhs_reg);
1109 }
1110 } else {
1111 DCHECK(instruction->IsSub());
1112 if (type == Primitive::kPrimInt) {
1113 if (use_imm)
1114 __ Addiu(dst, lhs, -rhs_imm);
1115 else
1116 __ Subu(dst, lhs, rhs_reg);
1117 } else {
1118 if (use_imm)
1119 __ Daddiu(dst, lhs, -rhs_imm);
1120 else
1121 __ Dsubu(dst, lhs, rhs_reg);
1122 }
1123 }
1124 break;
1125 }
1126 case Primitive::kPrimFloat:
1127 case Primitive::kPrimDouble: {
1128 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1129 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1130 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1131 if (instruction->IsAdd()) {
1132 if (type == Primitive::kPrimFloat)
1133 __ AddS(dst, lhs, rhs);
1134 else
1135 __ AddD(dst, lhs, rhs);
1136 } else if (instruction->IsSub()) {
1137 if (type == Primitive::kPrimFloat)
1138 __ SubS(dst, lhs, rhs);
1139 else
1140 __ SubD(dst, lhs, rhs);
1141 } else {
1142 LOG(FATAL) << "Unexpected floating-point binary operation";
1143 }
1144 break;
1145 }
1146 default:
1147 LOG(FATAL) << "Unexpected binary operation type " << type;
1148 }
1149}
1150
1151void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001152 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001153
1154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1155 Primitive::Type type = instr->GetResultType();
1156 switch (type) {
1157 case Primitive::kPrimInt:
1158 case Primitive::kPrimLong: {
1159 locations->SetInAt(0, Location::RequiresRegister());
1160 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001162 break;
1163 }
1164 default:
1165 LOG(FATAL) << "Unexpected shift type " << type;
1166 }
1167}
1168
1169void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001170 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001171 LocationSummary* locations = instr->GetLocations();
1172 Primitive::Type type = instr->GetType();
1173
1174 switch (type) {
1175 case Primitive::kPrimInt:
1176 case Primitive::kPrimLong: {
1177 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1178 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1179 Location rhs_location = locations->InAt(1);
1180
1181 GpuRegister rhs_reg = ZERO;
1182 int64_t rhs_imm = 0;
1183 bool use_imm = rhs_location.IsConstant();
1184 if (use_imm) {
1185 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1186 } else {
1187 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1188 }
1189
1190 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001191 uint32_t shift_value = rhs_imm &
1192 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001193
Alexey Frunze92d90602015-12-18 18:16:36 -08001194 if (shift_value == 0) {
1195 if (dst != lhs) {
1196 __ Move(dst, lhs);
1197 }
1198 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 if (instr->IsShl()) {
1200 __ Sll(dst, lhs, shift_value);
1201 } else if (instr->IsShr()) {
1202 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001203 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001205 } else {
1206 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001207 }
1208 } else {
1209 if (shift_value < 32) {
1210 if (instr->IsShl()) {
1211 __ Dsll(dst, lhs, shift_value);
1212 } else if (instr->IsShr()) {
1213 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001214 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001215 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001216 } else {
1217 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218 }
1219 } else {
1220 shift_value -= 32;
1221 if (instr->IsShl()) {
1222 __ Dsll32(dst, lhs, shift_value);
1223 } else if (instr->IsShr()) {
1224 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001225 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001226 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001227 } else {
1228 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 }
1230 }
1231 }
1232 } else {
1233 if (type == Primitive::kPrimInt) {
1234 if (instr->IsShl()) {
1235 __ Sllv(dst, lhs, rhs_reg);
1236 } else if (instr->IsShr()) {
1237 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001238 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001240 } else {
1241 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001242 }
1243 } else {
1244 if (instr->IsShl()) {
1245 __ Dsllv(dst, lhs, rhs_reg);
1246 } else if (instr->IsShr()) {
1247 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001248 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001249 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001250 } else {
1251 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001252 }
1253 }
1254 }
1255 break;
1256 }
1257 default:
1258 LOG(FATAL) << "Unexpected shift operation type " << type;
1259 }
1260}
1261
1262void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1263 HandleBinaryOp(instruction);
1264}
1265
1266void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1267 HandleBinaryOp(instruction);
1268}
1269
1270void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1271 HandleBinaryOp(instruction);
1272}
1273
1274void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1275 HandleBinaryOp(instruction);
1276}
1277
1278void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1279 LocationSummary* locations =
1280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1281 locations->SetInAt(0, Location::RequiresRegister());
1282 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1283 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1285 } else {
1286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1287 }
1288}
1289
1290void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1291 LocationSummary* locations = instruction->GetLocations();
1292 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1293 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001294 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001295
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001296 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001297 switch (type) {
1298 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001299 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1300 if (index.IsConstant()) {
1301 size_t offset =
1302 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1303 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1304 } else {
1305 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1306 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1307 }
1308 break;
1309 }
1310
1311 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001312 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1313 if (index.IsConstant()) {
1314 size_t offset =
1315 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1316 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1317 } else {
1318 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1319 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1320 }
1321 break;
1322 }
1323
1324 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001325 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1326 if (index.IsConstant()) {
1327 size_t offset =
1328 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1329 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1330 } else {
1331 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1332 __ Daddu(TMP, obj, TMP);
1333 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1334 }
1335 break;
1336 }
1337
1338 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001339 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1340 if (index.IsConstant()) {
1341 size_t offset =
1342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1343 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1344 } else {
1345 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1346 __ Daddu(TMP, obj, TMP);
1347 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1348 }
1349 break;
1350 }
1351
1352 case Primitive::kPrimInt:
1353 case Primitive::kPrimNot: {
1354 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001355 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1356 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1357 if (index.IsConstant()) {
1358 size_t offset =
1359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1360 __ LoadFromOffset(load_type, out, obj, offset);
1361 } else {
1362 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1363 __ Daddu(TMP, obj, TMP);
1364 __ LoadFromOffset(load_type, out, TMP, data_offset);
1365 }
1366 break;
1367 }
1368
1369 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001370 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1371 if (index.IsConstant()) {
1372 size_t offset =
1373 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1374 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1375 } else {
1376 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1377 __ Daddu(TMP, obj, TMP);
1378 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1379 }
1380 break;
1381 }
1382
1383 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001384 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1385 if (index.IsConstant()) {
1386 size_t offset =
1387 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1388 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1389 } else {
1390 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1391 __ Daddu(TMP, obj, TMP);
1392 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001398 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1399 if (index.IsConstant()) {
1400 size_t offset =
1401 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1402 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1403 } else {
1404 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1405 __ Daddu(TMP, obj, TMP);
1406 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1407 }
1408 break;
1409 }
1410
1411 case Primitive::kPrimVoid:
1412 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1413 UNREACHABLE();
1414 }
1415 codegen_->MaybeRecordImplicitNullCheck(instruction);
1416}
1417
1418void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1422}
1423
1424void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1425 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001426 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001427 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1428 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1429 __ LoadFromOffset(kLoadWord, out, obj, offset);
1430 codegen_->MaybeRecordImplicitNullCheck(instruction);
1431}
1432
1433void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001434 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1436 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001437 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001438 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001439 InvokeRuntimeCallingConvention calling_convention;
1440 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1441 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1442 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1443 } else {
1444 locations->SetInAt(0, Location::RequiresRegister());
1445 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1446 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1447 locations->SetInAt(2, Location::RequiresFpuRegister());
1448 } else {
1449 locations->SetInAt(2, Location::RequiresRegister());
1450 }
1451 }
1452}
1453
1454void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1455 LocationSummary* locations = instruction->GetLocations();
1456 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1457 Location index = locations->InAt(1);
1458 Primitive::Type value_type = instruction->GetComponentType();
1459 bool needs_runtime_call = locations->WillCall();
1460 bool needs_write_barrier =
1461 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1462
1463 switch (value_type) {
1464 case Primitive::kPrimBoolean:
1465 case Primitive::kPrimByte: {
1466 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1467 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1468 if (index.IsConstant()) {
1469 size_t offset =
1470 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1471 __ StoreToOffset(kStoreByte, value, obj, offset);
1472 } else {
1473 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1474 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1475 }
1476 break;
1477 }
1478
1479 case Primitive::kPrimShort:
1480 case Primitive::kPrimChar: {
1481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1482 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1483 if (index.IsConstant()) {
1484 size_t offset =
1485 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1486 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1487 } else {
1488 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1489 __ Daddu(TMP, obj, TMP);
1490 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1491 }
1492 break;
1493 }
1494
1495 case Primitive::kPrimInt:
1496 case Primitive::kPrimNot: {
1497 if (!needs_runtime_call) {
1498 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1499 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1500 if (index.IsConstant()) {
1501 size_t offset =
1502 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1503 __ StoreToOffset(kStoreWord, value, obj, offset);
1504 } else {
1505 DCHECK(index.IsRegister()) << index;
1506 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1507 __ Daddu(TMP, obj, TMP);
1508 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1509 }
1510 codegen_->MaybeRecordImplicitNullCheck(instruction);
1511 if (needs_write_barrier) {
1512 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001513 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001514 }
1515 } else {
1516 DCHECK_EQ(value_type, Primitive::kPrimNot);
1517 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1518 instruction,
1519 instruction->GetDexPc(),
1520 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001521 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001522 }
1523 break;
1524 }
1525
1526 case Primitive::kPrimLong: {
1527 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1528 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1529 if (index.IsConstant()) {
1530 size_t offset =
1531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1532 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1533 } else {
1534 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1535 __ Daddu(TMP, obj, TMP);
1536 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1537 }
1538 break;
1539 }
1540
1541 case Primitive::kPrimFloat: {
1542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1543 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1544 DCHECK(locations->InAt(2).IsFpuRegister());
1545 if (index.IsConstant()) {
1546 size_t offset =
1547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1548 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1549 } else {
1550 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1551 __ Daddu(TMP, obj, TMP);
1552 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1553 }
1554 break;
1555 }
1556
1557 case Primitive::kPrimDouble: {
1558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1559 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1560 DCHECK(locations->InAt(2).IsFpuRegister());
1561 if (index.IsConstant()) {
1562 size_t offset =
1563 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1564 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1565 } else {
1566 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1567 __ Daddu(TMP, obj, TMP);
1568 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1569 }
1570 break;
1571 }
1572
1573 case Primitive::kPrimVoid:
1574 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1575 UNREACHABLE();
1576 }
1577
1578 // Ints and objects are handled in the switch.
1579 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1580 codegen_->MaybeRecordImplicitNullCheck(instruction);
1581 }
1582}
1583
1584void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001585 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1586 ? LocationSummary::kCallOnSlowPath
1587 : LocationSummary::kNoCall;
1588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001589 locations->SetInAt(0, Location::RequiresRegister());
1590 locations->SetInAt(1, Location::RequiresRegister());
1591 if (instruction->HasUses()) {
1592 locations->SetOut(Location::SameAsFirstInput());
1593 }
1594}
1595
1596void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1597 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001598 BoundsCheckSlowPathMIPS64* slow_path =
1599 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001600 codegen_->AddSlowPath(slow_path);
1601
1602 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1603 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1604
1605 // length is limited by the maximum positive signed 32-bit integer.
1606 // Unsigned comparison of length and index checks for index < 0
1607 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001608 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001609}
1610
1611void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1612 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1613 instruction,
1614 LocationSummary::kCallOnSlowPath);
1615 locations->SetInAt(0, Location::RequiresRegister());
1616 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001617 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001618 locations->AddTemp(Location::RequiresRegister());
1619}
1620
1621void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1622 LocationSummary* locations = instruction->GetLocations();
1623 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1624 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1625 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1626
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001627 SlowPathCodeMIPS64* slow_path =
1628 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001629 codegen_->AddSlowPath(slow_path);
1630
1631 // TODO: avoid this check if we know obj is not null.
1632 __ Beqzc(obj, slow_path->GetExitLabel());
1633 // Compare the class of `obj` with `cls`.
1634 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1635 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1636 __ Bind(slow_path->GetExitLabel());
1637}
1638
1639void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1640 LocationSummary* locations =
1641 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1642 locations->SetInAt(0, Location::RequiresRegister());
1643 if (check->HasUses()) {
1644 locations->SetOut(Location::SameAsFirstInput());
1645 }
1646}
1647
1648void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1649 // We assume the class is not null.
1650 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1651 check->GetLoadClass(),
1652 check,
1653 check->GetDexPc(),
1654 true);
1655 codegen_->AddSlowPath(slow_path);
1656 GenerateClassInitializationCheck(slow_path,
1657 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1658}
1659
1660void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1661 Primitive::Type in_type = compare->InputAt(0)->GetType();
1662
Alexey Frunze299a9392015-12-08 16:08:02 -08001663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001664
1665 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001666 case Primitive::kPrimBoolean:
1667 case Primitive::kPrimByte:
1668 case Primitive::kPrimShort:
1669 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001670 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001671 case Primitive::kPrimLong:
1672 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001673 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1675 break;
1676
1677 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001678 case Primitive::kPrimDouble:
1679 locations->SetInAt(0, Location::RequiresFpuRegister());
1680 locations->SetInAt(1, Location::RequiresFpuRegister());
1681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001682 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001683
1684 default:
1685 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1686 }
1687}
1688
1689void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1690 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001691 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1693
1694 // 0 if: left == right
1695 // 1 if: left > right
1696 // -1 if: left < right
1697 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001698 case Primitive::kPrimBoolean:
1699 case Primitive::kPrimByte:
1700 case Primitive::kPrimShort:
1701 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001702 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001703 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001705 Location rhs_location = locations->InAt(1);
1706 bool use_imm = rhs_location.IsConstant();
1707 GpuRegister rhs = ZERO;
1708 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001709 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001710 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1711 if (value != 0) {
1712 rhs = AT;
1713 __ LoadConst64(rhs, value);
1714 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001715 } else {
1716 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1717 if (value != 0) {
1718 rhs = AT;
1719 __ LoadConst32(rhs, value);
1720 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001721 }
1722 } else {
1723 rhs = rhs_location.AsRegister<GpuRegister>();
1724 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001725 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001726 __ Slt(res, rhs, lhs);
1727 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728 break;
1729 }
1730
Alexey Frunze299a9392015-12-08 16:08:02 -08001731 case Primitive::kPrimFloat: {
1732 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1733 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1734 Mips64Label done;
1735 __ CmpEqS(FTMP, lhs, rhs);
1736 __ LoadConst32(res, 0);
1737 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001738 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001739 __ CmpLtS(FTMP, lhs, rhs);
1740 __ LoadConst32(res, -1);
1741 __ Bc1nez(FTMP, &done);
1742 __ LoadConst32(res, 1);
1743 } else {
1744 __ CmpLtS(FTMP, rhs, lhs);
1745 __ LoadConst32(res, 1);
1746 __ Bc1nez(FTMP, &done);
1747 __ LoadConst32(res, -1);
1748 }
1749 __ Bind(&done);
1750 break;
1751 }
1752
Alexey Frunze4dda3372015-06-01 18:31:49 -07001753 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001754 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1755 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1756 Mips64Label done;
1757 __ CmpEqD(FTMP, lhs, rhs);
1758 __ LoadConst32(res, 0);
1759 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001760 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001761 __ CmpLtD(FTMP, lhs, rhs);
1762 __ LoadConst32(res, -1);
1763 __ Bc1nez(FTMP, &done);
1764 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001765 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001766 __ CmpLtD(FTMP, rhs, lhs);
1767 __ LoadConst32(res, 1);
1768 __ Bc1nez(FTMP, &done);
1769 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001770 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001771 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 break;
1773 }
1774
1775 default:
1776 LOG(FATAL) << "Unimplemented compare type " << in_type;
1777 }
1778}
1779
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001781 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001782 switch (instruction->InputAt(0)->GetType()) {
1783 default:
1784 case Primitive::kPrimLong:
1785 locations->SetInAt(0, Location::RequiresRegister());
1786 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1787 break;
1788
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble:
1791 locations->SetInAt(0, Location::RequiresFpuRegister());
1792 locations->SetInAt(1, Location::RequiresFpuRegister());
1793 break;
1794 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001795 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001796 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1797 }
1798}
1799
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001801 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001802 return;
1803 }
1804
Alexey Frunze299a9392015-12-08 16:08:02 -08001805 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001806 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001807 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001808 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809
Alexey Frunze299a9392015-12-08 16:08:02 -08001810 switch (type) {
1811 default:
1812 // Integer case.
1813 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1814 return;
1815 case Primitive::kPrimLong:
1816 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1817 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001818
Alexey Frunze299a9392015-12-08 16:08:02 -08001819 case Primitive::kPrimFloat:
1820 case Primitive::kPrimDouble:
1821 // TODO: don't use branches.
1822 GenerateFpCompareAndBranch(instruction->GetCondition(),
1823 instruction->IsGtBias(),
1824 type,
1825 locations,
1826 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001827 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001828 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001829
1830 // Convert the branches into the result.
1831 Mips64Label done;
1832
1833 // False case: result = 0.
1834 __ LoadConst32(dst, 0);
1835 __ Bc(&done);
1836
1837 // True case: result = 1.
1838 __ Bind(&true_label);
1839 __ LoadConst32(dst, 1);
1840 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001841}
1842
Alexey Frunzec857c742015-09-23 15:12:39 -07001843void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1844 DCHECK(instruction->IsDiv() || instruction->IsRem());
1845 Primitive::Type type = instruction->GetResultType();
1846
1847 LocationSummary* locations = instruction->GetLocations();
1848 Location second = locations->InAt(1);
1849 DCHECK(second.IsConstant());
1850
1851 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1852 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1853 int64_t imm = Int64FromConstant(second.GetConstant());
1854 DCHECK(imm == 1 || imm == -1);
1855
1856 if (instruction->IsRem()) {
1857 __ Move(out, ZERO);
1858 } else {
1859 if (imm == -1) {
1860 if (type == Primitive::kPrimInt) {
1861 __ Subu(out, ZERO, dividend);
1862 } else {
1863 DCHECK_EQ(type, Primitive::kPrimLong);
1864 __ Dsubu(out, ZERO, dividend);
1865 }
1866 } else if (out != dividend) {
1867 __ Move(out, dividend);
1868 }
1869 }
1870}
1871
1872void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1873 DCHECK(instruction->IsDiv() || instruction->IsRem());
1874 Primitive::Type type = instruction->GetResultType();
1875
1876 LocationSummary* locations = instruction->GetLocations();
1877 Location second = locations->InAt(1);
1878 DCHECK(second.IsConstant());
1879
1880 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1881 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1882 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001883 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001884 int ctz_imm = CTZ(abs_imm);
1885
1886 if (instruction->IsDiv()) {
1887 if (type == Primitive::kPrimInt) {
1888 if (ctz_imm == 1) {
1889 // Fast path for division by +/-2, which is very common.
1890 __ Srl(TMP, dividend, 31);
1891 } else {
1892 __ Sra(TMP, dividend, 31);
1893 __ Srl(TMP, TMP, 32 - ctz_imm);
1894 }
1895 __ Addu(out, dividend, TMP);
1896 __ Sra(out, out, ctz_imm);
1897 if (imm < 0) {
1898 __ Subu(out, ZERO, out);
1899 }
1900 } else {
1901 DCHECK_EQ(type, Primitive::kPrimLong);
1902 if (ctz_imm == 1) {
1903 // Fast path for division by +/-2, which is very common.
1904 __ Dsrl32(TMP, dividend, 31);
1905 } else {
1906 __ Dsra32(TMP, dividend, 31);
1907 if (ctz_imm > 32) {
1908 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1909 } else {
1910 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1911 }
1912 }
1913 __ Daddu(out, dividend, TMP);
1914 if (ctz_imm < 32) {
1915 __ Dsra(out, out, ctz_imm);
1916 } else {
1917 __ Dsra32(out, out, ctz_imm - 32);
1918 }
1919 if (imm < 0) {
1920 __ Dsubu(out, ZERO, out);
1921 }
1922 }
1923 } else {
1924 if (type == Primitive::kPrimInt) {
1925 if (ctz_imm == 1) {
1926 // Fast path for modulo +/-2, which is very common.
1927 __ Sra(TMP, dividend, 31);
1928 __ Subu(out, dividend, TMP);
1929 __ Andi(out, out, 1);
1930 __ Addu(out, out, TMP);
1931 } else {
1932 __ Sra(TMP, dividend, 31);
1933 __ Srl(TMP, TMP, 32 - ctz_imm);
1934 __ Addu(out, dividend, TMP);
1935 if (IsUint<16>(abs_imm - 1)) {
1936 __ Andi(out, out, abs_imm - 1);
1937 } else {
1938 __ Sll(out, out, 32 - ctz_imm);
1939 __ Srl(out, out, 32 - ctz_imm);
1940 }
1941 __ Subu(out, out, TMP);
1942 }
1943 } else {
1944 DCHECK_EQ(type, Primitive::kPrimLong);
1945 if (ctz_imm == 1) {
1946 // Fast path for modulo +/-2, which is very common.
1947 __ Dsra32(TMP, dividend, 31);
1948 __ Dsubu(out, dividend, TMP);
1949 __ Andi(out, out, 1);
1950 __ Daddu(out, out, TMP);
1951 } else {
1952 __ Dsra32(TMP, dividend, 31);
1953 if (ctz_imm > 32) {
1954 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1955 } else {
1956 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1957 }
1958 __ Daddu(out, dividend, TMP);
1959 if (IsUint<16>(abs_imm - 1)) {
1960 __ Andi(out, out, abs_imm - 1);
1961 } else {
1962 if (ctz_imm > 32) {
1963 __ Dsll(out, out, 64 - ctz_imm);
1964 __ Dsrl(out, out, 64 - ctz_imm);
1965 } else {
1966 __ Dsll32(out, out, 32 - ctz_imm);
1967 __ Dsrl32(out, out, 32 - ctz_imm);
1968 }
1969 }
1970 __ Dsubu(out, out, TMP);
1971 }
1972 }
1973 }
1974}
1975
1976void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1977 DCHECK(instruction->IsDiv() || instruction->IsRem());
1978
1979 LocationSummary* locations = instruction->GetLocations();
1980 Location second = locations->InAt(1);
1981 DCHECK(second.IsConstant());
1982
1983 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1984 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1985 int64_t imm = Int64FromConstant(second.GetConstant());
1986
1987 Primitive::Type type = instruction->GetResultType();
1988 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1989
1990 int64_t magic;
1991 int shift;
1992 CalculateMagicAndShiftForDivRem(imm,
1993 (type == Primitive::kPrimLong),
1994 &magic,
1995 &shift);
1996
1997 if (type == Primitive::kPrimInt) {
1998 __ LoadConst32(TMP, magic);
1999 __ MuhR6(TMP, dividend, TMP);
2000
2001 if (imm > 0 && magic < 0) {
2002 __ Addu(TMP, TMP, dividend);
2003 } else if (imm < 0 && magic > 0) {
2004 __ Subu(TMP, TMP, dividend);
2005 }
2006
2007 if (shift != 0) {
2008 __ Sra(TMP, TMP, shift);
2009 }
2010
2011 if (instruction->IsDiv()) {
2012 __ Sra(out, TMP, 31);
2013 __ Subu(out, TMP, out);
2014 } else {
2015 __ Sra(AT, TMP, 31);
2016 __ Subu(AT, TMP, AT);
2017 __ LoadConst32(TMP, imm);
2018 __ MulR6(TMP, AT, TMP);
2019 __ Subu(out, dividend, TMP);
2020 }
2021 } else {
2022 __ LoadConst64(TMP, magic);
2023 __ Dmuh(TMP, dividend, TMP);
2024
2025 if (imm > 0 && magic < 0) {
2026 __ Daddu(TMP, TMP, dividend);
2027 } else if (imm < 0 && magic > 0) {
2028 __ Dsubu(TMP, TMP, dividend);
2029 }
2030
2031 if (shift >= 32) {
2032 __ Dsra32(TMP, TMP, shift - 32);
2033 } else if (shift > 0) {
2034 __ Dsra(TMP, TMP, shift);
2035 }
2036
2037 if (instruction->IsDiv()) {
2038 __ Dsra32(out, TMP, 31);
2039 __ Dsubu(out, TMP, out);
2040 } else {
2041 __ Dsra32(AT, TMP, 31);
2042 __ Dsubu(AT, TMP, AT);
2043 __ LoadConst64(TMP, imm);
2044 __ Dmul(TMP, AT, TMP);
2045 __ Dsubu(out, dividend, TMP);
2046 }
2047 }
2048}
2049
2050void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2051 DCHECK(instruction->IsDiv() || instruction->IsRem());
2052 Primitive::Type type = instruction->GetResultType();
2053 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2054
2055 LocationSummary* locations = instruction->GetLocations();
2056 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2057 Location second = locations->InAt(1);
2058
2059 if (second.IsConstant()) {
2060 int64_t imm = Int64FromConstant(second.GetConstant());
2061 if (imm == 0) {
2062 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2063 } else if (imm == 1 || imm == -1) {
2064 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002065 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002066 DivRemByPowerOfTwo(instruction);
2067 } else {
2068 DCHECK(imm <= -2 || imm >= 2);
2069 GenerateDivRemWithAnyConstant(instruction);
2070 }
2071 } else {
2072 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2073 GpuRegister divisor = second.AsRegister<GpuRegister>();
2074 if (instruction->IsDiv()) {
2075 if (type == Primitive::kPrimInt)
2076 __ DivR6(out, dividend, divisor);
2077 else
2078 __ Ddiv(out, dividend, divisor);
2079 } else {
2080 if (type == Primitive::kPrimInt)
2081 __ ModR6(out, dividend, divisor);
2082 else
2083 __ Dmod(out, dividend, divisor);
2084 }
2085 }
2086}
2087
Alexey Frunze4dda3372015-06-01 18:31:49 -07002088void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2089 LocationSummary* locations =
2090 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2091 switch (div->GetResultType()) {
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimLong:
2094 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002095 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2097 break;
2098
2099 case Primitive::kPrimFloat:
2100 case Primitive::kPrimDouble:
2101 locations->SetInAt(0, Location::RequiresFpuRegister());
2102 locations->SetInAt(1, Location::RequiresFpuRegister());
2103 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2104 break;
2105
2106 default:
2107 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2108 }
2109}
2110
2111void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2112 Primitive::Type type = instruction->GetType();
2113 LocationSummary* locations = instruction->GetLocations();
2114
2115 switch (type) {
2116 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002117 case Primitive::kPrimLong:
2118 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002119 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002120 case Primitive::kPrimFloat:
2121 case Primitive::kPrimDouble: {
2122 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2123 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2124 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2125 if (type == Primitive::kPrimFloat)
2126 __ DivS(dst, lhs, rhs);
2127 else
2128 __ DivD(dst, lhs, rhs);
2129 break;
2130 }
2131 default:
2132 LOG(FATAL) << "Unexpected div type " << type;
2133 }
2134}
2135
2136void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002137 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2138 ? LocationSummary::kCallOnSlowPath
2139 : LocationSummary::kNoCall;
2140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002141 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2142 if (instruction->HasUses()) {
2143 locations->SetOut(Location::SameAsFirstInput());
2144 }
2145}
2146
2147void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2148 SlowPathCodeMIPS64* slow_path =
2149 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2150 codegen_->AddSlowPath(slow_path);
2151 Location value = instruction->GetLocations()->InAt(0);
2152
2153 Primitive::Type type = instruction->GetType();
2154
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002155 if (!Primitive::IsIntegralType(type)) {
2156 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002157 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002158 }
2159
2160 if (value.IsConstant()) {
2161 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2162 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002163 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002164 } else {
2165 // A division by a non-null constant is valid. We don't need to perform
2166 // any check, so simply fall through.
2167 }
2168 } else {
2169 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2170 }
2171}
2172
2173void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2176 locations->SetOut(Location::ConstantLocation(constant));
2177}
2178
2179void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2180 // Will be generated at use site.
2181}
2182
2183void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2184 exit->SetLocations(nullptr);
2185}
2186
2187void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2188}
2189
2190void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2191 LocationSummary* locations =
2192 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2193 locations->SetOut(Location::ConstantLocation(constant));
2194}
2195
2196void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2197 // Will be generated at use site.
2198}
2199
David Brazdilfc6a86a2015-06-26 10:33:45 +00002200void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002201 DCHECK(!successor->IsExitBlock());
2202 HBasicBlock* block = got->GetBlock();
2203 HInstruction* previous = got->GetPrevious();
2204 HLoopInformation* info = block->GetLoopInformation();
2205
2206 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2207 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2208 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2209 return;
2210 }
2211 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2212 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2213 }
2214 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002215 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002216 }
2217}
2218
David Brazdilfc6a86a2015-06-26 10:33:45 +00002219void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2220 got->SetLocations(nullptr);
2221}
2222
2223void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2224 HandleGoto(got, got->GetSuccessor());
2225}
2226
2227void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2228 try_boundary->SetLocations(nullptr);
2229}
2230
2231void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2232 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2233 if (!successor->IsExitBlock()) {
2234 HandleGoto(try_boundary, successor);
2235 }
2236}
2237
Alexey Frunze299a9392015-12-08 16:08:02 -08002238void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2239 bool is64bit,
2240 LocationSummary* locations) {
2241 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2242 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2243 Location rhs_location = locations->InAt(1);
2244 GpuRegister rhs_reg = ZERO;
2245 int64_t rhs_imm = 0;
2246 bool use_imm = rhs_location.IsConstant();
2247 if (use_imm) {
2248 if (is64bit) {
2249 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2250 } else {
2251 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2252 }
2253 } else {
2254 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2255 }
2256 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2257
2258 switch (cond) {
2259 case kCondEQ:
2260 case kCondNE:
2261 if (use_imm && IsUint<16>(rhs_imm)) {
2262 __ Xori(dst, lhs, rhs_imm);
2263 } else {
2264 if (use_imm) {
2265 rhs_reg = TMP;
2266 __ LoadConst64(rhs_reg, rhs_imm);
2267 }
2268 __ Xor(dst, lhs, rhs_reg);
2269 }
2270 if (cond == kCondEQ) {
2271 __ Sltiu(dst, dst, 1);
2272 } else {
2273 __ Sltu(dst, ZERO, dst);
2274 }
2275 break;
2276
2277 case kCondLT:
2278 case kCondGE:
2279 if (use_imm && IsInt<16>(rhs_imm)) {
2280 __ Slti(dst, lhs, rhs_imm);
2281 } else {
2282 if (use_imm) {
2283 rhs_reg = TMP;
2284 __ LoadConst64(rhs_reg, rhs_imm);
2285 }
2286 __ Slt(dst, lhs, rhs_reg);
2287 }
2288 if (cond == kCondGE) {
2289 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2290 // only the slt instruction but no sge.
2291 __ Xori(dst, dst, 1);
2292 }
2293 break;
2294
2295 case kCondLE:
2296 case kCondGT:
2297 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2298 // Simulate lhs <= rhs via lhs < rhs + 1.
2299 __ Slti(dst, lhs, rhs_imm_plus_one);
2300 if (cond == kCondGT) {
2301 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2302 // only the slti instruction but no sgti.
2303 __ Xori(dst, dst, 1);
2304 }
2305 } else {
2306 if (use_imm) {
2307 rhs_reg = TMP;
2308 __ LoadConst64(rhs_reg, rhs_imm);
2309 }
2310 __ Slt(dst, rhs_reg, lhs);
2311 if (cond == kCondLE) {
2312 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2313 // only the slt instruction but no sle.
2314 __ Xori(dst, dst, 1);
2315 }
2316 }
2317 break;
2318
2319 case kCondB:
2320 case kCondAE:
2321 if (use_imm && IsInt<16>(rhs_imm)) {
2322 // Sltiu sign-extends its 16-bit immediate operand before
2323 // the comparison and thus lets us compare directly with
2324 // unsigned values in the ranges [0, 0x7fff] and
2325 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2326 __ Sltiu(dst, lhs, rhs_imm);
2327 } else {
2328 if (use_imm) {
2329 rhs_reg = TMP;
2330 __ LoadConst64(rhs_reg, rhs_imm);
2331 }
2332 __ Sltu(dst, lhs, rhs_reg);
2333 }
2334 if (cond == kCondAE) {
2335 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2336 // only the sltu instruction but no sgeu.
2337 __ Xori(dst, dst, 1);
2338 }
2339 break;
2340
2341 case kCondBE:
2342 case kCondA:
2343 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2344 // Simulate lhs <= rhs via lhs < rhs + 1.
2345 // Note that this only works if rhs + 1 does not overflow
2346 // to 0, hence the check above.
2347 // Sltiu sign-extends its 16-bit immediate operand before
2348 // the comparison and thus lets us compare directly with
2349 // unsigned values in the ranges [0, 0x7fff] and
2350 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2351 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2352 if (cond == kCondA) {
2353 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2354 // only the sltiu instruction but no sgtiu.
2355 __ Xori(dst, dst, 1);
2356 }
2357 } else {
2358 if (use_imm) {
2359 rhs_reg = TMP;
2360 __ LoadConst64(rhs_reg, rhs_imm);
2361 }
2362 __ Sltu(dst, rhs_reg, lhs);
2363 if (cond == kCondBE) {
2364 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2365 // only the sltu instruction but no sleu.
2366 __ Xori(dst, dst, 1);
2367 }
2368 }
2369 break;
2370 }
2371}
2372
2373void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2374 bool is64bit,
2375 LocationSummary* locations,
2376 Mips64Label* label) {
2377 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2378 Location rhs_location = locations->InAt(1);
2379 GpuRegister rhs_reg = ZERO;
2380 int64_t rhs_imm = 0;
2381 bool use_imm = rhs_location.IsConstant();
2382 if (use_imm) {
2383 if (is64bit) {
2384 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2385 } else {
2386 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2387 }
2388 } else {
2389 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2390 }
2391
2392 if (use_imm && rhs_imm == 0) {
2393 switch (cond) {
2394 case kCondEQ:
2395 case kCondBE: // <= 0 if zero
2396 __ Beqzc(lhs, label);
2397 break;
2398 case kCondNE:
2399 case kCondA: // > 0 if non-zero
2400 __ Bnezc(lhs, label);
2401 break;
2402 case kCondLT:
2403 __ Bltzc(lhs, label);
2404 break;
2405 case kCondGE:
2406 __ Bgezc(lhs, label);
2407 break;
2408 case kCondLE:
2409 __ Blezc(lhs, label);
2410 break;
2411 case kCondGT:
2412 __ Bgtzc(lhs, label);
2413 break;
2414 case kCondB: // always false
2415 break;
2416 case kCondAE: // always true
2417 __ Bc(label);
2418 break;
2419 }
2420 } else {
2421 if (use_imm) {
2422 rhs_reg = TMP;
2423 __ LoadConst64(rhs_reg, rhs_imm);
2424 }
2425 switch (cond) {
2426 case kCondEQ:
2427 __ Beqc(lhs, rhs_reg, label);
2428 break;
2429 case kCondNE:
2430 __ Bnec(lhs, rhs_reg, label);
2431 break;
2432 case kCondLT:
2433 __ Bltc(lhs, rhs_reg, label);
2434 break;
2435 case kCondGE:
2436 __ Bgec(lhs, rhs_reg, label);
2437 break;
2438 case kCondLE:
2439 __ Bgec(rhs_reg, lhs, label);
2440 break;
2441 case kCondGT:
2442 __ Bltc(rhs_reg, lhs, label);
2443 break;
2444 case kCondB:
2445 __ Bltuc(lhs, rhs_reg, label);
2446 break;
2447 case kCondAE:
2448 __ Bgeuc(lhs, rhs_reg, label);
2449 break;
2450 case kCondBE:
2451 __ Bgeuc(rhs_reg, lhs, label);
2452 break;
2453 case kCondA:
2454 __ Bltuc(rhs_reg, lhs, label);
2455 break;
2456 }
2457 }
2458}
2459
2460void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2461 bool gt_bias,
2462 Primitive::Type type,
2463 LocationSummary* locations,
2464 Mips64Label* label) {
2465 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2466 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2467 if (type == Primitive::kPrimFloat) {
2468 switch (cond) {
2469 case kCondEQ:
2470 __ CmpEqS(FTMP, lhs, rhs);
2471 __ Bc1nez(FTMP, label);
2472 break;
2473 case kCondNE:
2474 __ CmpEqS(FTMP, lhs, rhs);
2475 __ Bc1eqz(FTMP, label);
2476 break;
2477 case kCondLT:
2478 if (gt_bias) {
2479 __ CmpLtS(FTMP, lhs, rhs);
2480 } else {
2481 __ CmpUltS(FTMP, lhs, rhs);
2482 }
2483 __ Bc1nez(FTMP, label);
2484 break;
2485 case kCondLE:
2486 if (gt_bias) {
2487 __ CmpLeS(FTMP, lhs, rhs);
2488 } else {
2489 __ CmpUleS(FTMP, lhs, rhs);
2490 }
2491 __ Bc1nez(FTMP, label);
2492 break;
2493 case kCondGT:
2494 if (gt_bias) {
2495 __ CmpUltS(FTMP, rhs, lhs);
2496 } else {
2497 __ CmpLtS(FTMP, rhs, lhs);
2498 }
2499 __ Bc1nez(FTMP, label);
2500 break;
2501 case kCondGE:
2502 if (gt_bias) {
2503 __ CmpUleS(FTMP, rhs, lhs);
2504 } else {
2505 __ CmpLeS(FTMP, rhs, lhs);
2506 }
2507 __ Bc1nez(FTMP, label);
2508 break;
2509 default:
2510 LOG(FATAL) << "Unexpected non-floating-point condition";
2511 }
2512 } else {
2513 DCHECK_EQ(type, Primitive::kPrimDouble);
2514 switch (cond) {
2515 case kCondEQ:
2516 __ CmpEqD(FTMP, lhs, rhs);
2517 __ Bc1nez(FTMP, label);
2518 break;
2519 case kCondNE:
2520 __ CmpEqD(FTMP, lhs, rhs);
2521 __ Bc1eqz(FTMP, label);
2522 break;
2523 case kCondLT:
2524 if (gt_bias) {
2525 __ CmpLtD(FTMP, lhs, rhs);
2526 } else {
2527 __ CmpUltD(FTMP, lhs, rhs);
2528 }
2529 __ Bc1nez(FTMP, label);
2530 break;
2531 case kCondLE:
2532 if (gt_bias) {
2533 __ CmpLeD(FTMP, lhs, rhs);
2534 } else {
2535 __ CmpUleD(FTMP, lhs, rhs);
2536 }
2537 __ Bc1nez(FTMP, label);
2538 break;
2539 case kCondGT:
2540 if (gt_bias) {
2541 __ CmpUltD(FTMP, rhs, lhs);
2542 } else {
2543 __ CmpLtD(FTMP, rhs, lhs);
2544 }
2545 __ Bc1nez(FTMP, label);
2546 break;
2547 case kCondGE:
2548 if (gt_bias) {
2549 __ CmpUleD(FTMP, rhs, lhs);
2550 } else {
2551 __ CmpLeD(FTMP, rhs, lhs);
2552 }
2553 __ Bc1nez(FTMP, label);
2554 break;
2555 default:
2556 LOG(FATAL) << "Unexpected non-floating-point condition";
2557 }
2558 }
2559}
2560
Alexey Frunze4dda3372015-06-01 18:31:49 -07002561void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002562 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002563 Mips64Label* true_target,
2564 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002565 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002566
David Brazdil0debae72015-11-12 18:37:00 +00002567 if (true_target == nullptr && false_target == nullptr) {
2568 // Nothing to do. The code always falls through.
2569 return;
2570 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002571 // Constant condition, statically compared against "true" (integer value 1).
2572 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002573 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002574 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002575 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002576 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002577 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002578 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002579 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002580 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581 }
David Brazdil0debae72015-11-12 18:37:00 +00002582 return;
2583 }
2584
2585 // The following code generates these patterns:
2586 // (1) true_target == nullptr && false_target != nullptr
2587 // - opposite condition true => branch to false_target
2588 // (2) true_target != nullptr && false_target == nullptr
2589 // - condition true => branch to true_target
2590 // (3) true_target != nullptr && false_target != nullptr
2591 // - condition true => branch to true_target
2592 // - branch to false_target
2593 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002594 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002595 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002597 if (true_target == nullptr) {
2598 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2599 } else {
2600 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2601 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002602 } else {
2603 // The condition instruction has not been materialized, use its inputs as
2604 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002605 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002606 Primitive::Type type = condition->InputAt(0)->GetType();
2607 LocationSummary* locations = cond->GetLocations();
2608 IfCondition if_cond = condition->GetCondition();
2609 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002610
David Brazdil0debae72015-11-12 18:37:00 +00002611 if (true_target == nullptr) {
2612 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002613 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002614 }
2615
Alexey Frunze299a9392015-12-08 16:08:02 -08002616 switch (type) {
2617 default:
2618 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2619 break;
2620 case Primitive::kPrimLong:
2621 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2622 break;
2623 case Primitive::kPrimFloat:
2624 case Primitive::kPrimDouble:
2625 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2626 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002627 }
2628 }
David Brazdil0debae72015-11-12 18:37:00 +00002629
2630 // If neither branch falls through (case 3), the conditional branch to `true_target`
2631 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2632 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002633 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002634 }
2635}
2636
2637void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002639 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002640 locations->SetInAt(0, Location::RequiresRegister());
2641 }
2642}
2643
2644void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002645 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2646 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002647 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002648 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002649 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002650 nullptr : codegen_->GetLabelOf(false_successor);
2651 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002652}
2653
2654void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2655 LocationSummary* locations = new (GetGraph()->GetArena())
2656 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002657 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002658 locations->SetInAt(0, Location::RequiresRegister());
2659 }
2660}
2661
2662void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002663 SlowPathCodeMIPS64* slow_path =
2664 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002665 GenerateTestAndBranch(deoptimize,
2666 /* condition_input_index */ 0,
2667 slow_path->GetEntryLabel(),
2668 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002669}
2670
David Brazdil74eb1b22015-12-14 11:44:01 +00002671void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2673 if (Primitive::IsFloatingPointType(select->GetType())) {
2674 locations->SetInAt(0, Location::RequiresFpuRegister());
2675 locations->SetInAt(1, Location::RequiresFpuRegister());
2676 } else {
2677 locations->SetInAt(0, Location::RequiresRegister());
2678 locations->SetInAt(1, Location::RequiresRegister());
2679 }
2680 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2681 locations->SetInAt(2, Location::RequiresRegister());
2682 }
2683 locations->SetOut(Location::SameAsFirstInput());
2684}
2685
2686void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2687 LocationSummary* locations = select->GetLocations();
2688 Mips64Label false_target;
2689 GenerateTestAndBranch(select,
2690 /* condition_input_index */ 2,
2691 /* true_target */ nullptr,
2692 &false_target);
2693 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2694 __ Bind(&false_target);
2695}
2696
David Srbecky0cf44932015-12-09 14:09:59 +00002697void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2698 new (GetGraph()->GetArena()) LocationSummary(info);
2699}
2700
David Srbeckyd28f4a02016-03-14 17:14:24 +00002701void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2702 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002703}
2704
2705void CodeGeneratorMIPS64::GenerateNop() {
2706 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002707}
2708
Alexey Frunze4dda3372015-06-01 18:31:49 -07002709void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2710 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2711 LocationSummary* locations =
2712 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2713 locations->SetInAt(0, Location::RequiresRegister());
2714 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2715 locations->SetOut(Location::RequiresFpuRegister());
2716 } else {
2717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2718 }
2719}
2720
2721void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2722 const FieldInfo& field_info) {
2723 Primitive::Type type = field_info.GetFieldType();
2724 LocationSummary* locations = instruction->GetLocations();
2725 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2726 LoadOperandType load_type = kLoadUnsignedByte;
2727 switch (type) {
2728 case Primitive::kPrimBoolean:
2729 load_type = kLoadUnsignedByte;
2730 break;
2731 case Primitive::kPrimByte:
2732 load_type = kLoadSignedByte;
2733 break;
2734 case Primitive::kPrimShort:
2735 load_type = kLoadSignedHalfword;
2736 break;
2737 case Primitive::kPrimChar:
2738 load_type = kLoadUnsignedHalfword;
2739 break;
2740 case Primitive::kPrimInt:
2741 case Primitive::kPrimFloat:
2742 load_type = kLoadWord;
2743 break;
2744 case Primitive::kPrimLong:
2745 case Primitive::kPrimDouble:
2746 load_type = kLoadDoubleword;
2747 break;
2748 case Primitive::kPrimNot:
2749 load_type = kLoadUnsignedWord;
2750 break;
2751 case Primitive::kPrimVoid:
2752 LOG(FATAL) << "Unreachable type " << type;
2753 UNREACHABLE();
2754 }
2755 if (!Primitive::IsFloatingPointType(type)) {
2756 DCHECK(locations->Out().IsRegister());
2757 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2758 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2759 } else {
2760 DCHECK(locations->Out().IsFpuRegister());
2761 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2762 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2763 }
2764
2765 codegen_->MaybeRecordImplicitNullCheck(instruction);
2766 // TODO: memory barrier?
2767}
2768
2769void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2770 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2771 LocationSummary* locations =
2772 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2773 locations->SetInAt(0, Location::RequiresRegister());
2774 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2775 locations->SetInAt(1, Location::RequiresFpuRegister());
2776 } else {
2777 locations->SetInAt(1, Location::RequiresRegister());
2778 }
2779}
2780
2781void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002782 const FieldInfo& field_info,
2783 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002784 Primitive::Type type = field_info.GetFieldType();
2785 LocationSummary* locations = instruction->GetLocations();
2786 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2787 StoreOperandType store_type = kStoreByte;
2788 switch (type) {
2789 case Primitive::kPrimBoolean:
2790 case Primitive::kPrimByte:
2791 store_type = kStoreByte;
2792 break;
2793 case Primitive::kPrimShort:
2794 case Primitive::kPrimChar:
2795 store_type = kStoreHalfword;
2796 break;
2797 case Primitive::kPrimInt:
2798 case Primitive::kPrimFloat:
2799 case Primitive::kPrimNot:
2800 store_type = kStoreWord;
2801 break;
2802 case Primitive::kPrimLong:
2803 case Primitive::kPrimDouble:
2804 store_type = kStoreDoubleword;
2805 break;
2806 case Primitive::kPrimVoid:
2807 LOG(FATAL) << "Unreachable type " << type;
2808 UNREACHABLE();
2809 }
2810 if (!Primitive::IsFloatingPointType(type)) {
2811 DCHECK(locations->InAt(1).IsRegister());
2812 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2813 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2814 } else {
2815 DCHECK(locations->InAt(1).IsFpuRegister());
2816 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2817 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2818 }
2819
2820 codegen_->MaybeRecordImplicitNullCheck(instruction);
2821 // TODO: memory barriers?
2822 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2823 DCHECK(locations->InAt(1).IsRegister());
2824 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002825 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002826 }
2827}
2828
2829void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2830 HandleFieldGet(instruction, instruction->GetFieldInfo());
2831}
2832
2833void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2834 HandleFieldGet(instruction, instruction->GetFieldInfo());
2835}
2836
2837void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2838 HandleFieldSet(instruction, instruction->GetFieldInfo());
2839}
2840
2841void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002842 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002843}
2844
2845void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2846 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002847 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2849 locations->SetInAt(0, Location::RequiresRegister());
2850 locations->SetInAt(1, Location::RequiresRegister());
2851 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002852 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002853 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2854}
2855
2856void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2857 LocationSummary* locations = instruction->GetLocations();
2858 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2859 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2860 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2861
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002862 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863
2864 // Return 0 if `obj` is null.
2865 // TODO: Avoid this check if we know `obj` is not null.
2866 __ Move(out, ZERO);
2867 __ Beqzc(obj, &done);
2868
2869 // Compare the class of `obj` with `cls`.
2870 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002871 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002872 // Classes must be equal for the instanceof to succeed.
2873 __ Xor(out, out, cls);
2874 __ Sltiu(out, out, 1);
2875 } else {
2876 // If the classes are not equal, we go into a slow path.
2877 DCHECK(locations->OnlyCallsOnSlowPath());
2878 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002879 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880 codegen_->AddSlowPath(slow_path);
2881 __ Bnec(out, cls, slow_path->GetEntryLabel());
2882 __ LoadConst32(out, 1);
2883 __ Bind(slow_path->GetExitLabel());
2884 }
2885
2886 __ Bind(&done);
2887}
2888
2889void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2890 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2891 locations->SetOut(Location::ConstantLocation(constant));
2892}
2893
2894void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2895 // Will be generated at use site.
2896}
2897
2898void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2899 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2900 locations->SetOut(Location::ConstantLocation(constant));
2901}
2902
2903void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2904 // Will be generated at use site.
2905}
2906
Calin Juravle175dc732015-08-25 15:42:32 +01002907void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2908 // The trampoline uses the same calling convention as dex calling conventions,
2909 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2910 // the method_idx.
2911 HandleInvoke(invoke);
2912}
2913
2914void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2915 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2916}
2917
Alexey Frunze4dda3372015-06-01 18:31:49 -07002918void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2919 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2920 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2921}
2922
2923void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2924 HandleInvoke(invoke);
2925 // The register T0 is required to be used for the hidden argument in
2926 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2927 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2928}
2929
2930void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2931 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2932 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002933 Location receiver = invoke->GetLocations()->InAt(0);
2934 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002935 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002936
2937 // Set the hidden argument.
2938 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2939 invoke->GetDexMethodIndex());
2940
2941 // temp = object->GetClass();
2942 if (receiver.IsStackSlot()) {
2943 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2944 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2945 } else {
2946 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2947 }
2948 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002949 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2950 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2951 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002952 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002953 // temp = temp->GetImtEntryAt(method_offset);
2954 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2955 // T9 = temp->GetEntryPoint();
2956 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2957 // T9();
2958 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002959 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 DCHECK(!codegen_->IsLeafMethod());
2961 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2962}
2963
2964void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002965 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2966 if (intrinsic.TryDispatch(invoke)) {
2967 return;
2968 }
2969
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 HandleInvoke(invoke);
2971}
2972
2973void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002974 // Explicit clinit checks triggered by static invokes must have been pruned by
2975 // art::PrepareForRegisterAllocation.
2976 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002977
Chris Larsen3039e382015-08-26 07:54:08 -07002978 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2979 if (intrinsic.TryDispatch(invoke)) {
2980 return;
2981 }
2982
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002984}
2985
Chris Larsen3039e382015-08-26 07:54:08 -07002986static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002987 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002988 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2989 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002990 return true;
2991 }
2992 return false;
2993}
2994
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002995HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2996 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2997 // TODO: Implement other kinds.
2998 return HLoadString::LoadKind::kDexCacheViaMethod;
2999}
3000
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003001HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
3002 HLoadClass::LoadKind desired_class_load_kind) {
3003 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3004 // TODO: Implement other kinds.
3005 return HLoadClass::LoadKind::kDexCacheViaMethod;
3006}
3007
Vladimir Markodc151b22015-10-15 18:02:30 +01003008HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3009 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3010 MethodReference target_method ATTRIBUTE_UNUSED) {
3011 switch (desired_dispatch_info.method_load_kind) {
3012 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3013 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3014 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3015 return HInvokeStaticOrDirect::DispatchInfo {
3016 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3017 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3018 0u,
3019 0u
3020 };
3021 default:
3022 break;
3023 }
3024 switch (desired_dispatch_info.code_ptr_location) {
3025 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3026 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3027 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3028 return HInvokeStaticOrDirect::DispatchInfo {
3029 desired_dispatch_info.method_load_kind,
3030 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3031 desired_dispatch_info.method_load_data,
3032 0u
3033 };
3034 default:
3035 return desired_dispatch_info;
3036 }
3037}
3038
Alexey Frunze4dda3372015-06-01 18:31:49 -07003039void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3040 // All registers are assumed to be correctly set up per the calling convention.
3041
Vladimir Marko58155012015-08-19 12:49:41 +00003042 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3043 switch (invoke->GetMethodLoadKind()) {
3044 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3045 // temp = thread->string_init_entrypoint
3046 __ LoadFromOffset(kLoadDoubleword,
3047 temp.AsRegister<GpuRegister>(),
3048 TR,
3049 invoke->GetStringInitOffset());
3050 break;
3051 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003052 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003053 break;
3054 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3055 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3056 break;
3057 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003058 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003059 // TODO: Implement these types.
3060 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3061 LOG(FATAL) << "Unsupported";
3062 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003063 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003064 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003065 GpuRegister reg = temp.AsRegister<GpuRegister>();
3066 GpuRegister method_reg;
3067 if (current_method.IsRegister()) {
3068 method_reg = current_method.AsRegister<GpuRegister>();
3069 } else {
3070 // TODO: use the appropriate DCHECK() here if possible.
3071 // DCHECK(invoke->GetLocations()->Intrinsified());
3072 DCHECK(!current_method.IsValid());
3073 method_reg = reg;
3074 __ Ld(reg, SP, kCurrentMethodStackOffset);
3075 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003076
Vladimir Marko58155012015-08-19 12:49:41 +00003077 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003078 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003079 reg,
3080 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003081 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003082 // temp = temp[index_in_cache];
3083 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3084 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003085 __ LoadFromOffset(kLoadDoubleword,
3086 reg,
3087 reg,
3088 CodeGenerator::GetCachePointerOffset(index_in_cache));
3089 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003091 }
3092
Vladimir Marko58155012015-08-19 12:49:41 +00003093 switch (invoke->GetCodePtrLocation()) {
3094 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003095 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003096 break;
3097 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3098 // LR = invoke->GetDirectCodePtr();
3099 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3100 // LR()
3101 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003102 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003103 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003104 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003105 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3106 // TODO: Implement these types.
3107 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3108 LOG(FATAL) << "Unsupported";
3109 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003110 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3111 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3112 __ LoadFromOffset(kLoadDoubleword,
3113 T9,
3114 callee_method.AsRegister<GpuRegister>(),
3115 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003116 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003117 // T9()
3118 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003119 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003120 break;
3121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122 DCHECK(!IsLeafMethod());
3123}
3124
3125void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003126 // Explicit clinit checks triggered by static invokes must have been pruned by
3127 // art::PrepareForRegisterAllocation.
3128 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129
3130 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3131 return;
3132 }
3133
3134 LocationSummary* locations = invoke->GetLocations();
3135 codegen_->GenerateStaticOrDirectCall(invoke,
3136 locations->HasTemps()
3137 ? locations->GetTemp(0)
3138 : Location::NoLocation());
3139 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3140}
3141
Alexey Frunze53afca12015-11-05 16:34:23 -08003142void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003143 // Use the calling convention instead of the location of the receiver, as
3144 // intrinsics may have put the receiver in a different register. In the intrinsics
3145 // slow path, the arguments have been moved to the right place, so here we are
3146 // guaranteed that the receiver is the first register of the calling convention.
3147 InvokeDexCallingConvention calling_convention;
3148 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3149
Alexey Frunze53afca12015-11-05 16:34:23 -08003150 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3152 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3153 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003154 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003155
3156 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003157 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003158 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003159 // temp = temp->GetMethodAt(method_offset);
3160 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3161 // T9 = temp->GetEntryPoint();
3162 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3163 // T9();
3164 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003165 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003166}
3167
3168void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3169 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3170 return;
3171 }
3172
3173 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003174 DCHECK(!codegen_->IsLeafMethod());
3175 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3176}
3177
3178void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003179 InvokeRuntimeCallingConvention calling_convention;
3180 CodeGenerator::CreateLoadClassLocationSummary(
3181 cls,
3182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003183 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003184}
3185
3186void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3187 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003188 if (cls->NeedsAccessCheck()) {
3189 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3190 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3191 cls,
3192 cls->GetDexPc(),
3193 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003194 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003195 return;
3196 }
3197
3198 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3199 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3200 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003201 DCHECK(!cls->CanCallRuntime());
3202 DCHECK(!cls->MustGenerateClinitCheck());
3203 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3204 ArtMethod::DeclaringClassOffset().Int32Value());
3205 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003206 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3207 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003208 __ LoadFromOffset(
3209 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003210 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003211 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3212 DCHECK(cls->CanCallRuntime());
3213 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3214 cls,
3215 cls,
3216 cls->GetDexPc(),
3217 cls->MustGenerateClinitCheck());
3218 codegen_->AddSlowPath(slow_path);
3219 if (!cls->IsInDexCache()) {
3220 __ Beqzc(out, slow_path->GetEntryLabel());
3221 }
3222 if (cls->MustGenerateClinitCheck()) {
3223 GenerateClassInitializationCheck(slow_path, out);
3224 } else {
3225 __ Bind(slow_path->GetExitLabel());
3226 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003227 }
3228 }
3229}
3230
David Brazdilcb1c0552015-08-04 16:22:25 +01003231static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003232 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003233}
3234
Alexey Frunze4dda3372015-06-01 18:31:49 -07003235void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3236 LocationSummary* locations =
3237 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3238 locations->SetOut(Location::RequiresRegister());
3239}
3240
3241void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3242 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003243 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3244}
3245
3246void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3247 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3248}
3249
3250void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3251 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252}
3253
Alexey Frunze4dda3372015-06-01 18:31:49 -07003254void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003255 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3256 ? LocationSummary::kCallOnSlowPath
3257 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetOut(Location::RequiresRegister());
3261}
3262
3263void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264 LocationSummary* locations = load->GetLocations();
3265 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3266 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3267 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3268 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003269 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003270 __ LoadFromOffset(
3271 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003272 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003273
3274 if (!load->IsInDexCache()) {
3275 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3276 codegen_->AddSlowPath(slow_path);
3277 __ Beqzc(out, slow_path->GetEntryLabel());
3278 __ Bind(slow_path->GetExitLabel());
3279 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003280}
3281
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3284 locations->SetOut(Location::ConstantLocation(constant));
3285}
3286
3287void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3288 // Will be generated at use site.
3289}
3290
3291void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3292 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003294 InvokeRuntimeCallingConvention calling_convention;
3295 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3296}
3297
3298void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3299 codegen_->InvokeRuntime(instruction->IsEnter()
3300 ? QUICK_ENTRY_POINT(pLockObject)
3301 : QUICK_ENTRY_POINT(pUnlockObject),
3302 instruction,
3303 instruction->GetDexPc(),
3304 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003305 if (instruction->IsEnter()) {
3306 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3307 } else {
3308 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3309 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003310}
3311
3312void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3313 LocationSummary* locations =
3314 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3315 switch (mul->GetResultType()) {
3316 case Primitive::kPrimInt:
3317 case Primitive::kPrimLong:
3318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetInAt(1, Location::RequiresRegister());
3320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3321 break;
3322
3323 case Primitive::kPrimFloat:
3324 case Primitive::kPrimDouble:
3325 locations->SetInAt(0, Location::RequiresFpuRegister());
3326 locations->SetInAt(1, Location::RequiresFpuRegister());
3327 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3328 break;
3329
3330 default:
3331 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3332 }
3333}
3334
3335void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3336 Primitive::Type type = instruction->GetType();
3337 LocationSummary* locations = instruction->GetLocations();
3338
3339 switch (type) {
3340 case Primitive::kPrimInt:
3341 case Primitive::kPrimLong: {
3342 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3343 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3344 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3345 if (type == Primitive::kPrimInt)
3346 __ MulR6(dst, lhs, rhs);
3347 else
3348 __ Dmul(dst, lhs, rhs);
3349 break;
3350 }
3351 case Primitive::kPrimFloat:
3352 case Primitive::kPrimDouble: {
3353 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3354 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3355 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3356 if (type == Primitive::kPrimFloat)
3357 __ MulS(dst, lhs, rhs);
3358 else
3359 __ MulD(dst, lhs, rhs);
3360 break;
3361 }
3362 default:
3363 LOG(FATAL) << "Unexpected mul type " << type;
3364 }
3365}
3366
3367void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3368 LocationSummary* locations =
3369 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3370 switch (neg->GetResultType()) {
3371 case Primitive::kPrimInt:
3372 case Primitive::kPrimLong:
3373 locations->SetInAt(0, Location::RequiresRegister());
3374 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3375 break;
3376
3377 case Primitive::kPrimFloat:
3378 case Primitive::kPrimDouble:
3379 locations->SetInAt(0, Location::RequiresFpuRegister());
3380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3381 break;
3382
3383 default:
3384 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3385 }
3386}
3387
3388void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3389 Primitive::Type type = instruction->GetType();
3390 LocationSummary* locations = instruction->GetLocations();
3391
3392 switch (type) {
3393 case Primitive::kPrimInt:
3394 case Primitive::kPrimLong: {
3395 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3396 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3397 if (type == Primitive::kPrimInt)
3398 __ Subu(dst, ZERO, src);
3399 else
3400 __ Dsubu(dst, ZERO, src);
3401 break;
3402 }
3403 case Primitive::kPrimFloat:
3404 case Primitive::kPrimDouble: {
3405 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3406 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3407 if (type == Primitive::kPrimFloat)
3408 __ NegS(dst, src);
3409 else
3410 __ NegD(dst, src);
3411 break;
3412 }
3413 default:
3414 LOG(FATAL) << "Unexpected neg type " << type;
3415 }
3416}
3417
3418void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3419 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003421 InvokeRuntimeCallingConvention calling_convention;
3422 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3423 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3424 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3425 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3426}
3427
3428void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3429 LocationSummary* locations = instruction->GetLocations();
3430 // Move an uint16_t value to a register.
3431 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003432 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3433 instruction,
3434 instruction->GetDexPc(),
3435 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003436 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3437}
3438
3439void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3440 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003443 if (instruction->IsStringAlloc()) {
3444 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3445 } else {
3446 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3447 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3448 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003449 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3450}
3451
3452void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003453 if (instruction->IsStringAlloc()) {
3454 // String is allocated through StringFactory. Call NewEmptyString entry point.
3455 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003456 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003457 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003458 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3459 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3460 __ Jalr(T9);
3461 __ Nop();
3462 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3463 } else {
3464 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3465 instruction,
3466 instruction->GetDexPc(),
3467 nullptr);
3468 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3469 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003470}
3471
3472void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3474 locations->SetInAt(0, Location::RequiresRegister());
3475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3476}
3477
3478void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3479 Primitive::Type type = instruction->GetType();
3480 LocationSummary* locations = instruction->GetLocations();
3481
3482 switch (type) {
3483 case Primitive::kPrimInt:
3484 case Primitive::kPrimLong: {
3485 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3486 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3487 __ Nor(dst, src, ZERO);
3488 break;
3489 }
3490
3491 default:
3492 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3493 }
3494}
3495
3496void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3498 locations->SetInAt(0, Location::RequiresRegister());
3499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3500}
3501
3502void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3503 LocationSummary* locations = instruction->GetLocations();
3504 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3505 locations->InAt(0).AsRegister<GpuRegister>(),
3506 1);
3507}
3508
3509void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003510 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3511 ? LocationSummary::kCallOnSlowPath
3512 : LocationSummary::kNoCall;
3513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 locations->SetInAt(0, Location::RequiresRegister());
3515 if (instruction->HasUses()) {
3516 locations->SetOut(Location::SameAsFirstInput());
3517 }
3518}
3519
Calin Juravle2ae48182016-03-16 14:05:09 +00003520void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3521 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003522 return;
3523 }
3524 Location obj = instruction->GetLocations()->InAt(0);
3525
3526 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003527 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003528}
3529
Calin Juravle2ae48182016-03-16 14:05:09 +00003530void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003531 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003532 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003533
3534 Location obj = instruction->GetLocations()->InAt(0);
3535
3536 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3537}
3538
3539void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003540 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003541}
3542
3543void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3544 HandleBinaryOp(instruction);
3545}
3546
3547void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3548 HandleBinaryOp(instruction);
3549}
3550
3551void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3552 LOG(FATAL) << "Unreachable";
3553}
3554
3555void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3556 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3557}
3558
3559void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3561 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3562 if (location.IsStackSlot()) {
3563 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3564 } else if (location.IsDoubleStackSlot()) {
3565 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3566 }
3567 locations->SetOut(location);
3568}
3569
3570void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3571 ATTRIBUTE_UNUSED) {
3572 // Nothing to do, the parameter is already at its location.
3573}
3574
3575void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3576 LocationSummary* locations =
3577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3578 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3579}
3580
3581void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3582 ATTRIBUTE_UNUSED) {
3583 // Nothing to do, the method is already at its location.
3584}
3585
3586void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003588 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589 locations->SetInAt(i, Location::Any());
3590 }
3591 locations->SetOut(Location::Any());
3592}
3593
3594void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3595 LOG(FATAL) << "Unreachable";
3596}
3597
3598void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3599 Primitive::Type type = rem->GetResultType();
3600 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003601 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3602 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3604
3605 switch (type) {
3606 case Primitive::kPrimInt:
3607 case Primitive::kPrimLong:
3608 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003609 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3611 break;
3612
3613 case Primitive::kPrimFloat:
3614 case Primitive::kPrimDouble: {
3615 InvokeRuntimeCallingConvention calling_convention;
3616 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3617 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3618 locations->SetOut(calling_convention.GetReturnLocation(type));
3619 break;
3620 }
3621
3622 default:
3623 LOG(FATAL) << "Unexpected rem type " << type;
3624 }
3625}
3626
3627void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3628 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629
3630 switch (type) {
3631 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003632 case Primitive::kPrimLong:
3633 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003634 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635
3636 case Primitive::kPrimFloat:
3637 case Primitive::kPrimDouble: {
3638 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3639 : QUICK_ENTRY_POINT(pFmod);
3640 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003641 if (type == Primitive::kPrimFloat) {
3642 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3643 } else {
3644 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3645 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003646 break;
3647 }
3648 default:
3649 LOG(FATAL) << "Unexpected rem type " << type;
3650 }
3651}
3652
3653void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3654 memory_barrier->SetLocations(nullptr);
3655}
3656
3657void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3658 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3659}
3660
3661void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3662 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3663 Primitive::Type return_type = ret->InputAt(0)->GetType();
3664 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3665}
3666
3667void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3668 codegen_->GenerateFrameExit();
3669}
3670
3671void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3672 ret->SetLocations(nullptr);
3673}
3674
3675void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3676 codegen_->GenerateFrameExit();
3677}
3678
Alexey Frunze92d90602015-12-18 18:16:36 -08003679void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3680 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003681}
3682
Alexey Frunze92d90602015-12-18 18:16:36 -08003683void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3684 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003685}
3686
Alexey Frunze4dda3372015-06-01 18:31:49 -07003687void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3688 HandleShift(shl);
3689}
3690
3691void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3692 HandleShift(shl);
3693}
3694
3695void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3696 HandleShift(shr);
3697}
3698
3699void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3700 HandleShift(shr);
3701}
3702
Alexey Frunze4dda3372015-06-01 18:31:49 -07003703void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3704 HandleBinaryOp(instruction);
3705}
3706
3707void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3708 HandleBinaryOp(instruction);
3709}
3710
3711void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3712 HandleFieldGet(instruction, instruction->GetFieldInfo());
3713}
3714
3715void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3716 HandleFieldGet(instruction, instruction->GetFieldInfo());
3717}
3718
3719void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3720 HandleFieldSet(instruction, instruction->GetFieldInfo());
3721}
3722
3723void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003724 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003725}
3726
Calin Juravlee460d1d2015-09-29 04:52:17 +01003727void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3728 HUnresolvedInstanceFieldGet* instruction) {
3729 FieldAccessCallingConventionMIPS64 calling_convention;
3730 codegen_->CreateUnresolvedFieldLocationSummary(
3731 instruction, instruction->GetFieldType(), calling_convention);
3732}
3733
3734void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3735 HUnresolvedInstanceFieldGet* instruction) {
3736 FieldAccessCallingConventionMIPS64 calling_convention;
3737 codegen_->GenerateUnresolvedFieldAccess(instruction,
3738 instruction->GetFieldType(),
3739 instruction->GetFieldIndex(),
3740 instruction->GetDexPc(),
3741 calling_convention);
3742}
3743
3744void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3745 HUnresolvedInstanceFieldSet* instruction) {
3746 FieldAccessCallingConventionMIPS64 calling_convention;
3747 codegen_->CreateUnresolvedFieldLocationSummary(
3748 instruction, instruction->GetFieldType(), calling_convention);
3749}
3750
3751void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3752 HUnresolvedInstanceFieldSet* instruction) {
3753 FieldAccessCallingConventionMIPS64 calling_convention;
3754 codegen_->GenerateUnresolvedFieldAccess(instruction,
3755 instruction->GetFieldType(),
3756 instruction->GetFieldIndex(),
3757 instruction->GetDexPc(),
3758 calling_convention);
3759}
3760
3761void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3762 HUnresolvedStaticFieldGet* instruction) {
3763 FieldAccessCallingConventionMIPS64 calling_convention;
3764 codegen_->CreateUnresolvedFieldLocationSummary(
3765 instruction, instruction->GetFieldType(), calling_convention);
3766}
3767
3768void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3769 HUnresolvedStaticFieldGet* instruction) {
3770 FieldAccessCallingConventionMIPS64 calling_convention;
3771 codegen_->GenerateUnresolvedFieldAccess(instruction,
3772 instruction->GetFieldType(),
3773 instruction->GetFieldIndex(),
3774 instruction->GetDexPc(),
3775 calling_convention);
3776}
3777
3778void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3779 HUnresolvedStaticFieldSet* instruction) {
3780 FieldAccessCallingConventionMIPS64 calling_convention;
3781 codegen_->CreateUnresolvedFieldLocationSummary(
3782 instruction, instruction->GetFieldType(), calling_convention);
3783}
3784
3785void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3786 HUnresolvedStaticFieldSet* instruction) {
3787 FieldAccessCallingConventionMIPS64 calling_convention;
3788 codegen_->GenerateUnresolvedFieldAccess(instruction,
3789 instruction->GetFieldType(),
3790 instruction->GetFieldIndex(),
3791 instruction->GetDexPc(),
3792 calling_convention);
3793}
3794
Alexey Frunze4dda3372015-06-01 18:31:49 -07003795void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3797}
3798
3799void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3800 HBasicBlock* block = instruction->GetBlock();
3801 if (block->GetLoopInformation() != nullptr) {
3802 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3803 // The back edge will generate the suspend check.
3804 return;
3805 }
3806 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3807 // The goto will generate the suspend check.
3808 return;
3809 }
3810 GenerateSuspendCheck(instruction, nullptr);
3811}
3812
Alexey Frunze4dda3372015-06-01 18:31:49 -07003813void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3814 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 InvokeRuntimeCallingConvention calling_convention;
3817 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3818}
3819
3820void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3821 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3822 instruction,
3823 instruction->GetDexPc(),
3824 nullptr);
3825 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3826}
3827
3828void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3829 Primitive::Type input_type = conversion->GetInputType();
3830 Primitive::Type result_type = conversion->GetResultType();
3831 DCHECK_NE(input_type, result_type);
3832
3833 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3834 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3835 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3836 }
3837
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003838 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3839
3840 if (Primitive::IsFloatingPointType(input_type)) {
3841 locations->SetInAt(0, Location::RequiresFpuRegister());
3842 } else {
3843 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003844 }
3845
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003846 if (Primitive::IsFloatingPointType(result_type)) {
3847 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003848 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003849 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003850 }
3851}
3852
3853void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3854 LocationSummary* locations = conversion->GetLocations();
3855 Primitive::Type result_type = conversion->GetResultType();
3856 Primitive::Type input_type = conversion->GetInputType();
3857
3858 DCHECK_NE(input_type, result_type);
3859
3860 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3861 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3862 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3863
3864 switch (result_type) {
3865 case Primitive::kPrimChar:
3866 __ Andi(dst, src, 0xFFFF);
3867 break;
3868 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003869 if (input_type == Primitive::kPrimLong) {
3870 // Type conversion from long to types narrower than int is a result of code
3871 // transformations. To avoid unpredictable results for SEB and SEH, we first
3872 // need to sign-extend the low 32-bit value into bits 32 through 63.
3873 __ Sll(dst, src, 0);
3874 __ Seb(dst, dst);
3875 } else {
3876 __ Seb(dst, src);
3877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003878 break;
3879 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003880 if (input_type == Primitive::kPrimLong) {
3881 // Type conversion from long to types narrower than int is a result of code
3882 // transformations. To avoid unpredictable results for SEB and SEH, we first
3883 // need to sign-extend the low 32-bit value into bits 32 through 63.
3884 __ Sll(dst, src, 0);
3885 __ Seh(dst, dst);
3886 } else {
3887 __ Seh(dst, src);
3888 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003889 break;
3890 case Primitive::kPrimInt:
3891 case Primitive::kPrimLong:
3892 // Sign-extend 32-bit int into bits 32 through 63 for
3893 // int-to-long and long-to-int conversions
3894 __ Sll(dst, src, 0);
3895 break;
3896
3897 default:
3898 LOG(FATAL) << "Unexpected type conversion from " << input_type
3899 << " to " << result_type;
3900 }
3901 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003902 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3903 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3904 if (input_type == Primitive::kPrimLong) {
3905 __ Dmtc1(src, FTMP);
3906 if (result_type == Primitive::kPrimFloat) {
3907 __ Cvtsl(dst, FTMP);
3908 } else {
3909 __ Cvtdl(dst, FTMP);
3910 }
3911 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003912 __ Mtc1(src, FTMP);
3913 if (result_type == Primitive::kPrimFloat) {
3914 __ Cvtsw(dst, FTMP);
3915 } else {
3916 __ Cvtdw(dst, FTMP);
3917 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003918 }
3919 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3920 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003921 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3922 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3923 Mips64Label truncate;
3924 Mips64Label done;
3925
3926 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3927 // value when the input is either a NaN or is outside of the range of the output type
3928 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3929 // the same result.
3930 //
3931 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3932 // value of the output type if the input is outside of the range after the truncation or
3933 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3934 // results. This matches the desired float/double-to-int/long conversion exactly.
3935 //
3936 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3937 //
3938 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3939 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3940 // even though it must be NAN2008=1 on R6.
3941 //
3942 // The code takes care of the different behaviors by first comparing the input to the
3943 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3944 // If the input is greater than or equal to the minimum, it procedes to the truncate
3945 // instruction, which will handle such an input the same way irrespective of NAN2008.
3946 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3947 // in order to return either zero or the minimum value.
3948 //
3949 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3950 // truncate instruction for MIPS64R6.
3951 if (input_type == Primitive::kPrimFloat) {
3952 uint32_t min_val = (result_type == Primitive::kPrimLong)
3953 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3954 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3955 __ LoadConst32(TMP, min_val);
3956 __ Mtc1(TMP, FTMP);
3957 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003958 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003959 uint64_t min_val = (result_type == Primitive::kPrimLong)
3960 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3961 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3962 __ LoadConst64(TMP, min_val);
3963 __ Dmtc1(TMP, FTMP);
3964 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003965 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003966
3967 __ Bc1nez(FTMP, &truncate);
3968
3969 if (input_type == Primitive::kPrimFloat) {
3970 __ CmpEqS(FTMP, src, src);
3971 } else {
3972 __ CmpEqD(FTMP, src, src);
3973 }
3974 if (result_type == Primitive::kPrimLong) {
3975 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3976 } else {
3977 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3978 }
3979 __ Mfc1(TMP, FTMP);
3980 __ And(dst, dst, TMP);
3981
3982 __ Bc(&done);
3983
3984 __ Bind(&truncate);
3985
3986 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003987 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003988 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003989 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003990 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003991 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003992 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003993 } else {
3994 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003995 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003996 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003997 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003998 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003999 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004000 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004001
4002 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003 } else if (Primitive::IsFloatingPointType(result_type) &&
4004 Primitive::IsFloatingPointType(input_type)) {
4005 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4006 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4007 if (result_type == Primitive::kPrimFloat) {
4008 __ Cvtsd(dst, src);
4009 } else {
4010 __ Cvtds(dst, src);
4011 }
4012 } else {
4013 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4014 << " to " << result_type;
4015 }
4016}
4017
4018void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4019 HandleShift(ushr);
4020}
4021
4022void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4023 HandleShift(ushr);
4024}
4025
4026void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4027 HandleBinaryOp(instruction);
4028}
4029
4030void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4031 HandleBinaryOp(instruction);
4032}
4033
4034void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4035 // Nothing to do, this should be removed during prepare for register allocator.
4036 LOG(FATAL) << "Unreachable";
4037}
4038
4039void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4040 // Nothing to do, this should be removed during prepare for register allocator.
4041 LOG(FATAL) << "Unreachable";
4042}
4043
4044void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004045 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004046}
4047
4048void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004049 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004050}
4051
4052void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004053 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004054}
4055
4056void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004057 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004058}
4059
4060void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004061 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004062}
4063
4064void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004065 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066}
4067
4068void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004069 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004070}
4071
4072void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004073 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004074}
4075
4076void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004077 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004078}
4079
4080void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004081 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004082}
4083
4084void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004085 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004086}
4087
4088void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004089 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004090}
4091
Aart Bike9f37602015-10-09 11:15:55 -07004092void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004093 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004094}
4095
4096void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004097 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004098}
4099
4100void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004101 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004102}
4103
4104void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004105 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004106}
4107
4108void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004109 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004110}
4111
4112void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004114}
4115
4116void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004122}
4123
Mark Mendellfe57faa2015-09-18 09:26:15 -04004124// Simple implementation of packed switch - generate cascaded compare/jumps.
4125void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4126 LocationSummary* locations =
4127 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4128 locations->SetInAt(0, Location::RequiresRegister());
4129}
4130
4131void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4132 int32_t lower_bound = switch_instr->GetStartValue();
4133 int32_t num_entries = switch_instr->GetNumEntries();
4134 LocationSummary* locations = switch_instr->GetLocations();
4135 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4136 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4137
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004138 // Create a set of compare/jumps.
4139 GpuRegister temp_reg = TMP;
4140 if (IsInt<16>(-lower_bound)) {
4141 __ Addiu(temp_reg, value_reg, -lower_bound);
4142 } else {
4143 __ LoadConst32(AT, -lower_bound);
4144 __ Addu(temp_reg, value_reg, AT);
4145 }
4146 // Jump to default if index is negative
4147 // Note: We don't check the case that index is positive while value < lower_bound, because in
4148 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4149 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4150
Mark Mendellfe57faa2015-09-18 09:26:15 -04004151 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004152 // Jump to successors[0] if value == lower_bound.
4153 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4154 int32_t last_index = 0;
4155 for (; num_entries - last_index > 2; last_index += 2) {
4156 __ Addiu(temp_reg, temp_reg, -2);
4157 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4158 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4159 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4160 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4161 }
4162 if (num_entries - last_index == 2) {
4163 // The last missing case_value.
4164 __ Addiu(temp_reg, temp_reg, -1);
4165 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004166 }
4167
4168 // And the default for any other value.
4169 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004170 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004171 }
4172}
4173
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004174void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4175 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4176}
4177
4178void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4179 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4180}
4181
Alexey Frunze4dda3372015-06-01 18:31:49 -07004182} // namespace mips64
4183} // namespace art