blob: 9c8c4660f2d07e9aa588daa08381fa2ca8e576bb [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
105#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
Lazar Trsicd9672662015-09-03 17:33:01 +0200106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700107
108class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
109 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000110 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111
112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
115 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000116 if (instruction_->CanThrowIntoCatchBlock()) {
117 // Live registers will be restored in the catch block if caught.
118 SaveLiveRegisters(codegen, instruction_->GetLocations());
119 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700120 // We're moving two locations to locations that could overlap, so we need a parallel
121 // move resolver.
122 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
125 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
128 Primitive::kPrimInt);
129 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
130 instruction_,
131 instruction_->GetDexPc(),
132 this);
133 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
134 }
135
Alexandre Rames8158f282015-08-07 10:26:17 +0100136 bool IsFatal() const OVERRIDE { return true; }
137
Roland Levillain46648892015-06-19 16:07:18 +0100138 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
139
Alexey Frunze4dda3372015-06-01 18:31:49 -0700140 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700141 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
142};
143
144class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
145 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000146 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700147
148 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
149 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
150 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000151 if (instruction_->CanThrowIntoCatchBlock()) {
152 // Live registers will be restored in the catch block if caught.
153 SaveLiveRegisters(codegen, instruction_->GetLocations());
154 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700155 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
156 instruction_,
157 instruction_->GetDexPc(),
158 this);
159 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Roland Levillain46648892015-06-19 16:07:18 +0100164 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
165
Alexey Frunze4dda3372015-06-01 18:31:49 -0700166 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700167 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
168};
169
170class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
171 public:
172 LoadClassSlowPathMIPS64(HLoadClass* cls,
173 HInstruction* at,
174 uint32_t dex_pc,
175 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000176 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700177 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
178 }
179
180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
181 LocationSummary* locations = at_->GetLocations();
182 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
183
184 __ Bind(GetEntryLabel());
185 SaveLiveRegisters(codegen, locations);
186
187 InvokeRuntimeCallingConvention calling_convention;
188 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
189 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
190 : QUICK_ENTRY_POINT(pInitializeType);
191 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
192 if (do_clinit_) {
193 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
194 } else {
195 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
196 }
197
198 // Move the class to the desired location.
199 Location out = locations->Out();
200 if (out.IsValid()) {
201 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
202 Primitive::Type type = at_->GetType();
203 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
204 }
205
206 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700207 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700208 }
209
Roland Levillain46648892015-06-19 16:07:18 +0100210 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
211
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 private:
213 // The class this slow path will load.
214 HLoadClass* const cls_;
215
216 // The instruction where this slow path is happening.
217 // (Might be the load class or an initialization check).
218 HInstruction* const at_;
219
220 // The dex PC of `at_`.
221 const uint32_t dex_pc_;
222
223 // Whether to initialize the class.
224 const bool do_clinit_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227};
228
229class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000231 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 LocationSummary* locations = instruction_->GetLocations();
235 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237
238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
241 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000242 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
243 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700244 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
245 instruction_,
246 instruction_->GetDexPc(),
247 this);
248 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
249 Primitive::Type type = instruction_->GetType();
250 mips64_codegen->MoveLocation(locations->Out(),
251 calling_convention.GetReturnLocation(type),
252 type);
253
254 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700255 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700256 }
257
Roland Levillain46648892015-06-19 16:07:18 +0100258 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
259
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
262};
263
264class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
265 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000266 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267
268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
270 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000271 if (instruction_->CanThrowIntoCatchBlock()) {
272 // Live registers will be restored in the catch block if caught.
273 SaveLiveRegisters(codegen, instruction_->GetLocations());
274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
276 instruction_,
277 instruction_->GetDexPc(),
278 this);
279 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Roland Levillain46648892015-06-19 16:07:18 +0100284 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
285
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700287 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
288};
289
290class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
291 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100292 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294
295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
297 __ Bind(GetEntryLabel());
298 SaveLiveRegisters(codegen, instruction_->GetLocations());
299 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
300 instruction_,
301 instruction_->GetDexPc(),
302 this);
303 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
304 RestoreLiveRegisters(codegen, instruction_->GetLocations());
305 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700306 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700308 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 }
310 }
311
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 DCHECK(successor_ == nullptr);
314 return &return_label_;
315 }
316
Roland Levillain46648892015-06-19 16:07:18 +0100317 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
318
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 // If not null, the block to branch to after the suspend check.
321 HBasicBlock* const successor_;
322
323 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700324 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700325
326 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
327};
328
329class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
330 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000331 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
334 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
340
341 __ Bind(GetEntryLabel());
342 SaveLiveRegisters(codegen, locations);
343
344 // We're moving two locations to locations that could overlap, so we need a parallel
345 // move resolver.
346 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
349 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352 Primitive::kPrimNot);
353
354 if (instruction_->IsInstanceOf()) {
355 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
356 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000359 CheckEntrypointTypes<
360 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Primitive::Type ret_type = instruction_->GetType();
362 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
363 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 } else {
365 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100366 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
368 }
369
370 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700371 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 }
373
Roland Levillain46648892015-06-19 16:07:18 +0100374 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
375
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
378};
379
380class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
381 public:
Aart Bik42249c32016-01-07 15:33:50 -0800382 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800386 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800389 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
390 instruction_,
391 instruction_->GetDexPc(),
392 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000393 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700394 }
395
Roland Levillain46648892015-06-19 16:07:18 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
397
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
400};
401
402CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
403 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100404 const CompilerOptions& compiler_options,
405 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 : CodeGenerator(graph,
407 kNumberOfGpuRegisters,
408 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000409 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
411 arraysize(kCoreCalleeSaves)),
412 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
413 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100414 compiler_options,
415 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100416 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417 location_builder_(graph, this),
418 instruction_visitor_(graph, this),
419 move_resolver_(graph->GetArena(), this),
420 isa_features_(isa_features) {
421 // Save RA (containing the return address) to mimic Quick.
422 AddAllocatedRegister(Location::RegisterLocation(RA));
423}
424
425#undef __
426#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200427#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700428
429void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700430 // Ensure that we fix up branches.
431 __ FinalizeCode();
432
433 // Adjust native pc offsets in stack maps.
434 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
435 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
436 uint32_t new_position = __ GetAdjustedPosition(old_position);
437 DCHECK_GE(new_position, old_position);
438 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
439 }
440
441 // Adjust pc offsets for the disassembly information.
442 if (disasm_info_ != nullptr) {
443 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
444 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
445 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
446 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
447 it.second.start = __ GetAdjustedPosition(it.second.start);
448 it.second.end = __ GetAdjustedPosition(it.second.end);
449 }
450 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
451 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
452 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
453 }
454 }
455
Alexey Frunze4dda3372015-06-01 18:31:49 -0700456 CodeGenerator::Finalize(allocator);
457}
458
459Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
460 return codegen_->GetAssembler();
461}
462
463void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100464 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700465 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
466}
467
468void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100469 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700470 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
471}
472
473void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
474 // Pop reg
475 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200476 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477}
478
479void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
480 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200481 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700482 __ Sd(GpuRegister(reg), SP, 0);
483}
484
485void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
486 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
487 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
488 // Allocate a scratch register other than TMP, if available.
489 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
490 // automatically unspilled when the scratch scope object is destroyed).
491 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
492 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200493 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700494 __ LoadFromOffset(load_type,
495 GpuRegister(ensure_scratch.GetRegister()),
496 SP,
497 index1 + stack_offset);
498 __ LoadFromOffset(load_type,
499 TMP,
500 SP,
501 index2 + stack_offset);
502 __ StoreToOffset(store_type,
503 GpuRegister(ensure_scratch.GetRegister()),
504 SP,
505 index2 + stack_offset);
506 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
507}
508
509static dwarf::Reg DWARFReg(GpuRegister reg) {
510 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
511}
512
David Srbeckyba702002016-02-01 18:15:29 +0000513static dwarf::Reg DWARFReg(FpuRegister reg) {
514 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
515}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700516
517void CodeGeneratorMIPS64::GenerateFrameEntry() {
518 __ Bind(&frame_entry_label_);
519
520 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
521
522 if (do_overflow_check) {
523 __ LoadFromOffset(kLoadWord,
524 ZERO,
525 SP,
526 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
527 RecordPcInfo(nullptr, 0);
528 }
529
530 // TODO: anything related to T9/GP/GOT/PIC/.so's?
531
532 if (HasEmptyFrame()) {
533 return;
534 }
535
536 // Make sure the frame size isn't unreasonably large. Per the various APIs
537 // it looks like it should always be less than 2GB in size, which allows
538 // us using 32-bit signed offsets from the stack pointer.
539 if (GetFrameSize() > 0x7FFFFFFF)
540 LOG(FATAL) << "Stack frame larger than 2GB";
541
542 // Spill callee-saved registers.
543 // Note that their cumulative size is small and they can be indexed using
544 // 16-bit offsets.
545
546 // TODO: increment/decrement SP in one step instead of two or remove this comment.
547
548 uint32_t ofs = FrameEntrySpillSize();
549 __ IncreaseFrameSize(ofs);
550
551 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
552 GpuRegister reg = kCoreCalleeSaves[i];
553 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200554 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700555 __ Sd(reg, SP, ofs);
556 __ cfi().RelOffset(DWARFReg(reg), ofs);
557 }
558 }
559
560 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
561 FpuRegister reg = kFpuCalleeSaves[i];
562 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200563 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700564 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000565 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700566 }
567 }
568
569 // Allocate the rest of the frame and store the current method pointer
570 // at its end.
571
572 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
573
574 static_assert(IsInt<16>(kCurrentMethodStackOffset),
575 "kCurrentMethodStackOffset must fit into int16_t");
576 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
577}
578
579void CodeGeneratorMIPS64::GenerateFrameExit() {
580 __ cfi().RememberState();
581
582 // TODO: anything related to T9/GP/GOT/PIC/.so's?
583
584 if (!HasEmptyFrame()) {
585 // Deallocate the rest of the frame.
586
587 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
588
589 // Restore callee-saved registers.
590 // Note that their cumulative size is small and they can be indexed using
591 // 16-bit offsets.
592
593 // TODO: increment/decrement SP in one step instead of two or remove this comment.
594
595 uint32_t ofs = 0;
596
597 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
598 FpuRegister reg = kFpuCalleeSaves[i];
599 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
600 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200601 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000602 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700603 }
604 }
605
606 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
607 GpuRegister reg = kCoreCalleeSaves[i];
608 if (allocated_registers_.ContainsCoreRegister(reg)) {
609 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200610 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700611 __ cfi().Restore(DWARFReg(reg));
612 }
613 }
614
615 DCHECK_EQ(ofs, FrameEntrySpillSize());
616 __ DecreaseFrameSize(ofs);
617 }
618
619 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700620 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700621
622 __ cfi().RestoreState();
623 __ cfi().DefCFAOffset(GetFrameSize());
624}
625
626void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
627 __ Bind(GetLabelOf(block));
628}
629
630void CodeGeneratorMIPS64::MoveLocation(Location destination,
631 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100632 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700633 if (source.Equals(destination)) {
634 return;
635 }
636
637 // A valid move can always be inferred from the destination and source
638 // locations. When moving from and to a register, the argument type can be
639 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100640 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700641 DCHECK_EQ(unspecified_type, false);
642
643 if (destination.IsRegister() || destination.IsFpuRegister()) {
644 if (unspecified_type) {
645 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
646 if (source.IsStackSlot() ||
647 (src_cst != nullptr && (src_cst->IsIntConstant()
648 || src_cst->IsFloatConstant()
649 || src_cst->IsNullConstant()))) {
650 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 } else {
653 // If the source is a double stack slot or a 64bit constant, a 64bit
654 // type is appropriate. Else the source is a register, and since the
655 // type has not been specified, we chose a 64bit type to force a 64bit
656 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100657 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700658 }
659 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100660 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
661 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700662 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
663 // Move to GPR/FPR from stack
664 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100665 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700666 __ LoadFpuFromOffset(load_type,
667 destination.AsFpuRegister<FpuRegister>(),
668 SP,
669 source.GetStackIndex());
670 } else {
671 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
672 __ LoadFromOffset(load_type,
673 destination.AsRegister<GpuRegister>(),
674 SP,
675 source.GetStackIndex());
676 }
677 } else if (source.IsConstant()) {
678 // Move to GPR/FPR from constant
679 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100680 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700681 gpr = destination.AsRegister<GpuRegister>();
682 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700684 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
685 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
686 gpr = ZERO;
687 } else {
688 __ LoadConst32(gpr, value);
689 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700690 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700691 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
692 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
693 gpr = ZERO;
694 } else {
695 __ LoadConst64(gpr, value);
696 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700697 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100698 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700699 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100700 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
702 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 if (destination.IsRegister()) {
705 // Move to GPR from GPR
706 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
707 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100708 DCHECK(destination.IsFpuRegister());
709 if (Primitive::Is64BitType(dst_type)) {
710 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
711 } else {
712 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
713 }
714 }
715 } else if (source.IsFpuRegister()) {
716 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100718 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700719 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
720 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700722 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
723 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 } else {
725 DCHECK(destination.IsRegister());
726 if (Primitive::Is64BitType(dst_type)) {
727 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
728 } else {
729 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
730 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700731 }
732 }
733 } else { // The destination is not a register. It must be a stack slot.
734 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
735 if (source.IsRegister() || source.IsFpuRegister()) {
736 if (unspecified_type) {
737 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100738 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700739 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100740 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700741 }
742 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100743 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
744 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700745 // Move to stack from GPR/FPR
746 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
747 if (source.IsRegister()) {
748 __ StoreToOffset(store_type,
749 source.AsRegister<GpuRegister>(),
750 SP,
751 destination.GetStackIndex());
752 } else {
753 __ StoreFpuToOffset(store_type,
754 source.AsFpuRegister<FpuRegister>(),
755 SP,
756 destination.GetStackIndex());
757 }
758 } else if (source.IsConstant()) {
759 // Move to stack from constant
760 HConstant* src_cst = source.GetConstant();
761 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700762 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700763 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700764 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
765 if (value != 0) {
766 gpr = TMP;
767 __ LoadConst32(gpr, value);
768 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700769 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700770 DCHECK(destination.IsDoubleStackSlot());
771 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
772 if (value != 0) {
773 gpr = TMP;
774 __ LoadConst64(gpr, value);
775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700778 } else {
779 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
780 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
781 // Move to stack from stack
782 if (destination.IsStackSlot()) {
783 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
784 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
785 } else {
786 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
787 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
788 }
789 }
790 }
791}
792
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700793void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700794 DCHECK(!loc1.IsConstant());
795 DCHECK(!loc2.IsConstant());
796
797 if (loc1.Equals(loc2)) {
798 return;
799 }
800
801 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
802 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
803 bool is_fp_reg1 = loc1.IsFpuRegister();
804 bool is_fp_reg2 = loc2.IsFpuRegister();
805
806 if (loc2.IsRegister() && loc1.IsRegister()) {
807 // Swap 2 GPRs
808 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
809 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
810 __ Move(TMP, r2);
811 __ Move(r2, r1);
812 __ Move(r1, TMP);
813 } else if (is_fp_reg2 && is_fp_reg1) {
814 // Swap 2 FPRs
815 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
816 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700817 if (type == Primitive::kPrimFloat) {
818 __ MovS(FTMP, r1);
819 __ MovS(r1, r2);
820 __ MovS(r2, FTMP);
821 } else {
822 DCHECK_EQ(type, Primitive::kPrimDouble);
823 __ MovD(FTMP, r1);
824 __ MovD(r1, r2);
825 __ MovD(r2, FTMP);
826 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700827 } else if (is_slot1 != is_slot2) {
828 // Swap GPR/FPR and stack slot
829 Location reg_loc = is_slot1 ? loc2 : loc1;
830 Location mem_loc = is_slot1 ? loc1 : loc2;
831 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
832 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
833 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
834 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
835 if (reg_loc.IsFpuRegister()) {
836 __ StoreFpuToOffset(store_type,
837 reg_loc.AsFpuRegister<FpuRegister>(),
838 SP,
839 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700840 if (mem_loc.IsStackSlot()) {
841 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
842 } else {
843 DCHECK(mem_loc.IsDoubleStackSlot());
844 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
845 }
846 } else {
847 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
848 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
849 }
850 } else if (is_slot1 && is_slot2) {
851 move_resolver_.Exchange(loc1.GetStackIndex(),
852 loc2.GetStackIndex(),
853 loc1.IsDoubleStackSlot());
854 } else {
855 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
856 }
857}
858
Calin Juravle175dc732015-08-25 15:42:32 +0100859void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
860 DCHECK(location.IsRegister());
861 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
862}
863
Calin Juravlee460d1d2015-09-29 04:52:17 +0100864void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
865 if (location.IsRegister()) {
866 locations->AddTemp(location);
867 } else {
868 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
869 }
870}
871
Alexey Frunze4dda3372015-06-01 18:31:49 -0700872Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
873 Primitive::Type type = load->GetType();
874
875 switch (type) {
876 case Primitive::kPrimNot:
877 case Primitive::kPrimInt:
878 case Primitive::kPrimFloat:
879 return Location::StackSlot(GetStackSlot(load->GetLocal()));
880
881 case Primitive::kPrimLong:
882 case Primitive::kPrimDouble:
883 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
884
885 case Primitive::kPrimBoolean:
886 case Primitive::kPrimByte:
887 case Primitive::kPrimChar:
888 case Primitive::kPrimShort:
889 case Primitive::kPrimVoid:
890 LOG(FATAL) << "Unexpected type " << type;
891 }
892
893 LOG(FATAL) << "Unreachable";
894 return Location::NoLocation();
895}
896
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100897void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
898 GpuRegister value,
899 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700900 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700901 GpuRegister card = AT;
902 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100903 if (value_can_be_null) {
904 __ Beqzc(value, &done);
905 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700906 __ LoadFromOffset(kLoadDoubleword,
907 card,
908 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200909 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700910 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
911 __ Daddu(temp, card, temp);
912 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100913 if (value_can_be_null) {
914 __ Bind(&done);
915 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700916}
917
David Brazdil58282f42016-01-14 12:45:10 +0000918void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
920 blocked_core_registers_[ZERO] = true;
921 blocked_core_registers_[K0] = true;
922 blocked_core_registers_[K1] = true;
923 blocked_core_registers_[GP] = true;
924 blocked_core_registers_[SP] = true;
925 blocked_core_registers_[RA] = true;
926
Lazar Trsicd9672662015-09-03 17:33:01 +0200927 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
928 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700929 blocked_core_registers_[AT] = true;
930 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200931 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932 blocked_fpu_registers_[FTMP] = true;
933
934 // Reserve suspend and thread registers.
935 blocked_core_registers_[S0] = true;
936 blocked_core_registers_[TR] = true;
937
938 // Reserve T9 for function calls
939 blocked_core_registers_[T9] = true;
940
941 // TODO: review; anything else?
942
David Brazdil58282f42016-01-14 12:45:10 +0000943 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
945 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
946 }
947
948 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
949 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
950 }
951}
952
Alexey Frunze4dda3372015-06-01 18:31:49 -0700953size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
954 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200955 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956}
957
958size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
959 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200960 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700961}
962
963size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
964 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200965 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700966}
967
968size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
969 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200970 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971}
972
973void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100974 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700975}
976
977void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100978 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700979}
980
Calin Juravle175dc732015-08-25 15:42:32 +0100981void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
982 HInstruction* instruction,
983 uint32_t dex_pc,
984 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200985 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100986 instruction,
987 dex_pc,
988 slow_path);
989}
990
Alexey Frunze4dda3372015-06-01 18:31:49 -0700991void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
992 HInstruction* instruction,
993 uint32_t dex_pc,
994 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100995 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700996 // TODO: anything related to T9/GP/GOT/PIC/.so's?
997 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
998 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700999 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001000 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001}
1002
1003void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1004 GpuRegister class_reg) {
1005 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1006 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1007 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1008 // TODO: barrier needed?
1009 __ Bind(slow_path->GetExitLabel());
1010}
1011
1012void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1013 __ Sync(0); // only stype 0 is supported
1014}
1015
1016void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1017 HBasicBlock* successor) {
1018 SuspendCheckSlowPathMIPS64* slow_path =
1019 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1020 codegen_->AddSlowPath(slow_path);
1021
1022 __ LoadFromOffset(kLoadUnsignedHalfword,
1023 TMP,
1024 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026 if (successor == nullptr) {
1027 __ Bnezc(TMP, slow_path->GetEntryLabel());
1028 __ Bind(slow_path->GetReturnLabel());
1029 } else {
1030 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001031 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 // slow_path will return to GetLabelOf(successor).
1033 }
1034}
1035
1036InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1037 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001038 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001039 assembler_(codegen->GetAssembler()),
1040 codegen_(codegen) {}
1041
1042void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1043 DCHECK_EQ(instruction->InputCount(), 2U);
1044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1045 Primitive::Type type = instruction->GetResultType();
1046 switch (type) {
1047 case Primitive::kPrimInt:
1048 case Primitive::kPrimLong: {
1049 locations->SetInAt(0, Location::RequiresRegister());
1050 HInstruction* right = instruction->InputAt(1);
1051 bool can_use_imm = false;
1052 if (right->IsConstant()) {
1053 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1054 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1055 can_use_imm = IsUint<16>(imm);
1056 } else if (instruction->IsAdd()) {
1057 can_use_imm = IsInt<16>(imm);
1058 } else {
1059 DCHECK(instruction->IsSub());
1060 can_use_imm = IsInt<16>(-imm);
1061 }
1062 }
1063 if (can_use_imm)
1064 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1065 else
1066 locations->SetInAt(1, Location::RequiresRegister());
1067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1068 }
1069 break;
1070
1071 case Primitive::kPrimFloat:
1072 case Primitive::kPrimDouble:
1073 locations->SetInAt(0, Location::RequiresFpuRegister());
1074 locations->SetInAt(1, Location::RequiresFpuRegister());
1075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1076 break;
1077
1078 default:
1079 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1080 }
1081}
1082
1083void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1084 Primitive::Type type = instruction->GetType();
1085 LocationSummary* locations = instruction->GetLocations();
1086
1087 switch (type) {
1088 case Primitive::kPrimInt:
1089 case Primitive::kPrimLong: {
1090 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1091 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1092 Location rhs_location = locations->InAt(1);
1093
1094 GpuRegister rhs_reg = ZERO;
1095 int64_t rhs_imm = 0;
1096 bool use_imm = rhs_location.IsConstant();
1097 if (use_imm) {
1098 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1099 } else {
1100 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1101 }
1102
1103 if (instruction->IsAnd()) {
1104 if (use_imm)
1105 __ Andi(dst, lhs, rhs_imm);
1106 else
1107 __ And(dst, lhs, rhs_reg);
1108 } else if (instruction->IsOr()) {
1109 if (use_imm)
1110 __ Ori(dst, lhs, rhs_imm);
1111 else
1112 __ Or(dst, lhs, rhs_reg);
1113 } else if (instruction->IsXor()) {
1114 if (use_imm)
1115 __ Xori(dst, lhs, rhs_imm);
1116 else
1117 __ Xor(dst, lhs, rhs_reg);
1118 } else if (instruction->IsAdd()) {
1119 if (type == Primitive::kPrimInt) {
1120 if (use_imm)
1121 __ Addiu(dst, lhs, rhs_imm);
1122 else
1123 __ Addu(dst, lhs, rhs_reg);
1124 } else {
1125 if (use_imm)
1126 __ Daddiu(dst, lhs, rhs_imm);
1127 else
1128 __ Daddu(dst, lhs, rhs_reg);
1129 }
1130 } else {
1131 DCHECK(instruction->IsSub());
1132 if (type == Primitive::kPrimInt) {
1133 if (use_imm)
1134 __ Addiu(dst, lhs, -rhs_imm);
1135 else
1136 __ Subu(dst, lhs, rhs_reg);
1137 } else {
1138 if (use_imm)
1139 __ Daddiu(dst, lhs, -rhs_imm);
1140 else
1141 __ Dsubu(dst, lhs, rhs_reg);
1142 }
1143 }
1144 break;
1145 }
1146 case Primitive::kPrimFloat:
1147 case Primitive::kPrimDouble: {
1148 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1149 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1150 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1151 if (instruction->IsAdd()) {
1152 if (type == Primitive::kPrimFloat)
1153 __ AddS(dst, lhs, rhs);
1154 else
1155 __ AddD(dst, lhs, rhs);
1156 } else if (instruction->IsSub()) {
1157 if (type == Primitive::kPrimFloat)
1158 __ SubS(dst, lhs, rhs);
1159 else
1160 __ SubD(dst, lhs, rhs);
1161 } else {
1162 LOG(FATAL) << "Unexpected floating-point binary operation";
1163 }
1164 break;
1165 }
1166 default:
1167 LOG(FATAL) << "Unexpected binary operation type " << type;
1168 }
1169}
1170
1171void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001172 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173
1174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1175 Primitive::Type type = instr->GetResultType();
1176 switch (type) {
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimLong: {
1179 locations->SetInAt(0, Location::RequiresRegister());
1180 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 break;
1183 }
1184 default:
1185 LOG(FATAL) << "Unexpected shift type " << type;
1186 }
1187}
1188
1189void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001190 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 LocationSummary* locations = instr->GetLocations();
1192 Primitive::Type type = instr->GetType();
1193
1194 switch (type) {
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimLong: {
1197 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1198 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1199 Location rhs_location = locations->InAt(1);
1200
1201 GpuRegister rhs_reg = ZERO;
1202 int64_t rhs_imm = 0;
1203 bool use_imm = rhs_location.IsConstant();
1204 if (use_imm) {
1205 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1206 } else {
1207 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1208 }
1209
1210 if (use_imm) {
1211 uint32_t shift_value = (type == Primitive::kPrimInt)
1212 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1213 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1214
Alexey Frunze92d90602015-12-18 18:16:36 -08001215 if (shift_value == 0) {
1216 if (dst != lhs) {
1217 __ Move(dst, lhs);
1218 }
1219 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001220 if (instr->IsShl()) {
1221 __ Sll(dst, lhs, shift_value);
1222 } else if (instr->IsShr()) {
1223 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001224 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001225 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001226 } else {
1227 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001228 }
1229 } else {
1230 if (shift_value < 32) {
1231 if (instr->IsShl()) {
1232 __ Dsll(dst, lhs, shift_value);
1233 } else if (instr->IsShr()) {
1234 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001235 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001236 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001237 } else {
1238 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001239 }
1240 } else {
1241 shift_value -= 32;
1242 if (instr->IsShl()) {
1243 __ Dsll32(dst, lhs, shift_value);
1244 } else if (instr->IsShr()) {
1245 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001246 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001247 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001248 } else {
1249 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001250 }
1251 }
1252 }
1253 } else {
1254 if (type == Primitive::kPrimInt) {
1255 if (instr->IsShl()) {
1256 __ Sllv(dst, lhs, rhs_reg);
1257 } else if (instr->IsShr()) {
1258 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001259 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001260 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001261 } else {
1262 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001263 }
1264 } else {
1265 if (instr->IsShl()) {
1266 __ Dsllv(dst, lhs, rhs_reg);
1267 } else if (instr->IsShr()) {
1268 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001269 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001270 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001271 } else {
1272 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001273 }
1274 }
1275 }
1276 break;
1277 }
1278 default:
1279 LOG(FATAL) << "Unexpected shift operation type " << type;
1280 }
1281}
1282
1283void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1284 HandleBinaryOp(instruction);
1285}
1286
1287void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1288 HandleBinaryOp(instruction);
1289}
1290
1291void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1292 HandleBinaryOp(instruction);
1293}
1294
1295void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1296 HandleBinaryOp(instruction);
1297}
1298
1299void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1300 LocationSummary* locations =
1301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1302 locations->SetInAt(0, Location::RequiresRegister());
1303 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1304 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1305 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1306 } else {
1307 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1308 }
1309}
1310
1311void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1312 LocationSummary* locations = instruction->GetLocations();
1313 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1314 Location index = locations->InAt(1);
1315 Primitive::Type type = instruction->GetType();
1316
1317 switch (type) {
1318 case Primitive::kPrimBoolean: {
1319 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1320 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1321 if (index.IsConstant()) {
1322 size_t offset =
1323 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1324 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1325 } else {
1326 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1327 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1328 }
1329 break;
1330 }
1331
1332 case Primitive::kPrimByte: {
1333 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1334 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1335 if (index.IsConstant()) {
1336 size_t offset =
1337 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1338 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1339 } else {
1340 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1341 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1342 }
1343 break;
1344 }
1345
1346 case Primitive::kPrimShort: {
1347 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1348 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1349 if (index.IsConstant()) {
1350 size_t offset =
1351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1352 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1353 } else {
1354 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1355 __ Daddu(TMP, obj, TMP);
1356 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1357 }
1358 break;
1359 }
1360
1361 case Primitive::kPrimChar: {
1362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1363 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1364 if (index.IsConstant()) {
1365 size_t offset =
1366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1367 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1368 } else {
1369 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1370 __ Daddu(TMP, obj, TMP);
1371 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1372 }
1373 break;
1374 }
1375
1376 case Primitive::kPrimInt:
1377 case Primitive::kPrimNot: {
1378 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1379 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1380 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1381 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1382 if (index.IsConstant()) {
1383 size_t offset =
1384 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1385 __ LoadFromOffset(load_type, out, obj, offset);
1386 } else {
1387 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1388 __ Daddu(TMP, obj, TMP);
1389 __ LoadFromOffset(load_type, out, TMP, data_offset);
1390 }
1391 break;
1392 }
1393
1394 case Primitive::kPrimLong: {
1395 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1396 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1397 if (index.IsConstant()) {
1398 size_t offset =
1399 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1400 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1401 } else {
1402 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1403 __ Daddu(TMP, obj, TMP);
1404 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1405 }
1406 break;
1407 }
1408
1409 case Primitive::kPrimFloat: {
1410 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1411 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1412 if (index.IsConstant()) {
1413 size_t offset =
1414 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1415 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1416 } else {
1417 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1418 __ Daddu(TMP, obj, TMP);
1419 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1420 }
1421 break;
1422 }
1423
1424 case Primitive::kPrimDouble: {
1425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1426 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1427 if (index.IsConstant()) {
1428 size_t offset =
1429 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1430 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1431 } else {
1432 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1433 __ Daddu(TMP, obj, TMP);
1434 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1435 }
1436 break;
1437 }
1438
1439 case Primitive::kPrimVoid:
1440 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1441 UNREACHABLE();
1442 }
1443 codegen_->MaybeRecordImplicitNullCheck(instruction);
1444}
1445
1446void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1447 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1450}
1451
1452void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1453 LocationSummary* locations = instruction->GetLocations();
1454 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1455 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1456 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1457 __ LoadFromOffset(kLoadWord, out, obj, offset);
1458 codegen_->MaybeRecordImplicitNullCheck(instruction);
1459}
1460
1461void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001462 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1464 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001465 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1466 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001467 InvokeRuntimeCallingConvention calling_convention;
1468 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1469 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1470 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1471 } else {
1472 locations->SetInAt(0, Location::RequiresRegister());
1473 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1474 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1475 locations->SetInAt(2, Location::RequiresFpuRegister());
1476 } else {
1477 locations->SetInAt(2, Location::RequiresRegister());
1478 }
1479 }
1480}
1481
1482void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1483 LocationSummary* locations = instruction->GetLocations();
1484 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1485 Location index = locations->InAt(1);
1486 Primitive::Type value_type = instruction->GetComponentType();
1487 bool needs_runtime_call = locations->WillCall();
1488 bool needs_write_barrier =
1489 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1490
1491 switch (value_type) {
1492 case Primitive::kPrimBoolean:
1493 case Primitive::kPrimByte: {
1494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1495 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1496 if (index.IsConstant()) {
1497 size_t offset =
1498 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1499 __ StoreToOffset(kStoreByte, value, obj, offset);
1500 } else {
1501 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1502 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1503 }
1504 break;
1505 }
1506
1507 case Primitive::kPrimShort:
1508 case Primitive::kPrimChar: {
1509 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1510 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1511 if (index.IsConstant()) {
1512 size_t offset =
1513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1514 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1515 } else {
1516 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1517 __ Daddu(TMP, obj, TMP);
1518 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1519 }
1520 break;
1521 }
1522
1523 case Primitive::kPrimInt:
1524 case Primitive::kPrimNot: {
1525 if (!needs_runtime_call) {
1526 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1527 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1528 if (index.IsConstant()) {
1529 size_t offset =
1530 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1531 __ StoreToOffset(kStoreWord, value, obj, offset);
1532 } else {
1533 DCHECK(index.IsRegister()) << index;
1534 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1535 __ Daddu(TMP, obj, TMP);
1536 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1537 }
1538 codegen_->MaybeRecordImplicitNullCheck(instruction);
1539 if (needs_write_barrier) {
1540 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001541 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001542 }
1543 } else {
1544 DCHECK_EQ(value_type, Primitive::kPrimNot);
1545 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1546 instruction,
1547 instruction->GetDexPc(),
1548 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001549 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001550 }
1551 break;
1552 }
1553
1554 case Primitive::kPrimLong: {
1555 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1556 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1557 if (index.IsConstant()) {
1558 size_t offset =
1559 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1560 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1561 } else {
1562 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1563 __ Daddu(TMP, obj, TMP);
1564 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1565 }
1566 break;
1567 }
1568
1569 case Primitive::kPrimFloat: {
1570 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1571 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1572 DCHECK(locations->InAt(2).IsFpuRegister());
1573 if (index.IsConstant()) {
1574 size_t offset =
1575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1576 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1577 } else {
1578 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1579 __ Daddu(TMP, obj, TMP);
1580 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1581 }
1582 break;
1583 }
1584
1585 case Primitive::kPrimDouble: {
1586 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1587 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1588 DCHECK(locations->InAt(2).IsFpuRegister());
1589 if (index.IsConstant()) {
1590 size_t offset =
1591 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1592 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1593 } else {
1594 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1595 __ Daddu(TMP, obj, TMP);
1596 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1597 }
1598 break;
1599 }
1600
1601 case Primitive::kPrimVoid:
1602 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1603 UNREACHABLE();
1604 }
1605
1606 // Ints and objects are handled in the switch.
1607 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1608 codegen_->MaybeRecordImplicitNullCheck(instruction);
1609 }
1610}
1611
1612void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001613 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1614 ? LocationSummary::kCallOnSlowPath
1615 : LocationSummary::kNoCall;
1616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetInAt(1, Location::RequiresRegister());
1619 if (instruction->HasUses()) {
1620 locations->SetOut(Location::SameAsFirstInput());
1621 }
1622}
1623
1624void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1625 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001626 BoundsCheckSlowPathMIPS64* slow_path =
1627 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001628 codegen_->AddSlowPath(slow_path);
1629
1630 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1631 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1632
1633 // length is limited by the maximum positive signed 32-bit integer.
1634 // Unsigned comparison of length and index checks for index < 0
1635 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001636 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001637}
1638
1639void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1641 instruction,
1642 LocationSummary::kCallOnSlowPath);
1643 locations->SetInAt(0, Location::RequiresRegister());
1644 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001645 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001646 locations->AddTemp(Location::RequiresRegister());
1647}
1648
1649void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1650 LocationSummary* locations = instruction->GetLocations();
1651 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1652 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1653 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1654
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001655 SlowPathCodeMIPS64* slow_path =
1656 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001657 codegen_->AddSlowPath(slow_path);
1658
1659 // TODO: avoid this check if we know obj is not null.
1660 __ Beqzc(obj, slow_path->GetExitLabel());
1661 // Compare the class of `obj` with `cls`.
1662 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1663 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1664 __ Bind(slow_path->GetExitLabel());
1665}
1666
1667void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1668 LocationSummary* locations =
1669 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1670 locations->SetInAt(0, Location::RequiresRegister());
1671 if (check->HasUses()) {
1672 locations->SetOut(Location::SameAsFirstInput());
1673 }
1674}
1675
1676void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1677 // We assume the class is not null.
1678 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1679 check->GetLoadClass(),
1680 check,
1681 check->GetDexPc(),
1682 true);
1683 codegen_->AddSlowPath(slow_path);
1684 GenerateClassInitializationCheck(slow_path,
1685 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1686}
1687
1688void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1689 Primitive::Type in_type = compare->InputAt(0)->GetType();
1690
Alexey Frunze299a9392015-12-08 16:08:02 -08001691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001692
1693 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001694 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001695 case Primitive::kPrimLong:
1696 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001697 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1699 break;
1700
1701 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001702 case Primitive::kPrimDouble:
1703 locations->SetInAt(0, Location::RequiresFpuRegister());
1704 locations->SetInAt(1, Location::RequiresFpuRegister());
1705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707
1708 default:
1709 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1710 }
1711}
1712
1713void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1714 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001715 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001716 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1717
1718 // 0 if: left == right
1719 // 1 if: left > right
1720 // -1 if: left < right
1721 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08001722 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001723 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001724 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001725 Location rhs_location = locations->InAt(1);
1726 bool use_imm = rhs_location.IsConstant();
1727 GpuRegister rhs = ZERO;
1728 if (use_imm) {
Aart Bika19616e2016-02-01 18:57:58 -08001729 if (in_type == Primitive::kPrimInt) {
1730 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1731 if (value != 0) {
1732 rhs = AT;
1733 __ LoadConst32(rhs, value);
1734 }
1735 } else {
1736 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1737 if (value != 0) {
1738 rhs = AT;
1739 __ LoadConst64(rhs, value);
1740 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001741 }
1742 } else {
1743 rhs = rhs_location.AsRegister<GpuRegister>();
1744 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001745 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001746 __ Slt(res, rhs, lhs);
1747 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001748 break;
1749 }
1750
Alexey Frunze299a9392015-12-08 16:08:02 -08001751 case Primitive::kPrimFloat: {
1752 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1753 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1754 Mips64Label done;
1755 __ CmpEqS(FTMP, lhs, rhs);
1756 __ LoadConst32(res, 0);
1757 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001758 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001759 __ CmpLtS(FTMP, lhs, rhs);
1760 __ LoadConst32(res, -1);
1761 __ Bc1nez(FTMP, &done);
1762 __ LoadConst32(res, 1);
1763 } else {
1764 __ CmpLtS(FTMP, rhs, lhs);
1765 __ LoadConst32(res, 1);
1766 __ Bc1nez(FTMP, &done);
1767 __ LoadConst32(res, -1);
1768 }
1769 __ Bind(&done);
1770 break;
1771 }
1772
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001774 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1775 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1776 Mips64Label done;
1777 __ CmpEqD(FTMP, lhs, rhs);
1778 __ LoadConst32(res, 0);
1779 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001780 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001781 __ CmpLtD(FTMP, lhs, rhs);
1782 __ LoadConst32(res, -1);
1783 __ Bc1nez(FTMP, &done);
1784 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001785 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001786 __ CmpLtD(FTMP, rhs, lhs);
1787 __ LoadConst32(res, 1);
1788 __ Bc1nez(FTMP, &done);
1789 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001790 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001791 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792 break;
1793 }
1794
1795 default:
1796 LOG(FATAL) << "Unimplemented compare type " << in_type;
1797 }
1798}
1799
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001802 switch (instruction->InputAt(0)->GetType()) {
1803 default:
1804 case Primitive::kPrimLong:
1805 locations->SetInAt(0, Location::RequiresRegister());
1806 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1807 break;
1808
1809 case Primitive::kPrimFloat:
1810 case Primitive::kPrimDouble:
1811 locations->SetInAt(0, Location::RequiresFpuRegister());
1812 locations->SetInAt(1, Location::RequiresFpuRegister());
1813 break;
1814 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001815 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1817 }
1818}
1819
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001821 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001822 return;
1823 }
1824
Alexey Frunze299a9392015-12-08 16:08:02 -08001825 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001826 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001827 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001828 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001829
Alexey Frunze299a9392015-12-08 16:08:02 -08001830 switch (type) {
1831 default:
1832 // Integer case.
1833 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1834 return;
1835 case Primitive::kPrimLong:
1836 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1837 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001838
Alexey Frunze299a9392015-12-08 16:08:02 -08001839 case Primitive::kPrimFloat:
1840 case Primitive::kPrimDouble:
1841 // TODO: don't use branches.
1842 GenerateFpCompareAndBranch(instruction->GetCondition(),
1843 instruction->IsGtBias(),
1844 type,
1845 locations,
1846 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001847 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001848 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001849
1850 // Convert the branches into the result.
1851 Mips64Label done;
1852
1853 // False case: result = 0.
1854 __ LoadConst32(dst, 0);
1855 __ Bc(&done);
1856
1857 // True case: result = 1.
1858 __ Bind(&true_label);
1859 __ LoadConst32(dst, 1);
1860 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001861}
1862
Alexey Frunzec857c742015-09-23 15:12:39 -07001863void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1864 DCHECK(instruction->IsDiv() || instruction->IsRem());
1865 Primitive::Type type = instruction->GetResultType();
1866
1867 LocationSummary* locations = instruction->GetLocations();
1868 Location second = locations->InAt(1);
1869 DCHECK(second.IsConstant());
1870
1871 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1872 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1873 int64_t imm = Int64FromConstant(second.GetConstant());
1874 DCHECK(imm == 1 || imm == -1);
1875
1876 if (instruction->IsRem()) {
1877 __ Move(out, ZERO);
1878 } else {
1879 if (imm == -1) {
1880 if (type == Primitive::kPrimInt) {
1881 __ Subu(out, ZERO, dividend);
1882 } else {
1883 DCHECK_EQ(type, Primitive::kPrimLong);
1884 __ Dsubu(out, ZERO, dividend);
1885 }
1886 } else if (out != dividend) {
1887 __ Move(out, dividend);
1888 }
1889 }
1890}
1891
1892void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1893 DCHECK(instruction->IsDiv() || instruction->IsRem());
1894 Primitive::Type type = instruction->GetResultType();
1895
1896 LocationSummary* locations = instruction->GetLocations();
1897 Location second = locations->InAt(1);
1898 DCHECK(second.IsConstant());
1899
1900 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1901 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1902 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001903 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001904 int ctz_imm = CTZ(abs_imm);
1905
1906 if (instruction->IsDiv()) {
1907 if (type == Primitive::kPrimInt) {
1908 if (ctz_imm == 1) {
1909 // Fast path for division by +/-2, which is very common.
1910 __ Srl(TMP, dividend, 31);
1911 } else {
1912 __ Sra(TMP, dividend, 31);
1913 __ Srl(TMP, TMP, 32 - ctz_imm);
1914 }
1915 __ Addu(out, dividend, TMP);
1916 __ Sra(out, out, ctz_imm);
1917 if (imm < 0) {
1918 __ Subu(out, ZERO, out);
1919 }
1920 } else {
1921 DCHECK_EQ(type, Primitive::kPrimLong);
1922 if (ctz_imm == 1) {
1923 // Fast path for division by +/-2, which is very common.
1924 __ Dsrl32(TMP, dividend, 31);
1925 } else {
1926 __ Dsra32(TMP, dividend, 31);
1927 if (ctz_imm > 32) {
1928 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1929 } else {
1930 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1931 }
1932 }
1933 __ Daddu(out, dividend, TMP);
1934 if (ctz_imm < 32) {
1935 __ Dsra(out, out, ctz_imm);
1936 } else {
1937 __ Dsra32(out, out, ctz_imm - 32);
1938 }
1939 if (imm < 0) {
1940 __ Dsubu(out, ZERO, out);
1941 }
1942 }
1943 } else {
1944 if (type == Primitive::kPrimInt) {
1945 if (ctz_imm == 1) {
1946 // Fast path for modulo +/-2, which is very common.
1947 __ Sra(TMP, dividend, 31);
1948 __ Subu(out, dividend, TMP);
1949 __ Andi(out, out, 1);
1950 __ Addu(out, out, TMP);
1951 } else {
1952 __ Sra(TMP, dividend, 31);
1953 __ Srl(TMP, TMP, 32 - ctz_imm);
1954 __ Addu(out, dividend, TMP);
1955 if (IsUint<16>(abs_imm - 1)) {
1956 __ Andi(out, out, abs_imm - 1);
1957 } else {
1958 __ Sll(out, out, 32 - ctz_imm);
1959 __ Srl(out, out, 32 - ctz_imm);
1960 }
1961 __ Subu(out, out, TMP);
1962 }
1963 } else {
1964 DCHECK_EQ(type, Primitive::kPrimLong);
1965 if (ctz_imm == 1) {
1966 // Fast path for modulo +/-2, which is very common.
1967 __ Dsra32(TMP, dividend, 31);
1968 __ Dsubu(out, dividend, TMP);
1969 __ Andi(out, out, 1);
1970 __ Daddu(out, out, TMP);
1971 } else {
1972 __ Dsra32(TMP, dividend, 31);
1973 if (ctz_imm > 32) {
1974 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1975 } else {
1976 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1977 }
1978 __ Daddu(out, dividend, TMP);
1979 if (IsUint<16>(abs_imm - 1)) {
1980 __ Andi(out, out, abs_imm - 1);
1981 } else {
1982 if (ctz_imm > 32) {
1983 __ Dsll(out, out, 64 - ctz_imm);
1984 __ Dsrl(out, out, 64 - ctz_imm);
1985 } else {
1986 __ Dsll32(out, out, 32 - ctz_imm);
1987 __ Dsrl32(out, out, 32 - ctz_imm);
1988 }
1989 }
1990 __ Dsubu(out, out, TMP);
1991 }
1992 }
1993 }
1994}
1995
1996void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1997 DCHECK(instruction->IsDiv() || instruction->IsRem());
1998
1999 LocationSummary* locations = instruction->GetLocations();
2000 Location second = locations->InAt(1);
2001 DCHECK(second.IsConstant());
2002
2003 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2004 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2005 int64_t imm = Int64FromConstant(second.GetConstant());
2006
2007 Primitive::Type type = instruction->GetResultType();
2008 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2009
2010 int64_t magic;
2011 int shift;
2012 CalculateMagicAndShiftForDivRem(imm,
2013 (type == Primitive::kPrimLong),
2014 &magic,
2015 &shift);
2016
2017 if (type == Primitive::kPrimInt) {
2018 __ LoadConst32(TMP, magic);
2019 __ MuhR6(TMP, dividend, TMP);
2020
2021 if (imm > 0 && magic < 0) {
2022 __ Addu(TMP, TMP, dividend);
2023 } else if (imm < 0 && magic > 0) {
2024 __ Subu(TMP, TMP, dividend);
2025 }
2026
2027 if (shift != 0) {
2028 __ Sra(TMP, TMP, shift);
2029 }
2030
2031 if (instruction->IsDiv()) {
2032 __ Sra(out, TMP, 31);
2033 __ Subu(out, TMP, out);
2034 } else {
2035 __ Sra(AT, TMP, 31);
2036 __ Subu(AT, TMP, AT);
2037 __ LoadConst32(TMP, imm);
2038 __ MulR6(TMP, AT, TMP);
2039 __ Subu(out, dividend, TMP);
2040 }
2041 } else {
2042 __ LoadConst64(TMP, magic);
2043 __ Dmuh(TMP, dividend, TMP);
2044
2045 if (imm > 0 && magic < 0) {
2046 __ Daddu(TMP, TMP, dividend);
2047 } else if (imm < 0 && magic > 0) {
2048 __ Dsubu(TMP, TMP, dividend);
2049 }
2050
2051 if (shift >= 32) {
2052 __ Dsra32(TMP, TMP, shift - 32);
2053 } else if (shift > 0) {
2054 __ Dsra(TMP, TMP, shift);
2055 }
2056
2057 if (instruction->IsDiv()) {
2058 __ Dsra32(out, TMP, 31);
2059 __ Dsubu(out, TMP, out);
2060 } else {
2061 __ Dsra32(AT, TMP, 31);
2062 __ Dsubu(AT, TMP, AT);
2063 __ LoadConst64(TMP, imm);
2064 __ Dmul(TMP, AT, TMP);
2065 __ Dsubu(out, dividend, TMP);
2066 }
2067 }
2068}
2069
2070void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2071 DCHECK(instruction->IsDiv() || instruction->IsRem());
2072 Primitive::Type type = instruction->GetResultType();
2073 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2074
2075 LocationSummary* locations = instruction->GetLocations();
2076 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2077 Location second = locations->InAt(1);
2078
2079 if (second.IsConstant()) {
2080 int64_t imm = Int64FromConstant(second.GetConstant());
2081 if (imm == 0) {
2082 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2083 } else if (imm == 1 || imm == -1) {
2084 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002085 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002086 DivRemByPowerOfTwo(instruction);
2087 } else {
2088 DCHECK(imm <= -2 || imm >= 2);
2089 GenerateDivRemWithAnyConstant(instruction);
2090 }
2091 } else {
2092 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2093 GpuRegister divisor = second.AsRegister<GpuRegister>();
2094 if (instruction->IsDiv()) {
2095 if (type == Primitive::kPrimInt)
2096 __ DivR6(out, dividend, divisor);
2097 else
2098 __ Ddiv(out, dividend, divisor);
2099 } else {
2100 if (type == Primitive::kPrimInt)
2101 __ ModR6(out, dividend, divisor);
2102 else
2103 __ Dmod(out, dividend, divisor);
2104 }
2105 }
2106}
2107
Alexey Frunze4dda3372015-06-01 18:31:49 -07002108void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2109 LocationSummary* locations =
2110 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2111 switch (div->GetResultType()) {
2112 case Primitive::kPrimInt:
2113 case Primitive::kPrimLong:
2114 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002115 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2117 break;
2118
2119 case Primitive::kPrimFloat:
2120 case Primitive::kPrimDouble:
2121 locations->SetInAt(0, Location::RequiresFpuRegister());
2122 locations->SetInAt(1, Location::RequiresFpuRegister());
2123 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2128 }
2129}
2130
2131void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2132 Primitive::Type type = instruction->GetType();
2133 LocationSummary* locations = instruction->GetLocations();
2134
2135 switch (type) {
2136 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002137 case Primitive::kPrimLong:
2138 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002139 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002140 case Primitive::kPrimFloat:
2141 case Primitive::kPrimDouble: {
2142 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2143 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2144 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2145 if (type == Primitive::kPrimFloat)
2146 __ DivS(dst, lhs, rhs);
2147 else
2148 __ DivD(dst, lhs, rhs);
2149 break;
2150 }
2151 default:
2152 LOG(FATAL) << "Unexpected div type " << type;
2153 }
2154}
2155
2156void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002157 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2158 ? LocationSummary::kCallOnSlowPath
2159 : LocationSummary::kNoCall;
2160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002161 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2162 if (instruction->HasUses()) {
2163 locations->SetOut(Location::SameAsFirstInput());
2164 }
2165}
2166
2167void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2168 SlowPathCodeMIPS64* slow_path =
2169 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2170 codegen_->AddSlowPath(slow_path);
2171 Location value = instruction->GetLocations()->InAt(0);
2172
2173 Primitive::Type type = instruction->GetType();
2174
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002175 if (!Primitive::IsIntegralType(type)) {
2176 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002177 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002178 }
2179
2180 if (value.IsConstant()) {
2181 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2182 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002183 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002184 } else {
2185 // A division by a non-null constant is valid. We don't need to perform
2186 // any check, so simply fall through.
2187 }
2188 } else {
2189 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2190 }
2191}
2192
2193void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2194 LocationSummary* locations =
2195 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2196 locations->SetOut(Location::ConstantLocation(constant));
2197}
2198
2199void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2200 // Will be generated at use site.
2201}
2202
2203void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2204 exit->SetLocations(nullptr);
2205}
2206
2207void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2208}
2209
2210void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2213 locations->SetOut(Location::ConstantLocation(constant));
2214}
2215
2216void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2217 // Will be generated at use site.
2218}
2219
David Brazdilfc6a86a2015-06-26 10:33:45 +00002220void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002221 DCHECK(!successor->IsExitBlock());
2222 HBasicBlock* block = got->GetBlock();
2223 HInstruction* previous = got->GetPrevious();
2224 HLoopInformation* info = block->GetLoopInformation();
2225
2226 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2227 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2228 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2229 return;
2230 }
2231 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2232 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2233 }
2234 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002235 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002236 }
2237}
2238
David Brazdilfc6a86a2015-06-26 10:33:45 +00002239void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2240 got->SetLocations(nullptr);
2241}
2242
2243void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2244 HandleGoto(got, got->GetSuccessor());
2245}
2246
2247void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2248 try_boundary->SetLocations(nullptr);
2249}
2250
2251void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2252 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2253 if (!successor->IsExitBlock()) {
2254 HandleGoto(try_boundary, successor);
2255 }
2256}
2257
Alexey Frunze299a9392015-12-08 16:08:02 -08002258void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2259 bool is64bit,
2260 LocationSummary* locations) {
2261 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2262 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2263 Location rhs_location = locations->InAt(1);
2264 GpuRegister rhs_reg = ZERO;
2265 int64_t rhs_imm = 0;
2266 bool use_imm = rhs_location.IsConstant();
2267 if (use_imm) {
2268 if (is64bit) {
2269 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2270 } else {
2271 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2272 }
2273 } else {
2274 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2275 }
2276 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2277
2278 switch (cond) {
2279 case kCondEQ:
2280 case kCondNE:
2281 if (use_imm && IsUint<16>(rhs_imm)) {
2282 __ Xori(dst, lhs, rhs_imm);
2283 } else {
2284 if (use_imm) {
2285 rhs_reg = TMP;
2286 __ LoadConst64(rhs_reg, rhs_imm);
2287 }
2288 __ Xor(dst, lhs, rhs_reg);
2289 }
2290 if (cond == kCondEQ) {
2291 __ Sltiu(dst, dst, 1);
2292 } else {
2293 __ Sltu(dst, ZERO, dst);
2294 }
2295 break;
2296
2297 case kCondLT:
2298 case kCondGE:
2299 if (use_imm && IsInt<16>(rhs_imm)) {
2300 __ Slti(dst, lhs, rhs_imm);
2301 } else {
2302 if (use_imm) {
2303 rhs_reg = TMP;
2304 __ LoadConst64(rhs_reg, rhs_imm);
2305 }
2306 __ Slt(dst, lhs, rhs_reg);
2307 }
2308 if (cond == kCondGE) {
2309 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2310 // only the slt instruction but no sge.
2311 __ Xori(dst, dst, 1);
2312 }
2313 break;
2314
2315 case kCondLE:
2316 case kCondGT:
2317 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2318 // Simulate lhs <= rhs via lhs < rhs + 1.
2319 __ Slti(dst, lhs, rhs_imm_plus_one);
2320 if (cond == kCondGT) {
2321 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2322 // only the slti instruction but no sgti.
2323 __ Xori(dst, dst, 1);
2324 }
2325 } else {
2326 if (use_imm) {
2327 rhs_reg = TMP;
2328 __ LoadConst64(rhs_reg, rhs_imm);
2329 }
2330 __ Slt(dst, rhs_reg, lhs);
2331 if (cond == kCondLE) {
2332 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2333 // only the slt instruction but no sle.
2334 __ Xori(dst, dst, 1);
2335 }
2336 }
2337 break;
2338
2339 case kCondB:
2340 case kCondAE:
2341 if (use_imm && IsInt<16>(rhs_imm)) {
2342 // Sltiu sign-extends its 16-bit immediate operand before
2343 // the comparison and thus lets us compare directly with
2344 // unsigned values in the ranges [0, 0x7fff] and
2345 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2346 __ Sltiu(dst, lhs, rhs_imm);
2347 } else {
2348 if (use_imm) {
2349 rhs_reg = TMP;
2350 __ LoadConst64(rhs_reg, rhs_imm);
2351 }
2352 __ Sltu(dst, lhs, rhs_reg);
2353 }
2354 if (cond == kCondAE) {
2355 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2356 // only the sltu instruction but no sgeu.
2357 __ Xori(dst, dst, 1);
2358 }
2359 break;
2360
2361 case kCondBE:
2362 case kCondA:
2363 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2364 // Simulate lhs <= rhs via lhs < rhs + 1.
2365 // Note that this only works if rhs + 1 does not overflow
2366 // to 0, hence the check above.
2367 // Sltiu sign-extends its 16-bit immediate operand before
2368 // the comparison and thus lets us compare directly with
2369 // unsigned values in the ranges [0, 0x7fff] and
2370 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2371 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2372 if (cond == kCondA) {
2373 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2374 // only the sltiu instruction but no sgtiu.
2375 __ Xori(dst, dst, 1);
2376 }
2377 } else {
2378 if (use_imm) {
2379 rhs_reg = TMP;
2380 __ LoadConst64(rhs_reg, rhs_imm);
2381 }
2382 __ Sltu(dst, rhs_reg, lhs);
2383 if (cond == kCondBE) {
2384 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2385 // only the sltu instruction but no sleu.
2386 __ Xori(dst, dst, 1);
2387 }
2388 }
2389 break;
2390 }
2391}
2392
2393void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2394 bool is64bit,
2395 LocationSummary* locations,
2396 Mips64Label* label) {
2397 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2398 Location rhs_location = locations->InAt(1);
2399 GpuRegister rhs_reg = ZERO;
2400 int64_t rhs_imm = 0;
2401 bool use_imm = rhs_location.IsConstant();
2402 if (use_imm) {
2403 if (is64bit) {
2404 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2405 } else {
2406 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2407 }
2408 } else {
2409 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2410 }
2411
2412 if (use_imm && rhs_imm == 0) {
2413 switch (cond) {
2414 case kCondEQ:
2415 case kCondBE: // <= 0 if zero
2416 __ Beqzc(lhs, label);
2417 break;
2418 case kCondNE:
2419 case kCondA: // > 0 if non-zero
2420 __ Bnezc(lhs, label);
2421 break;
2422 case kCondLT:
2423 __ Bltzc(lhs, label);
2424 break;
2425 case kCondGE:
2426 __ Bgezc(lhs, label);
2427 break;
2428 case kCondLE:
2429 __ Blezc(lhs, label);
2430 break;
2431 case kCondGT:
2432 __ Bgtzc(lhs, label);
2433 break;
2434 case kCondB: // always false
2435 break;
2436 case kCondAE: // always true
2437 __ Bc(label);
2438 break;
2439 }
2440 } else {
2441 if (use_imm) {
2442 rhs_reg = TMP;
2443 __ LoadConst64(rhs_reg, rhs_imm);
2444 }
2445 switch (cond) {
2446 case kCondEQ:
2447 __ Beqc(lhs, rhs_reg, label);
2448 break;
2449 case kCondNE:
2450 __ Bnec(lhs, rhs_reg, label);
2451 break;
2452 case kCondLT:
2453 __ Bltc(lhs, rhs_reg, label);
2454 break;
2455 case kCondGE:
2456 __ Bgec(lhs, rhs_reg, label);
2457 break;
2458 case kCondLE:
2459 __ Bgec(rhs_reg, lhs, label);
2460 break;
2461 case kCondGT:
2462 __ Bltc(rhs_reg, lhs, label);
2463 break;
2464 case kCondB:
2465 __ Bltuc(lhs, rhs_reg, label);
2466 break;
2467 case kCondAE:
2468 __ Bgeuc(lhs, rhs_reg, label);
2469 break;
2470 case kCondBE:
2471 __ Bgeuc(rhs_reg, lhs, label);
2472 break;
2473 case kCondA:
2474 __ Bltuc(rhs_reg, lhs, label);
2475 break;
2476 }
2477 }
2478}
2479
2480void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2481 bool gt_bias,
2482 Primitive::Type type,
2483 LocationSummary* locations,
2484 Mips64Label* label) {
2485 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2486 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2487 if (type == Primitive::kPrimFloat) {
2488 switch (cond) {
2489 case kCondEQ:
2490 __ CmpEqS(FTMP, lhs, rhs);
2491 __ Bc1nez(FTMP, label);
2492 break;
2493 case kCondNE:
2494 __ CmpEqS(FTMP, lhs, rhs);
2495 __ Bc1eqz(FTMP, label);
2496 break;
2497 case kCondLT:
2498 if (gt_bias) {
2499 __ CmpLtS(FTMP, lhs, rhs);
2500 } else {
2501 __ CmpUltS(FTMP, lhs, rhs);
2502 }
2503 __ Bc1nez(FTMP, label);
2504 break;
2505 case kCondLE:
2506 if (gt_bias) {
2507 __ CmpLeS(FTMP, lhs, rhs);
2508 } else {
2509 __ CmpUleS(FTMP, lhs, rhs);
2510 }
2511 __ Bc1nez(FTMP, label);
2512 break;
2513 case kCondGT:
2514 if (gt_bias) {
2515 __ CmpUltS(FTMP, rhs, lhs);
2516 } else {
2517 __ CmpLtS(FTMP, rhs, lhs);
2518 }
2519 __ Bc1nez(FTMP, label);
2520 break;
2521 case kCondGE:
2522 if (gt_bias) {
2523 __ CmpUleS(FTMP, rhs, lhs);
2524 } else {
2525 __ CmpLeS(FTMP, rhs, lhs);
2526 }
2527 __ Bc1nez(FTMP, label);
2528 break;
2529 default:
2530 LOG(FATAL) << "Unexpected non-floating-point condition";
2531 }
2532 } else {
2533 DCHECK_EQ(type, Primitive::kPrimDouble);
2534 switch (cond) {
2535 case kCondEQ:
2536 __ CmpEqD(FTMP, lhs, rhs);
2537 __ Bc1nez(FTMP, label);
2538 break;
2539 case kCondNE:
2540 __ CmpEqD(FTMP, lhs, rhs);
2541 __ Bc1eqz(FTMP, label);
2542 break;
2543 case kCondLT:
2544 if (gt_bias) {
2545 __ CmpLtD(FTMP, lhs, rhs);
2546 } else {
2547 __ CmpUltD(FTMP, lhs, rhs);
2548 }
2549 __ Bc1nez(FTMP, label);
2550 break;
2551 case kCondLE:
2552 if (gt_bias) {
2553 __ CmpLeD(FTMP, lhs, rhs);
2554 } else {
2555 __ CmpUleD(FTMP, lhs, rhs);
2556 }
2557 __ Bc1nez(FTMP, label);
2558 break;
2559 case kCondGT:
2560 if (gt_bias) {
2561 __ CmpUltD(FTMP, rhs, lhs);
2562 } else {
2563 __ CmpLtD(FTMP, rhs, lhs);
2564 }
2565 __ Bc1nez(FTMP, label);
2566 break;
2567 case kCondGE:
2568 if (gt_bias) {
2569 __ CmpUleD(FTMP, rhs, lhs);
2570 } else {
2571 __ CmpLeD(FTMP, rhs, lhs);
2572 }
2573 __ Bc1nez(FTMP, label);
2574 break;
2575 default:
2576 LOG(FATAL) << "Unexpected non-floating-point condition";
2577 }
2578 }
2579}
2580
Alexey Frunze4dda3372015-06-01 18:31:49 -07002581void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002582 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002583 Mips64Label* true_target,
2584 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002585 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586
David Brazdil0debae72015-11-12 18:37:00 +00002587 if (true_target == nullptr && false_target == nullptr) {
2588 // Nothing to do. The code always falls through.
2589 return;
2590 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002591 // Constant condition, statically compared against "true" (integer value 1).
2592 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002593 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002594 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002595 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002597 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002598 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002599 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002600 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002601 }
David Brazdil0debae72015-11-12 18:37:00 +00002602 return;
2603 }
2604
2605 // The following code generates these patterns:
2606 // (1) true_target == nullptr && false_target != nullptr
2607 // - opposite condition true => branch to false_target
2608 // (2) true_target != nullptr && false_target == nullptr
2609 // - condition true => branch to true_target
2610 // (3) true_target != nullptr && false_target != nullptr
2611 // - condition true => branch to true_target
2612 // - branch to false_target
2613 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002614 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002615 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002616 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002617 if (true_target == nullptr) {
2618 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2619 } else {
2620 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2621 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002622 } else {
2623 // The condition instruction has not been materialized, use its inputs as
2624 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002625 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002626 Primitive::Type type = condition->InputAt(0)->GetType();
2627 LocationSummary* locations = cond->GetLocations();
2628 IfCondition if_cond = condition->GetCondition();
2629 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002630
David Brazdil0debae72015-11-12 18:37:00 +00002631 if (true_target == nullptr) {
2632 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002633 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002634 }
2635
Alexey Frunze299a9392015-12-08 16:08:02 -08002636 switch (type) {
2637 default:
2638 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2639 break;
2640 case Primitive::kPrimLong:
2641 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2642 break;
2643 case Primitive::kPrimFloat:
2644 case Primitive::kPrimDouble:
2645 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2646 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002647 }
2648 }
David Brazdil0debae72015-11-12 18:37:00 +00002649
2650 // If neither branch falls through (case 3), the conditional branch to `true_target`
2651 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2652 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002653 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654 }
2655}
2656
2657void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2658 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002659 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 locations->SetInAt(0, Location::RequiresRegister());
2661 }
2662}
2663
2664void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002665 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2666 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002667 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002668 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002669 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002670 nullptr : codegen_->GetLabelOf(false_successor);
2671 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002672}
2673
2674void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2675 LocationSummary* locations = new (GetGraph()->GetArena())
2676 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002677 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002678 locations->SetInAt(0, Location::RequiresRegister());
2679 }
2680}
2681
2682void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002683 SlowPathCodeMIPS64* slow_path =
2684 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002685 GenerateTestAndBranch(deoptimize,
2686 /* condition_input_index */ 0,
2687 slow_path->GetEntryLabel(),
2688 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689}
2690
David Brazdil74eb1b22015-12-14 11:44:01 +00002691void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2693 if (Primitive::IsFloatingPointType(select->GetType())) {
2694 locations->SetInAt(0, Location::RequiresFpuRegister());
2695 locations->SetInAt(1, Location::RequiresFpuRegister());
2696 } else {
2697 locations->SetInAt(0, Location::RequiresRegister());
2698 locations->SetInAt(1, Location::RequiresRegister());
2699 }
2700 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2701 locations->SetInAt(2, Location::RequiresRegister());
2702 }
2703 locations->SetOut(Location::SameAsFirstInput());
2704}
2705
2706void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2707 LocationSummary* locations = select->GetLocations();
2708 Mips64Label false_target;
2709 GenerateTestAndBranch(select,
2710 /* condition_input_index */ 2,
2711 /* true_target */ nullptr,
2712 &false_target);
2713 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2714 __ Bind(&false_target);
2715}
2716
David Srbecky0cf44932015-12-09 14:09:59 +00002717void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2718 new (GetGraph()->GetArena()) LocationSummary(info);
2719}
2720
2721void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00002722 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
2723}
2724
2725void CodeGeneratorMIPS64::GenerateNop() {
2726 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002727}
2728
Alexey Frunze4dda3372015-06-01 18:31:49 -07002729void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2730 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2731 LocationSummary* locations =
2732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2733 locations->SetInAt(0, Location::RequiresRegister());
2734 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2735 locations->SetOut(Location::RequiresFpuRegister());
2736 } else {
2737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2738 }
2739}
2740
2741void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2742 const FieldInfo& field_info) {
2743 Primitive::Type type = field_info.GetFieldType();
2744 LocationSummary* locations = instruction->GetLocations();
2745 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2746 LoadOperandType load_type = kLoadUnsignedByte;
2747 switch (type) {
2748 case Primitive::kPrimBoolean:
2749 load_type = kLoadUnsignedByte;
2750 break;
2751 case Primitive::kPrimByte:
2752 load_type = kLoadSignedByte;
2753 break;
2754 case Primitive::kPrimShort:
2755 load_type = kLoadSignedHalfword;
2756 break;
2757 case Primitive::kPrimChar:
2758 load_type = kLoadUnsignedHalfword;
2759 break;
2760 case Primitive::kPrimInt:
2761 case Primitive::kPrimFloat:
2762 load_type = kLoadWord;
2763 break;
2764 case Primitive::kPrimLong:
2765 case Primitive::kPrimDouble:
2766 load_type = kLoadDoubleword;
2767 break;
2768 case Primitive::kPrimNot:
2769 load_type = kLoadUnsignedWord;
2770 break;
2771 case Primitive::kPrimVoid:
2772 LOG(FATAL) << "Unreachable type " << type;
2773 UNREACHABLE();
2774 }
2775 if (!Primitive::IsFloatingPointType(type)) {
2776 DCHECK(locations->Out().IsRegister());
2777 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2778 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2779 } else {
2780 DCHECK(locations->Out().IsFpuRegister());
2781 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2782 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2783 }
2784
2785 codegen_->MaybeRecordImplicitNullCheck(instruction);
2786 // TODO: memory barrier?
2787}
2788
2789void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2790 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2791 LocationSummary* locations =
2792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2793 locations->SetInAt(0, Location::RequiresRegister());
2794 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2795 locations->SetInAt(1, Location::RequiresFpuRegister());
2796 } else {
2797 locations->SetInAt(1, Location::RequiresRegister());
2798 }
2799}
2800
2801void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002802 const FieldInfo& field_info,
2803 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002804 Primitive::Type type = field_info.GetFieldType();
2805 LocationSummary* locations = instruction->GetLocations();
2806 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2807 StoreOperandType store_type = kStoreByte;
2808 switch (type) {
2809 case Primitive::kPrimBoolean:
2810 case Primitive::kPrimByte:
2811 store_type = kStoreByte;
2812 break;
2813 case Primitive::kPrimShort:
2814 case Primitive::kPrimChar:
2815 store_type = kStoreHalfword;
2816 break;
2817 case Primitive::kPrimInt:
2818 case Primitive::kPrimFloat:
2819 case Primitive::kPrimNot:
2820 store_type = kStoreWord;
2821 break;
2822 case Primitive::kPrimLong:
2823 case Primitive::kPrimDouble:
2824 store_type = kStoreDoubleword;
2825 break;
2826 case Primitive::kPrimVoid:
2827 LOG(FATAL) << "Unreachable type " << type;
2828 UNREACHABLE();
2829 }
2830 if (!Primitive::IsFloatingPointType(type)) {
2831 DCHECK(locations->InAt(1).IsRegister());
2832 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2833 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2834 } else {
2835 DCHECK(locations->InAt(1).IsFpuRegister());
2836 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2837 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2838 }
2839
2840 codegen_->MaybeRecordImplicitNullCheck(instruction);
2841 // TODO: memory barriers?
2842 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2843 DCHECK(locations->InAt(1).IsRegister());
2844 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002845 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002846 }
2847}
2848
2849void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2850 HandleFieldGet(instruction, instruction->GetFieldInfo());
2851}
2852
2853void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2854 HandleFieldGet(instruction, instruction->GetFieldInfo());
2855}
2856
2857void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2858 HandleFieldSet(instruction, instruction->GetFieldInfo());
2859}
2860
2861void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002862 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002863}
2864
2865void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2866 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002867 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002868 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2869 locations->SetInAt(0, Location::RequiresRegister());
2870 locations->SetInAt(1, Location::RequiresRegister());
2871 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002872 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002873 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2874}
2875
2876void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2877 LocationSummary* locations = instruction->GetLocations();
2878 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2879 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2880 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2881
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002882 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002883
2884 // Return 0 if `obj` is null.
2885 // TODO: Avoid this check if we know `obj` is not null.
2886 __ Move(out, ZERO);
2887 __ Beqzc(obj, &done);
2888
2889 // Compare the class of `obj` with `cls`.
2890 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002891 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002892 // Classes must be equal for the instanceof to succeed.
2893 __ Xor(out, out, cls);
2894 __ Sltiu(out, out, 1);
2895 } else {
2896 // If the classes are not equal, we go into a slow path.
2897 DCHECK(locations->OnlyCallsOnSlowPath());
2898 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002899 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002900 codegen_->AddSlowPath(slow_path);
2901 __ Bnec(out, cls, slow_path->GetEntryLabel());
2902 __ LoadConst32(out, 1);
2903 __ Bind(slow_path->GetExitLabel());
2904 }
2905
2906 __ Bind(&done);
2907}
2908
2909void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2911 locations->SetOut(Location::ConstantLocation(constant));
2912}
2913
2914void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2915 // Will be generated at use site.
2916}
2917
2918void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2920 locations->SetOut(Location::ConstantLocation(constant));
2921}
2922
2923void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2924 // Will be generated at use site.
2925}
2926
Calin Juravle175dc732015-08-25 15:42:32 +01002927void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2928 // The trampoline uses the same calling convention as dex calling conventions,
2929 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2930 // the method_idx.
2931 HandleInvoke(invoke);
2932}
2933
2934void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2935 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2936}
2937
Alexey Frunze4dda3372015-06-01 18:31:49 -07002938void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2939 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2940 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2941}
2942
2943void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2944 HandleInvoke(invoke);
2945 // The register T0 is required to be used for the hidden argument in
2946 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2947 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2948}
2949
2950void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2951 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2952 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2953 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2954 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2955 Location receiver = invoke->GetLocations()->InAt(0);
2956 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002957 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002958
2959 // Set the hidden argument.
2960 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2961 invoke->GetDexMethodIndex());
2962
2963 // temp = object->GetClass();
2964 if (receiver.IsStackSlot()) {
2965 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2966 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2967 } else {
2968 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2969 }
2970 codegen_->MaybeRecordImplicitNullCheck(invoke);
2971 // temp = temp->GetImtEntryAt(method_offset);
2972 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2973 // T9 = temp->GetEntryPoint();
2974 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2975 // T9();
2976 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002977 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002978 DCHECK(!codegen_->IsLeafMethod());
2979 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2980}
2981
2982void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002983 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2984 if (intrinsic.TryDispatch(invoke)) {
2985 return;
2986 }
2987
Alexey Frunze4dda3372015-06-01 18:31:49 -07002988 HandleInvoke(invoke);
2989}
2990
2991void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002992 // Explicit clinit checks triggered by static invokes must have been pruned by
2993 // art::PrepareForRegisterAllocation.
2994 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002995
Chris Larsen3039e382015-08-26 07:54:08 -07002996 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2997 if (intrinsic.TryDispatch(invoke)) {
2998 return;
2999 }
3000
Alexey Frunze4dda3372015-06-01 18:31:49 -07003001 HandleInvoke(invoke);
3002
3003 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3004 // clobbering somewhere else, reduce further register pressure by avoiding
3005 // allocation of a register for the current method pointer like on x86 baseline.
3006 // TODO: remove this once all the issues with register saving/restoring are
3007 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003008 if (invoke->HasCurrentMethodInput()) {
3009 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003010 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003011 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003012 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003013 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003014 }
3015}
3016
Chris Larsen3039e382015-08-26 07:54:08 -07003017static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003018 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003019 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3020 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021 return true;
3022 }
3023 return false;
3024}
3025
Vladimir Markodc151b22015-10-15 18:02:30 +01003026HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3027 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3028 MethodReference target_method ATTRIBUTE_UNUSED) {
3029 switch (desired_dispatch_info.method_load_kind) {
3030 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3031 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3032 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3033 return HInvokeStaticOrDirect::DispatchInfo {
3034 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3035 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3036 0u,
3037 0u
3038 };
3039 default:
3040 break;
3041 }
3042 switch (desired_dispatch_info.code_ptr_location) {
3043 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3044 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3045 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3046 return HInvokeStaticOrDirect::DispatchInfo {
3047 desired_dispatch_info.method_load_kind,
3048 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3049 desired_dispatch_info.method_load_data,
3050 0u
3051 };
3052 default:
3053 return desired_dispatch_info;
3054 }
3055}
3056
Alexey Frunze4dda3372015-06-01 18:31:49 -07003057void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3058 // All registers are assumed to be correctly set up per the calling convention.
3059
Vladimir Marko58155012015-08-19 12:49:41 +00003060 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3061 switch (invoke->GetMethodLoadKind()) {
3062 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3063 // temp = thread->string_init_entrypoint
3064 __ LoadFromOffset(kLoadDoubleword,
3065 temp.AsRegister<GpuRegister>(),
3066 TR,
3067 invoke->GetStringInitOffset());
3068 break;
3069 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003070 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003071 break;
3072 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3073 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3074 break;
3075 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003076 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003077 // TODO: Implement these types.
3078 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3079 LOG(FATAL) << "Unsupported";
3080 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003081 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003082 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003083 GpuRegister reg = temp.AsRegister<GpuRegister>();
3084 GpuRegister method_reg;
3085 if (current_method.IsRegister()) {
3086 method_reg = current_method.AsRegister<GpuRegister>();
3087 } else {
3088 // TODO: use the appropriate DCHECK() here if possible.
3089 // DCHECK(invoke->GetLocations()->Intrinsified());
3090 DCHECK(!current_method.IsValid());
3091 method_reg = reg;
3092 __ Ld(reg, SP, kCurrentMethodStackOffset);
3093 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003094
Vladimir Marko58155012015-08-19 12:49:41 +00003095 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003096 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003097 reg,
3098 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003099 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003100 // temp = temp[index_in_cache]
3101 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3102 __ LoadFromOffset(kLoadDoubleword,
3103 reg,
3104 reg,
3105 CodeGenerator::GetCachePointerOffset(index_in_cache));
3106 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003108 }
3109
Vladimir Marko58155012015-08-19 12:49:41 +00003110 switch (invoke->GetCodePtrLocation()) {
3111 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003112 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003113 break;
3114 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3115 // LR = invoke->GetDirectCodePtr();
3116 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3117 // LR()
3118 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003119 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003120 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003121 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003122 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3123 // TODO: Implement these types.
3124 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3125 LOG(FATAL) << "Unsupported";
3126 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003127 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3128 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3129 __ LoadFromOffset(kLoadDoubleword,
3130 T9,
3131 callee_method.AsRegister<GpuRegister>(),
3132 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003133 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003134 // T9()
3135 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003136 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003137 break;
3138 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003139 DCHECK(!IsLeafMethod());
3140}
3141
3142void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003143 // Explicit clinit checks triggered by static invokes must have been pruned by
3144 // art::PrepareForRegisterAllocation.
3145 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003146
3147 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3148 return;
3149 }
3150
3151 LocationSummary* locations = invoke->GetLocations();
3152 codegen_->GenerateStaticOrDirectCall(invoke,
3153 locations->HasTemps()
3154 ? locations->GetTemp(0)
3155 : Location::NoLocation());
3156 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3157}
3158
Alexey Frunze53afca12015-11-05 16:34:23 -08003159void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003160 // Use the calling convention instead of the location of the receiver, as
3161 // intrinsics may have put the receiver in a different register. In the intrinsics
3162 // slow path, the arguments have been moved to the right place, so here we are
3163 // guaranteed that the receiver is the first register of the calling convention.
3164 InvokeDexCallingConvention calling_convention;
3165 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3166
Alexey Frunze53afca12015-11-05 16:34:23 -08003167 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003168 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3169 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3170 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003171 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003172
3173 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003174 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003175 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003176 // temp = temp->GetMethodAt(method_offset);
3177 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3178 // T9 = temp->GetEntryPoint();
3179 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3180 // T9();
3181 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003182 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003183}
3184
3185void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3186 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3187 return;
3188 }
3189
3190 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003191 DCHECK(!codegen_->IsLeafMethod());
3192 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3193}
3194
3195void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003196 InvokeRuntimeCallingConvention calling_convention;
3197 CodeGenerator::CreateLoadClassLocationSummary(
3198 cls,
3199 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003200 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003201}
3202
3203void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3204 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003205 if (cls->NeedsAccessCheck()) {
3206 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3207 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3208 cls,
3209 cls->GetDexPc(),
3210 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003211 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003212 return;
3213 }
3214
3215 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3216 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3217 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003218 DCHECK(!cls->CanCallRuntime());
3219 DCHECK(!cls->MustGenerateClinitCheck());
3220 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3221 ArtMethod::DeclaringClassOffset().Int32Value());
3222 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003223 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3224 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003225 __ LoadFromOffset(
3226 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003227 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003228 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3229 DCHECK(cls->CanCallRuntime());
3230 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3231 cls,
3232 cls,
3233 cls->GetDexPc(),
3234 cls->MustGenerateClinitCheck());
3235 codegen_->AddSlowPath(slow_path);
3236 if (!cls->IsInDexCache()) {
3237 __ Beqzc(out, slow_path->GetEntryLabel());
3238 }
3239 if (cls->MustGenerateClinitCheck()) {
3240 GenerateClassInitializationCheck(slow_path, out);
3241 } else {
3242 __ Bind(slow_path->GetExitLabel());
3243 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003244 }
3245 }
3246}
3247
David Brazdilcb1c0552015-08-04 16:22:25 +01003248static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003249 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003250}
3251
Alexey Frunze4dda3372015-06-01 18:31:49 -07003252void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3253 LocationSummary* locations =
3254 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3255 locations->SetOut(Location::RequiresRegister());
3256}
3257
3258void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3259 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003260 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3261}
3262
3263void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3264 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3265}
3266
3267void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3268 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003269}
3270
3271void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3272 load->SetLocations(nullptr);
3273}
3274
3275void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3276 // Nothing to do, this is driven by the code generator.
3277}
3278
3279void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003280 LocationSummary::CallKind call_kind = load->IsInDexCache()
3281 ? LocationSummary::kNoCall
3282 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003284 locations->SetInAt(0, Location::RequiresRegister());
3285 locations->SetOut(Location::RequiresRegister());
3286}
3287
3288void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003289 LocationSummary* locations = load->GetLocations();
3290 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3291 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3292 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3293 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003294 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003295 __ LoadFromOffset(
3296 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003297 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003298
3299 if (!load->IsInDexCache()) {
3300 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3301 codegen_->AddSlowPath(slow_path);
3302 __ Beqzc(out, slow_path->GetEntryLabel());
3303 __ Bind(slow_path->GetExitLabel());
3304 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003305}
3306
3307void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3308 local->SetLocations(nullptr);
3309}
3310
3311void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3312 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3313}
3314
3315void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3317 locations->SetOut(Location::ConstantLocation(constant));
3318}
3319
3320void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3321 // Will be generated at use site.
3322}
3323
3324void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3325 LocationSummary* locations =
3326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3327 InvokeRuntimeCallingConvention calling_convention;
3328 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3329}
3330
3331void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3332 codegen_->InvokeRuntime(instruction->IsEnter()
3333 ? QUICK_ENTRY_POINT(pLockObject)
3334 : QUICK_ENTRY_POINT(pUnlockObject),
3335 instruction,
3336 instruction->GetDexPc(),
3337 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003338 if (instruction->IsEnter()) {
3339 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3340 } else {
3341 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3342 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003343}
3344
3345void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3346 LocationSummary* locations =
3347 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3348 switch (mul->GetResultType()) {
3349 case Primitive::kPrimInt:
3350 case Primitive::kPrimLong:
3351 locations->SetInAt(0, Location::RequiresRegister());
3352 locations->SetInAt(1, Location::RequiresRegister());
3353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3354 break;
3355
3356 case Primitive::kPrimFloat:
3357 case Primitive::kPrimDouble:
3358 locations->SetInAt(0, Location::RequiresFpuRegister());
3359 locations->SetInAt(1, Location::RequiresFpuRegister());
3360 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3361 break;
3362
3363 default:
3364 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3365 }
3366}
3367
3368void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3369 Primitive::Type type = instruction->GetType();
3370 LocationSummary* locations = instruction->GetLocations();
3371
3372 switch (type) {
3373 case Primitive::kPrimInt:
3374 case Primitive::kPrimLong: {
3375 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3376 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3377 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3378 if (type == Primitive::kPrimInt)
3379 __ MulR6(dst, lhs, rhs);
3380 else
3381 __ Dmul(dst, lhs, rhs);
3382 break;
3383 }
3384 case Primitive::kPrimFloat:
3385 case Primitive::kPrimDouble: {
3386 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3387 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3388 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3389 if (type == Primitive::kPrimFloat)
3390 __ MulS(dst, lhs, rhs);
3391 else
3392 __ MulD(dst, lhs, rhs);
3393 break;
3394 }
3395 default:
3396 LOG(FATAL) << "Unexpected mul type " << type;
3397 }
3398}
3399
3400void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3401 LocationSummary* locations =
3402 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3403 switch (neg->GetResultType()) {
3404 case Primitive::kPrimInt:
3405 case Primitive::kPrimLong:
3406 locations->SetInAt(0, Location::RequiresRegister());
3407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3408 break;
3409
3410 case Primitive::kPrimFloat:
3411 case Primitive::kPrimDouble:
3412 locations->SetInAt(0, Location::RequiresFpuRegister());
3413 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3414 break;
3415
3416 default:
3417 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3418 }
3419}
3420
3421void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3422 Primitive::Type type = instruction->GetType();
3423 LocationSummary* locations = instruction->GetLocations();
3424
3425 switch (type) {
3426 case Primitive::kPrimInt:
3427 case Primitive::kPrimLong: {
3428 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3429 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3430 if (type == Primitive::kPrimInt)
3431 __ Subu(dst, ZERO, src);
3432 else
3433 __ Dsubu(dst, ZERO, src);
3434 break;
3435 }
3436 case Primitive::kPrimFloat:
3437 case Primitive::kPrimDouble: {
3438 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3439 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3440 if (type == Primitive::kPrimFloat)
3441 __ NegS(dst, src);
3442 else
3443 __ NegD(dst, src);
3444 break;
3445 }
3446 default:
3447 LOG(FATAL) << "Unexpected neg type " << type;
3448 }
3449}
3450
3451void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3452 LocationSummary* locations =
3453 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3454 InvokeRuntimeCallingConvention calling_convention;
3455 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3456 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3457 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3458 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3459}
3460
3461void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3462 LocationSummary* locations = instruction->GetLocations();
3463 // Move an uint16_t value to a register.
3464 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003465 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3466 instruction,
3467 instruction->GetDexPc(),
3468 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003469 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3470}
3471
3472void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3473 LocationSummary* locations =
3474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3475 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003476 if (instruction->IsStringAlloc()) {
3477 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3478 } else {
3479 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3480 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3481 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003482 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3483}
3484
3485void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003486 if (instruction->IsStringAlloc()) {
3487 // String is allocated through StringFactory. Call NewEmptyString entry point.
3488 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003489 MemberOffset code_offset =
3490 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003491 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3492 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3493 __ Jalr(T9);
3494 __ Nop();
3495 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3496 } else {
3497 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3498 instruction,
3499 instruction->GetDexPc(),
3500 nullptr);
3501 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3502 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503}
3504
3505void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3509}
3510
3511void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3512 Primitive::Type type = instruction->GetType();
3513 LocationSummary* locations = instruction->GetLocations();
3514
3515 switch (type) {
3516 case Primitive::kPrimInt:
3517 case Primitive::kPrimLong: {
3518 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3519 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3520 __ Nor(dst, src, ZERO);
3521 break;
3522 }
3523
3524 default:
3525 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3526 }
3527}
3528
3529void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3531 locations->SetInAt(0, Location::RequiresRegister());
3532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3533}
3534
3535void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3536 LocationSummary* locations = instruction->GetLocations();
3537 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3538 locations->InAt(0).AsRegister<GpuRegister>(),
3539 1);
3540}
3541
3542void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003543 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3544 ? LocationSummary::kCallOnSlowPath
3545 : LocationSummary::kNoCall;
3546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003547 locations->SetInAt(0, Location::RequiresRegister());
3548 if (instruction->HasUses()) {
3549 locations->SetOut(Location::SameAsFirstInput());
3550 }
3551}
3552
3553void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3554 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3555 return;
3556 }
3557 Location obj = instruction->GetLocations()->InAt(0);
3558
3559 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3560 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3561}
3562
3563void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3564 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3565 codegen_->AddSlowPath(slow_path);
3566
3567 Location obj = instruction->GetLocations()->InAt(0);
3568
3569 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3570}
3571
3572void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003573 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574 GenerateImplicitNullCheck(instruction);
3575 } else {
3576 GenerateExplicitNullCheck(instruction);
3577 }
3578}
3579
3580void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3581 HandleBinaryOp(instruction);
3582}
3583
3584void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3585 HandleBinaryOp(instruction);
3586}
3587
3588void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3589 LOG(FATAL) << "Unreachable";
3590}
3591
3592void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3593 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3594}
3595
3596void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3598 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3599 if (location.IsStackSlot()) {
3600 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3601 } else if (location.IsDoubleStackSlot()) {
3602 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3603 }
3604 locations->SetOut(location);
3605}
3606
3607void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3608 ATTRIBUTE_UNUSED) {
3609 // Nothing to do, the parameter is already at its location.
3610}
3611
3612void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3613 LocationSummary* locations =
3614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3615 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3616}
3617
3618void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3619 ATTRIBUTE_UNUSED) {
3620 // Nothing to do, the method is already at its location.
3621}
3622
3623void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3624 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3625 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3626 locations->SetInAt(i, Location::Any());
3627 }
3628 locations->SetOut(Location::Any());
3629}
3630
3631void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3632 LOG(FATAL) << "Unreachable";
3633}
3634
3635void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3636 Primitive::Type type = rem->GetResultType();
3637 LocationSummary::CallKind call_kind =
3638 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3640
3641 switch (type) {
3642 case Primitive::kPrimInt:
3643 case Primitive::kPrimLong:
3644 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003645 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3647 break;
3648
3649 case Primitive::kPrimFloat:
3650 case Primitive::kPrimDouble: {
3651 InvokeRuntimeCallingConvention calling_convention;
3652 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3653 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3654 locations->SetOut(calling_convention.GetReturnLocation(type));
3655 break;
3656 }
3657
3658 default:
3659 LOG(FATAL) << "Unexpected rem type " << type;
3660 }
3661}
3662
3663void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3664 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003665
3666 switch (type) {
3667 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003668 case Primitive::kPrimLong:
3669 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003670 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003671
3672 case Primitive::kPrimFloat:
3673 case Primitive::kPrimDouble: {
3674 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3675 : QUICK_ENTRY_POINT(pFmod);
3676 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003677 if (type == Primitive::kPrimFloat) {
3678 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3679 } else {
3680 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3681 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003682 break;
3683 }
3684 default:
3685 LOG(FATAL) << "Unexpected rem type " << type;
3686 }
3687}
3688
3689void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3690 memory_barrier->SetLocations(nullptr);
3691}
3692
3693void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3694 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3695}
3696
3697void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3699 Primitive::Type return_type = ret->InputAt(0)->GetType();
3700 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3701}
3702
3703void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3704 codegen_->GenerateFrameExit();
3705}
3706
3707void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3708 ret->SetLocations(nullptr);
3709}
3710
3711void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3712 codegen_->GenerateFrameExit();
3713}
3714
Alexey Frunze92d90602015-12-18 18:16:36 -08003715void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3716 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003717}
3718
Alexey Frunze92d90602015-12-18 18:16:36 -08003719void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3720 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003721}
3722
Alexey Frunze4dda3372015-06-01 18:31:49 -07003723void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3724 HandleShift(shl);
3725}
3726
3727void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3728 HandleShift(shl);
3729}
3730
3731void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3732 HandleShift(shr);
3733}
3734
3735void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3736 HandleShift(shr);
3737}
3738
3739void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3740 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3741 Primitive::Type field_type = store->InputAt(1)->GetType();
3742 switch (field_type) {
3743 case Primitive::kPrimNot:
3744 case Primitive::kPrimBoolean:
3745 case Primitive::kPrimByte:
3746 case Primitive::kPrimChar:
3747 case Primitive::kPrimShort:
3748 case Primitive::kPrimInt:
3749 case Primitive::kPrimFloat:
3750 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3751 break;
3752
3753 case Primitive::kPrimLong:
3754 case Primitive::kPrimDouble:
3755 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3756 break;
3757
3758 default:
3759 LOG(FATAL) << "Unimplemented local type " << field_type;
3760 }
3761}
3762
3763void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3764}
3765
3766void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3767 HandleBinaryOp(instruction);
3768}
3769
3770void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3771 HandleBinaryOp(instruction);
3772}
3773
3774void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3775 HandleFieldGet(instruction, instruction->GetFieldInfo());
3776}
3777
3778void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3779 HandleFieldGet(instruction, instruction->GetFieldInfo());
3780}
3781
3782void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3783 HandleFieldSet(instruction, instruction->GetFieldInfo());
3784}
3785
3786void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003787 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003788}
3789
Calin Juravlee460d1d2015-09-29 04:52:17 +01003790void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3791 HUnresolvedInstanceFieldGet* instruction) {
3792 FieldAccessCallingConventionMIPS64 calling_convention;
3793 codegen_->CreateUnresolvedFieldLocationSummary(
3794 instruction, instruction->GetFieldType(), calling_convention);
3795}
3796
3797void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3798 HUnresolvedInstanceFieldGet* instruction) {
3799 FieldAccessCallingConventionMIPS64 calling_convention;
3800 codegen_->GenerateUnresolvedFieldAccess(instruction,
3801 instruction->GetFieldType(),
3802 instruction->GetFieldIndex(),
3803 instruction->GetDexPc(),
3804 calling_convention);
3805}
3806
3807void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3808 HUnresolvedInstanceFieldSet* instruction) {
3809 FieldAccessCallingConventionMIPS64 calling_convention;
3810 codegen_->CreateUnresolvedFieldLocationSummary(
3811 instruction, instruction->GetFieldType(), calling_convention);
3812}
3813
3814void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3815 HUnresolvedInstanceFieldSet* instruction) {
3816 FieldAccessCallingConventionMIPS64 calling_convention;
3817 codegen_->GenerateUnresolvedFieldAccess(instruction,
3818 instruction->GetFieldType(),
3819 instruction->GetFieldIndex(),
3820 instruction->GetDexPc(),
3821 calling_convention);
3822}
3823
3824void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3825 HUnresolvedStaticFieldGet* instruction) {
3826 FieldAccessCallingConventionMIPS64 calling_convention;
3827 codegen_->CreateUnresolvedFieldLocationSummary(
3828 instruction, instruction->GetFieldType(), calling_convention);
3829}
3830
3831void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3832 HUnresolvedStaticFieldGet* instruction) {
3833 FieldAccessCallingConventionMIPS64 calling_convention;
3834 codegen_->GenerateUnresolvedFieldAccess(instruction,
3835 instruction->GetFieldType(),
3836 instruction->GetFieldIndex(),
3837 instruction->GetDexPc(),
3838 calling_convention);
3839}
3840
3841void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3842 HUnresolvedStaticFieldSet* instruction) {
3843 FieldAccessCallingConventionMIPS64 calling_convention;
3844 codegen_->CreateUnresolvedFieldLocationSummary(
3845 instruction, instruction->GetFieldType(), calling_convention);
3846}
3847
3848void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3849 HUnresolvedStaticFieldSet* instruction) {
3850 FieldAccessCallingConventionMIPS64 calling_convention;
3851 codegen_->GenerateUnresolvedFieldAccess(instruction,
3852 instruction->GetFieldType(),
3853 instruction->GetFieldIndex(),
3854 instruction->GetDexPc(),
3855 calling_convention);
3856}
3857
Alexey Frunze4dda3372015-06-01 18:31:49 -07003858void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3860}
3861
3862void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3863 HBasicBlock* block = instruction->GetBlock();
3864 if (block->GetLoopInformation() != nullptr) {
3865 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3866 // The back edge will generate the suspend check.
3867 return;
3868 }
3869 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3870 // The goto will generate the suspend check.
3871 return;
3872 }
3873 GenerateSuspendCheck(instruction, nullptr);
3874}
3875
Alexey Frunze4dda3372015-06-01 18:31:49 -07003876void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3877 LocationSummary* locations =
3878 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3879 InvokeRuntimeCallingConvention calling_convention;
3880 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3881}
3882
3883void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3884 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3885 instruction,
3886 instruction->GetDexPc(),
3887 nullptr);
3888 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3889}
3890
3891void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3892 Primitive::Type input_type = conversion->GetInputType();
3893 Primitive::Type result_type = conversion->GetResultType();
3894 DCHECK_NE(input_type, result_type);
3895
3896 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3897 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3898 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3899 }
3900
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3902
3903 if (Primitive::IsFloatingPointType(input_type)) {
3904 locations->SetInAt(0, Location::RequiresFpuRegister());
3905 } else {
3906 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003907 }
3908
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003909 if (Primitive::IsFloatingPointType(result_type)) {
3910 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003911 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003913 }
3914}
3915
3916void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3917 LocationSummary* locations = conversion->GetLocations();
3918 Primitive::Type result_type = conversion->GetResultType();
3919 Primitive::Type input_type = conversion->GetInputType();
3920
3921 DCHECK_NE(input_type, result_type);
3922
3923 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3924 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3925 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3926
3927 switch (result_type) {
3928 case Primitive::kPrimChar:
3929 __ Andi(dst, src, 0xFFFF);
3930 break;
3931 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003932 if (input_type == Primitive::kPrimLong) {
3933 // Type conversion from long to types narrower than int is a result of code
3934 // transformations. To avoid unpredictable results for SEB and SEH, we first
3935 // need to sign-extend the low 32-bit value into bits 32 through 63.
3936 __ Sll(dst, src, 0);
3937 __ Seb(dst, dst);
3938 } else {
3939 __ Seb(dst, src);
3940 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003941 break;
3942 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003943 if (input_type == Primitive::kPrimLong) {
3944 // Type conversion from long to types narrower than int is a result of code
3945 // transformations. To avoid unpredictable results for SEB and SEH, we first
3946 // need to sign-extend the low 32-bit value into bits 32 through 63.
3947 __ Sll(dst, src, 0);
3948 __ Seh(dst, dst);
3949 } else {
3950 __ Seh(dst, src);
3951 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003952 break;
3953 case Primitive::kPrimInt:
3954 case Primitive::kPrimLong:
3955 // Sign-extend 32-bit int into bits 32 through 63 for
3956 // int-to-long and long-to-int conversions
3957 __ Sll(dst, src, 0);
3958 break;
3959
3960 default:
3961 LOG(FATAL) << "Unexpected type conversion from " << input_type
3962 << " to " << result_type;
3963 }
3964 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003965 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3966 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3967 if (input_type == Primitive::kPrimLong) {
3968 __ Dmtc1(src, FTMP);
3969 if (result_type == Primitive::kPrimFloat) {
3970 __ Cvtsl(dst, FTMP);
3971 } else {
3972 __ Cvtdl(dst, FTMP);
3973 }
3974 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003975 __ Mtc1(src, FTMP);
3976 if (result_type == Primitive::kPrimFloat) {
3977 __ Cvtsw(dst, FTMP);
3978 } else {
3979 __ Cvtdw(dst, FTMP);
3980 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003981 }
3982 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3983 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003984 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3985 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3986 Mips64Label truncate;
3987 Mips64Label done;
3988
3989 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3990 // value when the input is either a NaN or is outside of the range of the output type
3991 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3992 // the same result.
3993 //
3994 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3995 // value of the output type if the input is outside of the range after the truncation or
3996 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3997 // results. This matches the desired float/double-to-int/long conversion exactly.
3998 //
3999 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4000 //
4001 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4002 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4003 // even though it must be NAN2008=1 on R6.
4004 //
4005 // The code takes care of the different behaviors by first comparing the input to the
4006 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4007 // If the input is greater than or equal to the minimum, it procedes to the truncate
4008 // instruction, which will handle such an input the same way irrespective of NAN2008.
4009 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4010 // in order to return either zero or the minimum value.
4011 //
4012 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4013 // truncate instruction for MIPS64R6.
4014 if (input_type == Primitive::kPrimFloat) {
4015 uint32_t min_val = (result_type == Primitive::kPrimLong)
4016 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4017 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4018 __ LoadConst32(TMP, min_val);
4019 __ Mtc1(TMP, FTMP);
4020 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004021 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004022 uint64_t min_val = (result_type == Primitive::kPrimLong)
4023 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4024 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4025 __ LoadConst64(TMP, min_val);
4026 __ Dmtc1(TMP, FTMP);
4027 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004028 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004029
4030 __ Bc1nez(FTMP, &truncate);
4031
4032 if (input_type == Primitive::kPrimFloat) {
4033 __ CmpEqS(FTMP, src, src);
4034 } else {
4035 __ CmpEqD(FTMP, src, src);
4036 }
4037 if (result_type == Primitive::kPrimLong) {
4038 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4039 } else {
4040 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4041 }
4042 __ Mfc1(TMP, FTMP);
4043 __ And(dst, dst, TMP);
4044
4045 __ Bc(&done);
4046
4047 __ Bind(&truncate);
4048
4049 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004050 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004051 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004052 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004053 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004054 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004055 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004056 } else {
4057 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004058 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004059 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004060 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004061 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004062 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004063 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004064
4065 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004066 } else if (Primitive::IsFloatingPointType(result_type) &&
4067 Primitive::IsFloatingPointType(input_type)) {
4068 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4069 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4070 if (result_type == Primitive::kPrimFloat) {
4071 __ Cvtsd(dst, src);
4072 } else {
4073 __ Cvtds(dst, src);
4074 }
4075 } else {
4076 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4077 << " to " << result_type;
4078 }
4079}
4080
4081void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4082 HandleShift(ushr);
4083}
4084
4085void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4086 HandleShift(ushr);
4087}
4088
4089void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4090 HandleBinaryOp(instruction);
4091}
4092
4093void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4094 HandleBinaryOp(instruction);
4095}
4096
4097void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4098 // Nothing to do, this should be removed during prepare for register allocator.
4099 LOG(FATAL) << "Unreachable";
4100}
4101
4102void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4103 // Nothing to do, this should be removed during prepare for register allocator.
4104 LOG(FATAL) << "Unreachable";
4105}
4106
4107void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004108 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004109}
4110
4111void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004112 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004113}
4114
4115void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004116 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004117}
4118
4119void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004120 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004121}
4122
4123void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004124 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004125}
4126
4127void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004128 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004129}
4130
4131void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004132 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004133}
4134
4135void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004136 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004137}
4138
4139void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004140 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004141}
4142
4143void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004144 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004145}
4146
4147void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004148 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004149}
4150
4151void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004152 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004153}
4154
Aart Bike9f37602015-10-09 11:15:55 -07004155void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004156 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004157}
4158
4159void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004160 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004161}
4162
4163void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004164 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004165}
4166
4167void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004168 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004169}
4170
4171void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004172 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004173}
4174
4175void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004176 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004177}
4178
4179void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004180 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004181}
4182
4183void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004184 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004185}
4186
Mark Mendellfe57faa2015-09-18 09:26:15 -04004187// Simple implementation of packed switch - generate cascaded compare/jumps.
4188void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4189 LocationSummary* locations =
4190 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4191 locations->SetInAt(0, Location::RequiresRegister());
4192}
4193
4194void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4195 int32_t lower_bound = switch_instr->GetStartValue();
4196 int32_t num_entries = switch_instr->GetNumEntries();
4197 LocationSummary* locations = switch_instr->GetLocations();
4198 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4199 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4200
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004201 // Create a set of compare/jumps.
4202 GpuRegister temp_reg = TMP;
4203 if (IsInt<16>(-lower_bound)) {
4204 __ Addiu(temp_reg, value_reg, -lower_bound);
4205 } else {
4206 __ LoadConst32(AT, -lower_bound);
4207 __ Addu(temp_reg, value_reg, AT);
4208 }
4209 // Jump to default if index is negative
4210 // Note: We don't check the case that index is positive while value < lower_bound, because in
4211 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4212 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4213
Mark Mendellfe57faa2015-09-18 09:26:15 -04004214 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004215 // Jump to successors[0] if value == lower_bound.
4216 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4217 int32_t last_index = 0;
4218 for (; num_entries - last_index > 2; last_index += 2) {
4219 __ Addiu(temp_reg, temp_reg, -2);
4220 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4221 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4222 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4223 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4224 }
4225 if (num_entries - last_index == 2) {
4226 // The last missing case_value.
4227 __ Addiu(temp_reg, temp_reg, -1);
4228 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004229 }
4230
4231 // And the default for any other value.
4232 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004233 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004234 }
4235}
4236
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004237void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4238 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4239}
4240
4241void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4242 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4243}
4244
Alexey Frunze4dda3372015-06-01 18:31:49 -07004245} // namespace mips64
4246} // namespace art