blob: 71d35bd0d202b81159847096ceb9469947578366 [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());
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
Serban Constantinescufc734082016-07-19 17:18:07 +0100380 mips64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000381 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 }
383
Roland Levillain46648892015-06-19 16:07:18 +0100384 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
385
Alexey Frunze4dda3372015-06-01 18:31:49 -0700386 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
388};
389
390CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
391 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100392 const CompilerOptions& compiler_options,
393 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700394 : CodeGenerator(graph,
395 kNumberOfGpuRegisters,
396 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000397 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
399 arraysize(kCoreCalleeSaves)),
400 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
401 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100402 compiler_options,
403 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100404 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700405 location_builder_(graph, this),
406 instruction_visitor_(graph, this),
407 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100408 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700409 isa_features_(isa_features) {
410 // Save RA (containing the return address) to mimic Quick.
411 AddAllocatedRegister(Location::RegisterLocation(RA));
412}
413
414#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100415// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
416#define __ down_cast<Mips64Assembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700417#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64PointerSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700418
419void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700420 // Ensure that we fix up branches.
421 __ FinalizeCode();
422
423 // Adjust native pc offsets in stack maps.
424 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
425 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
426 uint32_t new_position = __ GetAdjustedPosition(old_position);
427 DCHECK_GE(new_position, old_position);
428 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
429 }
430
431 // Adjust pc offsets for the disassembly information.
432 if (disasm_info_ != nullptr) {
433 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
434 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
435 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
436 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
437 it.second.start = __ GetAdjustedPosition(it.second.start);
438 it.second.end = __ GetAdjustedPosition(it.second.end);
439 }
440 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
441 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
442 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
443 }
444 }
445
Alexey Frunze4dda3372015-06-01 18:31:49 -0700446 CodeGenerator::Finalize(allocator);
447}
448
449Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
450 return codegen_->GetAssembler();
451}
452
453void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100454 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700455 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
456}
457
458void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100459 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700460 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
461}
462
463void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
464 // Pop reg
465 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200466 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700467}
468
469void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
470 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200471 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700472 __ Sd(GpuRegister(reg), SP, 0);
473}
474
475void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
476 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
477 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
478 // Allocate a scratch register other than TMP, if available.
479 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
480 // automatically unspilled when the scratch scope object is destroyed).
481 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
482 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200483 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700484 __ LoadFromOffset(load_type,
485 GpuRegister(ensure_scratch.GetRegister()),
486 SP,
487 index1 + stack_offset);
488 __ LoadFromOffset(load_type,
489 TMP,
490 SP,
491 index2 + stack_offset);
492 __ StoreToOffset(store_type,
493 GpuRegister(ensure_scratch.GetRegister()),
494 SP,
495 index2 + stack_offset);
496 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
497}
498
499static dwarf::Reg DWARFReg(GpuRegister reg) {
500 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
501}
502
David Srbeckyba702002016-02-01 18:15:29 +0000503static dwarf::Reg DWARFReg(FpuRegister reg) {
504 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
505}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700506
507void CodeGeneratorMIPS64::GenerateFrameEntry() {
508 __ Bind(&frame_entry_label_);
509
510 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
511
512 if (do_overflow_check) {
513 __ LoadFromOffset(kLoadWord,
514 ZERO,
515 SP,
516 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
517 RecordPcInfo(nullptr, 0);
518 }
519
520 // TODO: anything related to T9/GP/GOT/PIC/.so's?
521
522 if (HasEmptyFrame()) {
523 return;
524 }
525
526 // Make sure the frame size isn't unreasonably large. Per the various APIs
527 // it looks like it should always be less than 2GB in size, which allows
528 // us using 32-bit signed offsets from the stack pointer.
529 if (GetFrameSize() > 0x7FFFFFFF)
530 LOG(FATAL) << "Stack frame larger than 2GB";
531
532 // Spill callee-saved registers.
533 // Note that their cumulative size is small and they can be indexed using
534 // 16-bit offsets.
535
536 // TODO: increment/decrement SP in one step instead of two or remove this comment.
537
538 uint32_t ofs = FrameEntrySpillSize();
539 __ IncreaseFrameSize(ofs);
540
541 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
542 GpuRegister reg = kCoreCalleeSaves[i];
543 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200544 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700545 __ Sd(reg, SP, ofs);
546 __ cfi().RelOffset(DWARFReg(reg), ofs);
547 }
548 }
549
550 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
551 FpuRegister reg = kFpuCalleeSaves[i];
552 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200553 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700554 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000555 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700556 }
557 }
558
559 // Allocate the rest of the frame and store the current method pointer
560 // at its end.
561
562 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
563
564 static_assert(IsInt<16>(kCurrentMethodStackOffset),
565 "kCurrentMethodStackOffset must fit into int16_t");
566 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
567}
568
569void CodeGeneratorMIPS64::GenerateFrameExit() {
570 __ cfi().RememberState();
571
572 // TODO: anything related to T9/GP/GOT/PIC/.so's?
573
574 if (!HasEmptyFrame()) {
575 // Deallocate the rest of the frame.
576
577 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
578
579 // Restore callee-saved registers.
580 // Note that their cumulative size is small and they can be indexed using
581 // 16-bit offsets.
582
583 // TODO: increment/decrement SP in one step instead of two or remove this comment.
584
585 uint32_t ofs = 0;
586
587 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
588 FpuRegister reg = kFpuCalleeSaves[i];
589 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
590 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200591 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000592 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700593 }
594 }
595
596 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
597 GpuRegister reg = kCoreCalleeSaves[i];
598 if (allocated_registers_.ContainsCoreRegister(reg)) {
599 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200600 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700601 __ cfi().Restore(DWARFReg(reg));
602 }
603 }
604
605 DCHECK_EQ(ofs, FrameEntrySpillSize());
606 __ DecreaseFrameSize(ofs);
607 }
608
609 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700610 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700611
612 __ cfi().RestoreState();
613 __ cfi().DefCFAOffset(GetFrameSize());
614}
615
616void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
617 __ Bind(GetLabelOf(block));
618}
619
620void CodeGeneratorMIPS64::MoveLocation(Location destination,
621 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100622 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700623 if (source.Equals(destination)) {
624 return;
625 }
626
627 // A valid move can always be inferred from the destination and source
628 // locations. When moving from and to a register, the argument type can be
629 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100630 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700631 DCHECK_EQ(unspecified_type, false);
632
633 if (destination.IsRegister() || destination.IsFpuRegister()) {
634 if (unspecified_type) {
635 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
636 if (source.IsStackSlot() ||
637 (src_cst != nullptr && (src_cst->IsIntConstant()
638 || src_cst->IsFloatConstant()
639 || src_cst->IsNullConstant()))) {
640 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100641 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700642 } else {
643 // If the source is a double stack slot or a 64bit constant, a 64bit
644 // type is appropriate. Else the source is a register, and since the
645 // type has not been specified, we chose a 64bit type to force a 64bit
646 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100647 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700648 }
649 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100650 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
651 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
653 // Move to GPR/FPR from stack
654 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100655 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700656 __ LoadFpuFromOffset(load_type,
657 destination.AsFpuRegister<FpuRegister>(),
658 SP,
659 source.GetStackIndex());
660 } else {
661 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
662 __ LoadFromOffset(load_type,
663 destination.AsRegister<GpuRegister>(),
664 SP,
665 source.GetStackIndex());
666 }
667 } else if (source.IsConstant()) {
668 // Move to GPR/FPR from constant
669 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100670 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700671 gpr = destination.AsRegister<GpuRegister>();
672 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100673 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700674 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
675 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
676 gpr = ZERO;
677 } else {
678 __ LoadConst32(gpr, value);
679 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700680 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700681 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
682 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
683 gpr = ZERO;
684 } else {
685 __ LoadConst64(gpr, value);
686 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700687 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100688 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700689 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100690 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700691 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
692 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100693 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700694 if (destination.IsRegister()) {
695 // Move to GPR from GPR
696 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
697 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100698 DCHECK(destination.IsFpuRegister());
699 if (Primitive::Is64BitType(dst_type)) {
700 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
701 } else {
702 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
703 }
704 }
705 } else if (source.IsFpuRegister()) {
706 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100708 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700709 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
710 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100711 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700712 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
713 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 } else {
715 DCHECK(destination.IsRegister());
716 if (Primitive::Is64BitType(dst_type)) {
717 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
718 } else {
719 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
720 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700721 }
722 }
723 } else { // The destination is not a register. It must be a stack slot.
724 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
725 if (source.IsRegister() || source.IsFpuRegister()) {
726 if (unspecified_type) {
727 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100728 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700729 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100730 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700731 }
732 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100733 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
734 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700735 // Move to stack from GPR/FPR
736 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
737 if (source.IsRegister()) {
738 __ StoreToOffset(store_type,
739 source.AsRegister<GpuRegister>(),
740 SP,
741 destination.GetStackIndex());
742 } else {
743 __ StoreFpuToOffset(store_type,
744 source.AsFpuRegister<FpuRegister>(),
745 SP,
746 destination.GetStackIndex());
747 }
748 } else if (source.IsConstant()) {
749 // Move to stack from constant
750 HConstant* src_cst = source.GetConstant();
751 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700752 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700753 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700754 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
755 if (value != 0) {
756 gpr = TMP;
757 __ LoadConst32(gpr, value);
758 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700759 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700760 DCHECK(destination.IsDoubleStackSlot());
761 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
762 if (value != 0) {
763 gpr = TMP;
764 __ LoadConst64(gpr, value);
765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700766 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700767 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700768 } else {
769 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
770 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
771 // Move to stack from stack
772 if (destination.IsStackSlot()) {
773 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
774 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
775 } else {
776 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
777 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
778 }
779 }
780 }
781}
782
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700783void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700784 DCHECK(!loc1.IsConstant());
785 DCHECK(!loc2.IsConstant());
786
787 if (loc1.Equals(loc2)) {
788 return;
789 }
790
791 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
792 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
793 bool is_fp_reg1 = loc1.IsFpuRegister();
794 bool is_fp_reg2 = loc2.IsFpuRegister();
795
796 if (loc2.IsRegister() && loc1.IsRegister()) {
797 // Swap 2 GPRs
798 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
799 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
800 __ Move(TMP, r2);
801 __ Move(r2, r1);
802 __ Move(r1, TMP);
803 } else if (is_fp_reg2 && is_fp_reg1) {
804 // Swap 2 FPRs
805 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
806 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700807 if (type == Primitive::kPrimFloat) {
808 __ MovS(FTMP, r1);
809 __ MovS(r1, r2);
810 __ MovS(r2, FTMP);
811 } else {
812 DCHECK_EQ(type, Primitive::kPrimDouble);
813 __ MovD(FTMP, r1);
814 __ MovD(r1, r2);
815 __ MovD(r2, FTMP);
816 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700817 } else if (is_slot1 != is_slot2) {
818 // Swap GPR/FPR and stack slot
819 Location reg_loc = is_slot1 ? loc2 : loc1;
820 Location mem_loc = is_slot1 ? loc1 : loc2;
821 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
822 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
823 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
824 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
825 if (reg_loc.IsFpuRegister()) {
826 __ StoreFpuToOffset(store_type,
827 reg_loc.AsFpuRegister<FpuRegister>(),
828 SP,
829 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700830 if (mem_loc.IsStackSlot()) {
831 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
832 } else {
833 DCHECK(mem_loc.IsDoubleStackSlot());
834 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
835 }
836 } else {
837 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
838 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
839 }
840 } else if (is_slot1 && is_slot2) {
841 move_resolver_.Exchange(loc1.GetStackIndex(),
842 loc2.GetStackIndex(),
843 loc1.IsDoubleStackSlot());
844 } else {
845 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
846 }
847}
848
Calin Juravle175dc732015-08-25 15:42:32 +0100849void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
850 DCHECK(location.IsRegister());
851 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
852}
853
Calin Juravlee460d1d2015-09-29 04:52:17 +0100854void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
855 if (location.IsRegister()) {
856 locations->AddTemp(location);
857 } else {
858 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
859 }
860}
861
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100862void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
863 GpuRegister value,
864 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700865 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700866 GpuRegister card = AT;
867 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100868 if (value_can_be_null) {
869 __ Beqzc(value, &done);
870 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700871 __ LoadFromOffset(kLoadDoubleword,
872 card,
873 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700874 Thread::CardTableOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700875 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
876 __ Daddu(temp, card, temp);
877 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100878 if (value_can_be_null) {
879 __ Bind(&done);
880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700881}
882
David Brazdil58282f42016-01-14 12:45:10 +0000883void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700884 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
885 blocked_core_registers_[ZERO] = true;
886 blocked_core_registers_[K0] = true;
887 blocked_core_registers_[K1] = true;
888 blocked_core_registers_[GP] = true;
889 blocked_core_registers_[SP] = true;
890 blocked_core_registers_[RA] = true;
891
Lazar Trsicd9672662015-09-03 17:33:01 +0200892 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
893 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700894 blocked_core_registers_[AT] = true;
895 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200896 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700897 blocked_fpu_registers_[FTMP] = true;
898
899 // Reserve suspend and thread registers.
900 blocked_core_registers_[S0] = true;
901 blocked_core_registers_[TR] = true;
902
903 // Reserve T9 for function calls
904 blocked_core_registers_[T9] = true;
905
906 // TODO: review; anything else?
907
Goran Jakovljevic782be112016-06-21 12:39:04 +0200908 if (GetGraph()->IsDebuggable()) {
909 // Stubs do not save callee-save floating point registers. If the graph
910 // is debuggable, we need to deal with these registers differently. For
911 // now, just block them.
912 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
913 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
914 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700915 }
916}
917
Alexey Frunze4dda3372015-06-01 18:31:49 -0700918size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
919 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200920 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700921}
922
923size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
924 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200925 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700926}
927
928size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
929 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200930 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700931}
932
933size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
934 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200935 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936}
937
938void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100939 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700940}
941
942void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100943 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944}
945
Calin Juravle175dc732015-08-25 15:42:32 +0100946void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947 HInstruction* instruction,
948 uint32_t dex_pc,
949 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100950 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951 // TODO: anything related to T9/GP/GOT/PIC/.so's?
Serban Constantinescufc734082016-07-19 17:18:07 +0100952 __ LoadFromOffset(kLoadDoubleword,
953 T9,
954 TR,
955 GetThreadOffset<kMips64PointerSize>(entrypoint).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700957 __ Nop();
Serban Constantinescufc734082016-07-19 17:18:07 +0100958 if (EntrypointRequiresStackMap(entrypoint)) {
959 RecordPcInfo(instruction, dex_pc, slow_path);
960 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700961}
962
963void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
964 GpuRegister class_reg) {
965 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
966 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
967 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
968 // TODO: barrier needed?
969 __ Bind(slow_path->GetExitLabel());
970}
971
972void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
973 __ Sync(0); // only stype 0 is supported
974}
975
976void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
977 HBasicBlock* successor) {
978 SuspendCheckSlowPathMIPS64* slow_path =
979 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
980 codegen_->AddSlowPath(slow_path);
981
982 __ LoadFromOffset(kLoadUnsignedHalfword,
983 TMP,
984 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -0700985 Thread::ThreadFlagsOffset<kMips64PointerSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700986 if (successor == nullptr) {
987 __ Bnezc(TMP, slow_path->GetEntryLabel());
988 __ Bind(slow_path->GetReturnLabel());
989 } else {
990 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700991 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700992 // slow_path will return to GetLabelOf(successor).
993 }
994}
995
996InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
997 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800998 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700999 assembler_(codegen->GetAssembler()),
1000 codegen_(codegen) {}
1001
1002void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1003 DCHECK_EQ(instruction->InputCount(), 2U);
1004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1005 Primitive::Type type = instruction->GetResultType();
1006 switch (type) {
1007 case Primitive::kPrimInt:
1008 case Primitive::kPrimLong: {
1009 locations->SetInAt(0, Location::RequiresRegister());
1010 HInstruction* right = instruction->InputAt(1);
1011 bool can_use_imm = false;
1012 if (right->IsConstant()) {
1013 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1014 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1015 can_use_imm = IsUint<16>(imm);
1016 } else if (instruction->IsAdd()) {
1017 can_use_imm = IsInt<16>(imm);
1018 } else {
1019 DCHECK(instruction->IsSub());
1020 can_use_imm = IsInt<16>(-imm);
1021 }
1022 }
1023 if (can_use_imm)
1024 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1025 else
1026 locations->SetInAt(1, Location::RequiresRegister());
1027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1028 }
1029 break;
1030
1031 case Primitive::kPrimFloat:
1032 case Primitive::kPrimDouble:
1033 locations->SetInAt(0, Location::RequiresFpuRegister());
1034 locations->SetInAt(1, Location::RequiresFpuRegister());
1035 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1036 break;
1037
1038 default:
1039 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1040 }
1041}
1042
1043void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1044 Primitive::Type type = instruction->GetType();
1045 LocationSummary* locations = instruction->GetLocations();
1046
1047 switch (type) {
1048 case Primitive::kPrimInt:
1049 case Primitive::kPrimLong: {
1050 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1051 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1052 Location rhs_location = locations->InAt(1);
1053
1054 GpuRegister rhs_reg = ZERO;
1055 int64_t rhs_imm = 0;
1056 bool use_imm = rhs_location.IsConstant();
1057 if (use_imm) {
1058 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1059 } else {
1060 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1061 }
1062
1063 if (instruction->IsAnd()) {
1064 if (use_imm)
1065 __ Andi(dst, lhs, rhs_imm);
1066 else
1067 __ And(dst, lhs, rhs_reg);
1068 } else if (instruction->IsOr()) {
1069 if (use_imm)
1070 __ Ori(dst, lhs, rhs_imm);
1071 else
1072 __ Or(dst, lhs, rhs_reg);
1073 } else if (instruction->IsXor()) {
1074 if (use_imm)
1075 __ Xori(dst, lhs, rhs_imm);
1076 else
1077 __ Xor(dst, lhs, rhs_reg);
1078 } else if (instruction->IsAdd()) {
1079 if (type == Primitive::kPrimInt) {
1080 if (use_imm)
1081 __ Addiu(dst, lhs, rhs_imm);
1082 else
1083 __ Addu(dst, lhs, rhs_reg);
1084 } else {
1085 if (use_imm)
1086 __ Daddiu(dst, lhs, rhs_imm);
1087 else
1088 __ Daddu(dst, lhs, rhs_reg);
1089 }
1090 } else {
1091 DCHECK(instruction->IsSub());
1092 if (type == Primitive::kPrimInt) {
1093 if (use_imm)
1094 __ Addiu(dst, lhs, -rhs_imm);
1095 else
1096 __ Subu(dst, lhs, rhs_reg);
1097 } else {
1098 if (use_imm)
1099 __ Daddiu(dst, lhs, -rhs_imm);
1100 else
1101 __ Dsubu(dst, lhs, rhs_reg);
1102 }
1103 }
1104 break;
1105 }
1106 case Primitive::kPrimFloat:
1107 case Primitive::kPrimDouble: {
1108 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1109 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1110 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1111 if (instruction->IsAdd()) {
1112 if (type == Primitive::kPrimFloat)
1113 __ AddS(dst, lhs, rhs);
1114 else
1115 __ AddD(dst, lhs, rhs);
1116 } else if (instruction->IsSub()) {
1117 if (type == Primitive::kPrimFloat)
1118 __ SubS(dst, lhs, rhs);
1119 else
1120 __ SubD(dst, lhs, rhs);
1121 } else {
1122 LOG(FATAL) << "Unexpected floating-point binary operation";
1123 }
1124 break;
1125 }
1126 default:
1127 LOG(FATAL) << "Unexpected binary operation type " << type;
1128 }
1129}
1130
1131void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001132 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001133
1134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1135 Primitive::Type type = instr->GetResultType();
1136 switch (type) {
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimLong: {
1139 locations->SetInAt(0, Location::RequiresRegister());
1140 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001142 break;
1143 }
1144 default:
1145 LOG(FATAL) << "Unexpected shift type " << type;
1146 }
1147}
1148
1149void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001150 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001151 LocationSummary* locations = instr->GetLocations();
1152 Primitive::Type type = instr->GetType();
1153
1154 switch (type) {
1155 case Primitive::kPrimInt:
1156 case Primitive::kPrimLong: {
1157 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1158 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1159 Location rhs_location = locations->InAt(1);
1160
1161 GpuRegister rhs_reg = ZERO;
1162 int64_t rhs_imm = 0;
1163 bool use_imm = rhs_location.IsConstant();
1164 if (use_imm) {
1165 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1166 } else {
1167 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1168 }
1169
1170 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001171 uint32_t shift_value = rhs_imm &
1172 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173
Alexey Frunze92d90602015-12-18 18:16:36 -08001174 if (shift_value == 0) {
1175 if (dst != lhs) {
1176 __ Move(dst, lhs);
1177 }
1178 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001179 if (instr->IsShl()) {
1180 __ Sll(dst, lhs, shift_value);
1181 } else if (instr->IsShr()) {
1182 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001183 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001184 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001185 } else {
1186 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001187 }
1188 } else {
1189 if (shift_value < 32) {
1190 if (instr->IsShl()) {
1191 __ Dsll(dst, lhs, shift_value);
1192 } else if (instr->IsShr()) {
1193 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001194 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001196 } else {
1197 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001198 }
1199 } else {
1200 shift_value -= 32;
1201 if (instr->IsShl()) {
1202 __ Dsll32(dst, lhs, shift_value);
1203 } else if (instr->IsShr()) {
1204 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001205 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001206 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001207 } else {
1208 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001209 }
1210 }
1211 }
1212 } else {
1213 if (type == Primitive::kPrimInt) {
1214 if (instr->IsShl()) {
1215 __ Sllv(dst, lhs, rhs_reg);
1216 } else if (instr->IsShr()) {
1217 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001218 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001220 } else {
1221 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 }
1223 } else {
1224 if (instr->IsShl()) {
1225 __ Dsllv(dst, lhs, rhs_reg);
1226 } else if (instr->IsShr()) {
1227 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001228 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001229 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001230 } else {
1231 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001232 }
1233 }
1234 }
1235 break;
1236 }
1237 default:
1238 LOG(FATAL) << "Unexpected shift operation type " << type;
1239 }
1240}
1241
1242void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1243 HandleBinaryOp(instruction);
1244}
1245
1246void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1247 HandleBinaryOp(instruction);
1248}
1249
1250void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1251 HandleBinaryOp(instruction);
1252}
1253
1254void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1255 HandleBinaryOp(instruction);
1256}
1257
1258void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1259 LocationSummary* locations =
1260 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1261 locations->SetInAt(0, Location::RequiresRegister());
1262 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1263 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1264 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1265 } else {
1266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1267 }
1268}
1269
1270void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1271 LocationSummary* locations = instruction->GetLocations();
1272 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1273 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001274 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001275
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001276 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001277 switch (type) {
1278 case Primitive::kPrimBoolean: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001279 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1280 if (index.IsConstant()) {
1281 size_t offset =
1282 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1283 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1284 } else {
1285 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1286 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1287 }
1288 break;
1289 }
1290
1291 case Primitive::kPrimByte: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001292 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1293 if (index.IsConstant()) {
1294 size_t offset =
1295 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1296 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1297 } else {
1298 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1299 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1300 }
1301 break;
1302 }
1303
1304 case Primitive::kPrimShort: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001305 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1306 if (index.IsConstant()) {
1307 size_t offset =
1308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1309 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1310 } else {
1311 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1312 __ Daddu(TMP, obj, TMP);
1313 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1314 }
1315 break;
1316 }
1317
1318 case Primitive::kPrimChar: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001319 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1320 if (index.IsConstant()) {
1321 size_t offset =
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1323 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1324 } else {
1325 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1326 __ Daddu(TMP, obj, TMP);
1327 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1328 }
1329 break;
1330 }
1331
1332 case Primitive::kPrimInt:
1333 case Primitive::kPrimNot: {
1334 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001335 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1336 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1337 if (index.IsConstant()) {
1338 size_t offset =
1339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1340 __ LoadFromOffset(load_type, out, obj, offset);
1341 } else {
1342 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1343 __ Daddu(TMP, obj, TMP);
1344 __ LoadFromOffset(load_type, out, TMP, data_offset);
1345 }
1346 break;
1347 }
1348
1349 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001350 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1351 if (index.IsConstant()) {
1352 size_t offset =
1353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1354 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1355 } else {
1356 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1357 __ Daddu(TMP, obj, TMP);
1358 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1359 }
1360 break;
1361 }
1362
1363 case Primitive::kPrimFloat: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001364 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1365 if (index.IsConstant()) {
1366 size_t offset =
1367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1368 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1369 } else {
1370 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1371 __ Daddu(TMP, obj, TMP);
1372 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1373 }
1374 break;
1375 }
1376
1377 case Primitive::kPrimDouble: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001378 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1379 if (index.IsConstant()) {
1380 size_t offset =
1381 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1382 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1383 } else {
1384 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1385 __ Daddu(TMP, obj, TMP);
1386 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1387 }
1388 break;
1389 }
1390
1391 case Primitive::kPrimVoid:
1392 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1393 UNREACHABLE();
1394 }
1395 codegen_->MaybeRecordImplicitNullCheck(instruction);
1396}
1397
1398void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1400 locations->SetInAt(0, Location::RequiresRegister());
1401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1402}
1403
1404void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1405 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001406 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001407 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1408 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1409 __ LoadFromOffset(kLoadWord, out, obj, offset);
1410 codegen_->MaybeRecordImplicitNullCheck(instruction);
1411}
1412
1413void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001414 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1416 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001417 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
David Brazdilbb3d5052015-09-21 18:39:16 +01001418 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001419 InvokeRuntimeCallingConvention calling_convention;
1420 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1421 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1422 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1423 } else {
1424 locations->SetInAt(0, Location::RequiresRegister());
1425 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1426 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1427 locations->SetInAt(2, Location::RequiresFpuRegister());
1428 } else {
1429 locations->SetInAt(2, Location::RequiresRegister());
1430 }
1431 }
1432}
1433
1434void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1435 LocationSummary* locations = instruction->GetLocations();
1436 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1437 Location index = locations->InAt(1);
1438 Primitive::Type value_type = instruction->GetComponentType();
1439 bool needs_runtime_call = locations->WillCall();
1440 bool needs_write_barrier =
1441 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1442
1443 switch (value_type) {
1444 case Primitive::kPrimBoolean:
1445 case Primitive::kPrimByte: {
1446 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1447 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1448 if (index.IsConstant()) {
1449 size_t offset =
1450 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1451 __ StoreToOffset(kStoreByte, value, obj, offset);
1452 } else {
1453 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1454 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1455 }
1456 break;
1457 }
1458
1459 case Primitive::kPrimShort:
1460 case Primitive::kPrimChar: {
1461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1462 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1463 if (index.IsConstant()) {
1464 size_t offset =
1465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1466 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1467 } else {
1468 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1469 __ Daddu(TMP, obj, TMP);
1470 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1471 }
1472 break;
1473 }
1474
1475 case Primitive::kPrimInt:
1476 case Primitive::kPrimNot: {
1477 if (!needs_runtime_call) {
1478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1479 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1480 if (index.IsConstant()) {
1481 size_t offset =
1482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1483 __ StoreToOffset(kStoreWord, value, obj, offset);
1484 } else {
1485 DCHECK(index.IsRegister()) << index;
1486 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1487 __ Daddu(TMP, obj, TMP);
1488 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1489 }
1490 codegen_->MaybeRecordImplicitNullCheck(instruction);
1491 if (needs_write_barrier) {
1492 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001493 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001494 }
1495 } else {
1496 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufc734082016-07-19 17:18:07 +01001497 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00001498 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001499 }
1500 break;
1501 }
1502
1503 case Primitive::kPrimLong: {
1504 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1505 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1506 if (index.IsConstant()) {
1507 size_t offset =
1508 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1509 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1510 } else {
1511 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1512 __ Daddu(TMP, obj, TMP);
1513 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1514 }
1515 break;
1516 }
1517
1518 case Primitive::kPrimFloat: {
1519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1520 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1521 DCHECK(locations->InAt(2).IsFpuRegister());
1522 if (index.IsConstant()) {
1523 size_t offset =
1524 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1525 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1526 } else {
1527 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1528 __ Daddu(TMP, obj, TMP);
1529 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1530 }
1531 break;
1532 }
1533
1534 case Primitive::kPrimDouble: {
1535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1536 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1537 DCHECK(locations->InAt(2).IsFpuRegister());
1538 if (index.IsConstant()) {
1539 size_t offset =
1540 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1541 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1542 } else {
1543 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1544 __ Daddu(TMP, obj, TMP);
1545 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1546 }
1547 break;
1548 }
1549
1550 case Primitive::kPrimVoid:
1551 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1552 UNREACHABLE();
1553 }
1554
1555 // Ints and objects are handled in the switch.
1556 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1557 codegen_->MaybeRecordImplicitNullCheck(instruction);
1558 }
1559}
1560
1561void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001562 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1563 ? LocationSummary::kCallOnSlowPath
1564 : LocationSummary::kNoCall;
1565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001566 locations->SetInAt(0, Location::RequiresRegister());
1567 locations->SetInAt(1, Location::RequiresRegister());
1568 if (instruction->HasUses()) {
1569 locations->SetOut(Location::SameAsFirstInput());
1570 }
1571}
1572
1573void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1574 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001575 BoundsCheckSlowPathMIPS64* slow_path =
1576 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001577 codegen_->AddSlowPath(slow_path);
1578
1579 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1580 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1581
1582 // length is limited by the maximum positive signed 32-bit integer.
1583 // Unsigned comparison of length and index checks for index < 0
1584 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001585 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001586}
1587
1588void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1590 instruction,
1591 LocationSummary::kCallOnSlowPath);
1592 locations->SetInAt(0, Location::RequiresRegister());
1593 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001594 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001595 locations->AddTemp(Location::RequiresRegister());
1596}
1597
1598void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1599 LocationSummary* locations = instruction->GetLocations();
1600 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1601 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1602 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1603
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001604 SlowPathCodeMIPS64* slow_path =
1605 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001606 codegen_->AddSlowPath(slow_path);
1607
1608 // TODO: avoid this check if we know obj is not null.
1609 __ Beqzc(obj, slow_path->GetExitLabel());
1610 // Compare the class of `obj` with `cls`.
1611 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1612 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1613 __ Bind(slow_path->GetExitLabel());
1614}
1615
1616void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1617 LocationSummary* locations =
1618 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1619 locations->SetInAt(0, Location::RequiresRegister());
1620 if (check->HasUses()) {
1621 locations->SetOut(Location::SameAsFirstInput());
1622 }
1623}
1624
1625void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1626 // We assume the class is not null.
1627 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1628 check->GetLoadClass(),
1629 check,
1630 check->GetDexPc(),
1631 true);
1632 codegen_->AddSlowPath(slow_path);
1633 GenerateClassInitializationCheck(slow_path,
1634 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1635}
1636
1637void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1638 Primitive::Type in_type = compare->InputAt(0)->GetType();
1639
Alexey Frunze299a9392015-12-08 16:08:02 -08001640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001641
1642 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001643 case Primitive::kPrimBoolean:
1644 case Primitive::kPrimByte:
1645 case Primitive::kPrimShort:
1646 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001647 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001648 case Primitive::kPrimLong:
1649 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001650 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1652 break;
1653
1654 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001655 case Primitive::kPrimDouble:
1656 locations->SetInAt(0, Location::RequiresFpuRegister());
1657 locations->SetInAt(1, Location::RequiresFpuRegister());
1658 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001659 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001660
1661 default:
1662 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1663 }
1664}
1665
1666void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1667 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001668 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001669 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1670
1671 // 0 if: left == right
1672 // 1 if: left > right
1673 // -1 if: left < right
1674 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001675 case Primitive::kPrimBoolean:
1676 case Primitive::kPrimByte:
1677 case Primitive::kPrimShort:
1678 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001679 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001680 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001681 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001682 Location rhs_location = locations->InAt(1);
1683 bool use_imm = rhs_location.IsConstant();
1684 GpuRegister rhs = ZERO;
1685 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001686 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001687 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1688 if (value != 0) {
1689 rhs = AT;
1690 __ LoadConst64(rhs, value);
1691 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001692 } else {
1693 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1694 if (value != 0) {
1695 rhs = AT;
1696 __ LoadConst32(rhs, value);
1697 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001698 }
1699 } else {
1700 rhs = rhs_location.AsRegister<GpuRegister>();
1701 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001702 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001703 __ Slt(res, rhs, lhs);
1704 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705 break;
1706 }
1707
Alexey Frunze299a9392015-12-08 16:08:02 -08001708 case Primitive::kPrimFloat: {
1709 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1710 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1711 Mips64Label done;
1712 __ CmpEqS(FTMP, lhs, rhs);
1713 __ LoadConst32(res, 0);
1714 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001715 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001716 __ CmpLtS(FTMP, lhs, rhs);
1717 __ LoadConst32(res, -1);
1718 __ Bc1nez(FTMP, &done);
1719 __ LoadConst32(res, 1);
1720 } else {
1721 __ CmpLtS(FTMP, rhs, lhs);
1722 __ LoadConst32(res, 1);
1723 __ Bc1nez(FTMP, &done);
1724 __ LoadConst32(res, -1);
1725 }
1726 __ Bind(&done);
1727 break;
1728 }
1729
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001731 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1732 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1733 Mips64Label done;
1734 __ CmpEqD(FTMP, lhs, rhs);
1735 __ LoadConst32(res, 0);
1736 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001737 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001738 __ CmpLtD(FTMP, lhs, rhs);
1739 __ LoadConst32(res, -1);
1740 __ Bc1nez(FTMP, &done);
1741 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001742 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001743 __ CmpLtD(FTMP, rhs, lhs);
1744 __ LoadConst32(res, 1);
1745 __ Bc1nez(FTMP, &done);
1746 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001747 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001748 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001749 break;
1750 }
1751
1752 default:
1753 LOG(FATAL) << "Unimplemented compare type " << in_type;
1754 }
1755}
1756
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001759 switch (instruction->InputAt(0)->GetType()) {
1760 default:
1761 case Primitive::kPrimLong:
1762 locations->SetInAt(0, Location::RequiresRegister());
1763 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1764 break;
1765
1766 case Primitive::kPrimFloat:
1767 case Primitive::kPrimDouble:
1768 locations->SetInAt(0, Location::RequiresFpuRegister());
1769 locations->SetInAt(1, Location::RequiresFpuRegister());
1770 break;
1771 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001772 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1774 }
1775}
1776
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001778 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001779 return;
1780 }
1781
Alexey Frunze299a9392015-12-08 16:08:02 -08001782 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001785 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001786
Alexey Frunze299a9392015-12-08 16:08:02 -08001787 switch (type) {
1788 default:
1789 // Integer case.
1790 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1791 return;
1792 case Primitive::kPrimLong:
1793 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1794 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001795
Alexey Frunze299a9392015-12-08 16:08:02 -08001796 case Primitive::kPrimFloat:
1797 case Primitive::kPrimDouble:
1798 // TODO: don't use branches.
1799 GenerateFpCompareAndBranch(instruction->GetCondition(),
1800 instruction->IsGtBias(),
1801 type,
1802 locations,
1803 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001804 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001805 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001806
1807 // Convert the branches into the result.
1808 Mips64Label done;
1809
1810 // False case: result = 0.
1811 __ LoadConst32(dst, 0);
1812 __ Bc(&done);
1813
1814 // True case: result = 1.
1815 __ Bind(&true_label);
1816 __ LoadConst32(dst, 1);
1817 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001818}
1819
Alexey Frunzec857c742015-09-23 15:12:39 -07001820void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1821 DCHECK(instruction->IsDiv() || instruction->IsRem());
1822 Primitive::Type type = instruction->GetResultType();
1823
1824 LocationSummary* locations = instruction->GetLocations();
1825 Location second = locations->InAt(1);
1826 DCHECK(second.IsConstant());
1827
1828 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1829 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1830 int64_t imm = Int64FromConstant(second.GetConstant());
1831 DCHECK(imm == 1 || imm == -1);
1832
1833 if (instruction->IsRem()) {
1834 __ Move(out, ZERO);
1835 } else {
1836 if (imm == -1) {
1837 if (type == Primitive::kPrimInt) {
1838 __ Subu(out, ZERO, dividend);
1839 } else {
1840 DCHECK_EQ(type, Primitive::kPrimLong);
1841 __ Dsubu(out, ZERO, dividend);
1842 }
1843 } else if (out != dividend) {
1844 __ Move(out, dividend);
1845 }
1846 }
1847}
1848
1849void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1850 DCHECK(instruction->IsDiv() || instruction->IsRem());
1851 Primitive::Type type = instruction->GetResultType();
1852
1853 LocationSummary* locations = instruction->GetLocations();
1854 Location second = locations->InAt(1);
1855 DCHECK(second.IsConstant());
1856
1857 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1858 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1859 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001860 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001861 int ctz_imm = CTZ(abs_imm);
1862
1863 if (instruction->IsDiv()) {
1864 if (type == Primitive::kPrimInt) {
1865 if (ctz_imm == 1) {
1866 // Fast path for division by +/-2, which is very common.
1867 __ Srl(TMP, dividend, 31);
1868 } else {
1869 __ Sra(TMP, dividend, 31);
1870 __ Srl(TMP, TMP, 32 - ctz_imm);
1871 }
1872 __ Addu(out, dividend, TMP);
1873 __ Sra(out, out, ctz_imm);
1874 if (imm < 0) {
1875 __ Subu(out, ZERO, out);
1876 }
1877 } else {
1878 DCHECK_EQ(type, Primitive::kPrimLong);
1879 if (ctz_imm == 1) {
1880 // Fast path for division by +/-2, which is very common.
1881 __ Dsrl32(TMP, dividend, 31);
1882 } else {
1883 __ Dsra32(TMP, dividend, 31);
1884 if (ctz_imm > 32) {
1885 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1886 } else {
1887 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1888 }
1889 }
1890 __ Daddu(out, dividend, TMP);
1891 if (ctz_imm < 32) {
1892 __ Dsra(out, out, ctz_imm);
1893 } else {
1894 __ Dsra32(out, out, ctz_imm - 32);
1895 }
1896 if (imm < 0) {
1897 __ Dsubu(out, ZERO, out);
1898 }
1899 }
1900 } else {
1901 if (type == Primitive::kPrimInt) {
1902 if (ctz_imm == 1) {
1903 // Fast path for modulo +/-2, which is very common.
1904 __ Sra(TMP, dividend, 31);
1905 __ Subu(out, dividend, TMP);
1906 __ Andi(out, out, 1);
1907 __ Addu(out, out, TMP);
1908 } else {
1909 __ Sra(TMP, dividend, 31);
1910 __ Srl(TMP, TMP, 32 - ctz_imm);
1911 __ Addu(out, dividend, TMP);
1912 if (IsUint<16>(abs_imm - 1)) {
1913 __ Andi(out, out, abs_imm - 1);
1914 } else {
1915 __ Sll(out, out, 32 - ctz_imm);
1916 __ Srl(out, out, 32 - ctz_imm);
1917 }
1918 __ Subu(out, out, TMP);
1919 }
1920 } else {
1921 DCHECK_EQ(type, Primitive::kPrimLong);
1922 if (ctz_imm == 1) {
1923 // Fast path for modulo +/-2, which is very common.
1924 __ Dsra32(TMP, dividend, 31);
1925 __ Dsubu(out, dividend, TMP);
1926 __ Andi(out, out, 1);
1927 __ Daddu(out, out, TMP);
1928 } else {
1929 __ Dsra32(TMP, dividend, 31);
1930 if (ctz_imm > 32) {
1931 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1932 } else {
1933 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1934 }
1935 __ Daddu(out, dividend, TMP);
1936 if (IsUint<16>(abs_imm - 1)) {
1937 __ Andi(out, out, abs_imm - 1);
1938 } else {
1939 if (ctz_imm > 32) {
1940 __ Dsll(out, out, 64 - ctz_imm);
1941 __ Dsrl(out, out, 64 - ctz_imm);
1942 } else {
1943 __ Dsll32(out, out, 32 - ctz_imm);
1944 __ Dsrl32(out, out, 32 - ctz_imm);
1945 }
1946 }
1947 __ Dsubu(out, out, TMP);
1948 }
1949 }
1950 }
1951}
1952
1953void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1954 DCHECK(instruction->IsDiv() || instruction->IsRem());
1955
1956 LocationSummary* locations = instruction->GetLocations();
1957 Location second = locations->InAt(1);
1958 DCHECK(second.IsConstant());
1959
1960 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1961 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1962 int64_t imm = Int64FromConstant(second.GetConstant());
1963
1964 Primitive::Type type = instruction->GetResultType();
1965 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1966
1967 int64_t magic;
1968 int shift;
1969 CalculateMagicAndShiftForDivRem(imm,
1970 (type == Primitive::kPrimLong),
1971 &magic,
1972 &shift);
1973
1974 if (type == Primitive::kPrimInt) {
1975 __ LoadConst32(TMP, magic);
1976 __ MuhR6(TMP, dividend, TMP);
1977
1978 if (imm > 0 && magic < 0) {
1979 __ Addu(TMP, TMP, dividend);
1980 } else if (imm < 0 && magic > 0) {
1981 __ Subu(TMP, TMP, dividend);
1982 }
1983
1984 if (shift != 0) {
1985 __ Sra(TMP, TMP, shift);
1986 }
1987
1988 if (instruction->IsDiv()) {
1989 __ Sra(out, TMP, 31);
1990 __ Subu(out, TMP, out);
1991 } else {
1992 __ Sra(AT, TMP, 31);
1993 __ Subu(AT, TMP, AT);
1994 __ LoadConst32(TMP, imm);
1995 __ MulR6(TMP, AT, TMP);
1996 __ Subu(out, dividend, TMP);
1997 }
1998 } else {
1999 __ LoadConst64(TMP, magic);
2000 __ Dmuh(TMP, dividend, TMP);
2001
2002 if (imm > 0 && magic < 0) {
2003 __ Daddu(TMP, TMP, dividend);
2004 } else if (imm < 0 && magic > 0) {
2005 __ Dsubu(TMP, TMP, dividend);
2006 }
2007
2008 if (shift >= 32) {
2009 __ Dsra32(TMP, TMP, shift - 32);
2010 } else if (shift > 0) {
2011 __ Dsra(TMP, TMP, shift);
2012 }
2013
2014 if (instruction->IsDiv()) {
2015 __ Dsra32(out, TMP, 31);
2016 __ Dsubu(out, TMP, out);
2017 } else {
2018 __ Dsra32(AT, TMP, 31);
2019 __ Dsubu(AT, TMP, AT);
2020 __ LoadConst64(TMP, imm);
2021 __ Dmul(TMP, AT, TMP);
2022 __ Dsubu(out, dividend, TMP);
2023 }
2024 }
2025}
2026
2027void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2028 DCHECK(instruction->IsDiv() || instruction->IsRem());
2029 Primitive::Type type = instruction->GetResultType();
2030 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2031
2032 LocationSummary* locations = instruction->GetLocations();
2033 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2034 Location second = locations->InAt(1);
2035
2036 if (second.IsConstant()) {
2037 int64_t imm = Int64FromConstant(second.GetConstant());
2038 if (imm == 0) {
2039 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2040 } else if (imm == 1 || imm == -1) {
2041 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002042 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002043 DivRemByPowerOfTwo(instruction);
2044 } else {
2045 DCHECK(imm <= -2 || imm >= 2);
2046 GenerateDivRemWithAnyConstant(instruction);
2047 }
2048 } else {
2049 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2050 GpuRegister divisor = second.AsRegister<GpuRegister>();
2051 if (instruction->IsDiv()) {
2052 if (type == Primitive::kPrimInt)
2053 __ DivR6(out, dividend, divisor);
2054 else
2055 __ Ddiv(out, dividend, divisor);
2056 } else {
2057 if (type == Primitive::kPrimInt)
2058 __ ModR6(out, dividend, divisor);
2059 else
2060 __ Dmod(out, dividend, divisor);
2061 }
2062 }
2063}
2064
Alexey Frunze4dda3372015-06-01 18:31:49 -07002065void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2068 switch (div->GetResultType()) {
2069 case Primitive::kPrimInt:
2070 case Primitive::kPrimLong:
2071 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002072 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2074 break;
2075
2076 case Primitive::kPrimFloat:
2077 case Primitive::kPrimDouble:
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetInAt(1, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2081 break;
2082
2083 default:
2084 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2085 }
2086}
2087
2088void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2089 Primitive::Type type = instruction->GetType();
2090 LocationSummary* locations = instruction->GetLocations();
2091
2092 switch (type) {
2093 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002094 case Primitive::kPrimLong:
2095 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002096 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002097 case Primitive::kPrimFloat:
2098 case Primitive::kPrimDouble: {
2099 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2100 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2101 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2102 if (type == Primitive::kPrimFloat)
2103 __ DivS(dst, lhs, rhs);
2104 else
2105 __ DivD(dst, lhs, rhs);
2106 break;
2107 }
2108 default:
2109 LOG(FATAL) << "Unexpected div type " << type;
2110 }
2111}
2112
2113void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002114 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2115 ? LocationSummary::kCallOnSlowPath
2116 : LocationSummary::kNoCall;
2117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002118 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2119 if (instruction->HasUses()) {
2120 locations->SetOut(Location::SameAsFirstInput());
2121 }
2122}
2123
2124void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2125 SlowPathCodeMIPS64* slow_path =
2126 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2127 codegen_->AddSlowPath(slow_path);
2128 Location value = instruction->GetLocations()->InAt(0);
2129
2130 Primitive::Type type = instruction->GetType();
2131
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002132 if (!Primitive::IsIntegralType(type)) {
2133 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002134 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002135 }
2136
2137 if (value.IsConstant()) {
2138 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2139 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002140 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002141 } else {
2142 // A division by a non-null constant is valid. We don't need to perform
2143 // any check, so simply fall through.
2144 }
2145 } else {
2146 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2147 }
2148}
2149
2150void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2151 LocationSummary* locations =
2152 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2153 locations->SetOut(Location::ConstantLocation(constant));
2154}
2155
2156void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2157 // Will be generated at use site.
2158}
2159
2160void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2161 exit->SetLocations(nullptr);
2162}
2163
2164void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2165}
2166
2167void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2168 LocationSummary* locations =
2169 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2170 locations->SetOut(Location::ConstantLocation(constant));
2171}
2172
2173void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2174 // Will be generated at use site.
2175}
2176
David Brazdilfc6a86a2015-06-26 10:33:45 +00002177void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002178 DCHECK(!successor->IsExitBlock());
2179 HBasicBlock* block = got->GetBlock();
2180 HInstruction* previous = got->GetPrevious();
2181 HLoopInformation* info = block->GetLoopInformation();
2182
2183 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2184 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2185 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2186 return;
2187 }
2188 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2189 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2190 }
2191 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002192 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002193 }
2194}
2195
David Brazdilfc6a86a2015-06-26 10:33:45 +00002196void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2197 got->SetLocations(nullptr);
2198}
2199
2200void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2201 HandleGoto(got, got->GetSuccessor());
2202}
2203
2204void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2205 try_boundary->SetLocations(nullptr);
2206}
2207
2208void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2209 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2210 if (!successor->IsExitBlock()) {
2211 HandleGoto(try_boundary, successor);
2212 }
2213}
2214
Alexey Frunze299a9392015-12-08 16:08:02 -08002215void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2216 bool is64bit,
2217 LocationSummary* locations) {
2218 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2219 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2220 Location rhs_location = locations->InAt(1);
2221 GpuRegister rhs_reg = ZERO;
2222 int64_t rhs_imm = 0;
2223 bool use_imm = rhs_location.IsConstant();
2224 if (use_imm) {
2225 if (is64bit) {
2226 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2227 } else {
2228 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2229 }
2230 } else {
2231 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2232 }
2233 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2234
2235 switch (cond) {
2236 case kCondEQ:
2237 case kCondNE:
2238 if (use_imm && IsUint<16>(rhs_imm)) {
2239 __ Xori(dst, lhs, rhs_imm);
2240 } else {
2241 if (use_imm) {
2242 rhs_reg = TMP;
2243 __ LoadConst64(rhs_reg, rhs_imm);
2244 }
2245 __ Xor(dst, lhs, rhs_reg);
2246 }
2247 if (cond == kCondEQ) {
2248 __ Sltiu(dst, dst, 1);
2249 } else {
2250 __ Sltu(dst, ZERO, dst);
2251 }
2252 break;
2253
2254 case kCondLT:
2255 case kCondGE:
2256 if (use_imm && IsInt<16>(rhs_imm)) {
2257 __ Slti(dst, lhs, rhs_imm);
2258 } else {
2259 if (use_imm) {
2260 rhs_reg = TMP;
2261 __ LoadConst64(rhs_reg, rhs_imm);
2262 }
2263 __ Slt(dst, lhs, rhs_reg);
2264 }
2265 if (cond == kCondGE) {
2266 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2267 // only the slt instruction but no sge.
2268 __ Xori(dst, dst, 1);
2269 }
2270 break;
2271
2272 case kCondLE:
2273 case kCondGT:
2274 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2275 // Simulate lhs <= rhs via lhs < rhs + 1.
2276 __ Slti(dst, lhs, rhs_imm_plus_one);
2277 if (cond == kCondGT) {
2278 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2279 // only the slti instruction but no sgti.
2280 __ Xori(dst, dst, 1);
2281 }
2282 } else {
2283 if (use_imm) {
2284 rhs_reg = TMP;
2285 __ LoadConst64(rhs_reg, rhs_imm);
2286 }
2287 __ Slt(dst, rhs_reg, lhs);
2288 if (cond == kCondLE) {
2289 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2290 // only the slt instruction but no sle.
2291 __ Xori(dst, dst, 1);
2292 }
2293 }
2294 break;
2295
2296 case kCondB:
2297 case kCondAE:
2298 if (use_imm && IsInt<16>(rhs_imm)) {
2299 // Sltiu sign-extends its 16-bit immediate operand before
2300 // the comparison and thus lets us compare directly with
2301 // unsigned values in the ranges [0, 0x7fff] and
2302 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2303 __ Sltiu(dst, lhs, rhs_imm);
2304 } else {
2305 if (use_imm) {
2306 rhs_reg = TMP;
2307 __ LoadConst64(rhs_reg, rhs_imm);
2308 }
2309 __ Sltu(dst, lhs, rhs_reg);
2310 }
2311 if (cond == kCondAE) {
2312 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2313 // only the sltu instruction but no sgeu.
2314 __ Xori(dst, dst, 1);
2315 }
2316 break;
2317
2318 case kCondBE:
2319 case kCondA:
2320 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2321 // Simulate lhs <= rhs via lhs < rhs + 1.
2322 // Note that this only works if rhs + 1 does not overflow
2323 // to 0, hence the check above.
2324 // Sltiu sign-extends its 16-bit immediate operand before
2325 // the comparison and thus lets us compare directly with
2326 // unsigned values in the ranges [0, 0x7fff] and
2327 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2328 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2329 if (cond == kCondA) {
2330 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2331 // only the sltiu instruction but no sgtiu.
2332 __ Xori(dst, dst, 1);
2333 }
2334 } else {
2335 if (use_imm) {
2336 rhs_reg = TMP;
2337 __ LoadConst64(rhs_reg, rhs_imm);
2338 }
2339 __ Sltu(dst, rhs_reg, lhs);
2340 if (cond == kCondBE) {
2341 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2342 // only the sltu instruction but no sleu.
2343 __ Xori(dst, dst, 1);
2344 }
2345 }
2346 break;
2347 }
2348}
2349
2350void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2351 bool is64bit,
2352 LocationSummary* locations,
2353 Mips64Label* label) {
2354 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2355 Location rhs_location = locations->InAt(1);
2356 GpuRegister rhs_reg = ZERO;
2357 int64_t rhs_imm = 0;
2358 bool use_imm = rhs_location.IsConstant();
2359 if (use_imm) {
2360 if (is64bit) {
2361 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2362 } else {
2363 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2364 }
2365 } else {
2366 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2367 }
2368
2369 if (use_imm && rhs_imm == 0) {
2370 switch (cond) {
2371 case kCondEQ:
2372 case kCondBE: // <= 0 if zero
2373 __ Beqzc(lhs, label);
2374 break;
2375 case kCondNE:
2376 case kCondA: // > 0 if non-zero
2377 __ Bnezc(lhs, label);
2378 break;
2379 case kCondLT:
2380 __ Bltzc(lhs, label);
2381 break;
2382 case kCondGE:
2383 __ Bgezc(lhs, label);
2384 break;
2385 case kCondLE:
2386 __ Blezc(lhs, label);
2387 break;
2388 case kCondGT:
2389 __ Bgtzc(lhs, label);
2390 break;
2391 case kCondB: // always false
2392 break;
2393 case kCondAE: // always true
2394 __ Bc(label);
2395 break;
2396 }
2397 } else {
2398 if (use_imm) {
2399 rhs_reg = TMP;
2400 __ LoadConst64(rhs_reg, rhs_imm);
2401 }
2402 switch (cond) {
2403 case kCondEQ:
2404 __ Beqc(lhs, rhs_reg, label);
2405 break;
2406 case kCondNE:
2407 __ Bnec(lhs, rhs_reg, label);
2408 break;
2409 case kCondLT:
2410 __ Bltc(lhs, rhs_reg, label);
2411 break;
2412 case kCondGE:
2413 __ Bgec(lhs, rhs_reg, label);
2414 break;
2415 case kCondLE:
2416 __ Bgec(rhs_reg, lhs, label);
2417 break;
2418 case kCondGT:
2419 __ Bltc(rhs_reg, lhs, label);
2420 break;
2421 case kCondB:
2422 __ Bltuc(lhs, rhs_reg, label);
2423 break;
2424 case kCondAE:
2425 __ Bgeuc(lhs, rhs_reg, label);
2426 break;
2427 case kCondBE:
2428 __ Bgeuc(rhs_reg, lhs, label);
2429 break;
2430 case kCondA:
2431 __ Bltuc(rhs_reg, lhs, label);
2432 break;
2433 }
2434 }
2435}
2436
2437void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2438 bool gt_bias,
2439 Primitive::Type type,
2440 LocationSummary* locations,
2441 Mips64Label* label) {
2442 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2443 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2444 if (type == Primitive::kPrimFloat) {
2445 switch (cond) {
2446 case kCondEQ:
2447 __ CmpEqS(FTMP, lhs, rhs);
2448 __ Bc1nez(FTMP, label);
2449 break;
2450 case kCondNE:
2451 __ CmpEqS(FTMP, lhs, rhs);
2452 __ Bc1eqz(FTMP, label);
2453 break;
2454 case kCondLT:
2455 if (gt_bias) {
2456 __ CmpLtS(FTMP, lhs, rhs);
2457 } else {
2458 __ CmpUltS(FTMP, lhs, rhs);
2459 }
2460 __ Bc1nez(FTMP, label);
2461 break;
2462 case kCondLE:
2463 if (gt_bias) {
2464 __ CmpLeS(FTMP, lhs, rhs);
2465 } else {
2466 __ CmpUleS(FTMP, lhs, rhs);
2467 }
2468 __ Bc1nez(FTMP, label);
2469 break;
2470 case kCondGT:
2471 if (gt_bias) {
2472 __ CmpUltS(FTMP, rhs, lhs);
2473 } else {
2474 __ CmpLtS(FTMP, rhs, lhs);
2475 }
2476 __ Bc1nez(FTMP, label);
2477 break;
2478 case kCondGE:
2479 if (gt_bias) {
2480 __ CmpUleS(FTMP, rhs, lhs);
2481 } else {
2482 __ CmpLeS(FTMP, rhs, lhs);
2483 }
2484 __ Bc1nez(FTMP, label);
2485 break;
2486 default:
2487 LOG(FATAL) << "Unexpected non-floating-point condition";
2488 }
2489 } else {
2490 DCHECK_EQ(type, Primitive::kPrimDouble);
2491 switch (cond) {
2492 case kCondEQ:
2493 __ CmpEqD(FTMP, lhs, rhs);
2494 __ Bc1nez(FTMP, label);
2495 break;
2496 case kCondNE:
2497 __ CmpEqD(FTMP, lhs, rhs);
2498 __ Bc1eqz(FTMP, label);
2499 break;
2500 case kCondLT:
2501 if (gt_bias) {
2502 __ CmpLtD(FTMP, lhs, rhs);
2503 } else {
2504 __ CmpUltD(FTMP, lhs, rhs);
2505 }
2506 __ Bc1nez(FTMP, label);
2507 break;
2508 case kCondLE:
2509 if (gt_bias) {
2510 __ CmpLeD(FTMP, lhs, rhs);
2511 } else {
2512 __ CmpUleD(FTMP, lhs, rhs);
2513 }
2514 __ Bc1nez(FTMP, label);
2515 break;
2516 case kCondGT:
2517 if (gt_bias) {
2518 __ CmpUltD(FTMP, rhs, lhs);
2519 } else {
2520 __ CmpLtD(FTMP, rhs, lhs);
2521 }
2522 __ Bc1nez(FTMP, label);
2523 break;
2524 case kCondGE:
2525 if (gt_bias) {
2526 __ CmpUleD(FTMP, rhs, lhs);
2527 } else {
2528 __ CmpLeD(FTMP, rhs, lhs);
2529 }
2530 __ Bc1nez(FTMP, label);
2531 break;
2532 default:
2533 LOG(FATAL) << "Unexpected non-floating-point condition";
2534 }
2535 }
2536}
2537
Alexey Frunze4dda3372015-06-01 18:31:49 -07002538void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002539 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002540 Mips64Label* true_target,
2541 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002542 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002543
David Brazdil0debae72015-11-12 18:37:00 +00002544 if (true_target == nullptr && false_target == nullptr) {
2545 // Nothing to do. The code always falls through.
2546 return;
2547 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002548 // Constant condition, statically compared against "true" (integer value 1).
2549 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002550 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002551 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002552 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002553 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002554 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002555 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002556 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002557 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002558 }
David Brazdil0debae72015-11-12 18:37:00 +00002559 return;
2560 }
2561
2562 // The following code generates these patterns:
2563 // (1) true_target == nullptr && false_target != nullptr
2564 // - opposite condition true => branch to false_target
2565 // (2) true_target != nullptr && false_target == nullptr
2566 // - condition true => branch to true_target
2567 // (3) true_target != nullptr && false_target != nullptr
2568 // - condition true => branch to true_target
2569 // - branch to false_target
2570 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002571 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002572 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002573 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002574 if (true_target == nullptr) {
2575 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2576 } else {
2577 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2578 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002579 } else {
2580 // The condition instruction has not been materialized, use its inputs as
2581 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002582 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002583 Primitive::Type type = condition->InputAt(0)->GetType();
2584 LocationSummary* locations = cond->GetLocations();
2585 IfCondition if_cond = condition->GetCondition();
2586 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002587
David Brazdil0debae72015-11-12 18:37:00 +00002588 if (true_target == nullptr) {
2589 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002590 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002591 }
2592
Alexey Frunze299a9392015-12-08 16:08:02 -08002593 switch (type) {
2594 default:
2595 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2596 break;
2597 case Primitive::kPrimLong:
2598 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2599 break;
2600 case Primitive::kPrimFloat:
2601 case Primitive::kPrimDouble:
2602 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2603 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002604 }
2605 }
David Brazdil0debae72015-11-12 18:37:00 +00002606
2607 // If neither branch falls through (case 3), the conditional branch to `true_target`
2608 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2609 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002610 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002611 }
2612}
2613
2614void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002616 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002617 locations->SetInAt(0, Location::RequiresRegister());
2618 }
2619}
2620
2621void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002622 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2623 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002624 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002625 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002626 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002627 nullptr : codegen_->GetLabelOf(false_successor);
2628 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629}
2630
2631void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2632 LocationSummary* locations = new (GetGraph()->GetArena())
2633 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
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) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003464 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3465 ? LocationSummary::kCallOnSlowPath
3466 : LocationSummary::kNoCall;
3467 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003468 locations->SetInAt(0, Location::RequiresRegister());
3469 if (instruction->HasUses()) {
3470 locations->SetOut(Location::SameAsFirstInput());
3471 }
3472}
3473
Calin Juravle2ae48182016-03-16 14:05:09 +00003474void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3475 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476 return;
3477 }
3478 Location obj = instruction->GetLocations()->InAt(0);
3479
3480 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003481 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003482}
3483
Calin Juravle2ae48182016-03-16 14:05:09 +00003484void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003485 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003486 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003487
3488 Location obj = instruction->GetLocations()->InAt(0);
3489
3490 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3491}
3492
3493void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003494 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003495}
3496
3497void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3498 HandleBinaryOp(instruction);
3499}
3500
3501void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3502 HandleBinaryOp(instruction);
3503}
3504
3505void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3506 LOG(FATAL) << "Unreachable";
3507}
3508
3509void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3510 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3511}
3512
3513void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3515 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3516 if (location.IsStackSlot()) {
3517 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3518 } else if (location.IsDoubleStackSlot()) {
3519 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3520 }
3521 locations->SetOut(location);
3522}
3523
3524void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3525 ATTRIBUTE_UNUSED) {
3526 // Nothing to do, the parameter is already at its location.
3527}
3528
3529void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3530 LocationSummary* locations =
3531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3532 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3533}
3534
3535void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3536 ATTRIBUTE_UNUSED) {
3537 // Nothing to do, the method is already at its location.
3538}
3539
3540void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003542 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003543 locations->SetInAt(i, Location::Any());
3544 }
3545 locations->SetOut(Location::Any());
3546}
3547
3548void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3549 LOG(FATAL) << "Unreachable";
3550}
3551
3552void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3553 Primitive::Type type = rem->GetResultType();
3554 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003555 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
3556 : LocationSummary::kNoCall;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003557 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3558
3559 switch (type) {
3560 case Primitive::kPrimInt:
3561 case Primitive::kPrimLong:
3562 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003563 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3565 break;
3566
3567 case Primitive::kPrimFloat:
3568 case Primitive::kPrimDouble: {
3569 InvokeRuntimeCallingConvention calling_convention;
3570 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3571 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3572 locations->SetOut(calling_convention.GetReturnLocation(type));
3573 break;
3574 }
3575
3576 default:
3577 LOG(FATAL) << "Unexpected rem type " << type;
3578 }
3579}
3580
3581void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3582 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003583
3584 switch (type) {
3585 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003586 case Primitive::kPrimLong:
3587 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003588 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003589
3590 case Primitive::kPrimFloat:
3591 case Primitive::kPrimDouble: {
Serban Constantinescufc734082016-07-19 17:18:07 +01003592 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
3593 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003594 if (type == Primitive::kPrimFloat) {
3595 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3596 } else {
3597 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003599 break;
3600 }
3601 default:
3602 LOG(FATAL) << "Unexpected rem type " << type;
3603 }
3604}
3605
3606void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3607 memory_barrier->SetLocations(nullptr);
3608}
3609
3610void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3611 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3612}
3613
3614void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3616 Primitive::Type return_type = ret->InputAt(0)->GetType();
3617 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3618}
3619
3620void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3621 codegen_->GenerateFrameExit();
3622}
3623
3624void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3625 ret->SetLocations(nullptr);
3626}
3627
3628void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3629 codegen_->GenerateFrameExit();
3630}
3631
Alexey Frunze92d90602015-12-18 18:16:36 -08003632void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3633 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003634}
3635
Alexey Frunze92d90602015-12-18 18:16:36 -08003636void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3637 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003638}
3639
Alexey Frunze4dda3372015-06-01 18:31:49 -07003640void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3641 HandleShift(shl);
3642}
3643
3644void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3645 HandleShift(shl);
3646}
3647
3648void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3649 HandleShift(shr);
3650}
3651
3652void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3653 HandleShift(shr);
3654}
3655
Alexey Frunze4dda3372015-06-01 18:31:49 -07003656void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3657 HandleBinaryOp(instruction);
3658}
3659
3660void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3661 HandleBinaryOp(instruction);
3662}
3663
3664void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3665 HandleFieldGet(instruction, instruction->GetFieldInfo());
3666}
3667
3668void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3669 HandleFieldGet(instruction, instruction->GetFieldInfo());
3670}
3671
3672void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3673 HandleFieldSet(instruction, instruction->GetFieldInfo());
3674}
3675
3676void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003677 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003678}
3679
Calin Juravlee460d1d2015-09-29 04:52:17 +01003680void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3681 HUnresolvedInstanceFieldGet* instruction) {
3682 FieldAccessCallingConventionMIPS64 calling_convention;
3683 codegen_->CreateUnresolvedFieldLocationSummary(
3684 instruction, instruction->GetFieldType(), calling_convention);
3685}
3686
3687void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3688 HUnresolvedInstanceFieldGet* instruction) {
3689 FieldAccessCallingConventionMIPS64 calling_convention;
3690 codegen_->GenerateUnresolvedFieldAccess(instruction,
3691 instruction->GetFieldType(),
3692 instruction->GetFieldIndex(),
3693 instruction->GetDexPc(),
3694 calling_convention);
3695}
3696
3697void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3698 HUnresolvedInstanceFieldSet* instruction) {
3699 FieldAccessCallingConventionMIPS64 calling_convention;
3700 codegen_->CreateUnresolvedFieldLocationSummary(
3701 instruction, instruction->GetFieldType(), calling_convention);
3702}
3703
3704void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3705 HUnresolvedInstanceFieldSet* instruction) {
3706 FieldAccessCallingConventionMIPS64 calling_convention;
3707 codegen_->GenerateUnresolvedFieldAccess(instruction,
3708 instruction->GetFieldType(),
3709 instruction->GetFieldIndex(),
3710 instruction->GetDexPc(),
3711 calling_convention);
3712}
3713
3714void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3715 HUnresolvedStaticFieldGet* instruction) {
3716 FieldAccessCallingConventionMIPS64 calling_convention;
3717 codegen_->CreateUnresolvedFieldLocationSummary(
3718 instruction, instruction->GetFieldType(), calling_convention);
3719}
3720
3721void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3722 HUnresolvedStaticFieldGet* instruction) {
3723 FieldAccessCallingConventionMIPS64 calling_convention;
3724 codegen_->GenerateUnresolvedFieldAccess(instruction,
3725 instruction->GetFieldType(),
3726 instruction->GetFieldIndex(),
3727 instruction->GetDexPc(),
3728 calling_convention);
3729}
3730
3731void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3732 HUnresolvedStaticFieldSet* instruction) {
3733 FieldAccessCallingConventionMIPS64 calling_convention;
3734 codegen_->CreateUnresolvedFieldLocationSummary(
3735 instruction, instruction->GetFieldType(), calling_convention);
3736}
3737
3738void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3739 HUnresolvedStaticFieldSet* instruction) {
3740 FieldAccessCallingConventionMIPS64 calling_convention;
3741 codegen_->GenerateUnresolvedFieldAccess(instruction,
3742 instruction->GetFieldType(),
3743 instruction->GetFieldIndex(),
3744 instruction->GetDexPc(),
3745 calling_convention);
3746}
3747
Alexey Frunze4dda3372015-06-01 18:31:49 -07003748void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3749 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3750}
3751
3752void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3753 HBasicBlock* block = instruction->GetBlock();
3754 if (block->GetLoopInformation() != nullptr) {
3755 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3756 // The back edge will generate the suspend check.
3757 return;
3758 }
3759 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3760 // The goto will generate the suspend check.
3761 return;
3762 }
3763 GenerateSuspendCheck(instruction, nullptr);
3764}
3765
Alexey Frunze4dda3372015-06-01 18:31:49 -07003766void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3767 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003769 InvokeRuntimeCallingConvention calling_convention;
3770 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3771}
3772
3773void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
Serban Constantinescufc734082016-07-19 17:18:07 +01003774 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003775 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3776}
3777
3778void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3779 Primitive::Type input_type = conversion->GetInputType();
3780 Primitive::Type result_type = conversion->GetResultType();
3781 DCHECK_NE(input_type, result_type);
3782
3783 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3784 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3785 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3786 }
3787
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003788 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3789
3790 if (Primitive::IsFloatingPointType(input_type)) {
3791 locations->SetInAt(0, Location::RequiresFpuRegister());
3792 } else {
3793 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003794 }
3795
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003796 if (Primitive::IsFloatingPointType(result_type)) {
3797 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003798 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003800 }
3801}
3802
3803void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3804 LocationSummary* locations = conversion->GetLocations();
3805 Primitive::Type result_type = conversion->GetResultType();
3806 Primitive::Type input_type = conversion->GetInputType();
3807
3808 DCHECK_NE(input_type, result_type);
3809
3810 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3811 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3812 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3813
3814 switch (result_type) {
3815 case Primitive::kPrimChar:
3816 __ Andi(dst, src, 0xFFFF);
3817 break;
3818 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003819 if (input_type == Primitive::kPrimLong) {
3820 // Type conversion from long to types narrower than int is a result of code
3821 // transformations. To avoid unpredictable results for SEB and SEH, we first
3822 // need to sign-extend the low 32-bit value into bits 32 through 63.
3823 __ Sll(dst, src, 0);
3824 __ Seb(dst, dst);
3825 } else {
3826 __ Seb(dst, src);
3827 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003828 break;
3829 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003830 if (input_type == Primitive::kPrimLong) {
3831 // Type conversion from long to types narrower than int is a result of code
3832 // transformations. To avoid unpredictable results for SEB and SEH, we first
3833 // need to sign-extend the low 32-bit value into bits 32 through 63.
3834 __ Sll(dst, src, 0);
3835 __ Seh(dst, dst);
3836 } else {
3837 __ Seh(dst, src);
3838 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003839 break;
3840 case Primitive::kPrimInt:
3841 case Primitive::kPrimLong:
3842 // Sign-extend 32-bit int into bits 32 through 63 for
3843 // int-to-long and long-to-int conversions
3844 __ Sll(dst, src, 0);
3845 break;
3846
3847 default:
3848 LOG(FATAL) << "Unexpected type conversion from " << input_type
3849 << " to " << result_type;
3850 }
3851 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003852 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3853 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3854 if (input_type == Primitive::kPrimLong) {
3855 __ Dmtc1(src, FTMP);
3856 if (result_type == Primitive::kPrimFloat) {
3857 __ Cvtsl(dst, FTMP);
3858 } else {
3859 __ Cvtdl(dst, FTMP);
3860 }
3861 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003862 __ Mtc1(src, FTMP);
3863 if (result_type == Primitive::kPrimFloat) {
3864 __ Cvtsw(dst, FTMP);
3865 } else {
3866 __ Cvtdw(dst, FTMP);
3867 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003868 }
3869 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3870 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003871 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3872 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3873 Mips64Label truncate;
3874 Mips64Label done;
3875
3876 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3877 // value when the input is either a NaN or is outside of the range of the output type
3878 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3879 // the same result.
3880 //
3881 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3882 // value of the output type if the input is outside of the range after the truncation or
3883 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3884 // results. This matches the desired float/double-to-int/long conversion exactly.
3885 //
3886 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3887 //
3888 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3889 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3890 // even though it must be NAN2008=1 on R6.
3891 //
3892 // The code takes care of the different behaviors by first comparing the input to the
3893 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3894 // If the input is greater than or equal to the minimum, it procedes to the truncate
3895 // instruction, which will handle such an input the same way irrespective of NAN2008.
3896 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3897 // in order to return either zero or the minimum value.
3898 //
3899 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3900 // truncate instruction for MIPS64R6.
3901 if (input_type == Primitive::kPrimFloat) {
3902 uint32_t min_val = (result_type == Primitive::kPrimLong)
3903 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3904 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3905 __ LoadConst32(TMP, min_val);
3906 __ Mtc1(TMP, FTMP);
3907 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003908 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003909 uint64_t min_val = (result_type == Primitive::kPrimLong)
3910 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3911 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3912 __ LoadConst64(TMP, min_val);
3913 __ Dmtc1(TMP, FTMP);
3914 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003915 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003916
3917 __ Bc1nez(FTMP, &truncate);
3918
3919 if (input_type == Primitive::kPrimFloat) {
3920 __ CmpEqS(FTMP, src, src);
3921 } else {
3922 __ CmpEqD(FTMP, src, src);
3923 }
3924 if (result_type == Primitive::kPrimLong) {
3925 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3926 } else {
3927 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3928 }
3929 __ Mfc1(TMP, FTMP);
3930 __ And(dst, dst, TMP);
3931
3932 __ Bc(&done);
3933
3934 __ Bind(&truncate);
3935
3936 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003937 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003938 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003939 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003940 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003941 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003942 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003943 } else {
3944 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003945 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003946 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003947 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003948 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003949 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003950 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003951
3952 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003953 } else if (Primitive::IsFloatingPointType(result_type) &&
3954 Primitive::IsFloatingPointType(input_type)) {
3955 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3956 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3957 if (result_type == Primitive::kPrimFloat) {
3958 __ Cvtsd(dst, src);
3959 } else {
3960 __ Cvtds(dst, src);
3961 }
3962 } else {
3963 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3964 << " to " << result_type;
3965 }
3966}
3967
3968void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3969 HandleShift(ushr);
3970}
3971
3972void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3973 HandleShift(ushr);
3974}
3975
3976void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3977 HandleBinaryOp(instruction);
3978}
3979
3980void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3981 HandleBinaryOp(instruction);
3982}
3983
3984void LocationsBuilderMIPS64::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 InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3990 // Nothing to do, this should be removed during prepare for register allocator.
3991 LOG(FATAL) << "Unreachable";
3992}
3993
3994void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003995 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003996}
3997
3998void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003999 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004000}
4001
4002void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004003 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004004}
4005
4006void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004007 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004008}
4009
4010void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004011 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004012}
4013
4014void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004015 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004016}
4017
4018void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004019 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004020}
4021
4022void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004023 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004024}
4025
4026void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004027 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004028}
4029
4030void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004031 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004032}
4033
4034void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004035 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004036}
4037
4038void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004039 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004040}
4041
Aart Bike9f37602015-10-09 11:15:55 -07004042void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004044}
4045
4046void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004048}
4049
4050void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004052}
4053
4054void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004056}
4057
4058void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004060}
4061
4062void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004064}
4065
4066void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004068}
4069
4070void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004071 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004072}
4073
Mark Mendellfe57faa2015-09-18 09:26:15 -04004074// Simple implementation of packed switch - generate cascaded compare/jumps.
4075void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4076 LocationSummary* locations =
4077 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4078 locations->SetInAt(0, Location::RequiresRegister());
4079}
4080
4081void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4082 int32_t lower_bound = switch_instr->GetStartValue();
4083 int32_t num_entries = switch_instr->GetNumEntries();
4084 LocationSummary* locations = switch_instr->GetLocations();
4085 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4086 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4087
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004088 // Create a set of compare/jumps.
4089 GpuRegister temp_reg = TMP;
4090 if (IsInt<16>(-lower_bound)) {
4091 __ Addiu(temp_reg, value_reg, -lower_bound);
4092 } else {
4093 __ LoadConst32(AT, -lower_bound);
4094 __ Addu(temp_reg, value_reg, AT);
4095 }
4096 // Jump to default if index is negative
4097 // Note: We don't check the case that index is positive while value < lower_bound, because in
4098 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4099 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4100
Mark Mendellfe57faa2015-09-18 09:26:15 -04004101 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004102 // Jump to successors[0] if value == lower_bound.
4103 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4104 int32_t last_index = 0;
4105 for (; num_entries - last_index > 2; last_index += 2) {
4106 __ Addiu(temp_reg, temp_reg, -2);
4107 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4108 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4109 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4110 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4111 }
4112 if (num_entries - last_index == 2) {
4113 // The last missing case_value.
4114 __ Addiu(temp_reg, temp_reg, -1);
4115 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004116 }
4117
4118 // And the default for any other value.
4119 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004120 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004121 }
4122}
4123
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004124void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4125 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4126}
4127
4128void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4129 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4130}
4131
Alexey Frunze4dda3372015-06-01 18:31:49 -07004132} // namespace mips64
4133} // namespace art