blob: 246f5b7a650cd63bbae1e6e0b51c8a8765ed96e4 [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
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100872void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
873 GpuRegister value,
874 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700875 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700876 GpuRegister card = AT;
877 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100878 if (value_can_be_null) {
879 __ Beqzc(value, &done);
880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700881 __ LoadFromOffset(kLoadDoubleword,
882 card,
883 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200884 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700885 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
886 __ Daddu(temp, card, temp);
887 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100888 if (value_can_be_null) {
889 __ Bind(&done);
890 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700891}
892
David Brazdil58282f42016-01-14 12:45:10 +0000893void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700894 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
895 blocked_core_registers_[ZERO] = true;
896 blocked_core_registers_[K0] = true;
897 blocked_core_registers_[K1] = true;
898 blocked_core_registers_[GP] = true;
899 blocked_core_registers_[SP] = true;
900 blocked_core_registers_[RA] = true;
901
Lazar Trsicd9672662015-09-03 17:33:01 +0200902 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
903 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700904 blocked_core_registers_[AT] = true;
905 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200906 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700907 blocked_fpu_registers_[FTMP] = true;
908
909 // Reserve suspend and thread registers.
910 blocked_core_registers_[S0] = true;
911 blocked_core_registers_[TR] = true;
912
913 // Reserve T9 for function calls
914 blocked_core_registers_[T9] = true;
915
916 // TODO: review; anything else?
917
David Brazdil58282f42016-01-14 12:45:10 +0000918 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
920 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
921 }
922
923 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
924 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
925 }
926}
927
Alexey Frunze4dda3372015-06-01 18:31:49 -0700928size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
929 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200930 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700931}
932
933size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
934 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200935 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936}
937
938size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
939 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200940 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941}
942
943size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
944 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200945 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700946}
947
948void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100949 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700950}
951
952void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100953 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700954}
955
Calin Juravle175dc732015-08-25 15:42:32 +0100956void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
957 HInstruction* instruction,
958 uint32_t dex_pc,
959 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200960 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100961 instruction,
962 dex_pc,
963 slow_path);
964}
965
Alexey Frunze4dda3372015-06-01 18:31:49 -0700966void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
967 HInstruction* instruction,
968 uint32_t dex_pc,
969 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100970 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971 // TODO: anything related to T9/GP/GOT/PIC/.so's?
972 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
973 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700974 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700975 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976}
977
978void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
979 GpuRegister class_reg) {
980 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
981 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
982 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
983 // TODO: barrier needed?
984 __ Bind(slow_path->GetExitLabel());
985}
986
987void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
988 __ Sync(0); // only stype 0 is supported
989}
990
991void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
992 HBasicBlock* successor) {
993 SuspendCheckSlowPathMIPS64* slow_path =
994 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
995 codegen_->AddSlowPath(slow_path);
996
997 __ LoadFromOffset(kLoadUnsignedHalfword,
998 TMP,
999 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001000 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001 if (successor == nullptr) {
1002 __ Bnezc(TMP, slow_path->GetEntryLabel());
1003 __ Bind(slow_path->GetReturnLabel());
1004 } else {
1005 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001006 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001007 // slow_path will return to GetLabelOf(successor).
1008 }
1009}
1010
1011InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1012 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001013 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001014 assembler_(codegen->GetAssembler()),
1015 codegen_(codegen) {}
1016
1017void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1018 DCHECK_EQ(instruction->InputCount(), 2U);
1019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1020 Primitive::Type type = instruction->GetResultType();
1021 switch (type) {
1022 case Primitive::kPrimInt:
1023 case Primitive::kPrimLong: {
1024 locations->SetInAt(0, Location::RequiresRegister());
1025 HInstruction* right = instruction->InputAt(1);
1026 bool can_use_imm = false;
1027 if (right->IsConstant()) {
1028 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1029 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1030 can_use_imm = IsUint<16>(imm);
1031 } else if (instruction->IsAdd()) {
1032 can_use_imm = IsInt<16>(imm);
1033 } else {
1034 DCHECK(instruction->IsSub());
1035 can_use_imm = IsInt<16>(-imm);
1036 }
1037 }
1038 if (can_use_imm)
1039 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1040 else
1041 locations->SetInAt(1, Location::RequiresRegister());
1042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1043 }
1044 break;
1045
1046 case Primitive::kPrimFloat:
1047 case Primitive::kPrimDouble:
1048 locations->SetInAt(0, Location::RequiresFpuRegister());
1049 locations->SetInAt(1, Location::RequiresFpuRegister());
1050 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1051 break;
1052
1053 default:
1054 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1055 }
1056}
1057
1058void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1059 Primitive::Type type = instruction->GetType();
1060 LocationSummary* locations = instruction->GetLocations();
1061
1062 switch (type) {
1063 case Primitive::kPrimInt:
1064 case Primitive::kPrimLong: {
1065 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1066 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1067 Location rhs_location = locations->InAt(1);
1068
1069 GpuRegister rhs_reg = ZERO;
1070 int64_t rhs_imm = 0;
1071 bool use_imm = rhs_location.IsConstant();
1072 if (use_imm) {
1073 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1074 } else {
1075 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1076 }
1077
1078 if (instruction->IsAnd()) {
1079 if (use_imm)
1080 __ Andi(dst, lhs, rhs_imm);
1081 else
1082 __ And(dst, lhs, rhs_reg);
1083 } else if (instruction->IsOr()) {
1084 if (use_imm)
1085 __ Ori(dst, lhs, rhs_imm);
1086 else
1087 __ Or(dst, lhs, rhs_reg);
1088 } else if (instruction->IsXor()) {
1089 if (use_imm)
1090 __ Xori(dst, lhs, rhs_imm);
1091 else
1092 __ Xor(dst, lhs, rhs_reg);
1093 } else if (instruction->IsAdd()) {
1094 if (type == Primitive::kPrimInt) {
1095 if (use_imm)
1096 __ Addiu(dst, lhs, rhs_imm);
1097 else
1098 __ Addu(dst, lhs, rhs_reg);
1099 } else {
1100 if (use_imm)
1101 __ Daddiu(dst, lhs, rhs_imm);
1102 else
1103 __ Daddu(dst, lhs, rhs_reg);
1104 }
1105 } else {
1106 DCHECK(instruction->IsSub());
1107 if (type == Primitive::kPrimInt) {
1108 if (use_imm)
1109 __ Addiu(dst, lhs, -rhs_imm);
1110 else
1111 __ Subu(dst, lhs, rhs_reg);
1112 } else {
1113 if (use_imm)
1114 __ Daddiu(dst, lhs, -rhs_imm);
1115 else
1116 __ Dsubu(dst, lhs, rhs_reg);
1117 }
1118 }
1119 break;
1120 }
1121 case Primitive::kPrimFloat:
1122 case Primitive::kPrimDouble: {
1123 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1124 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1125 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1126 if (instruction->IsAdd()) {
1127 if (type == Primitive::kPrimFloat)
1128 __ AddS(dst, lhs, rhs);
1129 else
1130 __ AddD(dst, lhs, rhs);
1131 } else if (instruction->IsSub()) {
1132 if (type == Primitive::kPrimFloat)
1133 __ SubS(dst, lhs, rhs);
1134 else
1135 __ SubD(dst, lhs, rhs);
1136 } else {
1137 LOG(FATAL) << "Unexpected floating-point binary operation";
1138 }
1139 break;
1140 }
1141 default:
1142 LOG(FATAL) << "Unexpected binary operation type " << type;
1143 }
1144}
1145
1146void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001147 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001148
1149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1150 Primitive::Type type = instr->GetResultType();
1151 switch (type) {
1152 case Primitive::kPrimInt:
1153 case Primitive::kPrimLong: {
1154 locations->SetInAt(0, Location::RequiresRegister());
1155 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001156 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001157 break;
1158 }
1159 default:
1160 LOG(FATAL) << "Unexpected shift type " << type;
1161 }
1162}
1163
1164void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001165 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001166 LocationSummary* locations = instr->GetLocations();
1167 Primitive::Type type = instr->GetType();
1168
1169 switch (type) {
1170 case Primitive::kPrimInt:
1171 case Primitive::kPrimLong: {
1172 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1173 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1174 Location rhs_location = locations->InAt(1);
1175
1176 GpuRegister rhs_reg = ZERO;
1177 int64_t rhs_imm = 0;
1178 bool use_imm = rhs_location.IsConstant();
1179 if (use_imm) {
1180 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1181 } else {
1182 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1183 }
1184
1185 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001186 uint32_t shift_value = rhs_imm &
1187 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001188
Alexey Frunze92d90602015-12-18 18:16:36 -08001189 if (shift_value == 0) {
1190 if (dst != lhs) {
1191 __ Move(dst, lhs);
1192 }
1193 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001194 if (instr->IsShl()) {
1195 __ Sll(dst, lhs, shift_value);
1196 } else if (instr->IsShr()) {
1197 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001198 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001199 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001200 } else {
1201 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001202 }
1203 } else {
1204 if (shift_value < 32) {
1205 if (instr->IsShl()) {
1206 __ Dsll(dst, lhs, shift_value);
1207 } else if (instr->IsShr()) {
1208 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001209 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001210 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001211 } else {
1212 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001213 }
1214 } else {
1215 shift_value -= 32;
1216 if (instr->IsShl()) {
1217 __ Dsll32(dst, lhs, shift_value);
1218 } else if (instr->IsShr()) {
1219 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001220 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001221 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001222 } else {
1223 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 }
1225 }
1226 }
1227 } else {
1228 if (type == Primitive::kPrimInt) {
1229 if (instr->IsShl()) {
1230 __ Sllv(dst, lhs, rhs_reg);
1231 } else if (instr->IsShr()) {
1232 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001233 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001234 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001235 } else {
1236 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001237 }
1238 } else {
1239 if (instr->IsShl()) {
1240 __ Dsllv(dst, lhs, rhs_reg);
1241 } else if (instr->IsShr()) {
1242 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001243 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001244 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001245 } else {
1246 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001247 }
1248 }
1249 }
1250 break;
1251 }
1252 default:
1253 LOG(FATAL) << "Unexpected shift operation type " << type;
1254 }
1255}
1256
1257void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1258 HandleBinaryOp(instruction);
1259}
1260
1261void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1262 HandleBinaryOp(instruction);
1263}
1264
1265void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1266 HandleBinaryOp(instruction);
1267}
1268
1269void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1270 HandleBinaryOp(instruction);
1271}
1272
1273void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1274 LocationSummary* locations =
1275 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1276 locations->SetInAt(0, Location::RequiresRegister());
1277 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1278 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1279 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1280 } else {
1281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1282 }
1283}
1284
1285void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1286 LocationSummary* locations = instruction->GetLocations();
1287 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1288 Location index = locations->InAt(1);
1289 Primitive::Type type = instruction->GetType();
1290
1291 switch (type) {
1292 case Primitive::kPrimBoolean: {
1293 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1294 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1295 if (index.IsConstant()) {
1296 size_t offset =
1297 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1298 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1299 } else {
1300 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1301 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1302 }
1303 break;
1304 }
1305
1306 case Primitive::kPrimByte: {
1307 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1308 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1309 if (index.IsConstant()) {
1310 size_t offset =
1311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1312 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1313 } else {
1314 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1315 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1316 }
1317 break;
1318 }
1319
1320 case Primitive::kPrimShort: {
1321 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1322 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1323 if (index.IsConstant()) {
1324 size_t offset =
1325 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1326 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1327 } else {
1328 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1329 __ Daddu(TMP, obj, TMP);
1330 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1331 }
1332 break;
1333 }
1334
1335 case Primitive::kPrimChar: {
1336 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1337 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1338 if (index.IsConstant()) {
1339 size_t offset =
1340 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1341 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1342 } else {
1343 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1344 __ Daddu(TMP, obj, TMP);
1345 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1346 }
1347 break;
1348 }
1349
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimNot: {
1352 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1353 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1354 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1355 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1356 if (index.IsConstant()) {
1357 size_t offset =
1358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1359 __ LoadFromOffset(load_type, out, obj, offset);
1360 } else {
1361 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1362 __ Daddu(TMP, obj, TMP);
1363 __ LoadFromOffset(load_type, out, TMP, data_offset);
1364 }
1365 break;
1366 }
1367
1368 case Primitive::kPrimLong: {
1369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1370 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1371 if (index.IsConstant()) {
1372 size_t offset =
1373 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1374 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1375 } else {
1376 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1377 __ Daddu(TMP, obj, TMP);
1378 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1379 }
1380 break;
1381 }
1382
1383 case Primitive::kPrimFloat: {
1384 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1385 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1386 if (index.IsConstant()) {
1387 size_t offset =
1388 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1389 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1390 } else {
1391 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1392 __ Daddu(TMP, obj, TMP);
1393 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1394 }
1395 break;
1396 }
1397
1398 case Primitive::kPrimDouble: {
1399 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1400 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1401 if (index.IsConstant()) {
1402 size_t offset =
1403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1404 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1405 } else {
1406 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1407 __ Daddu(TMP, obj, TMP);
1408 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1409 }
1410 break;
1411 }
1412
1413 case Primitive::kPrimVoid:
1414 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1415 UNREACHABLE();
1416 }
1417 codegen_->MaybeRecordImplicitNullCheck(instruction);
1418}
1419
1420void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1422 locations->SetInAt(0, Location::RequiresRegister());
1423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1424}
1425
1426void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1427 LocationSummary* locations = instruction->GetLocations();
1428 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1429 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1430 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1431 __ LoadFromOffset(kLoadWord, out, obj, offset);
1432 codegen_->MaybeRecordImplicitNullCheck(instruction);
1433}
1434
1435void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001436 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1438 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001439 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1440 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001441 InvokeRuntimeCallingConvention calling_convention;
1442 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1443 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1444 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1445 } else {
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1448 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1449 locations->SetInAt(2, Location::RequiresFpuRegister());
1450 } else {
1451 locations->SetInAt(2, Location::RequiresRegister());
1452 }
1453 }
1454}
1455
1456void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1457 LocationSummary* locations = instruction->GetLocations();
1458 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1459 Location index = locations->InAt(1);
1460 Primitive::Type value_type = instruction->GetComponentType();
1461 bool needs_runtime_call = locations->WillCall();
1462 bool needs_write_barrier =
1463 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1464
1465 switch (value_type) {
1466 case Primitive::kPrimBoolean:
1467 case Primitive::kPrimByte: {
1468 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1469 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1470 if (index.IsConstant()) {
1471 size_t offset =
1472 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1473 __ StoreToOffset(kStoreByte, value, obj, offset);
1474 } else {
1475 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1476 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1477 }
1478 break;
1479 }
1480
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimChar: {
1483 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1484 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1485 if (index.IsConstant()) {
1486 size_t offset =
1487 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1488 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1489 } else {
1490 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1491 __ Daddu(TMP, obj, TMP);
1492 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1493 }
1494 break;
1495 }
1496
1497 case Primitive::kPrimInt:
1498 case Primitive::kPrimNot: {
1499 if (!needs_runtime_call) {
1500 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1501 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1502 if (index.IsConstant()) {
1503 size_t offset =
1504 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1505 __ StoreToOffset(kStoreWord, value, obj, offset);
1506 } else {
1507 DCHECK(index.IsRegister()) << index;
1508 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1509 __ Daddu(TMP, obj, TMP);
1510 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1511 }
1512 codegen_->MaybeRecordImplicitNullCheck(instruction);
1513 if (needs_write_barrier) {
1514 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001515 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001516 }
1517 } else {
1518 DCHECK_EQ(value_type, Primitive::kPrimNot);
1519 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1520 instruction,
1521 instruction->GetDexPc(),
1522 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001523 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001524 }
1525 break;
1526 }
1527
1528 case Primitive::kPrimLong: {
1529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1530 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1531 if (index.IsConstant()) {
1532 size_t offset =
1533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1534 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1535 } else {
1536 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1537 __ Daddu(TMP, obj, TMP);
1538 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1539 }
1540 break;
1541 }
1542
1543 case Primitive::kPrimFloat: {
1544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1545 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1546 DCHECK(locations->InAt(2).IsFpuRegister());
1547 if (index.IsConstant()) {
1548 size_t offset =
1549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1550 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1551 } else {
1552 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1553 __ Daddu(TMP, obj, TMP);
1554 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1555 }
1556 break;
1557 }
1558
1559 case Primitive::kPrimDouble: {
1560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1561 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1562 DCHECK(locations->InAt(2).IsFpuRegister());
1563 if (index.IsConstant()) {
1564 size_t offset =
1565 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1566 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1567 } else {
1568 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1569 __ Daddu(TMP, obj, TMP);
1570 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1571 }
1572 break;
1573 }
1574
1575 case Primitive::kPrimVoid:
1576 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1577 UNREACHABLE();
1578 }
1579
1580 // Ints and objects are handled in the switch.
1581 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1582 codegen_->MaybeRecordImplicitNullCheck(instruction);
1583 }
1584}
1585
1586void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001587 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1588 ? LocationSummary::kCallOnSlowPath
1589 : LocationSummary::kNoCall;
1590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001591 locations->SetInAt(0, Location::RequiresRegister());
1592 locations->SetInAt(1, Location::RequiresRegister());
1593 if (instruction->HasUses()) {
1594 locations->SetOut(Location::SameAsFirstInput());
1595 }
1596}
1597
1598void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1599 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001600 BoundsCheckSlowPathMIPS64* slow_path =
1601 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001602 codegen_->AddSlowPath(slow_path);
1603
1604 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1605 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1606
1607 // length is limited by the maximum positive signed 32-bit integer.
1608 // Unsigned comparison of length and index checks for index < 0
1609 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001610 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001611}
1612
1613void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1614 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1615 instruction,
1616 LocationSummary::kCallOnSlowPath);
1617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001619 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001620 locations->AddTemp(Location::RequiresRegister());
1621}
1622
1623void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1624 LocationSummary* locations = instruction->GetLocations();
1625 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1626 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1627 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1628
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001629 SlowPathCodeMIPS64* slow_path =
1630 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001631 codegen_->AddSlowPath(slow_path);
1632
1633 // TODO: avoid this check if we know obj is not null.
1634 __ Beqzc(obj, slow_path->GetExitLabel());
1635 // Compare the class of `obj` with `cls`.
1636 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1637 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1638 __ Bind(slow_path->GetExitLabel());
1639}
1640
1641void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1642 LocationSummary* locations =
1643 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1644 locations->SetInAt(0, Location::RequiresRegister());
1645 if (check->HasUses()) {
1646 locations->SetOut(Location::SameAsFirstInput());
1647 }
1648}
1649
1650void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1651 // We assume the class is not null.
1652 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1653 check->GetLoadClass(),
1654 check,
1655 check->GetDexPc(),
1656 true);
1657 codegen_->AddSlowPath(slow_path);
1658 GenerateClassInitializationCheck(slow_path,
1659 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1660}
1661
1662void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1663 Primitive::Type in_type = compare->InputAt(0)->GetType();
1664
Alexey Frunze299a9392015-12-08 16:08:02 -08001665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001666
1667 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001668 case Primitive::kPrimBoolean:
1669 case Primitive::kPrimByte:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001672 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001673 case Primitive::kPrimLong:
1674 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001675 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1677 break;
1678
1679 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001680 case Primitive::kPrimDouble:
1681 locations->SetInAt(0, Location::RequiresFpuRegister());
1682 locations->SetInAt(1, Location::RequiresFpuRegister());
1683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001684 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685
1686 default:
1687 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1688 }
1689}
1690
1691void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1692 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001693 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001694 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1695
1696 // 0 if: left == right
1697 // 1 if: left > right
1698 // -1 if: left < right
1699 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001700 case Primitive::kPrimBoolean:
1701 case Primitive::kPrimByte:
1702 case Primitive::kPrimShort:
1703 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001704 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001705 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001707 Location rhs_location = locations->InAt(1);
1708 bool use_imm = rhs_location.IsConstant();
1709 GpuRegister rhs = ZERO;
1710 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001711 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001712 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1713 if (value != 0) {
1714 rhs = AT;
1715 __ LoadConst64(rhs, value);
1716 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001717 } else {
1718 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1719 if (value != 0) {
1720 rhs = AT;
1721 __ LoadConst32(rhs, value);
1722 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001723 }
1724 } else {
1725 rhs = rhs_location.AsRegister<GpuRegister>();
1726 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001727 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001728 __ Slt(res, rhs, lhs);
1729 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 break;
1731 }
1732
Alexey Frunze299a9392015-12-08 16:08:02 -08001733 case Primitive::kPrimFloat: {
1734 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1735 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1736 Mips64Label done;
1737 __ CmpEqS(FTMP, lhs, rhs);
1738 __ LoadConst32(res, 0);
1739 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001740 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001741 __ CmpLtS(FTMP, lhs, rhs);
1742 __ LoadConst32(res, -1);
1743 __ Bc1nez(FTMP, &done);
1744 __ LoadConst32(res, 1);
1745 } else {
1746 __ CmpLtS(FTMP, rhs, lhs);
1747 __ LoadConst32(res, 1);
1748 __ Bc1nez(FTMP, &done);
1749 __ LoadConst32(res, -1);
1750 }
1751 __ Bind(&done);
1752 break;
1753 }
1754
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001756 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1757 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1758 Mips64Label done;
1759 __ CmpEqD(FTMP, lhs, rhs);
1760 __ LoadConst32(res, 0);
1761 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001762 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001763 __ CmpLtD(FTMP, lhs, rhs);
1764 __ LoadConst32(res, -1);
1765 __ Bc1nez(FTMP, &done);
1766 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001767 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001768 __ CmpLtD(FTMP, rhs, lhs);
1769 __ LoadConst32(res, 1);
1770 __ Bc1nez(FTMP, &done);
1771 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001772 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001773 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001774 break;
1775 }
1776
1777 default:
1778 LOG(FATAL) << "Unimplemented compare type " << in_type;
1779 }
1780}
1781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001784 switch (instruction->InputAt(0)->GetType()) {
1785 default:
1786 case Primitive::kPrimLong:
1787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1789 break;
1790
1791 case Primitive::kPrimFloat:
1792 case Primitive::kPrimDouble:
1793 locations->SetInAt(0, Location::RequiresFpuRegister());
1794 locations->SetInAt(1, Location::RequiresFpuRegister());
1795 break;
1796 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001797 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1799 }
1800}
1801
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001802void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001803 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001804 return;
1805 }
1806
Alexey Frunze299a9392015-12-08 16:08:02 -08001807 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001810 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001811
Alexey Frunze299a9392015-12-08 16:08:02 -08001812 switch (type) {
1813 default:
1814 // Integer case.
1815 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1816 return;
1817 case Primitive::kPrimLong:
1818 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1819 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001820
Alexey Frunze299a9392015-12-08 16:08:02 -08001821 case Primitive::kPrimFloat:
1822 case Primitive::kPrimDouble:
1823 // TODO: don't use branches.
1824 GenerateFpCompareAndBranch(instruction->GetCondition(),
1825 instruction->IsGtBias(),
1826 type,
1827 locations,
1828 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001829 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001830 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001831
1832 // Convert the branches into the result.
1833 Mips64Label done;
1834
1835 // False case: result = 0.
1836 __ LoadConst32(dst, 0);
1837 __ Bc(&done);
1838
1839 // True case: result = 1.
1840 __ Bind(&true_label);
1841 __ LoadConst32(dst, 1);
1842 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001843}
1844
Alexey Frunzec857c742015-09-23 15:12:39 -07001845void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1846 DCHECK(instruction->IsDiv() || instruction->IsRem());
1847 Primitive::Type type = instruction->GetResultType();
1848
1849 LocationSummary* locations = instruction->GetLocations();
1850 Location second = locations->InAt(1);
1851 DCHECK(second.IsConstant());
1852
1853 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1854 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1855 int64_t imm = Int64FromConstant(second.GetConstant());
1856 DCHECK(imm == 1 || imm == -1);
1857
1858 if (instruction->IsRem()) {
1859 __ Move(out, ZERO);
1860 } else {
1861 if (imm == -1) {
1862 if (type == Primitive::kPrimInt) {
1863 __ Subu(out, ZERO, dividend);
1864 } else {
1865 DCHECK_EQ(type, Primitive::kPrimLong);
1866 __ Dsubu(out, ZERO, dividend);
1867 }
1868 } else if (out != dividend) {
1869 __ Move(out, dividend);
1870 }
1871 }
1872}
1873
1874void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1875 DCHECK(instruction->IsDiv() || instruction->IsRem());
1876 Primitive::Type type = instruction->GetResultType();
1877
1878 LocationSummary* locations = instruction->GetLocations();
1879 Location second = locations->InAt(1);
1880 DCHECK(second.IsConstant());
1881
1882 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1883 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1884 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001885 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001886 int ctz_imm = CTZ(abs_imm);
1887
1888 if (instruction->IsDiv()) {
1889 if (type == Primitive::kPrimInt) {
1890 if (ctz_imm == 1) {
1891 // Fast path for division by +/-2, which is very common.
1892 __ Srl(TMP, dividend, 31);
1893 } else {
1894 __ Sra(TMP, dividend, 31);
1895 __ Srl(TMP, TMP, 32 - ctz_imm);
1896 }
1897 __ Addu(out, dividend, TMP);
1898 __ Sra(out, out, ctz_imm);
1899 if (imm < 0) {
1900 __ Subu(out, ZERO, out);
1901 }
1902 } else {
1903 DCHECK_EQ(type, Primitive::kPrimLong);
1904 if (ctz_imm == 1) {
1905 // Fast path for division by +/-2, which is very common.
1906 __ Dsrl32(TMP, dividend, 31);
1907 } else {
1908 __ Dsra32(TMP, dividend, 31);
1909 if (ctz_imm > 32) {
1910 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1911 } else {
1912 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1913 }
1914 }
1915 __ Daddu(out, dividend, TMP);
1916 if (ctz_imm < 32) {
1917 __ Dsra(out, out, ctz_imm);
1918 } else {
1919 __ Dsra32(out, out, ctz_imm - 32);
1920 }
1921 if (imm < 0) {
1922 __ Dsubu(out, ZERO, out);
1923 }
1924 }
1925 } else {
1926 if (type == Primitive::kPrimInt) {
1927 if (ctz_imm == 1) {
1928 // Fast path for modulo +/-2, which is very common.
1929 __ Sra(TMP, dividend, 31);
1930 __ Subu(out, dividend, TMP);
1931 __ Andi(out, out, 1);
1932 __ Addu(out, out, TMP);
1933 } else {
1934 __ Sra(TMP, dividend, 31);
1935 __ Srl(TMP, TMP, 32 - ctz_imm);
1936 __ Addu(out, dividend, TMP);
1937 if (IsUint<16>(abs_imm - 1)) {
1938 __ Andi(out, out, abs_imm - 1);
1939 } else {
1940 __ Sll(out, out, 32 - ctz_imm);
1941 __ Srl(out, out, 32 - ctz_imm);
1942 }
1943 __ Subu(out, out, TMP);
1944 }
1945 } else {
1946 DCHECK_EQ(type, Primitive::kPrimLong);
1947 if (ctz_imm == 1) {
1948 // Fast path for modulo +/-2, which is very common.
1949 __ Dsra32(TMP, dividend, 31);
1950 __ Dsubu(out, dividend, TMP);
1951 __ Andi(out, out, 1);
1952 __ Daddu(out, out, TMP);
1953 } else {
1954 __ Dsra32(TMP, dividend, 31);
1955 if (ctz_imm > 32) {
1956 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1957 } else {
1958 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1959 }
1960 __ Daddu(out, dividend, TMP);
1961 if (IsUint<16>(abs_imm - 1)) {
1962 __ Andi(out, out, abs_imm - 1);
1963 } else {
1964 if (ctz_imm > 32) {
1965 __ Dsll(out, out, 64 - ctz_imm);
1966 __ Dsrl(out, out, 64 - ctz_imm);
1967 } else {
1968 __ Dsll32(out, out, 32 - ctz_imm);
1969 __ Dsrl32(out, out, 32 - ctz_imm);
1970 }
1971 }
1972 __ Dsubu(out, out, TMP);
1973 }
1974 }
1975 }
1976}
1977
1978void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1979 DCHECK(instruction->IsDiv() || instruction->IsRem());
1980
1981 LocationSummary* locations = instruction->GetLocations();
1982 Location second = locations->InAt(1);
1983 DCHECK(second.IsConstant());
1984
1985 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1986 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1987 int64_t imm = Int64FromConstant(second.GetConstant());
1988
1989 Primitive::Type type = instruction->GetResultType();
1990 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1991
1992 int64_t magic;
1993 int shift;
1994 CalculateMagicAndShiftForDivRem(imm,
1995 (type == Primitive::kPrimLong),
1996 &magic,
1997 &shift);
1998
1999 if (type == Primitive::kPrimInt) {
2000 __ LoadConst32(TMP, magic);
2001 __ MuhR6(TMP, dividend, TMP);
2002
2003 if (imm > 0 && magic < 0) {
2004 __ Addu(TMP, TMP, dividend);
2005 } else if (imm < 0 && magic > 0) {
2006 __ Subu(TMP, TMP, dividend);
2007 }
2008
2009 if (shift != 0) {
2010 __ Sra(TMP, TMP, shift);
2011 }
2012
2013 if (instruction->IsDiv()) {
2014 __ Sra(out, TMP, 31);
2015 __ Subu(out, TMP, out);
2016 } else {
2017 __ Sra(AT, TMP, 31);
2018 __ Subu(AT, TMP, AT);
2019 __ LoadConst32(TMP, imm);
2020 __ MulR6(TMP, AT, TMP);
2021 __ Subu(out, dividend, TMP);
2022 }
2023 } else {
2024 __ LoadConst64(TMP, magic);
2025 __ Dmuh(TMP, dividend, TMP);
2026
2027 if (imm > 0 && magic < 0) {
2028 __ Daddu(TMP, TMP, dividend);
2029 } else if (imm < 0 && magic > 0) {
2030 __ Dsubu(TMP, TMP, dividend);
2031 }
2032
2033 if (shift >= 32) {
2034 __ Dsra32(TMP, TMP, shift - 32);
2035 } else if (shift > 0) {
2036 __ Dsra(TMP, TMP, shift);
2037 }
2038
2039 if (instruction->IsDiv()) {
2040 __ Dsra32(out, TMP, 31);
2041 __ Dsubu(out, TMP, out);
2042 } else {
2043 __ Dsra32(AT, TMP, 31);
2044 __ Dsubu(AT, TMP, AT);
2045 __ LoadConst64(TMP, imm);
2046 __ Dmul(TMP, AT, TMP);
2047 __ Dsubu(out, dividend, TMP);
2048 }
2049 }
2050}
2051
2052void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2053 DCHECK(instruction->IsDiv() || instruction->IsRem());
2054 Primitive::Type type = instruction->GetResultType();
2055 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2056
2057 LocationSummary* locations = instruction->GetLocations();
2058 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2059 Location second = locations->InAt(1);
2060
2061 if (second.IsConstant()) {
2062 int64_t imm = Int64FromConstant(second.GetConstant());
2063 if (imm == 0) {
2064 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2065 } else if (imm == 1 || imm == -1) {
2066 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002067 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002068 DivRemByPowerOfTwo(instruction);
2069 } else {
2070 DCHECK(imm <= -2 || imm >= 2);
2071 GenerateDivRemWithAnyConstant(instruction);
2072 }
2073 } else {
2074 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2075 GpuRegister divisor = second.AsRegister<GpuRegister>();
2076 if (instruction->IsDiv()) {
2077 if (type == Primitive::kPrimInt)
2078 __ DivR6(out, dividend, divisor);
2079 else
2080 __ Ddiv(out, dividend, divisor);
2081 } else {
2082 if (type == Primitive::kPrimInt)
2083 __ ModR6(out, dividend, divisor);
2084 else
2085 __ Dmod(out, dividend, divisor);
2086 }
2087 }
2088}
2089
Alexey Frunze4dda3372015-06-01 18:31:49 -07002090void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2091 LocationSummary* locations =
2092 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2093 switch (div->GetResultType()) {
2094 case Primitive::kPrimInt:
2095 case Primitive::kPrimLong:
2096 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002097 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2099 break;
2100
2101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble:
2103 locations->SetInAt(0, Location::RequiresFpuRegister());
2104 locations->SetInAt(1, Location::RequiresFpuRegister());
2105 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2106 break;
2107
2108 default:
2109 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2110 }
2111}
2112
2113void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2114 Primitive::Type type = instruction->GetType();
2115 LocationSummary* locations = instruction->GetLocations();
2116
2117 switch (type) {
2118 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002119 case Primitive::kPrimLong:
2120 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002121 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble: {
2124 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2125 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2126 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2127 if (type == Primitive::kPrimFloat)
2128 __ DivS(dst, lhs, rhs);
2129 else
2130 __ DivD(dst, lhs, rhs);
2131 break;
2132 }
2133 default:
2134 LOG(FATAL) << "Unexpected div type " << type;
2135 }
2136}
2137
2138void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002139 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2140 ? LocationSummary::kCallOnSlowPath
2141 : LocationSummary::kNoCall;
2142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002143 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2144 if (instruction->HasUses()) {
2145 locations->SetOut(Location::SameAsFirstInput());
2146 }
2147}
2148
2149void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2150 SlowPathCodeMIPS64* slow_path =
2151 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2152 codegen_->AddSlowPath(slow_path);
2153 Location value = instruction->GetLocations()->InAt(0);
2154
2155 Primitive::Type type = instruction->GetType();
2156
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002157 if (!Primitive::IsIntegralType(type)) {
2158 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002159 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002160 }
2161
2162 if (value.IsConstant()) {
2163 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2164 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002165 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002166 } else {
2167 // A division by a non-null constant is valid. We don't need to perform
2168 // any check, so simply fall through.
2169 }
2170 } else {
2171 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2172 }
2173}
2174
2175void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2178 locations->SetOut(Location::ConstantLocation(constant));
2179}
2180
2181void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2182 // Will be generated at use site.
2183}
2184
2185void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2186 exit->SetLocations(nullptr);
2187}
2188
2189void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2190}
2191
2192void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2195 locations->SetOut(Location::ConstantLocation(constant));
2196}
2197
2198void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2199 // Will be generated at use site.
2200}
2201
David Brazdilfc6a86a2015-06-26 10:33:45 +00002202void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002203 DCHECK(!successor->IsExitBlock());
2204 HBasicBlock* block = got->GetBlock();
2205 HInstruction* previous = got->GetPrevious();
2206 HLoopInformation* info = block->GetLoopInformation();
2207
2208 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2209 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2210 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2211 return;
2212 }
2213 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2214 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2215 }
2216 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002217 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002218 }
2219}
2220
David Brazdilfc6a86a2015-06-26 10:33:45 +00002221void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2222 got->SetLocations(nullptr);
2223}
2224
2225void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2226 HandleGoto(got, got->GetSuccessor());
2227}
2228
2229void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2230 try_boundary->SetLocations(nullptr);
2231}
2232
2233void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2234 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2235 if (!successor->IsExitBlock()) {
2236 HandleGoto(try_boundary, successor);
2237 }
2238}
2239
Alexey Frunze299a9392015-12-08 16:08:02 -08002240void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2241 bool is64bit,
2242 LocationSummary* locations) {
2243 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2244 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2245 Location rhs_location = locations->InAt(1);
2246 GpuRegister rhs_reg = ZERO;
2247 int64_t rhs_imm = 0;
2248 bool use_imm = rhs_location.IsConstant();
2249 if (use_imm) {
2250 if (is64bit) {
2251 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2252 } else {
2253 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2254 }
2255 } else {
2256 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2257 }
2258 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2259
2260 switch (cond) {
2261 case kCondEQ:
2262 case kCondNE:
2263 if (use_imm && IsUint<16>(rhs_imm)) {
2264 __ Xori(dst, lhs, rhs_imm);
2265 } else {
2266 if (use_imm) {
2267 rhs_reg = TMP;
2268 __ LoadConst64(rhs_reg, rhs_imm);
2269 }
2270 __ Xor(dst, lhs, rhs_reg);
2271 }
2272 if (cond == kCondEQ) {
2273 __ Sltiu(dst, dst, 1);
2274 } else {
2275 __ Sltu(dst, ZERO, dst);
2276 }
2277 break;
2278
2279 case kCondLT:
2280 case kCondGE:
2281 if (use_imm && IsInt<16>(rhs_imm)) {
2282 __ Slti(dst, lhs, rhs_imm);
2283 } else {
2284 if (use_imm) {
2285 rhs_reg = TMP;
2286 __ LoadConst64(rhs_reg, rhs_imm);
2287 }
2288 __ Slt(dst, lhs, rhs_reg);
2289 }
2290 if (cond == kCondGE) {
2291 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2292 // only the slt instruction but no sge.
2293 __ Xori(dst, dst, 1);
2294 }
2295 break;
2296
2297 case kCondLE:
2298 case kCondGT:
2299 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2300 // Simulate lhs <= rhs via lhs < rhs + 1.
2301 __ Slti(dst, lhs, rhs_imm_plus_one);
2302 if (cond == kCondGT) {
2303 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2304 // only the slti instruction but no sgti.
2305 __ Xori(dst, dst, 1);
2306 }
2307 } else {
2308 if (use_imm) {
2309 rhs_reg = TMP;
2310 __ LoadConst64(rhs_reg, rhs_imm);
2311 }
2312 __ Slt(dst, rhs_reg, lhs);
2313 if (cond == kCondLE) {
2314 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2315 // only the slt instruction but no sle.
2316 __ Xori(dst, dst, 1);
2317 }
2318 }
2319 break;
2320
2321 case kCondB:
2322 case kCondAE:
2323 if (use_imm && IsInt<16>(rhs_imm)) {
2324 // Sltiu sign-extends its 16-bit immediate operand before
2325 // the comparison and thus lets us compare directly with
2326 // unsigned values in the ranges [0, 0x7fff] and
2327 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2328 __ Sltiu(dst, lhs, rhs_imm);
2329 } else {
2330 if (use_imm) {
2331 rhs_reg = TMP;
2332 __ LoadConst64(rhs_reg, rhs_imm);
2333 }
2334 __ Sltu(dst, lhs, rhs_reg);
2335 }
2336 if (cond == kCondAE) {
2337 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2338 // only the sltu instruction but no sgeu.
2339 __ Xori(dst, dst, 1);
2340 }
2341 break;
2342
2343 case kCondBE:
2344 case kCondA:
2345 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2346 // Simulate lhs <= rhs via lhs < rhs + 1.
2347 // Note that this only works if rhs + 1 does not overflow
2348 // to 0, hence the check above.
2349 // Sltiu sign-extends its 16-bit immediate operand before
2350 // the comparison and thus lets us compare directly with
2351 // unsigned values in the ranges [0, 0x7fff] and
2352 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2353 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2354 if (cond == kCondA) {
2355 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2356 // only the sltiu instruction but no sgtiu.
2357 __ Xori(dst, dst, 1);
2358 }
2359 } else {
2360 if (use_imm) {
2361 rhs_reg = TMP;
2362 __ LoadConst64(rhs_reg, rhs_imm);
2363 }
2364 __ Sltu(dst, rhs_reg, lhs);
2365 if (cond == kCondBE) {
2366 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2367 // only the sltu instruction but no sleu.
2368 __ Xori(dst, dst, 1);
2369 }
2370 }
2371 break;
2372 }
2373}
2374
2375void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2376 bool is64bit,
2377 LocationSummary* locations,
2378 Mips64Label* label) {
2379 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2380 Location rhs_location = locations->InAt(1);
2381 GpuRegister rhs_reg = ZERO;
2382 int64_t rhs_imm = 0;
2383 bool use_imm = rhs_location.IsConstant();
2384 if (use_imm) {
2385 if (is64bit) {
2386 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2387 } else {
2388 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2389 }
2390 } else {
2391 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2392 }
2393
2394 if (use_imm && rhs_imm == 0) {
2395 switch (cond) {
2396 case kCondEQ:
2397 case kCondBE: // <= 0 if zero
2398 __ Beqzc(lhs, label);
2399 break;
2400 case kCondNE:
2401 case kCondA: // > 0 if non-zero
2402 __ Bnezc(lhs, label);
2403 break;
2404 case kCondLT:
2405 __ Bltzc(lhs, label);
2406 break;
2407 case kCondGE:
2408 __ Bgezc(lhs, label);
2409 break;
2410 case kCondLE:
2411 __ Blezc(lhs, label);
2412 break;
2413 case kCondGT:
2414 __ Bgtzc(lhs, label);
2415 break;
2416 case kCondB: // always false
2417 break;
2418 case kCondAE: // always true
2419 __ Bc(label);
2420 break;
2421 }
2422 } else {
2423 if (use_imm) {
2424 rhs_reg = TMP;
2425 __ LoadConst64(rhs_reg, rhs_imm);
2426 }
2427 switch (cond) {
2428 case kCondEQ:
2429 __ Beqc(lhs, rhs_reg, label);
2430 break;
2431 case kCondNE:
2432 __ Bnec(lhs, rhs_reg, label);
2433 break;
2434 case kCondLT:
2435 __ Bltc(lhs, rhs_reg, label);
2436 break;
2437 case kCondGE:
2438 __ Bgec(lhs, rhs_reg, label);
2439 break;
2440 case kCondLE:
2441 __ Bgec(rhs_reg, lhs, label);
2442 break;
2443 case kCondGT:
2444 __ Bltc(rhs_reg, lhs, label);
2445 break;
2446 case kCondB:
2447 __ Bltuc(lhs, rhs_reg, label);
2448 break;
2449 case kCondAE:
2450 __ Bgeuc(lhs, rhs_reg, label);
2451 break;
2452 case kCondBE:
2453 __ Bgeuc(rhs_reg, lhs, label);
2454 break;
2455 case kCondA:
2456 __ Bltuc(rhs_reg, lhs, label);
2457 break;
2458 }
2459 }
2460}
2461
2462void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2463 bool gt_bias,
2464 Primitive::Type type,
2465 LocationSummary* locations,
2466 Mips64Label* label) {
2467 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2468 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2469 if (type == Primitive::kPrimFloat) {
2470 switch (cond) {
2471 case kCondEQ:
2472 __ CmpEqS(FTMP, lhs, rhs);
2473 __ Bc1nez(FTMP, label);
2474 break;
2475 case kCondNE:
2476 __ CmpEqS(FTMP, lhs, rhs);
2477 __ Bc1eqz(FTMP, label);
2478 break;
2479 case kCondLT:
2480 if (gt_bias) {
2481 __ CmpLtS(FTMP, lhs, rhs);
2482 } else {
2483 __ CmpUltS(FTMP, lhs, rhs);
2484 }
2485 __ Bc1nez(FTMP, label);
2486 break;
2487 case kCondLE:
2488 if (gt_bias) {
2489 __ CmpLeS(FTMP, lhs, rhs);
2490 } else {
2491 __ CmpUleS(FTMP, lhs, rhs);
2492 }
2493 __ Bc1nez(FTMP, label);
2494 break;
2495 case kCondGT:
2496 if (gt_bias) {
2497 __ CmpUltS(FTMP, rhs, lhs);
2498 } else {
2499 __ CmpLtS(FTMP, rhs, lhs);
2500 }
2501 __ Bc1nez(FTMP, label);
2502 break;
2503 case kCondGE:
2504 if (gt_bias) {
2505 __ CmpUleS(FTMP, rhs, lhs);
2506 } else {
2507 __ CmpLeS(FTMP, rhs, lhs);
2508 }
2509 __ Bc1nez(FTMP, label);
2510 break;
2511 default:
2512 LOG(FATAL) << "Unexpected non-floating-point condition";
2513 }
2514 } else {
2515 DCHECK_EQ(type, Primitive::kPrimDouble);
2516 switch (cond) {
2517 case kCondEQ:
2518 __ CmpEqD(FTMP, lhs, rhs);
2519 __ Bc1nez(FTMP, label);
2520 break;
2521 case kCondNE:
2522 __ CmpEqD(FTMP, lhs, rhs);
2523 __ Bc1eqz(FTMP, label);
2524 break;
2525 case kCondLT:
2526 if (gt_bias) {
2527 __ CmpLtD(FTMP, lhs, rhs);
2528 } else {
2529 __ CmpUltD(FTMP, lhs, rhs);
2530 }
2531 __ Bc1nez(FTMP, label);
2532 break;
2533 case kCondLE:
2534 if (gt_bias) {
2535 __ CmpLeD(FTMP, lhs, rhs);
2536 } else {
2537 __ CmpUleD(FTMP, lhs, rhs);
2538 }
2539 __ Bc1nez(FTMP, label);
2540 break;
2541 case kCondGT:
2542 if (gt_bias) {
2543 __ CmpUltD(FTMP, rhs, lhs);
2544 } else {
2545 __ CmpLtD(FTMP, rhs, lhs);
2546 }
2547 __ Bc1nez(FTMP, label);
2548 break;
2549 case kCondGE:
2550 if (gt_bias) {
2551 __ CmpUleD(FTMP, rhs, lhs);
2552 } else {
2553 __ CmpLeD(FTMP, rhs, lhs);
2554 }
2555 __ Bc1nez(FTMP, label);
2556 break;
2557 default:
2558 LOG(FATAL) << "Unexpected non-floating-point condition";
2559 }
2560 }
2561}
2562
Alexey Frunze4dda3372015-06-01 18:31:49 -07002563void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002564 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002565 Mips64Label* true_target,
2566 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002567 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002568
David Brazdil0debae72015-11-12 18:37:00 +00002569 if (true_target == nullptr && false_target == nullptr) {
2570 // Nothing to do. The code always falls through.
2571 return;
2572 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002573 // Constant condition, statically compared against "true" (integer value 1).
2574 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002575 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002576 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002577 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002579 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002580 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002581 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002582 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002583 }
David Brazdil0debae72015-11-12 18:37:00 +00002584 return;
2585 }
2586
2587 // The following code generates these patterns:
2588 // (1) true_target == nullptr && false_target != nullptr
2589 // - opposite condition true => branch to false_target
2590 // (2) true_target != nullptr && false_target == nullptr
2591 // - condition true => branch to true_target
2592 // (3) true_target != nullptr && false_target != nullptr
2593 // - condition true => branch to true_target
2594 // - branch to false_target
2595 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002596 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002597 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002598 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002599 if (true_target == nullptr) {
2600 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2601 } else {
2602 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2603 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002604 } else {
2605 // The condition instruction has not been materialized, use its inputs as
2606 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002607 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002608 Primitive::Type type = condition->InputAt(0)->GetType();
2609 LocationSummary* locations = cond->GetLocations();
2610 IfCondition if_cond = condition->GetCondition();
2611 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002612
David Brazdil0debae72015-11-12 18:37:00 +00002613 if (true_target == nullptr) {
2614 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002615 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002616 }
2617
Alexey Frunze299a9392015-12-08 16:08:02 -08002618 switch (type) {
2619 default:
2620 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2621 break;
2622 case Primitive::kPrimLong:
2623 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2624 break;
2625 case Primitive::kPrimFloat:
2626 case Primitive::kPrimDouble:
2627 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2628 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629 }
2630 }
David Brazdil0debae72015-11-12 18:37:00 +00002631
2632 // If neither branch falls through (case 3), the conditional branch to `true_target`
2633 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2634 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002635 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002636 }
2637}
2638
2639void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2640 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002641 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642 locations->SetInAt(0, Location::RequiresRegister());
2643 }
2644}
2645
2646void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002647 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2648 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002649 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002650 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002651 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002652 nullptr : codegen_->GetLabelOf(false_successor);
2653 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654}
2655
2656void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2657 LocationSummary* locations = new (GetGraph()->GetArena())
2658 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002659 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002660 locations->SetInAt(0, Location::RequiresRegister());
2661 }
2662}
2663
2664void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002665 SlowPathCodeMIPS64* slow_path =
2666 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002667 GenerateTestAndBranch(deoptimize,
2668 /* condition_input_index */ 0,
2669 slow_path->GetEntryLabel(),
2670 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002671}
2672
David Brazdil74eb1b22015-12-14 11:44:01 +00002673void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2675 if (Primitive::IsFloatingPointType(select->GetType())) {
2676 locations->SetInAt(0, Location::RequiresFpuRegister());
2677 locations->SetInAt(1, Location::RequiresFpuRegister());
2678 } else {
2679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetInAt(1, Location::RequiresRegister());
2681 }
2682 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2683 locations->SetInAt(2, Location::RequiresRegister());
2684 }
2685 locations->SetOut(Location::SameAsFirstInput());
2686}
2687
2688void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2689 LocationSummary* locations = select->GetLocations();
2690 Mips64Label false_target;
2691 GenerateTestAndBranch(select,
2692 /* condition_input_index */ 2,
2693 /* true_target */ nullptr,
2694 &false_target);
2695 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2696 __ Bind(&false_target);
2697}
2698
David Srbecky0cf44932015-12-09 14:09:59 +00002699void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2700 new (GetGraph()->GetArena()) LocationSummary(info);
2701}
2702
David Srbeckyd28f4a02016-03-14 17:14:24 +00002703void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2704 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002705}
2706
2707void CodeGeneratorMIPS64::GenerateNop() {
2708 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002709}
2710
Alexey Frunze4dda3372015-06-01 18:31:49 -07002711void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2712 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2715 locations->SetInAt(0, Location::RequiresRegister());
2716 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2717 locations->SetOut(Location::RequiresFpuRegister());
2718 } else {
2719 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2720 }
2721}
2722
2723void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2724 const FieldInfo& field_info) {
2725 Primitive::Type type = field_info.GetFieldType();
2726 LocationSummary* locations = instruction->GetLocations();
2727 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2728 LoadOperandType load_type = kLoadUnsignedByte;
2729 switch (type) {
2730 case Primitive::kPrimBoolean:
2731 load_type = kLoadUnsignedByte;
2732 break;
2733 case Primitive::kPrimByte:
2734 load_type = kLoadSignedByte;
2735 break;
2736 case Primitive::kPrimShort:
2737 load_type = kLoadSignedHalfword;
2738 break;
2739 case Primitive::kPrimChar:
2740 load_type = kLoadUnsignedHalfword;
2741 break;
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimFloat:
2744 load_type = kLoadWord;
2745 break;
2746 case Primitive::kPrimLong:
2747 case Primitive::kPrimDouble:
2748 load_type = kLoadDoubleword;
2749 break;
2750 case Primitive::kPrimNot:
2751 load_type = kLoadUnsignedWord;
2752 break;
2753 case Primitive::kPrimVoid:
2754 LOG(FATAL) << "Unreachable type " << type;
2755 UNREACHABLE();
2756 }
2757 if (!Primitive::IsFloatingPointType(type)) {
2758 DCHECK(locations->Out().IsRegister());
2759 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2760 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2761 } else {
2762 DCHECK(locations->Out().IsFpuRegister());
2763 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2764 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2765 }
2766
2767 codegen_->MaybeRecordImplicitNullCheck(instruction);
2768 // TODO: memory barrier?
2769}
2770
2771void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2772 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2773 LocationSummary* locations =
2774 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2775 locations->SetInAt(0, Location::RequiresRegister());
2776 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2777 locations->SetInAt(1, Location::RequiresFpuRegister());
2778 } else {
2779 locations->SetInAt(1, Location::RequiresRegister());
2780 }
2781}
2782
2783void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002784 const FieldInfo& field_info,
2785 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002786 Primitive::Type type = field_info.GetFieldType();
2787 LocationSummary* locations = instruction->GetLocations();
2788 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2789 StoreOperandType store_type = kStoreByte;
2790 switch (type) {
2791 case Primitive::kPrimBoolean:
2792 case Primitive::kPrimByte:
2793 store_type = kStoreByte;
2794 break;
2795 case Primitive::kPrimShort:
2796 case Primitive::kPrimChar:
2797 store_type = kStoreHalfword;
2798 break;
2799 case Primitive::kPrimInt:
2800 case Primitive::kPrimFloat:
2801 case Primitive::kPrimNot:
2802 store_type = kStoreWord;
2803 break;
2804 case Primitive::kPrimLong:
2805 case Primitive::kPrimDouble:
2806 store_type = kStoreDoubleword;
2807 break;
2808 case Primitive::kPrimVoid:
2809 LOG(FATAL) << "Unreachable type " << type;
2810 UNREACHABLE();
2811 }
2812 if (!Primitive::IsFloatingPointType(type)) {
2813 DCHECK(locations->InAt(1).IsRegister());
2814 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2815 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2816 } else {
2817 DCHECK(locations->InAt(1).IsFpuRegister());
2818 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2819 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2820 }
2821
2822 codegen_->MaybeRecordImplicitNullCheck(instruction);
2823 // TODO: memory barriers?
2824 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2825 DCHECK(locations->InAt(1).IsRegister());
2826 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002827 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002828 }
2829}
2830
2831void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2832 HandleFieldGet(instruction, instruction->GetFieldInfo());
2833}
2834
2835void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2836 HandleFieldGet(instruction, instruction->GetFieldInfo());
2837}
2838
2839void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2840 HandleFieldSet(instruction, instruction->GetFieldInfo());
2841}
2842
2843void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002844 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002845}
2846
2847void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2848 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002849 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::RequiresRegister());
2853 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002854 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002855 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2856}
2857
2858void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2859 LocationSummary* locations = instruction->GetLocations();
2860 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2861 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2862 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2863
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002864 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002865
2866 // Return 0 if `obj` is null.
2867 // TODO: Avoid this check if we know `obj` is not null.
2868 __ Move(out, ZERO);
2869 __ Beqzc(obj, &done);
2870
2871 // Compare the class of `obj` with `cls`.
2872 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002873 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002874 // Classes must be equal for the instanceof to succeed.
2875 __ Xor(out, out, cls);
2876 __ Sltiu(out, out, 1);
2877 } else {
2878 // If the classes are not equal, we go into a slow path.
2879 DCHECK(locations->OnlyCallsOnSlowPath());
2880 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002881 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002882 codegen_->AddSlowPath(slow_path);
2883 __ Bnec(out, cls, slow_path->GetEntryLabel());
2884 __ LoadConst32(out, 1);
2885 __ Bind(slow_path->GetExitLabel());
2886 }
2887
2888 __ Bind(&done);
2889}
2890
2891void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2893 locations->SetOut(Location::ConstantLocation(constant));
2894}
2895
2896void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2897 // Will be generated at use site.
2898}
2899
2900void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2902 locations->SetOut(Location::ConstantLocation(constant));
2903}
2904
2905void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2906 // Will be generated at use site.
2907}
2908
Calin Juravle175dc732015-08-25 15:42:32 +01002909void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2910 // The trampoline uses the same calling convention as dex calling conventions,
2911 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2912 // the method_idx.
2913 HandleInvoke(invoke);
2914}
2915
2916void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2917 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2918}
2919
Alexey Frunze4dda3372015-06-01 18:31:49 -07002920void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2921 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2922 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2923}
2924
2925void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2926 HandleInvoke(invoke);
2927 // The register T0 is required to be used for the hidden argument in
2928 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2929 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2930}
2931
2932void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2933 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2934 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2935 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2936 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2937 Location receiver = invoke->GetLocations()->InAt(0);
2938 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002939 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002940
2941 // Set the hidden argument.
2942 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2943 invoke->GetDexMethodIndex());
2944
2945 // temp = object->GetClass();
2946 if (receiver.IsStackSlot()) {
2947 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2948 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2949 } else {
2950 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2951 }
2952 codegen_->MaybeRecordImplicitNullCheck(invoke);
2953 // temp = temp->GetImtEntryAt(method_offset);
2954 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2955 // T9 = temp->GetEntryPoint();
2956 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2957 // T9();
2958 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002959 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002960 DCHECK(!codegen_->IsLeafMethod());
2961 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2962}
2963
2964void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002965 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2966 if (intrinsic.TryDispatch(invoke)) {
2967 return;
2968 }
2969
Alexey Frunze4dda3372015-06-01 18:31:49 -07002970 HandleInvoke(invoke);
2971}
2972
2973void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002974 // Explicit clinit checks triggered by static invokes must have been pruned by
2975 // art::PrepareForRegisterAllocation.
2976 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002977
Chris Larsen3039e382015-08-26 07:54:08 -07002978 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2979 if (intrinsic.TryDispatch(invoke)) {
2980 return;
2981 }
2982
Alexey Frunze4dda3372015-06-01 18:31:49 -07002983 HandleInvoke(invoke);
2984
2985 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2986 // clobbering somewhere else, reduce further register pressure by avoiding
2987 // allocation of a register for the current method pointer like on x86 baseline.
2988 // TODO: remove this once all the issues with register saving/restoring are
2989 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002990 if (invoke->HasCurrentMethodInput()) {
2991 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002992 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002993 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002994 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002995 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002996 }
2997}
2998
Chris Larsen3039e382015-08-26 07:54:08 -07002999static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003000 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003001 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3002 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003003 return true;
3004 }
3005 return false;
3006}
3007
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003008HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3009 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3010 // TODO: Implement other kinds.
3011 return HLoadString::LoadKind::kDexCacheViaMethod;
3012}
3013
Vladimir Markodc151b22015-10-15 18:02:30 +01003014HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3015 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3016 MethodReference target_method ATTRIBUTE_UNUSED) {
3017 switch (desired_dispatch_info.method_load_kind) {
3018 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3019 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3020 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3021 return HInvokeStaticOrDirect::DispatchInfo {
3022 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3023 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3024 0u,
3025 0u
3026 };
3027 default:
3028 break;
3029 }
3030 switch (desired_dispatch_info.code_ptr_location) {
3031 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3032 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3033 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3034 return HInvokeStaticOrDirect::DispatchInfo {
3035 desired_dispatch_info.method_load_kind,
3036 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3037 desired_dispatch_info.method_load_data,
3038 0u
3039 };
3040 default:
3041 return desired_dispatch_info;
3042 }
3043}
3044
Alexey Frunze4dda3372015-06-01 18:31:49 -07003045void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3046 // All registers are assumed to be correctly set up per the calling convention.
3047
Vladimir Marko58155012015-08-19 12:49:41 +00003048 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3049 switch (invoke->GetMethodLoadKind()) {
3050 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3051 // temp = thread->string_init_entrypoint
3052 __ LoadFromOffset(kLoadDoubleword,
3053 temp.AsRegister<GpuRegister>(),
3054 TR,
3055 invoke->GetStringInitOffset());
3056 break;
3057 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003058 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003059 break;
3060 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3061 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3062 break;
3063 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003064 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003065 // TODO: Implement these types.
3066 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3067 LOG(FATAL) << "Unsupported";
3068 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003069 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003070 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003071 GpuRegister reg = temp.AsRegister<GpuRegister>();
3072 GpuRegister method_reg;
3073 if (current_method.IsRegister()) {
3074 method_reg = current_method.AsRegister<GpuRegister>();
3075 } else {
3076 // TODO: use the appropriate DCHECK() here if possible.
3077 // DCHECK(invoke->GetLocations()->Intrinsified());
3078 DCHECK(!current_method.IsValid());
3079 method_reg = reg;
3080 __ Ld(reg, SP, kCurrentMethodStackOffset);
3081 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082
Vladimir Marko58155012015-08-19 12:49:41 +00003083 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003084 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003085 reg,
3086 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003087 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003088 // temp = temp[index_in_cache];
3089 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3090 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003091 __ LoadFromOffset(kLoadDoubleword,
3092 reg,
3093 reg,
3094 CodeGenerator::GetCachePointerOffset(index_in_cache));
3095 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003096 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097 }
3098
Vladimir Marko58155012015-08-19 12:49:41 +00003099 switch (invoke->GetCodePtrLocation()) {
3100 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003101 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003102 break;
3103 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3104 // LR = invoke->GetDirectCodePtr();
3105 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3106 // LR()
3107 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003108 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003109 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003110 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003111 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3112 // TODO: Implement these types.
3113 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3114 LOG(FATAL) << "Unsupported";
3115 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003116 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3117 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3118 __ LoadFromOffset(kLoadDoubleword,
3119 T9,
3120 callee_method.AsRegister<GpuRegister>(),
3121 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003122 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003123 // T9()
3124 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003125 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003126 break;
3127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128 DCHECK(!IsLeafMethod());
3129}
3130
3131void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003132 // Explicit clinit checks triggered by static invokes must have been pruned by
3133 // art::PrepareForRegisterAllocation.
3134 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003135
3136 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3137 return;
3138 }
3139
3140 LocationSummary* locations = invoke->GetLocations();
3141 codegen_->GenerateStaticOrDirectCall(invoke,
3142 locations->HasTemps()
3143 ? locations->GetTemp(0)
3144 : Location::NoLocation());
3145 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3146}
3147
Alexey Frunze53afca12015-11-05 16:34:23 -08003148void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003149 // Use the calling convention instead of the location of the receiver, as
3150 // intrinsics may have put the receiver in a different register. In the intrinsics
3151 // slow path, the arguments have been moved to the right place, so here we are
3152 // guaranteed that the receiver is the first register of the calling convention.
3153 InvokeDexCallingConvention calling_convention;
3154 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3155
Alexey Frunze53afca12015-11-05 16:34:23 -08003156 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003157 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3158 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3159 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003160 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003161
3162 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003163 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003164 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003165 // temp = temp->GetMethodAt(method_offset);
3166 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3167 // T9 = temp->GetEntryPoint();
3168 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3169 // T9();
3170 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003171 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003172}
3173
3174void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3175 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3176 return;
3177 }
3178
3179 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003180 DCHECK(!codegen_->IsLeafMethod());
3181 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3182}
3183
3184void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003185 InvokeRuntimeCallingConvention calling_convention;
3186 CodeGenerator::CreateLoadClassLocationSummary(
3187 cls,
3188 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003189 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003190}
3191
3192void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3193 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003194 if (cls->NeedsAccessCheck()) {
3195 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3196 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3197 cls,
3198 cls->GetDexPc(),
3199 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003200 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003201 return;
3202 }
3203
3204 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3205 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3206 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003207 DCHECK(!cls->CanCallRuntime());
3208 DCHECK(!cls->MustGenerateClinitCheck());
3209 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3210 ArtMethod::DeclaringClassOffset().Int32Value());
3211 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003212 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3213 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003214 __ LoadFromOffset(
3215 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003216 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003217 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3218 DCHECK(cls->CanCallRuntime());
3219 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3220 cls,
3221 cls,
3222 cls->GetDexPc(),
3223 cls->MustGenerateClinitCheck());
3224 codegen_->AddSlowPath(slow_path);
3225 if (!cls->IsInDexCache()) {
3226 __ Beqzc(out, slow_path->GetEntryLabel());
3227 }
3228 if (cls->MustGenerateClinitCheck()) {
3229 GenerateClassInitializationCheck(slow_path, out);
3230 } else {
3231 __ Bind(slow_path->GetExitLabel());
3232 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003233 }
3234 }
3235}
3236
David Brazdilcb1c0552015-08-04 16:22:25 +01003237static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003238 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003239}
3240
Alexey Frunze4dda3372015-06-01 18:31:49 -07003241void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3242 LocationSummary* locations =
3243 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3244 locations->SetOut(Location::RequiresRegister());
3245}
3246
3247void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3248 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003249 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3250}
3251
3252void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3253 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3254}
3255
3256void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3257 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003258}
3259
Alexey Frunze4dda3372015-06-01 18:31:49 -07003260void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003261 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3262 ? LocationSummary::kCallOnSlowPath
3263 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003265 locations->SetInAt(0, Location::RequiresRegister());
3266 locations->SetOut(Location::RequiresRegister());
3267}
3268
3269void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003270 LocationSummary* locations = load->GetLocations();
3271 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3272 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3273 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3274 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003275 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003276 __ LoadFromOffset(
3277 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003278 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003279
3280 if (!load->IsInDexCache()) {
3281 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3282 codegen_->AddSlowPath(slow_path);
3283 __ Beqzc(out, slow_path->GetEntryLabel());
3284 __ Bind(slow_path->GetExitLabel());
3285 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003286}
3287
Alexey Frunze4dda3372015-06-01 18:31:49 -07003288void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3290 locations->SetOut(Location::ConstantLocation(constant));
3291}
3292
3293void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3294 // Will be generated at use site.
3295}
3296
3297void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3298 LocationSummary* locations =
3299 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3300 InvokeRuntimeCallingConvention calling_convention;
3301 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3302}
3303
3304void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3305 codegen_->InvokeRuntime(instruction->IsEnter()
3306 ? QUICK_ENTRY_POINT(pLockObject)
3307 : QUICK_ENTRY_POINT(pUnlockObject),
3308 instruction,
3309 instruction->GetDexPc(),
3310 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003311 if (instruction->IsEnter()) {
3312 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3313 } else {
3314 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3315 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003316}
3317
3318void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3319 LocationSummary* locations =
3320 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3321 switch (mul->GetResultType()) {
3322 case Primitive::kPrimInt:
3323 case Primitive::kPrimLong:
3324 locations->SetInAt(0, Location::RequiresRegister());
3325 locations->SetInAt(1, Location::RequiresRegister());
3326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3327 break;
3328
3329 case Primitive::kPrimFloat:
3330 case Primitive::kPrimDouble:
3331 locations->SetInAt(0, Location::RequiresFpuRegister());
3332 locations->SetInAt(1, Location::RequiresFpuRegister());
3333 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3334 break;
3335
3336 default:
3337 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3338 }
3339}
3340
3341void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3342 Primitive::Type type = instruction->GetType();
3343 LocationSummary* locations = instruction->GetLocations();
3344
3345 switch (type) {
3346 case Primitive::kPrimInt:
3347 case Primitive::kPrimLong: {
3348 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3349 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3350 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3351 if (type == Primitive::kPrimInt)
3352 __ MulR6(dst, lhs, rhs);
3353 else
3354 __ Dmul(dst, lhs, rhs);
3355 break;
3356 }
3357 case Primitive::kPrimFloat:
3358 case Primitive::kPrimDouble: {
3359 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3360 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3361 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3362 if (type == Primitive::kPrimFloat)
3363 __ MulS(dst, lhs, rhs);
3364 else
3365 __ MulD(dst, lhs, rhs);
3366 break;
3367 }
3368 default:
3369 LOG(FATAL) << "Unexpected mul type " << type;
3370 }
3371}
3372
3373void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3374 LocationSummary* locations =
3375 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3376 switch (neg->GetResultType()) {
3377 case Primitive::kPrimInt:
3378 case Primitive::kPrimLong:
3379 locations->SetInAt(0, Location::RequiresRegister());
3380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3381 break;
3382
3383 case Primitive::kPrimFloat:
3384 case Primitive::kPrimDouble:
3385 locations->SetInAt(0, Location::RequiresFpuRegister());
3386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3387 break;
3388
3389 default:
3390 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3391 }
3392}
3393
3394void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3395 Primitive::Type type = instruction->GetType();
3396 LocationSummary* locations = instruction->GetLocations();
3397
3398 switch (type) {
3399 case Primitive::kPrimInt:
3400 case Primitive::kPrimLong: {
3401 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3402 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3403 if (type == Primitive::kPrimInt)
3404 __ Subu(dst, ZERO, src);
3405 else
3406 __ Dsubu(dst, ZERO, src);
3407 break;
3408 }
3409 case Primitive::kPrimFloat:
3410 case Primitive::kPrimDouble: {
3411 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3412 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3413 if (type == Primitive::kPrimFloat)
3414 __ NegS(dst, src);
3415 else
3416 __ NegD(dst, src);
3417 break;
3418 }
3419 default:
3420 LOG(FATAL) << "Unexpected neg type " << type;
3421 }
3422}
3423
3424void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3425 LocationSummary* locations =
3426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3427 InvokeRuntimeCallingConvention calling_convention;
3428 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3429 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3430 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3431 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3432}
3433
3434void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3435 LocationSummary* locations = instruction->GetLocations();
3436 // Move an uint16_t value to a register.
3437 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003438 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3439 instruction,
3440 instruction->GetDexPc(),
3441 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003442 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3443}
3444
3445void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3446 LocationSummary* locations =
3447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3448 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003449 if (instruction->IsStringAlloc()) {
3450 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3451 } else {
3452 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3453 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3454 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003455 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3456}
3457
3458void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003459 if (instruction->IsStringAlloc()) {
3460 // String is allocated through StringFactory. Call NewEmptyString entry point.
3461 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003462 MemberOffset code_offset =
3463 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003464 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3465 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3466 __ Jalr(T9);
3467 __ Nop();
3468 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3469 } else {
3470 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3471 instruction,
3472 instruction->GetDexPc(),
3473 nullptr);
3474 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3475 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003476}
3477
3478void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3480 locations->SetInAt(0, Location::RequiresRegister());
3481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3482}
3483
3484void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3485 Primitive::Type type = instruction->GetType();
3486 LocationSummary* locations = instruction->GetLocations();
3487
3488 switch (type) {
3489 case Primitive::kPrimInt:
3490 case Primitive::kPrimLong: {
3491 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3492 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3493 __ Nor(dst, src, ZERO);
3494 break;
3495 }
3496
3497 default:
3498 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3499 }
3500}
3501
3502void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3504 locations->SetInAt(0, Location::RequiresRegister());
3505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3506}
3507
3508void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3509 LocationSummary* locations = instruction->GetLocations();
3510 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3511 locations->InAt(0).AsRegister<GpuRegister>(),
3512 1);
3513}
3514
3515void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003516 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3517 ? LocationSummary::kCallOnSlowPath
3518 : LocationSummary::kNoCall;
3519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003520 locations->SetInAt(0, Location::RequiresRegister());
3521 if (instruction->HasUses()) {
3522 locations->SetOut(Location::SameAsFirstInput());
3523 }
3524}
3525
Calin Juravle2ae48182016-03-16 14:05:09 +00003526void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3527 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003528 return;
3529 }
3530 Location obj = instruction->GetLocations()->InAt(0);
3531
3532 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003533 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003534}
3535
Calin Juravle2ae48182016-03-16 14:05:09 +00003536void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003537 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003538 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003539
3540 Location obj = instruction->GetLocations()->InAt(0);
3541
3542 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3543}
3544
3545void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003546 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003547}
3548
3549void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3550 HandleBinaryOp(instruction);
3551}
3552
3553void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3554 HandleBinaryOp(instruction);
3555}
3556
3557void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3558 LOG(FATAL) << "Unreachable";
3559}
3560
3561void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3562 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3563}
3564
3565void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3567 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3568 if (location.IsStackSlot()) {
3569 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3570 } else if (location.IsDoubleStackSlot()) {
3571 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3572 }
3573 locations->SetOut(location);
3574}
3575
3576void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3577 ATTRIBUTE_UNUSED) {
3578 // Nothing to do, the parameter is already at its location.
3579}
3580
3581void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3582 LocationSummary* locations =
3583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3584 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3585}
3586
3587void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3588 ATTRIBUTE_UNUSED) {
3589 // Nothing to do, the method is already at its location.
3590}
3591
3592void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3593 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3594 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3595 locations->SetInAt(i, Location::Any());
3596 }
3597 locations->SetOut(Location::Any());
3598}
3599
3600void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3601 LOG(FATAL) << "Unreachable";
3602}
3603
3604void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3605 Primitive::Type type = rem->GetResultType();
3606 LocationSummary::CallKind call_kind =
3607 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3609
3610 switch (type) {
3611 case Primitive::kPrimInt:
3612 case Primitive::kPrimLong:
3613 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003614 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3616 break;
3617
3618 case Primitive::kPrimFloat:
3619 case Primitive::kPrimDouble: {
3620 InvokeRuntimeCallingConvention calling_convention;
3621 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3622 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3623 locations->SetOut(calling_convention.GetReturnLocation(type));
3624 break;
3625 }
3626
3627 default:
3628 LOG(FATAL) << "Unexpected rem type " << type;
3629 }
3630}
3631
3632void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3633 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003634
3635 switch (type) {
3636 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003637 case Primitive::kPrimLong:
3638 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003639 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003640
3641 case Primitive::kPrimFloat:
3642 case Primitive::kPrimDouble: {
3643 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3644 : QUICK_ENTRY_POINT(pFmod);
3645 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003646 if (type == Primitive::kPrimFloat) {
3647 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3648 } else {
3649 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3650 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003651 break;
3652 }
3653 default:
3654 LOG(FATAL) << "Unexpected rem type " << type;
3655 }
3656}
3657
3658void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3659 memory_barrier->SetLocations(nullptr);
3660}
3661
3662void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3663 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3664}
3665
3666void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3668 Primitive::Type return_type = ret->InputAt(0)->GetType();
3669 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3670}
3671
3672void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3673 codegen_->GenerateFrameExit();
3674}
3675
3676void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3677 ret->SetLocations(nullptr);
3678}
3679
3680void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3681 codegen_->GenerateFrameExit();
3682}
3683
Alexey Frunze92d90602015-12-18 18:16:36 -08003684void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3685 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003686}
3687
Alexey Frunze92d90602015-12-18 18:16:36 -08003688void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3689 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003690}
3691
Alexey Frunze4dda3372015-06-01 18:31:49 -07003692void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3693 HandleShift(shl);
3694}
3695
3696void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3697 HandleShift(shl);
3698}
3699
3700void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3701 HandleShift(shr);
3702}
3703
3704void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3705 HandleShift(shr);
3706}
3707
Alexey Frunze4dda3372015-06-01 18:31:49 -07003708void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3709 HandleBinaryOp(instruction);
3710}
3711
3712void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3713 HandleBinaryOp(instruction);
3714}
3715
3716void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3717 HandleFieldGet(instruction, instruction->GetFieldInfo());
3718}
3719
3720void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3721 HandleFieldGet(instruction, instruction->GetFieldInfo());
3722}
3723
3724void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3725 HandleFieldSet(instruction, instruction->GetFieldInfo());
3726}
3727
3728void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003729 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003730}
3731
Calin Juravlee460d1d2015-09-29 04:52:17 +01003732void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3733 HUnresolvedInstanceFieldGet* instruction) {
3734 FieldAccessCallingConventionMIPS64 calling_convention;
3735 codegen_->CreateUnresolvedFieldLocationSummary(
3736 instruction, instruction->GetFieldType(), calling_convention);
3737}
3738
3739void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3740 HUnresolvedInstanceFieldGet* instruction) {
3741 FieldAccessCallingConventionMIPS64 calling_convention;
3742 codegen_->GenerateUnresolvedFieldAccess(instruction,
3743 instruction->GetFieldType(),
3744 instruction->GetFieldIndex(),
3745 instruction->GetDexPc(),
3746 calling_convention);
3747}
3748
3749void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3750 HUnresolvedInstanceFieldSet* instruction) {
3751 FieldAccessCallingConventionMIPS64 calling_convention;
3752 codegen_->CreateUnresolvedFieldLocationSummary(
3753 instruction, instruction->GetFieldType(), calling_convention);
3754}
3755
3756void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3757 HUnresolvedInstanceFieldSet* instruction) {
3758 FieldAccessCallingConventionMIPS64 calling_convention;
3759 codegen_->GenerateUnresolvedFieldAccess(instruction,
3760 instruction->GetFieldType(),
3761 instruction->GetFieldIndex(),
3762 instruction->GetDexPc(),
3763 calling_convention);
3764}
3765
3766void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3767 HUnresolvedStaticFieldGet* instruction) {
3768 FieldAccessCallingConventionMIPS64 calling_convention;
3769 codegen_->CreateUnresolvedFieldLocationSummary(
3770 instruction, instruction->GetFieldType(), calling_convention);
3771}
3772
3773void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3774 HUnresolvedStaticFieldGet* instruction) {
3775 FieldAccessCallingConventionMIPS64 calling_convention;
3776 codegen_->GenerateUnresolvedFieldAccess(instruction,
3777 instruction->GetFieldType(),
3778 instruction->GetFieldIndex(),
3779 instruction->GetDexPc(),
3780 calling_convention);
3781}
3782
3783void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3784 HUnresolvedStaticFieldSet* instruction) {
3785 FieldAccessCallingConventionMIPS64 calling_convention;
3786 codegen_->CreateUnresolvedFieldLocationSummary(
3787 instruction, instruction->GetFieldType(), calling_convention);
3788}
3789
3790void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3791 HUnresolvedStaticFieldSet* instruction) {
3792 FieldAccessCallingConventionMIPS64 calling_convention;
3793 codegen_->GenerateUnresolvedFieldAccess(instruction,
3794 instruction->GetFieldType(),
3795 instruction->GetFieldIndex(),
3796 instruction->GetDexPc(),
3797 calling_convention);
3798}
3799
Alexey Frunze4dda3372015-06-01 18:31:49 -07003800void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3801 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3802}
3803
3804void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3805 HBasicBlock* block = instruction->GetBlock();
3806 if (block->GetLoopInformation() != nullptr) {
3807 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3808 // The back edge will generate the suspend check.
3809 return;
3810 }
3811 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3812 // The goto will generate the suspend check.
3813 return;
3814 }
3815 GenerateSuspendCheck(instruction, nullptr);
3816}
3817
Alexey Frunze4dda3372015-06-01 18:31:49 -07003818void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3819 LocationSummary* locations =
3820 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3821 InvokeRuntimeCallingConvention calling_convention;
3822 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3823}
3824
3825void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3826 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3827 instruction,
3828 instruction->GetDexPc(),
3829 nullptr);
3830 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3831}
3832
3833void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3834 Primitive::Type input_type = conversion->GetInputType();
3835 Primitive::Type result_type = conversion->GetResultType();
3836 DCHECK_NE(input_type, result_type);
3837
3838 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3839 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3840 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3841 }
3842
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003843 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3844
3845 if (Primitive::IsFloatingPointType(input_type)) {
3846 locations->SetInAt(0, Location::RequiresFpuRegister());
3847 } else {
3848 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003849 }
3850
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003851 if (Primitive::IsFloatingPointType(result_type)) {
3852 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003853 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003855 }
3856}
3857
3858void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3859 LocationSummary* locations = conversion->GetLocations();
3860 Primitive::Type result_type = conversion->GetResultType();
3861 Primitive::Type input_type = conversion->GetInputType();
3862
3863 DCHECK_NE(input_type, result_type);
3864
3865 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3866 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3867 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3868
3869 switch (result_type) {
3870 case Primitive::kPrimChar:
3871 __ Andi(dst, src, 0xFFFF);
3872 break;
3873 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003874 if (input_type == Primitive::kPrimLong) {
3875 // Type conversion from long to types narrower than int is a result of code
3876 // transformations. To avoid unpredictable results for SEB and SEH, we first
3877 // need to sign-extend the low 32-bit value into bits 32 through 63.
3878 __ Sll(dst, src, 0);
3879 __ Seb(dst, dst);
3880 } else {
3881 __ Seb(dst, src);
3882 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003883 break;
3884 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003885 if (input_type == Primitive::kPrimLong) {
3886 // Type conversion from long to types narrower than int is a result of code
3887 // transformations. To avoid unpredictable results for SEB and SEH, we first
3888 // need to sign-extend the low 32-bit value into bits 32 through 63.
3889 __ Sll(dst, src, 0);
3890 __ Seh(dst, dst);
3891 } else {
3892 __ Seh(dst, src);
3893 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003894 break;
3895 case Primitive::kPrimInt:
3896 case Primitive::kPrimLong:
3897 // Sign-extend 32-bit int into bits 32 through 63 for
3898 // int-to-long and long-to-int conversions
3899 __ Sll(dst, src, 0);
3900 break;
3901
3902 default:
3903 LOG(FATAL) << "Unexpected type conversion from " << input_type
3904 << " to " << result_type;
3905 }
3906 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003907 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3908 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3909 if (input_type == Primitive::kPrimLong) {
3910 __ Dmtc1(src, FTMP);
3911 if (result_type == Primitive::kPrimFloat) {
3912 __ Cvtsl(dst, FTMP);
3913 } else {
3914 __ Cvtdl(dst, FTMP);
3915 }
3916 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003917 __ Mtc1(src, FTMP);
3918 if (result_type == Primitive::kPrimFloat) {
3919 __ Cvtsw(dst, FTMP);
3920 } else {
3921 __ Cvtdw(dst, FTMP);
3922 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003923 }
3924 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3925 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003926 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3927 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3928 Mips64Label truncate;
3929 Mips64Label done;
3930
3931 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3932 // value when the input is either a NaN or is outside of the range of the output type
3933 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3934 // the same result.
3935 //
3936 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3937 // value of the output type if the input is outside of the range after the truncation or
3938 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3939 // results. This matches the desired float/double-to-int/long conversion exactly.
3940 //
3941 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3942 //
3943 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3944 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3945 // even though it must be NAN2008=1 on R6.
3946 //
3947 // The code takes care of the different behaviors by first comparing the input to the
3948 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3949 // If the input is greater than or equal to the minimum, it procedes to the truncate
3950 // instruction, which will handle such an input the same way irrespective of NAN2008.
3951 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3952 // in order to return either zero or the minimum value.
3953 //
3954 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3955 // truncate instruction for MIPS64R6.
3956 if (input_type == Primitive::kPrimFloat) {
3957 uint32_t min_val = (result_type == Primitive::kPrimLong)
3958 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3959 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3960 __ LoadConst32(TMP, min_val);
3961 __ Mtc1(TMP, FTMP);
3962 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003963 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003964 uint64_t min_val = (result_type == Primitive::kPrimLong)
3965 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3966 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3967 __ LoadConst64(TMP, min_val);
3968 __ Dmtc1(TMP, FTMP);
3969 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003970 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003971
3972 __ Bc1nez(FTMP, &truncate);
3973
3974 if (input_type == Primitive::kPrimFloat) {
3975 __ CmpEqS(FTMP, src, src);
3976 } else {
3977 __ CmpEqD(FTMP, src, src);
3978 }
3979 if (result_type == Primitive::kPrimLong) {
3980 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3981 } else {
3982 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3983 }
3984 __ Mfc1(TMP, FTMP);
3985 __ And(dst, dst, TMP);
3986
3987 __ Bc(&done);
3988
3989 __ Bind(&truncate);
3990
3991 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003992 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003993 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003994 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003995 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003996 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003997 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003998 } else {
3999 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004000 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004001 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004002 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004003 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004004 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004005 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004006
4007 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004008 } else if (Primitive::IsFloatingPointType(result_type) &&
4009 Primitive::IsFloatingPointType(input_type)) {
4010 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4011 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4012 if (result_type == Primitive::kPrimFloat) {
4013 __ Cvtsd(dst, src);
4014 } else {
4015 __ Cvtds(dst, src);
4016 }
4017 } else {
4018 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4019 << " to " << result_type;
4020 }
4021}
4022
4023void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4024 HandleShift(ushr);
4025}
4026
4027void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4028 HandleShift(ushr);
4029}
4030
4031void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4032 HandleBinaryOp(instruction);
4033}
4034
4035void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4036 HandleBinaryOp(instruction);
4037}
4038
4039void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4040 // Nothing to do, this should be removed during prepare for register allocator.
4041 LOG(FATAL) << "Unreachable";
4042}
4043
4044void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4045 // Nothing to do, this should be removed during prepare for register allocator.
4046 LOG(FATAL) << "Unreachable";
4047}
4048
4049void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004050 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004051}
4052
4053void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004054 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004055}
4056
4057void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004058 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004059}
4060
4061void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004062 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004063}
4064
4065void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004066 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004067}
4068
4069void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004070 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004071}
4072
4073void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004074 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004075}
4076
4077void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004078 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004079}
4080
4081void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004082 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004083}
4084
4085void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004086 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004087}
4088
4089void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004090 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004091}
4092
4093void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004094 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004095}
4096
Aart Bike9f37602015-10-09 11:15:55 -07004097void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004098 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004099}
4100
4101void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004102 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004103}
4104
4105void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004106 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004107}
4108
4109void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004110 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004111}
4112
4113void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004114 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004115}
4116
4117void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004118 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004119}
4120
4121void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004122 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004123}
4124
4125void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004126 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004127}
4128
Mark Mendellfe57faa2015-09-18 09:26:15 -04004129// Simple implementation of packed switch - generate cascaded compare/jumps.
4130void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4131 LocationSummary* locations =
4132 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4133 locations->SetInAt(0, Location::RequiresRegister());
4134}
4135
4136void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4137 int32_t lower_bound = switch_instr->GetStartValue();
4138 int32_t num_entries = switch_instr->GetNumEntries();
4139 LocationSummary* locations = switch_instr->GetLocations();
4140 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4141 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4142
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004143 // Create a set of compare/jumps.
4144 GpuRegister temp_reg = TMP;
4145 if (IsInt<16>(-lower_bound)) {
4146 __ Addiu(temp_reg, value_reg, -lower_bound);
4147 } else {
4148 __ LoadConst32(AT, -lower_bound);
4149 __ Addu(temp_reg, value_reg, AT);
4150 }
4151 // Jump to default if index is negative
4152 // Note: We don't check the case that index is positive while value < lower_bound, because in
4153 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4154 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4155
Mark Mendellfe57faa2015-09-18 09:26:15 -04004156 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004157 // Jump to successors[0] if value == lower_bound.
4158 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4159 int32_t last_index = 0;
4160 for (; num_entries - last_index > 2; last_index += 2) {
4161 __ Addiu(temp_reg, temp_reg, -2);
4162 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4163 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4164 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4165 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4166 }
4167 if (num_entries - last_index == 2) {
4168 // The last missing case_value.
4169 __ Addiu(temp_reg, temp_reg, -1);
4170 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004171 }
4172
4173 // And the default for any other value.
4174 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004175 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004176 }
4177}
4178
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004179void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4180 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4181}
4182
4183void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4184 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4185}
4186
Alexey Frunze4dda3372015-06-01 18:31:49 -07004187} // namespace mips64
4188} // namespace art