blob: 2f9eca6ac3bb31de12df9886ffd55b58cfdac884 [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips64.h"
18
Alexey Frunzec857c742015-09-23 15:12:39 -070019#include "art_method.h"
20#include "code_generator_utils.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070021#include "entrypoints/quick/quick_entrypoints.h"
22#include "entrypoints/quick/quick_entrypoints_enum.h"
23#include "gc/accounting/card_table.h"
24#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070026#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070030#include "utils/assembler.h"
Alexey Frunzea0e87b02015-09-24 22:57:20 -070031#include "utils/mips64/assembler_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070032#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
Alexey Frunze4dda3372015-06-01 18:31:49 -070040Location Mips64ReturnLocation(Primitive::Type return_type) {
41 switch (return_type) {
42 case Primitive::kPrimBoolean:
43 case Primitive::kPrimByte:
44 case Primitive::kPrimChar:
45 case Primitive::kPrimShort:
46 case Primitive::kPrimInt:
47 case Primitive::kPrimNot:
48 case Primitive::kPrimLong:
49 return Location::RegisterLocation(V0);
50
51 case Primitive::kPrimFloat:
52 case Primitive::kPrimDouble:
53 return Location::FpuRegisterLocation(F0);
54
55 case Primitive::kPrimVoid:
56 return Location();
57 }
58 UNREACHABLE();
59}
60
61Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
62 return Mips64ReturnLocation(type);
63}
64
65Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
66 return Location::RegisterLocation(kMethodRegisterArgument);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
70 Location next_location;
71 if (type == Primitive::kPrimVoid) {
72 LOG(FATAL) << "Unexpected parameter type " << type;
73 }
74
75 if (Primitive::IsFloatingPointType(type) &&
76 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
77 next_location = Location::FpuRegisterLocation(
78 calling_convention.GetFpuRegisterAt(float_index_++));
79 gp_index_++;
80 } else if (!Primitive::IsFloatingPointType(type) &&
81 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
82 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
83 float_index_++;
84 } else {
85 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
86 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
87 : Location::StackSlot(stack_offset);
88 }
89
90 // Space on the stack is reserved for all arguments.
91 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
92
93 // TODO: review
94
95 // TODO: shouldn't we use a whole machine word per argument on the stack?
96 // Implicit 4-byte method pointer (and such) will cause misalignment.
97
98 return next_location;
99}
100
101Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
102 return Mips64ReturnLocation(type);
103}
104
105#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
Lazar Trsicd9672662015-09-03 17:33:01 +0200106#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700107
108class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
109 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000110 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700111
112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
115 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000116 if (instruction_->CanThrowIntoCatchBlock()) {
117 // Live registers will be restored in the catch block if caught.
118 SaveLiveRegisters(codegen, instruction_->GetLocations());
119 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700120 // We're moving two locations to locations that could overlap, so we need a parallel
121 // move resolver.
122 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100123 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700124 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
125 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
128 Primitive::kPrimInt);
129 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
130 instruction_,
131 instruction_->GetDexPc(),
132 this);
133 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
134 }
135
Alexandre Rames8158f282015-08-07 10:26:17 +0100136 bool IsFatal() const OVERRIDE { return true; }
137
Roland Levillain46648892015-06-19 16:07:18 +0100138 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
139
Alexey Frunze4dda3372015-06-01 18:31:49 -0700140 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700141 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
142};
143
144class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
145 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000146 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700147
148 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
149 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
150 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000151 if (instruction_->CanThrowIntoCatchBlock()) {
152 // Live registers will be restored in the catch block if caught.
153 SaveLiveRegisters(codegen, instruction_->GetLocations());
154 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700155 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
156 instruction_,
157 instruction_->GetDexPc(),
158 this);
159 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Roland Levillain46648892015-06-19 16:07:18 +0100164 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
165
Alexey Frunze4dda3372015-06-01 18:31:49 -0700166 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700167 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
168};
169
170class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
171 public:
172 LoadClassSlowPathMIPS64(HLoadClass* cls,
173 HInstruction* at,
174 uint32_t dex_pc,
175 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000176 : SlowPathCodeMIPS64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700177 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
178 }
179
180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
181 LocationSummary* locations = at_->GetLocations();
182 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
183
184 __ Bind(GetEntryLabel());
185 SaveLiveRegisters(codegen, locations);
186
187 InvokeRuntimeCallingConvention calling_convention;
188 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
189 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
190 : QUICK_ENTRY_POINT(pInitializeType);
191 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
192 if (do_clinit_) {
193 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
194 } else {
195 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
196 }
197
198 // Move the class to the desired location.
199 Location out = locations->Out();
200 if (out.IsValid()) {
201 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
202 Primitive::Type type = at_->GetType();
203 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
204 }
205
206 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700207 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700208 }
209
Roland Levillain46648892015-06-19 16:07:18 +0100210 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
211
Alexey Frunze4dda3372015-06-01 18:31:49 -0700212 private:
213 // The class this slow path will load.
214 HLoadClass* const cls_;
215
216 // The instruction where this slow path is happening.
217 // (Might be the load class or an initialization check).
218 HInstruction* const at_;
219
220 // The dex PC of `at_`.
221 const uint32_t dex_pc_;
222
223 // Whether to initialize the class.
224 const bool do_clinit_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
227};
228
229class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
230 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000231 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700232
233 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
234 LocationSummary* locations = instruction_->GetLocations();
235 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
236 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
237
238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
241 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000242 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
243 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700244 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
245 instruction_,
246 instruction_->GetDexPc(),
247 this);
248 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
249 Primitive::Type type = instruction_->GetType();
250 mips64_codegen->MoveLocation(locations->Out(),
251 calling_convention.GetReturnLocation(type),
252 type);
253
254 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700255 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700256 }
257
Roland Levillain46648892015-06-19 16:07:18 +0100258 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
259
Alexey Frunze4dda3372015-06-01 18:31:49 -0700260 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700261 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
262};
263
264class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
265 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000266 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : SlowPathCodeMIPS64(instr) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267
268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
270 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000271 if (instruction_->CanThrowIntoCatchBlock()) {
272 // Live registers will be restored in the catch block if caught.
273 SaveLiveRegisters(codegen, instruction_->GetLocations());
274 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700275 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
276 instruction_,
277 instruction_->GetDexPc(),
278 this);
279 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Roland Levillain46648892015-06-19 16:07:18 +0100284 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
285
Alexey Frunze4dda3372015-06-01 18:31:49 -0700286 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700287 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
288};
289
290class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
291 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100292 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000293 : SlowPathCodeMIPS64(instruction), successor_(successor) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700294
295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
297 __ Bind(GetEntryLabel());
298 SaveLiveRegisters(codegen, instruction_->GetLocations());
299 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
300 instruction_,
301 instruction_->GetDexPc(),
302 this);
303 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
304 RestoreLiveRegisters(codegen, instruction_->GetLocations());
305 if (successor_ == nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700306 __ Bc(GetReturnLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700307 } else {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700308 __ Bc(mips64_codegen->GetLabelOf(successor_));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700309 }
310 }
311
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700312 Mips64Label* GetReturnLabel() {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700313 DCHECK(successor_ == nullptr);
314 return &return_label_;
315 }
316
Roland Levillain46648892015-06-19 16:07:18 +0100317 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
318
Alexey Frunze4dda3372015-06-01 18:31:49 -0700319 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700320 // If not null, the block to branch to after the suspend check.
321 HBasicBlock* const successor_;
322
323 // If `successor_` is null, the label to branch to after the suspend check.
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700324 Mips64Label return_label_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700325
326 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
327};
328
329class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
330 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000331 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700332
333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
334 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
339 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
340
341 __ Bind(GetEntryLabel());
342 SaveLiveRegisters(codegen, locations);
343
344 // We're moving two locations to locations that could overlap, so we need a parallel
345 // move resolver.
346 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
349 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352 Primitive::kPrimNot);
353
354 if (instruction_->IsInstanceOf()) {
355 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
356 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100357 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700358 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000359 CheckEntrypointTypes<
360 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Primitive::Type ret_type = instruction_->GetType();
362 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
363 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 } else {
365 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100366 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700367 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
368 }
369
370 RestoreLiveRegisters(codegen, locations);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700371 __ Bc(GetExitLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700372 }
373
Roland Levillain46648892015-06-19 16:07:18 +0100374 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
375
Alexey Frunze4dda3372015-06-01 18:31:49 -0700376 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700377 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
378};
379
380class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
381 public:
Aart Bik42249c32016-01-07 15:33:50 -0800382 explicit DeoptimizationSlowPathMIPS64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000383 : SlowPathCodeMIPS64(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700384
385 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800386 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800389 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
390 instruction_,
391 instruction_->GetDexPc(),
392 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000393 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700394 }
395
Roland Levillain46648892015-06-19 16:07:18 +0100396 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
397
Alexey Frunze4dda3372015-06-01 18:31:49 -0700398 private:
Alexey Frunze4dda3372015-06-01 18:31:49 -0700399 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
400};
401
402CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
403 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100404 const CompilerOptions& compiler_options,
405 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700406 : CodeGenerator(graph,
407 kNumberOfGpuRegisters,
408 kNumberOfFpuRegisters,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000409 /* number_of_register_pairs */ 0,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700410 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
411 arraysize(kCoreCalleeSaves)),
412 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
413 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100414 compiler_options,
415 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100416 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700417 location_builder_(graph, this),
418 instruction_visitor_(graph, this),
419 move_resolver_(graph->GetArena(), this),
420 isa_features_(isa_features) {
421 // Save RA (containing the return address) to mimic Quick.
422 AddAllocatedRegister(Location::RegisterLocation(RA));
423}
424
425#undef __
426#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200427#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700428
429void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700430 // Ensure that we fix up branches.
431 __ FinalizeCode();
432
433 // Adjust native pc offsets in stack maps.
434 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
435 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
436 uint32_t new_position = __ GetAdjustedPosition(old_position);
437 DCHECK_GE(new_position, old_position);
438 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
439 }
440
441 // Adjust pc offsets for the disassembly information.
442 if (disasm_info_ != nullptr) {
443 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
444 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
445 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
446 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
447 it.second.start = __ GetAdjustedPosition(it.second.start);
448 it.second.end = __ GetAdjustedPosition(it.second.end);
449 }
450 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
451 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
452 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
453 }
454 }
455
Alexey Frunze4dda3372015-06-01 18:31:49 -0700456 CodeGenerator::Finalize(allocator);
457}
458
459Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
460 return codegen_->GetAssembler();
461}
462
463void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100464 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700465 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
466}
467
468void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100469 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700470 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
471}
472
473void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
474 // Pop reg
475 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200476 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700477}
478
479void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
480 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200481 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700482 __ Sd(GpuRegister(reg), SP, 0);
483}
484
485void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
486 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
487 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
488 // Allocate a scratch register other than TMP, if available.
489 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
490 // automatically unspilled when the scratch scope object is destroyed).
491 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
492 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200493 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700494 __ LoadFromOffset(load_type,
495 GpuRegister(ensure_scratch.GetRegister()),
496 SP,
497 index1 + stack_offset);
498 __ LoadFromOffset(load_type,
499 TMP,
500 SP,
501 index2 + stack_offset);
502 __ StoreToOffset(store_type,
503 GpuRegister(ensure_scratch.GetRegister()),
504 SP,
505 index2 + stack_offset);
506 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
507}
508
509static dwarf::Reg DWARFReg(GpuRegister reg) {
510 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
511}
512
David Srbeckyba702002016-02-01 18:15:29 +0000513static dwarf::Reg DWARFReg(FpuRegister reg) {
514 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
515}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700516
517void CodeGeneratorMIPS64::GenerateFrameEntry() {
518 __ Bind(&frame_entry_label_);
519
520 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
521
522 if (do_overflow_check) {
523 __ LoadFromOffset(kLoadWord,
524 ZERO,
525 SP,
526 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
527 RecordPcInfo(nullptr, 0);
528 }
529
530 // TODO: anything related to T9/GP/GOT/PIC/.so's?
531
532 if (HasEmptyFrame()) {
533 return;
534 }
535
536 // Make sure the frame size isn't unreasonably large. Per the various APIs
537 // it looks like it should always be less than 2GB in size, which allows
538 // us using 32-bit signed offsets from the stack pointer.
539 if (GetFrameSize() > 0x7FFFFFFF)
540 LOG(FATAL) << "Stack frame larger than 2GB";
541
542 // Spill callee-saved registers.
543 // Note that their cumulative size is small and they can be indexed using
544 // 16-bit offsets.
545
546 // TODO: increment/decrement SP in one step instead of two or remove this comment.
547
548 uint32_t ofs = FrameEntrySpillSize();
549 __ IncreaseFrameSize(ofs);
550
551 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
552 GpuRegister reg = kCoreCalleeSaves[i];
553 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200554 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700555 __ Sd(reg, SP, ofs);
556 __ cfi().RelOffset(DWARFReg(reg), ofs);
557 }
558 }
559
560 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
561 FpuRegister reg = kFpuCalleeSaves[i];
562 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200563 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700564 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000565 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700566 }
567 }
568
569 // Allocate the rest of the frame and store the current method pointer
570 // at its end.
571
572 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
573
574 static_assert(IsInt<16>(kCurrentMethodStackOffset),
575 "kCurrentMethodStackOffset must fit into int16_t");
576 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
577}
578
579void CodeGeneratorMIPS64::GenerateFrameExit() {
580 __ cfi().RememberState();
581
582 // TODO: anything related to T9/GP/GOT/PIC/.so's?
583
584 if (!HasEmptyFrame()) {
585 // Deallocate the rest of the frame.
586
587 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
588
589 // Restore callee-saved registers.
590 // Note that their cumulative size is small and they can be indexed using
591 // 16-bit offsets.
592
593 // TODO: increment/decrement SP in one step instead of two or remove this comment.
594
595 uint32_t ofs = 0;
596
597 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
598 FpuRegister reg = kFpuCalleeSaves[i];
599 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
600 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200601 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000602 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700603 }
604 }
605
606 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
607 GpuRegister reg = kCoreCalleeSaves[i];
608 if (allocated_registers_.ContainsCoreRegister(reg)) {
609 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200610 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700611 __ cfi().Restore(DWARFReg(reg));
612 }
613 }
614
615 DCHECK_EQ(ofs, FrameEntrySpillSize());
616 __ DecreaseFrameSize(ofs);
617 }
618
619 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700620 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700621
622 __ cfi().RestoreState();
623 __ cfi().DefCFAOffset(GetFrameSize());
624}
625
626void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
627 __ Bind(GetLabelOf(block));
628}
629
630void CodeGeneratorMIPS64::MoveLocation(Location destination,
631 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100632 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700633 if (source.Equals(destination)) {
634 return;
635 }
636
637 // A valid move can always be inferred from the destination and source
638 // locations. When moving from and to a register, the argument type can be
639 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100640 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700641 DCHECK_EQ(unspecified_type, false);
642
643 if (destination.IsRegister() || destination.IsFpuRegister()) {
644 if (unspecified_type) {
645 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
646 if (source.IsStackSlot() ||
647 (src_cst != nullptr && (src_cst->IsIntConstant()
648 || src_cst->IsFloatConstant()
649 || src_cst->IsNullConstant()))) {
650 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 } else {
653 // If the source is a double stack slot or a 64bit constant, a 64bit
654 // type is appropriate. Else the source is a register, and since the
655 // type has not been specified, we chose a 64bit type to force a 64bit
656 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100657 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700658 }
659 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100660 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
661 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700662 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
663 // Move to GPR/FPR from stack
664 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100665 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700666 __ LoadFpuFromOffset(load_type,
667 destination.AsFpuRegister<FpuRegister>(),
668 SP,
669 source.GetStackIndex());
670 } else {
671 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
672 __ LoadFromOffset(load_type,
673 destination.AsRegister<GpuRegister>(),
674 SP,
675 source.GetStackIndex());
676 }
677 } else if (source.IsConstant()) {
678 // Move to GPR/FPR from constant
679 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100680 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700681 gpr = destination.AsRegister<GpuRegister>();
682 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100683 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700684 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
685 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
686 gpr = ZERO;
687 } else {
688 __ LoadConst32(gpr, value);
689 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700690 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700691 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
692 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
693 gpr = ZERO;
694 } else {
695 __ LoadConst64(gpr, value);
696 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700697 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100698 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700699 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100700 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700701 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
702 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100703 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700704 if (destination.IsRegister()) {
705 // Move to GPR from GPR
706 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
707 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100708 DCHECK(destination.IsFpuRegister());
709 if (Primitive::Is64BitType(dst_type)) {
710 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
711 } else {
712 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
713 }
714 }
715 } else if (source.IsFpuRegister()) {
716 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100718 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700719 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
720 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100721 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700722 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
723 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100724 } else {
725 DCHECK(destination.IsRegister());
726 if (Primitive::Is64BitType(dst_type)) {
727 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
728 } else {
729 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
730 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700731 }
732 }
733 } else { // The destination is not a register. It must be a stack slot.
734 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
735 if (source.IsRegister() || source.IsFpuRegister()) {
736 if (unspecified_type) {
737 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100738 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700739 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100740 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700741 }
742 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100743 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
744 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700745 // Move to stack from GPR/FPR
746 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
747 if (source.IsRegister()) {
748 __ StoreToOffset(store_type,
749 source.AsRegister<GpuRegister>(),
750 SP,
751 destination.GetStackIndex());
752 } else {
753 __ StoreFpuToOffset(store_type,
754 source.AsFpuRegister<FpuRegister>(),
755 SP,
756 destination.GetStackIndex());
757 }
758 } else if (source.IsConstant()) {
759 // Move to stack from constant
760 HConstant* src_cst = source.GetConstant();
761 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700762 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700763 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700764 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
765 if (value != 0) {
766 gpr = TMP;
767 __ LoadConst32(gpr, value);
768 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700769 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700770 DCHECK(destination.IsDoubleStackSlot());
771 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
772 if (value != 0) {
773 gpr = TMP;
774 __ LoadConst64(gpr, value);
775 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700776 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700777 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700778 } else {
779 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
780 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
781 // Move to stack from stack
782 if (destination.IsStackSlot()) {
783 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
784 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
785 } else {
786 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
787 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
788 }
789 }
790 }
791}
792
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700793void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700794 DCHECK(!loc1.IsConstant());
795 DCHECK(!loc2.IsConstant());
796
797 if (loc1.Equals(loc2)) {
798 return;
799 }
800
801 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
802 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
803 bool is_fp_reg1 = loc1.IsFpuRegister();
804 bool is_fp_reg2 = loc2.IsFpuRegister();
805
806 if (loc2.IsRegister() && loc1.IsRegister()) {
807 // Swap 2 GPRs
808 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
809 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
810 __ Move(TMP, r2);
811 __ Move(r2, r1);
812 __ Move(r1, TMP);
813 } else if (is_fp_reg2 && is_fp_reg1) {
814 // Swap 2 FPRs
815 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
816 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700817 if (type == Primitive::kPrimFloat) {
818 __ MovS(FTMP, r1);
819 __ MovS(r1, r2);
820 __ MovS(r2, FTMP);
821 } else {
822 DCHECK_EQ(type, Primitive::kPrimDouble);
823 __ MovD(FTMP, r1);
824 __ MovD(r1, r2);
825 __ MovD(r2, FTMP);
826 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700827 } else if (is_slot1 != is_slot2) {
828 // Swap GPR/FPR and stack slot
829 Location reg_loc = is_slot1 ? loc2 : loc1;
830 Location mem_loc = is_slot1 ? loc1 : loc2;
831 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
832 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
833 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
834 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
835 if (reg_loc.IsFpuRegister()) {
836 __ StoreFpuToOffset(store_type,
837 reg_loc.AsFpuRegister<FpuRegister>(),
838 SP,
839 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700840 if (mem_loc.IsStackSlot()) {
841 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
842 } else {
843 DCHECK(mem_loc.IsDoubleStackSlot());
844 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
845 }
846 } else {
847 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
848 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
849 }
850 } else if (is_slot1 && is_slot2) {
851 move_resolver_.Exchange(loc1.GetStackIndex(),
852 loc2.GetStackIndex(),
853 loc1.IsDoubleStackSlot());
854 } else {
855 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
856 }
857}
858
Calin Juravle175dc732015-08-25 15:42:32 +0100859void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
860 DCHECK(location.IsRegister());
861 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
862}
863
Calin Juravlee460d1d2015-09-29 04:52:17 +0100864void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
865 if (location.IsRegister()) {
866 locations->AddTemp(location);
867 } else {
868 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
869 }
870}
871
Alexey Frunze4dda3372015-06-01 18:31:49 -0700872Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
873 Primitive::Type type = load->GetType();
874
875 switch (type) {
876 case Primitive::kPrimNot:
877 case Primitive::kPrimInt:
878 case Primitive::kPrimFloat:
879 return Location::StackSlot(GetStackSlot(load->GetLocal()));
880
881 case Primitive::kPrimLong:
882 case Primitive::kPrimDouble:
883 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
884
885 case Primitive::kPrimBoolean:
886 case Primitive::kPrimByte:
887 case Primitive::kPrimChar:
888 case Primitive::kPrimShort:
889 case Primitive::kPrimVoid:
890 LOG(FATAL) << "Unexpected type " << type;
891 }
892
893 LOG(FATAL) << "Unreachable";
894 return Location::NoLocation();
895}
896
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100897void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
898 GpuRegister value,
899 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700900 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700901 GpuRegister card = AT;
902 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100903 if (value_can_be_null) {
904 __ Beqzc(value, &done);
905 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700906 __ LoadFromOffset(kLoadDoubleword,
907 card,
908 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200909 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700910 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
911 __ Daddu(temp, card, temp);
912 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100913 if (value_can_be_null) {
914 __ Bind(&done);
915 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700916}
917
David Brazdil58282f42016-01-14 12:45:10 +0000918void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700919 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
920 blocked_core_registers_[ZERO] = true;
921 blocked_core_registers_[K0] = true;
922 blocked_core_registers_[K1] = true;
923 blocked_core_registers_[GP] = true;
924 blocked_core_registers_[SP] = true;
925 blocked_core_registers_[RA] = true;
926
Lazar Trsicd9672662015-09-03 17:33:01 +0200927 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
928 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700929 blocked_core_registers_[AT] = true;
930 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200931 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932 blocked_fpu_registers_[FTMP] = true;
933
934 // Reserve suspend and thread registers.
935 blocked_core_registers_[S0] = true;
936 blocked_core_registers_[TR] = true;
937
938 // Reserve T9 for function calls
939 blocked_core_registers_[T9] = true;
940
941 // TODO: review; anything else?
942
David Brazdil58282f42016-01-14 12:45:10 +0000943 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
945 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
946 }
947
948 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
949 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
950 }
951}
952
Alexey Frunze4dda3372015-06-01 18:31:49 -0700953size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
954 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200955 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700956}
957
958size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
959 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200960 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700961}
962
963size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
964 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200965 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700966}
967
968size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
969 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200970 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700971}
972
973void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100974 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700975}
976
977void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100978 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700979}
980
Calin Juravle175dc732015-08-25 15:42:32 +0100981void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
982 HInstruction* instruction,
983 uint32_t dex_pc,
984 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200985 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100986 instruction,
987 dex_pc,
988 slow_path);
989}
990
Alexey Frunze4dda3372015-06-01 18:31:49 -0700991void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
992 HInstruction* instruction,
993 uint32_t dex_pc,
994 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100995 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700996 // TODO: anything related to T9/GP/GOT/PIC/.so's?
997 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
998 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700999 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001000 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001}
1002
1003void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1004 GpuRegister class_reg) {
1005 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1006 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1007 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1008 // TODO: barrier needed?
1009 __ Bind(slow_path->GetExitLabel());
1010}
1011
1012void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1013 __ Sync(0); // only stype 0 is supported
1014}
1015
1016void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1017 HBasicBlock* successor) {
1018 SuspendCheckSlowPathMIPS64* slow_path =
1019 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1020 codegen_->AddSlowPath(slow_path);
1021
1022 __ LoadFromOffset(kLoadUnsignedHalfword,
1023 TMP,
1024 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001025 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001026 if (successor == nullptr) {
1027 __ Bnezc(TMP, slow_path->GetEntryLabel());
1028 __ Bind(slow_path->GetReturnLabel());
1029 } else {
1030 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001031 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001032 // slow_path will return to GetLabelOf(successor).
1033 }
1034}
1035
1036InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1037 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001038 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001039 assembler_(codegen->GetAssembler()),
1040 codegen_(codegen) {}
1041
1042void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1043 DCHECK_EQ(instruction->InputCount(), 2U);
1044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1045 Primitive::Type type = instruction->GetResultType();
1046 switch (type) {
1047 case Primitive::kPrimInt:
1048 case Primitive::kPrimLong: {
1049 locations->SetInAt(0, Location::RequiresRegister());
1050 HInstruction* right = instruction->InputAt(1);
1051 bool can_use_imm = false;
1052 if (right->IsConstant()) {
1053 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1054 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1055 can_use_imm = IsUint<16>(imm);
1056 } else if (instruction->IsAdd()) {
1057 can_use_imm = IsInt<16>(imm);
1058 } else {
1059 DCHECK(instruction->IsSub());
1060 can_use_imm = IsInt<16>(-imm);
1061 }
1062 }
1063 if (can_use_imm)
1064 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1065 else
1066 locations->SetInAt(1, Location::RequiresRegister());
1067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1068 }
1069 break;
1070
1071 case Primitive::kPrimFloat:
1072 case Primitive::kPrimDouble:
1073 locations->SetInAt(0, Location::RequiresFpuRegister());
1074 locations->SetInAt(1, Location::RequiresFpuRegister());
1075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1076 break;
1077
1078 default:
1079 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1080 }
1081}
1082
1083void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1084 Primitive::Type type = instruction->GetType();
1085 LocationSummary* locations = instruction->GetLocations();
1086
1087 switch (type) {
1088 case Primitive::kPrimInt:
1089 case Primitive::kPrimLong: {
1090 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1091 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1092 Location rhs_location = locations->InAt(1);
1093
1094 GpuRegister rhs_reg = ZERO;
1095 int64_t rhs_imm = 0;
1096 bool use_imm = rhs_location.IsConstant();
1097 if (use_imm) {
1098 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1099 } else {
1100 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1101 }
1102
1103 if (instruction->IsAnd()) {
1104 if (use_imm)
1105 __ Andi(dst, lhs, rhs_imm);
1106 else
1107 __ And(dst, lhs, rhs_reg);
1108 } else if (instruction->IsOr()) {
1109 if (use_imm)
1110 __ Ori(dst, lhs, rhs_imm);
1111 else
1112 __ Or(dst, lhs, rhs_reg);
1113 } else if (instruction->IsXor()) {
1114 if (use_imm)
1115 __ Xori(dst, lhs, rhs_imm);
1116 else
1117 __ Xor(dst, lhs, rhs_reg);
1118 } else if (instruction->IsAdd()) {
1119 if (type == Primitive::kPrimInt) {
1120 if (use_imm)
1121 __ Addiu(dst, lhs, rhs_imm);
1122 else
1123 __ Addu(dst, lhs, rhs_reg);
1124 } else {
1125 if (use_imm)
1126 __ Daddiu(dst, lhs, rhs_imm);
1127 else
1128 __ Daddu(dst, lhs, rhs_reg);
1129 }
1130 } else {
1131 DCHECK(instruction->IsSub());
1132 if (type == Primitive::kPrimInt) {
1133 if (use_imm)
1134 __ Addiu(dst, lhs, -rhs_imm);
1135 else
1136 __ Subu(dst, lhs, rhs_reg);
1137 } else {
1138 if (use_imm)
1139 __ Daddiu(dst, lhs, -rhs_imm);
1140 else
1141 __ Dsubu(dst, lhs, rhs_reg);
1142 }
1143 }
1144 break;
1145 }
1146 case Primitive::kPrimFloat:
1147 case Primitive::kPrimDouble: {
1148 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1149 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1150 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1151 if (instruction->IsAdd()) {
1152 if (type == Primitive::kPrimFloat)
1153 __ AddS(dst, lhs, rhs);
1154 else
1155 __ AddD(dst, lhs, rhs);
1156 } else if (instruction->IsSub()) {
1157 if (type == Primitive::kPrimFloat)
1158 __ SubS(dst, lhs, rhs);
1159 else
1160 __ SubD(dst, lhs, rhs);
1161 } else {
1162 LOG(FATAL) << "Unexpected floating-point binary operation";
1163 }
1164 break;
1165 }
1166 default:
1167 LOG(FATAL) << "Unexpected binary operation type " << type;
1168 }
1169}
1170
1171void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001172 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001173
1174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1175 Primitive::Type type = instr->GetResultType();
1176 switch (type) {
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimLong: {
1179 locations->SetInAt(0, Location::RequiresRegister());
1180 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001182 break;
1183 }
1184 default:
1185 LOG(FATAL) << "Unexpected shift type " << type;
1186 }
1187}
1188
1189void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001190 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001191 LocationSummary* locations = instr->GetLocations();
1192 Primitive::Type type = instr->GetType();
1193
1194 switch (type) {
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimLong: {
1197 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1198 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1199 Location rhs_location = locations->InAt(1);
1200
1201 GpuRegister rhs_reg = ZERO;
1202 int64_t rhs_imm = 0;
1203 bool use_imm = rhs_location.IsConstant();
1204 if (use_imm) {
1205 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1206 } else {
1207 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1208 }
1209
1210 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001211 uint32_t shift_value = rhs_imm &
1212 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001213
Alexey Frunze92d90602015-12-18 18:16:36 -08001214 if (shift_value == 0) {
1215 if (dst != lhs) {
1216 __ Move(dst, lhs);
1217 }
1218 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001219 if (instr->IsShl()) {
1220 __ Sll(dst, lhs, shift_value);
1221 } else if (instr->IsShr()) {
1222 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001223 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001224 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001225 } else {
1226 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001227 }
1228 } else {
1229 if (shift_value < 32) {
1230 if (instr->IsShl()) {
1231 __ Dsll(dst, lhs, shift_value);
1232 } else if (instr->IsShr()) {
1233 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001234 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001236 } else {
1237 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 }
1239 } else {
1240 shift_value -= 32;
1241 if (instr->IsShl()) {
1242 __ Dsll32(dst, lhs, shift_value);
1243 } else if (instr->IsShr()) {
1244 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001245 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001246 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001247 } else {
1248 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001249 }
1250 }
1251 }
1252 } else {
1253 if (type == Primitive::kPrimInt) {
1254 if (instr->IsShl()) {
1255 __ Sllv(dst, lhs, rhs_reg);
1256 } else if (instr->IsShr()) {
1257 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001258 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001259 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001260 } else {
1261 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001262 }
1263 } else {
1264 if (instr->IsShl()) {
1265 __ Dsllv(dst, lhs, rhs_reg);
1266 } else if (instr->IsShr()) {
1267 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001268 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001269 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001270 } else {
1271 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001272 }
1273 }
1274 }
1275 break;
1276 }
1277 default:
1278 LOG(FATAL) << "Unexpected shift operation type " << type;
1279 }
1280}
1281
1282void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1283 HandleBinaryOp(instruction);
1284}
1285
1286void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1287 HandleBinaryOp(instruction);
1288}
1289
1290void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1291 HandleBinaryOp(instruction);
1292}
1293
1294void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1295 HandleBinaryOp(instruction);
1296}
1297
1298void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1299 LocationSummary* locations =
1300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1301 locations->SetInAt(0, Location::RequiresRegister());
1302 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1303 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1304 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1305 } else {
1306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1307 }
1308}
1309
1310void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1311 LocationSummary* locations = instruction->GetLocations();
1312 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1313 Location index = locations->InAt(1);
1314 Primitive::Type type = instruction->GetType();
1315
1316 switch (type) {
1317 case Primitive::kPrimBoolean: {
1318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1319 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1320 if (index.IsConstant()) {
1321 size_t offset =
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1323 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1324 } else {
1325 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1326 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1327 }
1328 break;
1329 }
1330
1331 case Primitive::kPrimByte: {
1332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1333 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1334 if (index.IsConstant()) {
1335 size_t offset =
1336 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1337 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1338 } else {
1339 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1340 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1341 }
1342 break;
1343 }
1344
1345 case Primitive::kPrimShort: {
1346 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1347 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1348 if (index.IsConstant()) {
1349 size_t offset =
1350 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1351 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1352 } else {
1353 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1354 __ Daddu(TMP, obj, TMP);
1355 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1356 }
1357 break;
1358 }
1359
1360 case Primitive::kPrimChar: {
1361 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1362 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1363 if (index.IsConstant()) {
1364 size_t offset =
1365 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1366 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1367 } else {
1368 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1369 __ Daddu(TMP, obj, TMP);
1370 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1371 }
1372 break;
1373 }
1374
1375 case Primitive::kPrimInt:
1376 case Primitive::kPrimNot: {
1377 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1378 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1379 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1380 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1381 if (index.IsConstant()) {
1382 size_t offset =
1383 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1384 __ LoadFromOffset(load_type, out, obj, offset);
1385 } else {
1386 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1387 __ Daddu(TMP, obj, TMP);
1388 __ LoadFromOffset(load_type, out, TMP, data_offset);
1389 }
1390 break;
1391 }
1392
1393 case Primitive::kPrimLong: {
1394 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1395 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1396 if (index.IsConstant()) {
1397 size_t offset =
1398 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1399 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1400 } else {
1401 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1402 __ Daddu(TMP, obj, TMP);
1403 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1404 }
1405 break;
1406 }
1407
1408 case Primitive::kPrimFloat: {
1409 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1410 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1411 if (index.IsConstant()) {
1412 size_t offset =
1413 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1414 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1415 } else {
1416 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1417 __ Daddu(TMP, obj, TMP);
1418 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1419 }
1420 break;
1421 }
1422
1423 case Primitive::kPrimDouble: {
1424 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1425 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1426 if (index.IsConstant()) {
1427 size_t offset =
1428 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1429 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1430 } else {
1431 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1432 __ Daddu(TMP, obj, TMP);
1433 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1434 }
1435 break;
1436 }
1437
1438 case Primitive::kPrimVoid:
1439 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1440 UNREACHABLE();
1441 }
1442 codegen_->MaybeRecordImplicitNullCheck(instruction);
1443}
1444
1445void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1446 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1447 locations->SetInAt(0, Location::RequiresRegister());
1448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1449}
1450
1451void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1452 LocationSummary* locations = instruction->GetLocations();
1453 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1454 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1455 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1456 __ LoadFromOffset(kLoadWord, out, obj, offset);
1457 codegen_->MaybeRecordImplicitNullCheck(instruction);
1458}
1459
1460void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001461 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1463 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001464 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1465 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001466 InvokeRuntimeCallingConvention calling_convention;
1467 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1468 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1469 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1470 } else {
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1473 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1474 locations->SetInAt(2, Location::RequiresFpuRegister());
1475 } else {
1476 locations->SetInAt(2, Location::RequiresRegister());
1477 }
1478 }
1479}
1480
1481void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1482 LocationSummary* locations = instruction->GetLocations();
1483 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1484 Location index = locations->InAt(1);
1485 Primitive::Type value_type = instruction->GetComponentType();
1486 bool needs_runtime_call = locations->WillCall();
1487 bool needs_write_barrier =
1488 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1489
1490 switch (value_type) {
1491 case Primitive::kPrimBoolean:
1492 case Primitive::kPrimByte: {
1493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1494 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1495 if (index.IsConstant()) {
1496 size_t offset =
1497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1498 __ StoreToOffset(kStoreByte, value, obj, offset);
1499 } else {
1500 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1501 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1502 }
1503 break;
1504 }
1505
1506 case Primitive::kPrimShort:
1507 case Primitive::kPrimChar: {
1508 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1509 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1510 if (index.IsConstant()) {
1511 size_t offset =
1512 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1513 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1514 } else {
1515 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1516 __ Daddu(TMP, obj, TMP);
1517 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1518 }
1519 break;
1520 }
1521
1522 case Primitive::kPrimInt:
1523 case Primitive::kPrimNot: {
1524 if (!needs_runtime_call) {
1525 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1526 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1527 if (index.IsConstant()) {
1528 size_t offset =
1529 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1530 __ StoreToOffset(kStoreWord, value, obj, offset);
1531 } else {
1532 DCHECK(index.IsRegister()) << index;
1533 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1534 __ Daddu(TMP, obj, TMP);
1535 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1536 }
1537 codegen_->MaybeRecordImplicitNullCheck(instruction);
1538 if (needs_write_barrier) {
1539 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001540 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001541 }
1542 } else {
1543 DCHECK_EQ(value_type, Primitive::kPrimNot);
1544 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1545 instruction,
1546 instruction->GetDexPc(),
1547 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001548 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001549 }
1550 break;
1551 }
1552
1553 case Primitive::kPrimLong: {
1554 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1555 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1556 if (index.IsConstant()) {
1557 size_t offset =
1558 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1559 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1560 } else {
1561 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1562 __ Daddu(TMP, obj, TMP);
1563 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1564 }
1565 break;
1566 }
1567
1568 case Primitive::kPrimFloat: {
1569 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1570 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1571 DCHECK(locations->InAt(2).IsFpuRegister());
1572 if (index.IsConstant()) {
1573 size_t offset =
1574 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1575 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1576 } else {
1577 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1578 __ Daddu(TMP, obj, TMP);
1579 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1580 }
1581 break;
1582 }
1583
1584 case Primitive::kPrimDouble: {
1585 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1586 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1587 DCHECK(locations->InAt(2).IsFpuRegister());
1588 if (index.IsConstant()) {
1589 size_t offset =
1590 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1591 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1592 } else {
1593 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1594 __ Daddu(TMP, obj, TMP);
1595 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1596 }
1597 break;
1598 }
1599
1600 case Primitive::kPrimVoid:
1601 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1602 UNREACHABLE();
1603 }
1604
1605 // Ints and objects are handled in the switch.
1606 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1607 codegen_->MaybeRecordImplicitNullCheck(instruction);
1608 }
1609}
1610
1611void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001612 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1613 ? LocationSummary::kCallOnSlowPath
1614 : LocationSummary::kNoCall;
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001616 locations->SetInAt(0, Location::RequiresRegister());
1617 locations->SetInAt(1, Location::RequiresRegister());
1618 if (instruction->HasUses()) {
1619 locations->SetOut(Location::SameAsFirstInput());
1620 }
1621}
1622
1623void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1624 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001625 BoundsCheckSlowPathMIPS64* slow_path =
1626 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001627 codegen_->AddSlowPath(slow_path);
1628
1629 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1630 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1631
1632 // length is limited by the maximum positive signed 32-bit integer.
1633 // Unsigned comparison of length and index checks for index < 0
1634 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001635 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001636}
1637
1638void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1640 instruction,
1641 LocationSummary::kCallOnSlowPath);
1642 locations->SetInAt(0, Location::RequiresRegister());
1643 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001644 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001645 locations->AddTemp(Location::RequiresRegister());
1646}
1647
1648void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1649 LocationSummary* locations = instruction->GetLocations();
1650 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1651 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1652 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1653
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001654 SlowPathCodeMIPS64* slow_path =
1655 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001656 codegen_->AddSlowPath(slow_path);
1657
1658 // TODO: avoid this check if we know obj is not null.
1659 __ Beqzc(obj, slow_path->GetExitLabel());
1660 // Compare the class of `obj` with `cls`.
1661 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1662 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1663 __ Bind(slow_path->GetExitLabel());
1664}
1665
1666void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1667 LocationSummary* locations =
1668 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1669 locations->SetInAt(0, Location::RequiresRegister());
1670 if (check->HasUses()) {
1671 locations->SetOut(Location::SameAsFirstInput());
1672 }
1673}
1674
1675void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1676 // We assume the class is not null.
1677 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1678 check->GetLoadClass(),
1679 check,
1680 check->GetDexPc(),
1681 true);
1682 codegen_->AddSlowPath(slow_path);
1683 GenerateClassInitializationCheck(slow_path,
1684 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1685}
1686
1687void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1688 Primitive::Type in_type = compare->InputAt(0)->GetType();
1689
Alexey Frunze299a9392015-12-08 16:08:02 -08001690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001691
1692 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001693 case Primitive::kPrimBoolean:
1694 case Primitive::kPrimByte:
1695 case Primitive::kPrimShort:
1696 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001697 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001698 case Primitive::kPrimLong:
1699 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001700 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1702 break;
1703
1704 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001705 case Primitive::kPrimDouble:
1706 locations->SetInAt(0, Location::RequiresFpuRegister());
1707 locations->SetInAt(1, Location::RequiresFpuRegister());
1708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001709 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001710
1711 default:
1712 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1713 }
1714}
1715
1716void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1717 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001718 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001719 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1720
1721 // 0 if: left == right
1722 // 1 if: left > right
1723 // -1 if: left < right
1724 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001725 case Primitive::kPrimBoolean:
1726 case Primitive::kPrimByte:
1727 case Primitive::kPrimShort:
1728 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001729 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001730 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001732 Location rhs_location = locations->InAt(1);
1733 bool use_imm = rhs_location.IsConstant();
1734 GpuRegister rhs = ZERO;
1735 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001736 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001737 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1738 if (value != 0) {
1739 rhs = AT;
1740 __ LoadConst64(rhs, value);
1741 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001742 } else {
1743 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1744 if (value != 0) {
1745 rhs = AT;
1746 __ LoadConst32(rhs, value);
1747 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001748 }
1749 } else {
1750 rhs = rhs_location.AsRegister<GpuRegister>();
1751 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001752 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001753 __ Slt(res, rhs, lhs);
1754 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001755 break;
1756 }
1757
Alexey Frunze299a9392015-12-08 16:08:02 -08001758 case Primitive::kPrimFloat: {
1759 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1760 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1761 Mips64Label done;
1762 __ CmpEqS(FTMP, lhs, rhs);
1763 __ LoadConst32(res, 0);
1764 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001765 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001766 __ CmpLtS(FTMP, lhs, rhs);
1767 __ LoadConst32(res, -1);
1768 __ Bc1nez(FTMP, &done);
1769 __ LoadConst32(res, 1);
1770 } else {
1771 __ CmpLtS(FTMP, rhs, lhs);
1772 __ LoadConst32(res, 1);
1773 __ Bc1nez(FTMP, &done);
1774 __ LoadConst32(res, -1);
1775 }
1776 __ Bind(&done);
1777 break;
1778 }
1779
Alexey Frunze4dda3372015-06-01 18:31:49 -07001780 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001781 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1782 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1783 Mips64Label done;
1784 __ CmpEqD(FTMP, lhs, rhs);
1785 __ LoadConst32(res, 0);
1786 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001787 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001788 __ CmpLtD(FTMP, lhs, rhs);
1789 __ LoadConst32(res, -1);
1790 __ Bc1nez(FTMP, &done);
1791 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001792 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001793 __ CmpLtD(FTMP, rhs, lhs);
1794 __ LoadConst32(res, 1);
1795 __ Bc1nez(FTMP, &done);
1796 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001797 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001798 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 break;
1800 }
1801
1802 default:
1803 LOG(FATAL) << "Unimplemented compare type " << in_type;
1804 }
1805}
1806
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001808 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001809 switch (instruction->InputAt(0)->GetType()) {
1810 default:
1811 case Primitive::kPrimLong:
1812 locations->SetInAt(0, Location::RequiresRegister());
1813 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1814 break;
1815
1816 case Primitive::kPrimFloat:
1817 case Primitive::kPrimDouble:
1818 locations->SetInAt(0, Location::RequiresFpuRegister());
1819 locations->SetInAt(1, Location::RequiresFpuRegister());
1820 break;
1821 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001822 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1824 }
1825}
1826
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001827void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001828 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001829 return;
1830 }
1831
Alexey Frunze299a9392015-12-08 16:08:02 -08001832 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001833 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001834 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001835 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001836
Alexey Frunze299a9392015-12-08 16:08:02 -08001837 switch (type) {
1838 default:
1839 // Integer case.
1840 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1841 return;
1842 case Primitive::kPrimLong:
1843 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1844 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001845
Alexey Frunze299a9392015-12-08 16:08:02 -08001846 case Primitive::kPrimFloat:
1847 case Primitive::kPrimDouble:
1848 // TODO: don't use branches.
1849 GenerateFpCompareAndBranch(instruction->GetCondition(),
1850 instruction->IsGtBias(),
1851 type,
1852 locations,
1853 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001854 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001855 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001856
1857 // Convert the branches into the result.
1858 Mips64Label done;
1859
1860 // False case: result = 0.
1861 __ LoadConst32(dst, 0);
1862 __ Bc(&done);
1863
1864 // True case: result = 1.
1865 __ Bind(&true_label);
1866 __ LoadConst32(dst, 1);
1867 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001868}
1869
Alexey Frunzec857c742015-09-23 15:12:39 -07001870void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1871 DCHECK(instruction->IsDiv() || instruction->IsRem());
1872 Primitive::Type type = instruction->GetResultType();
1873
1874 LocationSummary* locations = instruction->GetLocations();
1875 Location second = locations->InAt(1);
1876 DCHECK(second.IsConstant());
1877
1878 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1879 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1880 int64_t imm = Int64FromConstant(second.GetConstant());
1881 DCHECK(imm == 1 || imm == -1);
1882
1883 if (instruction->IsRem()) {
1884 __ Move(out, ZERO);
1885 } else {
1886 if (imm == -1) {
1887 if (type == Primitive::kPrimInt) {
1888 __ Subu(out, ZERO, dividend);
1889 } else {
1890 DCHECK_EQ(type, Primitive::kPrimLong);
1891 __ Dsubu(out, ZERO, dividend);
1892 }
1893 } else if (out != dividend) {
1894 __ Move(out, dividend);
1895 }
1896 }
1897}
1898
1899void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1900 DCHECK(instruction->IsDiv() || instruction->IsRem());
1901 Primitive::Type type = instruction->GetResultType();
1902
1903 LocationSummary* locations = instruction->GetLocations();
1904 Location second = locations->InAt(1);
1905 DCHECK(second.IsConstant());
1906
1907 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1908 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1909 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001910 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001911 int ctz_imm = CTZ(abs_imm);
1912
1913 if (instruction->IsDiv()) {
1914 if (type == Primitive::kPrimInt) {
1915 if (ctz_imm == 1) {
1916 // Fast path for division by +/-2, which is very common.
1917 __ Srl(TMP, dividend, 31);
1918 } else {
1919 __ Sra(TMP, dividend, 31);
1920 __ Srl(TMP, TMP, 32 - ctz_imm);
1921 }
1922 __ Addu(out, dividend, TMP);
1923 __ Sra(out, out, ctz_imm);
1924 if (imm < 0) {
1925 __ Subu(out, ZERO, out);
1926 }
1927 } else {
1928 DCHECK_EQ(type, Primitive::kPrimLong);
1929 if (ctz_imm == 1) {
1930 // Fast path for division by +/-2, which is very common.
1931 __ Dsrl32(TMP, dividend, 31);
1932 } else {
1933 __ Dsra32(TMP, dividend, 31);
1934 if (ctz_imm > 32) {
1935 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1936 } else {
1937 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1938 }
1939 }
1940 __ Daddu(out, dividend, TMP);
1941 if (ctz_imm < 32) {
1942 __ Dsra(out, out, ctz_imm);
1943 } else {
1944 __ Dsra32(out, out, ctz_imm - 32);
1945 }
1946 if (imm < 0) {
1947 __ Dsubu(out, ZERO, out);
1948 }
1949 }
1950 } else {
1951 if (type == Primitive::kPrimInt) {
1952 if (ctz_imm == 1) {
1953 // Fast path for modulo +/-2, which is very common.
1954 __ Sra(TMP, dividend, 31);
1955 __ Subu(out, dividend, TMP);
1956 __ Andi(out, out, 1);
1957 __ Addu(out, out, TMP);
1958 } else {
1959 __ Sra(TMP, dividend, 31);
1960 __ Srl(TMP, TMP, 32 - ctz_imm);
1961 __ Addu(out, dividend, TMP);
1962 if (IsUint<16>(abs_imm - 1)) {
1963 __ Andi(out, out, abs_imm - 1);
1964 } else {
1965 __ Sll(out, out, 32 - ctz_imm);
1966 __ Srl(out, out, 32 - ctz_imm);
1967 }
1968 __ Subu(out, out, TMP);
1969 }
1970 } else {
1971 DCHECK_EQ(type, Primitive::kPrimLong);
1972 if (ctz_imm == 1) {
1973 // Fast path for modulo +/-2, which is very common.
1974 __ Dsra32(TMP, dividend, 31);
1975 __ Dsubu(out, dividend, TMP);
1976 __ Andi(out, out, 1);
1977 __ Daddu(out, out, TMP);
1978 } else {
1979 __ Dsra32(TMP, dividend, 31);
1980 if (ctz_imm > 32) {
1981 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1982 } else {
1983 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1984 }
1985 __ Daddu(out, dividend, TMP);
1986 if (IsUint<16>(abs_imm - 1)) {
1987 __ Andi(out, out, abs_imm - 1);
1988 } else {
1989 if (ctz_imm > 32) {
1990 __ Dsll(out, out, 64 - ctz_imm);
1991 __ Dsrl(out, out, 64 - ctz_imm);
1992 } else {
1993 __ Dsll32(out, out, 32 - ctz_imm);
1994 __ Dsrl32(out, out, 32 - ctz_imm);
1995 }
1996 }
1997 __ Dsubu(out, out, TMP);
1998 }
1999 }
2000 }
2001}
2002
2003void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2004 DCHECK(instruction->IsDiv() || instruction->IsRem());
2005
2006 LocationSummary* locations = instruction->GetLocations();
2007 Location second = locations->InAt(1);
2008 DCHECK(second.IsConstant());
2009
2010 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2011 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2012 int64_t imm = Int64FromConstant(second.GetConstant());
2013
2014 Primitive::Type type = instruction->GetResultType();
2015 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2016
2017 int64_t magic;
2018 int shift;
2019 CalculateMagicAndShiftForDivRem(imm,
2020 (type == Primitive::kPrimLong),
2021 &magic,
2022 &shift);
2023
2024 if (type == Primitive::kPrimInt) {
2025 __ LoadConst32(TMP, magic);
2026 __ MuhR6(TMP, dividend, TMP);
2027
2028 if (imm > 0 && magic < 0) {
2029 __ Addu(TMP, TMP, dividend);
2030 } else if (imm < 0 && magic > 0) {
2031 __ Subu(TMP, TMP, dividend);
2032 }
2033
2034 if (shift != 0) {
2035 __ Sra(TMP, TMP, shift);
2036 }
2037
2038 if (instruction->IsDiv()) {
2039 __ Sra(out, TMP, 31);
2040 __ Subu(out, TMP, out);
2041 } else {
2042 __ Sra(AT, TMP, 31);
2043 __ Subu(AT, TMP, AT);
2044 __ LoadConst32(TMP, imm);
2045 __ MulR6(TMP, AT, TMP);
2046 __ Subu(out, dividend, TMP);
2047 }
2048 } else {
2049 __ LoadConst64(TMP, magic);
2050 __ Dmuh(TMP, dividend, TMP);
2051
2052 if (imm > 0 && magic < 0) {
2053 __ Daddu(TMP, TMP, dividend);
2054 } else if (imm < 0 && magic > 0) {
2055 __ Dsubu(TMP, TMP, dividend);
2056 }
2057
2058 if (shift >= 32) {
2059 __ Dsra32(TMP, TMP, shift - 32);
2060 } else if (shift > 0) {
2061 __ Dsra(TMP, TMP, shift);
2062 }
2063
2064 if (instruction->IsDiv()) {
2065 __ Dsra32(out, TMP, 31);
2066 __ Dsubu(out, TMP, out);
2067 } else {
2068 __ Dsra32(AT, TMP, 31);
2069 __ Dsubu(AT, TMP, AT);
2070 __ LoadConst64(TMP, imm);
2071 __ Dmul(TMP, AT, TMP);
2072 __ Dsubu(out, dividend, TMP);
2073 }
2074 }
2075}
2076
2077void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2078 DCHECK(instruction->IsDiv() || instruction->IsRem());
2079 Primitive::Type type = instruction->GetResultType();
2080 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2081
2082 LocationSummary* locations = instruction->GetLocations();
2083 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2084 Location second = locations->InAt(1);
2085
2086 if (second.IsConstant()) {
2087 int64_t imm = Int64FromConstant(second.GetConstant());
2088 if (imm == 0) {
2089 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2090 } else if (imm == 1 || imm == -1) {
2091 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002092 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002093 DivRemByPowerOfTwo(instruction);
2094 } else {
2095 DCHECK(imm <= -2 || imm >= 2);
2096 GenerateDivRemWithAnyConstant(instruction);
2097 }
2098 } else {
2099 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2100 GpuRegister divisor = second.AsRegister<GpuRegister>();
2101 if (instruction->IsDiv()) {
2102 if (type == Primitive::kPrimInt)
2103 __ DivR6(out, dividend, divisor);
2104 else
2105 __ Ddiv(out, dividend, divisor);
2106 } else {
2107 if (type == Primitive::kPrimInt)
2108 __ ModR6(out, dividend, divisor);
2109 else
2110 __ Dmod(out, dividend, divisor);
2111 }
2112 }
2113}
2114
Alexey Frunze4dda3372015-06-01 18:31:49 -07002115void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2116 LocationSummary* locations =
2117 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2118 switch (div->GetResultType()) {
2119 case Primitive::kPrimInt:
2120 case Primitive::kPrimLong:
2121 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002122 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2124 break;
2125
2126 case Primitive::kPrimFloat:
2127 case Primitive::kPrimDouble:
2128 locations->SetInAt(0, Location::RequiresFpuRegister());
2129 locations->SetInAt(1, Location::RequiresFpuRegister());
2130 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2135 }
2136}
2137
2138void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2139 Primitive::Type type = instruction->GetType();
2140 LocationSummary* locations = instruction->GetLocations();
2141
2142 switch (type) {
2143 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002144 case Primitive::kPrimLong:
2145 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002146 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002147 case Primitive::kPrimFloat:
2148 case Primitive::kPrimDouble: {
2149 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2150 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2151 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2152 if (type == Primitive::kPrimFloat)
2153 __ DivS(dst, lhs, rhs);
2154 else
2155 __ DivD(dst, lhs, rhs);
2156 break;
2157 }
2158 default:
2159 LOG(FATAL) << "Unexpected div type " << type;
2160 }
2161}
2162
2163void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002164 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2165 ? LocationSummary::kCallOnSlowPath
2166 : LocationSummary::kNoCall;
2167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002168 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2169 if (instruction->HasUses()) {
2170 locations->SetOut(Location::SameAsFirstInput());
2171 }
2172}
2173
2174void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2175 SlowPathCodeMIPS64* slow_path =
2176 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2177 codegen_->AddSlowPath(slow_path);
2178 Location value = instruction->GetLocations()->InAt(0);
2179
2180 Primitive::Type type = instruction->GetType();
2181
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002182 if (!Primitive::IsIntegralType(type)) {
2183 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002184 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002185 }
2186
2187 if (value.IsConstant()) {
2188 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2189 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002190 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002191 } else {
2192 // A division by a non-null constant is valid. We don't need to perform
2193 // any check, so simply fall through.
2194 }
2195 } else {
2196 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2197 }
2198}
2199
2200void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2201 LocationSummary* locations =
2202 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2203 locations->SetOut(Location::ConstantLocation(constant));
2204}
2205
2206void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2207 // Will be generated at use site.
2208}
2209
2210void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2211 exit->SetLocations(nullptr);
2212}
2213
2214void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2215}
2216
2217void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2220 locations->SetOut(Location::ConstantLocation(constant));
2221}
2222
2223void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2224 // Will be generated at use site.
2225}
2226
David Brazdilfc6a86a2015-06-26 10:33:45 +00002227void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002228 DCHECK(!successor->IsExitBlock());
2229 HBasicBlock* block = got->GetBlock();
2230 HInstruction* previous = got->GetPrevious();
2231 HLoopInformation* info = block->GetLoopInformation();
2232
2233 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2234 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2235 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2236 return;
2237 }
2238 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2239 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2240 }
2241 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002242 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002243 }
2244}
2245
David Brazdilfc6a86a2015-06-26 10:33:45 +00002246void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2247 got->SetLocations(nullptr);
2248}
2249
2250void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2251 HandleGoto(got, got->GetSuccessor());
2252}
2253
2254void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2255 try_boundary->SetLocations(nullptr);
2256}
2257
2258void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2259 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2260 if (!successor->IsExitBlock()) {
2261 HandleGoto(try_boundary, successor);
2262 }
2263}
2264
Alexey Frunze299a9392015-12-08 16:08:02 -08002265void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2266 bool is64bit,
2267 LocationSummary* locations) {
2268 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2269 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2270 Location rhs_location = locations->InAt(1);
2271 GpuRegister rhs_reg = ZERO;
2272 int64_t rhs_imm = 0;
2273 bool use_imm = rhs_location.IsConstant();
2274 if (use_imm) {
2275 if (is64bit) {
2276 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2277 } else {
2278 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2279 }
2280 } else {
2281 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2282 }
2283 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2284
2285 switch (cond) {
2286 case kCondEQ:
2287 case kCondNE:
2288 if (use_imm && IsUint<16>(rhs_imm)) {
2289 __ Xori(dst, lhs, rhs_imm);
2290 } else {
2291 if (use_imm) {
2292 rhs_reg = TMP;
2293 __ LoadConst64(rhs_reg, rhs_imm);
2294 }
2295 __ Xor(dst, lhs, rhs_reg);
2296 }
2297 if (cond == kCondEQ) {
2298 __ Sltiu(dst, dst, 1);
2299 } else {
2300 __ Sltu(dst, ZERO, dst);
2301 }
2302 break;
2303
2304 case kCondLT:
2305 case kCondGE:
2306 if (use_imm && IsInt<16>(rhs_imm)) {
2307 __ Slti(dst, lhs, rhs_imm);
2308 } else {
2309 if (use_imm) {
2310 rhs_reg = TMP;
2311 __ LoadConst64(rhs_reg, rhs_imm);
2312 }
2313 __ Slt(dst, lhs, rhs_reg);
2314 }
2315 if (cond == kCondGE) {
2316 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2317 // only the slt instruction but no sge.
2318 __ Xori(dst, dst, 1);
2319 }
2320 break;
2321
2322 case kCondLE:
2323 case kCondGT:
2324 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2325 // Simulate lhs <= rhs via lhs < rhs + 1.
2326 __ Slti(dst, lhs, rhs_imm_plus_one);
2327 if (cond == kCondGT) {
2328 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2329 // only the slti instruction but no sgti.
2330 __ Xori(dst, dst, 1);
2331 }
2332 } else {
2333 if (use_imm) {
2334 rhs_reg = TMP;
2335 __ LoadConst64(rhs_reg, rhs_imm);
2336 }
2337 __ Slt(dst, rhs_reg, lhs);
2338 if (cond == kCondLE) {
2339 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2340 // only the slt instruction but no sle.
2341 __ Xori(dst, dst, 1);
2342 }
2343 }
2344 break;
2345
2346 case kCondB:
2347 case kCondAE:
2348 if (use_imm && IsInt<16>(rhs_imm)) {
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);
2354 } else {
2355 if (use_imm) {
2356 rhs_reg = TMP;
2357 __ LoadConst64(rhs_reg, rhs_imm);
2358 }
2359 __ Sltu(dst, lhs, rhs_reg);
2360 }
2361 if (cond == kCondAE) {
2362 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2363 // only the sltu instruction but no sgeu.
2364 __ Xori(dst, dst, 1);
2365 }
2366 break;
2367
2368 case kCondBE:
2369 case kCondA:
2370 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2371 // Simulate lhs <= rhs via lhs < rhs + 1.
2372 // Note that this only works if rhs + 1 does not overflow
2373 // to 0, hence the check above.
2374 // Sltiu sign-extends its 16-bit immediate operand before
2375 // the comparison and thus lets us compare directly with
2376 // unsigned values in the ranges [0, 0x7fff] and
2377 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2378 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2379 if (cond == kCondA) {
2380 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2381 // only the sltiu instruction but no sgtiu.
2382 __ Xori(dst, dst, 1);
2383 }
2384 } else {
2385 if (use_imm) {
2386 rhs_reg = TMP;
2387 __ LoadConst64(rhs_reg, rhs_imm);
2388 }
2389 __ Sltu(dst, rhs_reg, lhs);
2390 if (cond == kCondBE) {
2391 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2392 // only the sltu instruction but no sleu.
2393 __ Xori(dst, dst, 1);
2394 }
2395 }
2396 break;
2397 }
2398}
2399
2400void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2401 bool is64bit,
2402 LocationSummary* locations,
2403 Mips64Label* label) {
2404 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2405 Location rhs_location = locations->InAt(1);
2406 GpuRegister rhs_reg = ZERO;
2407 int64_t rhs_imm = 0;
2408 bool use_imm = rhs_location.IsConstant();
2409 if (use_imm) {
2410 if (is64bit) {
2411 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2412 } else {
2413 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2414 }
2415 } else {
2416 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2417 }
2418
2419 if (use_imm && rhs_imm == 0) {
2420 switch (cond) {
2421 case kCondEQ:
2422 case kCondBE: // <= 0 if zero
2423 __ Beqzc(lhs, label);
2424 break;
2425 case kCondNE:
2426 case kCondA: // > 0 if non-zero
2427 __ Bnezc(lhs, label);
2428 break;
2429 case kCondLT:
2430 __ Bltzc(lhs, label);
2431 break;
2432 case kCondGE:
2433 __ Bgezc(lhs, label);
2434 break;
2435 case kCondLE:
2436 __ Blezc(lhs, label);
2437 break;
2438 case kCondGT:
2439 __ Bgtzc(lhs, label);
2440 break;
2441 case kCondB: // always false
2442 break;
2443 case kCondAE: // always true
2444 __ Bc(label);
2445 break;
2446 }
2447 } else {
2448 if (use_imm) {
2449 rhs_reg = TMP;
2450 __ LoadConst64(rhs_reg, rhs_imm);
2451 }
2452 switch (cond) {
2453 case kCondEQ:
2454 __ Beqc(lhs, rhs_reg, label);
2455 break;
2456 case kCondNE:
2457 __ Bnec(lhs, rhs_reg, label);
2458 break;
2459 case kCondLT:
2460 __ Bltc(lhs, rhs_reg, label);
2461 break;
2462 case kCondGE:
2463 __ Bgec(lhs, rhs_reg, label);
2464 break;
2465 case kCondLE:
2466 __ Bgec(rhs_reg, lhs, label);
2467 break;
2468 case kCondGT:
2469 __ Bltc(rhs_reg, lhs, label);
2470 break;
2471 case kCondB:
2472 __ Bltuc(lhs, rhs_reg, label);
2473 break;
2474 case kCondAE:
2475 __ Bgeuc(lhs, rhs_reg, label);
2476 break;
2477 case kCondBE:
2478 __ Bgeuc(rhs_reg, lhs, label);
2479 break;
2480 case kCondA:
2481 __ Bltuc(rhs_reg, lhs, label);
2482 break;
2483 }
2484 }
2485}
2486
2487void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2488 bool gt_bias,
2489 Primitive::Type type,
2490 LocationSummary* locations,
2491 Mips64Label* label) {
2492 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2493 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2494 if (type == Primitive::kPrimFloat) {
2495 switch (cond) {
2496 case kCondEQ:
2497 __ CmpEqS(FTMP, lhs, rhs);
2498 __ Bc1nez(FTMP, label);
2499 break;
2500 case kCondNE:
2501 __ CmpEqS(FTMP, lhs, rhs);
2502 __ Bc1eqz(FTMP, label);
2503 break;
2504 case kCondLT:
2505 if (gt_bias) {
2506 __ CmpLtS(FTMP, lhs, rhs);
2507 } else {
2508 __ CmpUltS(FTMP, lhs, rhs);
2509 }
2510 __ Bc1nez(FTMP, label);
2511 break;
2512 case kCondLE:
2513 if (gt_bias) {
2514 __ CmpLeS(FTMP, lhs, rhs);
2515 } else {
2516 __ CmpUleS(FTMP, lhs, rhs);
2517 }
2518 __ Bc1nez(FTMP, label);
2519 break;
2520 case kCondGT:
2521 if (gt_bias) {
2522 __ CmpUltS(FTMP, rhs, lhs);
2523 } else {
2524 __ CmpLtS(FTMP, rhs, lhs);
2525 }
2526 __ Bc1nez(FTMP, label);
2527 break;
2528 case kCondGE:
2529 if (gt_bias) {
2530 __ CmpUleS(FTMP, rhs, lhs);
2531 } else {
2532 __ CmpLeS(FTMP, rhs, lhs);
2533 }
2534 __ Bc1nez(FTMP, label);
2535 break;
2536 default:
2537 LOG(FATAL) << "Unexpected non-floating-point condition";
2538 }
2539 } else {
2540 DCHECK_EQ(type, Primitive::kPrimDouble);
2541 switch (cond) {
2542 case kCondEQ:
2543 __ CmpEqD(FTMP, lhs, rhs);
2544 __ Bc1nez(FTMP, label);
2545 break;
2546 case kCondNE:
2547 __ CmpEqD(FTMP, lhs, rhs);
2548 __ Bc1eqz(FTMP, label);
2549 break;
2550 case kCondLT:
2551 if (gt_bias) {
2552 __ CmpLtD(FTMP, lhs, rhs);
2553 } else {
2554 __ CmpUltD(FTMP, lhs, rhs);
2555 }
2556 __ Bc1nez(FTMP, label);
2557 break;
2558 case kCondLE:
2559 if (gt_bias) {
2560 __ CmpLeD(FTMP, lhs, rhs);
2561 } else {
2562 __ CmpUleD(FTMP, lhs, rhs);
2563 }
2564 __ Bc1nez(FTMP, label);
2565 break;
2566 case kCondGT:
2567 if (gt_bias) {
2568 __ CmpUltD(FTMP, rhs, lhs);
2569 } else {
2570 __ CmpLtD(FTMP, rhs, lhs);
2571 }
2572 __ Bc1nez(FTMP, label);
2573 break;
2574 case kCondGE:
2575 if (gt_bias) {
2576 __ CmpUleD(FTMP, rhs, lhs);
2577 } else {
2578 __ CmpLeD(FTMP, rhs, lhs);
2579 }
2580 __ Bc1nez(FTMP, label);
2581 break;
2582 default:
2583 LOG(FATAL) << "Unexpected non-floating-point condition";
2584 }
2585 }
2586}
2587
Alexey Frunze4dda3372015-06-01 18:31:49 -07002588void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002589 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002590 Mips64Label* true_target,
2591 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002592 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002593
David Brazdil0debae72015-11-12 18:37:00 +00002594 if (true_target == nullptr && false_target == nullptr) {
2595 // Nothing to do. The code always falls through.
2596 return;
2597 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002598 // Constant condition, statically compared against "true" (integer value 1).
2599 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002600 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002601 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002602 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002603 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002604 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002605 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002606 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002607 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002608 }
David Brazdil0debae72015-11-12 18:37:00 +00002609 return;
2610 }
2611
2612 // The following code generates these patterns:
2613 // (1) true_target == nullptr && false_target != nullptr
2614 // - opposite condition true => branch to false_target
2615 // (2) true_target != nullptr && false_target == nullptr
2616 // - condition true => branch to true_target
2617 // (3) true_target != nullptr && false_target != nullptr
2618 // - condition true => branch to true_target
2619 // - branch to false_target
2620 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002621 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002622 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002623 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002624 if (true_target == nullptr) {
2625 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2626 } else {
2627 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2628 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002629 } else {
2630 // The condition instruction has not been materialized, use its inputs as
2631 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002632 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002633 Primitive::Type type = condition->InputAt(0)->GetType();
2634 LocationSummary* locations = cond->GetLocations();
2635 IfCondition if_cond = condition->GetCondition();
2636 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002637
David Brazdil0debae72015-11-12 18:37:00 +00002638 if (true_target == nullptr) {
2639 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002640 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002641 }
2642
Alexey Frunze299a9392015-12-08 16:08:02 -08002643 switch (type) {
2644 default:
2645 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2646 break;
2647 case Primitive::kPrimLong:
2648 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2649 break;
2650 case Primitive::kPrimFloat:
2651 case Primitive::kPrimDouble:
2652 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2653 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002654 }
2655 }
David Brazdil0debae72015-11-12 18:37:00 +00002656
2657 // If neither branch falls through (case 3), the conditional branch to `true_target`
2658 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2659 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002660 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002661 }
2662}
2663
2664void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002666 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002667 locations->SetInAt(0, Location::RequiresRegister());
2668 }
2669}
2670
2671void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002672 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2673 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002674 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002675 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002676 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002677 nullptr : codegen_->GetLabelOf(false_successor);
2678 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002679}
2680
2681void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2682 LocationSummary* locations = new (GetGraph()->GetArena())
2683 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002684 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002685 locations->SetInAt(0, Location::RequiresRegister());
2686 }
2687}
2688
2689void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002690 SlowPathCodeMIPS64* slow_path =
2691 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002692 GenerateTestAndBranch(deoptimize,
2693 /* condition_input_index */ 0,
2694 slow_path->GetEntryLabel(),
2695 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002696}
2697
David Brazdil74eb1b22015-12-14 11:44:01 +00002698void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2700 if (Primitive::IsFloatingPointType(select->GetType())) {
2701 locations->SetInAt(0, Location::RequiresFpuRegister());
2702 locations->SetInAt(1, Location::RequiresFpuRegister());
2703 } else {
2704 locations->SetInAt(0, Location::RequiresRegister());
2705 locations->SetInAt(1, Location::RequiresRegister());
2706 }
2707 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2708 locations->SetInAt(2, Location::RequiresRegister());
2709 }
2710 locations->SetOut(Location::SameAsFirstInput());
2711}
2712
2713void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2714 LocationSummary* locations = select->GetLocations();
2715 Mips64Label false_target;
2716 GenerateTestAndBranch(select,
2717 /* condition_input_index */ 2,
2718 /* true_target */ nullptr,
2719 &false_target);
2720 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2721 __ Bind(&false_target);
2722}
2723
David Srbecky0cf44932015-12-09 14:09:59 +00002724void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2725 new (GetGraph()->GetArena()) LocationSummary(info);
2726}
2727
David Srbeckyd28f4a02016-03-14 17:14:24 +00002728void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2729 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002730}
2731
2732void CodeGeneratorMIPS64::GenerateNop() {
2733 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002734}
2735
Alexey Frunze4dda3372015-06-01 18:31:49 -07002736void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2737 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2740 locations->SetInAt(0, Location::RequiresRegister());
2741 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2742 locations->SetOut(Location::RequiresFpuRegister());
2743 } else {
2744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2745 }
2746}
2747
2748void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2749 const FieldInfo& field_info) {
2750 Primitive::Type type = field_info.GetFieldType();
2751 LocationSummary* locations = instruction->GetLocations();
2752 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2753 LoadOperandType load_type = kLoadUnsignedByte;
2754 switch (type) {
2755 case Primitive::kPrimBoolean:
2756 load_type = kLoadUnsignedByte;
2757 break;
2758 case Primitive::kPrimByte:
2759 load_type = kLoadSignedByte;
2760 break;
2761 case Primitive::kPrimShort:
2762 load_type = kLoadSignedHalfword;
2763 break;
2764 case Primitive::kPrimChar:
2765 load_type = kLoadUnsignedHalfword;
2766 break;
2767 case Primitive::kPrimInt:
2768 case Primitive::kPrimFloat:
2769 load_type = kLoadWord;
2770 break;
2771 case Primitive::kPrimLong:
2772 case Primitive::kPrimDouble:
2773 load_type = kLoadDoubleword;
2774 break;
2775 case Primitive::kPrimNot:
2776 load_type = kLoadUnsignedWord;
2777 break;
2778 case Primitive::kPrimVoid:
2779 LOG(FATAL) << "Unreachable type " << type;
2780 UNREACHABLE();
2781 }
2782 if (!Primitive::IsFloatingPointType(type)) {
2783 DCHECK(locations->Out().IsRegister());
2784 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2785 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2786 } else {
2787 DCHECK(locations->Out().IsFpuRegister());
2788 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2789 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2790 }
2791
2792 codegen_->MaybeRecordImplicitNullCheck(instruction);
2793 // TODO: memory barrier?
2794}
2795
2796void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2797 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2800 locations->SetInAt(0, Location::RequiresRegister());
2801 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2802 locations->SetInAt(1, Location::RequiresFpuRegister());
2803 } else {
2804 locations->SetInAt(1, Location::RequiresRegister());
2805 }
2806}
2807
2808void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002809 const FieldInfo& field_info,
2810 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002811 Primitive::Type type = field_info.GetFieldType();
2812 LocationSummary* locations = instruction->GetLocations();
2813 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2814 StoreOperandType store_type = kStoreByte;
2815 switch (type) {
2816 case Primitive::kPrimBoolean:
2817 case Primitive::kPrimByte:
2818 store_type = kStoreByte;
2819 break;
2820 case Primitive::kPrimShort:
2821 case Primitive::kPrimChar:
2822 store_type = kStoreHalfword;
2823 break;
2824 case Primitive::kPrimInt:
2825 case Primitive::kPrimFloat:
2826 case Primitive::kPrimNot:
2827 store_type = kStoreWord;
2828 break;
2829 case Primitive::kPrimLong:
2830 case Primitive::kPrimDouble:
2831 store_type = kStoreDoubleword;
2832 break;
2833 case Primitive::kPrimVoid:
2834 LOG(FATAL) << "Unreachable type " << type;
2835 UNREACHABLE();
2836 }
2837 if (!Primitive::IsFloatingPointType(type)) {
2838 DCHECK(locations->InAt(1).IsRegister());
2839 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2840 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2841 } else {
2842 DCHECK(locations->InAt(1).IsFpuRegister());
2843 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2844 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2845 }
2846
2847 codegen_->MaybeRecordImplicitNullCheck(instruction);
2848 // TODO: memory barriers?
2849 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2850 DCHECK(locations->InAt(1).IsRegister());
2851 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002852 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002853 }
2854}
2855
2856void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2857 HandleFieldGet(instruction, instruction->GetFieldInfo());
2858}
2859
2860void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2861 HandleFieldGet(instruction, instruction->GetFieldInfo());
2862}
2863
2864void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2865 HandleFieldSet(instruction, instruction->GetFieldInfo());
2866}
2867
2868void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002869 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002870}
2871
2872void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2873 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002874 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2876 locations->SetInAt(0, Location::RequiresRegister());
2877 locations->SetInAt(1, Location::RequiresRegister());
2878 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002879 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002880 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2881}
2882
2883void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2884 LocationSummary* locations = instruction->GetLocations();
2885 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2886 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2887 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2888
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002889 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002890
2891 // Return 0 if `obj` is null.
2892 // TODO: Avoid this check if we know `obj` is not null.
2893 __ Move(out, ZERO);
2894 __ Beqzc(obj, &done);
2895
2896 // Compare the class of `obj` with `cls`.
2897 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002898 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002899 // Classes must be equal for the instanceof to succeed.
2900 __ Xor(out, out, cls);
2901 __ Sltiu(out, out, 1);
2902 } else {
2903 // If the classes are not equal, we go into a slow path.
2904 DCHECK(locations->OnlyCallsOnSlowPath());
2905 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002906 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002907 codegen_->AddSlowPath(slow_path);
2908 __ Bnec(out, cls, slow_path->GetEntryLabel());
2909 __ LoadConst32(out, 1);
2910 __ Bind(slow_path->GetExitLabel());
2911 }
2912
2913 __ Bind(&done);
2914}
2915
2916void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2918 locations->SetOut(Location::ConstantLocation(constant));
2919}
2920
2921void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2922 // Will be generated at use site.
2923}
2924
2925void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2926 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2927 locations->SetOut(Location::ConstantLocation(constant));
2928}
2929
2930void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2931 // Will be generated at use site.
2932}
2933
Calin Juravle175dc732015-08-25 15:42:32 +01002934void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2935 // The trampoline uses the same calling convention as dex calling conventions,
2936 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2937 // the method_idx.
2938 HandleInvoke(invoke);
2939}
2940
2941void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2942 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2943}
2944
Alexey Frunze4dda3372015-06-01 18:31:49 -07002945void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2946 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2947 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2948}
2949
2950void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2951 HandleInvoke(invoke);
2952 // The register T0 is required to be used for the hidden argument in
2953 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2954 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2955}
2956
2957void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2958 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2959 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2960 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2961 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2962 Location receiver = invoke->GetLocations()->InAt(0);
2963 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002964 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002965
2966 // Set the hidden argument.
2967 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2968 invoke->GetDexMethodIndex());
2969
2970 // temp = object->GetClass();
2971 if (receiver.IsStackSlot()) {
2972 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2973 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2974 } else {
2975 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2976 }
2977 codegen_->MaybeRecordImplicitNullCheck(invoke);
2978 // temp = temp->GetImtEntryAt(method_offset);
2979 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2980 // T9 = temp->GetEntryPoint();
2981 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2982 // T9();
2983 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002984 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002985 DCHECK(!codegen_->IsLeafMethod());
2986 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2987}
2988
2989void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002990 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2991 if (intrinsic.TryDispatch(invoke)) {
2992 return;
2993 }
2994
Alexey Frunze4dda3372015-06-01 18:31:49 -07002995 HandleInvoke(invoke);
2996}
2997
2998void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002999 // Explicit clinit checks triggered by static invokes must have been pruned by
3000 // art::PrepareForRegisterAllocation.
3001 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003002
Chris Larsen3039e382015-08-26 07:54:08 -07003003 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
3004 if (intrinsic.TryDispatch(invoke)) {
3005 return;
3006 }
3007
Alexey Frunze4dda3372015-06-01 18:31:49 -07003008 HandleInvoke(invoke);
3009
3010 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
3011 // clobbering somewhere else, reduce further register pressure by avoiding
3012 // allocation of a register for the current method pointer like on x86 baseline.
3013 // TODO: remove this once all the issues with register saving/restoring are
3014 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003015 if (invoke->HasCurrentMethodInput()) {
3016 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00003017 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003018 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003019 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00003020 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003021 }
3022}
3023
Chris Larsen3039e382015-08-26 07:54:08 -07003024static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003025 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003026 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3027 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003028 return true;
3029 }
3030 return false;
3031}
3032
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003033HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3034 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3035 // TODO: Implement other kinds.
3036 return HLoadString::LoadKind::kDexCacheViaMethod;
3037}
3038
Vladimir Markodc151b22015-10-15 18:02:30 +01003039HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3040 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3041 MethodReference target_method ATTRIBUTE_UNUSED) {
3042 switch (desired_dispatch_info.method_load_kind) {
3043 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3044 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3045 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3046 return HInvokeStaticOrDirect::DispatchInfo {
3047 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3048 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3049 0u,
3050 0u
3051 };
3052 default:
3053 break;
3054 }
3055 switch (desired_dispatch_info.code_ptr_location) {
3056 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3057 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3058 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3059 return HInvokeStaticOrDirect::DispatchInfo {
3060 desired_dispatch_info.method_load_kind,
3061 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3062 desired_dispatch_info.method_load_data,
3063 0u
3064 };
3065 default:
3066 return desired_dispatch_info;
3067 }
3068}
3069
Alexey Frunze4dda3372015-06-01 18:31:49 -07003070void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3071 // All registers are assumed to be correctly set up per the calling convention.
3072
Vladimir Marko58155012015-08-19 12:49:41 +00003073 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3074 switch (invoke->GetMethodLoadKind()) {
3075 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3076 // temp = thread->string_init_entrypoint
3077 __ LoadFromOffset(kLoadDoubleword,
3078 temp.AsRegister<GpuRegister>(),
3079 TR,
3080 invoke->GetStringInitOffset());
3081 break;
3082 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003083 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003084 break;
3085 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3086 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3087 break;
3088 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003089 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003090 // TODO: Implement these types.
3091 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3092 LOG(FATAL) << "Unsupported";
3093 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003094 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003095 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003096 GpuRegister reg = temp.AsRegister<GpuRegister>();
3097 GpuRegister method_reg;
3098 if (current_method.IsRegister()) {
3099 method_reg = current_method.AsRegister<GpuRegister>();
3100 } else {
3101 // TODO: use the appropriate DCHECK() here if possible.
3102 // DCHECK(invoke->GetLocations()->Intrinsified());
3103 DCHECK(!current_method.IsValid());
3104 method_reg = reg;
3105 __ Ld(reg, SP, kCurrentMethodStackOffset);
3106 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003107
Vladimir Marko58155012015-08-19 12:49:41 +00003108 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003109 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003110 reg,
3111 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003112 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003113 // temp = temp[index_in_cache]
3114 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3115 __ LoadFromOffset(kLoadDoubleword,
3116 reg,
3117 reg,
3118 CodeGenerator::GetCachePointerOffset(index_in_cache));
3119 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003120 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003121 }
3122
Vladimir Marko58155012015-08-19 12:49:41 +00003123 switch (invoke->GetCodePtrLocation()) {
3124 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003125 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003126 break;
3127 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3128 // LR = invoke->GetDirectCodePtr();
3129 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3130 // LR()
3131 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003132 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003133 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003134 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003135 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3136 // TODO: Implement these types.
3137 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3138 LOG(FATAL) << "Unsupported";
3139 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003140 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3141 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3142 __ LoadFromOffset(kLoadDoubleword,
3143 T9,
3144 callee_method.AsRegister<GpuRegister>(),
3145 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003146 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003147 // T9()
3148 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003149 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003150 break;
3151 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003152 DCHECK(!IsLeafMethod());
3153}
3154
3155void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003156 // Explicit clinit checks triggered by static invokes must have been pruned by
3157 // art::PrepareForRegisterAllocation.
3158 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003159
3160 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3161 return;
3162 }
3163
3164 LocationSummary* locations = invoke->GetLocations();
3165 codegen_->GenerateStaticOrDirectCall(invoke,
3166 locations->HasTemps()
3167 ? locations->GetTemp(0)
3168 : Location::NoLocation());
3169 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3170}
3171
Alexey Frunze53afca12015-11-05 16:34:23 -08003172void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003173 // Use the calling convention instead of the location of the receiver, as
3174 // intrinsics may have put the receiver in a different register. In the intrinsics
3175 // slow path, the arguments have been moved to the right place, so here we are
3176 // guaranteed that the receiver is the first register of the calling convention.
3177 InvokeDexCallingConvention calling_convention;
3178 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3179
Alexey Frunze53afca12015-11-05 16:34:23 -08003180 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3182 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3183 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003184 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003185
3186 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003187 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003188 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003189 // temp = temp->GetMethodAt(method_offset);
3190 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3191 // T9 = temp->GetEntryPoint();
3192 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3193 // T9();
3194 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003195 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003196}
3197
3198void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3199 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3200 return;
3201 }
3202
3203 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003204 DCHECK(!codegen_->IsLeafMethod());
3205 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3206}
3207
3208void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003209 InvokeRuntimeCallingConvention calling_convention;
3210 CodeGenerator::CreateLoadClassLocationSummary(
3211 cls,
3212 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003213 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003214}
3215
3216void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3217 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003218 if (cls->NeedsAccessCheck()) {
3219 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3220 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3221 cls,
3222 cls->GetDexPc(),
3223 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003224 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003225 return;
3226 }
3227
3228 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3229 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3230 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003231 DCHECK(!cls->CanCallRuntime());
3232 DCHECK(!cls->MustGenerateClinitCheck());
3233 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3234 ArtMethod::DeclaringClassOffset().Int32Value());
3235 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003236 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3237 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003238 __ LoadFromOffset(
3239 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003240 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003241 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3242 DCHECK(cls->CanCallRuntime());
3243 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3244 cls,
3245 cls,
3246 cls->GetDexPc(),
3247 cls->MustGenerateClinitCheck());
3248 codegen_->AddSlowPath(slow_path);
3249 if (!cls->IsInDexCache()) {
3250 __ Beqzc(out, slow_path->GetEntryLabel());
3251 }
3252 if (cls->MustGenerateClinitCheck()) {
3253 GenerateClassInitializationCheck(slow_path, out);
3254 } else {
3255 __ Bind(slow_path->GetExitLabel());
3256 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003257 }
3258 }
3259}
3260
David Brazdilcb1c0552015-08-04 16:22:25 +01003261static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003262 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003263}
3264
Alexey Frunze4dda3372015-06-01 18:31:49 -07003265void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3266 LocationSummary* locations =
3267 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3268 locations->SetOut(Location::RequiresRegister());
3269}
3270
3271void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3272 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003273 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3274}
3275
3276void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3277 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3278}
3279
3280void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3281 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003282}
3283
3284void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
3285 load->SetLocations(nullptr);
3286}
3287
3288void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3289 // Nothing to do, this is driven by the code generator.
3290}
3291
3292void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003293 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3294 ? LocationSummary::kCallOnSlowPath
3295 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003296 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003297 locations->SetInAt(0, Location::RequiresRegister());
3298 locations->SetOut(Location::RequiresRegister());
3299}
3300
3301void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003302 LocationSummary* locations = load->GetLocations();
3303 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3304 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3305 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3306 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003307 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003308 __ LoadFromOffset(
3309 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003310 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003311
3312 if (!load->IsInDexCache()) {
3313 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3314 codegen_->AddSlowPath(slow_path);
3315 __ Beqzc(out, slow_path->GetEntryLabel());
3316 __ Bind(slow_path->GetExitLabel());
3317 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003318}
3319
3320void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
3321 local->SetLocations(nullptr);
3322}
3323
3324void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
3325 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3326}
3327
3328void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3330 locations->SetOut(Location::ConstantLocation(constant));
3331}
3332
3333void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3334 // Will be generated at use site.
3335}
3336
3337void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3338 LocationSummary* locations =
3339 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3340 InvokeRuntimeCallingConvention calling_convention;
3341 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3342}
3343
3344void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3345 codegen_->InvokeRuntime(instruction->IsEnter()
3346 ? QUICK_ENTRY_POINT(pLockObject)
3347 : QUICK_ENTRY_POINT(pUnlockObject),
3348 instruction,
3349 instruction->GetDexPc(),
3350 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003351 if (instruction->IsEnter()) {
3352 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3353 } else {
3354 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3355 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003356}
3357
3358void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3359 LocationSummary* locations =
3360 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3361 switch (mul->GetResultType()) {
3362 case Primitive::kPrimInt:
3363 case Primitive::kPrimLong:
3364 locations->SetInAt(0, Location::RequiresRegister());
3365 locations->SetInAt(1, Location::RequiresRegister());
3366 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3367 break;
3368
3369 case Primitive::kPrimFloat:
3370 case Primitive::kPrimDouble:
3371 locations->SetInAt(0, Location::RequiresFpuRegister());
3372 locations->SetInAt(1, Location::RequiresFpuRegister());
3373 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3374 break;
3375
3376 default:
3377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3378 }
3379}
3380
3381void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3382 Primitive::Type type = instruction->GetType();
3383 LocationSummary* locations = instruction->GetLocations();
3384
3385 switch (type) {
3386 case Primitive::kPrimInt:
3387 case Primitive::kPrimLong: {
3388 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3389 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3390 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3391 if (type == Primitive::kPrimInt)
3392 __ MulR6(dst, lhs, rhs);
3393 else
3394 __ Dmul(dst, lhs, rhs);
3395 break;
3396 }
3397 case Primitive::kPrimFloat:
3398 case Primitive::kPrimDouble: {
3399 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3400 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3401 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3402 if (type == Primitive::kPrimFloat)
3403 __ MulS(dst, lhs, rhs);
3404 else
3405 __ MulD(dst, lhs, rhs);
3406 break;
3407 }
3408 default:
3409 LOG(FATAL) << "Unexpected mul type " << type;
3410 }
3411}
3412
3413void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3414 LocationSummary* locations =
3415 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3416 switch (neg->GetResultType()) {
3417 case Primitive::kPrimInt:
3418 case Primitive::kPrimLong:
3419 locations->SetInAt(0, Location::RequiresRegister());
3420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3421 break;
3422
3423 case Primitive::kPrimFloat:
3424 case Primitive::kPrimDouble:
3425 locations->SetInAt(0, Location::RequiresFpuRegister());
3426 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3427 break;
3428
3429 default:
3430 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3431 }
3432}
3433
3434void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3435 Primitive::Type type = instruction->GetType();
3436 LocationSummary* locations = instruction->GetLocations();
3437
3438 switch (type) {
3439 case Primitive::kPrimInt:
3440 case Primitive::kPrimLong: {
3441 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3442 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3443 if (type == Primitive::kPrimInt)
3444 __ Subu(dst, ZERO, src);
3445 else
3446 __ Dsubu(dst, ZERO, src);
3447 break;
3448 }
3449 case Primitive::kPrimFloat:
3450 case Primitive::kPrimDouble: {
3451 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3452 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3453 if (type == Primitive::kPrimFloat)
3454 __ NegS(dst, src);
3455 else
3456 __ NegD(dst, src);
3457 break;
3458 }
3459 default:
3460 LOG(FATAL) << "Unexpected neg type " << type;
3461 }
3462}
3463
3464void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3465 LocationSummary* locations =
3466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3467 InvokeRuntimeCallingConvention calling_convention;
3468 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3469 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3470 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3471 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3472}
3473
3474void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3475 LocationSummary* locations = instruction->GetLocations();
3476 // Move an uint16_t value to a register.
3477 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003478 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3479 instruction,
3480 instruction->GetDexPc(),
3481 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003482 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3483}
3484
3485void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3486 LocationSummary* locations =
3487 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3488 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003489 if (instruction->IsStringAlloc()) {
3490 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3491 } else {
3492 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3493 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3494 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003495 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003499 if (instruction->IsStringAlloc()) {
3500 // String is allocated through StringFactory. Call NewEmptyString entry point.
3501 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003502 MemberOffset code_offset =
3503 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003504 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3505 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3506 __ Jalr(T9);
3507 __ Nop();
3508 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3509 } else {
3510 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3511 instruction,
3512 instruction->GetDexPc(),
3513 nullptr);
3514 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3515 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516}
3517
3518void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3520 locations->SetInAt(0, Location::RequiresRegister());
3521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3522}
3523
3524void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3525 Primitive::Type type = instruction->GetType();
3526 LocationSummary* locations = instruction->GetLocations();
3527
3528 switch (type) {
3529 case Primitive::kPrimInt:
3530 case Primitive::kPrimLong: {
3531 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3532 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3533 __ Nor(dst, src, ZERO);
3534 break;
3535 }
3536
3537 default:
3538 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3539 }
3540}
3541
3542void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3544 locations->SetInAt(0, Location::RequiresRegister());
3545 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3546}
3547
3548void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3549 LocationSummary* locations = instruction->GetLocations();
3550 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3551 locations->InAt(0).AsRegister<GpuRegister>(),
3552 1);
3553}
3554
3555void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003556 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3557 ? LocationSummary::kCallOnSlowPath
3558 : LocationSummary::kNoCall;
3559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003560 locations->SetInAt(0, Location::RequiresRegister());
3561 if (instruction->HasUses()) {
3562 locations->SetOut(Location::SameAsFirstInput());
3563 }
3564}
3565
Calin Juravle2ae48182016-03-16 14:05:09 +00003566void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3567 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568 return;
3569 }
3570 Location obj = instruction->GetLocations()->InAt(0);
3571
3572 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003573 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003574}
3575
Calin Juravle2ae48182016-03-16 14:05:09 +00003576void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003577 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003578 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003579
3580 Location obj = instruction->GetLocations()->InAt(0);
3581
3582 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3583}
3584
3585void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003586 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003587}
3588
3589void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3590 HandleBinaryOp(instruction);
3591}
3592
3593void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3594 HandleBinaryOp(instruction);
3595}
3596
3597void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3598 LOG(FATAL) << "Unreachable";
3599}
3600
3601void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3602 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3603}
3604
3605void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3607 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3608 if (location.IsStackSlot()) {
3609 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3610 } else if (location.IsDoubleStackSlot()) {
3611 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3612 }
3613 locations->SetOut(location);
3614}
3615
3616void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3617 ATTRIBUTE_UNUSED) {
3618 // Nothing to do, the parameter is already at its location.
3619}
3620
3621void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3622 LocationSummary* locations =
3623 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3624 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3625}
3626
3627void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3628 ATTRIBUTE_UNUSED) {
3629 // Nothing to do, the method is already at its location.
3630}
3631
3632void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3634 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3635 locations->SetInAt(i, Location::Any());
3636 }
3637 locations->SetOut(Location::Any());
3638}
3639
3640void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3641 LOG(FATAL) << "Unreachable";
3642}
3643
3644void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3645 Primitive::Type type = rem->GetResultType();
3646 LocationSummary::CallKind call_kind =
3647 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3648 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3649
3650 switch (type) {
3651 case Primitive::kPrimInt:
3652 case Primitive::kPrimLong:
3653 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003654 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3656 break;
3657
3658 case Primitive::kPrimFloat:
3659 case Primitive::kPrimDouble: {
3660 InvokeRuntimeCallingConvention calling_convention;
3661 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3662 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3663 locations->SetOut(calling_convention.GetReturnLocation(type));
3664 break;
3665 }
3666
3667 default:
3668 LOG(FATAL) << "Unexpected rem type " << type;
3669 }
3670}
3671
3672void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3673 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003674
3675 switch (type) {
3676 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003677 case Primitive::kPrimLong:
3678 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003679 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003680
3681 case Primitive::kPrimFloat:
3682 case Primitive::kPrimDouble: {
3683 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3684 : QUICK_ENTRY_POINT(pFmod);
3685 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003686 if (type == Primitive::kPrimFloat) {
3687 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3688 } else {
3689 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3690 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003691 break;
3692 }
3693 default:
3694 LOG(FATAL) << "Unexpected rem type " << type;
3695 }
3696}
3697
3698void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3699 memory_barrier->SetLocations(nullptr);
3700}
3701
3702void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3703 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3704}
3705
3706void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3708 Primitive::Type return_type = ret->InputAt(0)->GetType();
3709 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3710}
3711
3712void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3713 codegen_->GenerateFrameExit();
3714}
3715
3716void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3717 ret->SetLocations(nullptr);
3718}
3719
3720void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3721 codegen_->GenerateFrameExit();
3722}
3723
Alexey Frunze92d90602015-12-18 18:16:36 -08003724void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3725 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003726}
3727
Alexey Frunze92d90602015-12-18 18:16:36 -08003728void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3729 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003730}
3731
Alexey Frunze4dda3372015-06-01 18:31:49 -07003732void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3733 HandleShift(shl);
3734}
3735
3736void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3737 HandleShift(shl);
3738}
3739
3740void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3741 HandleShift(shr);
3742}
3743
3744void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3745 HandleShift(shr);
3746}
3747
3748void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3750 Primitive::Type field_type = store->InputAt(1)->GetType();
3751 switch (field_type) {
3752 case Primitive::kPrimNot:
3753 case Primitive::kPrimBoolean:
3754 case Primitive::kPrimByte:
3755 case Primitive::kPrimChar:
3756 case Primitive::kPrimShort:
3757 case Primitive::kPrimInt:
3758 case Primitive::kPrimFloat:
3759 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3760 break;
3761
3762 case Primitive::kPrimLong:
3763 case Primitive::kPrimDouble:
3764 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3765 break;
3766
3767 default:
3768 LOG(FATAL) << "Unimplemented local type " << field_type;
3769 }
3770}
3771
3772void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3773}
3774
3775void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3776 HandleBinaryOp(instruction);
3777}
3778
3779void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3780 HandleBinaryOp(instruction);
3781}
3782
3783void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3784 HandleFieldGet(instruction, instruction->GetFieldInfo());
3785}
3786
3787void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3788 HandleFieldGet(instruction, instruction->GetFieldInfo());
3789}
3790
3791void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3792 HandleFieldSet(instruction, instruction->GetFieldInfo());
3793}
3794
3795void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003796 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003797}
3798
Calin Juravlee460d1d2015-09-29 04:52:17 +01003799void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3800 HUnresolvedInstanceFieldGet* instruction) {
3801 FieldAccessCallingConventionMIPS64 calling_convention;
3802 codegen_->CreateUnresolvedFieldLocationSummary(
3803 instruction, instruction->GetFieldType(), calling_convention);
3804}
3805
3806void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3807 HUnresolvedInstanceFieldGet* instruction) {
3808 FieldAccessCallingConventionMIPS64 calling_convention;
3809 codegen_->GenerateUnresolvedFieldAccess(instruction,
3810 instruction->GetFieldType(),
3811 instruction->GetFieldIndex(),
3812 instruction->GetDexPc(),
3813 calling_convention);
3814}
3815
3816void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3817 HUnresolvedInstanceFieldSet* instruction) {
3818 FieldAccessCallingConventionMIPS64 calling_convention;
3819 codegen_->CreateUnresolvedFieldLocationSummary(
3820 instruction, instruction->GetFieldType(), calling_convention);
3821}
3822
3823void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3824 HUnresolvedInstanceFieldSet* instruction) {
3825 FieldAccessCallingConventionMIPS64 calling_convention;
3826 codegen_->GenerateUnresolvedFieldAccess(instruction,
3827 instruction->GetFieldType(),
3828 instruction->GetFieldIndex(),
3829 instruction->GetDexPc(),
3830 calling_convention);
3831}
3832
3833void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3834 HUnresolvedStaticFieldGet* instruction) {
3835 FieldAccessCallingConventionMIPS64 calling_convention;
3836 codegen_->CreateUnresolvedFieldLocationSummary(
3837 instruction, instruction->GetFieldType(), calling_convention);
3838}
3839
3840void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3841 HUnresolvedStaticFieldGet* instruction) {
3842 FieldAccessCallingConventionMIPS64 calling_convention;
3843 codegen_->GenerateUnresolvedFieldAccess(instruction,
3844 instruction->GetFieldType(),
3845 instruction->GetFieldIndex(),
3846 instruction->GetDexPc(),
3847 calling_convention);
3848}
3849
3850void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3851 HUnresolvedStaticFieldSet* instruction) {
3852 FieldAccessCallingConventionMIPS64 calling_convention;
3853 codegen_->CreateUnresolvedFieldLocationSummary(
3854 instruction, instruction->GetFieldType(), calling_convention);
3855}
3856
3857void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3858 HUnresolvedStaticFieldSet* instruction) {
3859 FieldAccessCallingConventionMIPS64 calling_convention;
3860 codegen_->GenerateUnresolvedFieldAccess(instruction,
3861 instruction->GetFieldType(),
3862 instruction->GetFieldIndex(),
3863 instruction->GetDexPc(),
3864 calling_convention);
3865}
3866
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3868 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3869}
3870
3871void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3872 HBasicBlock* block = instruction->GetBlock();
3873 if (block->GetLoopInformation() != nullptr) {
3874 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3875 // The back edge will generate the suspend check.
3876 return;
3877 }
3878 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3879 // The goto will generate the suspend check.
3880 return;
3881 }
3882 GenerateSuspendCheck(instruction, nullptr);
3883}
3884
Alexey Frunze4dda3372015-06-01 18:31:49 -07003885void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3886 LocationSummary* locations =
3887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3888 InvokeRuntimeCallingConvention calling_convention;
3889 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3890}
3891
3892void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3893 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3894 instruction,
3895 instruction->GetDexPc(),
3896 nullptr);
3897 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3898}
3899
3900void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3901 Primitive::Type input_type = conversion->GetInputType();
3902 Primitive::Type result_type = conversion->GetResultType();
3903 DCHECK_NE(input_type, result_type);
3904
3905 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3906 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3907 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3908 }
3909
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3911
3912 if (Primitive::IsFloatingPointType(input_type)) {
3913 locations->SetInAt(0, Location::RequiresFpuRegister());
3914 } else {
3915 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003916 }
3917
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003918 if (Primitive::IsFloatingPointType(result_type)) {
3919 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003920 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003922 }
3923}
3924
3925void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3926 LocationSummary* locations = conversion->GetLocations();
3927 Primitive::Type result_type = conversion->GetResultType();
3928 Primitive::Type input_type = conversion->GetInputType();
3929
3930 DCHECK_NE(input_type, result_type);
3931
3932 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3933 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3934 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3935
3936 switch (result_type) {
3937 case Primitive::kPrimChar:
3938 __ Andi(dst, src, 0xFFFF);
3939 break;
3940 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003941 if (input_type == Primitive::kPrimLong) {
3942 // Type conversion from long to types narrower than int is a result of code
3943 // transformations. To avoid unpredictable results for SEB and SEH, we first
3944 // need to sign-extend the low 32-bit value into bits 32 through 63.
3945 __ Sll(dst, src, 0);
3946 __ Seb(dst, dst);
3947 } else {
3948 __ Seb(dst, src);
3949 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003950 break;
3951 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003952 if (input_type == Primitive::kPrimLong) {
3953 // Type conversion from long to types narrower than int is a result of code
3954 // transformations. To avoid unpredictable results for SEB and SEH, we first
3955 // need to sign-extend the low 32-bit value into bits 32 through 63.
3956 __ Sll(dst, src, 0);
3957 __ Seh(dst, dst);
3958 } else {
3959 __ Seh(dst, src);
3960 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003961 break;
3962 case Primitive::kPrimInt:
3963 case Primitive::kPrimLong:
3964 // Sign-extend 32-bit int into bits 32 through 63 for
3965 // int-to-long and long-to-int conversions
3966 __ Sll(dst, src, 0);
3967 break;
3968
3969 default:
3970 LOG(FATAL) << "Unexpected type conversion from " << input_type
3971 << " to " << result_type;
3972 }
3973 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003974 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3975 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3976 if (input_type == Primitive::kPrimLong) {
3977 __ Dmtc1(src, FTMP);
3978 if (result_type == Primitive::kPrimFloat) {
3979 __ Cvtsl(dst, FTMP);
3980 } else {
3981 __ Cvtdl(dst, FTMP);
3982 }
3983 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003984 __ Mtc1(src, FTMP);
3985 if (result_type == Primitive::kPrimFloat) {
3986 __ Cvtsw(dst, FTMP);
3987 } else {
3988 __ Cvtdw(dst, FTMP);
3989 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003990 }
3991 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3992 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003993 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3994 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3995 Mips64Label truncate;
3996 Mips64Label done;
3997
3998 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3999 // value when the input is either a NaN or is outside of the range of the output type
4000 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4001 // the same result.
4002 //
4003 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4004 // value of the output type if the input is outside of the range after the truncation or
4005 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4006 // results. This matches the desired float/double-to-int/long conversion exactly.
4007 //
4008 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4009 //
4010 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4011 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4012 // even though it must be NAN2008=1 on R6.
4013 //
4014 // The code takes care of the different behaviors by first comparing the input to the
4015 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4016 // If the input is greater than or equal to the minimum, it procedes to the truncate
4017 // instruction, which will handle such an input the same way irrespective of NAN2008.
4018 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4019 // in order to return either zero or the minimum value.
4020 //
4021 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4022 // truncate instruction for MIPS64R6.
4023 if (input_type == Primitive::kPrimFloat) {
4024 uint32_t min_val = (result_type == Primitive::kPrimLong)
4025 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
4026 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4027 __ LoadConst32(TMP, min_val);
4028 __ Mtc1(TMP, FTMP);
4029 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004030 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004031 uint64_t min_val = (result_type == Primitive::kPrimLong)
4032 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
4033 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4034 __ LoadConst64(TMP, min_val);
4035 __ Dmtc1(TMP, FTMP);
4036 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004037 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004038
4039 __ Bc1nez(FTMP, &truncate);
4040
4041 if (input_type == Primitive::kPrimFloat) {
4042 __ CmpEqS(FTMP, src, src);
4043 } else {
4044 __ CmpEqD(FTMP, src, src);
4045 }
4046 if (result_type == Primitive::kPrimLong) {
4047 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
4048 } else {
4049 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4050 }
4051 __ Mfc1(TMP, FTMP);
4052 __ And(dst, dst, TMP);
4053
4054 __ Bc(&done);
4055
4056 __ Bind(&truncate);
4057
4058 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00004059 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004060 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004061 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004062 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004063 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004064 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004065 } else {
4066 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004067 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004068 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004069 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004070 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004071 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004072 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004073
4074 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004075 } else if (Primitive::IsFloatingPointType(result_type) &&
4076 Primitive::IsFloatingPointType(input_type)) {
4077 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4078 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4079 if (result_type == Primitive::kPrimFloat) {
4080 __ Cvtsd(dst, src);
4081 } else {
4082 __ Cvtds(dst, src);
4083 }
4084 } else {
4085 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4086 << " to " << result_type;
4087 }
4088}
4089
4090void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4091 HandleShift(ushr);
4092}
4093
4094void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4095 HandleShift(ushr);
4096}
4097
4098void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4099 HandleBinaryOp(instruction);
4100}
4101
4102void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4103 HandleBinaryOp(instruction);
4104}
4105
4106void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4107 // Nothing to do, this should be removed during prepare for register allocator.
4108 LOG(FATAL) << "Unreachable";
4109}
4110
4111void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4112 // Nothing to do, this should be removed during prepare for register allocator.
4113 LOG(FATAL) << "Unreachable";
4114}
4115
4116void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004117 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004118}
4119
4120void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004121 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004122}
4123
4124void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004125 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004126}
4127
4128void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004129 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004130}
4131
4132void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004133 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004134}
4135
4136void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004137 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004138}
4139
4140void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004141 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004142}
4143
4144void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004145 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004146}
4147
4148void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004149 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004150}
4151
4152void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004153 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004154}
4155
4156void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004157 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004158}
4159
4160void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004161 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004162}
4163
Aart Bike9f37602015-10-09 11:15:55 -07004164void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004165 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004166}
4167
4168void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004169 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004170}
4171
4172void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004173 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004174}
4175
4176void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004177 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004178}
4179
4180void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004181 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004182}
4183
4184void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004185 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004186}
4187
4188void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004189 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004190}
4191
4192void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004193 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004194}
4195
Mark Mendellfe57faa2015-09-18 09:26:15 -04004196// Simple implementation of packed switch - generate cascaded compare/jumps.
4197void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4198 LocationSummary* locations =
4199 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4200 locations->SetInAt(0, Location::RequiresRegister());
4201}
4202
4203void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4204 int32_t lower_bound = switch_instr->GetStartValue();
4205 int32_t num_entries = switch_instr->GetNumEntries();
4206 LocationSummary* locations = switch_instr->GetLocations();
4207 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4208 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4209
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004210 // Create a set of compare/jumps.
4211 GpuRegister temp_reg = TMP;
4212 if (IsInt<16>(-lower_bound)) {
4213 __ Addiu(temp_reg, value_reg, -lower_bound);
4214 } else {
4215 __ LoadConst32(AT, -lower_bound);
4216 __ Addu(temp_reg, value_reg, AT);
4217 }
4218 // Jump to default if index is negative
4219 // Note: We don't check the case that index is positive while value < lower_bound, because in
4220 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4221 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4222
Mark Mendellfe57faa2015-09-18 09:26:15 -04004223 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004224 // Jump to successors[0] if value == lower_bound.
4225 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4226 int32_t last_index = 0;
4227 for (; num_entries - last_index > 2; last_index += 2) {
4228 __ Addiu(temp_reg, temp_reg, -2);
4229 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4230 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4231 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4232 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4233 }
4234 if (num_entries - last_index == 2) {
4235 // The last missing case_value.
4236 __ Addiu(temp_reg, temp_reg, -1);
4237 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004238 }
4239
4240 // And the default for any other value.
4241 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004242 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004243 }
4244}
4245
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004246void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4247 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4248}
4249
4250void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4251 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4252}
4253
Alexey Frunze4dda3372015-06-01 18:31:49 -07004254} // namespace mips64
4255} // namespace art