blob: 4d8752320673012c57e39aa05bc9d2c328d0e6ee [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());
David Brazdil77a48ae2015-09-15 12:34:04 +0000153 if (instruction_->CanThrowIntoCatchBlock()) {
154 // Live registers will be restored in the catch block if caught.
155 SaveLiveRegisters(codegen, instruction_->GetLocations());
156 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100157 mips64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700158 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
159 }
160
Alexandre Rames8158f282015-08-07 10:26:17 +0100161 bool IsFatal() const OVERRIDE { return true; }
162
Roland Levillain46648892015-06-19 16:07:18 +0100163 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
164
Alexey Frunze4dda3372015-06-01 18:31:49 -0700165 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700166 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
167};
168
169class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
170 public:
171 LoadClassSlowPathMIPS64(HLoadClass* cls,
172 HInstruction* at,
173 uint32_t dex_pc,
174 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000175 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700176 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
177 }
178
179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
180 LocationSummary* locations = at_->GetLocations();
181 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
182
183 __ Bind(GetEntryLabel());
184 SaveLiveRegisters(codegen, locations);
185
186 InvokeRuntimeCallingConvention calling_convention;
187 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +0100188 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
189 : kQuickInitializeType;
190 mips64_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700191 if (do_clinit_) {
192 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
193 } else {
194 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
195 }
196
197 // Move the class to the desired location.
198 Location out = locations->Out();
199 if (out.IsValid()) {
200 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
201 Primitive::Type type = at_->GetType();
202 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
203 }
204
205 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700206 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700207 }
208
Roland Levillain46648892015-06-19 16:07:18 +0100209 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
210
Alexey Frunze4dda3372015-06-01 18:31:49 -0700211 private:
212 // The class this slow path will load.
213 HLoadClass* const cls_;
214
215 // The instruction where this slow path is happening.
216 // (Might be the load class or an initialization check).
217 HInstruction* const at_;
218
219 // The dex PC of `at_`.
220 const uint32_t dex_pc_;
221
222 // Whether to initialize the class.
223 const bool do_clinit_;
224
225 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
226};
227
228class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
229 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000230 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700231
232 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
233 LocationSummary* locations = instruction_->GetLocations();
234 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
235 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
236
237 __ Bind(GetEntryLabel());
238 SaveLiveRegisters(codegen, locations);
239
240 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000241 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
242 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufc734082016-07-19 17:18:07 +0100243 mips64_codegen->InvokeRuntime(kQuickResolveString,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700244 instruction_,
245 instruction_->GetDexPc(),
246 this);
247 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
248 Primitive::Type type = instruction_->GetType();
249 mips64_codegen->MoveLocation(locations->Out(),
250 calling_convention.GetReturnLocation(type),
251 type);
252
253 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700254 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700255 }
256
Roland Levillain46648892015-06-19 16:07:18 +0100257 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
258
Alexey Frunze4dda3372015-06-01 18:31:49 -0700259 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
261};
262
263class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
264 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000265 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700266
267 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
268 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
269 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000270 if (instruction_->CanThrowIntoCatchBlock()) {
271 // Live registers will be restored in the catch block if caught.
272 SaveLiveRegisters(codegen, instruction_->GetLocations());
273 }
Serban Constantinescufc734082016-07-19 17:18:07 +0100274 mips64_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275 instruction_,
276 instruction_->GetDexPc(),
277 this);
278 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
279 }
280
Alexandre Rames8158f282015-08-07 10:26:17 +0100281 bool IsFatal() const OVERRIDE { return true; }
282
Roland Levillain46648892015-06-19 16:07:18 +0100283 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
284
Alexey Frunze4dda3372015-06-01 18:31:49 -0700285 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
287};
288
289class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
290 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100291 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000292 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293
294 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
295 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
296 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100297 mips64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700298 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700299 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700300 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700301 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700302 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700303 }
304 }
305
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700306 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307 DCHECK(successor_ == nullptr);
308 return &return_label_;
309 }
310
Roland Levillain46648892015-06-19 16:07:18 +0100311 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
312
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700314 // If not null, the block to branch to after the suspend check.
315 HBasicBlock* const successor_;
316
317 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700318 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319
320 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
321};
322
323class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
324 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700326
327 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
328 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700331 DCHECK(instruction_->IsCheckCast()
332 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
333 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
334
335 __ Bind(GetEntryLabel());
336 SaveLiveRegisters(codegen, locations);
337
338 // We're moving two locations to locations that could overlap, so we need a parallel
339 // move resolver.
340 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
343 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100344 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700345 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
346 Primitive::kPrimNot);
347
348 if (instruction_->IsInstanceOf()) {
Serban Constantinescufc734082016-07-19 17:18:07 +0100349 mips64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000350 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700351 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700352 Primitive::Type ret_type = instruction_->GetType();
353 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
354 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700355 } else {
356 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufc734082016-07-19 17:18:07 +0100357 mips64_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
359 }
360
361 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700362 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700363 }
364
Roland Levillain46648892015-06-19 16:07:18 +0100365 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
366
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700368 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
369};
370
371class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
372 public:
Aart Bik42249c32016-01-07 15:33:50 -0800373 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000374 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700375
376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800377 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700378 __ Bind(GetEntryLabel());
Serban Constantinescufc734082016-07-19 17:18:07 +0100379 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000380 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700381 }
382
Roland Levillain46648892015-06-19 16:07:18 +0100383 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
384
Alexey Frunze4dda3372015-06-01 18:31:49 -0700385 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
387};
388
389CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
390 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100391 const CompilerOptions& compiler_options,
392 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393 : CodeGenerator(graph,
394 kNumberOfGpuRegisters,
395 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000396 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700397 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
398 arraysize(kCoreCalleeSaves)),
399 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
400 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100401 compiler_options,
402 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100403 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700404 location_builder_(graph, this),
405 instruction_visitor_(graph, this),
406 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100407 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700408 isa_features_(isa_features) {
409 // Save RA (containing the return address) to mimic Quick.
410 AddAllocatedRegister(Location::RegisterLocation(RA));
411}
412
413#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100414// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
415#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700416#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417
418void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700419 // Ensure that we fix up branches.
420 __ FinalizeCode();
421
422 // Adjust native pc offsets in stack maps.
423 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
424 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
425 uint32_t new_position = __ GetAdjustedPosition(old_position);
426 DCHECK_GE(new_position, old_position);
427 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
428 }
429
430 // Adjust pc offsets for the disassembly information.
431 if (disasm_info_ != nullptr) {
432 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
433 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
434 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
435 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
436 it.second.start = __ GetAdjustedPosition(it.second.start);
437 it.second.end = __ GetAdjustedPosition(it.second.end);
438 }
439 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
440 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
441 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
442 }
443 }
444
Alexey Frunze4dda3372015-06-01 18:31:49 -0700445 CodeGenerator::Finalize(allocator);
446}
447
448Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
449 return codegen_->GetAssembler();
450}
451
452void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100453 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700454 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
455}
456
457void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100458 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700459 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
460}
461
462void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
463 // Pop reg
464 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200465 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700466}
467
468void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
469 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200470 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700471 __ Sd(GpuRegister(reg), SP, 0);
472}
473
474void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
475 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
476 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
477 // Allocate a scratch register other than TMP, if available.
478 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
479 // automatically unspilled when the scratch scope object is destroyed).
480 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
481 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200482 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 __ LoadFromOffset(load_type,
484 GpuRegister(ensure_scratch.GetRegister()),
485 SP,
486 index1 + stack_offset);
487 __ LoadFromOffset(load_type,
488 TMP,
489 SP,
490 index2 + stack_offset);
491 __ StoreToOffset(store_type,
492 GpuRegister(ensure_scratch.GetRegister()),
493 SP,
494 index2 + stack_offset);
495 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
496}
497
498static dwarf::Reg DWARFReg(GpuRegister reg) {
499 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
500}
501
David Srbeckyba702002016-02-01 18:15:29 +0000502static dwarf::Reg DWARFReg(FpuRegister reg) {
503 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
504}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700505
506void CodeGeneratorMIPS64::GenerateFrameEntry() {
507 __ Bind(&frame_entry_label_);
508
509 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
510
511 if (do_overflow_check) {
512 __ LoadFromOffset(kLoadWord,
513 ZERO,
514 SP,
515 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
516 RecordPcInfo(nullptr, 0);
517 }
518
519 // TODO: anything related to T9/GP/GOT/PIC/.so's?
520
521 if (HasEmptyFrame()) {
522 return;
523 }
524
525 // Make sure the frame size isn't unreasonably large. Per the various APIs
526 // it looks like it should always be less than 2GB in size, which allows
527 // us using 32-bit signed offsets from the stack pointer.
528 if (GetFrameSize() > 0x7FFFFFFF)
529 LOG(FATAL) << "Stack frame larger than 2GB";
530
531 // Spill callee-saved registers.
532 // Note that their cumulative size is small and they can be indexed using
533 // 16-bit offsets.
534
535 // TODO: increment/decrement SP in one step instead of two or remove this comment.
536
537 uint32_t ofs = FrameEntrySpillSize();
538 __ IncreaseFrameSize(ofs);
539
540 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
541 GpuRegister reg = kCoreCalleeSaves[i];
542 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200543 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700544 __ Sd(reg, SP, ofs);
545 __ cfi().RelOffset(DWARFReg(reg), ofs);
546 }
547 }
548
549 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
550 FpuRegister reg = kFpuCalleeSaves[i];
551 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200552 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700553 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000554 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700555 }
556 }
557
558 // Allocate the rest of the frame and store the current method pointer
559 // at its end.
560
561 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
562
563 static_assert(IsInt<16>(kCurrentMethodStackOffset),
564 "kCurrentMethodStackOffset must fit into int16_t");
565 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
566}
567
568void CodeGeneratorMIPS64::GenerateFrameExit() {
569 __ cfi().RememberState();
570
571 // TODO: anything related to T9/GP/GOT/PIC/.so's?
572
573 if (!HasEmptyFrame()) {
574 // Deallocate the rest of the frame.
575
576 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
577
578 // Restore callee-saved registers.
579 // Note that their cumulative size is small and they can be indexed using
580 // 16-bit offsets.
581
582 // TODO: increment/decrement SP in one step instead of two or remove this comment.
583
584 uint32_t ofs = 0;
585
586 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
587 FpuRegister reg = kFpuCalleeSaves[i];
588 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
589 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200590 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000591 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700592 }
593 }
594
595 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
596 GpuRegister reg = kCoreCalleeSaves[i];
597 if (allocated_registers_.ContainsCoreRegister(reg)) {
598 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200599 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700600 __ cfi().Restore(DWARFReg(reg));
601 }
602 }
603
604 DCHECK_EQ(ofs, FrameEntrySpillSize());
605 __ DecreaseFrameSize(ofs);
606 }
607
608 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700609 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700610
611 __ cfi().RestoreState();
612 __ cfi().DefCFAOffset(GetFrameSize());
613}
614
615void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
616 __ Bind(GetLabelOf(block));
617}
618
619void CodeGeneratorMIPS64::MoveLocation(Location destination,
620 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100621 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700622 if (source.Equals(destination)) {
623 return;
624 }
625
626 // A valid move can always be inferred from the destination and source
627 // locations. When moving from and to a register, the argument type can be
628 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100629 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700630 DCHECK_EQ(unspecified_type, false);
631
632 if (destination.IsRegister() || destination.IsFpuRegister()) {
633 if (unspecified_type) {
634 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
635 if (source.IsStackSlot() ||
636 (src_cst != nullptr && (src_cst->IsIntConstant()
637 || src_cst->IsFloatConstant()
638 || src_cst->IsNullConstant()))) {
639 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100640 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700641 } else {
642 // If the source is a double stack slot or a 64bit constant, a 64bit
643 // type is appropriate. Else the source is a register, and since the
644 // type has not been specified, we chose a 64bit type to force a 64bit
645 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100646 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700647 }
648 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100649 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
650 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700651 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
652 // Move to GPR/FPR from stack
653 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100654 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700655 __ LoadFpuFromOffset(load_type,
656 destination.AsFpuRegister<FpuRegister>(),
657 SP,
658 source.GetStackIndex());
659 } else {
660 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
661 __ LoadFromOffset(load_type,
662 destination.AsRegister<GpuRegister>(),
663 SP,
664 source.GetStackIndex());
665 }
666 } else if (source.IsConstant()) {
667 // Move to GPR/FPR from constant
668 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100669 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700670 gpr = destination.AsRegister<GpuRegister>();
671 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100672 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700673 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
674 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
675 gpr = ZERO;
676 } else {
677 __ LoadConst32(gpr, value);
678 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700679 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700680 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
681 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
682 gpr = ZERO;
683 } else {
684 __ LoadConst64(gpr, value);
685 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700686 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100687 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700688 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100689 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700690 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
691 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100692 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700693 if (destination.IsRegister()) {
694 // Move to GPR from GPR
695 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
696 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100697 DCHECK(destination.IsFpuRegister());
698 if (Primitive::Is64BitType(dst_type)) {
699 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
700 } else {
701 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
702 }
703 }
704 } else if (source.IsFpuRegister()) {
705 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700706 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100707 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
709 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100710 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700711 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
712 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100713 } else {
714 DCHECK(destination.IsRegister());
715 if (Primitive::Is64BitType(dst_type)) {
716 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
717 } else {
718 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
719 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700720 }
721 }
722 } else { // The destination is not a register. It must be a stack slot.
723 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
724 if (source.IsRegister() || source.IsFpuRegister()) {
725 if (unspecified_type) {
726 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100727 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700728 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100729 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700730 }
731 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100732 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
733 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700734 // Move to stack from GPR/FPR
735 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
736 if (source.IsRegister()) {
737 __ StoreToOffset(store_type,
738 source.AsRegister<GpuRegister>(),
739 SP,
740 destination.GetStackIndex());
741 } else {
742 __ StoreFpuToOffset(store_type,
743 source.AsFpuRegister<FpuRegister>(),
744 SP,
745 destination.GetStackIndex());
746 }
747 } else if (source.IsConstant()) {
748 // Move to stack from constant
749 HConstant* src_cst = source.GetConstant();
750 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700751 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700753 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
754 if (value != 0) {
755 gpr = TMP;
756 __ LoadConst32(gpr, value);
757 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700758 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700759 DCHECK(destination.IsDoubleStackSlot());
760 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
761 if (value != 0) {
762 gpr = TMP;
763 __ LoadConst64(gpr, value);
764 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700765 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700766 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700767 } else {
768 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
769 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
770 // Move to stack from stack
771 if (destination.IsStackSlot()) {
772 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
773 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
774 } else {
775 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
776 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
777 }
778 }
779 }
780}
781
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700782void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700783 DCHECK(!loc1.IsConstant());
784 DCHECK(!loc2.IsConstant());
785
786 if (loc1.Equals(loc2)) {
787 return;
788 }
789
790 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
791 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
792 bool is_fp_reg1 = loc1.IsFpuRegister();
793 bool is_fp_reg2 = loc2.IsFpuRegister();
794
795 if (loc2.IsRegister() && loc1.IsRegister()) {
796 // Swap 2 GPRs
797 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
798 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
799 __ Move(TMP, r2);
800 __ Move(r2, r1);
801 __ Move(r1, TMP);
802 } else if (is_fp_reg2 && is_fp_reg1) {
803 // Swap 2 FPRs
804 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
805 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700806 if (type == Primitive::kPrimFloat) {
807 __ MovS(FTMP, r1);
808 __ MovS(r1, r2);
809 __ MovS(r2, FTMP);
810 } else {
811 DCHECK_EQ(type, Primitive::kPrimDouble);
812 __ MovD(FTMP, r1);
813 __ MovD(r1, r2);
814 __ MovD(r2, FTMP);
815 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700816 } else if (is_slot1 != is_slot2) {
817 // Swap GPR/FPR and stack slot
818 Location reg_loc = is_slot1 ? loc2 : loc1;
819 Location mem_loc = is_slot1 ? loc1 : loc2;
820 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
821 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
822 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
823 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
824 if (reg_loc.IsFpuRegister()) {
825 __ StoreFpuToOffset(store_type,
826 reg_loc.AsFpuRegister<FpuRegister>(),
827 SP,
828 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700829 if (mem_loc.IsStackSlot()) {
830 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
831 } else {
832 DCHECK(mem_loc.IsDoubleStackSlot());
833 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
834 }
835 } else {
836 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
837 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
838 }
839 } else if (is_slot1 && is_slot2) {
840 move_resolver_.Exchange(loc1.GetStackIndex(),
841 loc2.GetStackIndex(),
842 loc1.IsDoubleStackSlot());
843 } else {
844 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
845 }
846}
847
Calin Juravle175dc732015-08-25 15:42:32 +0100848void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
849 DCHECK(location.IsRegister());
850 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
851}
852
Calin Juravlee460d1d2015-09-29 04:52:17 +0100853void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
854 if (location.IsRegister()) {
855 locations->AddTemp(location);
856 } else {
857 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
858 }
859}
860
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100861void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
862 GpuRegister value,
863 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700864 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700865 GpuRegister card = AT;
866 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100867 if (value_can_be_null) {
868 __ Beqzc(value, &done);
869 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700870 __ LoadFromOffset(kLoadDoubleword,
871 card,
872 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700873 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700874 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
875 __ Daddu(temp, card, temp);
876 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100877 if (value_can_be_null) {
878 __ Bind(&done);
879 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700880}
881
David Brazdil58282f42016-01-14 12:45:10 +0000882void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700883 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
884 blocked_core_registers_[ZERO] = true;
885 blocked_core_registers_[K0] = true;
886 blocked_core_registers_[K1] = true;
887 blocked_core_registers_[GP] = true;
888 blocked_core_registers_[SP] = true;
889 blocked_core_registers_[RA] = true;
890
Lazar Trsicd9672662015-09-03 17:33:01 +0200891 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
892 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700893 blocked_core_registers_[AT] = true;
894 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200895 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700896 blocked_fpu_registers_[FTMP] = true;
897
898 // Reserve suspend and thread registers.
899 blocked_core_registers_[S0] = true;
900 blocked_core_registers_[TR] = true;
901
902 // Reserve T9 for function calls
903 blocked_core_registers_[T9] = true;
904
905 // TODO: review; anything else?
906
Goran Jakovljevic782be112016-06-21 12:39:04 +0200907 if (GetGraph()->IsDebuggable()) {
908 // Stubs do not save callee-save floating point registers. If the graph
909 // is debuggable, we need to deal with these registers differently. For
910 // now, just block them.
911 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
912 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
913 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700914 }
915}
916
Alexey Frunze4dda3372015-06-01 18:31:49 -0700917size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
918 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200919 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700920}
921
922size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
923 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200924 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700925}
926
927size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
928 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200929 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700930}
931
932size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
933 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200934 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700935}
936
937void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100938 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700939}
940
941void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100942 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700943}
944
Calin Juravle175dc732015-08-25 15:42:32 +0100945void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946 HInstruction* instruction,
947 uint32_t dex_pc,
948 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100949 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700950 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100951 __ LoadFromOffset(kLoadDoubleword,
952 T9,
953 TR,
954 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700955 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700956 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100957 if (EntrypointRequiresStackMap(entrypoint)) {
958 RecordPcInfo(instruction, dex_pc, slow_path);
959 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700960}
961
962void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
963 GpuRegister class_reg) {
964 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
965 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
966 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
967 // TODO: barrier needed?
968 __ Bind(slow_path->GetExitLabel());
969}
970
971void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
972 __ Sync(0); // only stype 0 is supported
973}
974
975void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
976 HBasicBlock* successor) {
977 SuspendCheckSlowPathMIPS64* slow_path =
978 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
979 codegen_->AddSlowPath(slow_path);
980
981 __ LoadFromOffset(kLoadUnsignedHalfword,
982 TMP,
983 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700984 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700985 if (successor == nullptr) {
986 __ Bnezc(TMP, slow_path->GetEntryLabel());
987 __ Bind(slow_path->GetReturnLabel());
988 } else {
989 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700990 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700991 // slow_path will return to GetLabelOf(successor).
992 }
993}
994
995InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
996 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800997 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700998 assembler_(codegen->GetAssembler()),
999 codegen_(codegen) {}
1000
1001void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1002 DCHECK_EQ(instruction->InputCount(), 2U);
1003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1004 Primitive::Type type = instruction->GetResultType();
1005 switch (type) {
1006 case Primitive::kPrimInt:
1007 case Primitive::kPrimLong: {
1008 locations->SetInAt(0, Location::RequiresRegister());
1009 HInstruction* right = instruction->InputAt(1);
1010 bool can_use_imm = false;
1011 if (right->IsConstant()) {
1012 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1013 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1014 can_use_imm = IsUint<16>(imm);
1015 } else if (instruction->IsAdd()) {
1016 can_use_imm = IsInt<16>(imm);
1017 } else {
1018 DCHECK(instruction->IsSub());
1019 can_use_imm = IsInt<16>(-imm);
1020 }
1021 }
1022 if (can_use_imm)
1023 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1024 else
1025 locations->SetInAt(1, Location::RequiresRegister());
1026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1027 }
1028 break;
1029
1030 case Primitive::kPrimFloat:
1031 case Primitive::kPrimDouble:
1032 locations->SetInAt(0, Location::RequiresFpuRegister());
1033 locations->SetInAt(1, Location::RequiresFpuRegister());
1034 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1035 break;
1036
1037 default:
1038 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1039 }
1040}
1041
1042void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1043 Primitive::Type type = instruction->GetType();
1044 LocationSummary* locations = instruction->GetLocations();
1045
1046 switch (type) {
1047 case Primitive::kPrimInt:
1048 case Primitive::kPrimLong: {
1049 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1050 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1051 Location rhs_location = locations->InAt(1);
1052
1053 GpuRegister rhs_reg = ZERO;
1054 int64_t rhs_imm = 0;
1055 bool use_imm = rhs_location.IsConstant();
1056 if (use_imm) {
1057 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1058 } else {
1059 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1060 }
1061
1062 if (instruction->IsAnd()) {
1063 if (use_imm)
1064 __ Andi(dst, lhs, rhs_imm);
1065 else
1066 __ And(dst, lhs, rhs_reg);
1067 } else if (instruction->IsOr()) {
1068 if (use_imm)
1069 __ Ori(dst, lhs, rhs_imm);
1070 else
1071 __ Or(dst, lhs, rhs_reg);
1072 } else if (instruction->IsXor()) {
1073 if (use_imm)
1074 __ Xori(dst, lhs, rhs_imm);
1075 else
1076 __ Xor(dst, lhs, rhs_reg);
1077 } else if (instruction->IsAdd()) {
1078 if (type == Primitive::kPrimInt) {
1079 if (use_imm)
1080 __ Addiu(dst, lhs, rhs_imm);
1081 else
1082 __ Addu(dst, lhs, rhs_reg);
1083 } else {
1084 if (use_imm)
1085 __ Daddiu(dst, lhs, rhs_imm);
1086 else
1087 __ Daddu(dst, lhs, rhs_reg);
1088 }
1089 } else {
1090 DCHECK(instruction->IsSub());
1091 if (type == Primitive::kPrimInt) {
1092 if (use_imm)
1093 __ Addiu(dst, lhs, -rhs_imm);
1094 else
1095 __ Subu(dst, lhs, rhs_reg);
1096 } else {
1097 if (use_imm)
1098 __ Daddiu(dst, lhs, -rhs_imm);
1099 else
1100 __ Dsubu(dst, lhs, rhs_reg);
1101 }
1102 }
1103 break;
1104 }
1105 case Primitive::kPrimFloat:
1106 case Primitive::kPrimDouble: {
1107 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1108 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1109 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1110 if (instruction->IsAdd()) {
1111 if (type == Primitive::kPrimFloat)
1112 __ AddS(dst, lhs, rhs);
1113 else
1114 __ AddD(dst, lhs, rhs);
1115 } else if (instruction->IsSub()) {
1116 if (type == Primitive::kPrimFloat)
1117 __ SubS(dst, lhs, rhs);
1118 else
1119 __ SubD(dst, lhs, rhs);
1120 } else {
1121 LOG(FATAL) << "Unexpected floating-point binary operation";
1122 }
1123 break;
1124 }
1125 default:
1126 LOG(FATAL) << "Unexpected binary operation type " << type;
1127 }
1128}
1129
1130void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001131 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001132
1133 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1134 Primitive::Type type = instr->GetResultType();
1135 switch (type) {
1136 case Primitive::kPrimInt:
1137 case Primitive::kPrimLong: {
1138 locations->SetInAt(0, Location::RequiresRegister());
1139 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001140 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001141 break;
1142 }
1143 default:
1144 LOG(FATAL) << "Unexpected shift type " << type;
1145 }
1146}
1147
1148void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001149 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001150 LocationSummary* locations = instr->GetLocations();
1151 Primitive::Type type = instr->GetType();
1152
1153 switch (type) {
1154 case Primitive::kPrimInt:
1155 case Primitive::kPrimLong: {
1156 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1157 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1158 Location rhs_location = locations->InAt(1);
1159
1160 GpuRegister rhs_reg = ZERO;
1161 int64_t rhs_imm = 0;
1162 bool use_imm = rhs_location.IsConstant();
1163 if (use_imm) {
1164 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1165 } else {
1166 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1167 }
1168
1169 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001170 uint32_t shift_value = rhs_imm &
1171 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001172
Alexey Frunze92d90602015-12-18 18:16:36 -08001173 if (shift_value == 0) {
1174 if (dst != lhs) {
1175 __ Move(dst, lhs);
1176 }
1177 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001178 if (instr->IsShl()) {
1179 __ Sll(dst, lhs, shift_value);
1180 } else if (instr->IsShr()) {
1181 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001182 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001183 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001184 } else {
1185 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001186 }
1187 } else {
1188 if (shift_value < 32) {
1189 if (instr->IsShl()) {
1190 __ Dsll(dst, lhs, shift_value);
1191 } else if (instr->IsShr()) {
1192 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001193 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001195 } else {
1196 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001197 }
1198 } else {
1199 shift_value -= 32;
1200 if (instr->IsShl()) {
1201 __ Dsll32(dst, lhs, shift_value);
1202 } else if (instr->IsShr()) {
1203 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001204 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001205 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001206 } else {
1207 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001208 }
1209 }
1210 }
1211 } else {
1212 if (type == Primitive::kPrimInt) {
1213 if (instr->IsShl()) {
1214 __ Sllv(dst, lhs, rhs_reg);
1215 } else if (instr->IsShr()) {
1216 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001217 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001218 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001219 } else {
1220 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 }
1222 } else {
1223 if (instr->IsShl()) {
1224 __ Dsllv(dst, lhs, rhs_reg);
1225 } else if (instr->IsShr()) {
1226 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001227 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001229 } else {
1230 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001231 }
1232 }
1233 }
1234 break;
1235 }
1236 default:
1237 LOG(FATAL) << "Unexpected shift operation type " << type;
1238 }
1239}
1240
1241void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1242 HandleBinaryOp(instruction);
1243}
1244
1245void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1246 HandleBinaryOp(instruction);
1247}
1248
1249void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1250 HandleBinaryOp(instruction);
1251}
1252
1253void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1254 HandleBinaryOp(instruction);
1255}
1256
1257void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1258 LocationSummary* locations =
1259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1260 locations->SetInAt(0, Location::RequiresRegister());
1261 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1262 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1263 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1264 } else {
1265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1266 }
1267}
1268
1269void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1270 LocationSummary* locations = instruction->GetLocations();
1271 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1272 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001273 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001274
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001275 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001276 switch (type) {
1277 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001278 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1279 if (index.IsConstant()) {
1280 size_t offset =
1281 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1282 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1283 } else {
1284 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1285 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1286 }
1287 break;
1288 }
1289
1290 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001291 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1292 if (index.IsConstant()) {
1293 size_t offset =
1294 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1295 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1296 } else {
1297 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1298 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1299 }
1300 break;
1301 }
1302
1303 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001304 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1305 if (index.IsConstant()) {
1306 size_t offset =
1307 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1308 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1309 } else {
1310 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1311 __ Daddu(TMP, obj, TMP);
1312 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1313 }
1314 break;
1315 }
1316
1317 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001318 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1319 if (index.IsConstant()) {
1320 size_t offset =
1321 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1322 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1323 } else {
1324 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1325 __ Daddu(TMP, obj, TMP);
1326 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1327 }
1328 break;
1329 }
1330
1331 case Primitive::kPrimInt:
1332 case Primitive::kPrimNot: {
1333 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001334 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1335 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1336 if (index.IsConstant()) {
1337 size_t offset =
1338 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1339 __ LoadFromOffset(load_type, out, obj, offset);
1340 } else {
1341 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1342 __ Daddu(TMP, obj, TMP);
1343 __ LoadFromOffset(load_type, out, TMP, data_offset);
1344 }
1345 break;
1346 }
1347
1348 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001349 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1350 if (index.IsConstant()) {
1351 size_t offset =
1352 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1353 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1354 } else {
1355 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1356 __ Daddu(TMP, obj, TMP);
1357 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1358 }
1359 break;
1360 }
1361
1362 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001363 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1364 if (index.IsConstant()) {
1365 size_t offset =
1366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1367 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1368 } else {
1369 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1370 __ Daddu(TMP, obj, TMP);
1371 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1372 }
1373 break;
1374 }
1375
1376 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001377 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1378 if (index.IsConstant()) {
1379 size_t offset =
1380 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1381 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1382 } else {
1383 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1384 __ Daddu(TMP, obj, TMP);
1385 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1386 }
1387 break;
1388 }
1389
1390 case Primitive::kPrimVoid:
1391 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1392 UNREACHABLE();
1393 }
1394 codegen_->MaybeRecordImplicitNullCheck(instruction);
1395}
1396
1397void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1399 locations->SetInAt(0, Location::RequiresRegister());
1400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1401}
1402
1403void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1404 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001405 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001406 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1407 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1408 __ LoadFromOffset(kLoadWord, out, obj, offset);
1409 codegen_->MaybeRecordImplicitNullCheck(instruction);
1410}
1411
1412void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001413 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1415 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001416 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001417 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001418 InvokeRuntimeCallingConvention calling_convention;
1419 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1420 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1421 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1422 } else {
1423 locations->SetInAt(0, Location::RequiresRegister());
1424 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1425 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1426 locations->SetInAt(2, Location::RequiresFpuRegister());
1427 } else {
1428 locations->SetInAt(2, Location::RequiresRegister());
1429 }
1430 }
1431}
1432
1433void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1434 LocationSummary* locations = instruction->GetLocations();
1435 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1436 Location index = locations->InAt(1);
1437 Primitive::Type value_type = instruction->GetComponentType();
1438 bool needs_runtime_call = locations->WillCall();
1439 bool needs_write_barrier =
1440 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1441
1442 switch (value_type) {
1443 case Primitive::kPrimBoolean:
1444 case Primitive::kPrimByte: {
1445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1446 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1447 if (index.IsConstant()) {
1448 size_t offset =
1449 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1450 __ StoreToOffset(kStoreByte, value, obj, offset);
1451 } else {
1452 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1453 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1454 }
1455 break;
1456 }
1457
1458 case Primitive::kPrimShort:
1459 case Primitive::kPrimChar: {
1460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1461 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1462 if (index.IsConstant()) {
1463 size_t offset =
1464 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1465 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1466 } else {
1467 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1468 __ Daddu(TMP, obj, TMP);
1469 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1470 }
1471 break;
1472 }
1473
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimNot: {
1476 if (!needs_runtime_call) {
1477 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1478 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1479 if (index.IsConstant()) {
1480 size_t offset =
1481 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1482 __ StoreToOffset(kStoreWord, value, obj, offset);
1483 } else {
1484 DCHECK(index.IsRegister()) << index;
1485 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1486 __ Daddu(TMP, obj, TMP);
1487 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1488 }
1489 codegen_->MaybeRecordImplicitNullCheck(instruction);
1490 if (needs_write_barrier) {
1491 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001492 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001493 }
1494 } else {
1495 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001496 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001497 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001498 }
1499 break;
1500 }
1501
1502 case Primitive::kPrimLong: {
1503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1504 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1505 if (index.IsConstant()) {
1506 size_t offset =
1507 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1508 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1509 } else {
1510 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1511 __ Daddu(TMP, obj, TMP);
1512 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1513 }
1514 break;
1515 }
1516
1517 case Primitive::kPrimFloat: {
1518 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1519 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1520 DCHECK(locations->InAt(2).IsFpuRegister());
1521 if (index.IsConstant()) {
1522 size_t offset =
1523 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1524 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1525 } else {
1526 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1527 __ Daddu(TMP, obj, TMP);
1528 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1529 }
1530 break;
1531 }
1532
1533 case Primitive::kPrimDouble: {
1534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1535 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1536 DCHECK(locations->InAt(2).IsFpuRegister());
1537 if (index.IsConstant()) {
1538 size_t offset =
1539 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1540 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1541 } else {
1542 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1543 __ Daddu(TMP, obj, TMP);
1544 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1545 }
1546 break;
1547 }
1548
1549 case Primitive::kPrimVoid:
1550 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1551 UNREACHABLE();
1552 }
1553
1554 // Ints and objects are handled in the switch.
1555 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1556 codegen_->MaybeRecordImplicitNullCheck(instruction);
1557 }
1558}
1559
1560void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001561 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1562 ? LocationSummary::kCallOnSlowPath
1563 : LocationSummary::kNoCall;
1564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001565 locations->SetInAt(0, Location::RequiresRegister());
1566 locations->SetInAt(1, Location::RequiresRegister());
1567 if (instruction->HasUses()) {
1568 locations->SetOut(Location::SameAsFirstInput());
1569 }
1570}
1571
1572void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1573 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001574 BoundsCheckSlowPathMIPS64* slow_path =
1575 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001576 codegen_->AddSlowPath(slow_path);
1577
1578 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1579 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1580
1581 // length is limited by the maximum positive signed 32-bit integer.
1582 // Unsigned comparison of length and index checks for index < 0
1583 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001584 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001585}
1586
1587void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1588 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1589 instruction,
1590 LocationSummary::kCallOnSlowPath);
1591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001593 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001594 locations->AddTemp(Location::RequiresRegister());
1595}
1596
1597void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1598 LocationSummary* locations = instruction->GetLocations();
1599 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1600 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1601 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1602
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001603 SlowPathCodeMIPS64* slow_path =
1604 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001605 codegen_->AddSlowPath(slow_path);
1606
1607 // TODO: avoid this check if we know obj is not null.
1608 __ Beqzc(obj, slow_path->GetExitLabel());
1609 // Compare the class of `obj` with `cls`.
1610 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1611 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1612 __ Bind(slow_path->GetExitLabel());
1613}
1614
1615void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1616 LocationSummary* locations =
1617 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1618 locations->SetInAt(0, Location::RequiresRegister());
1619 if (check->HasUses()) {
1620 locations->SetOut(Location::SameAsFirstInput());
1621 }
1622}
1623
1624void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1625 // We assume the class is not null.
1626 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1627 check->GetLoadClass(),
1628 check,
1629 check->GetDexPc(),
1630 true);
1631 codegen_->AddSlowPath(slow_path);
1632 GenerateClassInitializationCheck(slow_path,
1633 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1634}
1635
1636void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1637 Primitive::Type in_type = compare->InputAt(0)->GetType();
1638
Alexey Frunze299a9392015-12-08 16:08:02 -08001639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001640
1641 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001642 case Primitive::kPrimBoolean:
1643 case Primitive::kPrimByte:
1644 case Primitive::kPrimShort:
1645 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001646 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001647 case Primitive::kPrimLong:
1648 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001649 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1651 break;
1652
1653 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001654 case Primitive::kPrimDouble:
1655 locations->SetInAt(0, Location::RequiresFpuRegister());
1656 locations->SetInAt(1, Location::RequiresFpuRegister());
1657 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001658 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001659
1660 default:
1661 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1662 }
1663}
1664
1665void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1666 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001667 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001668 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1669
1670 // 0 if: left == right
1671 // 1 if: left > right
1672 // -1 if: left < right
1673 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001674 case Primitive::kPrimBoolean:
1675 case Primitive::kPrimByte:
1676 case Primitive::kPrimShort:
1677 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001678 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001679 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001680 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001681 Location rhs_location = locations->InAt(1);
1682 bool use_imm = rhs_location.IsConstant();
1683 GpuRegister rhs = ZERO;
1684 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001685 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001686 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1687 if (value != 0) {
1688 rhs = AT;
1689 __ LoadConst64(rhs, value);
1690 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001691 } else {
1692 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1693 if (value != 0) {
1694 rhs = AT;
1695 __ LoadConst32(rhs, value);
1696 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001697 }
1698 } else {
1699 rhs = rhs_location.AsRegister<GpuRegister>();
1700 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001702 __ Slt(res, rhs, lhs);
1703 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001704 break;
1705 }
1706
Alexey Frunze299a9392015-12-08 16:08:02 -08001707 case Primitive::kPrimFloat: {
1708 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1709 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1710 Mips64Label done;
1711 __ CmpEqS(FTMP, lhs, rhs);
1712 __ LoadConst32(res, 0);
1713 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001714 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001715 __ CmpLtS(FTMP, lhs, rhs);
1716 __ LoadConst32(res, -1);
1717 __ Bc1nez(FTMP, &done);
1718 __ LoadConst32(res, 1);
1719 } else {
1720 __ CmpLtS(FTMP, rhs, lhs);
1721 __ LoadConst32(res, 1);
1722 __ Bc1nez(FTMP, &done);
1723 __ LoadConst32(res, -1);
1724 }
1725 __ Bind(&done);
1726 break;
1727 }
1728
Alexey Frunze4dda3372015-06-01 18:31:49 -07001729 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001730 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1731 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1732 Mips64Label done;
1733 __ CmpEqD(FTMP, lhs, rhs);
1734 __ LoadConst32(res, 0);
1735 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001736 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001737 __ CmpLtD(FTMP, lhs, rhs);
1738 __ LoadConst32(res, -1);
1739 __ Bc1nez(FTMP, &done);
1740 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001741 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001742 __ CmpLtD(FTMP, rhs, lhs);
1743 __ LoadConst32(res, 1);
1744 __ Bc1nez(FTMP, &done);
1745 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001746 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001747 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001748 break;
1749 }
1750
1751 default:
1752 LOG(FATAL) << "Unimplemented compare type " << in_type;
1753 }
1754}
1755
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001758 switch (instruction->InputAt(0)->GetType()) {
1759 default:
1760 case Primitive::kPrimLong:
1761 locations->SetInAt(0, Location::RequiresRegister());
1762 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1763 break;
1764
1765 case Primitive::kPrimFloat:
1766 case Primitive::kPrimDouble:
1767 locations->SetInAt(0, Location::RequiresFpuRegister());
1768 locations->SetInAt(1, Location::RequiresFpuRegister());
1769 break;
1770 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001771 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1773 }
1774}
1775
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001777 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001778 return;
1779 }
1780
Alexey Frunze299a9392015-12-08 16:08:02 -08001781 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001782 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001784 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001785
Alexey Frunze299a9392015-12-08 16:08:02 -08001786 switch (type) {
1787 default:
1788 // Integer case.
1789 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1790 return;
1791 case Primitive::kPrimLong:
1792 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1793 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001794
Alexey Frunze299a9392015-12-08 16:08:02 -08001795 case Primitive::kPrimFloat:
1796 case Primitive::kPrimDouble:
1797 // TODO: don't use branches.
1798 GenerateFpCompareAndBranch(instruction->GetCondition(),
1799 instruction->IsGtBias(),
1800 type,
1801 locations,
1802 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001803 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001805
1806 // Convert the branches into the result.
1807 Mips64Label done;
1808
1809 // False case: result = 0.
1810 __ LoadConst32(dst, 0);
1811 __ Bc(&done);
1812
1813 // True case: result = 1.
1814 __ Bind(&true_label);
1815 __ LoadConst32(dst, 1);
1816 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001817}
1818
Alexey Frunzec857c742015-09-23 15:12:39 -07001819void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1820 DCHECK(instruction->IsDiv() || instruction->IsRem());
1821 Primitive::Type type = instruction->GetResultType();
1822
1823 LocationSummary* locations = instruction->GetLocations();
1824 Location second = locations->InAt(1);
1825 DCHECK(second.IsConstant());
1826
1827 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1828 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1829 int64_t imm = Int64FromConstant(second.GetConstant());
1830 DCHECK(imm == 1 || imm == -1);
1831
1832 if (instruction->IsRem()) {
1833 __ Move(out, ZERO);
1834 } else {
1835 if (imm == -1) {
1836 if (type == Primitive::kPrimInt) {
1837 __ Subu(out, ZERO, dividend);
1838 } else {
1839 DCHECK_EQ(type, Primitive::kPrimLong);
1840 __ Dsubu(out, ZERO, dividend);
1841 }
1842 } else if (out != dividend) {
1843 __ Move(out, dividend);
1844 }
1845 }
1846}
1847
1848void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1849 DCHECK(instruction->IsDiv() || instruction->IsRem());
1850 Primitive::Type type = instruction->GetResultType();
1851
1852 LocationSummary* locations = instruction->GetLocations();
1853 Location second = locations->InAt(1);
1854 DCHECK(second.IsConstant());
1855
1856 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1857 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1858 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001859 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001860 int ctz_imm = CTZ(abs_imm);
1861
1862 if (instruction->IsDiv()) {
1863 if (type == Primitive::kPrimInt) {
1864 if (ctz_imm == 1) {
1865 // Fast path for division by +/-2, which is very common.
1866 __ Srl(TMP, dividend, 31);
1867 } else {
1868 __ Sra(TMP, dividend, 31);
1869 __ Srl(TMP, TMP, 32 - ctz_imm);
1870 }
1871 __ Addu(out, dividend, TMP);
1872 __ Sra(out, out, ctz_imm);
1873 if (imm < 0) {
1874 __ Subu(out, ZERO, out);
1875 }
1876 } else {
1877 DCHECK_EQ(type, Primitive::kPrimLong);
1878 if (ctz_imm == 1) {
1879 // Fast path for division by +/-2, which is very common.
1880 __ Dsrl32(TMP, dividend, 31);
1881 } else {
1882 __ Dsra32(TMP, dividend, 31);
1883 if (ctz_imm > 32) {
1884 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1885 } else {
1886 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1887 }
1888 }
1889 __ Daddu(out, dividend, TMP);
1890 if (ctz_imm < 32) {
1891 __ Dsra(out, out, ctz_imm);
1892 } else {
1893 __ Dsra32(out, out, ctz_imm - 32);
1894 }
1895 if (imm < 0) {
1896 __ Dsubu(out, ZERO, out);
1897 }
1898 }
1899 } else {
1900 if (type == Primitive::kPrimInt) {
1901 if (ctz_imm == 1) {
1902 // Fast path for modulo +/-2, which is very common.
1903 __ Sra(TMP, dividend, 31);
1904 __ Subu(out, dividend, TMP);
1905 __ Andi(out, out, 1);
1906 __ Addu(out, out, TMP);
1907 } else {
1908 __ Sra(TMP, dividend, 31);
1909 __ Srl(TMP, TMP, 32 - ctz_imm);
1910 __ Addu(out, dividend, TMP);
1911 if (IsUint<16>(abs_imm - 1)) {
1912 __ Andi(out, out, abs_imm - 1);
1913 } else {
1914 __ Sll(out, out, 32 - ctz_imm);
1915 __ Srl(out, out, 32 - ctz_imm);
1916 }
1917 __ Subu(out, out, TMP);
1918 }
1919 } else {
1920 DCHECK_EQ(type, Primitive::kPrimLong);
1921 if (ctz_imm == 1) {
1922 // Fast path for modulo +/-2, which is very common.
1923 __ Dsra32(TMP, dividend, 31);
1924 __ Dsubu(out, dividend, TMP);
1925 __ Andi(out, out, 1);
1926 __ Daddu(out, out, TMP);
1927 } else {
1928 __ Dsra32(TMP, dividend, 31);
1929 if (ctz_imm > 32) {
1930 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1931 } else {
1932 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1933 }
1934 __ Daddu(out, dividend, TMP);
1935 if (IsUint<16>(abs_imm - 1)) {
1936 __ Andi(out, out, abs_imm - 1);
1937 } else {
1938 if (ctz_imm > 32) {
1939 __ Dsll(out, out, 64 - ctz_imm);
1940 __ Dsrl(out, out, 64 - ctz_imm);
1941 } else {
1942 __ Dsll32(out, out, 32 - ctz_imm);
1943 __ Dsrl32(out, out, 32 - ctz_imm);
1944 }
1945 }
1946 __ Dsubu(out, out, TMP);
1947 }
1948 }
1949 }
1950}
1951
1952void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1953 DCHECK(instruction->IsDiv() || instruction->IsRem());
1954
1955 LocationSummary* locations = instruction->GetLocations();
1956 Location second = locations->InAt(1);
1957 DCHECK(second.IsConstant());
1958
1959 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1960 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1961 int64_t imm = Int64FromConstant(second.GetConstant());
1962
1963 Primitive::Type type = instruction->GetResultType();
1964 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1965
1966 int64_t magic;
1967 int shift;
1968 CalculateMagicAndShiftForDivRem(imm,
1969 (type == Primitive::kPrimLong),
1970 &magic,
1971 &shift);
1972
1973 if (type == Primitive::kPrimInt) {
1974 __ LoadConst32(TMP, magic);
1975 __ MuhR6(TMP, dividend, TMP);
1976
1977 if (imm > 0 && magic < 0) {
1978 __ Addu(TMP, TMP, dividend);
1979 } else if (imm < 0 && magic > 0) {
1980 __ Subu(TMP, TMP, dividend);
1981 }
1982
1983 if (shift != 0) {
1984 __ Sra(TMP, TMP, shift);
1985 }
1986
1987 if (instruction->IsDiv()) {
1988 __ Sra(out, TMP, 31);
1989 __ Subu(out, TMP, out);
1990 } else {
1991 __ Sra(AT, TMP, 31);
1992 __ Subu(AT, TMP, AT);
1993 __ LoadConst32(TMP, imm);
1994 __ MulR6(TMP, AT, TMP);
1995 __ Subu(out, dividend, TMP);
1996 }
1997 } else {
1998 __ LoadConst64(TMP, magic);
1999 __ Dmuh(TMP, dividend, TMP);
2000
2001 if (imm > 0 && magic < 0) {
2002 __ Daddu(TMP, TMP, dividend);
2003 } else if (imm < 0 && magic > 0) {
2004 __ Dsubu(TMP, TMP, dividend);
2005 }
2006
2007 if (shift >= 32) {
2008 __ Dsra32(TMP, TMP, shift - 32);
2009 } else if (shift > 0) {
2010 __ Dsra(TMP, TMP, shift);
2011 }
2012
2013 if (instruction->IsDiv()) {
2014 __ Dsra32(out, TMP, 31);
2015 __ Dsubu(out, TMP, out);
2016 } else {
2017 __ Dsra32(AT, TMP, 31);
2018 __ Dsubu(AT, TMP, AT);
2019 __ LoadConst64(TMP, imm);
2020 __ Dmul(TMP, AT, TMP);
2021 __ Dsubu(out, dividend, TMP);
2022 }
2023 }
2024}
2025
2026void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2027 DCHECK(instruction->IsDiv() || instruction->IsRem());
2028 Primitive::Type type = instruction->GetResultType();
2029 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2030
2031 LocationSummary* locations = instruction->GetLocations();
2032 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2033 Location second = locations->InAt(1);
2034
2035 if (second.IsConstant()) {
2036 int64_t imm = Int64FromConstant(second.GetConstant());
2037 if (imm == 0) {
2038 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2039 } else if (imm == 1 || imm == -1) {
2040 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002041 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002042 DivRemByPowerOfTwo(instruction);
2043 } else {
2044 DCHECK(imm <= -2 || imm >= 2);
2045 GenerateDivRemWithAnyConstant(instruction);
2046 }
2047 } else {
2048 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2049 GpuRegister divisor = second.AsRegister<GpuRegister>();
2050 if (instruction->IsDiv()) {
2051 if (type == Primitive::kPrimInt)
2052 __ DivR6(out, dividend, divisor);
2053 else
2054 __ Ddiv(out, dividend, divisor);
2055 } else {
2056 if (type == Primitive::kPrimInt)
2057 __ ModR6(out, dividend, divisor);
2058 else
2059 __ Dmod(out, dividend, divisor);
2060 }
2061 }
2062}
2063
Alexey Frunze4dda3372015-06-01 18:31:49 -07002064void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2067 switch (div->GetResultType()) {
2068 case Primitive::kPrimInt:
2069 case Primitive::kPrimLong:
2070 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002071 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002072 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2073 break;
2074
2075 case Primitive::kPrimFloat:
2076 case Primitive::kPrimDouble:
2077 locations->SetInAt(0, Location::RequiresFpuRegister());
2078 locations->SetInAt(1, Location::RequiresFpuRegister());
2079 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2080 break;
2081
2082 default:
2083 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2084 }
2085}
2086
2087void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2088 Primitive::Type type = instruction->GetType();
2089 LocationSummary* locations = instruction->GetLocations();
2090
2091 switch (type) {
2092 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002093 case Primitive::kPrimLong:
2094 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002095 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 case Primitive::kPrimFloat:
2097 case Primitive::kPrimDouble: {
2098 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2099 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2100 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2101 if (type == Primitive::kPrimFloat)
2102 __ DivS(dst, lhs, rhs);
2103 else
2104 __ DivD(dst, lhs, rhs);
2105 break;
2106 }
2107 default:
2108 LOG(FATAL) << "Unexpected div type " << type;
2109 }
2110}
2111
2112void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002113 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2114 ? LocationSummary::kCallOnSlowPath
2115 : LocationSummary::kNoCall;
2116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002117 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2118 if (instruction->HasUses()) {
2119 locations->SetOut(Location::SameAsFirstInput());
2120 }
2121}
2122
2123void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2124 SlowPathCodeMIPS64* slow_path =
2125 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2126 codegen_->AddSlowPath(slow_path);
2127 Location value = instruction->GetLocations()->InAt(0);
2128
2129 Primitive::Type type = instruction->GetType();
2130
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002131 if (!Primitive::IsIntegralType(type)) {
2132 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002133 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002134 }
2135
2136 if (value.IsConstant()) {
2137 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2138 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002139 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 } else {
2141 // A division by a non-null constant is valid. We don't need to perform
2142 // any check, so simply fall through.
2143 }
2144 } else {
2145 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2146 }
2147}
2148
2149void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2150 LocationSummary* locations =
2151 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2152 locations->SetOut(Location::ConstantLocation(constant));
2153}
2154
2155void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2156 // Will be generated at use site.
2157}
2158
2159void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2160 exit->SetLocations(nullptr);
2161}
2162
2163void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2164}
2165
2166void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2169 locations->SetOut(Location::ConstantLocation(constant));
2170}
2171
2172void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2173 // Will be generated at use site.
2174}
2175
David Brazdilfc6a86a2015-06-26 10:33:45 +00002176void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002177 DCHECK(!successor->IsExitBlock());
2178 HBasicBlock* block = got->GetBlock();
2179 HInstruction* previous = got->GetPrevious();
2180 HLoopInformation* info = block->GetLoopInformation();
2181
2182 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2183 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2184 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2185 return;
2186 }
2187 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2188 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2189 }
2190 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002191 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002192 }
2193}
2194
David Brazdilfc6a86a2015-06-26 10:33:45 +00002195void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2196 got->SetLocations(nullptr);
2197}
2198
2199void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2200 HandleGoto(got, got->GetSuccessor());
2201}
2202
2203void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2204 try_boundary->SetLocations(nullptr);
2205}
2206
2207void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2208 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2209 if (!successor->IsExitBlock()) {
2210 HandleGoto(try_boundary, successor);
2211 }
2212}
2213
Alexey Frunze299a9392015-12-08 16:08:02 -08002214void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2215 bool is64bit,
2216 LocationSummary* locations) {
2217 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2218 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2219 Location rhs_location = locations->InAt(1);
2220 GpuRegister rhs_reg = ZERO;
2221 int64_t rhs_imm = 0;
2222 bool use_imm = rhs_location.IsConstant();
2223 if (use_imm) {
2224 if (is64bit) {
2225 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2226 } else {
2227 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2228 }
2229 } else {
2230 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2231 }
2232 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2233
2234 switch (cond) {
2235 case kCondEQ:
2236 case kCondNE:
2237 if (use_imm && IsUint<16>(rhs_imm)) {
2238 __ Xori(dst, lhs, rhs_imm);
2239 } else {
2240 if (use_imm) {
2241 rhs_reg = TMP;
2242 __ LoadConst64(rhs_reg, rhs_imm);
2243 }
2244 __ Xor(dst, lhs, rhs_reg);
2245 }
2246 if (cond == kCondEQ) {
2247 __ Sltiu(dst, dst, 1);
2248 } else {
2249 __ Sltu(dst, ZERO, dst);
2250 }
2251 break;
2252
2253 case kCondLT:
2254 case kCondGE:
2255 if (use_imm && IsInt<16>(rhs_imm)) {
2256 __ Slti(dst, lhs, rhs_imm);
2257 } else {
2258 if (use_imm) {
2259 rhs_reg = TMP;
2260 __ LoadConst64(rhs_reg, rhs_imm);
2261 }
2262 __ Slt(dst, lhs, rhs_reg);
2263 }
2264 if (cond == kCondGE) {
2265 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2266 // only the slt instruction but no sge.
2267 __ Xori(dst, dst, 1);
2268 }
2269 break;
2270
2271 case kCondLE:
2272 case kCondGT:
2273 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2274 // Simulate lhs <= rhs via lhs < rhs + 1.
2275 __ Slti(dst, lhs, rhs_imm_plus_one);
2276 if (cond == kCondGT) {
2277 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2278 // only the slti instruction but no sgti.
2279 __ Xori(dst, dst, 1);
2280 }
2281 } else {
2282 if (use_imm) {
2283 rhs_reg = TMP;
2284 __ LoadConst64(rhs_reg, rhs_imm);
2285 }
2286 __ Slt(dst, rhs_reg, lhs);
2287 if (cond == kCondLE) {
2288 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2289 // only the slt instruction but no sle.
2290 __ Xori(dst, dst, 1);
2291 }
2292 }
2293 break;
2294
2295 case kCondB:
2296 case kCondAE:
2297 if (use_imm && IsInt<16>(rhs_imm)) {
2298 // Sltiu sign-extends its 16-bit immediate operand before
2299 // the comparison and thus lets us compare directly with
2300 // unsigned values in the ranges [0, 0x7fff] and
2301 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2302 __ Sltiu(dst, lhs, rhs_imm);
2303 } else {
2304 if (use_imm) {
2305 rhs_reg = TMP;
2306 __ LoadConst64(rhs_reg, rhs_imm);
2307 }
2308 __ Sltu(dst, lhs, rhs_reg);
2309 }
2310 if (cond == kCondAE) {
2311 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2312 // only the sltu instruction but no sgeu.
2313 __ Xori(dst, dst, 1);
2314 }
2315 break;
2316
2317 case kCondBE:
2318 case kCondA:
2319 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2320 // Simulate lhs <= rhs via lhs < rhs + 1.
2321 // Note that this only works if rhs + 1 does not overflow
2322 // to 0, hence the check above.
2323 // Sltiu sign-extends its 16-bit immediate operand before
2324 // the comparison and thus lets us compare directly with
2325 // unsigned values in the ranges [0, 0x7fff] and
2326 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2327 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2328 if (cond == kCondA) {
2329 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2330 // only the sltiu instruction but no sgtiu.
2331 __ Xori(dst, dst, 1);
2332 }
2333 } else {
2334 if (use_imm) {
2335 rhs_reg = TMP;
2336 __ LoadConst64(rhs_reg, rhs_imm);
2337 }
2338 __ Sltu(dst, rhs_reg, lhs);
2339 if (cond == kCondBE) {
2340 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2341 // only the sltu instruction but no sleu.
2342 __ Xori(dst, dst, 1);
2343 }
2344 }
2345 break;
2346 }
2347}
2348
2349void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2350 bool is64bit,
2351 LocationSummary* locations,
2352 Mips64Label* label) {
2353 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2354 Location rhs_location = locations->InAt(1);
2355 GpuRegister rhs_reg = ZERO;
2356 int64_t rhs_imm = 0;
2357 bool use_imm = rhs_location.IsConstant();
2358 if (use_imm) {
2359 if (is64bit) {
2360 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2361 } else {
2362 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2363 }
2364 } else {
2365 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2366 }
2367
2368 if (use_imm && rhs_imm == 0) {
2369 switch (cond) {
2370 case kCondEQ:
2371 case kCondBE: // <= 0 if zero
2372 __ Beqzc(lhs, label);
2373 break;
2374 case kCondNE:
2375 case kCondA: // > 0 if non-zero
2376 __ Bnezc(lhs, label);
2377 break;
2378 case kCondLT:
2379 __ Bltzc(lhs, label);
2380 break;
2381 case kCondGE:
2382 __ Bgezc(lhs, label);
2383 break;
2384 case kCondLE:
2385 __ Blezc(lhs, label);
2386 break;
2387 case kCondGT:
2388 __ Bgtzc(lhs, label);
2389 break;
2390 case kCondB: // always false
2391 break;
2392 case kCondAE: // always true
2393 __ Bc(label);
2394 break;
2395 }
2396 } else {
2397 if (use_imm) {
2398 rhs_reg = TMP;
2399 __ LoadConst64(rhs_reg, rhs_imm);
2400 }
2401 switch (cond) {
2402 case kCondEQ:
2403 __ Beqc(lhs, rhs_reg, label);
2404 break;
2405 case kCondNE:
2406 __ Bnec(lhs, rhs_reg, label);
2407 break;
2408 case kCondLT:
2409 __ Bltc(lhs, rhs_reg, label);
2410 break;
2411 case kCondGE:
2412 __ Bgec(lhs, rhs_reg, label);
2413 break;
2414 case kCondLE:
2415 __ Bgec(rhs_reg, lhs, label);
2416 break;
2417 case kCondGT:
2418 __ Bltc(rhs_reg, lhs, label);
2419 break;
2420 case kCondB:
2421 __ Bltuc(lhs, rhs_reg, label);
2422 break;
2423 case kCondAE:
2424 __ Bgeuc(lhs, rhs_reg, label);
2425 break;
2426 case kCondBE:
2427 __ Bgeuc(rhs_reg, lhs, label);
2428 break;
2429 case kCondA:
2430 __ Bltuc(rhs_reg, lhs, label);
2431 break;
2432 }
2433 }
2434}
2435
2436void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2437 bool gt_bias,
2438 Primitive::Type type,
2439 LocationSummary* locations,
2440 Mips64Label* label) {
2441 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2442 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2443 if (type == Primitive::kPrimFloat) {
2444 switch (cond) {
2445 case kCondEQ:
2446 __ CmpEqS(FTMP, lhs, rhs);
2447 __ Bc1nez(FTMP, label);
2448 break;
2449 case kCondNE:
2450 __ CmpEqS(FTMP, lhs, rhs);
2451 __ Bc1eqz(FTMP, label);
2452 break;
2453 case kCondLT:
2454 if (gt_bias) {
2455 __ CmpLtS(FTMP, lhs, rhs);
2456 } else {
2457 __ CmpUltS(FTMP, lhs, rhs);
2458 }
2459 __ Bc1nez(FTMP, label);
2460 break;
2461 case kCondLE:
2462 if (gt_bias) {
2463 __ CmpLeS(FTMP, lhs, rhs);
2464 } else {
2465 __ CmpUleS(FTMP, lhs, rhs);
2466 }
2467 __ Bc1nez(FTMP, label);
2468 break;
2469 case kCondGT:
2470 if (gt_bias) {
2471 __ CmpUltS(FTMP, rhs, lhs);
2472 } else {
2473 __ CmpLtS(FTMP, rhs, lhs);
2474 }
2475 __ Bc1nez(FTMP, label);
2476 break;
2477 case kCondGE:
2478 if (gt_bias) {
2479 __ CmpUleS(FTMP, rhs, lhs);
2480 } else {
2481 __ CmpLeS(FTMP, rhs, lhs);
2482 }
2483 __ Bc1nez(FTMP, label);
2484 break;
2485 default:
2486 LOG(FATAL) << "Unexpected non-floating-point condition";
2487 }
2488 } else {
2489 DCHECK_EQ(type, Primitive::kPrimDouble);
2490 switch (cond) {
2491 case kCondEQ:
2492 __ CmpEqD(FTMP, lhs, rhs);
2493 __ Bc1nez(FTMP, label);
2494 break;
2495 case kCondNE:
2496 __ CmpEqD(FTMP, lhs, rhs);
2497 __ Bc1eqz(FTMP, label);
2498 break;
2499 case kCondLT:
2500 if (gt_bias) {
2501 __ CmpLtD(FTMP, lhs, rhs);
2502 } else {
2503 __ CmpUltD(FTMP, lhs, rhs);
2504 }
2505 __ Bc1nez(FTMP, label);
2506 break;
2507 case kCondLE:
2508 if (gt_bias) {
2509 __ CmpLeD(FTMP, lhs, rhs);
2510 } else {
2511 __ CmpUleD(FTMP, lhs, rhs);
2512 }
2513 __ Bc1nez(FTMP, label);
2514 break;
2515 case kCondGT:
2516 if (gt_bias) {
2517 __ CmpUltD(FTMP, rhs, lhs);
2518 } else {
2519 __ CmpLtD(FTMP, rhs, lhs);
2520 }
2521 __ Bc1nez(FTMP, label);
2522 break;
2523 case kCondGE:
2524 if (gt_bias) {
2525 __ CmpUleD(FTMP, rhs, lhs);
2526 } else {
2527 __ CmpLeD(FTMP, rhs, lhs);
2528 }
2529 __ Bc1nez(FTMP, label);
2530 break;
2531 default:
2532 LOG(FATAL) << "Unexpected non-floating-point condition";
2533 }
2534 }
2535}
2536
Alexey Frunze4dda3372015-06-01 18:31:49 -07002537void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002538 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002539 Mips64Label* true_target,
2540 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002541 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002542
David Brazdil0debae72015-11-12 18:37:00 +00002543 if (true_target == nullptr && false_target == nullptr) {
2544 // Nothing to do. The code always falls through.
2545 return;
2546 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002547 // Constant condition, statically compared against "true" (integer value 1).
2548 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002549 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002550 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002551 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002552 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002553 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002554 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002555 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002556 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002557 }
David Brazdil0debae72015-11-12 18:37:00 +00002558 return;
2559 }
2560
2561 // The following code generates these patterns:
2562 // (1) true_target == nullptr && false_target != nullptr
2563 // - opposite condition true => branch to false_target
2564 // (2) true_target != nullptr && false_target == nullptr
2565 // - condition true => branch to true_target
2566 // (3) true_target != nullptr && false_target != nullptr
2567 // - condition true => branch to true_target
2568 // - branch to false_target
2569 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002570 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002571 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002573 if (true_target == nullptr) {
2574 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2575 } else {
2576 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2577 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 } else {
2579 // The condition instruction has not been materialized, use its inputs as
2580 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002581 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002582 Primitive::Type type = condition->InputAt(0)->GetType();
2583 LocationSummary* locations = cond->GetLocations();
2584 IfCondition if_cond = condition->GetCondition();
2585 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002586
David Brazdil0debae72015-11-12 18:37:00 +00002587 if (true_target == nullptr) {
2588 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002589 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002590 }
2591
Alexey Frunze299a9392015-12-08 16:08:02 -08002592 switch (type) {
2593 default:
2594 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2595 break;
2596 case Primitive::kPrimLong:
2597 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2598 break;
2599 case Primitive::kPrimFloat:
2600 case Primitive::kPrimDouble:
2601 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2602 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 }
2604 }
David Brazdil0debae72015-11-12 18:37:00 +00002605
2606 // If neither branch falls through (case 3), the conditional branch to `true_target`
2607 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2608 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002609 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002610 }
2611}
2612
2613void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002615 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616 locations->SetInAt(0, Location::RequiresRegister());
2617 }
2618}
2619
2620void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002621 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2622 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002623 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002624 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002625 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002626 nullptr : codegen_->GetLabelOf(false_successor);
2627 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002628}
2629
2630void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2631 LocationSummary* locations = new (GetGraph()->GetArena())
2632 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01002633 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002634 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002635 locations->SetInAt(0, Location::RequiresRegister());
2636 }
2637}
2638
2639void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002640 SlowPathCodeMIPS64* slow_path =
2641 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002642 GenerateTestAndBranch(deoptimize,
2643 /* condition_input_index */ 0,
2644 slow_path->GetEntryLabel(),
2645 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646}
2647
David Brazdil74eb1b22015-12-14 11:44:01 +00002648void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2650 if (Primitive::IsFloatingPointType(select->GetType())) {
2651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetInAt(1, Location::RequiresFpuRegister());
2653 } else {
2654 locations->SetInAt(0, Location::RequiresRegister());
2655 locations->SetInAt(1, Location::RequiresRegister());
2656 }
2657 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2658 locations->SetInAt(2, Location::RequiresRegister());
2659 }
2660 locations->SetOut(Location::SameAsFirstInput());
2661}
2662
2663void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2664 LocationSummary* locations = select->GetLocations();
2665 Mips64Label false_target;
2666 GenerateTestAndBranch(select,
2667 /* condition_input_index */ 2,
2668 /* true_target */ nullptr,
2669 &false_target);
2670 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2671 __ Bind(&false_target);
2672}
2673
David Srbecky0cf44932015-12-09 14:09:59 +00002674void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2675 new (GetGraph()->GetArena()) LocationSummary(info);
2676}
2677
David Srbeckyd28f4a02016-03-14 17:14:24 +00002678void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2679 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002680}
2681
2682void CodeGeneratorMIPS64::GenerateNop() {
2683 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002684}
2685
Alexey Frunze4dda3372015-06-01 18:31:49 -07002686void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2687 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2688 LocationSummary* locations =
2689 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2690 locations->SetInAt(0, Location::RequiresRegister());
2691 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2692 locations->SetOut(Location::RequiresFpuRegister());
2693 } else {
2694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2695 }
2696}
2697
2698void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2699 const FieldInfo& field_info) {
2700 Primitive::Type type = field_info.GetFieldType();
2701 LocationSummary* locations = instruction->GetLocations();
2702 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2703 LoadOperandType load_type = kLoadUnsignedByte;
2704 switch (type) {
2705 case Primitive::kPrimBoolean:
2706 load_type = kLoadUnsignedByte;
2707 break;
2708 case Primitive::kPrimByte:
2709 load_type = kLoadSignedByte;
2710 break;
2711 case Primitive::kPrimShort:
2712 load_type = kLoadSignedHalfword;
2713 break;
2714 case Primitive::kPrimChar:
2715 load_type = kLoadUnsignedHalfword;
2716 break;
2717 case Primitive::kPrimInt:
2718 case Primitive::kPrimFloat:
2719 load_type = kLoadWord;
2720 break;
2721 case Primitive::kPrimLong:
2722 case Primitive::kPrimDouble:
2723 load_type = kLoadDoubleword;
2724 break;
2725 case Primitive::kPrimNot:
2726 load_type = kLoadUnsignedWord;
2727 break;
2728 case Primitive::kPrimVoid:
2729 LOG(FATAL) << "Unreachable type " << type;
2730 UNREACHABLE();
2731 }
2732 if (!Primitive::IsFloatingPointType(type)) {
2733 DCHECK(locations->Out().IsRegister());
2734 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2735 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2736 } else {
2737 DCHECK(locations->Out().IsFpuRegister());
2738 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2739 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2740 }
2741
2742 codegen_->MaybeRecordImplicitNullCheck(instruction);
2743 // TODO: memory barrier?
2744}
2745
2746void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2747 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2748 LocationSummary* locations =
2749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2750 locations->SetInAt(0, Location::RequiresRegister());
2751 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2752 locations->SetInAt(1, Location::RequiresFpuRegister());
2753 } else {
2754 locations->SetInAt(1, Location::RequiresRegister());
2755 }
2756}
2757
2758void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002759 const FieldInfo& field_info,
2760 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761 Primitive::Type type = field_info.GetFieldType();
2762 LocationSummary* locations = instruction->GetLocations();
2763 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2764 StoreOperandType store_type = kStoreByte;
2765 switch (type) {
2766 case Primitive::kPrimBoolean:
2767 case Primitive::kPrimByte:
2768 store_type = kStoreByte;
2769 break;
2770 case Primitive::kPrimShort:
2771 case Primitive::kPrimChar:
2772 store_type = kStoreHalfword;
2773 break;
2774 case Primitive::kPrimInt:
2775 case Primitive::kPrimFloat:
2776 case Primitive::kPrimNot:
2777 store_type = kStoreWord;
2778 break;
2779 case Primitive::kPrimLong:
2780 case Primitive::kPrimDouble:
2781 store_type = kStoreDoubleword;
2782 break;
2783 case Primitive::kPrimVoid:
2784 LOG(FATAL) << "Unreachable type " << type;
2785 UNREACHABLE();
2786 }
2787 if (!Primitive::IsFloatingPointType(type)) {
2788 DCHECK(locations->InAt(1).IsRegister());
2789 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2790 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2791 } else {
2792 DCHECK(locations->InAt(1).IsFpuRegister());
2793 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2794 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2795 }
2796
2797 codegen_->MaybeRecordImplicitNullCheck(instruction);
2798 // TODO: memory barriers?
2799 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2800 DCHECK(locations->InAt(1).IsRegister());
2801 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002802 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002803 }
2804}
2805
2806void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2807 HandleFieldGet(instruction, instruction->GetFieldInfo());
2808}
2809
2810void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2811 HandleFieldGet(instruction, instruction->GetFieldInfo());
2812}
2813
2814void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2815 HandleFieldSet(instruction, instruction->GetFieldInfo());
2816}
2817
2818void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002819 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002820}
2821
2822void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2823 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002824 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2826 locations->SetInAt(0, Location::RequiresRegister());
2827 locations->SetInAt(1, Location::RequiresRegister());
2828 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002829 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002830 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2831}
2832
2833void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2834 LocationSummary* locations = instruction->GetLocations();
2835 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2836 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2837 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2838
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002839 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002840
2841 // Return 0 if `obj` is null.
2842 // TODO: Avoid this check if we know `obj` is not null.
2843 __ Move(out, ZERO);
2844 __ Beqzc(obj, &done);
2845
2846 // Compare the class of `obj` with `cls`.
2847 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002848 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002849 // Classes must be equal for the instanceof to succeed.
2850 __ Xor(out, out, cls);
2851 __ Sltiu(out, out, 1);
2852 } else {
2853 // If the classes are not equal, we go into a slow path.
2854 DCHECK(locations->OnlyCallsOnSlowPath());
2855 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002856 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002857 codegen_->AddSlowPath(slow_path);
2858 __ Bnec(out, cls, slow_path->GetEntryLabel());
2859 __ LoadConst32(out, 1);
2860 __ Bind(slow_path->GetExitLabel());
2861 }
2862
2863 __ Bind(&done);
2864}
2865
2866void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2868 locations->SetOut(Location::ConstantLocation(constant));
2869}
2870
2871void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2872 // Will be generated at use site.
2873}
2874
2875void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2876 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2877 locations->SetOut(Location::ConstantLocation(constant));
2878}
2879
2880void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2881 // Will be generated at use site.
2882}
2883
Calin Juravle175dc732015-08-25 15:42:32 +01002884void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2885 // The trampoline uses the same calling convention as dex calling conventions,
2886 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2887 // the method_idx.
2888 HandleInvoke(invoke);
2889}
2890
2891void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2892 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2893}
2894
Alexey Frunze4dda3372015-06-01 18:31:49 -07002895void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2896 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2897 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2898}
2899
2900void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2901 HandleInvoke(invoke);
2902 // The register T0 is required to be used for the hidden argument in
2903 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2904 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2905}
2906
2907void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2908 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2909 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002910 Location receiver = invoke->GetLocations()->InAt(0);
2911 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07002912 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002913
2914 // Set the hidden argument.
2915 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2916 invoke->GetDexMethodIndex());
2917
2918 // temp = object->GetClass();
2919 if (receiver.IsStackSlot()) {
2920 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2921 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2922 } else {
2923 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2924 }
2925 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002926 __ LoadFromOffset(kLoadDoubleword, temp, temp,
2927 mirror::Class::ImtPtrOffset(kMips64PointerSize).Uint32Value());
2928 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002929 invoke->GetImtIndex(), kMips64PointerSize));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002930 // temp = temp->GetImtEntryAt(method_offset);
2931 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2932 // T9 = temp->GetEntryPoint();
2933 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2934 // T9();
2935 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002936 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002937 DCHECK(!codegen_->IsLeafMethod());
2938 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2939}
2940
2941void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002942 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2943 if (intrinsic.TryDispatch(invoke)) {
2944 return;
2945 }
2946
Alexey Frunze4dda3372015-06-01 18:31:49 -07002947 HandleInvoke(invoke);
2948}
2949
2950void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002951 // Explicit clinit checks triggered by static invokes must have been pruned by
2952 // art::PrepareForRegisterAllocation.
2953 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002954
Chris Larsen3039e382015-08-26 07:54:08 -07002955 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2956 if (intrinsic.TryDispatch(invoke)) {
2957 return;
2958 }
2959
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 HandleInvoke(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961}
2962
Chris Larsen3039e382015-08-26 07:54:08 -07002963static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002964 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002965 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2966 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002967 return true;
2968 }
2969 return false;
2970}
2971
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002972HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
2973 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
2974 // TODO: Implement other kinds.
2975 return HLoadString::LoadKind::kDexCacheViaMethod;
2976}
2977
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002978HLoadClass::LoadKind CodeGeneratorMIPS64::GetSupportedLoadClassKind(
2979 HLoadClass::LoadKind desired_class_load_kind) {
2980 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
2981 // TODO: Implement other kinds.
2982 return HLoadClass::LoadKind::kDexCacheViaMethod;
2983}
2984
Vladimir Markodc151b22015-10-15 18:02:30 +01002985HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2986 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2987 MethodReference target_method ATTRIBUTE_UNUSED) {
2988 switch (desired_dispatch_info.method_load_kind) {
2989 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2990 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2991 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2992 return HInvokeStaticOrDirect::DispatchInfo {
2993 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2994 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2995 0u,
2996 0u
2997 };
2998 default:
2999 break;
3000 }
3001 switch (desired_dispatch_info.code_ptr_location) {
3002 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3003 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3004 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3005 return HInvokeStaticOrDirect::DispatchInfo {
3006 desired_dispatch_info.method_load_kind,
3007 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3008 desired_dispatch_info.method_load_data,
3009 0u
3010 };
3011 default:
3012 return desired_dispatch_info;
3013 }
3014}
3015
Alexey Frunze4dda3372015-06-01 18:31:49 -07003016void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3017 // All registers are assumed to be correctly set up per the calling convention.
3018
Vladimir Marko58155012015-08-19 12:49:41 +00003019 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3020 switch (invoke->GetMethodLoadKind()) {
3021 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3022 // temp = thread->string_init_entrypoint
3023 __ LoadFromOffset(kLoadDoubleword,
3024 temp.AsRegister<GpuRegister>(),
3025 TR,
3026 invoke->GetStringInitOffset());
3027 break;
3028 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003029 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003030 break;
3031 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3032 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3033 break;
3034 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003035 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003036 // TODO: Implement these types.
3037 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3038 LOG(FATAL) << "Unsupported";
3039 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003040 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003041 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003042 GpuRegister reg = temp.AsRegister<GpuRegister>();
3043 GpuRegister method_reg;
3044 if (current_method.IsRegister()) {
3045 method_reg = current_method.AsRegister<GpuRegister>();
3046 } else {
3047 // TODO: use the appropriate DCHECK() here if possible.
3048 // DCHECK(invoke->GetLocations()->Intrinsified());
3049 DCHECK(!current_method.IsValid());
3050 method_reg = reg;
3051 __ Ld(reg, SP, kCurrentMethodStackOffset);
3052 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003053
Vladimir Marko58155012015-08-19 12:49:41 +00003054 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003055 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003056 reg,
3057 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003058 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003059 // temp = temp[index_in_cache];
3060 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3061 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003062 __ LoadFromOffset(kLoadDoubleword,
3063 reg,
3064 reg,
3065 CodeGenerator::GetCachePointerOffset(index_in_cache));
3066 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003067 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003068 }
3069
Vladimir Marko58155012015-08-19 12:49:41 +00003070 switch (invoke->GetCodePtrLocation()) {
3071 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003072 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003073 break;
3074 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3075 // LR = invoke->GetDirectCodePtr();
3076 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3077 // LR()
3078 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003079 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003080 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003081 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003082 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3083 // TODO: Implement these types.
3084 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3085 LOG(FATAL) << "Unsupported";
3086 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003087 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3088 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3089 __ LoadFromOffset(kLoadDoubleword,
3090 T9,
3091 callee_method.AsRegister<GpuRegister>(),
3092 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07003093 kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003094 // T9()
3095 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003096 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003097 break;
3098 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003099 DCHECK(!IsLeafMethod());
3100}
3101
3102void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003103 // Explicit clinit checks triggered by static invokes must have been pruned by
3104 // art::PrepareForRegisterAllocation.
3105 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003106
3107 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3108 return;
3109 }
3110
3111 LocationSummary* locations = invoke->GetLocations();
3112 codegen_->GenerateStaticOrDirectCall(invoke,
3113 locations->HasTemps()
3114 ? locations->GetTemp(0)
3115 : Location::NoLocation());
3116 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3117}
3118
Alexey Frunze53afca12015-11-05 16:34:23 -08003119void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003120 // Use the calling convention instead of the location of the receiver, as
3121 // intrinsics may have put the receiver in a different register. In the intrinsics
3122 // slow path, the arguments have been moved to the right place, so here we are
3123 // guaranteed that the receiver is the first register of the calling convention.
3124 InvokeDexCallingConvention calling_convention;
3125 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3126
Alexey Frunze53afca12015-11-05 16:34:23 -08003127 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3129 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07003131 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003132
3133 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003134 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003135 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003136 // temp = temp->GetMethodAt(method_offset);
3137 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3138 // T9 = temp->GetEntryPoint();
3139 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3140 // T9();
3141 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003142 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003143}
3144
3145void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3146 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3147 return;
3148 }
3149
3150 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003151 DCHECK(!codegen_->IsLeafMethod());
3152 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3153}
3154
3155void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003156 InvokeRuntimeCallingConvention calling_convention;
3157 CodeGenerator::CreateLoadClassLocationSummary(
3158 cls,
3159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003160 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161}
3162
3163void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3164 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003165 if (cls->NeedsAccessCheck()) {
3166 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003167 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003168 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003169 return;
3170 }
3171
3172 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3173 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3174 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003175 DCHECK(!cls->CanCallRuntime());
3176 DCHECK(!cls->MustGenerateClinitCheck());
3177 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3178 ArtMethod::DeclaringClassOffset().Int32Value());
3179 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003180 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3181 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003182 __ LoadFromOffset(
3183 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003184 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003185 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3186 DCHECK(cls->CanCallRuntime());
3187 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3188 cls,
3189 cls,
3190 cls->GetDexPc(),
3191 cls->MustGenerateClinitCheck());
3192 codegen_->AddSlowPath(slow_path);
3193 if (!cls->IsInDexCache()) {
3194 __ Beqzc(out, slow_path->GetEntryLabel());
3195 }
3196 if (cls->MustGenerateClinitCheck()) {
3197 GenerateClassInitializationCheck(slow_path, out);
3198 } else {
3199 __ Bind(slow_path->GetExitLabel());
3200 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003201 }
3202 }
3203}
3204
David Brazdilcb1c0552015-08-04 16:22:25 +01003205static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07003206 return Thread::ExceptionOffset<kMips64PointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003207}
3208
Alexey Frunze4dda3372015-06-01 18:31:49 -07003209void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3210 LocationSummary* locations =
3211 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3212 locations->SetOut(Location::RequiresRegister());
3213}
3214
3215void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3216 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003217 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3218}
3219
3220void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3221 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3222}
3223
3224void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3225 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003226}
3227
Alexey Frunze4dda3372015-06-01 18:31:49 -07003228void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003229 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3230 ? LocationSummary::kCallOnSlowPath
3231 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003233 locations->SetInAt(0, Location::RequiresRegister());
3234 locations->SetOut(Location::RequiresRegister());
3235}
3236
3237void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07003238 // TODO: Re-add the compiler code to do string dex cache lookup again.
3239 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3240 codegen_->AddSlowPath(slow_path);
3241 __ Bc(slow_path->GetEntryLabel());
3242 __ Bind(slow_path->GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003243}
3244
Alexey Frunze4dda3372015-06-01 18:31:49 -07003245void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3246 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3247 locations->SetOut(Location::ConstantLocation(constant));
3248}
3249
3250void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3251 // Will be generated at use site.
3252}
3253
3254void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3255 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003257 InvokeRuntimeCallingConvention calling_convention;
3258 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3259}
3260
3261void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003262 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003263 instruction,
Serban Constantinescufc734082016-07-19 17:18:07 +01003264 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003265 if (instruction->IsEnter()) {
3266 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3267 } else {
3268 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3269 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270}
3271
3272void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3273 LocationSummary* locations =
3274 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3275 switch (mul->GetResultType()) {
3276 case Primitive::kPrimInt:
3277 case Primitive::kPrimLong:
3278 locations->SetInAt(0, Location::RequiresRegister());
3279 locations->SetInAt(1, Location::RequiresRegister());
3280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3281 break;
3282
3283 case Primitive::kPrimFloat:
3284 case Primitive::kPrimDouble:
3285 locations->SetInAt(0, Location::RequiresFpuRegister());
3286 locations->SetInAt(1, Location::RequiresFpuRegister());
3287 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3288 break;
3289
3290 default:
3291 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3292 }
3293}
3294
3295void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3296 Primitive::Type type = instruction->GetType();
3297 LocationSummary* locations = instruction->GetLocations();
3298
3299 switch (type) {
3300 case Primitive::kPrimInt:
3301 case Primitive::kPrimLong: {
3302 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3303 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3304 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3305 if (type == Primitive::kPrimInt)
3306 __ MulR6(dst, lhs, rhs);
3307 else
3308 __ Dmul(dst, lhs, rhs);
3309 break;
3310 }
3311 case Primitive::kPrimFloat:
3312 case Primitive::kPrimDouble: {
3313 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3314 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3315 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3316 if (type == Primitive::kPrimFloat)
3317 __ MulS(dst, lhs, rhs);
3318 else
3319 __ MulD(dst, lhs, rhs);
3320 break;
3321 }
3322 default:
3323 LOG(FATAL) << "Unexpected mul type " << type;
3324 }
3325}
3326
3327void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3328 LocationSummary* locations =
3329 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3330 switch (neg->GetResultType()) {
3331 case Primitive::kPrimInt:
3332 case Primitive::kPrimLong:
3333 locations->SetInAt(0, Location::RequiresRegister());
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 break;
3336
3337 case Primitive::kPrimFloat:
3338 case Primitive::kPrimDouble:
3339 locations->SetInAt(0, Location::RequiresFpuRegister());
3340 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3341 break;
3342
3343 default:
3344 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3345 }
3346}
3347
3348void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3349 Primitive::Type type = instruction->GetType();
3350 LocationSummary* locations = instruction->GetLocations();
3351
3352 switch (type) {
3353 case Primitive::kPrimInt:
3354 case Primitive::kPrimLong: {
3355 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3356 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3357 if (type == Primitive::kPrimInt)
3358 __ Subu(dst, ZERO, src);
3359 else
3360 __ Dsubu(dst, ZERO, src);
3361 break;
3362 }
3363 case Primitive::kPrimFloat:
3364 case Primitive::kPrimDouble: {
3365 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3366 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3367 if (type == Primitive::kPrimFloat)
3368 __ NegS(dst, src);
3369 else
3370 __ NegD(dst, src);
3371 break;
3372 }
3373 default:
3374 LOG(FATAL) << "Unexpected neg type " << type;
3375 }
3376}
3377
3378void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3379 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003381 InvokeRuntimeCallingConvention calling_convention;
3382 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3383 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3384 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3385 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3386}
3387
3388void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3389 LocationSummary* locations = instruction->GetLocations();
3390 // Move an uint16_t value to a register.
3391 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Serban Constantinescufc734082016-07-19 17:18:07 +01003392 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003393 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3394}
3395
3396void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3397 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003399 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003400 if (instruction->IsStringAlloc()) {
3401 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3402 } else {
3403 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3404 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3405 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003406 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3407}
3408
3409void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003410 if (instruction->IsStringAlloc()) {
3411 // String is allocated through StringFactory. Call NewEmptyString entry point.
3412 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003413 MemberOffset code_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -07003414 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003415 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3416 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3417 __ Jalr(T9);
3418 __ Nop();
3419 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3420 } else {
Serban Constantinescufc734082016-07-19 17:18:07 +01003421 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003422 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3423 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003424}
3425
3426void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3428 locations->SetInAt(0, Location::RequiresRegister());
3429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3430}
3431
3432void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3433 Primitive::Type type = instruction->GetType();
3434 LocationSummary* locations = instruction->GetLocations();
3435
3436 switch (type) {
3437 case Primitive::kPrimInt:
3438 case Primitive::kPrimLong: {
3439 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3440 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3441 __ Nor(dst, src, ZERO);
3442 break;
3443 }
3444
3445 default:
3446 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3447 }
3448}
3449
3450void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3451 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3452 locations->SetInAt(0, Location::RequiresRegister());
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454}
3455
3456void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3457 LocationSummary* locations = instruction->GetLocations();
3458 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3459 locations->InAt(0).AsRegister<GpuRegister>(),
3460 1);
3461}
3462
3463void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00003464 codegen_->CreateNullCheckLocations(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003465}
3466
Calin Juravle2ae48182016-03-16 14:05:09 +00003467void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3468 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003469 return;
3470 }
3471 Location obj = instruction->GetLocations()->InAt(0);
3472
3473 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003474 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003475}
3476
Calin Juravle2ae48182016-03-16 14:05:09 +00003477void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003478 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003479 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003480
3481 Location obj = instruction->GetLocations()->InAt(0);
3482
3483 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3484}
3485
3486void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003487 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003488}
3489
3490void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3491 HandleBinaryOp(instruction);
3492}
3493
3494void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3495 HandleBinaryOp(instruction);
3496}
3497
3498void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3499 LOG(FATAL) << "Unreachable";
3500}
3501
3502void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3503 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3504}
3505
3506void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3508 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3509 if (location.IsStackSlot()) {
3510 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3511 } else if (location.IsDoubleStackSlot()) {
3512 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3513 }
3514 locations->SetOut(location);
3515}
3516
3517void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3518 ATTRIBUTE_UNUSED) {
3519 // Nothing to do, the parameter is already at its location.
3520}
3521
3522void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3523 LocationSummary* locations =
3524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3525 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3526}
3527
3528void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3529 ATTRIBUTE_UNUSED) {
3530 // Nothing to do, the method is already at its location.
3531}
3532
3533void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003535 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003536 locations->SetInAt(i, Location::Any());
3537 }
3538 locations->SetOut(Location::Any());
3539}
3540
3541void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3542 LOG(FATAL) << "Unreachable";
3543}
3544
3545void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3546 Primitive::Type type = rem->GetResultType();
3547 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003548 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3549 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003550 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3551
3552 switch (type) {
3553 case Primitive::kPrimInt:
3554 case Primitive::kPrimLong:
3555 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003556 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3558 break;
3559
3560 case Primitive::kPrimFloat:
3561 case Primitive::kPrimDouble: {
3562 InvokeRuntimeCallingConvention calling_convention;
3563 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3564 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3565 locations->SetOut(calling_convention.GetReturnLocation(type));
3566 break;
3567 }
3568
3569 default:
3570 LOG(FATAL) << "Unexpected rem type " << type;
3571 }
3572}
3573
3574void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3575 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003576
3577 switch (type) {
3578 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003579 case Primitive::kPrimLong:
3580 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003581 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003582
3583 case Primitive::kPrimFloat:
3584 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003585 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3586 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003587 if (type == Primitive::kPrimFloat) {
3588 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3589 } else {
3590 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3591 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003592 break;
3593 }
3594 default:
3595 LOG(FATAL) << "Unexpected rem type " << type;
3596 }
3597}
3598
3599void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3600 memory_barrier->SetLocations(nullptr);
3601}
3602
3603void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3604 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3605}
3606
3607void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3609 Primitive::Type return_type = ret->InputAt(0)->GetType();
3610 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3611}
3612
3613void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3614 codegen_->GenerateFrameExit();
3615}
3616
3617void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3618 ret->SetLocations(nullptr);
3619}
3620
3621void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3622 codegen_->GenerateFrameExit();
3623}
3624
Alexey Frunze92d90602015-12-18 18:16:36 -08003625void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3626 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003627}
3628
Alexey Frunze92d90602015-12-18 18:16:36 -08003629void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3630 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003631}
3632
Alexey Frunze4dda3372015-06-01 18:31:49 -07003633void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3634 HandleShift(shl);
3635}
3636
3637void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3638 HandleShift(shl);
3639}
3640
3641void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3642 HandleShift(shr);
3643}
3644
3645void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3646 HandleShift(shr);
3647}
3648
Alexey Frunze4dda3372015-06-01 18:31:49 -07003649void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3650 HandleBinaryOp(instruction);
3651}
3652
3653void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3654 HandleBinaryOp(instruction);
3655}
3656
3657void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3658 HandleFieldGet(instruction, instruction->GetFieldInfo());
3659}
3660
3661void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3662 HandleFieldGet(instruction, instruction->GetFieldInfo());
3663}
3664
3665void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3666 HandleFieldSet(instruction, instruction->GetFieldInfo());
3667}
3668
3669void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003670 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003671}
3672
Calin Juravlee460d1d2015-09-29 04:52:17 +01003673void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3674 HUnresolvedInstanceFieldGet* instruction) {
3675 FieldAccessCallingConventionMIPS64 calling_convention;
3676 codegen_->CreateUnresolvedFieldLocationSummary(
3677 instruction, instruction->GetFieldType(), calling_convention);
3678}
3679
3680void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3681 HUnresolvedInstanceFieldGet* instruction) {
3682 FieldAccessCallingConventionMIPS64 calling_convention;
3683 codegen_->GenerateUnresolvedFieldAccess(instruction,
3684 instruction->GetFieldType(),
3685 instruction->GetFieldIndex(),
3686 instruction->GetDexPc(),
3687 calling_convention);
3688}
3689
3690void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3691 HUnresolvedInstanceFieldSet* instruction) {
3692 FieldAccessCallingConventionMIPS64 calling_convention;
3693 codegen_->CreateUnresolvedFieldLocationSummary(
3694 instruction, instruction->GetFieldType(), calling_convention);
3695}
3696
3697void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3698 HUnresolvedInstanceFieldSet* instruction) {
3699 FieldAccessCallingConventionMIPS64 calling_convention;
3700 codegen_->GenerateUnresolvedFieldAccess(instruction,
3701 instruction->GetFieldType(),
3702 instruction->GetFieldIndex(),
3703 instruction->GetDexPc(),
3704 calling_convention);
3705}
3706
3707void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3708 HUnresolvedStaticFieldGet* instruction) {
3709 FieldAccessCallingConventionMIPS64 calling_convention;
3710 codegen_->CreateUnresolvedFieldLocationSummary(
3711 instruction, instruction->GetFieldType(), calling_convention);
3712}
3713
3714void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3715 HUnresolvedStaticFieldGet* instruction) {
3716 FieldAccessCallingConventionMIPS64 calling_convention;
3717 codegen_->GenerateUnresolvedFieldAccess(instruction,
3718 instruction->GetFieldType(),
3719 instruction->GetFieldIndex(),
3720 instruction->GetDexPc(),
3721 calling_convention);
3722}
3723
3724void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3725 HUnresolvedStaticFieldSet* instruction) {
3726 FieldAccessCallingConventionMIPS64 calling_convention;
3727 codegen_->CreateUnresolvedFieldLocationSummary(
3728 instruction, instruction->GetFieldType(), calling_convention);
3729}
3730
3731void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3732 HUnresolvedStaticFieldSet* instruction) {
3733 FieldAccessCallingConventionMIPS64 calling_convention;
3734 codegen_->GenerateUnresolvedFieldAccess(instruction,
3735 instruction->GetFieldType(),
3736 instruction->GetFieldIndex(),
3737 instruction->GetDexPc(),
3738 calling_convention);
3739}
3740
Alexey Frunze4dda3372015-06-01 18:31:49 -07003741void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01003742 LocationSummary* locations =
3743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3744 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Alexey Frunze4dda3372015-06-01 18:31:49 -07003745}
3746
3747void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3748 HBasicBlock* block = instruction->GetBlock();
3749 if (block->GetLoopInformation() != nullptr) {
3750 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3751 // The back edge will generate the suspend check.
3752 return;
3753 }
3754 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3755 // The goto will generate the suspend check.
3756 return;
3757 }
3758 GenerateSuspendCheck(instruction, nullptr);
3759}
3760
Alexey Frunze4dda3372015-06-01 18:31:49 -07003761void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3762 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003763 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003764 InvokeRuntimeCallingConvention calling_convention;
3765 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3766}
3767
3768void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003769 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003770 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3771}
3772
3773void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3774 Primitive::Type input_type = conversion->GetInputType();
3775 Primitive::Type result_type = conversion->GetResultType();
3776 DCHECK_NE(input_type, result_type);
3777
3778 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3779 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3780 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3781 }
3782
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3784
3785 if (Primitive::IsFloatingPointType(input_type)) {
3786 locations->SetInAt(0, Location::RequiresFpuRegister());
3787 } else {
3788 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003789 }
3790
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003791 if (Primitive::IsFloatingPointType(result_type)) {
3792 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003793 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003795 }
3796}
3797
3798void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3799 LocationSummary* locations = conversion->GetLocations();
3800 Primitive::Type result_type = conversion->GetResultType();
3801 Primitive::Type input_type = conversion->GetInputType();
3802
3803 DCHECK_NE(input_type, result_type);
3804
3805 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3806 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3807 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3808
3809 switch (result_type) {
3810 case Primitive::kPrimChar:
3811 __ Andi(dst, src, 0xFFFF);
3812 break;
3813 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003814 if (input_type == Primitive::kPrimLong) {
3815 // Type conversion from long to types narrower than int is a result of code
3816 // transformations. To avoid unpredictable results for SEB and SEH, we first
3817 // need to sign-extend the low 32-bit value into bits 32 through 63.
3818 __ Sll(dst, src, 0);
3819 __ Seb(dst, dst);
3820 } else {
3821 __ Seb(dst, src);
3822 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003823 break;
3824 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003825 if (input_type == Primitive::kPrimLong) {
3826 // Type conversion from long to types narrower than int is a result of code
3827 // transformations. To avoid unpredictable results for SEB and SEH, we first
3828 // need to sign-extend the low 32-bit value into bits 32 through 63.
3829 __ Sll(dst, src, 0);
3830 __ Seh(dst, dst);
3831 } else {
3832 __ Seh(dst, src);
3833 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003834 break;
3835 case Primitive::kPrimInt:
3836 case Primitive::kPrimLong:
3837 // Sign-extend 32-bit int into bits 32 through 63 for
3838 // int-to-long and long-to-int conversions
3839 __ Sll(dst, src, 0);
3840 break;
3841
3842 default:
3843 LOG(FATAL) << "Unexpected type conversion from " << input_type
3844 << " to " << result_type;
3845 }
3846 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003847 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3848 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3849 if (input_type == Primitive::kPrimLong) {
3850 __ Dmtc1(src, FTMP);
3851 if (result_type == Primitive::kPrimFloat) {
3852 __ Cvtsl(dst, FTMP);
3853 } else {
3854 __ Cvtdl(dst, FTMP);
3855 }
3856 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003857 __ Mtc1(src, FTMP);
3858 if (result_type == Primitive::kPrimFloat) {
3859 __ Cvtsw(dst, FTMP);
3860 } else {
3861 __ Cvtdw(dst, FTMP);
3862 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003863 }
3864 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3865 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003866 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3867 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3868 Mips64Label truncate;
3869 Mips64Label done;
3870
3871 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3872 // value when the input is either a NaN or is outside of the range of the output type
3873 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3874 // the same result.
3875 //
3876 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3877 // value of the output type if the input is outside of the range after the truncation or
3878 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3879 // results. This matches the desired float/double-to-int/long conversion exactly.
3880 //
3881 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3882 //
3883 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3884 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3885 // even though it must be NAN2008=1 on R6.
3886 //
3887 // The code takes care of the different behaviors by first comparing the input to the
3888 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3889 // If the input is greater than or equal to the minimum, it procedes to the truncate
3890 // instruction, which will handle such an input the same way irrespective of NAN2008.
3891 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3892 // in order to return either zero or the minimum value.
3893 //
3894 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3895 // truncate instruction for MIPS64R6.
3896 if (input_type == Primitive::kPrimFloat) {
3897 uint32_t min_val = (result_type == Primitive::kPrimLong)
3898 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3899 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3900 __ LoadConst32(TMP, min_val);
3901 __ Mtc1(TMP, FTMP);
3902 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003903 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003904 uint64_t min_val = (result_type == Primitive::kPrimLong)
3905 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3906 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3907 __ LoadConst64(TMP, min_val);
3908 __ Dmtc1(TMP, FTMP);
3909 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003910 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003911
3912 __ Bc1nez(FTMP, &truncate);
3913
3914 if (input_type == Primitive::kPrimFloat) {
3915 __ CmpEqS(FTMP, src, src);
3916 } else {
3917 __ CmpEqD(FTMP, src, src);
3918 }
3919 if (result_type == Primitive::kPrimLong) {
3920 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3921 } else {
3922 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3923 }
3924 __ Mfc1(TMP, FTMP);
3925 __ And(dst, dst, TMP);
3926
3927 __ Bc(&done);
3928
3929 __ Bind(&truncate);
3930
3931 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003932 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003933 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003934 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003935 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003936 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003937 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003938 } else {
3939 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003940 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003941 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003942 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003943 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003944 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003945 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003946
3947 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003948 } else if (Primitive::IsFloatingPointType(result_type) &&
3949 Primitive::IsFloatingPointType(input_type)) {
3950 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3951 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3952 if (result_type == Primitive::kPrimFloat) {
3953 __ Cvtsd(dst, src);
3954 } else {
3955 __ Cvtds(dst, src);
3956 }
3957 } else {
3958 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3959 << " to " << result_type;
3960 }
3961}
3962
3963void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3964 HandleShift(ushr);
3965}
3966
3967void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3968 HandleShift(ushr);
3969}
3970
3971void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3972 HandleBinaryOp(instruction);
3973}
3974
3975void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3976 HandleBinaryOp(instruction);
3977}
3978
3979void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3980 // Nothing to do, this should be removed during prepare for register allocator.
3981 LOG(FATAL) << "Unreachable";
3982}
3983
3984void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3985 // Nothing to do, this should be removed during prepare for register allocator.
3986 LOG(FATAL) << "Unreachable";
3987}
3988
3989void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003990 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003991}
3992
3993void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003994 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003995}
3996
3997void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003998 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003999}
4000
4001void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004002 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004003}
4004
4005void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004006 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004007}
4008
4009void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004010 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004011}
4012
4013void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004014 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004015}
4016
4017void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004018 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004019}
4020
4021void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004022 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004023}
4024
4025void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004026 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004027}
4028
4029void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004030 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004031}
4032
4033void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004034 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004035}
4036
Aart Bike9f37602015-10-09 11:15:55 -07004037void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004038 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004039}
4040
4041void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004042 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004043}
4044
4045void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004046 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004047}
4048
4049void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004050 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004051}
4052
4053void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004054 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004055}
4056
4057void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004058 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004059}
4060
4061void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004062 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004063}
4064
4065void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004066 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004067}
4068
Mark Mendellfe57faa2015-09-18 09:26:15 -04004069// Simple implementation of packed switch - generate cascaded compare/jumps.
4070void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4071 LocationSummary* locations =
4072 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4073 locations->SetInAt(0, Location::RequiresRegister());
4074}
4075
4076void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4077 int32_t lower_bound = switch_instr->GetStartValue();
4078 int32_t num_entries = switch_instr->GetNumEntries();
4079 LocationSummary* locations = switch_instr->GetLocations();
4080 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4081 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4082
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004083 // Create a set of compare/jumps.
4084 GpuRegister temp_reg = TMP;
4085 if (IsInt<16>(-lower_bound)) {
4086 __ Addiu(temp_reg, value_reg, -lower_bound);
4087 } else {
4088 __ LoadConst32(AT, -lower_bound);
4089 __ Addu(temp_reg, value_reg, AT);
4090 }
4091 // Jump to default if index is negative
4092 // Note: We don't check the case that index is positive while value < lower_bound, because in
4093 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4094 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4095
Mark Mendellfe57faa2015-09-18 09:26:15 -04004096 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004097 // Jump to successors[0] if value == lower_bound.
4098 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4099 int32_t last_index = 0;
4100 for (; num_entries - last_index > 2; last_index += 2) {
4101 __ Addiu(temp_reg, temp_reg, -2);
4102 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4103 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4104 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4105 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4106 }
4107 if (num_entries - last_index == 2) {
4108 // The last missing case_value.
4109 __ Addiu(temp_reg, temp_reg, -1);
4110 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004111 }
4112
4113 // And the default for any other value.
4114 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004115 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004116 }
4117}
4118
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004119void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4120 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4121}
4122
4123void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4124 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4125}
4126
Alexey Frunze4dda3372015-06-01 18:31:49 -07004127} // namespace mips64
4128} // namespace art