blob: 02b01c85e5846b0b6ee0ead15c57192ec4252493 [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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800183 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
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
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700327 DCHECK(instruction_->IsCheckCast()
328 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
329 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
330
331 __ Bind(GetEntryLabel());
332 SaveLiveRegisters(codegen, locations);
333
334 // We're moving two locations to locations that could overlap, so we need a parallel
335 // move resolver.
336 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800337 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700338 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
339 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800340 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700341 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
342 Primitive::kPrimNot);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700343 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100344 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800345 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700346 Primitive::Type ret_type = instruction_->GetType();
347 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
348 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700349 } else {
350 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 mips64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
352 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700353 }
354
355 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700356 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700357 }
358
Roland Levillain46648892015-06-19 16:07:18 +0100359 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
360
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
363};
364
365class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
366 public:
Aart Bik42249c32016-01-07 15:33:50 -0800367 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000368 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369
370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800371 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100373 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000374 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375 }
376
Roland Levillain46648892015-06-19 16:07:18 +0100377 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
378
Alexey Frunze4dda3372015-06-01 18:31:49 -0700379 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
381};
382
383CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
384 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100385 const CompilerOptions& compiler_options,
386 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 : CodeGenerator(graph,
388 kNumberOfGpuRegisters,
389 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000390 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
392 arraysize(kCoreCalleeSaves)),
393 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
394 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100395 compiler_options,
396 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100397 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 location_builder_(graph, this),
399 instruction_visitor_(graph, this),
400 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100401 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700402 isa_features_(isa_features) {
403 // Save RA (containing the return address) to mimic Quick.
404 AddAllocatedRegister(Location::RegisterLocation(RA));
405}
406
407#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100408// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
409#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700410#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700411
412void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700413 // Ensure that we fix up branches.
414 __ FinalizeCode();
415
416 // Adjust native pc offsets in stack maps.
417 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
418 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
419 uint32_t new_position = __ GetAdjustedPosition(old_position);
420 DCHECK_GE(new_position, old_position);
421 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
422 }
423
424 // Adjust pc offsets for the disassembly information.
425 if (disasm_info_ != nullptr) {
426 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
427 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
428 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
429 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
430 it.second.start = __ GetAdjustedPosition(it.second.start);
431 it.second.end = __ GetAdjustedPosition(it.second.end);
432 }
433 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
434 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
435 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
436 }
437 }
438
Alexey Frunze4dda3372015-06-01 18:31:49 -0700439 CodeGenerator::Finalize(allocator);
440}
441
442Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
443 return codegen_->GetAssembler();
444}
445
446void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100447 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700448 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
449}
450
451void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100452 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700453 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
454}
455
456void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
457 // Pop reg
458 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200459 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460}
461
462void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
463 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200464 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700465 __ Sd(GpuRegister(reg), SP, 0);
466}
467
468void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
469 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
470 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
471 // Allocate a scratch register other than TMP, if available.
472 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
473 // automatically unspilled when the scratch scope object is destroyed).
474 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
475 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200476 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477 __ LoadFromOffset(load_type,
478 GpuRegister(ensure_scratch.GetRegister()),
479 SP,
480 index1 + stack_offset);
481 __ LoadFromOffset(load_type,
482 TMP,
483 SP,
484 index2 + stack_offset);
485 __ StoreToOffset(store_type,
486 GpuRegister(ensure_scratch.GetRegister()),
487 SP,
488 index2 + stack_offset);
489 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
490}
491
492static dwarf::Reg DWARFReg(GpuRegister reg) {
493 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
494}
495
David Srbeckyba702002016-02-01 18:15:29 +0000496static dwarf::Reg DWARFReg(FpuRegister reg) {
497 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
498}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700499
500void CodeGeneratorMIPS64::GenerateFrameEntry() {
501 __ Bind(&frame_entry_label_);
502
503 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
504
505 if (do_overflow_check) {
506 __ LoadFromOffset(kLoadWord,
507 ZERO,
508 SP,
509 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
510 RecordPcInfo(nullptr, 0);
511 }
512
513 // TODO: anything related to T9/GP/GOT/PIC/.so's?
514
515 if (HasEmptyFrame()) {
516 return;
517 }
518
519 // Make sure the frame size isn't unreasonably large. Per the various APIs
520 // it looks like it should always be less than 2GB in size, which allows
521 // us using 32-bit signed offsets from the stack pointer.
522 if (GetFrameSize() > 0x7FFFFFFF)
523 LOG(FATAL) << "Stack frame larger than 2GB";
524
525 // Spill callee-saved registers.
526 // Note that their cumulative size is small and they can be indexed using
527 // 16-bit offsets.
528
529 // TODO: increment/decrement SP in one step instead of two or remove this comment.
530
531 uint32_t ofs = FrameEntrySpillSize();
532 __ IncreaseFrameSize(ofs);
533
534 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
535 GpuRegister reg = kCoreCalleeSaves[i];
536 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200537 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700538 __ Sd(reg, SP, ofs);
539 __ cfi().RelOffset(DWARFReg(reg), ofs);
540 }
541 }
542
543 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
544 FpuRegister reg = kFpuCalleeSaves[i];
545 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200546 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700547 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000548 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700549 }
550 }
551
552 // Allocate the rest of the frame and store the current method pointer
553 // at its end.
554
555 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
556
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100557 // Save the current method if we need it. Note that we do not
558 // do this in HCurrentMethod, as the instruction might have been removed
559 // in the SSA graph.
560 if (RequiresCurrentMethod()) {
561 static_assert(IsInt<16>(kCurrentMethodStackOffset),
562 "kCurrentMethodStackOffset must fit into int16_t");
563 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
564 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700565}
566
567void CodeGeneratorMIPS64::GenerateFrameExit() {
568 __ cfi().RememberState();
569
570 // TODO: anything related to T9/GP/GOT/PIC/.so's?
571
572 if (!HasEmptyFrame()) {
573 // Deallocate the rest of the frame.
574
575 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
576
577 // Restore callee-saved registers.
578 // Note that their cumulative size is small and they can be indexed using
579 // 16-bit offsets.
580
581 // TODO: increment/decrement SP in one step instead of two or remove this comment.
582
583 uint32_t ofs = 0;
584
585 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
586 FpuRegister reg = kFpuCalleeSaves[i];
587 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
588 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200589 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000590 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700591 }
592 }
593
594 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
595 GpuRegister reg = kCoreCalleeSaves[i];
596 if (allocated_registers_.ContainsCoreRegister(reg)) {
597 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200598 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700599 __ cfi().Restore(DWARFReg(reg));
600 }
601 }
602
603 DCHECK_EQ(ofs, FrameEntrySpillSize());
604 __ DecreaseFrameSize(ofs);
605 }
606
607 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700608 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700609
610 __ cfi().RestoreState();
611 __ cfi().DefCFAOffset(GetFrameSize());
612}
613
614void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
615 __ Bind(GetLabelOf(block));
616}
617
618void CodeGeneratorMIPS64::MoveLocation(Location destination,
619 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100620 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700621 if (source.Equals(destination)) {
622 return;
623 }
624
625 // A valid move can always be inferred from the destination and source
626 // locations. When moving from and to a register, the argument type can be
627 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100628 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700629 DCHECK_EQ(unspecified_type, false);
630
631 if (destination.IsRegister() || destination.IsFpuRegister()) {
632 if (unspecified_type) {
633 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
634 if (source.IsStackSlot() ||
635 (src_cst != nullptr && (src_cst->IsIntConstant()
636 || src_cst->IsFloatConstant()
637 || src_cst->IsNullConstant()))) {
638 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100639 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700640 } else {
641 // If the source is a double stack slot or a 64bit constant, a 64bit
642 // type is appropriate. Else the source is a register, and since the
643 // type has not been specified, we chose a 64bit type to force a 64bit
644 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100645 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700646 }
647 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100648 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
649 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700650 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
651 // Move to GPR/FPR from stack
652 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100653 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700654 __ LoadFpuFromOffset(load_type,
655 destination.AsFpuRegister<FpuRegister>(),
656 SP,
657 source.GetStackIndex());
658 } else {
659 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
660 __ LoadFromOffset(load_type,
661 destination.AsRegister<GpuRegister>(),
662 SP,
663 source.GetStackIndex());
664 }
665 } else if (source.IsConstant()) {
666 // Move to GPR/FPR from constant
667 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100668 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700669 gpr = destination.AsRegister<GpuRegister>();
670 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100671 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700672 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
673 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
674 gpr = ZERO;
675 } else {
676 __ LoadConst32(gpr, value);
677 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700678 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700679 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
680 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
681 gpr = ZERO;
682 } else {
683 __ LoadConst64(gpr, value);
684 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700685 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100686 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700687 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100688 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700689 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
690 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100691 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700692 if (destination.IsRegister()) {
693 // Move to GPR from GPR
694 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
695 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100696 DCHECK(destination.IsFpuRegister());
697 if (Primitive::Is64BitType(dst_type)) {
698 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
699 } else {
700 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
701 }
702 }
703 } else if (source.IsFpuRegister()) {
704 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700705 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100706 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
708 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700710 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
711 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100712 } else {
713 DCHECK(destination.IsRegister());
714 if (Primitive::Is64BitType(dst_type)) {
715 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
716 } else {
717 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
718 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700719 }
720 }
721 } else { // The destination is not a register. It must be a stack slot.
722 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
723 if (source.IsRegister() || source.IsFpuRegister()) {
724 if (unspecified_type) {
725 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100726 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700727 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100728 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700729 }
730 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100731 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
732 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700733 // Move to stack from GPR/FPR
734 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
735 if (source.IsRegister()) {
736 __ StoreToOffset(store_type,
737 source.AsRegister<GpuRegister>(),
738 SP,
739 destination.GetStackIndex());
740 } else {
741 __ StoreFpuToOffset(store_type,
742 source.AsFpuRegister<FpuRegister>(),
743 SP,
744 destination.GetStackIndex());
745 }
746 } else if (source.IsConstant()) {
747 // Move to stack from constant
748 HConstant* src_cst = source.GetConstant();
749 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700750 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700751 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700752 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
753 if (value != 0) {
754 gpr = TMP;
755 __ LoadConst32(gpr, value);
756 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700758 DCHECK(destination.IsDoubleStackSlot());
759 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
760 if (value != 0) {
761 gpr = TMP;
762 __ LoadConst64(gpr, value);
763 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700764 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700765 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 } else {
767 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
768 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
769 // Move to stack from stack
770 if (destination.IsStackSlot()) {
771 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
772 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
773 } else {
774 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
775 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
776 }
777 }
778 }
779}
780
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700781void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700782 DCHECK(!loc1.IsConstant());
783 DCHECK(!loc2.IsConstant());
784
785 if (loc1.Equals(loc2)) {
786 return;
787 }
788
789 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
790 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
791 bool is_fp_reg1 = loc1.IsFpuRegister();
792 bool is_fp_reg2 = loc2.IsFpuRegister();
793
794 if (loc2.IsRegister() && loc1.IsRegister()) {
795 // Swap 2 GPRs
796 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
797 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
798 __ Move(TMP, r2);
799 __ Move(r2, r1);
800 __ Move(r1, TMP);
801 } else if (is_fp_reg2 && is_fp_reg1) {
802 // Swap 2 FPRs
803 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
804 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700805 if (type == Primitive::kPrimFloat) {
806 __ MovS(FTMP, r1);
807 __ MovS(r1, r2);
808 __ MovS(r2, FTMP);
809 } else {
810 DCHECK_EQ(type, Primitive::kPrimDouble);
811 __ MovD(FTMP, r1);
812 __ MovD(r1, r2);
813 __ MovD(r2, FTMP);
814 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700815 } else if (is_slot1 != is_slot2) {
816 // Swap GPR/FPR and stack slot
817 Location reg_loc = is_slot1 ? loc2 : loc1;
818 Location mem_loc = is_slot1 ? loc1 : loc2;
819 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
820 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
821 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
822 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
823 if (reg_loc.IsFpuRegister()) {
824 __ StoreFpuToOffset(store_type,
825 reg_loc.AsFpuRegister<FpuRegister>(),
826 SP,
827 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700828 if (mem_loc.IsStackSlot()) {
829 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
830 } else {
831 DCHECK(mem_loc.IsDoubleStackSlot());
832 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
833 }
834 } else {
835 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
836 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
837 }
838 } else if (is_slot1 && is_slot2) {
839 move_resolver_.Exchange(loc1.GetStackIndex(),
840 loc2.GetStackIndex(),
841 loc1.IsDoubleStackSlot());
842 } else {
843 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
844 }
845}
846
Calin Juravle175dc732015-08-25 15:42:32 +0100847void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
848 DCHECK(location.IsRegister());
849 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
850}
851
Calin Juravlee460d1d2015-09-29 04:52:17 +0100852void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
853 if (location.IsRegister()) {
854 locations->AddTemp(location);
855 } else {
856 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
857 }
858}
859
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100860void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
861 GpuRegister value,
862 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700863 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700864 GpuRegister card = AT;
865 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100866 if (value_can_be_null) {
867 __ Beqzc(value, &done);
868 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700869 __ LoadFromOffset(kLoadDoubleword,
870 card,
871 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700872 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700873 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
874 __ Daddu(temp, card, temp);
875 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100876 if (value_can_be_null) {
877 __ Bind(&done);
878 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700879}
880
David Brazdil58282f42016-01-14 12:45:10 +0000881void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700882 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
883 blocked_core_registers_[ZERO] = true;
884 blocked_core_registers_[K0] = true;
885 blocked_core_registers_[K1] = true;
886 blocked_core_registers_[GP] = true;
887 blocked_core_registers_[SP] = true;
888 blocked_core_registers_[RA] = true;
889
Lazar Trsicd9672662015-09-03 17:33:01 +0200890 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
891 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700892 blocked_core_registers_[AT] = true;
893 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200894 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700895 blocked_fpu_registers_[FTMP] = true;
896
897 // Reserve suspend and thread registers.
898 blocked_core_registers_[S0] = true;
899 blocked_core_registers_[TR] = true;
900
901 // Reserve T9 for function calls
902 blocked_core_registers_[T9] = true;
903
904 // TODO: review; anything else?
905
Goran Jakovljevic782be112016-06-21 12:39:04 +0200906 if (GetGraph()->IsDebuggable()) {
907 // Stubs do not save callee-save floating point registers. If the graph
908 // is debuggable, we need to deal with these registers differently. For
909 // now, just block them.
910 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
911 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
912 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700913 }
914}
915
Alexey Frunze4dda3372015-06-01 18:31:49 -0700916size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
917 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200918 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919}
920
921size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
922 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200923 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700924}
925
926size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
927 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200928 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700929}
930
931size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
932 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200933 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700934}
935
936void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100937 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700938}
939
940void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100941 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942}
943
Calin Juravle175dc732015-08-25 15:42:32 +0100944void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700945 HInstruction* instruction,
946 uint32_t dex_pc,
947 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100948 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700949 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100950 __ LoadFromOffset(kLoadDoubleword,
951 T9,
952 TR,
953 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700955 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100956 if (EntrypointRequiresStackMap(entrypoint)) {
957 RecordPcInfo(instruction, dex_pc, slow_path);
958 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700959}
960
961void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
962 GpuRegister class_reg) {
963 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
964 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
965 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
966 // TODO: barrier needed?
967 __ Bind(slow_path->GetExitLabel());
968}
969
970void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
971 __ Sync(0); // only stype 0 is supported
972}
973
974void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
975 HBasicBlock* successor) {
976 SuspendCheckSlowPathMIPS64* slow_path =
977 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
978 codegen_->AddSlowPath(slow_path);
979
980 __ LoadFromOffset(kLoadUnsignedHalfword,
981 TMP,
982 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700983 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700984 if (successor == nullptr) {
985 __ Bnezc(TMP, slow_path->GetEntryLabel());
986 __ Bind(slow_path->GetReturnLabel());
987 } else {
988 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700989 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700990 // slow_path will return to GetLabelOf(successor).
991 }
992}
993
994InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
995 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800996 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700997 assembler_(codegen->GetAssembler()),
998 codegen_(codegen) {}
999
1000void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1001 DCHECK_EQ(instruction->InputCount(), 2U);
1002 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1003 Primitive::Type type = instruction->GetResultType();
1004 switch (type) {
1005 case Primitive::kPrimInt:
1006 case Primitive::kPrimLong: {
1007 locations->SetInAt(0, Location::RequiresRegister());
1008 HInstruction* right = instruction->InputAt(1);
1009 bool can_use_imm = false;
1010 if (right->IsConstant()) {
1011 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1012 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1013 can_use_imm = IsUint<16>(imm);
1014 } else if (instruction->IsAdd()) {
1015 can_use_imm = IsInt<16>(imm);
1016 } else {
1017 DCHECK(instruction->IsSub());
1018 can_use_imm = IsInt<16>(-imm);
1019 }
1020 }
1021 if (can_use_imm)
1022 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1023 else
1024 locations->SetInAt(1, Location::RequiresRegister());
1025 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1026 }
1027 break;
1028
1029 case Primitive::kPrimFloat:
1030 case Primitive::kPrimDouble:
1031 locations->SetInAt(0, Location::RequiresFpuRegister());
1032 locations->SetInAt(1, Location::RequiresFpuRegister());
1033 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1034 break;
1035
1036 default:
1037 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1038 }
1039}
1040
1041void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1042 Primitive::Type type = instruction->GetType();
1043 LocationSummary* locations = instruction->GetLocations();
1044
1045 switch (type) {
1046 case Primitive::kPrimInt:
1047 case Primitive::kPrimLong: {
1048 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1049 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1050 Location rhs_location = locations->InAt(1);
1051
1052 GpuRegister rhs_reg = ZERO;
1053 int64_t rhs_imm = 0;
1054 bool use_imm = rhs_location.IsConstant();
1055 if (use_imm) {
1056 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1057 } else {
1058 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1059 }
1060
1061 if (instruction->IsAnd()) {
1062 if (use_imm)
1063 __ Andi(dst, lhs, rhs_imm);
1064 else
1065 __ And(dst, lhs, rhs_reg);
1066 } else if (instruction->IsOr()) {
1067 if (use_imm)
1068 __ Ori(dst, lhs, rhs_imm);
1069 else
1070 __ Or(dst, lhs, rhs_reg);
1071 } else if (instruction->IsXor()) {
1072 if (use_imm)
1073 __ Xori(dst, lhs, rhs_imm);
1074 else
1075 __ Xor(dst, lhs, rhs_reg);
1076 } else if (instruction->IsAdd()) {
1077 if (type == Primitive::kPrimInt) {
1078 if (use_imm)
1079 __ Addiu(dst, lhs, rhs_imm);
1080 else
1081 __ Addu(dst, lhs, rhs_reg);
1082 } else {
1083 if (use_imm)
1084 __ Daddiu(dst, lhs, rhs_imm);
1085 else
1086 __ Daddu(dst, lhs, rhs_reg);
1087 }
1088 } else {
1089 DCHECK(instruction->IsSub());
1090 if (type == Primitive::kPrimInt) {
1091 if (use_imm)
1092 __ Addiu(dst, lhs, -rhs_imm);
1093 else
1094 __ Subu(dst, lhs, rhs_reg);
1095 } else {
1096 if (use_imm)
1097 __ Daddiu(dst, lhs, -rhs_imm);
1098 else
1099 __ Dsubu(dst, lhs, rhs_reg);
1100 }
1101 }
1102 break;
1103 }
1104 case Primitive::kPrimFloat:
1105 case Primitive::kPrimDouble: {
1106 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1107 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1108 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1109 if (instruction->IsAdd()) {
1110 if (type == Primitive::kPrimFloat)
1111 __ AddS(dst, lhs, rhs);
1112 else
1113 __ AddD(dst, lhs, rhs);
1114 } else if (instruction->IsSub()) {
1115 if (type == Primitive::kPrimFloat)
1116 __ SubS(dst, lhs, rhs);
1117 else
1118 __ SubD(dst, lhs, rhs);
1119 } else {
1120 LOG(FATAL) << "Unexpected floating-point binary operation";
1121 }
1122 break;
1123 }
1124 default:
1125 LOG(FATAL) << "Unexpected binary operation type " << type;
1126 }
1127}
1128
1129void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001130 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001131
1132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1133 Primitive::Type type = instr->GetResultType();
1134 switch (type) {
1135 case Primitive::kPrimInt:
1136 case Primitive::kPrimLong: {
1137 locations->SetInAt(0, Location::RequiresRegister());
1138 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001140 break;
1141 }
1142 default:
1143 LOG(FATAL) << "Unexpected shift type " << type;
1144 }
1145}
1146
1147void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001148 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149 LocationSummary* locations = instr->GetLocations();
1150 Primitive::Type type = instr->GetType();
1151
1152 switch (type) {
1153 case Primitive::kPrimInt:
1154 case Primitive::kPrimLong: {
1155 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1156 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1157 Location rhs_location = locations->InAt(1);
1158
1159 GpuRegister rhs_reg = ZERO;
1160 int64_t rhs_imm = 0;
1161 bool use_imm = rhs_location.IsConstant();
1162 if (use_imm) {
1163 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1164 } else {
1165 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1166 }
1167
1168 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001169 uint32_t shift_value = rhs_imm &
1170 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001171
Alexey Frunze92d90602015-12-18 18:16:36 -08001172 if (shift_value == 0) {
1173 if (dst != lhs) {
1174 __ Move(dst, lhs);
1175 }
1176 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001177 if (instr->IsShl()) {
1178 __ Sll(dst, lhs, shift_value);
1179 } else if (instr->IsShr()) {
1180 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001181 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001183 } else {
1184 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001185 }
1186 } else {
1187 if (shift_value < 32) {
1188 if (instr->IsShl()) {
1189 __ Dsll(dst, lhs, shift_value);
1190 } else if (instr->IsShr()) {
1191 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001192 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001193 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001194 } else {
1195 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001196 }
1197 } else {
1198 shift_value -= 32;
1199 if (instr->IsShl()) {
1200 __ Dsll32(dst, lhs, shift_value);
1201 } else if (instr->IsShr()) {
1202 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001203 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001204 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001205 } else {
1206 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001207 }
1208 }
1209 }
1210 } else {
1211 if (type == Primitive::kPrimInt) {
1212 if (instr->IsShl()) {
1213 __ Sllv(dst, lhs, rhs_reg);
1214 } else if (instr->IsShr()) {
1215 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001216 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001217 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001218 } else {
1219 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001220 }
1221 } else {
1222 if (instr->IsShl()) {
1223 __ Dsllv(dst, lhs, rhs_reg);
1224 } else if (instr->IsShr()) {
1225 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001226 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001227 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001228 } else {
1229 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001230 }
1231 }
1232 }
1233 break;
1234 }
1235 default:
1236 LOG(FATAL) << "Unexpected shift operation type " << type;
1237 }
1238}
1239
1240void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1241 HandleBinaryOp(instruction);
1242}
1243
1244void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1245 HandleBinaryOp(instruction);
1246}
1247
1248void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1249 HandleBinaryOp(instruction);
1250}
1251
1252void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1253 HandleBinaryOp(instruction);
1254}
1255
1256void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1257 LocationSummary* locations =
1258 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1259 locations->SetInAt(0, Location::RequiresRegister());
1260 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1261 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1262 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1263 } else {
1264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1265 }
1266}
1267
1268void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1269 LocationSummary* locations = instruction->GetLocations();
1270 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1271 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001272 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001274 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275 switch (type) {
1276 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001277 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1278 if (index.IsConstant()) {
1279 size_t offset =
1280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1281 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1282 } else {
1283 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1284 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1285 }
1286 break;
1287 }
1288
1289 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001290 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1291 if (index.IsConstant()) {
1292 size_t offset =
1293 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1294 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1295 } else {
1296 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1297 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1298 }
1299 break;
1300 }
1301
1302 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001303 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1304 if (index.IsConstant()) {
1305 size_t offset =
1306 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1307 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1308 } else {
1309 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1310 __ Daddu(TMP, obj, TMP);
1311 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1312 }
1313 break;
1314 }
1315
1316 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001317 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1318 if (index.IsConstant()) {
1319 size_t offset =
1320 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1321 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1322 } else {
1323 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1324 __ Daddu(TMP, obj, TMP);
1325 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1326 }
1327 break;
1328 }
1329
1330 case Primitive::kPrimInt:
1331 case Primitive::kPrimNot: {
1332 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001333 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1334 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1335 if (index.IsConstant()) {
1336 size_t offset =
1337 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1338 __ LoadFromOffset(load_type, out, obj, offset);
1339 } else {
1340 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1341 __ Daddu(TMP, obj, TMP);
1342 __ LoadFromOffset(load_type, out, TMP, data_offset);
1343 }
1344 break;
1345 }
1346
1347 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001348 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1349 if (index.IsConstant()) {
1350 size_t offset =
1351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1352 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1353 } else {
1354 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1355 __ Daddu(TMP, obj, TMP);
1356 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1357 }
1358 break;
1359 }
1360
1361 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001362 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1363 if (index.IsConstant()) {
1364 size_t offset =
1365 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1366 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1367 } else {
1368 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1369 __ Daddu(TMP, obj, TMP);
1370 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1371 }
1372 break;
1373 }
1374
1375 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001376 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1377 if (index.IsConstant()) {
1378 size_t offset =
1379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1380 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1381 } else {
1382 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1383 __ Daddu(TMP, obj, TMP);
1384 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1385 }
1386 break;
1387 }
1388
1389 case Primitive::kPrimVoid:
1390 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1391 UNREACHABLE();
1392 }
1393 codegen_->MaybeRecordImplicitNullCheck(instruction);
1394}
1395
1396void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1398 locations->SetInAt(0, Location::RequiresRegister());
1399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1400}
1401
1402void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1403 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001404 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001405 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1406 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1407 __ LoadFromOffset(kLoadWord, out, obj, offset);
1408 codegen_->MaybeRecordImplicitNullCheck(instruction);
1409}
1410
1411void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001412 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1414 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001415 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001416 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001417 InvokeRuntimeCallingConvention calling_convention;
1418 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1419 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1420 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1421 } else {
1422 locations->SetInAt(0, Location::RequiresRegister());
1423 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1424 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1425 locations->SetInAt(2, Location::RequiresFpuRegister());
1426 } else {
1427 locations->SetInAt(2, Location::RequiresRegister());
1428 }
1429 }
1430}
1431
1432void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1433 LocationSummary* locations = instruction->GetLocations();
1434 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1435 Location index = locations->InAt(1);
1436 Primitive::Type value_type = instruction->GetComponentType();
1437 bool needs_runtime_call = locations->WillCall();
1438 bool needs_write_barrier =
1439 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1440
1441 switch (value_type) {
1442 case Primitive::kPrimBoolean:
1443 case Primitive::kPrimByte: {
1444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1445 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1446 if (index.IsConstant()) {
1447 size_t offset =
1448 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1449 __ StoreToOffset(kStoreByte, value, obj, offset);
1450 } else {
1451 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1452 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1453 }
1454 break;
1455 }
1456
1457 case Primitive::kPrimShort:
1458 case Primitive::kPrimChar: {
1459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1460 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1461 if (index.IsConstant()) {
1462 size_t offset =
1463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1464 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1465 } else {
1466 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1467 __ Daddu(TMP, obj, TMP);
1468 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1469 }
1470 break;
1471 }
1472
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimNot: {
1475 if (!needs_runtime_call) {
1476 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1477 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1478 if (index.IsConstant()) {
1479 size_t offset =
1480 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1481 __ StoreToOffset(kStoreWord, value, obj, offset);
1482 } else {
1483 DCHECK(index.IsRegister()) << index;
1484 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1485 __ Daddu(TMP, obj, TMP);
1486 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1487 }
1488 codegen_->MaybeRecordImplicitNullCheck(instruction);
1489 if (needs_write_barrier) {
1490 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001491 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001492 }
1493 } else {
1494 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001495 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001496 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001497 }
1498 break;
1499 }
1500
1501 case Primitive::kPrimLong: {
1502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1503 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1504 if (index.IsConstant()) {
1505 size_t offset =
1506 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1507 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1508 } else {
1509 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1510 __ Daddu(TMP, obj, TMP);
1511 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1512 }
1513 break;
1514 }
1515
1516 case Primitive::kPrimFloat: {
1517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1518 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1519 DCHECK(locations->InAt(2).IsFpuRegister());
1520 if (index.IsConstant()) {
1521 size_t offset =
1522 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1523 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1524 } else {
1525 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1526 __ Daddu(TMP, obj, TMP);
1527 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1528 }
1529 break;
1530 }
1531
1532 case Primitive::kPrimDouble: {
1533 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1534 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1535 DCHECK(locations->InAt(2).IsFpuRegister());
1536 if (index.IsConstant()) {
1537 size_t offset =
1538 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1539 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1540 } else {
1541 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1542 __ Daddu(TMP, obj, TMP);
1543 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1544 }
1545 break;
1546 }
1547
1548 case Primitive::kPrimVoid:
1549 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1550 UNREACHABLE();
1551 }
1552
1553 // Ints and objects are handled in the switch.
1554 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1555 codegen_->MaybeRecordImplicitNullCheck(instruction);
1556 }
1557}
1558
1559void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001560 RegisterSet caller_saves = RegisterSet::Empty();
1561 InvokeRuntimeCallingConvention calling_convention;
1562 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1563 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1564 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001565 locations->SetInAt(0, Location::RequiresRegister());
1566 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001567}
1568
1569void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1570 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001571 BoundsCheckSlowPathMIPS64* slow_path =
1572 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001573 codegen_->AddSlowPath(slow_path);
1574
1575 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1576 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1577
1578 // length is limited by the maximum positive signed 32-bit integer.
1579 // Unsigned comparison of length and index checks for index < 0
1580 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001581 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001582}
1583
1584void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1586 instruction,
1587 LocationSummary::kCallOnSlowPath);
1588 locations->SetInAt(0, Location::RequiresRegister());
1589 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001590 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001591 locations->AddTemp(Location::RequiresRegister());
1592}
1593
1594void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1595 LocationSummary* locations = instruction->GetLocations();
1596 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1597 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1598 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1599
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001600 SlowPathCodeMIPS64* slow_path =
1601 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001602 codegen_->AddSlowPath(slow_path);
1603
1604 // TODO: avoid this check if we know obj is not null.
1605 __ Beqzc(obj, slow_path->GetExitLabel());
1606 // Compare the class of `obj` with `cls`.
1607 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1608 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1609 __ Bind(slow_path->GetExitLabel());
1610}
1611
1612void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1613 LocationSummary* locations =
1614 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1615 locations->SetInAt(0, Location::RequiresRegister());
1616 if (check->HasUses()) {
1617 locations->SetOut(Location::SameAsFirstInput());
1618 }
1619}
1620
1621void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1622 // We assume the class is not null.
1623 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1624 check->GetLoadClass(),
1625 check,
1626 check->GetDexPc(),
1627 true);
1628 codegen_->AddSlowPath(slow_path);
1629 GenerateClassInitializationCheck(slow_path,
1630 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1631}
1632
1633void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1634 Primitive::Type in_type = compare->InputAt(0)->GetType();
1635
Alexey Frunze299a9392015-12-08 16:08:02 -08001636 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001637
1638 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001639 case Primitive::kPrimBoolean:
1640 case Primitive::kPrimByte:
1641 case Primitive::kPrimShort:
1642 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001643 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001644 case Primitive::kPrimLong:
1645 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001646 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1648 break;
1649
1650 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001651 case Primitive::kPrimDouble:
1652 locations->SetInAt(0, Location::RequiresFpuRegister());
1653 locations->SetInAt(1, Location::RequiresFpuRegister());
1654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001655 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001656
1657 default:
1658 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1659 }
1660}
1661
1662void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1663 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001664 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001665 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1666
1667 // 0 if: left == right
1668 // 1 if: left > right
1669 // -1 if: left < right
1670 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001671 case Primitive::kPrimBoolean:
1672 case Primitive::kPrimByte:
1673 case Primitive::kPrimShort:
1674 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001675 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001677 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001678 Location rhs_location = locations->InAt(1);
1679 bool use_imm = rhs_location.IsConstant();
1680 GpuRegister rhs = ZERO;
1681 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001682 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001683 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1684 if (value != 0) {
1685 rhs = AT;
1686 __ LoadConst64(rhs, value);
1687 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001688 } else {
1689 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1690 if (value != 0) {
1691 rhs = AT;
1692 __ LoadConst32(rhs, value);
1693 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001694 }
1695 } else {
1696 rhs = rhs_location.AsRegister<GpuRegister>();
1697 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001699 __ Slt(res, rhs, lhs);
1700 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 break;
1702 }
1703
Alexey Frunze299a9392015-12-08 16:08:02 -08001704 case Primitive::kPrimFloat: {
1705 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1706 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1707 Mips64Label done;
1708 __ CmpEqS(FTMP, lhs, rhs);
1709 __ LoadConst32(res, 0);
1710 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001711 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001712 __ CmpLtS(FTMP, lhs, rhs);
1713 __ LoadConst32(res, -1);
1714 __ Bc1nez(FTMP, &done);
1715 __ LoadConst32(res, 1);
1716 } else {
1717 __ CmpLtS(FTMP, rhs, lhs);
1718 __ LoadConst32(res, 1);
1719 __ Bc1nez(FTMP, &done);
1720 __ LoadConst32(res, -1);
1721 }
1722 __ Bind(&done);
1723 break;
1724 }
1725
Alexey Frunze4dda3372015-06-01 18:31:49 -07001726 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001727 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1728 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1729 Mips64Label done;
1730 __ CmpEqD(FTMP, lhs, rhs);
1731 __ LoadConst32(res, 0);
1732 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001733 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001734 __ CmpLtD(FTMP, lhs, rhs);
1735 __ LoadConst32(res, -1);
1736 __ Bc1nez(FTMP, &done);
1737 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001738 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001739 __ CmpLtD(FTMP, rhs, lhs);
1740 __ LoadConst32(res, 1);
1741 __ Bc1nez(FTMP, &done);
1742 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001743 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001744 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001745 break;
1746 }
1747
1748 default:
1749 LOG(FATAL) << "Unimplemented compare type " << in_type;
1750 }
1751}
1752
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001755 switch (instruction->InputAt(0)->GetType()) {
1756 default:
1757 case Primitive::kPrimLong:
1758 locations->SetInAt(0, Location::RequiresRegister());
1759 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1760 break;
1761
1762 case Primitive::kPrimFloat:
1763 case Primitive::kPrimDouble:
1764 locations->SetInAt(0, Location::RequiresFpuRegister());
1765 locations->SetInAt(1, Location::RequiresFpuRegister());
1766 break;
1767 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001768 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001769 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1770 }
1771}
1772
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001774 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775 return;
1776 }
1777
Alexey Frunze299a9392015-12-08 16:08:02 -08001778 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001780 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001781 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001782
Alexey Frunze299a9392015-12-08 16:08:02 -08001783 switch (type) {
1784 default:
1785 // Integer case.
1786 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1787 return;
1788 case Primitive::kPrimLong:
1789 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1790 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001791
Alexey Frunze299a9392015-12-08 16:08:02 -08001792 case Primitive::kPrimFloat:
1793 case Primitive::kPrimDouble:
1794 // TODO: don't use branches.
1795 GenerateFpCompareAndBranch(instruction->GetCondition(),
1796 instruction->IsGtBias(),
1797 type,
1798 locations,
1799 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001800 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001801 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001802
1803 // Convert the branches into the result.
1804 Mips64Label done;
1805
1806 // False case: result = 0.
1807 __ LoadConst32(dst, 0);
1808 __ Bc(&done);
1809
1810 // True case: result = 1.
1811 __ Bind(&true_label);
1812 __ LoadConst32(dst, 1);
1813 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001814}
1815
Alexey Frunzec857c742015-09-23 15:12:39 -07001816void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1817 DCHECK(instruction->IsDiv() || instruction->IsRem());
1818 Primitive::Type type = instruction->GetResultType();
1819
1820 LocationSummary* locations = instruction->GetLocations();
1821 Location second = locations->InAt(1);
1822 DCHECK(second.IsConstant());
1823
1824 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1825 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1826 int64_t imm = Int64FromConstant(second.GetConstant());
1827 DCHECK(imm == 1 || imm == -1);
1828
1829 if (instruction->IsRem()) {
1830 __ Move(out, ZERO);
1831 } else {
1832 if (imm == -1) {
1833 if (type == Primitive::kPrimInt) {
1834 __ Subu(out, ZERO, dividend);
1835 } else {
1836 DCHECK_EQ(type, Primitive::kPrimLong);
1837 __ Dsubu(out, ZERO, dividend);
1838 }
1839 } else if (out != dividend) {
1840 __ Move(out, dividend);
1841 }
1842 }
1843}
1844
1845void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1846 DCHECK(instruction->IsDiv() || instruction->IsRem());
1847 Primitive::Type type = instruction->GetResultType();
1848
1849 LocationSummary* locations = instruction->GetLocations();
1850 Location second = locations->InAt(1);
1851 DCHECK(second.IsConstant());
1852
1853 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1854 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1855 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001856 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001857 int ctz_imm = CTZ(abs_imm);
1858
1859 if (instruction->IsDiv()) {
1860 if (type == Primitive::kPrimInt) {
1861 if (ctz_imm == 1) {
1862 // Fast path for division by +/-2, which is very common.
1863 __ Srl(TMP, dividend, 31);
1864 } else {
1865 __ Sra(TMP, dividend, 31);
1866 __ Srl(TMP, TMP, 32 - ctz_imm);
1867 }
1868 __ Addu(out, dividend, TMP);
1869 __ Sra(out, out, ctz_imm);
1870 if (imm < 0) {
1871 __ Subu(out, ZERO, out);
1872 }
1873 } else {
1874 DCHECK_EQ(type, Primitive::kPrimLong);
1875 if (ctz_imm == 1) {
1876 // Fast path for division by +/-2, which is very common.
1877 __ Dsrl32(TMP, dividend, 31);
1878 } else {
1879 __ Dsra32(TMP, dividend, 31);
1880 if (ctz_imm > 32) {
1881 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1882 } else {
1883 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1884 }
1885 }
1886 __ Daddu(out, dividend, TMP);
1887 if (ctz_imm < 32) {
1888 __ Dsra(out, out, ctz_imm);
1889 } else {
1890 __ Dsra32(out, out, ctz_imm - 32);
1891 }
1892 if (imm < 0) {
1893 __ Dsubu(out, ZERO, out);
1894 }
1895 }
1896 } else {
1897 if (type == Primitive::kPrimInt) {
1898 if (ctz_imm == 1) {
1899 // Fast path for modulo +/-2, which is very common.
1900 __ Sra(TMP, dividend, 31);
1901 __ Subu(out, dividend, TMP);
1902 __ Andi(out, out, 1);
1903 __ Addu(out, out, TMP);
1904 } else {
1905 __ Sra(TMP, dividend, 31);
1906 __ Srl(TMP, TMP, 32 - ctz_imm);
1907 __ Addu(out, dividend, TMP);
1908 if (IsUint<16>(abs_imm - 1)) {
1909 __ Andi(out, out, abs_imm - 1);
1910 } else {
1911 __ Sll(out, out, 32 - ctz_imm);
1912 __ Srl(out, out, 32 - ctz_imm);
1913 }
1914 __ Subu(out, out, TMP);
1915 }
1916 } else {
1917 DCHECK_EQ(type, Primitive::kPrimLong);
1918 if (ctz_imm == 1) {
1919 // Fast path for modulo +/-2, which is very common.
1920 __ Dsra32(TMP, dividend, 31);
1921 __ Dsubu(out, dividend, TMP);
1922 __ Andi(out, out, 1);
1923 __ Daddu(out, out, TMP);
1924 } else {
1925 __ Dsra32(TMP, dividend, 31);
1926 if (ctz_imm > 32) {
1927 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1928 } else {
1929 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1930 }
1931 __ Daddu(out, dividend, TMP);
1932 if (IsUint<16>(abs_imm - 1)) {
1933 __ Andi(out, out, abs_imm - 1);
1934 } else {
1935 if (ctz_imm > 32) {
1936 __ Dsll(out, out, 64 - ctz_imm);
1937 __ Dsrl(out, out, 64 - ctz_imm);
1938 } else {
1939 __ Dsll32(out, out, 32 - ctz_imm);
1940 __ Dsrl32(out, out, 32 - ctz_imm);
1941 }
1942 }
1943 __ Dsubu(out, out, TMP);
1944 }
1945 }
1946 }
1947}
1948
1949void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1950 DCHECK(instruction->IsDiv() || instruction->IsRem());
1951
1952 LocationSummary* locations = instruction->GetLocations();
1953 Location second = locations->InAt(1);
1954 DCHECK(second.IsConstant());
1955
1956 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1957 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1958 int64_t imm = Int64FromConstant(second.GetConstant());
1959
1960 Primitive::Type type = instruction->GetResultType();
1961 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1962
1963 int64_t magic;
1964 int shift;
1965 CalculateMagicAndShiftForDivRem(imm,
1966 (type == Primitive::kPrimLong),
1967 &magic,
1968 &shift);
1969
1970 if (type == Primitive::kPrimInt) {
1971 __ LoadConst32(TMP, magic);
1972 __ MuhR6(TMP, dividend, TMP);
1973
1974 if (imm > 0 && magic < 0) {
1975 __ Addu(TMP, TMP, dividend);
1976 } else if (imm < 0 && magic > 0) {
1977 __ Subu(TMP, TMP, dividend);
1978 }
1979
1980 if (shift != 0) {
1981 __ Sra(TMP, TMP, shift);
1982 }
1983
1984 if (instruction->IsDiv()) {
1985 __ Sra(out, TMP, 31);
1986 __ Subu(out, TMP, out);
1987 } else {
1988 __ Sra(AT, TMP, 31);
1989 __ Subu(AT, TMP, AT);
1990 __ LoadConst32(TMP, imm);
1991 __ MulR6(TMP, AT, TMP);
1992 __ Subu(out, dividend, TMP);
1993 }
1994 } else {
1995 __ LoadConst64(TMP, magic);
1996 __ Dmuh(TMP, dividend, TMP);
1997
1998 if (imm > 0 && magic < 0) {
1999 __ Daddu(TMP, TMP, dividend);
2000 } else if (imm < 0 && magic > 0) {
2001 __ Dsubu(TMP, TMP, dividend);
2002 }
2003
2004 if (shift >= 32) {
2005 __ Dsra32(TMP, TMP, shift - 32);
2006 } else if (shift > 0) {
2007 __ Dsra(TMP, TMP, shift);
2008 }
2009
2010 if (instruction->IsDiv()) {
2011 __ Dsra32(out, TMP, 31);
2012 __ Dsubu(out, TMP, out);
2013 } else {
2014 __ Dsra32(AT, TMP, 31);
2015 __ Dsubu(AT, TMP, AT);
2016 __ LoadConst64(TMP, imm);
2017 __ Dmul(TMP, AT, TMP);
2018 __ Dsubu(out, dividend, TMP);
2019 }
2020 }
2021}
2022
2023void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2024 DCHECK(instruction->IsDiv() || instruction->IsRem());
2025 Primitive::Type type = instruction->GetResultType();
2026 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2027
2028 LocationSummary* locations = instruction->GetLocations();
2029 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2030 Location second = locations->InAt(1);
2031
2032 if (second.IsConstant()) {
2033 int64_t imm = Int64FromConstant(second.GetConstant());
2034 if (imm == 0) {
2035 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2036 } else if (imm == 1 || imm == -1) {
2037 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002038 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002039 DivRemByPowerOfTwo(instruction);
2040 } else {
2041 DCHECK(imm <= -2 || imm >= 2);
2042 GenerateDivRemWithAnyConstant(instruction);
2043 }
2044 } else {
2045 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2046 GpuRegister divisor = second.AsRegister<GpuRegister>();
2047 if (instruction->IsDiv()) {
2048 if (type == Primitive::kPrimInt)
2049 __ DivR6(out, dividend, divisor);
2050 else
2051 __ Ddiv(out, dividend, divisor);
2052 } else {
2053 if (type == Primitive::kPrimInt)
2054 __ ModR6(out, dividend, divisor);
2055 else
2056 __ Dmod(out, dividend, divisor);
2057 }
2058 }
2059}
2060
Alexey Frunze4dda3372015-06-01 18:31:49 -07002061void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2064 switch (div->GetResultType()) {
2065 case Primitive::kPrimInt:
2066 case Primitive::kPrimLong:
2067 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002068 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2070 break;
2071
2072 case Primitive::kPrimFloat:
2073 case Primitive::kPrimDouble:
2074 locations->SetInAt(0, Location::RequiresFpuRegister());
2075 locations->SetInAt(1, Location::RequiresFpuRegister());
2076 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2077 break;
2078
2079 default:
2080 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2081 }
2082}
2083
2084void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2085 Primitive::Type type = instruction->GetType();
2086 LocationSummary* locations = instruction->GetLocations();
2087
2088 switch (type) {
2089 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002090 case Primitive::kPrimLong:
2091 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002092 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002093 case Primitive::kPrimFloat:
2094 case Primitive::kPrimDouble: {
2095 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2096 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2097 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2098 if (type == Primitive::kPrimFloat)
2099 __ DivS(dst, lhs, rhs);
2100 else
2101 __ DivD(dst, lhs, rhs);
2102 break;
2103 }
2104 default:
2105 LOG(FATAL) << "Unexpected div type " << type;
2106 }
2107}
2108
2109void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002110 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002111 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002112}
2113
2114void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2115 SlowPathCodeMIPS64* slow_path =
2116 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2117 codegen_->AddSlowPath(slow_path);
2118 Location value = instruction->GetLocations()->InAt(0);
2119
2120 Primitive::Type type = instruction->GetType();
2121
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002122 if (!Primitive::IsIntegralType(type)) {
2123 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002124 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002125 }
2126
2127 if (value.IsConstant()) {
2128 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2129 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002130 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 } else {
2132 // A division by a non-null constant is valid. We don't need to perform
2133 // any check, so simply fall through.
2134 }
2135 } else {
2136 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2137 }
2138}
2139
2140void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2141 LocationSummary* locations =
2142 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2143 locations->SetOut(Location::ConstantLocation(constant));
2144}
2145
2146void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2147 // Will be generated at use site.
2148}
2149
2150void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2151 exit->SetLocations(nullptr);
2152}
2153
2154void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2155}
2156
2157void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2158 LocationSummary* locations =
2159 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2160 locations->SetOut(Location::ConstantLocation(constant));
2161}
2162
2163void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2164 // Will be generated at use site.
2165}
2166
David Brazdilfc6a86a2015-06-26 10:33:45 +00002167void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002168 DCHECK(!successor->IsExitBlock());
2169 HBasicBlock* block = got->GetBlock();
2170 HInstruction* previous = got->GetPrevious();
2171 HLoopInformation* info = block->GetLoopInformation();
2172
2173 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2174 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2175 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2176 return;
2177 }
2178 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2179 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2180 }
2181 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002182 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002183 }
2184}
2185
David Brazdilfc6a86a2015-06-26 10:33:45 +00002186void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2187 got->SetLocations(nullptr);
2188}
2189
2190void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2191 HandleGoto(got, got->GetSuccessor());
2192}
2193
2194void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2195 try_boundary->SetLocations(nullptr);
2196}
2197
2198void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2199 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2200 if (!successor->IsExitBlock()) {
2201 HandleGoto(try_boundary, successor);
2202 }
2203}
2204
Alexey Frunze299a9392015-12-08 16:08:02 -08002205void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2206 bool is64bit,
2207 LocationSummary* locations) {
2208 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2209 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2210 Location rhs_location = locations->InAt(1);
2211 GpuRegister rhs_reg = ZERO;
2212 int64_t rhs_imm = 0;
2213 bool use_imm = rhs_location.IsConstant();
2214 if (use_imm) {
2215 if (is64bit) {
2216 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2217 } else {
2218 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2219 }
2220 } else {
2221 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2222 }
2223 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2224
2225 switch (cond) {
2226 case kCondEQ:
2227 case kCondNE:
2228 if (use_imm && IsUint<16>(rhs_imm)) {
2229 __ Xori(dst, lhs, rhs_imm);
2230 } else {
2231 if (use_imm) {
2232 rhs_reg = TMP;
2233 __ LoadConst64(rhs_reg, rhs_imm);
2234 }
2235 __ Xor(dst, lhs, rhs_reg);
2236 }
2237 if (cond == kCondEQ) {
2238 __ Sltiu(dst, dst, 1);
2239 } else {
2240 __ Sltu(dst, ZERO, dst);
2241 }
2242 break;
2243
2244 case kCondLT:
2245 case kCondGE:
2246 if (use_imm && IsInt<16>(rhs_imm)) {
2247 __ Slti(dst, lhs, rhs_imm);
2248 } else {
2249 if (use_imm) {
2250 rhs_reg = TMP;
2251 __ LoadConst64(rhs_reg, rhs_imm);
2252 }
2253 __ Slt(dst, lhs, rhs_reg);
2254 }
2255 if (cond == kCondGE) {
2256 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2257 // only the slt instruction but no sge.
2258 __ Xori(dst, dst, 1);
2259 }
2260 break;
2261
2262 case kCondLE:
2263 case kCondGT:
2264 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2265 // Simulate lhs <= rhs via lhs < rhs + 1.
2266 __ Slti(dst, lhs, rhs_imm_plus_one);
2267 if (cond == kCondGT) {
2268 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2269 // only the slti instruction but no sgti.
2270 __ Xori(dst, dst, 1);
2271 }
2272 } else {
2273 if (use_imm) {
2274 rhs_reg = TMP;
2275 __ LoadConst64(rhs_reg, rhs_imm);
2276 }
2277 __ Slt(dst, rhs_reg, lhs);
2278 if (cond == kCondLE) {
2279 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2280 // only the slt instruction but no sle.
2281 __ Xori(dst, dst, 1);
2282 }
2283 }
2284 break;
2285
2286 case kCondB:
2287 case kCondAE:
2288 if (use_imm && IsInt<16>(rhs_imm)) {
2289 // Sltiu sign-extends its 16-bit immediate operand before
2290 // the comparison and thus lets us compare directly with
2291 // unsigned values in the ranges [0, 0x7fff] and
2292 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2293 __ Sltiu(dst, lhs, rhs_imm);
2294 } else {
2295 if (use_imm) {
2296 rhs_reg = TMP;
2297 __ LoadConst64(rhs_reg, rhs_imm);
2298 }
2299 __ Sltu(dst, lhs, rhs_reg);
2300 }
2301 if (cond == kCondAE) {
2302 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2303 // only the sltu instruction but no sgeu.
2304 __ Xori(dst, dst, 1);
2305 }
2306 break;
2307
2308 case kCondBE:
2309 case kCondA:
2310 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2311 // Simulate lhs <= rhs via lhs < rhs + 1.
2312 // Note that this only works if rhs + 1 does not overflow
2313 // to 0, hence the check above.
2314 // Sltiu sign-extends its 16-bit immediate operand before
2315 // the comparison and thus lets us compare directly with
2316 // unsigned values in the ranges [0, 0x7fff] and
2317 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2318 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2319 if (cond == kCondA) {
2320 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2321 // only the sltiu instruction but no sgtiu.
2322 __ Xori(dst, dst, 1);
2323 }
2324 } else {
2325 if (use_imm) {
2326 rhs_reg = TMP;
2327 __ LoadConst64(rhs_reg, rhs_imm);
2328 }
2329 __ Sltu(dst, rhs_reg, lhs);
2330 if (cond == kCondBE) {
2331 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2332 // only the sltu instruction but no sleu.
2333 __ Xori(dst, dst, 1);
2334 }
2335 }
2336 break;
2337 }
2338}
2339
2340void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2341 bool is64bit,
2342 LocationSummary* locations,
2343 Mips64Label* label) {
2344 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2345 Location rhs_location = locations->InAt(1);
2346 GpuRegister rhs_reg = ZERO;
2347 int64_t rhs_imm = 0;
2348 bool use_imm = rhs_location.IsConstant();
2349 if (use_imm) {
2350 if (is64bit) {
2351 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2352 } else {
2353 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2354 }
2355 } else {
2356 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2357 }
2358
2359 if (use_imm && rhs_imm == 0) {
2360 switch (cond) {
2361 case kCondEQ:
2362 case kCondBE: // <= 0 if zero
2363 __ Beqzc(lhs, label);
2364 break;
2365 case kCondNE:
2366 case kCondA: // > 0 if non-zero
2367 __ Bnezc(lhs, label);
2368 break;
2369 case kCondLT:
2370 __ Bltzc(lhs, label);
2371 break;
2372 case kCondGE:
2373 __ Bgezc(lhs, label);
2374 break;
2375 case kCondLE:
2376 __ Blezc(lhs, label);
2377 break;
2378 case kCondGT:
2379 __ Bgtzc(lhs, label);
2380 break;
2381 case kCondB: // always false
2382 break;
2383 case kCondAE: // always true
2384 __ Bc(label);
2385 break;
2386 }
2387 } else {
2388 if (use_imm) {
2389 rhs_reg = TMP;
2390 __ LoadConst64(rhs_reg, rhs_imm);
2391 }
2392 switch (cond) {
2393 case kCondEQ:
2394 __ Beqc(lhs, rhs_reg, label);
2395 break;
2396 case kCondNE:
2397 __ Bnec(lhs, rhs_reg, label);
2398 break;
2399 case kCondLT:
2400 __ Bltc(lhs, rhs_reg, label);
2401 break;
2402 case kCondGE:
2403 __ Bgec(lhs, rhs_reg, label);
2404 break;
2405 case kCondLE:
2406 __ Bgec(rhs_reg, lhs, label);
2407 break;
2408 case kCondGT:
2409 __ Bltc(rhs_reg, lhs, label);
2410 break;
2411 case kCondB:
2412 __ Bltuc(lhs, rhs_reg, label);
2413 break;
2414 case kCondAE:
2415 __ Bgeuc(lhs, rhs_reg, label);
2416 break;
2417 case kCondBE:
2418 __ Bgeuc(rhs_reg, lhs, label);
2419 break;
2420 case kCondA:
2421 __ Bltuc(rhs_reg, lhs, label);
2422 break;
2423 }
2424 }
2425}
2426
2427void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2428 bool gt_bias,
2429 Primitive::Type type,
2430 LocationSummary* locations,
2431 Mips64Label* label) {
2432 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2433 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2434 if (type == Primitive::kPrimFloat) {
2435 switch (cond) {
2436 case kCondEQ:
2437 __ CmpEqS(FTMP, lhs, rhs);
2438 __ Bc1nez(FTMP, label);
2439 break;
2440 case kCondNE:
2441 __ CmpEqS(FTMP, lhs, rhs);
2442 __ Bc1eqz(FTMP, label);
2443 break;
2444 case kCondLT:
2445 if (gt_bias) {
2446 __ CmpLtS(FTMP, lhs, rhs);
2447 } else {
2448 __ CmpUltS(FTMP, lhs, rhs);
2449 }
2450 __ Bc1nez(FTMP, label);
2451 break;
2452 case kCondLE:
2453 if (gt_bias) {
2454 __ CmpLeS(FTMP, lhs, rhs);
2455 } else {
2456 __ CmpUleS(FTMP, lhs, rhs);
2457 }
2458 __ Bc1nez(FTMP, label);
2459 break;
2460 case kCondGT:
2461 if (gt_bias) {
2462 __ CmpUltS(FTMP, rhs, lhs);
2463 } else {
2464 __ CmpLtS(FTMP, rhs, lhs);
2465 }
2466 __ Bc1nez(FTMP, label);
2467 break;
2468 case kCondGE:
2469 if (gt_bias) {
2470 __ CmpUleS(FTMP, rhs, lhs);
2471 } else {
2472 __ CmpLeS(FTMP, rhs, lhs);
2473 }
2474 __ Bc1nez(FTMP, label);
2475 break;
2476 default:
2477 LOG(FATAL) << "Unexpected non-floating-point condition";
2478 }
2479 } else {
2480 DCHECK_EQ(type, Primitive::kPrimDouble);
2481 switch (cond) {
2482 case kCondEQ:
2483 __ CmpEqD(FTMP, lhs, rhs);
2484 __ Bc1nez(FTMP, label);
2485 break;
2486 case kCondNE:
2487 __ CmpEqD(FTMP, lhs, rhs);
2488 __ Bc1eqz(FTMP, label);
2489 break;
2490 case kCondLT:
2491 if (gt_bias) {
2492 __ CmpLtD(FTMP, lhs, rhs);
2493 } else {
2494 __ CmpUltD(FTMP, lhs, rhs);
2495 }
2496 __ Bc1nez(FTMP, label);
2497 break;
2498 case kCondLE:
2499 if (gt_bias) {
2500 __ CmpLeD(FTMP, lhs, rhs);
2501 } else {
2502 __ CmpUleD(FTMP, lhs, rhs);
2503 }
2504 __ Bc1nez(FTMP, label);
2505 break;
2506 case kCondGT:
2507 if (gt_bias) {
2508 __ CmpUltD(FTMP, rhs, lhs);
2509 } else {
2510 __ CmpLtD(FTMP, rhs, lhs);
2511 }
2512 __ Bc1nez(FTMP, label);
2513 break;
2514 case kCondGE:
2515 if (gt_bias) {
2516 __ CmpUleD(FTMP, rhs, lhs);
2517 } else {
2518 __ CmpLeD(FTMP, rhs, lhs);
2519 }
2520 __ Bc1nez(FTMP, label);
2521 break;
2522 default:
2523 LOG(FATAL) << "Unexpected non-floating-point condition";
2524 }
2525 }
2526}
2527
Alexey Frunze4dda3372015-06-01 18:31:49 -07002528void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002529 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002530 Mips64Label* true_target,
2531 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002532 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002533
David Brazdil0debae72015-11-12 18:37:00 +00002534 if (true_target == nullptr && false_target == nullptr) {
2535 // Nothing to do. The code always falls through.
2536 return;
2537 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002538 // Constant condition, statically compared against "true" (integer value 1).
2539 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002540 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002541 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002543 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002544 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002545 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002546 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002547 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002548 }
David Brazdil0debae72015-11-12 18:37:00 +00002549 return;
2550 }
2551
2552 // The following code generates these patterns:
2553 // (1) true_target == nullptr && false_target != nullptr
2554 // - opposite condition true => branch to false_target
2555 // (2) true_target != nullptr && false_target == nullptr
2556 // - condition true => branch to true_target
2557 // (3) true_target != nullptr && false_target != nullptr
2558 // - condition true => branch to true_target
2559 // - branch to false_target
2560 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002561 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002562 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002564 if (true_target == nullptr) {
2565 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2566 } else {
2567 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2568 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002569 } else {
2570 // The condition instruction has not been materialized, use its inputs as
2571 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002572 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002573 Primitive::Type type = condition->InputAt(0)->GetType();
2574 LocationSummary* locations = cond->GetLocations();
2575 IfCondition if_cond = condition->GetCondition();
2576 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002577
David Brazdil0debae72015-11-12 18:37:00 +00002578 if (true_target == nullptr) {
2579 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002580 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002581 }
2582
Alexey Frunze299a9392015-12-08 16:08:02 -08002583 switch (type) {
2584 default:
2585 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2586 break;
2587 case Primitive::kPrimLong:
2588 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2589 break;
2590 case Primitive::kPrimFloat:
2591 case Primitive::kPrimDouble:
2592 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2593 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002594 }
2595 }
David Brazdil0debae72015-11-12 18:37:00 +00002596
2597 // If neither branch falls through (case 3), the conditional branch to `true_target`
2598 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2599 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002600 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 }
2602}
2603
2604void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002606 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002607 locations->SetInAt(0, Location::RequiresRegister());
2608 }
2609}
2610
2611void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002612 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2613 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002614 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002615 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002616 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002617 nullptr : codegen_->GetLabelOf(false_successor);
2618 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002619}
2620
2621void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2622 LocationSummary* locations = new (GetGraph()->GetArena())
2623 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002624 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002625 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002626 locations->SetInAt(0, Location::RequiresRegister());
2627 }
2628}
2629
2630void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002631 SlowPathCodeMIPS64* slow_path =
2632 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002633 GenerateTestAndBranch(deoptimize,
2634 /* condition_input_index */ 0,
2635 slow_path->GetEntryLabel(),
2636 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002637}
2638
David Brazdil74eb1b22015-12-14 11:44:01 +00002639void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2641 if (Primitive::IsFloatingPointType(select->GetType())) {
2642 locations->SetInAt(0, Location::RequiresFpuRegister());
2643 locations->SetInAt(1, Location::RequiresFpuRegister());
2644 } else {
2645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::RequiresRegister());
2647 }
2648 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2649 locations->SetInAt(2, Location::RequiresRegister());
2650 }
2651 locations->SetOut(Location::SameAsFirstInput());
2652}
2653
2654void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2655 LocationSummary* locations = select->GetLocations();
2656 Mips64Label false_target;
2657 GenerateTestAndBranch(select,
2658 /* condition_input_index */ 2,
2659 /* true_target */ nullptr,
2660 &false_target);
2661 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2662 __ Bind(&false_target);
2663}
2664
David Srbecky0cf44932015-12-09 14:09:59 +00002665void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2666 new (GetGraph()->GetArena()) LocationSummary(info);
2667}
2668
David Srbeckyd28f4a02016-03-14 17:14:24 +00002669void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2670 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002671}
2672
2673void CodeGeneratorMIPS64::GenerateNop() {
2674 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002675}
2676
Alexey Frunze4dda3372015-06-01 18:31:49 -07002677void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2678 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2679 LocationSummary* locations =
2680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2681 locations->SetInAt(0, Location::RequiresRegister());
2682 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2683 locations->SetOut(Location::RequiresFpuRegister());
2684 } else {
2685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2686 }
2687}
2688
2689void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2690 const FieldInfo& field_info) {
2691 Primitive::Type type = field_info.GetFieldType();
2692 LocationSummary* locations = instruction->GetLocations();
2693 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2694 LoadOperandType load_type = kLoadUnsignedByte;
2695 switch (type) {
2696 case Primitive::kPrimBoolean:
2697 load_type = kLoadUnsignedByte;
2698 break;
2699 case Primitive::kPrimByte:
2700 load_type = kLoadSignedByte;
2701 break;
2702 case Primitive::kPrimShort:
2703 load_type = kLoadSignedHalfword;
2704 break;
2705 case Primitive::kPrimChar:
2706 load_type = kLoadUnsignedHalfword;
2707 break;
2708 case Primitive::kPrimInt:
2709 case Primitive::kPrimFloat:
2710 load_type = kLoadWord;
2711 break;
2712 case Primitive::kPrimLong:
2713 case Primitive::kPrimDouble:
2714 load_type = kLoadDoubleword;
2715 break;
2716 case Primitive::kPrimNot:
2717 load_type = kLoadUnsignedWord;
2718 break;
2719 case Primitive::kPrimVoid:
2720 LOG(FATAL) << "Unreachable type " << type;
2721 UNREACHABLE();
2722 }
2723 if (!Primitive::IsFloatingPointType(type)) {
2724 DCHECK(locations->Out().IsRegister());
2725 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2726 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2727 } else {
2728 DCHECK(locations->Out().IsFpuRegister());
2729 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2730 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2731 }
2732
2733 codegen_->MaybeRecordImplicitNullCheck(instruction);
2734 // TODO: memory barrier?
2735}
2736
2737void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2738 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2739 LocationSummary* locations =
2740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2741 locations->SetInAt(0, Location::RequiresRegister());
2742 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2743 locations->SetInAt(1, Location::RequiresFpuRegister());
2744 } else {
2745 locations->SetInAt(1, Location::RequiresRegister());
2746 }
2747}
2748
2749void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002750 const FieldInfo& field_info,
2751 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002752 Primitive::Type type = field_info.GetFieldType();
2753 LocationSummary* locations = instruction->GetLocations();
2754 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2755 StoreOperandType store_type = kStoreByte;
2756 switch (type) {
2757 case Primitive::kPrimBoolean:
2758 case Primitive::kPrimByte:
2759 store_type = kStoreByte;
2760 break;
2761 case Primitive::kPrimShort:
2762 case Primitive::kPrimChar:
2763 store_type = kStoreHalfword;
2764 break;
2765 case Primitive::kPrimInt:
2766 case Primitive::kPrimFloat:
2767 case Primitive::kPrimNot:
2768 store_type = kStoreWord;
2769 break;
2770 case Primitive::kPrimLong:
2771 case Primitive::kPrimDouble:
2772 store_type = kStoreDoubleword;
2773 break;
2774 case Primitive::kPrimVoid:
2775 LOG(FATAL) << "Unreachable type " << type;
2776 UNREACHABLE();
2777 }
2778 if (!Primitive::IsFloatingPointType(type)) {
2779 DCHECK(locations->InAt(1).IsRegister());
2780 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2781 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2782 } else {
2783 DCHECK(locations->InAt(1).IsFpuRegister());
2784 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2785 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2786 }
2787
2788 codegen_->MaybeRecordImplicitNullCheck(instruction);
2789 // TODO: memory barriers?
2790 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2791 DCHECK(locations->InAt(1).IsRegister());
2792 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002793 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002794 }
2795}
2796
2797void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2798 HandleFieldGet(instruction, instruction->GetFieldInfo());
2799}
2800
2801void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2802 HandleFieldGet(instruction, instruction->GetFieldInfo());
2803}
2804
2805void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2806 HandleFieldSet(instruction, instruction->GetFieldInfo());
2807}
2808
2809void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002810 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002811}
2812
2813void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2814 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002815 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002816 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2817 locations->SetInAt(0, Location::RequiresRegister());
2818 locations->SetInAt(1, Location::RequiresRegister());
2819 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002820 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002821 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2822}
2823
2824void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2825 LocationSummary* locations = instruction->GetLocations();
2826 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2827 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2828 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2829
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002830 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002831
2832 // Return 0 if `obj` is null.
2833 // TODO: Avoid this check if we know `obj` is not null.
2834 __ Move(out, ZERO);
2835 __ Beqzc(obj, &done);
2836
2837 // Compare the class of `obj` with `cls`.
2838 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002839 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840 // Classes must be equal for the instanceof to succeed.
2841 __ Xor(out, out, cls);
2842 __ Sltiu(out, out, 1);
2843 } else {
2844 // If the classes are not equal, we go into a slow path.
2845 DCHECK(locations->OnlyCallsOnSlowPath());
2846 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002847 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002848 codegen_->AddSlowPath(slow_path);
2849 __ Bnec(out, cls, slow_path->GetEntryLabel());
2850 __ LoadConst32(out, 1);
2851 __ Bind(slow_path->GetExitLabel());
2852 }
2853
2854 __ Bind(&done);
2855}
2856
2857void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2858 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2859 locations->SetOut(Location::ConstantLocation(constant));
2860}
2861
2862void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2863 // Will be generated at use site.
2864}
2865
2866void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2868 locations->SetOut(Location::ConstantLocation(constant));
2869}
2870
2871void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2872 // Will be generated at use site.
2873}
2874
Calin Juravle175dc732015-08-25 15:42:32 +01002875void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2876 // The trampoline uses the same calling convention as dex calling conventions,
2877 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2878 // the method_idx.
2879 HandleInvoke(invoke);
2880}
2881
2882void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2883 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2884}
2885
Alexey Frunze4dda3372015-06-01 18:31:49 -07002886void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2887 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2888 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2889}
2890
2891void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2892 HandleInvoke(invoke);
2893 // The register T0 is required to be used for the hidden argument in
2894 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2895 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2896}
2897
2898void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2899 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2900 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002901 Location receiver = invoke->GetLocations()->InAt(0);
2902 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002903 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002904
2905 // Set the hidden argument.
2906 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2907 invoke->GetDexMethodIndex());
2908
2909 // temp = object->GetClass();
2910 if (receiver.IsStackSlot()) {
2911 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2912 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2913 } else {
2914 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2915 }
2916 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002917 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2918 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2919 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002920 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002921 // temp = temp->GetImtEntryAt(method_offset);
2922 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2923 // T9 = temp->GetEntryPoint();
2924 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2925 // T9();
2926 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002927 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002928 DCHECK(!codegen_->IsLeafMethod());
2929 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2930}
2931
2932void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002933 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2934 if (intrinsic.TryDispatch(invoke)) {
2935 return;
2936 }
2937
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938 HandleInvoke(invoke);
2939}
2940
2941void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002942 // Explicit clinit checks triggered by static invokes must have been pruned by
2943 // art::PrepareForRegisterAllocation.
2944 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002945
Chris Larsen3039e382015-08-26 07:54:08 -07002946 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2947 if (intrinsic.TryDispatch(invoke)) {
2948 return;
2949 }
2950
Alexey Frunze4dda3372015-06-01 18:31:49 -07002951 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952}
2953
Chris Larsen3039e382015-08-26 07:54:08 -07002954static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002955 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002956 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2957 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002958 return true;
2959 }
2960 return false;
2961}
2962
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002963HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2964 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2965 // TODO: Implement other kinds.
2966 return HLoadString::LoadKind::kDexCacheViaMethod;
2967}
2968
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002969HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
2970 HLoadClass::LoadKind desired_class_load_kind) {
2971 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
2972 // TODO: Implement other kinds.
2973 return HLoadClass::LoadKind::kDexCacheViaMethod;
2974}
2975
Vladimir Markodc151b22015-10-15 18:02:30 +01002976HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2977 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01002978 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01002979 switch (desired_dispatch_info.method_load_kind) {
2980 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2981 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2982 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2983 return HInvokeStaticOrDirect::DispatchInfo {
2984 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2985 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2986 0u,
2987 0u
2988 };
2989 default:
2990 break;
2991 }
2992 switch (desired_dispatch_info.code_ptr_location) {
2993 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2994 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2995 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
2996 return HInvokeStaticOrDirect::DispatchInfo {
2997 desired_dispatch_info.method_load_kind,
2998 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2999 desired_dispatch_info.method_load_data,
3000 0u
3001 };
3002 default:
3003 return desired_dispatch_info;
3004 }
3005}
3006
Alexey Frunze4dda3372015-06-01 18:31:49 -07003007void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3008 // All registers are assumed to be correctly set up per the calling convention.
3009
Vladimir Marko58155012015-08-19 12:49:41 +00003010 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3011 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003012 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00003013 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003014 uint32_t offset =
3015 GetThreadOffset<kMips64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00003016 __ LoadFromOffset(kLoadDoubleword,
3017 temp.AsRegister<GpuRegister>(),
3018 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003019 offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003020 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01003021 }
Vladimir Marko58155012015-08-19 12:49:41 +00003022 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003023 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003024 break;
3025 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3026 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3027 break;
3028 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003029 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003030 // TODO: Implement these types.
3031 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3032 LOG(FATAL) << "Unsupported";
3033 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003034 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003035 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003036 GpuRegister reg = temp.AsRegister<GpuRegister>();
3037 GpuRegister method_reg;
3038 if (current_method.IsRegister()) {
3039 method_reg = current_method.AsRegister<GpuRegister>();
3040 } else {
3041 // TODO: use the appropriate DCHECK() here if possible.
3042 // DCHECK(invoke->GetLocations()->Intrinsified());
3043 DCHECK(!current_method.IsValid());
3044 method_reg = reg;
3045 __ Ld(reg, SP, kCurrentMethodStackOffset);
3046 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003047
Vladimir Marko58155012015-08-19 12:49:41 +00003048 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003049 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003050 reg,
3051 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003052 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003053 // temp = temp[index_in_cache];
3054 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3055 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003056 __ LoadFromOffset(kLoadDoubleword,
3057 reg,
3058 reg,
3059 CodeGenerator::GetCachePointerOffset(index_in_cache));
3060 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003061 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003062 }
3063
Vladimir Marko58155012015-08-19 12:49:41 +00003064 switch (invoke->GetCodePtrLocation()) {
3065 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003066 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003067 break;
3068 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3069 // LR = invoke->GetDirectCodePtr();
3070 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3071 // LR()
3072 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003073 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003074 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003075 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003076 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3077 // TODO: Implement these types.
3078 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3079 LOG(FATAL) << "Unsupported";
3080 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003081 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3082 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3083 __ LoadFromOffset(kLoadDoubleword,
3084 T9,
3085 callee_method.AsRegister<GpuRegister>(),
3086 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003087 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003088 // T9()
3089 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003090 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003091 break;
3092 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003093 DCHECK(!IsLeafMethod());
3094}
3095
3096void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003097 // Explicit clinit checks triggered by static invokes must have been pruned by
3098 // art::PrepareForRegisterAllocation.
3099 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003100
3101 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3102 return;
3103 }
3104
3105 LocationSummary* locations = invoke->GetLocations();
3106 codegen_->GenerateStaticOrDirectCall(invoke,
3107 locations->HasTemps()
3108 ? locations->GetTemp(0)
3109 : Location::NoLocation());
3110 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3111}
3112
Alexey Frunze53afca12015-11-05 16:34:23 -08003113void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003114 // Use the calling convention instead of the location of the receiver, as
3115 // intrinsics may have put the receiver in a different register. In the intrinsics
3116 // slow path, the arguments have been moved to the right place, so here we are
3117 // guaranteed that the receiver is the first register of the calling convention.
3118 InvokeDexCallingConvention calling_convention;
3119 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3120
Alexey Frunze53afca12015-11-05 16:34:23 -08003121 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3123 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3124 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003125 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003126
3127 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003128 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003129 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003130 // temp = temp->GetMethodAt(method_offset);
3131 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3132 // T9 = temp->GetEntryPoint();
3133 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3134 // T9();
3135 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003136 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003137}
3138
3139void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3140 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3141 return;
3142 }
3143
3144 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003145 DCHECK(!codegen_->IsLeafMethod());
3146 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3147}
3148
3149void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003150 InvokeRuntimeCallingConvention calling_convention;
3151 CodeGenerator::CreateLoadClassLocationSummary(
3152 cls,
3153 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003154 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003155}
3156
3157void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3158 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003159 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08003160 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003161 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003162 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003163 return;
3164 }
3165
3166 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3167 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3168 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003169 DCHECK(!cls->CanCallRuntime());
3170 DCHECK(!cls->MustGenerateClinitCheck());
3171 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3172 ArtMethod::DeclaringClassOffset().Int32Value());
3173 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003174 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3175 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003176 __ LoadFromOffset(
Andreas Gampea5b09a62016-11-17 15:21:22 -08003177 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_));
Vladimir Marko05792b92015-08-03 11:56:49 +01003178 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003179 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3180 DCHECK(cls->CanCallRuntime());
3181 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3182 cls,
3183 cls,
3184 cls->GetDexPc(),
3185 cls->MustGenerateClinitCheck());
3186 codegen_->AddSlowPath(slow_path);
3187 if (!cls->IsInDexCache()) {
3188 __ Beqzc(out, slow_path->GetEntryLabel());
3189 }
3190 if (cls->MustGenerateClinitCheck()) {
3191 GenerateClassInitializationCheck(slow_path, out);
3192 } else {
3193 __ Bind(slow_path->GetExitLabel());
3194 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003195 }
3196 }
3197}
3198
David Brazdilcb1c0552015-08-04 16:22:25 +01003199static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003200 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003201}
3202
Alexey Frunze4dda3372015-06-01 18:31:49 -07003203void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3204 LocationSummary* locations =
3205 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3206 locations->SetOut(Location::RequiresRegister());
3207}
3208
3209void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3210 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003211 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3212}
3213
3214void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3215 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3216}
3217
3218void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3219 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003220}
3221
Alexey Frunze4dda3372015-06-01 18:31:49 -07003222void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003223 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3224 ? LocationSummary::kCallOnSlowPath
3225 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003226 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003227 locations->SetInAt(0, Location::RequiresRegister());
3228 locations->SetOut(Location::RequiresRegister());
3229}
3230
3231void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003232 // TODO: Re-add the compiler code to do string dex cache lookup again.
3233 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3234 codegen_->AddSlowPath(slow_path);
3235 __ Bc(slow_path->GetEntryLabel());
3236 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003237}
3238
Alexey Frunze4dda3372015-06-01 18:31:49 -07003239void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3241 locations->SetOut(Location::ConstantLocation(constant));
3242}
3243
3244void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3245 // Will be generated at use site.
3246}
3247
3248void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3249 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003251 InvokeRuntimeCallingConvention calling_convention;
3252 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3253}
3254
3255void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003256 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003257 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003258 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003259 if (instruction->IsEnter()) {
3260 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3261 } else {
3262 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003264}
3265
3266void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3267 LocationSummary* locations =
3268 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3269 switch (mul->GetResultType()) {
3270 case Primitive::kPrimInt:
3271 case Primitive::kPrimLong:
3272 locations->SetInAt(0, Location::RequiresRegister());
3273 locations->SetInAt(1, Location::RequiresRegister());
3274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3275 break;
3276
3277 case Primitive::kPrimFloat:
3278 case Primitive::kPrimDouble:
3279 locations->SetInAt(0, Location::RequiresFpuRegister());
3280 locations->SetInAt(1, Location::RequiresFpuRegister());
3281 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3282 break;
3283
3284 default:
3285 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3286 }
3287}
3288
3289void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3290 Primitive::Type type = instruction->GetType();
3291 LocationSummary* locations = instruction->GetLocations();
3292
3293 switch (type) {
3294 case Primitive::kPrimInt:
3295 case Primitive::kPrimLong: {
3296 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3297 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3298 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3299 if (type == Primitive::kPrimInt)
3300 __ MulR6(dst, lhs, rhs);
3301 else
3302 __ Dmul(dst, lhs, rhs);
3303 break;
3304 }
3305 case Primitive::kPrimFloat:
3306 case Primitive::kPrimDouble: {
3307 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3308 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3309 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3310 if (type == Primitive::kPrimFloat)
3311 __ MulS(dst, lhs, rhs);
3312 else
3313 __ MulD(dst, lhs, rhs);
3314 break;
3315 }
3316 default:
3317 LOG(FATAL) << "Unexpected mul type " << type;
3318 }
3319}
3320
3321void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3322 LocationSummary* locations =
3323 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3324 switch (neg->GetResultType()) {
3325 case Primitive::kPrimInt:
3326 case Primitive::kPrimLong:
3327 locations->SetInAt(0, Location::RequiresRegister());
3328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3329 break;
3330
3331 case Primitive::kPrimFloat:
3332 case Primitive::kPrimDouble:
3333 locations->SetInAt(0, Location::RequiresFpuRegister());
3334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3335 break;
3336
3337 default:
3338 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3339 }
3340}
3341
3342void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3343 Primitive::Type type = instruction->GetType();
3344 LocationSummary* locations = instruction->GetLocations();
3345
3346 switch (type) {
3347 case Primitive::kPrimInt:
3348 case Primitive::kPrimLong: {
3349 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3350 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3351 if (type == Primitive::kPrimInt)
3352 __ Subu(dst, ZERO, src);
3353 else
3354 __ Dsubu(dst, ZERO, src);
3355 break;
3356 }
3357 case Primitive::kPrimFloat:
3358 case Primitive::kPrimDouble: {
3359 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3360 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3361 if (type == Primitive::kPrimFloat)
3362 __ NegS(dst, src);
3363 else
3364 __ NegD(dst, src);
3365 break;
3366 }
3367 default:
3368 LOG(FATAL) << "Unexpected neg type " << type;
3369 }
3370}
3371
3372void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3373 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003375 InvokeRuntimeCallingConvention calling_convention;
3376 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3377 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3378 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3379 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3380}
3381
3382void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3383 LocationSummary* locations = instruction->GetLocations();
3384 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08003385 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(),
3386 instruction->GetTypeIndex().index_);
Serban Constantinescufc734082016-07-19 17:18:07 +01003387 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003388 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3389}
3390
3391void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3392 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003393 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003394 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003395 if (instruction->IsStringAlloc()) {
3396 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3397 } else {
3398 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3399 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3400 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003401 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3402}
3403
3404void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003405 if (instruction->IsStringAlloc()) {
3406 // String is allocated through StringFactory. Call NewEmptyString entry point.
3407 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003408 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003409 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003410 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3411 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3412 __ Jalr(T9);
3413 __ Nop();
3414 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3415 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003416 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003417 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3418 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003419}
3420
3421void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3423 locations->SetInAt(0, Location::RequiresRegister());
3424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3425}
3426
3427void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3428 Primitive::Type type = instruction->GetType();
3429 LocationSummary* locations = instruction->GetLocations();
3430
3431 switch (type) {
3432 case Primitive::kPrimInt:
3433 case Primitive::kPrimLong: {
3434 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3435 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3436 __ Nor(dst, src, ZERO);
3437 break;
3438 }
3439
3440 default:
3441 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3442 }
3443}
3444
3445void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3447 locations->SetInAt(0, Location::RequiresRegister());
3448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3449}
3450
3451void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3452 LocationSummary* locations = instruction->GetLocations();
3453 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3454 locations->InAt(0).AsRegister<GpuRegister>(),
3455 1);
3456}
3457
3458void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003459 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
3460 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003461}
3462
Calin Juravle2ae48182016-03-16 14:05:09 +00003463void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3464 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003465 return;
3466 }
3467 Location obj = instruction->GetLocations()->InAt(0);
3468
3469 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003470 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003471}
3472
Calin Juravle2ae48182016-03-16 14:05:09 +00003473void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003474 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003475 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476
3477 Location obj = instruction->GetLocations()->InAt(0);
3478
3479 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3480}
3481
3482void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003483 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003484}
3485
3486void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3487 HandleBinaryOp(instruction);
3488}
3489
3490void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3491 HandleBinaryOp(instruction);
3492}
3493
3494void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3495 LOG(FATAL) << "Unreachable";
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3499 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3500}
3501
3502void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3504 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3505 if (location.IsStackSlot()) {
3506 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3507 } else if (location.IsDoubleStackSlot()) {
3508 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3509 }
3510 locations->SetOut(location);
3511}
3512
3513void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3514 ATTRIBUTE_UNUSED) {
3515 // Nothing to do, the parameter is already at its location.
3516}
3517
3518void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3519 LocationSummary* locations =
3520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3521 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3522}
3523
3524void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3525 ATTRIBUTE_UNUSED) {
3526 // Nothing to do, the method is already at its location.
3527}
3528
3529void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003531 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003532 locations->SetInAt(i, Location::Any());
3533 }
3534 locations->SetOut(Location::Any());
3535}
3536
3537void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3538 LOG(FATAL) << "Unreachable";
3539}
3540
3541void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3542 Primitive::Type type = rem->GetResultType();
3543 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003544 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3545 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3547
3548 switch (type) {
3549 case Primitive::kPrimInt:
3550 case Primitive::kPrimLong:
3551 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003552 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3554 break;
3555
3556 case Primitive::kPrimFloat:
3557 case Primitive::kPrimDouble: {
3558 InvokeRuntimeCallingConvention calling_convention;
3559 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3560 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3561 locations->SetOut(calling_convention.GetReturnLocation(type));
3562 break;
3563 }
3564
3565 default:
3566 LOG(FATAL) << "Unexpected rem type " << type;
3567 }
3568}
3569
3570void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3571 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003572
3573 switch (type) {
3574 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003575 case Primitive::kPrimLong:
3576 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003577 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578
3579 case Primitive::kPrimFloat:
3580 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003581 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3582 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003583 if (type == Primitive::kPrimFloat) {
3584 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3585 } else {
3586 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3587 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 break;
3589 }
3590 default:
3591 LOG(FATAL) << "Unexpected rem type " << type;
3592 }
3593}
3594
3595void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3596 memory_barrier->SetLocations(nullptr);
3597}
3598
3599void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3600 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3601}
3602
3603void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3605 Primitive::Type return_type = ret->InputAt(0)->GetType();
3606 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3607}
3608
3609void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3610 codegen_->GenerateFrameExit();
3611}
3612
3613void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3614 ret->SetLocations(nullptr);
3615}
3616
3617void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3618 codegen_->GenerateFrameExit();
3619}
3620
Alexey Frunze92d90602015-12-18 18:16:36 -08003621void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3622 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003623}
3624
Alexey Frunze92d90602015-12-18 18:16:36 -08003625void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3626 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003627}
3628
Alexey Frunze4dda3372015-06-01 18:31:49 -07003629void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3630 HandleShift(shl);
3631}
3632
3633void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3634 HandleShift(shl);
3635}
3636
3637void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3638 HandleShift(shr);
3639}
3640
3641void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3642 HandleShift(shr);
3643}
3644
Alexey Frunze4dda3372015-06-01 18:31:49 -07003645void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3646 HandleBinaryOp(instruction);
3647}
3648
3649void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3650 HandleBinaryOp(instruction);
3651}
3652
3653void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3654 HandleFieldGet(instruction, instruction->GetFieldInfo());
3655}
3656
3657void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3658 HandleFieldGet(instruction, instruction->GetFieldInfo());
3659}
3660
3661void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3662 HandleFieldSet(instruction, instruction->GetFieldInfo());
3663}
3664
3665void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003666 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003667}
3668
Calin Juravlee460d1d2015-09-29 04:52:17 +01003669void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3670 HUnresolvedInstanceFieldGet* instruction) {
3671 FieldAccessCallingConventionMIPS64 calling_convention;
3672 codegen_->CreateUnresolvedFieldLocationSummary(
3673 instruction, instruction->GetFieldType(), calling_convention);
3674}
3675
3676void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3677 HUnresolvedInstanceFieldGet* instruction) {
3678 FieldAccessCallingConventionMIPS64 calling_convention;
3679 codegen_->GenerateUnresolvedFieldAccess(instruction,
3680 instruction->GetFieldType(),
3681 instruction->GetFieldIndex(),
3682 instruction->GetDexPc(),
3683 calling_convention);
3684}
3685
3686void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3687 HUnresolvedInstanceFieldSet* instruction) {
3688 FieldAccessCallingConventionMIPS64 calling_convention;
3689 codegen_->CreateUnresolvedFieldLocationSummary(
3690 instruction, instruction->GetFieldType(), calling_convention);
3691}
3692
3693void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3694 HUnresolvedInstanceFieldSet* instruction) {
3695 FieldAccessCallingConventionMIPS64 calling_convention;
3696 codegen_->GenerateUnresolvedFieldAccess(instruction,
3697 instruction->GetFieldType(),
3698 instruction->GetFieldIndex(),
3699 instruction->GetDexPc(),
3700 calling_convention);
3701}
3702
3703void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3704 HUnresolvedStaticFieldGet* instruction) {
3705 FieldAccessCallingConventionMIPS64 calling_convention;
3706 codegen_->CreateUnresolvedFieldLocationSummary(
3707 instruction, instruction->GetFieldType(), calling_convention);
3708}
3709
3710void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3711 HUnresolvedStaticFieldGet* instruction) {
3712 FieldAccessCallingConventionMIPS64 calling_convention;
3713 codegen_->GenerateUnresolvedFieldAccess(instruction,
3714 instruction->GetFieldType(),
3715 instruction->GetFieldIndex(),
3716 instruction->GetDexPc(),
3717 calling_convention);
3718}
3719
3720void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3721 HUnresolvedStaticFieldSet* instruction) {
3722 FieldAccessCallingConventionMIPS64 calling_convention;
3723 codegen_->CreateUnresolvedFieldLocationSummary(
3724 instruction, instruction->GetFieldType(), calling_convention);
3725}
3726
3727void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3728 HUnresolvedStaticFieldSet* instruction) {
3729 FieldAccessCallingConventionMIPS64 calling_convention;
3730 codegen_->GenerateUnresolvedFieldAccess(instruction,
3731 instruction->GetFieldType(),
3732 instruction->GetFieldIndex(),
3733 instruction->GetDexPc(),
3734 calling_convention);
3735}
3736
Alexey Frunze4dda3372015-06-01 18:31:49 -07003737void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003738 LocationSummary* locations =
3739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003741}
3742
3743void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3744 HBasicBlock* block = instruction->GetBlock();
3745 if (block->GetLoopInformation() != nullptr) {
3746 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3747 // The back edge will generate the suspend check.
3748 return;
3749 }
3750 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3751 // The goto will generate the suspend check.
3752 return;
3753 }
3754 GenerateSuspendCheck(instruction, nullptr);
3755}
3756
Alexey Frunze4dda3372015-06-01 18:31:49 -07003757void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3758 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003760 InvokeRuntimeCallingConvention calling_convention;
3761 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3762}
3763
3764void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003765 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003766 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3767}
3768
3769void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3770 Primitive::Type input_type = conversion->GetInputType();
3771 Primitive::Type result_type = conversion->GetResultType();
3772 DCHECK_NE(input_type, result_type);
3773
3774 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3775 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3776 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3777 }
3778
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3780
3781 if (Primitive::IsFloatingPointType(input_type)) {
3782 locations->SetInAt(0, Location::RequiresFpuRegister());
3783 } else {
3784 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003785 }
3786
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003787 if (Primitive::IsFloatingPointType(result_type)) {
3788 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003789 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003790 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003791 }
3792}
3793
3794void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3795 LocationSummary* locations = conversion->GetLocations();
3796 Primitive::Type result_type = conversion->GetResultType();
3797 Primitive::Type input_type = conversion->GetInputType();
3798
3799 DCHECK_NE(input_type, result_type);
3800
3801 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3802 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3803 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3804
3805 switch (result_type) {
3806 case Primitive::kPrimChar:
3807 __ Andi(dst, src, 0xFFFF);
3808 break;
3809 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003810 if (input_type == Primitive::kPrimLong) {
3811 // Type conversion from long to types narrower than int is a result of code
3812 // transformations. To avoid unpredictable results for SEB and SEH, we first
3813 // need to sign-extend the low 32-bit value into bits 32 through 63.
3814 __ Sll(dst, src, 0);
3815 __ Seb(dst, dst);
3816 } else {
3817 __ Seb(dst, src);
3818 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003819 break;
3820 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003821 if (input_type == Primitive::kPrimLong) {
3822 // Type conversion from long to types narrower than int is a result of code
3823 // transformations. To avoid unpredictable results for SEB and SEH, we first
3824 // need to sign-extend the low 32-bit value into bits 32 through 63.
3825 __ Sll(dst, src, 0);
3826 __ Seh(dst, dst);
3827 } else {
3828 __ Seh(dst, src);
3829 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003830 break;
3831 case Primitive::kPrimInt:
3832 case Primitive::kPrimLong:
3833 // Sign-extend 32-bit int into bits 32 through 63 for
3834 // int-to-long and long-to-int conversions
3835 __ Sll(dst, src, 0);
3836 break;
3837
3838 default:
3839 LOG(FATAL) << "Unexpected type conversion from " << input_type
3840 << " to " << result_type;
3841 }
3842 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003843 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3844 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3845 if (input_type == Primitive::kPrimLong) {
3846 __ Dmtc1(src, FTMP);
3847 if (result_type == Primitive::kPrimFloat) {
3848 __ Cvtsl(dst, FTMP);
3849 } else {
3850 __ Cvtdl(dst, FTMP);
3851 }
3852 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003853 __ Mtc1(src, FTMP);
3854 if (result_type == Primitive::kPrimFloat) {
3855 __ Cvtsw(dst, FTMP);
3856 } else {
3857 __ Cvtdw(dst, FTMP);
3858 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003859 }
3860 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3861 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003862 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3863 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3864 Mips64Label truncate;
3865 Mips64Label done;
3866
3867 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3868 // value when the input is either a NaN or is outside of the range of the output type
3869 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3870 // the same result.
3871 //
3872 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3873 // value of the output type if the input is outside of the range after the truncation or
3874 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3875 // results. This matches the desired float/double-to-int/long conversion exactly.
3876 //
3877 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3878 //
3879 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3880 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3881 // even though it must be NAN2008=1 on R6.
3882 //
3883 // The code takes care of the different behaviors by first comparing the input to the
3884 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3885 // If the input is greater than or equal to the minimum, it procedes to the truncate
3886 // instruction, which will handle such an input the same way irrespective of NAN2008.
3887 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3888 // in order to return either zero or the minimum value.
3889 //
3890 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3891 // truncate instruction for MIPS64R6.
3892 if (input_type == Primitive::kPrimFloat) {
3893 uint32_t min_val = (result_type == Primitive::kPrimLong)
3894 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3895 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3896 __ LoadConst32(TMP, min_val);
3897 __ Mtc1(TMP, FTMP);
3898 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003899 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003900 uint64_t min_val = (result_type == Primitive::kPrimLong)
3901 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3902 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3903 __ LoadConst64(TMP, min_val);
3904 __ Dmtc1(TMP, FTMP);
3905 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003906 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003907
3908 __ Bc1nez(FTMP, &truncate);
3909
3910 if (input_type == Primitive::kPrimFloat) {
3911 __ CmpEqS(FTMP, src, src);
3912 } else {
3913 __ CmpEqD(FTMP, src, src);
3914 }
3915 if (result_type == Primitive::kPrimLong) {
3916 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3917 } else {
3918 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3919 }
3920 __ Mfc1(TMP, FTMP);
3921 __ And(dst, dst, TMP);
3922
3923 __ Bc(&done);
3924
3925 __ Bind(&truncate);
3926
3927 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003928 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003929 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003930 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003931 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003932 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003933 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003934 } else {
3935 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003936 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003937 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003938 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003939 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003940 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003941 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003942
3943 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003944 } else if (Primitive::IsFloatingPointType(result_type) &&
3945 Primitive::IsFloatingPointType(input_type)) {
3946 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3947 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3948 if (result_type == Primitive::kPrimFloat) {
3949 __ Cvtsd(dst, src);
3950 } else {
3951 __ Cvtds(dst, src);
3952 }
3953 } else {
3954 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3955 << " to " << result_type;
3956 }
3957}
3958
3959void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3960 HandleShift(ushr);
3961}
3962
3963void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3964 HandleShift(ushr);
3965}
3966
3967void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3968 HandleBinaryOp(instruction);
3969}
3970
3971void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3972 HandleBinaryOp(instruction);
3973}
3974
3975void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3976 // Nothing to do, this should be removed during prepare for register allocator.
3977 LOG(FATAL) << "Unreachable";
3978}
3979
3980void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3981 // Nothing to do, this should be removed during prepare for register allocator.
3982 LOG(FATAL) << "Unreachable";
3983}
3984
3985void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003986 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003987}
3988
3989void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003990 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991}
3992
3993void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003994 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995}
3996
3997void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003998 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003999}
4000
4001void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004002 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003}
4004
4005void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004006 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007}
4008
4009void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004010 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004011}
4012
4013void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004014 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015}
4016
4017void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004018 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004022 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004023}
4024
4025void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004026 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004027}
4028
4029void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004030 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031}
4032
Aart Bike9f37602015-10-09 11:15:55 -07004033void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004034 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004035}
4036
4037void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004038 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004039}
4040
4041void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004042 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004043}
4044
4045void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004046 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004047}
4048
4049void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004050 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004051}
4052
4053void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004054 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004055}
4056
4057void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004058 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004059}
4060
4061void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004062 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004063}
4064
Mark Mendellfe57faa2015-09-18 09:26:15 -04004065// Simple implementation of packed switch - generate cascaded compare/jumps.
4066void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4069 locations->SetInAt(0, Location::RequiresRegister());
4070}
4071
4072void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4073 int32_t lower_bound = switch_instr->GetStartValue();
4074 int32_t num_entries = switch_instr->GetNumEntries();
4075 LocationSummary* locations = switch_instr->GetLocations();
4076 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4077 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4078
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004079 // Create a set of compare/jumps.
4080 GpuRegister temp_reg = TMP;
4081 if (IsInt<16>(-lower_bound)) {
4082 __ Addiu(temp_reg, value_reg, -lower_bound);
4083 } else {
4084 __ LoadConst32(AT, -lower_bound);
4085 __ Addu(temp_reg, value_reg, AT);
4086 }
4087 // Jump to default if index is negative
4088 // Note: We don't check the case that index is positive while value < lower_bound, because in
4089 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4090 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4091
Mark Mendellfe57faa2015-09-18 09:26:15 -04004092 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004093 // Jump to successors[0] if value == lower_bound.
4094 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4095 int32_t last_index = 0;
4096 for (; num_entries - last_index > 2; last_index += 2) {
4097 __ Addiu(temp_reg, temp_reg, -2);
4098 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4099 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4100 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4101 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4102 }
4103 if (num_entries - last_index == 2) {
4104 // The last missing case_value.
4105 __ Addiu(temp_reg, temp_reg, -1);
4106 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004107 }
4108
4109 // And the default for any other value.
4110 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004111 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004112 }
4113}
4114
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004115void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4116 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4117}
4118
4119void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4120 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4121}
4122
Alexey Frunze4dda3372015-06-01 18:31:49 -07004123} // namespace mips64
4124} // namespace art