blob: 7598740d3c34a3ed2f00da18059af61d62d0ad78 [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);
Serban Constantinescufc734082016-07-19 17:18:07 +0100130 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
131 ? kQuickThrowStringBounds
132 : kQuickThrowArrayBounds;
133 mips64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100134 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700135 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
136 }
137
Alexandre Rames8158f282015-08-07 10:26:17 +0100138 bool IsFatal() const OVERRIDE { return true; }
139
Roland Levillain46648892015-06-19 16:07:18 +0100140 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
141
Alexey Frunze4dda3372015-06-01 18:31:49 -0700142 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
144};
145
146class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
147 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000148 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700149
150 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
151 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
152 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100153 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700154 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
155 }
156
Alexandre Rames8158f282015-08-07 10:26:17 +0100157 bool IsFatal() const OVERRIDE { return true; }
158
Roland Levillain46648892015-06-19 16:07:18 +0100159 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
160
Alexey Frunze4dda3372015-06-01 18:31:49 -0700161 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
163};
164
165class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
166 public:
167 LoadClassSlowPathMIPS64(HLoadClass* cls,
168 HInstruction* at,
169 uint32_t dex_pc,
170 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000171 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700172 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
173 }
174
175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
176 LocationSummary* locations = at_->GetLocations();
177 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
178
179 __ Bind(GetEntryLabel());
180 SaveLiveRegisters(codegen, locations);
181
182 InvokeRuntimeCallingConvention calling_convention;
183 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +0100184 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
185 : kQuickInitializeType;
186 mips64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700187 if (do_clinit_) {
188 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
189 } else {
190 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
191 }
192
193 // Move the class to the desired location.
194 Location out = locations->Out();
195 if (out.IsValid()) {
196 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
197 Primitive::Type type = at_->GetType();
198 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
199 }
200
201 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700202 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700203 }
204
Roland Levillain46648892015-06-19 16:07:18 +0100205 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
206
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 private:
208 // The class this slow path will load.
209 HLoadClass* const cls_;
210
211 // The instruction where this slow path is happening.
212 // (Might be the load class or an initialization check).
213 HInstruction* const at_;
214
215 // The dex PC of `at_`.
216 const uint32_t dex_pc_;
217
218 // Whether to initialize the class.
219 const bool do_clinit_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
222};
223
224class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
225 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700227
228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
229 LocationSummary* locations = instruction_->GetLocations();
230 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
231 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
232
233 __ Bind(GetEntryLabel());
234 SaveLiveRegisters(codegen, locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000237 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
238 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufc734082016-07-19 17:18:07 +0100239 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700240 instruction_,
241 instruction_->GetDexPc(),
242 this);
243 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
244 Primitive::Type type = instruction_->GetType();
245 mips64_codegen->MoveLocation(locations->Out(),
246 calling_convention.GetReturnLocation(type),
247 type);
248
249 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700250 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700251 }
252
Roland Levillain46648892015-06-19 16:07:18 +0100253 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
254
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700256 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
257};
258
259class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
260 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000261 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700262
263 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
264 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
265 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000266 if (instruction_->CanThrowIntoCatchBlock()) {
267 // Live registers will be restored in the catch block if caught.
268 SaveLiveRegisters(codegen, instruction_->GetLocations());
269 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100270 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700271 instruction_,
272 instruction_->GetDexPc(),
273 this);
274 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
275 }
276
Alexandre Rames8158f282015-08-07 10:26:17 +0100277 bool IsFatal() const OVERRIDE { return true; }
278
Roland Levillain46648892015-06-19 16:07:18 +0100279 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
280
Alexey Frunze4dda3372015-06-01 18:31:49 -0700281 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
283};
284
285class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
286 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100287 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000288 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700289
290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
291 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
292 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100293 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700296 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700297 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700298 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299 }
300 }
301
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700302 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303 DCHECK(successor_ == nullptr);
304 return &return_label_;
305 }
306
Roland Levillain46648892015-06-19 16:07:18 +0100307 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
308
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700310 // If not null, the block to branch to after the suspend check.
311 HBasicBlock* const successor_;
312
313 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700314 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700315
316 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
317};
318
319class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800325 Location arg0, arg1;
326 if (instruction_->IsInstanceOf()) {
327 arg0 = locations->InAt(1);
328 arg1 = locations->Out();
329 } else {
330 arg0 = locations->InAt(0);
331 arg1 = locations->InAt(1);
332 }
333
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700335 DCHECK(instruction_->IsCheckCast()
336 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
337 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
338
339 __ Bind(GetEntryLabel());
340 SaveLiveRegisters(codegen, locations);
341
342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
344 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800345 codegen->EmitParallelMoves(arg0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
347 Primitive::kPrimNot,
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800348 arg1,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
350 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100352 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000353 CheckEntrypointTypes<
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700355 Primitive::Type ret_type = instruction_->GetType();
356 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
357 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 } else {
359 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800360 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
361 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 }
363
364 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700365 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700366 }
367
Roland Levillain46648892015-06-19 16:07:18 +0100368 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
369
Alexey Frunze4dda3372015-06-01 18:31:49 -0700370 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
372};
373
374class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
375 public:
Aart Bik42249c32016-01-07 15:33:50 -0800376 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800380 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700381 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100382 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000383 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384 }
385
Roland Levillain46648892015-06-19 16:07:18 +0100386 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
387
Alexey Frunze4dda3372015-06-01 18:31:49 -0700388 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
390};
391
392CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
393 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100394 const CompilerOptions& compiler_options,
395 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700396 : CodeGenerator(graph,
397 kNumberOfGpuRegisters,
398 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000399 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700400 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
401 arraysize(kCoreCalleeSaves)),
402 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
403 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100404 compiler_options,
405 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100406 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700407 location_builder_(graph, this),
408 instruction_visitor_(graph, this),
409 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100410 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411 isa_features_(isa_features) {
412 // Save RA (containing the return address) to mimic Quick.
413 AddAllocatedRegister(Location::RegisterLocation(RA));
414}
415
416#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100417// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
418#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700419#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700420
421void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700422 // Ensure that we fix up branches.
423 __ FinalizeCode();
424
425 // Adjust native pc offsets in stack maps.
426 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
427 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
428 uint32_t new_position = __ GetAdjustedPosition(old_position);
429 DCHECK_GE(new_position, old_position);
430 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
431 }
432
433 // Adjust pc offsets for the disassembly information.
434 if (disasm_info_ != nullptr) {
435 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
436 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
437 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
438 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
439 it.second.start = __ GetAdjustedPosition(it.second.start);
440 it.second.end = __ GetAdjustedPosition(it.second.end);
441 }
442 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
443 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
444 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
445 }
446 }
447
Alexey Frunze4dda3372015-06-01 18:31:49 -0700448 CodeGenerator::Finalize(allocator);
449}
450
451Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
452 return codegen_->GetAssembler();
453}
454
455void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100456 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700457 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
458}
459
460void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100461 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700462 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
463}
464
465void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
466 // Pop reg
467 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200468 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700469}
470
471void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
472 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200473 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700474 __ Sd(GpuRegister(reg), SP, 0);
475}
476
477void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
478 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
479 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
480 // Allocate a scratch register other than TMP, if available.
481 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
482 // automatically unspilled when the scratch scope object is destroyed).
483 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
484 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200485 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700486 __ LoadFromOffset(load_type,
487 GpuRegister(ensure_scratch.GetRegister()),
488 SP,
489 index1 + stack_offset);
490 __ LoadFromOffset(load_type,
491 TMP,
492 SP,
493 index2 + stack_offset);
494 __ StoreToOffset(store_type,
495 GpuRegister(ensure_scratch.GetRegister()),
496 SP,
497 index2 + stack_offset);
498 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
499}
500
501static dwarf::Reg DWARFReg(GpuRegister reg) {
502 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
503}
504
David Srbeckyba702002016-02-01 18:15:29 +0000505static dwarf::Reg DWARFReg(FpuRegister reg) {
506 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
507}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700508
509void CodeGeneratorMIPS64::GenerateFrameEntry() {
510 __ Bind(&frame_entry_label_);
511
512 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
513
514 if (do_overflow_check) {
515 __ LoadFromOffset(kLoadWord,
516 ZERO,
517 SP,
518 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
519 RecordPcInfo(nullptr, 0);
520 }
521
522 // TODO: anything related to T9/GP/GOT/PIC/.so's?
523
524 if (HasEmptyFrame()) {
525 return;
526 }
527
528 // Make sure the frame size isn't unreasonably large. Per the various APIs
529 // it looks like it should always be less than 2GB in size, which allows
530 // us using 32-bit signed offsets from the stack pointer.
531 if (GetFrameSize() > 0x7FFFFFFF)
532 LOG(FATAL) << "Stack frame larger than 2GB";
533
534 // Spill callee-saved registers.
535 // Note that their cumulative size is small and they can be indexed using
536 // 16-bit offsets.
537
538 // TODO: increment/decrement SP in one step instead of two or remove this comment.
539
540 uint32_t ofs = FrameEntrySpillSize();
541 __ IncreaseFrameSize(ofs);
542
543 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
544 GpuRegister reg = kCoreCalleeSaves[i];
545 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200546 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700547 __ Sd(reg, SP, ofs);
548 __ cfi().RelOffset(DWARFReg(reg), ofs);
549 }
550 }
551
552 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
553 FpuRegister reg = kFpuCalleeSaves[i];
554 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200555 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700556 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000557 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700558 }
559 }
560
561 // Allocate the rest of the frame and store the current method pointer
562 // at its end.
563
564 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
565
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100566 // Save the current method if we need it. Note that we do not
567 // do this in HCurrentMethod, as the instruction might have been removed
568 // in the SSA graph.
569 if (RequiresCurrentMethod()) {
570 static_assert(IsInt<16>(kCurrentMethodStackOffset),
571 "kCurrentMethodStackOffset must fit into int16_t");
572 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
573 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700574}
575
576void CodeGeneratorMIPS64::GenerateFrameExit() {
577 __ cfi().RememberState();
578
579 // TODO: anything related to T9/GP/GOT/PIC/.so's?
580
581 if (!HasEmptyFrame()) {
582 // Deallocate the rest of the frame.
583
584 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
585
586 // Restore callee-saved registers.
587 // Note that their cumulative size is small and they can be indexed using
588 // 16-bit offsets.
589
590 // TODO: increment/decrement SP in one step instead of two or remove this comment.
591
592 uint32_t ofs = 0;
593
594 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
595 FpuRegister reg = kFpuCalleeSaves[i];
596 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
597 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200598 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000599 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700600 }
601 }
602
603 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
604 GpuRegister reg = kCoreCalleeSaves[i];
605 if (allocated_registers_.ContainsCoreRegister(reg)) {
606 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200607 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700608 __ cfi().Restore(DWARFReg(reg));
609 }
610 }
611
612 DCHECK_EQ(ofs, FrameEntrySpillSize());
613 __ DecreaseFrameSize(ofs);
614 }
615
616 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700617 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700618
619 __ cfi().RestoreState();
620 __ cfi().DefCFAOffset(GetFrameSize());
621}
622
623void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
624 __ Bind(GetLabelOf(block));
625}
626
627void CodeGeneratorMIPS64::MoveLocation(Location destination,
628 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100629 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700630 if (source.Equals(destination)) {
631 return;
632 }
633
634 // A valid move can always be inferred from the destination and source
635 // locations. When moving from and to a register, the argument type can be
636 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100637 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700638 DCHECK_EQ(unspecified_type, false);
639
640 if (destination.IsRegister() || destination.IsFpuRegister()) {
641 if (unspecified_type) {
642 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
643 if (source.IsStackSlot() ||
644 (src_cst != nullptr && (src_cst->IsIntConstant()
645 || src_cst->IsFloatConstant()
646 || src_cst->IsNullConstant()))) {
647 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100648 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700649 } else {
650 // If the source is a double stack slot or a 64bit constant, a 64bit
651 // type is appropriate. Else the source is a register, and since the
652 // type has not been specified, we chose a 64bit type to force a 64bit
653 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100654 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700655 }
656 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100657 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
658 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
660 // Move to GPR/FPR from stack
661 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100662 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 __ LoadFpuFromOffset(load_type,
664 destination.AsFpuRegister<FpuRegister>(),
665 SP,
666 source.GetStackIndex());
667 } else {
668 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
669 __ LoadFromOffset(load_type,
670 destination.AsRegister<GpuRegister>(),
671 SP,
672 source.GetStackIndex());
673 }
674 } else if (source.IsConstant()) {
675 // Move to GPR/FPR from constant
676 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100677 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 gpr = destination.AsRegister<GpuRegister>();
679 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100680 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700681 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
682 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
683 gpr = ZERO;
684 } else {
685 __ LoadConst32(gpr, value);
686 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700687 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700688 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
689 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
690 gpr = ZERO;
691 } else {
692 __ LoadConst64(gpr, value);
693 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100695 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700696 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100697 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700698 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
699 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100700 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 if (destination.IsRegister()) {
702 // Move to GPR from GPR
703 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
704 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100705 DCHECK(destination.IsFpuRegister());
706 if (Primitive::Is64BitType(dst_type)) {
707 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
708 } else {
709 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
710 }
711 }
712 } else if (source.IsFpuRegister()) {
713 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700714 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100715 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
717 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100718 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700719 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
720 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 } else {
722 DCHECK(destination.IsRegister());
723 if (Primitive::Is64BitType(dst_type)) {
724 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
725 } else {
726 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
727 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 }
729 }
730 } else { // The destination is not a register. It must be a stack slot.
731 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
732 if (source.IsRegister() || source.IsFpuRegister()) {
733 if (unspecified_type) {
734 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100735 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700736 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100737 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700738 }
739 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100740 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
741 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 // Move to stack from GPR/FPR
743 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
744 if (source.IsRegister()) {
745 __ StoreToOffset(store_type,
746 source.AsRegister<GpuRegister>(),
747 SP,
748 destination.GetStackIndex());
749 } else {
750 __ StoreFpuToOffset(store_type,
751 source.AsFpuRegister<FpuRegister>(),
752 SP,
753 destination.GetStackIndex());
754 }
755 } else if (source.IsConstant()) {
756 // Move to stack from constant
757 HConstant* src_cst = source.GetConstant();
758 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700759 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700760 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700761 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
762 if (value != 0) {
763 gpr = TMP;
764 __ LoadConst32(gpr, value);
765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 DCHECK(destination.IsDoubleStackSlot());
768 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
769 if (value != 0) {
770 gpr = TMP;
771 __ LoadConst64(gpr, value);
772 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700773 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700774 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700775 } else {
776 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
777 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
778 // Move to stack from stack
779 if (destination.IsStackSlot()) {
780 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
781 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
782 } else {
783 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
784 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
785 }
786 }
787 }
788}
789
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700790void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700791 DCHECK(!loc1.IsConstant());
792 DCHECK(!loc2.IsConstant());
793
794 if (loc1.Equals(loc2)) {
795 return;
796 }
797
798 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
799 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
800 bool is_fp_reg1 = loc1.IsFpuRegister();
801 bool is_fp_reg2 = loc2.IsFpuRegister();
802
803 if (loc2.IsRegister() && loc1.IsRegister()) {
804 // Swap 2 GPRs
805 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
806 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
807 __ Move(TMP, r2);
808 __ Move(r2, r1);
809 __ Move(r1, TMP);
810 } else if (is_fp_reg2 && is_fp_reg1) {
811 // Swap 2 FPRs
812 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
813 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700814 if (type == Primitive::kPrimFloat) {
815 __ MovS(FTMP, r1);
816 __ MovS(r1, r2);
817 __ MovS(r2, FTMP);
818 } else {
819 DCHECK_EQ(type, Primitive::kPrimDouble);
820 __ MovD(FTMP, r1);
821 __ MovD(r1, r2);
822 __ MovD(r2, FTMP);
823 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700824 } else if (is_slot1 != is_slot2) {
825 // Swap GPR/FPR and stack slot
826 Location reg_loc = is_slot1 ? loc2 : loc1;
827 Location mem_loc = is_slot1 ? loc1 : loc2;
828 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
829 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
830 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
831 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
832 if (reg_loc.IsFpuRegister()) {
833 __ StoreFpuToOffset(store_type,
834 reg_loc.AsFpuRegister<FpuRegister>(),
835 SP,
836 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700837 if (mem_loc.IsStackSlot()) {
838 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
839 } else {
840 DCHECK(mem_loc.IsDoubleStackSlot());
841 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
842 }
843 } else {
844 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
845 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
846 }
847 } else if (is_slot1 && is_slot2) {
848 move_resolver_.Exchange(loc1.GetStackIndex(),
849 loc2.GetStackIndex(),
850 loc1.IsDoubleStackSlot());
851 } else {
852 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
853 }
854}
855
Calin Juravle175dc732015-08-25 15:42:32 +0100856void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
857 DCHECK(location.IsRegister());
858 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
859}
860
Calin Juravlee460d1d2015-09-29 04:52:17 +0100861void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
862 if (location.IsRegister()) {
863 locations->AddTemp(location);
864 } else {
865 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
866 }
867}
868
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100869void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
870 GpuRegister value,
871 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700872 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700873 GpuRegister card = AT;
874 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100875 if (value_can_be_null) {
876 __ Beqzc(value, &done);
877 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700878 __ LoadFromOffset(kLoadDoubleword,
879 card,
880 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700881 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700882 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
883 __ Daddu(temp, card, temp);
884 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100885 if (value_can_be_null) {
886 __ Bind(&done);
887 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700888}
889
David Brazdil58282f42016-01-14 12:45:10 +0000890void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700891 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
892 blocked_core_registers_[ZERO] = true;
893 blocked_core_registers_[K0] = true;
894 blocked_core_registers_[K1] = true;
895 blocked_core_registers_[GP] = true;
896 blocked_core_registers_[SP] = true;
897 blocked_core_registers_[RA] = true;
898
Lazar Trsicd9672662015-09-03 17:33:01 +0200899 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
900 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700901 blocked_core_registers_[AT] = true;
902 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200903 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700904 blocked_fpu_registers_[FTMP] = true;
905
906 // Reserve suspend and thread registers.
907 blocked_core_registers_[S0] = true;
908 blocked_core_registers_[TR] = true;
909
910 // Reserve T9 for function calls
911 blocked_core_registers_[T9] = true;
912
913 // TODO: review; anything else?
914
Goran Jakovljevic782be112016-06-21 12:39:04 +0200915 if (GetGraph()->IsDebuggable()) {
916 // Stubs do not save callee-save floating point registers. If the graph
917 // is debuggable, we need to deal with these registers differently. For
918 // now, just block them.
919 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
920 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
921 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700922 }
923}
924
Alexey Frunze4dda3372015-06-01 18:31:49 -0700925size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
926 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200927 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700928}
929
930size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
931 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200932 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700933}
934
935size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
936 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200937 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700938}
939
940size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
941 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200942 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943}
944
945void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100946 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947}
948
949void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100950 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951}
952
Calin Juravle175dc732015-08-25 15:42:32 +0100953void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 HInstruction* instruction,
955 uint32_t dex_pc,
956 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100957 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700958 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100959 __ LoadFromOffset(kLoadDoubleword,
960 T9,
961 TR,
962 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700963 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700964 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100965 if (EntrypointRequiresStackMap(entrypoint)) {
966 RecordPcInfo(instruction, dex_pc, slow_path);
967 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700968}
969
970void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
971 GpuRegister class_reg) {
972 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
973 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
974 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
975 // TODO: barrier needed?
976 __ Bind(slow_path->GetExitLabel());
977}
978
979void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
980 __ Sync(0); // only stype 0 is supported
981}
982
983void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
984 HBasicBlock* successor) {
985 SuspendCheckSlowPathMIPS64* slow_path =
986 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
987 codegen_->AddSlowPath(slow_path);
988
989 __ LoadFromOffset(kLoadUnsignedHalfword,
990 TMP,
991 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700992 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700993 if (successor == nullptr) {
994 __ Bnezc(TMP, slow_path->GetEntryLabel());
995 __ Bind(slow_path->GetReturnLabel());
996 } else {
997 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700998 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700999 // slow_path will return to GetLabelOf(successor).
1000 }
1001}
1002
1003InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1004 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001005 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001006 assembler_(codegen->GetAssembler()),
1007 codegen_(codegen) {}
1008
1009void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1010 DCHECK_EQ(instruction->InputCount(), 2U);
1011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1012 Primitive::Type type = instruction->GetResultType();
1013 switch (type) {
1014 case Primitive::kPrimInt:
1015 case Primitive::kPrimLong: {
1016 locations->SetInAt(0, Location::RequiresRegister());
1017 HInstruction* right = instruction->InputAt(1);
1018 bool can_use_imm = false;
1019 if (right->IsConstant()) {
1020 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1021 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1022 can_use_imm = IsUint<16>(imm);
1023 } else if (instruction->IsAdd()) {
1024 can_use_imm = IsInt<16>(imm);
1025 } else {
1026 DCHECK(instruction->IsSub());
1027 can_use_imm = IsInt<16>(-imm);
1028 }
1029 }
1030 if (can_use_imm)
1031 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1032 else
1033 locations->SetInAt(1, Location::RequiresRegister());
1034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1035 }
1036 break;
1037
1038 case Primitive::kPrimFloat:
1039 case Primitive::kPrimDouble:
1040 locations->SetInAt(0, Location::RequiresFpuRegister());
1041 locations->SetInAt(1, Location::RequiresFpuRegister());
1042 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1043 break;
1044
1045 default:
1046 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1047 }
1048}
1049
1050void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1051 Primitive::Type type = instruction->GetType();
1052 LocationSummary* locations = instruction->GetLocations();
1053
1054 switch (type) {
1055 case Primitive::kPrimInt:
1056 case Primitive::kPrimLong: {
1057 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1058 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1059 Location rhs_location = locations->InAt(1);
1060
1061 GpuRegister rhs_reg = ZERO;
1062 int64_t rhs_imm = 0;
1063 bool use_imm = rhs_location.IsConstant();
1064 if (use_imm) {
1065 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1066 } else {
1067 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1068 }
1069
1070 if (instruction->IsAnd()) {
1071 if (use_imm)
1072 __ Andi(dst, lhs, rhs_imm);
1073 else
1074 __ And(dst, lhs, rhs_reg);
1075 } else if (instruction->IsOr()) {
1076 if (use_imm)
1077 __ Ori(dst, lhs, rhs_imm);
1078 else
1079 __ Or(dst, lhs, rhs_reg);
1080 } else if (instruction->IsXor()) {
1081 if (use_imm)
1082 __ Xori(dst, lhs, rhs_imm);
1083 else
1084 __ Xor(dst, lhs, rhs_reg);
1085 } else if (instruction->IsAdd()) {
1086 if (type == Primitive::kPrimInt) {
1087 if (use_imm)
1088 __ Addiu(dst, lhs, rhs_imm);
1089 else
1090 __ Addu(dst, lhs, rhs_reg);
1091 } else {
1092 if (use_imm)
1093 __ Daddiu(dst, lhs, rhs_imm);
1094 else
1095 __ Daddu(dst, lhs, rhs_reg);
1096 }
1097 } else {
1098 DCHECK(instruction->IsSub());
1099 if (type == Primitive::kPrimInt) {
1100 if (use_imm)
1101 __ Addiu(dst, lhs, -rhs_imm);
1102 else
1103 __ Subu(dst, lhs, rhs_reg);
1104 } else {
1105 if (use_imm)
1106 __ Daddiu(dst, lhs, -rhs_imm);
1107 else
1108 __ Dsubu(dst, lhs, rhs_reg);
1109 }
1110 }
1111 break;
1112 }
1113 case Primitive::kPrimFloat:
1114 case Primitive::kPrimDouble: {
1115 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1116 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1117 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1118 if (instruction->IsAdd()) {
1119 if (type == Primitive::kPrimFloat)
1120 __ AddS(dst, lhs, rhs);
1121 else
1122 __ AddD(dst, lhs, rhs);
1123 } else if (instruction->IsSub()) {
1124 if (type == Primitive::kPrimFloat)
1125 __ SubS(dst, lhs, rhs);
1126 else
1127 __ SubD(dst, lhs, rhs);
1128 } else {
1129 LOG(FATAL) << "Unexpected floating-point binary operation";
1130 }
1131 break;
1132 }
1133 default:
1134 LOG(FATAL) << "Unexpected binary operation type " << type;
1135 }
1136}
1137
1138void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001139 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001140
1141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1142 Primitive::Type type = instr->GetResultType();
1143 switch (type) {
1144 case Primitive::kPrimInt:
1145 case Primitive::kPrimLong: {
1146 locations->SetInAt(0, Location::RequiresRegister());
1147 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149 break;
1150 }
1151 default:
1152 LOG(FATAL) << "Unexpected shift type " << type;
1153 }
1154}
1155
1156void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001157 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001158 LocationSummary* locations = instr->GetLocations();
1159 Primitive::Type type = instr->GetType();
1160
1161 switch (type) {
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimLong: {
1164 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1165 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1166 Location rhs_location = locations->InAt(1);
1167
1168 GpuRegister rhs_reg = ZERO;
1169 int64_t rhs_imm = 0;
1170 bool use_imm = rhs_location.IsConstant();
1171 if (use_imm) {
1172 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1173 } else {
1174 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1175 }
1176
1177 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001178 uint32_t shift_value = rhs_imm &
1179 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001180
Alexey Frunze92d90602015-12-18 18:16:36 -08001181 if (shift_value == 0) {
1182 if (dst != lhs) {
1183 __ Move(dst, lhs);
1184 }
1185 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 if (instr->IsShl()) {
1187 __ Sll(dst, lhs, shift_value);
1188 } else if (instr->IsShr()) {
1189 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001190 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001192 } else {
1193 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 }
1195 } else {
1196 if (shift_value < 32) {
1197 if (instr->IsShl()) {
1198 __ Dsll(dst, lhs, shift_value);
1199 } else if (instr->IsShr()) {
1200 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001201 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001202 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001203 } else {
1204 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001205 }
1206 } else {
1207 shift_value -= 32;
1208 if (instr->IsShl()) {
1209 __ Dsll32(dst, lhs, shift_value);
1210 } else if (instr->IsShr()) {
1211 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001212 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001213 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001214 } else {
1215 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001216 }
1217 }
1218 }
1219 } else {
1220 if (type == Primitive::kPrimInt) {
1221 if (instr->IsShl()) {
1222 __ Sllv(dst, lhs, rhs_reg);
1223 } else if (instr->IsShr()) {
1224 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001225 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001226 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001227 } else {
1228 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 }
1230 } else {
1231 if (instr->IsShl()) {
1232 __ Dsllv(dst, lhs, rhs_reg);
1233 } else if (instr->IsShr()) {
1234 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001235 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001236 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001237 } else {
1238 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 }
1240 }
1241 }
1242 break;
1243 }
1244 default:
1245 LOG(FATAL) << "Unexpected shift operation type " << type;
1246 }
1247}
1248
1249void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1250 HandleBinaryOp(instruction);
1251}
1252
1253void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1254 HandleBinaryOp(instruction);
1255}
1256
1257void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1258 HandleBinaryOp(instruction);
1259}
1260
1261void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1262 HandleBinaryOp(instruction);
1263}
1264
1265void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1266 LocationSummary* locations =
1267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1268 locations->SetInAt(0, Location::RequiresRegister());
1269 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1270 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1271 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1272 } else {
1273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1274 }
1275}
1276
1277void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1278 LocationSummary* locations = instruction->GetLocations();
1279 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1280 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001281 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001282
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001283 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001284 switch (type) {
1285 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001286 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1287 if (index.IsConstant()) {
1288 size_t offset =
1289 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1290 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1291 } else {
1292 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1293 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1294 }
1295 break;
1296 }
1297
1298 case Primitive::kPrimByte: {
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(kLoadSignedByte, out, obj, offset);
1304 } else {
1305 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1306 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1307 }
1308 break;
1309 }
1310
1311 case Primitive::kPrimShort: {
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_2) + data_offset;
1316 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1317 } else {
1318 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1319 __ Daddu(TMP, obj, TMP);
1320 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1321 }
1322 break;
1323 }
1324
1325 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001326 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1327 if (index.IsConstant()) {
1328 size_t offset =
1329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1330 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1331 } else {
1332 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1333 __ Daddu(TMP, obj, TMP);
1334 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1335 }
1336 break;
1337 }
1338
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimNot: {
1341 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001342 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1343 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1344 if (index.IsConstant()) {
1345 size_t offset =
1346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1347 __ LoadFromOffset(load_type, out, obj, offset);
1348 } else {
1349 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1350 __ Daddu(TMP, obj, TMP);
1351 __ LoadFromOffset(load_type, out, TMP, data_offset);
1352 }
1353 break;
1354 }
1355
1356 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001357 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1358 if (index.IsConstant()) {
1359 size_t offset =
1360 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1361 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1362 } else {
1363 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1364 __ Daddu(TMP, obj, TMP);
1365 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1366 }
1367 break;
1368 }
1369
1370 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001371 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1372 if (index.IsConstant()) {
1373 size_t offset =
1374 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1375 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1376 } else {
1377 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1378 __ Daddu(TMP, obj, TMP);
1379 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1380 }
1381 break;
1382 }
1383
1384 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001385 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1386 if (index.IsConstant()) {
1387 size_t offset =
1388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1389 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1390 } else {
1391 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1392 __ Daddu(TMP, obj, TMP);
1393 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1394 }
1395 break;
1396 }
1397
1398 case Primitive::kPrimVoid:
1399 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1400 UNREACHABLE();
1401 }
1402 codegen_->MaybeRecordImplicitNullCheck(instruction);
1403}
1404
1405void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1407 locations->SetInAt(0, Location::RequiresRegister());
1408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1409}
1410
1411void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1412 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001413 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1415 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1416 __ LoadFromOffset(kLoadWord, out, obj, offset);
1417 codegen_->MaybeRecordImplicitNullCheck(instruction);
1418}
1419
1420void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001421 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1423 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001424 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001425 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001426 InvokeRuntimeCallingConvention calling_convention;
1427 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1428 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1429 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1430 } else {
1431 locations->SetInAt(0, Location::RequiresRegister());
1432 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1433 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1434 locations->SetInAt(2, Location::RequiresFpuRegister());
1435 } else {
1436 locations->SetInAt(2, Location::RequiresRegister());
1437 }
1438 }
1439}
1440
1441void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1442 LocationSummary* locations = instruction->GetLocations();
1443 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1444 Location index = locations->InAt(1);
1445 Primitive::Type value_type = instruction->GetComponentType();
1446 bool needs_runtime_call = locations->WillCall();
1447 bool needs_write_barrier =
1448 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1449
1450 switch (value_type) {
1451 case Primitive::kPrimBoolean:
1452 case Primitive::kPrimByte: {
1453 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1454 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1455 if (index.IsConstant()) {
1456 size_t offset =
1457 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1458 __ StoreToOffset(kStoreByte, value, obj, offset);
1459 } else {
1460 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1461 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1462 }
1463 break;
1464 }
1465
1466 case Primitive::kPrimShort:
1467 case Primitive::kPrimChar: {
1468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1469 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1470 if (index.IsConstant()) {
1471 size_t offset =
1472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1473 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1474 } else {
1475 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1476 __ Daddu(TMP, obj, TMP);
1477 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1478 }
1479 break;
1480 }
1481
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimNot: {
1484 if (!needs_runtime_call) {
1485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1486 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1487 if (index.IsConstant()) {
1488 size_t offset =
1489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1490 __ StoreToOffset(kStoreWord, value, obj, offset);
1491 } else {
1492 DCHECK(index.IsRegister()) << index;
1493 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1494 __ Daddu(TMP, obj, TMP);
1495 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1496 }
1497 codegen_->MaybeRecordImplicitNullCheck(instruction);
1498 if (needs_write_barrier) {
1499 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001500 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001501 }
1502 } else {
1503 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001504 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001505 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001506 }
1507 break;
1508 }
1509
1510 case Primitive::kPrimLong: {
1511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1512 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1513 if (index.IsConstant()) {
1514 size_t offset =
1515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1516 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1517 } else {
1518 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1519 __ Daddu(TMP, obj, TMP);
1520 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1521 }
1522 break;
1523 }
1524
1525 case Primitive::kPrimFloat: {
1526 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1527 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1528 DCHECK(locations->InAt(2).IsFpuRegister());
1529 if (index.IsConstant()) {
1530 size_t offset =
1531 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1532 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1533 } else {
1534 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1535 __ Daddu(TMP, obj, TMP);
1536 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1537 }
1538 break;
1539 }
1540
1541 case Primitive::kPrimDouble: {
1542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).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_8) + data_offset;
1548 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1549 } else {
1550 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1551 __ Daddu(TMP, obj, TMP);
1552 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1553 }
1554 break;
1555 }
1556
1557 case Primitive::kPrimVoid:
1558 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1559 UNREACHABLE();
1560 }
1561
1562 // Ints and objects are handled in the switch.
1563 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1564 codegen_->MaybeRecordImplicitNullCheck(instruction);
1565 }
1566}
1567
1568void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001569 RegisterSet caller_saves = RegisterSet::Empty();
1570 InvokeRuntimeCallingConvention calling_convention;
1571 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1572 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1573 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001574 locations->SetInAt(0, Location::RequiresRegister());
1575 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001576}
1577
1578void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1579 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001580 BoundsCheckSlowPathMIPS64* slow_path =
1581 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001582 codegen_->AddSlowPath(slow_path);
1583
1584 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1585 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1586
1587 // length is limited by the maximum positive signed 32-bit integer.
1588 // Unsigned comparison of length and index checks for index < 0
1589 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001590 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001591}
1592
1593void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1595 instruction,
1596 LocationSummary::kCallOnSlowPath);
1597 locations->SetInAt(0, Location::RequiresRegister());
1598 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001599 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001600 locations->AddTemp(Location::RequiresRegister());
1601}
1602
1603void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1604 LocationSummary* locations = instruction->GetLocations();
1605 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1606 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1607 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1608
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001609 SlowPathCodeMIPS64* slow_path =
1610 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611 codegen_->AddSlowPath(slow_path);
1612
1613 // TODO: avoid this check if we know obj is not null.
1614 __ Beqzc(obj, slow_path->GetExitLabel());
1615 // Compare the class of `obj` with `cls`.
1616 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1617 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1618 __ Bind(slow_path->GetExitLabel());
1619}
1620
1621void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1622 LocationSummary* locations =
1623 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1624 locations->SetInAt(0, Location::RequiresRegister());
1625 if (check->HasUses()) {
1626 locations->SetOut(Location::SameAsFirstInput());
1627 }
1628}
1629
1630void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1631 // We assume the class is not null.
1632 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1633 check->GetLoadClass(),
1634 check,
1635 check->GetDexPc(),
1636 true);
1637 codegen_->AddSlowPath(slow_path);
1638 GenerateClassInitializationCheck(slow_path,
1639 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1640}
1641
1642void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1643 Primitive::Type in_type = compare->InputAt(0)->GetType();
1644
Alexey Frunze299a9392015-12-08 16:08:02 -08001645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646
1647 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001648 case Primitive::kPrimBoolean:
1649 case Primitive::kPrimByte:
1650 case Primitive::kPrimShort:
1651 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001652 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001653 case Primitive::kPrimLong:
1654 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001655 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1657 break;
1658
1659 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001660 case Primitive::kPrimDouble:
1661 locations->SetInAt(0, Location::RequiresFpuRegister());
1662 locations->SetInAt(1, Location::RequiresFpuRegister());
1663 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001664 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001665
1666 default:
1667 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1668 }
1669}
1670
1671void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1672 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001673 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1675
1676 // 0 if: left == right
1677 // 1 if: left > right
1678 // -1 if: left < right
1679 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001680 case Primitive::kPrimBoolean:
1681 case Primitive::kPrimByte:
1682 case Primitive::kPrimShort:
1683 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001684 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001686 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001687 Location rhs_location = locations->InAt(1);
1688 bool use_imm = rhs_location.IsConstant();
1689 GpuRegister rhs = ZERO;
1690 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001691 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001692 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1693 if (value != 0) {
1694 rhs = AT;
1695 __ LoadConst64(rhs, value);
1696 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001697 } else {
1698 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1699 if (value != 0) {
1700 rhs = AT;
1701 __ LoadConst32(rhs, value);
1702 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001703 }
1704 } else {
1705 rhs = rhs_location.AsRegister<GpuRegister>();
1706 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001708 __ Slt(res, rhs, lhs);
1709 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710 break;
1711 }
1712
Alexey Frunze299a9392015-12-08 16:08:02 -08001713 case Primitive::kPrimFloat: {
1714 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1715 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1716 Mips64Label done;
1717 __ CmpEqS(FTMP, lhs, rhs);
1718 __ LoadConst32(res, 0);
1719 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001720 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001721 __ CmpLtS(FTMP, lhs, rhs);
1722 __ LoadConst32(res, -1);
1723 __ Bc1nez(FTMP, &done);
1724 __ LoadConst32(res, 1);
1725 } else {
1726 __ CmpLtS(FTMP, rhs, lhs);
1727 __ LoadConst32(res, 1);
1728 __ Bc1nez(FTMP, &done);
1729 __ LoadConst32(res, -1);
1730 }
1731 __ Bind(&done);
1732 break;
1733 }
1734
Alexey Frunze4dda3372015-06-01 18:31:49 -07001735 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001736 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1737 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1738 Mips64Label done;
1739 __ CmpEqD(FTMP, lhs, rhs);
1740 __ LoadConst32(res, 0);
1741 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001742 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001743 __ CmpLtD(FTMP, lhs, rhs);
1744 __ LoadConst32(res, -1);
1745 __ Bc1nez(FTMP, &done);
1746 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001748 __ CmpLtD(FTMP, rhs, lhs);
1749 __ LoadConst32(res, 1);
1750 __ Bc1nez(FTMP, &done);
1751 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001752 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001753 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754 break;
1755 }
1756
1757 default:
1758 LOG(FATAL) << "Unimplemented compare type " << in_type;
1759 }
1760}
1761
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001764 switch (instruction->InputAt(0)->GetType()) {
1765 default:
1766 case Primitive::kPrimLong:
1767 locations->SetInAt(0, Location::RequiresRegister());
1768 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1769 break;
1770
1771 case Primitive::kPrimFloat:
1772 case Primitive::kPrimDouble:
1773 locations->SetInAt(0, Location::RequiresFpuRegister());
1774 locations->SetInAt(1, Location::RequiresFpuRegister());
1775 break;
1776 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001777 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1779 }
1780}
1781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001783 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 return;
1785 }
1786
Alexey Frunze299a9392015-12-08 16:08:02 -08001787 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001788 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001789 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001790 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791
Alexey Frunze299a9392015-12-08 16:08:02 -08001792 switch (type) {
1793 default:
1794 // Integer case.
1795 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1796 return;
1797 case Primitive::kPrimLong:
1798 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1799 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001800
Alexey Frunze299a9392015-12-08 16:08:02 -08001801 case Primitive::kPrimFloat:
1802 case Primitive::kPrimDouble:
1803 // TODO: don't use branches.
1804 GenerateFpCompareAndBranch(instruction->GetCondition(),
1805 instruction->IsGtBias(),
1806 type,
1807 locations,
1808 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001809 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001810 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001811
1812 // Convert the branches into the result.
1813 Mips64Label done;
1814
1815 // False case: result = 0.
1816 __ LoadConst32(dst, 0);
1817 __ Bc(&done);
1818
1819 // True case: result = 1.
1820 __ Bind(&true_label);
1821 __ LoadConst32(dst, 1);
1822 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001823}
1824
Alexey Frunzec857c742015-09-23 15:12:39 -07001825void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1826 DCHECK(instruction->IsDiv() || instruction->IsRem());
1827 Primitive::Type type = instruction->GetResultType();
1828
1829 LocationSummary* locations = instruction->GetLocations();
1830 Location second = locations->InAt(1);
1831 DCHECK(second.IsConstant());
1832
1833 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1834 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1835 int64_t imm = Int64FromConstant(second.GetConstant());
1836 DCHECK(imm == 1 || imm == -1);
1837
1838 if (instruction->IsRem()) {
1839 __ Move(out, ZERO);
1840 } else {
1841 if (imm == -1) {
1842 if (type == Primitive::kPrimInt) {
1843 __ Subu(out, ZERO, dividend);
1844 } else {
1845 DCHECK_EQ(type, Primitive::kPrimLong);
1846 __ Dsubu(out, ZERO, dividend);
1847 }
1848 } else if (out != dividend) {
1849 __ Move(out, dividend);
1850 }
1851 }
1852}
1853
1854void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1855 DCHECK(instruction->IsDiv() || instruction->IsRem());
1856 Primitive::Type type = instruction->GetResultType();
1857
1858 LocationSummary* locations = instruction->GetLocations();
1859 Location second = locations->InAt(1);
1860 DCHECK(second.IsConstant());
1861
1862 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1863 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1864 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001865 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001866 int ctz_imm = CTZ(abs_imm);
1867
1868 if (instruction->IsDiv()) {
1869 if (type == Primitive::kPrimInt) {
1870 if (ctz_imm == 1) {
1871 // Fast path for division by +/-2, which is very common.
1872 __ Srl(TMP, dividend, 31);
1873 } else {
1874 __ Sra(TMP, dividend, 31);
1875 __ Srl(TMP, TMP, 32 - ctz_imm);
1876 }
1877 __ Addu(out, dividend, TMP);
1878 __ Sra(out, out, ctz_imm);
1879 if (imm < 0) {
1880 __ Subu(out, ZERO, out);
1881 }
1882 } else {
1883 DCHECK_EQ(type, Primitive::kPrimLong);
1884 if (ctz_imm == 1) {
1885 // Fast path for division by +/-2, which is very common.
1886 __ Dsrl32(TMP, dividend, 31);
1887 } else {
1888 __ Dsra32(TMP, dividend, 31);
1889 if (ctz_imm > 32) {
1890 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1891 } else {
1892 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1893 }
1894 }
1895 __ Daddu(out, dividend, TMP);
1896 if (ctz_imm < 32) {
1897 __ Dsra(out, out, ctz_imm);
1898 } else {
1899 __ Dsra32(out, out, ctz_imm - 32);
1900 }
1901 if (imm < 0) {
1902 __ Dsubu(out, ZERO, out);
1903 }
1904 }
1905 } else {
1906 if (type == Primitive::kPrimInt) {
1907 if (ctz_imm == 1) {
1908 // Fast path for modulo +/-2, which is very common.
1909 __ Sra(TMP, dividend, 31);
1910 __ Subu(out, dividend, TMP);
1911 __ Andi(out, out, 1);
1912 __ Addu(out, out, TMP);
1913 } else {
1914 __ Sra(TMP, dividend, 31);
1915 __ Srl(TMP, TMP, 32 - ctz_imm);
1916 __ Addu(out, dividend, TMP);
1917 if (IsUint<16>(abs_imm - 1)) {
1918 __ Andi(out, out, abs_imm - 1);
1919 } else {
1920 __ Sll(out, out, 32 - ctz_imm);
1921 __ Srl(out, out, 32 - ctz_imm);
1922 }
1923 __ Subu(out, out, TMP);
1924 }
1925 } else {
1926 DCHECK_EQ(type, Primitive::kPrimLong);
1927 if (ctz_imm == 1) {
1928 // Fast path for modulo +/-2, which is very common.
1929 __ Dsra32(TMP, dividend, 31);
1930 __ Dsubu(out, dividend, TMP);
1931 __ Andi(out, out, 1);
1932 __ Daddu(out, out, TMP);
1933 } else {
1934 __ Dsra32(TMP, dividend, 31);
1935 if (ctz_imm > 32) {
1936 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1937 } else {
1938 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1939 }
1940 __ Daddu(out, dividend, TMP);
1941 if (IsUint<16>(abs_imm - 1)) {
1942 __ Andi(out, out, abs_imm - 1);
1943 } else {
1944 if (ctz_imm > 32) {
1945 __ Dsll(out, out, 64 - ctz_imm);
1946 __ Dsrl(out, out, 64 - ctz_imm);
1947 } else {
1948 __ Dsll32(out, out, 32 - ctz_imm);
1949 __ Dsrl32(out, out, 32 - ctz_imm);
1950 }
1951 }
1952 __ Dsubu(out, out, TMP);
1953 }
1954 }
1955 }
1956}
1957
1958void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1959 DCHECK(instruction->IsDiv() || instruction->IsRem());
1960
1961 LocationSummary* locations = instruction->GetLocations();
1962 Location second = locations->InAt(1);
1963 DCHECK(second.IsConstant());
1964
1965 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1966 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1967 int64_t imm = Int64FromConstant(second.GetConstant());
1968
1969 Primitive::Type type = instruction->GetResultType();
1970 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1971
1972 int64_t magic;
1973 int shift;
1974 CalculateMagicAndShiftForDivRem(imm,
1975 (type == Primitive::kPrimLong),
1976 &magic,
1977 &shift);
1978
1979 if (type == Primitive::kPrimInt) {
1980 __ LoadConst32(TMP, magic);
1981 __ MuhR6(TMP, dividend, TMP);
1982
1983 if (imm > 0 && magic < 0) {
1984 __ Addu(TMP, TMP, dividend);
1985 } else if (imm < 0 && magic > 0) {
1986 __ Subu(TMP, TMP, dividend);
1987 }
1988
1989 if (shift != 0) {
1990 __ Sra(TMP, TMP, shift);
1991 }
1992
1993 if (instruction->IsDiv()) {
1994 __ Sra(out, TMP, 31);
1995 __ Subu(out, TMP, out);
1996 } else {
1997 __ Sra(AT, TMP, 31);
1998 __ Subu(AT, TMP, AT);
1999 __ LoadConst32(TMP, imm);
2000 __ MulR6(TMP, AT, TMP);
2001 __ Subu(out, dividend, TMP);
2002 }
2003 } else {
2004 __ LoadConst64(TMP, magic);
2005 __ Dmuh(TMP, dividend, TMP);
2006
2007 if (imm > 0 && magic < 0) {
2008 __ Daddu(TMP, TMP, dividend);
2009 } else if (imm < 0 && magic > 0) {
2010 __ Dsubu(TMP, TMP, dividend);
2011 }
2012
2013 if (shift >= 32) {
2014 __ Dsra32(TMP, TMP, shift - 32);
2015 } else if (shift > 0) {
2016 __ Dsra(TMP, TMP, shift);
2017 }
2018
2019 if (instruction->IsDiv()) {
2020 __ Dsra32(out, TMP, 31);
2021 __ Dsubu(out, TMP, out);
2022 } else {
2023 __ Dsra32(AT, TMP, 31);
2024 __ Dsubu(AT, TMP, AT);
2025 __ LoadConst64(TMP, imm);
2026 __ Dmul(TMP, AT, TMP);
2027 __ Dsubu(out, dividend, TMP);
2028 }
2029 }
2030}
2031
2032void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2033 DCHECK(instruction->IsDiv() || instruction->IsRem());
2034 Primitive::Type type = instruction->GetResultType();
2035 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2036
2037 LocationSummary* locations = instruction->GetLocations();
2038 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2039 Location second = locations->InAt(1);
2040
2041 if (second.IsConstant()) {
2042 int64_t imm = Int64FromConstant(second.GetConstant());
2043 if (imm == 0) {
2044 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2045 } else if (imm == 1 || imm == -1) {
2046 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002047 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002048 DivRemByPowerOfTwo(instruction);
2049 } else {
2050 DCHECK(imm <= -2 || imm >= 2);
2051 GenerateDivRemWithAnyConstant(instruction);
2052 }
2053 } else {
2054 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2055 GpuRegister divisor = second.AsRegister<GpuRegister>();
2056 if (instruction->IsDiv()) {
2057 if (type == Primitive::kPrimInt)
2058 __ DivR6(out, dividend, divisor);
2059 else
2060 __ Ddiv(out, dividend, divisor);
2061 } else {
2062 if (type == Primitive::kPrimInt)
2063 __ ModR6(out, dividend, divisor);
2064 else
2065 __ Dmod(out, dividend, divisor);
2066 }
2067 }
2068}
2069
Alexey Frunze4dda3372015-06-01 18:31:49 -07002070void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2071 LocationSummary* locations =
2072 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2073 switch (div->GetResultType()) {
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimLong:
2076 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002077 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002078 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2079 break;
2080
2081 case Primitive::kPrimFloat:
2082 case Primitive::kPrimDouble:
2083 locations->SetInAt(0, Location::RequiresFpuRegister());
2084 locations->SetInAt(1, Location::RequiresFpuRegister());
2085 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2086 break;
2087
2088 default:
2089 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2090 }
2091}
2092
2093void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2094 Primitive::Type type = instruction->GetType();
2095 LocationSummary* locations = instruction->GetLocations();
2096
2097 switch (type) {
2098 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002099 case Primitive::kPrimLong:
2100 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002101 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002102 case Primitive::kPrimFloat:
2103 case Primitive::kPrimDouble: {
2104 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2105 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2106 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2107 if (type == Primitive::kPrimFloat)
2108 __ DivS(dst, lhs, rhs);
2109 else
2110 __ DivD(dst, lhs, rhs);
2111 break;
2112 }
2113 default:
2114 LOG(FATAL) << "Unexpected div type " << type;
2115 }
2116}
2117
2118void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002119 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002120 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121}
2122
2123void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2124 SlowPathCodeMIPS64* slow_path =
2125 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2126 codegen_->AddSlowPath(slow_path);
2127 Location value = instruction->GetLocations()->InAt(0);
2128
2129 Primitive::Type type = instruction->GetType();
2130
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002131 if (!Primitive::IsIntegralType(type)) {
2132 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002133 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002134 }
2135
2136 if (value.IsConstant()) {
2137 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2138 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002139 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 } else {
2141 // A division by a non-null constant is valid. We don't need to perform
2142 // any check, so simply fall through.
2143 }
2144 } else {
2145 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2146 }
2147}
2148
2149void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
2155void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2156 // Will be generated at use site.
2157}
2158
2159void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2160 exit->SetLocations(nullptr);
2161}
2162
2163void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2164}
2165
2166void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2169 locations->SetOut(Location::ConstantLocation(constant));
2170}
2171
2172void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2173 // Will be generated at use site.
2174}
2175
David Brazdilfc6a86a2015-06-26 10:33:45 +00002176void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 DCHECK(!successor->IsExitBlock());
2178 HBasicBlock* block = got->GetBlock();
2179 HInstruction* previous = got->GetPrevious();
2180 HLoopInformation* info = block->GetLoopInformation();
2181
2182 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2183 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2184 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2185 return;
2186 }
2187 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2188 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2189 }
2190 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002191 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 }
2193}
2194
David Brazdilfc6a86a2015-06-26 10:33:45 +00002195void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2196 got->SetLocations(nullptr);
2197}
2198
2199void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2200 HandleGoto(got, got->GetSuccessor());
2201}
2202
2203void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2204 try_boundary->SetLocations(nullptr);
2205}
2206
2207void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2208 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2209 if (!successor->IsExitBlock()) {
2210 HandleGoto(try_boundary, successor);
2211 }
2212}
2213
Alexey Frunze299a9392015-12-08 16:08:02 -08002214void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2215 bool is64bit,
2216 LocationSummary* locations) {
2217 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2218 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2219 Location rhs_location = locations->InAt(1);
2220 GpuRegister rhs_reg = ZERO;
2221 int64_t rhs_imm = 0;
2222 bool use_imm = rhs_location.IsConstant();
2223 if (use_imm) {
2224 if (is64bit) {
2225 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2226 } else {
2227 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2228 }
2229 } else {
2230 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2231 }
2232 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2233
2234 switch (cond) {
2235 case kCondEQ:
2236 case kCondNE:
2237 if (use_imm && IsUint<16>(rhs_imm)) {
2238 __ Xori(dst, lhs, rhs_imm);
2239 } else {
2240 if (use_imm) {
2241 rhs_reg = TMP;
2242 __ LoadConst64(rhs_reg, rhs_imm);
2243 }
2244 __ Xor(dst, lhs, rhs_reg);
2245 }
2246 if (cond == kCondEQ) {
2247 __ Sltiu(dst, dst, 1);
2248 } else {
2249 __ Sltu(dst, ZERO, dst);
2250 }
2251 break;
2252
2253 case kCondLT:
2254 case kCondGE:
2255 if (use_imm && IsInt<16>(rhs_imm)) {
2256 __ Slti(dst, lhs, rhs_imm);
2257 } else {
2258 if (use_imm) {
2259 rhs_reg = TMP;
2260 __ LoadConst64(rhs_reg, rhs_imm);
2261 }
2262 __ Slt(dst, lhs, rhs_reg);
2263 }
2264 if (cond == kCondGE) {
2265 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2266 // only the slt instruction but no sge.
2267 __ Xori(dst, dst, 1);
2268 }
2269 break;
2270
2271 case kCondLE:
2272 case kCondGT:
2273 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2274 // Simulate lhs <= rhs via lhs < rhs + 1.
2275 __ Slti(dst, lhs, rhs_imm_plus_one);
2276 if (cond == kCondGT) {
2277 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2278 // only the slti instruction but no sgti.
2279 __ Xori(dst, dst, 1);
2280 }
2281 } else {
2282 if (use_imm) {
2283 rhs_reg = TMP;
2284 __ LoadConst64(rhs_reg, rhs_imm);
2285 }
2286 __ Slt(dst, rhs_reg, lhs);
2287 if (cond == kCondLE) {
2288 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2289 // only the slt instruction but no sle.
2290 __ Xori(dst, dst, 1);
2291 }
2292 }
2293 break;
2294
2295 case kCondB:
2296 case kCondAE:
2297 if (use_imm && IsInt<16>(rhs_imm)) {
2298 // Sltiu sign-extends its 16-bit immediate operand before
2299 // the comparison and thus lets us compare directly with
2300 // unsigned values in the ranges [0, 0x7fff] and
2301 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2302 __ Sltiu(dst, lhs, rhs_imm);
2303 } else {
2304 if (use_imm) {
2305 rhs_reg = TMP;
2306 __ LoadConst64(rhs_reg, rhs_imm);
2307 }
2308 __ Sltu(dst, lhs, rhs_reg);
2309 }
2310 if (cond == kCondAE) {
2311 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2312 // only the sltu instruction but no sgeu.
2313 __ Xori(dst, dst, 1);
2314 }
2315 break;
2316
2317 case kCondBE:
2318 case kCondA:
2319 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2320 // Simulate lhs <= rhs via lhs < rhs + 1.
2321 // Note that this only works if rhs + 1 does not overflow
2322 // to 0, hence the check above.
2323 // Sltiu sign-extends its 16-bit immediate operand before
2324 // the comparison and thus lets us compare directly with
2325 // unsigned values in the ranges [0, 0x7fff] and
2326 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2327 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2328 if (cond == kCondA) {
2329 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2330 // only the sltiu instruction but no sgtiu.
2331 __ Xori(dst, dst, 1);
2332 }
2333 } else {
2334 if (use_imm) {
2335 rhs_reg = TMP;
2336 __ LoadConst64(rhs_reg, rhs_imm);
2337 }
2338 __ Sltu(dst, rhs_reg, lhs);
2339 if (cond == kCondBE) {
2340 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2341 // only the sltu instruction but no sleu.
2342 __ Xori(dst, dst, 1);
2343 }
2344 }
2345 break;
2346 }
2347}
2348
2349void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2350 bool is64bit,
2351 LocationSummary* locations,
2352 Mips64Label* label) {
2353 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2354 Location rhs_location = locations->InAt(1);
2355 GpuRegister rhs_reg = ZERO;
2356 int64_t rhs_imm = 0;
2357 bool use_imm = rhs_location.IsConstant();
2358 if (use_imm) {
2359 if (is64bit) {
2360 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2361 } else {
2362 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2363 }
2364 } else {
2365 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2366 }
2367
2368 if (use_imm && rhs_imm == 0) {
2369 switch (cond) {
2370 case kCondEQ:
2371 case kCondBE: // <= 0 if zero
2372 __ Beqzc(lhs, label);
2373 break;
2374 case kCondNE:
2375 case kCondA: // > 0 if non-zero
2376 __ Bnezc(lhs, label);
2377 break;
2378 case kCondLT:
2379 __ Bltzc(lhs, label);
2380 break;
2381 case kCondGE:
2382 __ Bgezc(lhs, label);
2383 break;
2384 case kCondLE:
2385 __ Blezc(lhs, label);
2386 break;
2387 case kCondGT:
2388 __ Bgtzc(lhs, label);
2389 break;
2390 case kCondB: // always false
2391 break;
2392 case kCondAE: // always true
2393 __ Bc(label);
2394 break;
2395 }
2396 } else {
2397 if (use_imm) {
2398 rhs_reg = TMP;
2399 __ LoadConst64(rhs_reg, rhs_imm);
2400 }
2401 switch (cond) {
2402 case kCondEQ:
2403 __ Beqc(lhs, rhs_reg, label);
2404 break;
2405 case kCondNE:
2406 __ Bnec(lhs, rhs_reg, label);
2407 break;
2408 case kCondLT:
2409 __ Bltc(lhs, rhs_reg, label);
2410 break;
2411 case kCondGE:
2412 __ Bgec(lhs, rhs_reg, label);
2413 break;
2414 case kCondLE:
2415 __ Bgec(rhs_reg, lhs, label);
2416 break;
2417 case kCondGT:
2418 __ Bltc(rhs_reg, lhs, label);
2419 break;
2420 case kCondB:
2421 __ Bltuc(lhs, rhs_reg, label);
2422 break;
2423 case kCondAE:
2424 __ Bgeuc(lhs, rhs_reg, label);
2425 break;
2426 case kCondBE:
2427 __ Bgeuc(rhs_reg, lhs, label);
2428 break;
2429 case kCondA:
2430 __ Bltuc(rhs_reg, lhs, label);
2431 break;
2432 }
2433 }
2434}
2435
2436void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2437 bool gt_bias,
2438 Primitive::Type type,
2439 LocationSummary* locations,
2440 Mips64Label* label) {
2441 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2442 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2443 if (type == Primitive::kPrimFloat) {
2444 switch (cond) {
2445 case kCondEQ:
2446 __ CmpEqS(FTMP, lhs, rhs);
2447 __ Bc1nez(FTMP, label);
2448 break;
2449 case kCondNE:
2450 __ CmpEqS(FTMP, lhs, rhs);
2451 __ Bc1eqz(FTMP, label);
2452 break;
2453 case kCondLT:
2454 if (gt_bias) {
2455 __ CmpLtS(FTMP, lhs, rhs);
2456 } else {
2457 __ CmpUltS(FTMP, lhs, rhs);
2458 }
2459 __ Bc1nez(FTMP, label);
2460 break;
2461 case kCondLE:
2462 if (gt_bias) {
2463 __ CmpLeS(FTMP, lhs, rhs);
2464 } else {
2465 __ CmpUleS(FTMP, lhs, rhs);
2466 }
2467 __ Bc1nez(FTMP, label);
2468 break;
2469 case kCondGT:
2470 if (gt_bias) {
2471 __ CmpUltS(FTMP, rhs, lhs);
2472 } else {
2473 __ CmpLtS(FTMP, rhs, lhs);
2474 }
2475 __ Bc1nez(FTMP, label);
2476 break;
2477 case kCondGE:
2478 if (gt_bias) {
2479 __ CmpUleS(FTMP, rhs, lhs);
2480 } else {
2481 __ CmpLeS(FTMP, rhs, lhs);
2482 }
2483 __ Bc1nez(FTMP, label);
2484 break;
2485 default:
2486 LOG(FATAL) << "Unexpected non-floating-point condition";
2487 }
2488 } else {
2489 DCHECK_EQ(type, Primitive::kPrimDouble);
2490 switch (cond) {
2491 case kCondEQ:
2492 __ CmpEqD(FTMP, lhs, rhs);
2493 __ Bc1nez(FTMP, label);
2494 break;
2495 case kCondNE:
2496 __ CmpEqD(FTMP, lhs, rhs);
2497 __ Bc1eqz(FTMP, label);
2498 break;
2499 case kCondLT:
2500 if (gt_bias) {
2501 __ CmpLtD(FTMP, lhs, rhs);
2502 } else {
2503 __ CmpUltD(FTMP, lhs, rhs);
2504 }
2505 __ Bc1nez(FTMP, label);
2506 break;
2507 case kCondLE:
2508 if (gt_bias) {
2509 __ CmpLeD(FTMP, lhs, rhs);
2510 } else {
2511 __ CmpUleD(FTMP, lhs, rhs);
2512 }
2513 __ Bc1nez(FTMP, label);
2514 break;
2515 case kCondGT:
2516 if (gt_bias) {
2517 __ CmpUltD(FTMP, rhs, lhs);
2518 } else {
2519 __ CmpLtD(FTMP, rhs, lhs);
2520 }
2521 __ Bc1nez(FTMP, label);
2522 break;
2523 case kCondGE:
2524 if (gt_bias) {
2525 __ CmpUleD(FTMP, rhs, lhs);
2526 } else {
2527 __ CmpLeD(FTMP, rhs, lhs);
2528 }
2529 __ Bc1nez(FTMP, label);
2530 break;
2531 default:
2532 LOG(FATAL) << "Unexpected non-floating-point condition";
2533 }
2534 }
2535}
2536
Alexey Frunze4dda3372015-06-01 18:31:49 -07002537void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002538 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002539 Mips64Label* true_target,
2540 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002541 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542
David Brazdil0debae72015-11-12 18:37:00 +00002543 if (true_target == nullptr && false_target == nullptr) {
2544 // Nothing to do. The code always falls through.
2545 return;
2546 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002547 // Constant condition, statically compared against "true" (integer value 1).
2548 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002549 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002550 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002552 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002553 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002554 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002555 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002556 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002557 }
David Brazdil0debae72015-11-12 18:37:00 +00002558 return;
2559 }
2560
2561 // The following code generates these patterns:
2562 // (1) true_target == nullptr && false_target != nullptr
2563 // - opposite condition true => branch to false_target
2564 // (2) true_target != nullptr && false_target == nullptr
2565 // - condition true => branch to true_target
2566 // (3) true_target != nullptr && false_target != nullptr
2567 // - condition true => branch to true_target
2568 // - branch to false_target
2569 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002570 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002571 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002573 if (true_target == nullptr) {
2574 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2575 } else {
2576 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2577 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 } else {
2579 // The condition instruction has not been materialized, use its inputs as
2580 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002581 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002582 Primitive::Type type = condition->InputAt(0)->GetType();
2583 LocationSummary* locations = cond->GetLocations();
2584 IfCondition if_cond = condition->GetCondition();
2585 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002586
David Brazdil0debae72015-11-12 18:37:00 +00002587 if (true_target == nullptr) {
2588 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002589 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002590 }
2591
Alexey Frunze299a9392015-12-08 16:08:02 -08002592 switch (type) {
2593 default:
2594 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2595 break;
2596 case Primitive::kPrimLong:
2597 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2598 break;
2599 case Primitive::kPrimFloat:
2600 case Primitive::kPrimDouble:
2601 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2602 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 }
2604 }
David Brazdil0debae72015-11-12 18:37:00 +00002605
2606 // If neither branch falls through (case 3), the conditional branch to `true_target`
2607 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2608 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002609 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002610 }
2611}
2612
2613void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002615 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616 locations->SetInAt(0, Location::RequiresRegister());
2617 }
2618}
2619
2620void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002621 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2622 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002623 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002624 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002625 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002626 nullptr : codegen_->GetLabelOf(false_successor);
2627 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002628}
2629
2630void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2631 LocationSummary* locations = new (GetGraph()->GetArena())
2632 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002633 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002634 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002635 locations->SetInAt(0, Location::RequiresRegister());
2636 }
2637}
2638
2639void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002640 SlowPathCodeMIPS64* slow_path =
2641 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002642 GenerateTestAndBranch(deoptimize,
2643 /* condition_input_index */ 0,
2644 slow_path->GetEntryLabel(),
2645 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646}
2647
David Brazdil74eb1b22015-12-14 11:44:01 +00002648void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2650 if (Primitive::IsFloatingPointType(select->GetType())) {
2651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetInAt(1, Location::RequiresFpuRegister());
2653 } else {
2654 locations->SetInAt(0, Location::RequiresRegister());
2655 locations->SetInAt(1, Location::RequiresRegister());
2656 }
2657 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2658 locations->SetInAt(2, Location::RequiresRegister());
2659 }
2660 locations->SetOut(Location::SameAsFirstInput());
2661}
2662
2663void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2664 LocationSummary* locations = select->GetLocations();
2665 Mips64Label false_target;
2666 GenerateTestAndBranch(select,
2667 /* condition_input_index */ 2,
2668 /* true_target */ nullptr,
2669 &false_target);
2670 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2671 __ Bind(&false_target);
2672}
2673
David Srbecky0cf44932015-12-09 14:09:59 +00002674void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2675 new (GetGraph()->GetArena()) LocationSummary(info);
2676}
2677
David Srbeckyd28f4a02016-03-14 17:14:24 +00002678void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2679 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002680}
2681
2682void CodeGeneratorMIPS64::GenerateNop() {
2683 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002684}
2685
Alexey Frunze4dda3372015-06-01 18:31:49 -07002686void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2687 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2688 LocationSummary* locations =
2689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2692 locations->SetOut(Location::RequiresFpuRegister());
2693 } else {
2694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2695 }
2696}
2697
2698void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2699 const FieldInfo& field_info) {
2700 Primitive::Type type = field_info.GetFieldType();
2701 LocationSummary* locations = instruction->GetLocations();
2702 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2703 LoadOperandType load_type = kLoadUnsignedByte;
2704 switch (type) {
2705 case Primitive::kPrimBoolean:
2706 load_type = kLoadUnsignedByte;
2707 break;
2708 case Primitive::kPrimByte:
2709 load_type = kLoadSignedByte;
2710 break;
2711 case Primitive::kPrimShort:
2712 load_type = kLoadSignedHalfword;
2713 break;
2714 case Primitive::kPrimChar:
2715 load_type = kLoadUnsignedHalfword;
2716 break;
2717 case Primitive::kPrimInt:
2718 case Primitive::kPrimFloat:
2719 load_type = kLoadWord;
2720 break;
2721 case Primitive::kPrimLong:
2722 case Primitive::kPrimDouble:
2723 load_type = kLoadDoubleword;
2724 break;
2725 case Primitive::kPrimNot:
2726 load_type = kLoadUnsignedWord;
2727 break;
2728 case Primitive::kPrimVoid:
2729 LOG(FATAL) << "Unreachable type " << type;
2730 UNREACHABLE();
2731 }
2732 if (!Primitive::IsFloatingPointType(type)) {
2733 DCHECK(locations->Out().IsRegister());
2734 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2735 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2736 } else {
2737 DCHECK(locations->Out().IsFpuRegister());
2738 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2739 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2740 }
2741
2742 codegen_->MaybeRecordImplicitNullCheck(instruction);
2743 // TODO: memory barrier?
2744}
2745
2746void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2747 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2748 LocationSummary* locations =
2749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2750 locations->SetInAt(0, Location::RequiresRegister());
2751 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2752 locations->SetInAt(1, Location::RequiresFpuRegister());
2753 } else {
2754 locations->SetInAt(1, Location::RequiresRegister());
2755 }
2756}
2757
2758void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002759 const FieldInfo& field_info,
2760 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761 Primitive::Type type = field_info.GetFieldType();
2762 LocationSummary* locations = instruction->GetLocations();
2763 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2764 StoreOperandType store_type = kStoreByte;
2765 switch (type) {
2766 case Primitive::kPrimBoolean:
2767 case Primitive::kPrimByte:
2768 store_type = kStoreByte;
2769 break;
2770 case Primitive::kPrimShort:
2771 case Primitive::kPrimChar:
2772 store_type = kStoreHalfword;
2773 break;
2774 case Primitive::kPrimInt:
2775 case Primitive::kPrimFloat:
2776 case Primitive::kPrimNot:
2777 store_type = kStoreWord;
2778 break;
2779 case Primitive::kPrimLong:
2780 case Primitive::kPrimDouble:
2781 store_type = kStoreDoubleword;
2782 break;
2783 case Primitive::kPrimVoid:
2784 LOG(FATAL) << "Unreachable type " << type;
2785 UNREACHABLE();
2786 }
2787 if (!Primitive::IsFloatingPointType(type)) {
2788 DCHECK(locations->InAt(1).IsRegister());
2789 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2790 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2791 } else {
2792 DCHECK(locations->InAt(1).IsFpuRegister());
2793 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2794 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2795 }
2796
2797 codegen_->MaybeRecordImplicitNullCheck(instruction);
2798 // TODO: memory barriers?
2799 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2800 DCHECK(locations->InAt(1).IsRegister());
2801 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002802 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002803 }
2804}
2805
2806void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2807 HandleFieldGet(instruction, instruction->GetFieldInfo());
2808}
2809
2810void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2811 HandleFieldGet(instruction, instruction->GetFieldInfo());
2812}
2813
2814void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2815 HandleFieldSet(instruction, instruction->GetFieldInfo());
2816}
2817
2818void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002819 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002820}
2821
2822void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2823 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002824 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2826 locations->SetInAt(0, Location::RequiresRegister());
2827 locations->SetInAt(1, Location::RequiresRegister());
2828 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002829 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002830 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2831}
2832
2833void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2834 LocationSummary* locations = instruction->GetLocations();
2835 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2836 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2837 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2838
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002839 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840
2841 // Return 0 if `obj` is null.
2842 // TODO: Avoid this check if we know `obj` is not null.
2843 __ Move(out, ZERO);
2844 __ Beqzc(obj, &done);
2845
2846 // Compare the class of `obj` with `cls`.
2847 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002848 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002849 // Classes must be equal for the instanceof to succeed.
2850 __ Xor(out, out, cls);
2851 __ Sltiu(out, out, 1);
2852 } else {
2853 // If the classes are not equal, we go into a slow path.
2854 DCHECK(locations->OnlyCallsOnSlowPath());
2855 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002856 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002857 codegen_->AddSlowPath(slow_path);
2858 __ Bnec(out, cls, slow_path->GetEntryLabel());
2859 __ LoadConst32(out, 1);
2860 __ Bind(slow_path->GetExitLabel());
2861 }
2862
2863 __ Bind(&done);
2864}
2865
2866void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2868 locations->SetOut(Location::ConstantLocation(constant));
2869}
2870
2871void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2872 // Will be generated at use site.
2873}
2874
2875void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2877 locations->SetOut(Location::ConstantLocation(constant));
2878}
2879
2880void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2881 // Will be generated at use site.
2882}
2883
Calin Juravle175dc732015-08-25 15:42:32 +01002884void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2885 // The trampoline uses the same calling convention as dex calling conventions,
2886 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2887 // the method_idx.
2888 HandleInvoke(invoke);
2889}
2890
2891void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2892 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2893}
2894
Alexey Frunze4dda3372015-06-01 18:31:49 -07002895void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2896 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2897 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2898}
2899
2900void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2901 HandleInvoke(invoke);
2902 // The register T0 is required to be used for the hidden argument in
2903 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2904 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2905}
2906
2907void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2908 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2909 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002910 Location receiver = invoke->GetLocations()->InAt(0);
2911 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002912 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913
2914 // Set the hidden argument.
2915 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2916 invoke->GetDexMethodIndex());
2917
2918 // temp = object->GetClass();
2919 if (receiver.IsStackSlot()) {
2920 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2921 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2922 } else {
2923 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2924 }
2925 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002926 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2927 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2928 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002929 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002930 // temp = temp->GetImtEntryAt(method_offset);
2931 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2932 // T9 = temp->GetEntryPoint();
2933 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2934 // T9();
2935 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002936 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002937 DCHECK(!codegen_->IsLeafMethod());
2938 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2939}
2940
2941void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002942 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2943 if (intrinsic.TryDispatch(invoke)) {
2944 return;
2945 }
2946
Alexey Frunze4dda3372015-06-01 18:31:49 -07002947 HandleInvoke(invoke);
2948}
2949
2950void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002951 // Explicit clinit checks triggered by static invokes must have been pruned by
2952 // art::PrepareForRegisterAllocation.
2953 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002954
Chris Larsen3039e382015-08-26 07:54:08 -07002955 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2956 if (intrinsic.TryDispatch(invoke)) {
2957 return;
2958 }
2959
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961}
2962
Chris Larsen3039e382015-08-26 07:54:08 -07002963static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002964 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002965 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2966 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002967 return true;
2968 }
2969 return false;
2970}
2971
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002972HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2973 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2974 // TODO: Implement other kinds.
2975 return HLoadString::LoadKind::kDexCacheViaMethod;
2976}
2977
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002978HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
2979 HLoadClass::LoadKind desired_class_load_kind) {
2980 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
2981 // TODO: Implement other kinds.
2982 return HLoadClass::LoadKind::kDexCacheViaMethod;
2983}
2984
Vladimir Markodc151b22015-10-15 18:02:30 +01002985HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2986 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01002987 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01002988 switch (desired_dispatch_info.method_load_kind) {
2989 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2990 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2991 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2992 return HInvokeStaticOrDirect::DispatchInfo {
2993 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2994 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2995 0u,
2996 0u
2997 };
2998 default:
2999 break;
3000 }
3001 switch (desired_dispatch_info.code_ptr_location) {
3002 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3003 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3004 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3005 return HInvokeStaticOrDirect::DispatchInfo {
3006 desired_dispatch_info.method_load_kind,
3007 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3008 desired_dispatch_info.method_load_data,
3009 0u
3010 };
3011 default:
3012 return desired_dispatch_info;
3013 }
3014}
3015
Alexey Frunze4dda3372015-06-01 18:31:49 -07003016void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3017 // All registers are assumed to be correctly set up per the calling convention.
3018
Vladimir Marko58155012015-08-19 12:49:41 +00003019 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3020 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003021 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003022 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003023 uint32_t offset =
3024 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003025 __ LoadFromOffset(kLoadDoubleword,
3026 temp.AsRegister<GpuRegister>(),
3027 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003028 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003029 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003030 }
Vladimir Marko58155012015-08-19 12:49:41 +00003031 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003032 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003033 break;
3034 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3035 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3036 break;
3037 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003038 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003039 // TODO: Implement these types.
3040 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3041 LOG(FATAL) << "Unsupported";
3042 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003043 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003044 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003045 GpuRegister reg = temp.AsRegister<GpuRegister>();
3046 GpuRegister method_reg;
3047 if (current_method.IsRegister()) {
3048 method_reg = current_method.AsRegister<GpuRegister>();
3049 } else {
3050 // TODO: use the appropriate DCHECK() here if possible.
3051 // DCHECK(invoke->GetLocations()->Intrinsified());
3052 DCHECK(!current_method.IsValid());
3053 method_reg = reg;
3054 __ Ld(reg, SP, kCurrentMethodStackOffset);
3055 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003056
Vladimir Marko58155012015-08-19 12:49:41 +00003057 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003058 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003059 reg,
3060 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003061 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003062 // temp = temp[index_in_cache];
3063 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3064 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003065 __ LoadFromOffset(kLoadDoubleword,
3066 reg,
3067 reg,
3068 CodeGenerator::GetCachePointerOffset(index_in_cache));
3069 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003070 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003071 }
3072
Vladimir Marko58155012015-08-19 12:49:41 +00003073 switch (invoke->GetCodePtrLocation()) {
3074 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003075 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003076 break;
3077 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3078 // LR = invoke->GetDirectCodePtr();
3079 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3080 // LR()
3081 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003082 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003083 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003084 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003085 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3086 // TODO: Implement these types.
3087 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3088 LOG(FATAL) << "Unsupported";
3089 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003090 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3091 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3092 __ LoadFromOffset(kLoadDoubleword,
3093 T9,
3094 callee_method.AsRegister<GpuRegister>(),
3095 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003096 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003097 // T9()
3098 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003099 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003100 break;
3101 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003102 DCHECK(!IsLeafMethod());
3103}
3104
3105void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003106 // Explicit clinit checks triggered by static invokes must have been pruned by
3107 // art::PrepareForRegisterAllocation.
3108 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003109
3110 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3111 return;
3112 }
3113
3114 LocationSummary* locations = invoke->GetLocations();
3115 codegen_->GenerateStaticOrDirectCall(invoke,
3116 locations->HasTemps()
3117 ? locations->GetTemp(0)
3118 : Location::NoLocation());
3119 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3120}
3121
Alexey Frunze53afca12015-11-05 16:34:23 -08003122void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003123 // Use the calling convention instead of the location of the receiver, as
3124 // intrinsics may have put the receiver in a different register. In the intrinsics
3125 // slow path, the arguments have been moved to the right place, so here we are
3126 // guaranteed that the receiver is the first register of the calling convention.
3127 InvokeDexCallingConvention calling_convention;
3128 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3129
Alexey Frunze53afca12015-11-05 16:34:23 -08003130 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003131 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3132 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3133 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003134 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003135
3136 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003137 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003138 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003139 // temp = temp->GetMethodAt(method_offset);
3140 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3141 // T9 = temp->GetEntryPoint();
3142 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3143 // T9();
3144 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003145 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003146}
3147
3148void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3149 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3150 return;
3151 }
3152
3153 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003154 DCHECK(!codegen_->IsLeafMethod());
3155 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3156}
3157
3158void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003159 InvokeRuntimeCallingConvention calling_convention;
3160 CodeGenerator::CreateLoadClassLocationSummary(
3161 cls,
3162 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003163 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003164}
3165
3166void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3167 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003168 if (cls->NeedsAccessCheck()) {
3169 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003170 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003171 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003172 return;
3173 }
3174
3175 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3176 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3177 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003178 DCHECK(!cls->CanCallRuntime());
3179 DCHECK(!cls->MustGenerateClinitCheck());
3180 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3181 ArtMethod::DeclaringClassOffset().Int32Value());
3182 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003183 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3184 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003185 __ LoadFromOffset(
3186 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003187 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003188 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3189 DCHECK(cls->CanCallRuntime());
3190 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3191 cls,
3192 cls,
3193 cls->GetDexPc(),
3194 cls->MustGenerateClinitCheck());
3195 codegen_->AddSlowPath(slow_path);
3196 if (!cls->IsInDexCache()) {
3197 __ Beqzc(out, slow_path->GetEntryLabel());
3198 }
3199 if (cls->MustGenerateClinitCheck()) {
3200 GenerateClassInitializationCheck(slow_path, out);
3201 } else {
3202 __ Bind(slow_path->GetExitLabel());
3203 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003204 }
3205 }
3206}
3207
David Brazdilcb1c0552015-08-04 16:22:25 +01003208static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003209 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003210}
3211
Alexey Frunze4dda3372015-06-01 18:31:49 -07003212void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3213 LocationSummary* locations =
3214 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3215 locations->SetOut(Location::RequiresRegister());
3216}
3217
3218void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3219 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003220 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3221}
3222
3223void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3224 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3225}
3226
3227void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3228 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003229}
3230
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003232 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3233 ? LocationSummary::kCallOnSlowPath
3234 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003235 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003236 locations->SetInAt(0, Location::RequiresRegister());
3237 locations->SetOut(Location::RequiresRegister());
3238}
3239
3240void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003241 // TODO: Re-add the compiler code to do string dex cache lookup again.
3242 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3243 codegen_->AddSlowPath(slow_path);
3244 __ Bc(slow_path->GetEntryLabel());
3245 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003246}
3247
Alexey Frunze4dda3372015-06-01 18:31:49 -07003248void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3250 locations->SetOut(Location::ConstantLocation(constant));
3251}
3252
3253void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3254 // Will be generated at use site.
3255}
3256
3257void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3258 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003260 InvokeRuntimeCallingConvention calling_convention;
3261 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3262}
3263
3264void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003265 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003267 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003268 if (instruction->IsEnter()) {
3269 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3270 } else {
3271 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3272 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003273}
3274
3275void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3276 LocationSummary* locations =
3277 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3278 switch (mul->GetResultType()) {
3279 case Primitive::kPrimInt:
3280 case Primitive::kPrimLong:
3281 locations->SetInAt(0, Location::RequiresRegister());
3282 locations->SetInAt(1, Location::RequiresRegister());
3283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3284 break;
3285
3286 case Primitive::kPrimFloat:
3287 case Primitive::kPrimDouble:
3288 locations->SetInAt(0, Location::RequiresFpuRegister());
3289 locations->SetInAt(1, Location::RequiresFpuRegister());
3290 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3291 break;
3292
3293 default:
3294 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3295 }
3296}
3297
3298void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3299 Primitive::Type type = instruction->GetType();
3300 LocationSummary* locations = instruction->GetLocations();
3301
3302 switch (type) {
3303 case Primitive::kPrimInt:
3304 case Primitive::kPrimLong: {
3305 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3306 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3307 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3308 if (type == Primitive::kPrimInt)
3309 __ MulR6(dst, lhs, rhs);
3310 else
3311 __ Dmul(dst, lhs, rhs);
3312 break;
3313 }
3314 case Primitive::kPrimFloat:
3315 case Primitive::kPrimDouble: {
3316 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3317 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3318 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3319 if (type == Primitive::kPrimFloat)
3320 __ MulS(dst, lhs, rhs);
3321 else
3322 __ MulD(dst, lhs, rhs);
3323 break;
3324 }
3325 default:
3326 LOG(FATAL) << "Unexpected mul type " << type;
3327 }
3328}
3329
3330void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3331 LocationSummary* locations =
3332 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3333 switch (neg->GetResultType()) {
3334 case Primitive::kPrimInt:
3335 case Primitive::kPrimLong:
3336 locations->SetInAt(0, Location::RequiresRegister());
3337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3338 break;
3339
3340 case Primitive::kPrimFloat:
3341 case Primitive::kPrimDouble:
3342 locations->SetInAt(0, Location::RequiresFpuRegister());
3343 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3344 break;
3345
3346 default:
3347 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3348 }
3349}
3350
3351void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3352 Primitive::Type type = instruction->GetType();
3353 LocationSummary* locations = instruction->GetLocations();
3354
3355 switch (type) {
3356 case Primitive::kPrimInt:
3357 case Primitive::kPrimLong: {
3358 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3359 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3360 if (type == Primitive::kPrimInt)
3361 __ Subu(dst, ZERO, src);
3362 else
3363 __ Dsubu(dst, ZERO, src);
3364 break;
3365 }
3366 case Primitive::kPrimFloat:
3367 case Primitive::kPrimDouble: {
3368 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3369 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3370 if (type == Primitive::kPrimFloat)
3371 __ NegS(dst, src);
3372 else
3373 __ NegD(dst, src);
3374 break;
3375 }
3376 default:
3377 LOG(FATAL) << "Unexpected neg type " << type;
3378 }
3379}
3380
3381void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3382 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003384 InvokeRuntimeCallingConvention calling_convention;
3385 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3386 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3387 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3388 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3389}
3390
3391void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3392 LocationSummary* locations = instruction->GetLocations();
3393 // Move an uint16_t value to a register.
3394 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003395 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003396 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3397}
3398
3399void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3400 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003402 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003403 if (instruction->IsStringAlloc()) {
3404 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3405 } else {
3406 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3407 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3408 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003409 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3410}
3411
3412void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003413 if (instruction->IsStringAlloc()) {
3414 // String is allocated through StringFactory. Call NewEmptyString entry point.
3415 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003416 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003417 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003418 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3419 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3420 __ Jalr(T9);
3421 __ Nop();
3422 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3423 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003424 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003425 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3426 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003427}
3428
3429void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3431 locations->SetInAt(0, Location::RequiresRegister());
3432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3433}
3434
3435void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3436 Primitive::Type type = instruction->GetType();
3437 LocationSummary* locations = instruction->GetLocations();
3438
3439 switch (type) {
3440 case Primitive::kPrimInt:
3441 case Primitive::kPrimLong: {
3442 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3443 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3444 __ Nor(dst, src, ZERO);
3445 break;
3446 }
3447
3448 default:
3449 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3450 }
3451}
3452
3453void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3455 locations->SetInAt(0, Location::RequiresRegister());
3456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3457}
3458
3459void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3460 LocationSummary* locations = instruction->GetLocations();
3461 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3462 locations->InAt(0).AsRegister<GpuRegister>(),
3463 1);
3464}
3465
3466void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003467 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3468 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003469}
3470
Calin Juravle2ae48182016-03-16 14:05:09 +00003471void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3472 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003473 return;
3474 }
3475 Location obj = instruction->GetLocations()->InAt(0);
3476
3477 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003478 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003479}
3480
Calin Juravle2ae48182016-03-16 14:05:09 +00003481void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003482 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003483 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003484
3485 Location obj = instruction->GetLocations()->InAt(0);
3486
3487 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3488}
3489
3490void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003491 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492}
3493
3494void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3495 HandleBinaryOp(instruction);
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3499 HandleBinaryOp(instruction);
3500}
3501
3502void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3503 LOG(FATAL) << "Unreachable";
3504}
3505
3506void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3507 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3508}
3509
3510void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3512 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3513 if (location.IsStackSlot()) {
3514 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3515 } else if (location.IsDoubleStackSlot()) {
3516 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3517 }
3518 locations->SetOut(location);
3519}
3520
3521void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3522 ATTRIBUTE_UNUSED) {
3523 // Nothing to do, the parameter is already at its location.
3524}
3525
3526void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3527 LocationSummary* locations =
3528 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3529 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3530}
3531
3532void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3533 ATTRIBUTE_UNUSED) {
3534 // Nothing to do, the method is already at its location.
3535}
3536
3537void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003539 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003540 locations->SetInAt(i, Location::Any());
3541 }
3542 locations->SetOut(Location::Any());
3543}
3544
3545void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3546 LOG(FATAL) << "Unreachable";
3547}
3548
3549void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3550 Primitive::Type type = rem->GetResultType();
3551 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003552 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3553 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003554 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3555
3556 switch (type) {
3557 case Primitive::kPrimInt:
3558 case Primitive::kPrimLong:
3559 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003560 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3562 break;
3563
3564 case Primitive::kPrimFloat:
3565 case Primitive::kPrimDouble: {
3566 InvokeRuntimeCallingConvention calling_convention;
3567 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3568 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3569 locations->SetOut(calling_convention.GetReturnLocation(type));
3570 break;
3571 }
3572
3573 default:
3574 LOG(FATAL) << "Unexpected rem type " << type;
3575 }
3576}
3577
3578void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3579 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003580
3581 switch (type) {
3582 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003583 case Primitive::kPrimLong:
3584 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003585 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003586
3587 case Primitive::kPrimFloat:
3588 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003589 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3590 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003591 if (type == Primitive::kPrimFloat) {
3592 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3593 } else {
3594 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3595 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003596 break;
3597 }
3598 default:
3599 LOG(FATAL) << "Unexpected rem type " << type;
3600 }
3601}
3602
3603void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3604 memory_barrier->SetLocations(nullptr);
3605}
3606
3607void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3608 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3609}
3610
3611void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3612 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3613 Primitive::Type return_type = ret->InputAt(0)->GetType();
3614 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3615}
3616
3617void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3618 codegen_->GenerateFrameExit();
3619}
3620
3621void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3622 ret->SetLocations(nullptr);
3623}
3624
3625void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3626 codegen_->GenerateFrameExit();
3627}
3628
Alexey Frunze92d90602015-12-18 18:16:36 -08003629void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3630 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003631}
3632
Alexey Frunze92d90602015-12-18 18:16:36 -08003633void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3634 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003635}
3636
Alexey Frunze4dda3372015-06-01 18:31:49 -07003637void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3638 HandleShift(shl);
3639}
3640
3641void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3642 HandleShift(shl);
3643}
3644
3645void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3646 HandleShift(shr);
3647}
3648
3649void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3650 HandleShift(shr);
3651}
3652
Alexey Frunze4dda3372015-06-01 18:31:49 -07003653void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3654 HandleBinaryOp(instruction);
3655}
3656
3657void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3658 HandleBinaryOp(instruction);
3659}
3660
3661void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3662 HandleFieldGet(instruction, instruction->GetFieldInfo());
3663}
3664
3665void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3666 HandleFieldGet(instruction, instruction->GetFieldInfo());
3667}
3668
3669void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3670 HandleFieldSet(instruction, instruction->GetFieldInfo());
3671}
3672
3673void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003674 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003675}
3676
Calin Juravlee460d1d2015-09-29 04:52:17 +01003677void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3678 HUnresolvedInstanceFieldGet* instruction) {
3679 FieldAccessCallingConventionMIPS64 calling_convention;
3680 codegen_->CreateUnresolvedFieldLocationSummary(
3681 instruction, instruction->GetFieldType(), calling_convention);
3682}
3683
3684void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3685 HUnresolvedInstanceFieldGet* instruction) {
3686 FieldAccessCallingConventionMIPS64 calling_convention;
3687 codegen_->GenerateUnresolvedFieldAccess(instruction,
3688 instruction->GetFieldType(),
3689 instruction->GetFieldIndex(),
3690 instruction->GetDexPc(),
3691 calling_convention);
3692}
3693
3694void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3695 HUnresolvedInstanceFieldSet* instruction) {
3696 FieldAccessCallingConventionMIPS64 calling_convention;
3697 codegen_->CreateUnresolvedFieldLocationSummary(
3698 instruction, instruction->GetFieldType(), calling_convention);
3699}
3700
3701void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3702 HUnresolvedInstanceFieldSet* instruction) {
3703 FieldAccessCallingConventionMIPS64 calling_convention;
3704 codegen_->GenerateUnresolvedFieldAccess(instruction,
3705 instruction->GetFieldType(),
3706 instruction->GetFieldIndex(),
3707 instruction->GetDexPc(),
3708 calling_convention);
3709}
3710
3711void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3712 HUnresolvedStaticFieldGet* instruction) {
3713 FieldAccessCallingConventionMIPS64 calling_convention;
3714 codegen_->CreateUnresolvedFieldLocationSummary(
3715 instruction, instruction->GetFieldType(), calling_convention);
3716}
3717
3718void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3719 HUnresolvedStaticFieldGet* instruction) {
3720 FieldAccessCallingConventionMIPS64 calling_convention;
3721 codegen_->GenerateUnresolvedFieldAccess(instruction,
3722 instruction->GetFieldType(),
3723 instruction->GetFieldIndex(),
3724 instruction->GetDexPc(),
3725 calling_convention);
3726}
3727
3728void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3729 HUnresolvedStaticFieldSet* instruction) {
3730 FieldAccessCallingConventionMIPS64 calling_convention;
3731 codegen_->CreateUnresolvedFieldLocationSummary(
3732 instruction, instruction->GetFieldType(), calling_convention);
3733}
3734
3735void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3736 HUnresolvedStaticFieldSet* instruction) {
3737 FieldAccessCallingConventionMIPS64 calling_convention;
3738 codegen_->GenerateUnresolvedFieldAccess(instruction,
3739 instruction->GetFieldType(),
3740 instruction->GetFieldIndex(),
3741 instruction->GetDexPc(),
3742 calling_convention);
3743}
3744
Alexey Frunze4dda3372015-06-01 18:31:49 -07003745void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003746 LocationSummary* locations =
3747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003748 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003749}
3750
3751void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3752 HBasicBlock* block = instruction->GetBlock();
3753 if (block->GetLoopInformation() != nullptr) {
3754 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3755 // The back edge will generate the suspend check.
3756 return;
3757 }
3758 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3759 // The goto will generate the suspend check.
3760 return;
3761 }
3762 GenerateSuspendCheck(instruction, nullptr);
3763}
3764
Alexey Frunze4dda3372015-06-01 18:31:49 -07003765void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3766 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003767 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003768 InvokeRuntimeCallingConvention calling_convention;
3769 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3770}
3771
3772void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003773 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003774 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3775}
3776
3777void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3778 Primitive::Type input_type = conversion->GetInputType();
3779 Primitive::Type result_type = conversion->GetResultType();
3780 DCHECK_NE(input_type, result_type);
3781
3782 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3783 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3784 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3785 }
3786
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003787 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3788
3789 if (Primitive::IsFloatingPointType(input_type)) {
3790 locations->SetInAt(0, Location::RequiresFpuRegister());
3791 } else {
3792 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003793 }
3794
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003795 if (Primitive::IsFloatingPointType(result_type)) {
3796 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003797 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003799 }
3800}
3801
3802void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3803 LocationSummary* locations = conversion->GetLocations();
3804 Primitive::Type result_type = conversion->GetResultType();
3805 Primitive::Type input_type = conversion->GetInputType();
3806
3807 DCHECK_NE(input_type, result_type);
3808
3809 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3810 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3811 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3812
3813 switch (result_type) {
3814 case Primitive::kPrimChar:
3815 __ Andi(dst, src, 0xFFFF);
3816 break;
3817 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003818 if (input_type == Primitive::kPrimLong) {
3819 // Type conversion from long to types narrower than int is a result of code
3820 // transformations. To avoid unpredictable results for SEB and SEH, we first
3821 // need to sign-extend the low 32-bit value into bits 32 through 63.
3822 __ Sll(dst, src, 0);
3823 __ Seb(dst, dst);
3824 } else {
3825 __ Seb(dst, src);
3826 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003827 break;
3828 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003829 if (input_type == Primitive::kPrimLong) {
3830 // Type conversion from long to types narrower than int is a result of code
3831 // transformations. To avoid unpredictable results for SEB and SEH, we first
3832 // need to sign-extend the low 32-bit value into bits 32 through 63.
3833 __ Sll(dst, src, 0);
3834 __ Seh(dst, dst);
3835 } else {
3836 __ Seh(dst, src);
3837 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003838 break;
3839 case Primitive::kPrimInt:
3840 case Primitive::kPrimLong:
3841 // Sign-extend 32-bit int into bits 32 through 63 for
3842 // int-to-long and long-to-int conversions
3843 __ Sll(dst, src, 0);
3844 break;
3845
3846 default:
3847 LOG(FATAL) << "Unexpected type conversion from " << input_type
3848 << " to " << result_type;
3849 }
3850 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003851 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3852 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3853 if (input_type == Primitive::kPrimLong) {
3854 __ Dmtc1(src, FTMP);
3855 if (result_type == Primitive::kPrimFloat) {
3856 __ Cvtsl(dst, FTMP);
3857 } else {
3858 __ Cvtdl(dst, FTMP);
3859 }
3860 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003861 __ Mtc1(src, FTMP);
3862 if (result_type == Primitive::kPrimFloat) {
3863 __ Cvtsw(dst, FTMP);
3864 } else {
3865 __ Cvtdw(dst, FTMP);
3866 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867 }
3868 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3869 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003870 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3871 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3872 Mips64Label truncate;
3873 Mips64Label done;
3874
3875 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3876 // value when the input is either a NaN or is outside of the range of the output type
3877 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3878 // the same result.
3879 //
3880 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3881 // value of the output type if the input is outside of the range after the truncation or
3882 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3883 // results. This matches the desired float/double-to-int/long conversion exactly.
3884 //
3885 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3886 //
3887 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3888 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3889 // even though it must be NAN2008=1 on R6.
3890 //
3891 // The code takes care of the different behaviors by first comparing the input to the
3892 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3893 // If the input is greater than or equal to the minimum, it procedes to the truncate
3894 // instruction, which will handle such an input the same way irrespective of NAN2008.
3895 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3896 // in order to return either zero or the minimum value.
3897 //
3898 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3899 // truncate instruction for MIPS64R6.
3900 if (input_type == Primitive::kPrimFloat) {
3901 uint32_t min_val = (result_type == Primitive::kPrimLong)
3902 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3903 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3904 __ LoadConst32(TMP, min_val);
3905 __ Mtc1(TMP, FTMP);
3906 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003907 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003908 uint64_t min_val = (result_type == Primitive::kPrimLong)
3909 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3910 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3911 __ LoadConst64(TMP, min_val);
3912 __ Dmtc1(TMP, FTMP);
3913 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003914 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003915
3916 __ Bc1nez(FTMP, &truncate);
3917
3918 if (input_type == Primitive::kPrimFloat) {
3919 __ CmpEqS(FTMP, src, src);
3920 } else {
3921 __ CmpEqD(FTMP, src, src);
3922 }
3923 if (result_type == Primitive::kPrimLong) {
3924 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3925 } else {
3926 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3927 }
3928 __ Mfc1(TMP, FTMP);
3929 __ And(dst, dst, TMP);
3930
3931 __ Bc(&done);
3932
3933 __ Bind(&truncate);
3934
3935 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003936 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003937 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003938 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003939 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003940 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003941 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003942 } else {
3943 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003944 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003945 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003946 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003947 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003948 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003949 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003950
3951 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003952 } else if (Primitive::IsFloatingPointType(result_type) &&
3953 Primitive::IsFloatingPointType(input_type)) {
3954 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3955 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3956 if (result_type == Primitive::kPrimFloat) {
3957 __ Cvtsd(dst, src);
3958 } else {
3959 __ Cvtds(dst, src);
3960 }
3961 } else {
3962 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3963 << " to " << result_type;
3964 }
3965}
3966
3967void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3968 HandleShift(ushr);
3969}
3970
3971void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3972 HandleShift(ushr);
3973}
3974
3975void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3976 HandleBinaryOp(instruction);
3977}
3978
3979void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3980 HandleBinaryOp(instruction);
3981}
3982
3983void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3984 // Nothing to do, this should be removed during prepare for register allocator.
3985 LOG(FATAL) << "Unreachable";
3986}
3987
3988void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3989 // Nothing to do, this should be removed during prepare for register allocator.
3990 LOG(FATAL) << "Unreachable";
3991}
3992
3993void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003994 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995}
3996
3997void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003998 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003999}
4000
4001void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004002 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003}
4004
4005void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004006 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007}
4008
4009void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004010 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004011}
4012
4013void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004014 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015}
4016
4017void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004018 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004022 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004023}
4024
4025void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004026 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004027}
4028
4029void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004030 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031}
4032
4033void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004034 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004035}
4036
4037void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004038 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004039}
4040
Aart Bike9f37602015-10-09 11:15:55 -07004041void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004042 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004043}
4044
4045void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004046 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004047}
4048
4049void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004050 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004051}
4052
4053void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004054 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004055}
4056
4057void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004058 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004059}
4060
4061void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004062 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004063}
4064
4065void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004066 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004067}
4068
4069void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004070 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004071}
4072
Mark Mendellfe57faa2015-09-18 09:26:15 -04004073// Simple implementation of packed switch - generate cascaded compare/jumps.
4074void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4075 LocationSummary* locations =
4076 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4077 locations->SetInAt(0, Location::RequiresRegister());
4078}
4079
4080void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4081 int32_t lower_bound = switch_instr->GetStartValue();
4082 int32_t num_entries = switch_instr->GetNumEntries();
4083 LocationSummary* locations = switch_instr->GetLocations();
4084 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4085 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4086
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004087 // Create a set of compare/jumps.
4088 GpuRegister temp_reg = TMP;
4089 if (IsInt<16>(-lower_bound)) {
4090 __ Addiu(temp_reg, value_reg, -lower_bound);
4091 } else {
4092 __ LoadConst32(AT, -lower_bound);
4093 __ Addu(temp_reg, value_reg, AT);
4094 }
4095 // Jump to default if index is negative
4096 // Note: We don't check the case that index is positive while value < lower_bound, because in
4097 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4098 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4099
Mark Mendellfe57faa2015-09-18 09:26:15 -04004100 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004101 // Jump to successors[0] if value == lower_bound.
4102 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4103 int32_t last_index = 0;
4104 for (; num_entries - last_index > 2; last_index += 2) {
4105 __ Addiu(temp_reg, temp_reg, -2);
4106 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4107 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4108 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4109 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4110 }
4111 if (num_entries - last_index == 2) {
4112 // The last missing case_value.
4113 __ Addiu(temp_reg, temp_reg, -1);
4114 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004115 }
4116
4117 // And the default for any other value.
4118 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004119 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004120 }
4121}
4122
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004123void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4124 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4125}
4126
4127void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4128 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4129}
4130
Alexey Frunze4dda3372015-06-01 18:31:49 -07004131} // namespace mips64
4132} // namespace art