blob: 56ac38ef84c9c6200ca22459db122f1ab97934fd [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),
Vladimir Marko93205e32016-04-13 11:59:46 +0100420 assembler_(graph->GetArena()),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700421 isa_features_(isa_features) {
422 // Save RA (containing the return address) to mimic Quick.
423 AddAllocatedRegister(Location::RegisterLocation(RA));
424}
425
426#undef __
427#define __ down_cast<Mips64Assembler*>(GetAssembler())->
Lazar Trsicd9672662015-09-03 17:33:01 +0200428#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64DoublewordSize, x).Int32Value()
Alexey Frunze4dda3372015-06-01 18:31:49 -0700429
430void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700431 // Ensure that we fix up branches.
432 __ FinalizeCode();
433
434 // Adjust native pc offsets in stack maps.
435 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
436 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
437 uint32_t new_position = __ GetAdjustedPosition(old_position);
438 DCHECK_GE(new_position, old_position);
439 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
440 }
441
442 // Adjust pc offsets for the disassembly information.
443 if (disasm_info_ != nullptr) {
444 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
445 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
446 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
447 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
448 it.second.start = __ GetAdjustedPosition(it.second.start);
449 it.second.end = __ GetAdjustedPosition(it.second.end);
450 }
451 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
452 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
453 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
454 }
455 }
456
Alexey Frunze4dda3372015-06-01 18:31:49 -0700457 CodeGenerator::Finalize(allocator);
458}
459
460Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
461 return codegen_->GetAssembler();
462}
463
464void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100465 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700466 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
467}
468
469void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100470 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700471 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
472}
473
474void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
475 // Pop reg
476 __ Ld(GpuRegister(reg), SP, 0);
Lazar Trsicd9672662015-09-03 17:33:01 +0200477 __ DecreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700478}
479
480void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
481 // Push reg
Lazar Trsicd9672662015-09-03 17:33:01 +0200482 __ IncreaseFrameSize(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700483 __ Sd(GpuRegister(reg), SP, 0);
484}
485
486void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
487 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
488 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
489 // Allocate a scratch register other than TMP, if available.
490 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
491 // automatically unspilled when the scratch scope object is destroyed).
492 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
493 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Lazar Trsicd9672662015-09-03 17:33:01 +0200494 int stack_offset = ensure_scratch.IsSpilled() ? kMips64DoublewordSize : 0;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700495 __ LoadFromOffset(load_type,
496 GpuRegister(ensure_scratch.GetRegister()),
497 SP,
498 index1 + stack_offset);
499 __ LoadFromOffset(load_type,
500 TMP,
501 SP,
502 index2 + stack_offset);
503 __ StoreToOffset(store_type,
504 GpuRegister(ensure_scratch.GetRegister()),
505 SP,
506 index2 + stack_offset);
507 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
508}
509
510static dwarf::Reg DWARFReg(GpuRegister reg) {
511 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
512}
513
David Srbeckyba702002016-02-01 18:15:29 +0000514static dwarf::Reg DWARFReg(FpuRegister reg) {
515 return dwarf::Reg::Mips64Fp(static_cast<int>(reg));
516}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700517
518void CodeGeneratorMIPS64::GenerateFrameEntry() {
519 __ Bind(&frame_entry_label_);
520
521 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
522
523 if (do_overflow_check) {
524 __ LoadFromOffset(kLoadWord,
525 ZERO,
526 SP,
527 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
528 RecordPcInfo(nullptr, 0);
529 }
530
531 // TODO: anything related to T9/GP/GOT/PIC/.so's?
532
533 if (HasEmptyFrame()) {
534 return;
535 }
536
537 // Make sure the frame size isn't unreasonably large. Per the various APIs
538 // it looks like it should always be less than 2GB in size, which allows
539 // us using 32-bit signed offsets from the stack pointer.
540 if (GetFrameSize() > 0x7FFFFFFF)
541 LOG(FATAL) << "Stack frame larger than 2GB";
542
543 // Spill callee-saved registers.
544 // Note that their cumulative size is small and they can be indexed using
545 // 16-bit offsets.
546
547 // TODO: increment/decrement SP in one step instead of two or remove this comment.
548
549 uint32_t ofs = FrameEntrySpillSize();
550 __ IncreaseFrameSize(ofs);
551
552 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
553 GpuRegister reg = kCoreCalleeSaves[i];
554 if (allocated_registers_.ContainsCoreRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200555 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700556 __ Sd(reg, SP, ofs);
557 __ cfi().RelOffset(DWARFReg(reg), ofs);
558 }
559 }
560
561 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
562 FpuRegister reg = kFpuCalleeSaves[i];
563 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200564 ofs -= kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700565 __ Sdc1(reg, SP, ofs);
David Srbeckyba702002016-02-01 18:15:29 +0000566 __ cfi().RelOffset(DWARFReg(reg), ofs);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700567 }
568 }
569
570 // Allocate the rest of the frame and store the current method pointer
571 // at its end.
572
573 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
574
575 static_assert(IsInt<16>(kCurrentMethodStackOffset),
576 "kCurrentMethodStackOffset must fit into int16_t");
577 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
578}
579
580void CodeGeneratorMIPS64::GenerateFrameExit() {
581 __ cfi().RememberState();
582
583 // TODO: anything related to T9/GP/GOT/PIC/.so's?
584
585 if (!HasEmptyFrame()) {
586 // Deallocate the rest of the frame.
587
588 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
589
590 // Restore callee-saved registers.
591 // Note that their cumulative size is small and they can be indexed using
592 // 16-bit offsets.
593
594 // TODO: increment/decrement SP in one step instead of two or remove this comment.
595
596 uint32_t ofs = 0;
597
598 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
599 FpuRegister reg = kFpuCalleeSaves[i];
600 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
601 __ Ldc1(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200602 ofs += kMips64DoublewordSize;
David Srbeckyba702002016-02-01 18:15:29 +0000603 __ cfi().Restore(DWARFReg(reg));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700604 }
605 }
606
607 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
608 GpuRegister reg = kCoreCalleeSaves[i];
609 if (allocated_registers_.ContainsCoreRegister(reg)) {
610 __ Ld(reg, SP, ofs);
Lazar Trsicd9672662015-09-03 17:33:01 +0200611 ofs += kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700612 __ cfi().Restore(DWARFReg(reg));
613 }
614 }
615
616 DCHECK_EQ(ofs, FrameEntrySpillSize());
617 __ DecreaseFrameSize(ofs);
618 }
619
620 __ Jr(RA);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700621 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700622
623 __ cfi().RestoreState();
624 __ cfi().DefCFAOffset(GetFrameSize());
625}
626
627void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
628 __ Bind(GetLabelOf(block));
629}
630
631void CodeGeneratorMIPS64::MoveLocation(Location destination,
632 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100633 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700634 if (source.Equals(destination)) {
635 return;
636 }
637
638 // A valid move can always be inferred from the destination and source
639 // locations. When moving from and to a register, the argument type can be
640 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100641 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700642 DCHECK_EQ(unspecified_type, false);
643
644 if (destination.IsRegister() || destination.IsFpuRegister()) {
645 if (unspecified_type) {
646 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
647 if (source.IsStackSlot() ||
648 (src_cst != nullptr && (src_cst->IsIntConstant()
649 || src_cst->IsFloatConstant()
650 || src_cst->IsNullConstant()))) {
651 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100652 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700653 } else {
654 // If the source is a double stack slot or a 64bit constant, a 64bit
655 // type is appropriate. Else the source is a register, and since the
656 // type has not been specified, we chose a 64bit type to force a 64bit
657 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100658 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700659 }
660 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100661 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
662 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700663 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
664 // Move to GPR/FPR from stack
665 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100666 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700667 __ LoadFpuFromOffset(load_type,
668 destination.AsFpuRegister<FpuRegister>(),
669 SP,
670 source.GetStackIndex());
671 } else {
672 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
673 __ LoadFromOffset(load_type,
674 destination.AsRegister<GpuRegister>(),
675 SP,
676 source.GetStackIndex());
677 }
678 } else if (source.IsConstant()) {
679 // Move to GPR/FPR from constant
680 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100681 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700682 gpr = destination.AsRegister<GpuRegister>();
683 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100684 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700685 int32_t value = GetInt32ValueOf(source.GetConstant()->AsConstant());
686 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
687 gpr = ZERO;
688 } else {
689 __ LoadConst32(gpr, value);
690 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700691 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700692 int64_t value = GetInt64ValueOf(source.GetConstant()->AsConstant());
693 if (Primitive::IsFloatingPointType(dst_type) && value == 0) {
694 gpr = ZERO;
695 } else {
696 __ LoadConst64(gpr, value);
697 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700698 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100699 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700700 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100701 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
703 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100704 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700705 if (destination.IsRegister()) {
706 // Move to GPR from GPR
707 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
708 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100709 DCHECK(destination.IsFpuRegister());
710 if (Primitive::Is64BitType(dst_type)) {
711 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
712 } else {
713 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
714 }
715 }
716 } else if (source.IsFpuRegister()) {
717 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700718 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100719 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700720 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
721 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100722 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700723 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
724 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100725 } else {
726 DCHECK(destination.IsRegister());
727 if (Primitive::Is64BitType(dst_type)) {
728 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
729 } else {
730 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
731 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700732 }
733 }
734 } else { // The destination is not a register. It must be a stack slot.
735 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
736 if (source.IsRegister() || source.IsFpuRegister()) {
737 if (unspecified_type) {
738 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100739 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700740 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100741 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700742 }
743 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100744 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
745 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700746 // Move to stack from GPR/FPR
747 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
748 if (source.IsRegister()) {
749 __ StoreToOffset(store_type,
750 source.AsRegister<GpuRegister>(),
751 SP,
752 destination.GetStackIndex());
753 } else {
754 __ StoreFpuToOffset(store_type,
755 source.AsFpuRegister<FpuRegister>(),
756 SP,
757 destination.GetStackIndex());
758 }
759 } else if (source.IsConstant()) {
760 // Move to stack from constant
761 HConstant* src_cst = source.GetConstant();
762 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700763 GpuRegister gpr = ZERO;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700764 if (destination.IsStackSlot()) {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700765 int32_t value = GetInt32ValueOf(src_cst->AsConstant());
766 if (value != 0) {
767 gpr = TMP;
768 __ LoadConst32(gpr, value);
769 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700770 } else {
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700771 DCHECK(destination.IsDoubleStackSlot());
772 int64_t value = GetInt64ValueOf(src_cst->AsConstant());
773 if (value != 0) {
774 gpr = TMP;
775 __ LoadConst64(gpr, value);
776 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700777 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700778 __ StoreToOffset(store_type, gpr, SP, destination.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700779 } else {
780 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
781 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
782 // Move to stack from stack
783 if (destination.IsStackSlot()) {
784 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
785 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
786 } else {
787 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
788 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
789 }
790 }
791 }
792}
793
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700794void CodeGeneratorMIPS64::SwapLocations(Location loc1, Location loc2, Primitive::Type type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700795 DCHECK(!loc1.IsConstant());
796 DCHECK(!loc2.IsConstant());
797
798 if (loc1.Equals(loc2)) {
799 return;
800 }
801
802 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
803 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
804 bool is_fp_reg1 = loc1.IsFpuRegister();
805 bool is_fp_reg2 = loc2.IsFpuRegister();
806
807 if (loc2.IsRegister() && loc1.IsRegister()) {
808 // Swap 2 GPRs
809 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
810 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
811 __ Move(TMP, r2);
812 __ Move(r2, r1);
813 __ Move(r1, TMP);
814 } else if (is_fp_reg2 && is_fp_reg1) {
815 // Swap 2 FPRs
816 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
817 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -0700818 if (type == Primitive::kPrimFloat) {
819 __ MovS(FTMP, r1);
820 __ MovS(r1, r2);
821 __ MovS(r2, FTMP);
822 } else {
823 DCHECK_EQ(type, Primitive::kPrimDouble);
824 __ MovD(FTMP, r1);
825 __ MovD(r1, r2);
826 __ MovD(r2, FTMP);
827 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700828 } else if (is_slot1 != is_slot2) {
829 // Swap GPR/FPR and stack slot
830 Location reg_loc = is_slot1 ? loc2 : loc1;
831 Location mem_loc = is_slot1 ? loc1 : loc2;
832 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
833 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
834 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
835 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
836 if (reg_loc.IsFpuRegister()) {
837 __ StoreFpuToOffset(store_type,
838 reg_loc.AsFpuRegister<FpuRegister>(),
839 SP,
840 mem_loc.GetStackIndex());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700841 if (mem_loc.IsStackSlot()) {
842 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
843 } else {
844 DCHECK(mem_loc.IsDoubleStackSlot());
845 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
846 }
847 } else {
848 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
849 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
850 }
851 } else if (is_slot1 && is_slot2) {
852 move_resolver_.Exchange(loc1.GetStackIndex(),
853 loc2.GetStackIndex(),
854 loc1.IsDoubleStackSlot());
855 } else {
856 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
857 }
858}
859
Calin Juravle175dc732015-08-25 15:42:32 +0100860void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
861 DCHECK(location.IsRegister());
862 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
863}
864
Calin Juravlee460d1d2015-09-29 04:52:17 +0100865void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
866 if (location.IsRegister()) {
867 locations->AddTemp(location);
868 } else {
869 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
870 }
871}
872
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100873void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object,
874 GpuRegister value,
875 bool value_can_be_null) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700876 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700877 GpuRegister card = AT;
878 GpuRegister temp = TMP;
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100879 if (value_can_be_null) {
880 __ Beqzc(value, &done);
881 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700882 __ LoadFromOffset(kLoadDoubleword,
883 card,
884 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +0200885 Thread::CardTableOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -0700886 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
887 __ Daddu(temp, card, temp);
888 __ Sb(card, temp, 0);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +0100889 if (value_can_be_null) {
890 __ Bind(&done);
891 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700892}
893
David Brazdil58282f42016-01-14 12:45:10 +0000894void CodeGeneratorMIPS64::SetupBlockedRegisters() const {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700895 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
896 blocked_core_registers_[ZERO] = true;
897 blocked_core_registers_[K0] = true;
898 blocked_core_registers_[K1] = true;
899 blocked_core_registers_[GP] = true;
900 blocked_core_registers_[SP] = true;
901 blocked_core_registers_[RA] = true;
902
Lazar Trsicd9672662015-09-03 17:33:01 +0200903 // AT, TMP(T8) and TMP2(T3) are used as temporary/scratch
904 // registers (similar to how AT is used by MIPS assemblers).
Alexey Frunze4dda3372015-06-01 18:31:49 -0700905 blocked_core_registers_[AT] = true;
906 blocked_core_registers_[TMP] = true;
Lazar Trsicd9672662015-09-03 17:33:01 +0200907 blocked_core_registers_[TMP2] = true;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700908 blocked_fpu_registers_[FTMP] = true;
909
910 // Reserve suspend and thread registers.
911 blocked_core_registers_[S0] = true;
912 blocked_core_registers_[TR] = true;
913
914 // Reserve T9 for function calls
915 blocked_core_registers_[T9] = true;
916
917 // TODO: review; anything else?
918
David Brazdil58282f42016-01-14 12:45:10 +0000919 // TODO: remove once all the issues with register saving/restoring are sorted out.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700920 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
921 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
922 }
923
924 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
925 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
926 }
927}
928
Alexey Frunze4dda3372015-06-01 18:31:49 -0700929size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
930 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200931 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700932}
933
934size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
935 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200936 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700937}
938
939size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
940 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200941 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700942}
943
944size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
945 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
Lazar Trsicd9672662015-09-03 17:33:01 +0200946 return kMips64DoublewordSize;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700947}
948
949void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100950 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700951}
952
953void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100954 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700955}
956
Calin Juravle175dc732015-08-25 15:42:32 +0100957void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
958 HInstruction* instruction,
959 uint32_t dex_pc,
960 SlowPathCode* slow_path) {
Lazar Trsicd9672662015-09-03 17:33:01 +0200961 InvokeRuntime(GetThreadOffset<kMips64DoublewordSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +0100962 instruction,
963 dex_pc,
964 slow_path);
965}
966
Alexey Frunze4dda3372015-06-01 18:31:49 -0700967void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
968 HInstruction* instruction,
969 uint32_t dex_pc,
970 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100971 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700972 // TODO: anything related to T9/GP/GOT/PIC/.so's?
973 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
974 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -0700975 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700976 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700977}
978
979void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
980 GpuRegister class_reg) {
981 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
982 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
983 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
984 // TODO: barrier needed?
985 __ Bind(slow_path->GetExitLabel());
986}
987
988void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
989 __ Sync(0); // only stype 0 is supported
990}
991
992void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
993 HBasicBlock* successor) {
994 SuspendCheckSlowPathMIPS64* slow_path =
995 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
996 codegen_->AddSlowPath(slow_path);
997
998 __ LoadFromOffset(kLoadUnsignedHalfword,
999 TMP,
1000 TR,
Lazar Trsicd9672662015-09-03 17:33:01 +02001001 Thread::ThreadFlagsOffset<kMips64DoublewordSize>().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001002 if (successor == nullptr) {
1003 __ Bnezc(TMP, slow_path->GetEntryLabel());
1004 __ Bind(slow_path->GetReturnLabel());
1005 } else {
1006 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001007 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001008 // slow_path will return to GetLabelOf(successor).
1009 }
1010}
1011
1012InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1013 CodeGeneratorMIPS64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001014 : InstructionCodeGenerator(graph, codegen),
Alexey Frunze4dda3372015-06-01 18:31:49 -07001015 assembler_(codegen->GetAssembler()),
1016 codegen_(codegen) {}
1017
1018void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1019 DCHECK_EQ(instruction->InputCount(), 2U);
1020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1021 Primitive::Type type = instruction->GetResultType();
1022 switch (type) {
1023 case Primitive::kPrimInt:
1024 case Primitive::kPrimLong: {
1025 locations->SetInAt(0, Location::RequiresRegister());
1026 HInstruction* right = instruction->InputAt(1);
1027 bool can_use_imm = false;
1028 if (right->IsConstant()) {
1029 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1030 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1031 can_use_imm = IsUint<16>(imm);
1032 } else if (instruction->IsAdd()) {
1033 can_use_imm = IsInt<16>(imm);
1034 } else {
1035 DCHECK(instruction->IsSub());
1036 can_use_imm = IsInt<16>(-imm);
1037 }
1038 }
1039 if (can_use_imm)
1040 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1041 else
1042 locations->SetInAt(1, Location::RequiresRegister());
1043 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1044 }
1045 break;
1046
1047 case Primitive::kPrimFloat:
1048 case Primitive::kPrimDouble:
1049 locations->SetInAt(0, Location::RequiresFpuRegister());
1050 locations->SetInAt(1, Location::RequiresFpuRegister());
1051 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1052 break;
1053
1054 default:
1055 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1056 }
1057}
1058
1059void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1060 Primitive::Type type = instruction->GetType();
1061 LocationSummary* locations = instruction->GetLocations();
1062
1063 switch (type) {
1064 case Primitive::kPrimInt:
1065 case Primitive::kPrimLong: {
1066 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1067 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1068 Location rhs_location = locations->InAt(1);
1069
1070 GpuRegister rhs_reg = ZERO;
1071 int64_t rhs_imm = 0;
1072 bool use_imm = rhs_location.IsConstant();
1073 if (use_imm) {
1074 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1075 } else {
1076 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1077 }
1078
1079 if (instruction->IsAnd()) {
1080 if (use_imm)
1081 __ Andi(dst, lhs, rhs_imm);
1082 else
1083 __ And(dst, lhs, rhs_reg);
1084 } else if (instruction->IsOr()) {
1085 if (use_imm)
1086 __ Ori(dst, lhs, rhs_imm);
1087 else
1088 __ Or(dst, lhs, rhs_reg);
1089 } else if (instruction->IsXor()) {
1090 if (use_imm)
1091 __ Xori(dst, lhs, rhs_imm);
1092 else
1093 __ Xor(dst, lhs, rhs_reg);
1094 } else if (instruction->IsAdd()) {
1095 if (type == Primitive::kPrimInt) {
1096 if (use_imm)
1097 __ Addiu(dst, lhs, rhs_imm);
1098 else
1099 __ Addu(dst, lhs, rhs_reg);
1100 } else {
1101 if (use_imm)
1102 __ Daddiu(dst, lhs, rhs_imm);
1103 else
1104 __ Daddu(dst, lhs, rhs_reg);
1105 }
1106 } else {
1107 DCHECK(instruction->IsSub());
1108 if (type == Primitive::kPrimInt) {
1109 if (use_imm)
1110 __ Addiu(dst, lhs, -rhs_imm);
1111 else
1112 __ Subu(dst, lhs, rhs_reg);
1113 } else {
1114 if (use_imm)
1115 __ Daddiu(dst, lhs, -rhs_imm);
1116 else
1117 __ Dsubu(dst, lhs, rhs_reg);
1118 }
1119 }
1120 break;
1121 }
1122 case Primitive::kPrimFloat:
1123 case Primitive::kPrimDouble: {
1124 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1125 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1126 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1127 if (instruction->IsAdd()) {
1128 if (type == Primitive::kPrimFloat)
1129 __ AddS(dst, lhs, rhs);
1130 else
1131 __ AddD(dst, lhs, rhs);
1132 } else if (instruction->IsSub()) {
1133 if (type == Primitive::kPrimFloat)
1134 __ SubS(dst, lhs, rhs);
1135 else
1136 __ SubD(dst, lhs, rhs);
1137 } else {
1138 LOG(FATAL) << "Unexpected floating-point binary operation";
1139 }
1140 break;
1141 }
1142 default:
1143 LOG(FATAL) << "Unexpected binary operation type " << type;
1144 }
1145}
1146
1147void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001148 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001149
1150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1151 Primitive::Type type = instr->GetResultType();
1152 switch (type) {
1153 case Primitive::kPrimInt:
1154 case Primitive::kPrimLong: {
1155 locations->SetInAt(0, Location::RequiresRegister());
1156 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001157 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001158 break;
1159 }
1160 default:
1161 LOG(FATAL) << "Unexpected shift type " << type;
1162 }
1163}
1164
1165void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001166 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001167 LocationSummary* locations = instr->GetLocations();
1168 Primitive::Type type = instr->GetType();
1169
1170 switch (type) {
1171 case Primitive::kPrimInt:
1172 case Primitive::kPrimLong: {
1173 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1174 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1175 Location rhs_location = locations->InAt(1);
1176
1177 GpuRegister rhs_reg = ZERO;
1178 int64_t rhs_imm = 0;
1179 bool use_imm = rhs_location.IsConstant();
1180 if (use_imm) {
1181 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1182 } else {
1183 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1184 }
1185
1186 if (use_imm) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001187 uint32_t shift_value = rhs_imm &
1188 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001189
Alexey Frunze92d90602015-12-18 18:16:36 -08001190 if (shift_value == 0) {
1191 if (dst != lhs) {
1192 __ Move(dst, lhs);
1193 }
1194 } else if (type == Primitive::kPrimInt) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001195 if (instr->IsShl()) {
1196 __ Sll(dst, lhs, shift_value);
1197 } else if (instr->IsShr()) {
1198 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001199 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001200 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001201 } else {
1202 __ Rotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001203 }
1204 } else {
1205 if (shift_value < 32) {
1206 if (instr->IsShl()) {
1207 __ Dsll(dst, lhs, shift_value);
1208 } else if (instr->IsShr()) {
1209 __ Dsra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001210 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001211 __ Dsrl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001212 } else {
1213 __ Drotr(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001214 }
1215 } else {
1216 shift_value -= 32;
1217 if (instr->IsShl()) {
1218 __ Dsll32(dst, lhs, shift_value);
1219 } else if (instr->IsShr()) {
1220 __ Dsra32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001221 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001222 __ Dsrl32(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001223 } else {
1224 __ Drotr32(dst, lhs, shift_value);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001225 }
1226 }
1227 }
1228 } else {
1229 if (type == Primitive::kPrimInt) {
1230 if (instr->IsShl()) {
1231 __ Sllv(dst, lhs, rhs_reg);
1232 } else if (instr->IsShr()) {
1233 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001234 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001235 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001236 } else {
1237 __ Rotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001238 }
1239 } else {
1240 if (instr->IsShl()) {
1241 __ Dsllv(dst, lhs, rhs_reg);
1242 } else if (instr->IsShr()) {
1243 __ Dsrav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001244 } else if (instr->IsUShr()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001245 __ Dsrlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001246 } else {
1247 __ Drotrv(dst, lhs, rhs_reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001248 }
1249 }
1250 }
1251 break;
1252 }
1253 default:
1254 LOG(FATAL) << "Unexpected shift operation type " << type;
1255 }
1256}
1257
1258void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1259 HandleBinaryOp(instruction);
1260}
1261
1262void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1263 HandleBinaryOp(instruction);
1264}
1265
1266void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1267 HandleBinaryOp(instruction);
1268}
1269
1270void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1271 HandleBinaryOp(instruction);
1272}
1273
1274void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1275 LocationSummary* locations =
1276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1277 locations->SetInAt(0, Location::RequiresRegister());
1278 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1279 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1280 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1281 } else {
1282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1283 }
1284}
1285
1286void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1287 LocationSummary* locations = instruction->GetLocations();
1288 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1289 Location index = locations->InAt(1);
1290 Primitive::Type type = instruction->GetType();
1291
1292 switch (type) {
1293 case Primitive::kPrimBoolean: {
1294 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1295 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1296 if (index.IsConstant()) {
1297 size_t offset =
1298 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1299 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1300 } else {
1301 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1302 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1303 }
1304 break;
1305 }
1306
1307 case Primitive::kPrimByte: {
1308 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1309 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1310 if (index.IsConstant()) {
1311 size_t offset =
1312 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1313 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1314 } else {
1315 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1316 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1317 }
1318 break;
1319 }
1320
1321 case Primitive::kPrimShort: {
1322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1323 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1324 if (index.IsConstant()) {
1325 size_t offset =
1326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1327 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1328 } else {
1329 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1330 __ Daddu(TMP, obj, TMP);
1331 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1332 }
1333 break;
1334 }
1335
1336 case Primitive::kPrimChar: {
1337 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1338 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1339 if (index.IsConstant()) {
1340 size_t offset =
1341 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1342 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1343 } else {
1344 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1345 __ Daddu(TMP, obj, TMP);
1346 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1347 }
1348 break;
1349 }
1350
1351 case Primitive::kPrimInt:
1352 case Primitive::kPrimNot: {
1353 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1355 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1356 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1357 if (index.IsConstant()) {
1358 size_t offset =
1359 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1360 __ LoadFromOffset(load_type, out, obj, offset);
1361 } else {
1362 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1363 __ Daddu(TMP, obj, TMP);
1364 __ LoadFromOffset(load_type, out, TMP, data_offset);
1365 }
1366 break;
1367 }
1368
1369 case Primitive::kPrimLong: {
1370 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1371 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1372 if (index.IsConstant()) {
1373 size_t offset =
1374 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1375 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1376 } else {
1377 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1378 __ Daddu(TMP, obj, TMP);
1379 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1380 }
1381 break;
1382 }
1383
1384 case Primitive::kPrimFloat: {
1385 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1386 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1387 if (index.IsConstant()) {
1388 size_t offset =
1389 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1390 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1391 } else {
1392 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1393 __ Daddu(TMP, obj, TMP);
1394 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1395 }
1396 break;
1397 }
1398
1399 case Primitive::kPrimDouble: {
1400 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1401 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1402 if (index.IsConstant()) {
1403 size_t offset =
1404 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1405 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1406 } else {
1407 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1408 __ Daddu(TMP, obj, TMP);
1409 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1410 }
1411 break;
1412 }
1413
1414 case Primitive::kPrimVoid:
1415 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1416 UNREACHABLE();
1417 }
1418 codegen_->MaybeRecordImplicitNullCheck(instruction);
1419}
1420
1421void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1423 locations->SetInAt(0, Location::RequiresRegister());
1424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1425}
1426
1427void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1428 LocationSummary* locations = instruction->GetLocations();
1429 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1430 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1431 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1432 __ LoadFromOffset(kLoadWord, out, obj, offset);
1433 codegen_->MaybeRecordImplicitNullCheck(instruction);
1434}
1435
1436void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001437 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001438 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1439 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001440 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1441 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001442 InvokeRuntimeCallingConvention calling_convention;
1443 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1444 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1445 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1446 } else {
1447 locations->SetInAt(0, Location::RequiresRegister());
1448 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1449 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1450 locations->SetInAt(2, Location::RequiresFpuRegister());
1451 } else {
1452 locations->SetInAt(2, Location::RequiresRegister());
1453 }
1454 }
1455}
1456
1457void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1458 LocationSummary* locations = instruction->GetLocations();
1459 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1460 Location index = locations->InAt(1);
1461 Primitive::Type value_type = instruction->GetComponentType();
1462 bool needs_runtime_call = locations->WillCall();
1463 bool needs_write_barrier =
1464 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1465
1466 switch (value_type) {
1467 case Primitive::kPrimBoolean:
1468 case Primitive::kPrimByte: {
1469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1470 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1471 if (index.IsConstant()) {
1472 size_t offset =
1473 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1474 __ StoreToOffset(kStoreByte, value, obj, offset);
1475 } else {
1476 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1477 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1478 }
1479 break;
1480 }
1481
1482 case Primitive::kPrimShort:
1483 case Primitive::kPrimChar: {
1484 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1485 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1486 if (index.IsConstant()) {
1487 size_t offset =
1488 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1489 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1490 } else {
1491 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1492 __ Daddu(TMP, obj, TMP);
1493 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1494 }
1495 break;
1496 }
1497
1498 case Primitive::kPrimInt:
1499 case Primitive::kPrimNot: {
1500 if (!needs_runtime_call) {
1501 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1502 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1503 if (index.IsConstant()) {
1504 size_t offset =
1505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1506 __ StoreToOffset(kStoreWord, value, obj, offset);
1507 } else {
1508 DCHECK(index.IsRegister()) << index;
1509 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1510 __ Daddu(TMP, obj, TMP);
1511 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1512 }
1513 codegen_->MaybeRecordImplicitNullCheck(instruction);
1514 if (needs_write_barrier) {
1515 DCHECK_EQ(value_type, Primitive::kPrimNot);
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01001516 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001517 }
1518 } else {
1519 DCHECK_EQ(value_type, Primitive::kPrimNot);
1520 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1521 instruction,
1522 instruction->GetDexPc(),
1523 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00001524 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001525 }
1526 break;
1527 }
1528
1529 case Primitive::kPrimLong: {
1530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1531 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1532 if (index.IsConstant()) {
1533 size_t offset =
1534 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1535 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1536 } else {
1537 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1538 __ Daddu(TMP, obj, TMP);
1539 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1540 }
1541 break;
1542 }
1543
1544 case Primitive::kPrimFloat: {
1545 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1546 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1547 DCHECK(locations->InAt(2).IsFpuRegister());
1548 if (index.IsConstant()) {
1549 size_t offset =
1550 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1551 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1552 } else {
1553 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1554 __ Daddu(TMP, obj, TMP);
1555 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1556 }
1557 break;
1558 }
1559
1560 case Primitive::kPrimDouble: {
1561 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1562 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1563 DCHECK(locations->InAt(2).IsFpuRegister());
1564 if (index.IsConstant()) {
1565 size_t offset =
1566 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1567 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1568 } else {
1569 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1570 __ Daddu(TMP, obj, TMP);
1571 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1572 }
1573 break;
1574 }
1575
1576 case Primitive::kPrimVoid:
1577 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1578 UNREACHABLE();
1579 }
1580
1581 // Ints and objects are handled in the switch.
1582 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1583 codegen_->MaybeRecordImplicitNullCheck(instruction);
1584 }
1585}
1586
1587void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001588 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1589 ? LocationSummary::kCallOnSlowPath
1590 : LocationSummary::kNoCall;
1591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001592 locations->SetInAt(0, Location::RequiresRegister());
1593 locations->SetInAt(1, Location::RequiresRegister());
1594 if (instruction->HasUses()) {
1595 locations->SetOut(Location::SameAsFirstInput());
1596 }
1597}
1598
1599void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1600 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001601 BoundsCheckSlowPathMIPS64* slow_path =
1602 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001603 codegen_->AddSlowPath(slow_path);
1604
1605 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1606 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1607
1608 // length is limited by the maximum positive signed 32-bit integer.
1609 // Unsigned comparison of length and index checks for index < 0
1610 // and for length <= index simultaneously.
Alexey Frunzea0e87b02015-09-24 22:57:20 -07001611 __ Bgeuc(index, length, slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07001612}
1613
1614void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1615 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1616 instruction,
1617 LocationSummary::kCallOnSlowPath);
1618 locations->SetInAt(0, Location::RequiresRegister());
1619 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001620 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001621 locations->AddTemp(Location::RequiresRegister());
1622}
1623
1624void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1625 LocationSummary* locations = instruction->GetLocations();
1626 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1627 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1628 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1629
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001630 SlowPathCodeMIPS64* slow_path =
1631 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001632 codegen_->AddSlowPath(slow_path);
1633
1634 // TODO: avoid this check if we know obj is not null.
1635 __ Beqzc(obj, slow_path->GetExitLabel());
1636 // Compare the class of `obj` with `cls`.
1637 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1638 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1639 __ Bind(slow_path->GetExitLabel());
1640}
1641
1642void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1643 LocationSummary* locations =
1644 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1645 locations->SetInAt(0, Location::RequiresRegister());
1646 if (check->HasUses()) {
1647 locations->SetOut(Location::SameAsFirstInput());
1648 }
1649}
1650
1651void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1652 // We assume the class is not null.
1653 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1654 check->GetLoadClass(),
1655 check,
1656 check->GetDexPc(),
1657 true);
1658 codegen_->AddSlowPath(slow_path);
1659 GenerateClassInitializationCheck(slow_path,
1660 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1661}
1662
1663void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1664 Primitive::Type in_type = compare->InputAt(0)->GetType();
1665
Alexey Frunze299a9392015-12-08 16:08:02 -08001666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001667
1668 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001669 case Primitive::kPrimBoolean:
1670 case Primitive::kPrimByte:
1671 case Primitive::kPrimShort:
1672 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001673 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001674 case Primitive::kPrimLong:
1675 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001676 locations->SetInAt(1, Location::RegisterOrConstant(compare->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07001677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1678 break;
1679
1680 case Primitive::kPrimFloat:
Alexey Frunze299a9392015-12-08 16:08:02 -08001681 case Primitive::kPrimDouble:
1682 locations->SetInAt(0, Location::RequiresFpuRegister());
1683 locations->SetInAt(1, Location::RequiresFpuRegister());
1684 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001685 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001686
1687 default:
1688 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1689 }
1690}
1691
1692void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1693 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze299a9392015-12-08 16:08:02 -08001694 GpuRegister res = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001695 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1696
1697 // 0 if: left == right
1698 // 1 if: left > right
1699 // -1 if: left < right
1700 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001701 case Primitive::kPrimBoolean:
1702 case Primitive::kPrimByte:
1703 case Primitive::kPrimShort:
1704 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08001705 case Primitive::kPrimInt:
Alexey Frunze4dda3372015-06-01 18:31:49 -07001706 case Primitive::kPrimLong: {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001707 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001708 Location rhs_location = locations->InAt(1);
1709 bool use_imm = rhs_location.IsConstant();
1710 GpuRegister rhs = ZERO;
1711 if (use_imm) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00001712 if (in_type == Primitive::kPrimLong) {
Aart Bika19616e2016-02-01 18:57:58 -08001713 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1714 if (value != 0) {
1715 rhs = AT;
1716 __ LoadConst64(rhs, value);
1717 }
Roland Levillaina5c4a402016-03-15 15:02:50 +00001718 } else {
1719 int32_t value = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()->AsConstant());
1720 if (value != 0) {
1721 rhs = AT;
1722 __ LoadConst32(rhs, value);
1723 }
Alexey Frunze5c75ffa2015-09-24 14:41:59 -07001724 }
1725 } else {
1726 rhs = rhs_location.AsRegister<GpuRegister>();
1727 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07001728 __ Slt(TMP, lhs, rhs);
Alexey Frunze299a9392015-12-08 16:08:02 -08001729 __ Slt(res, rhs, lhs);
1730 __ Subu(res, res, TMP);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001731 break;
1732 }
1733
Alexey Frunze299a9392015-12-08 16:08:02 -08001734 case Primitive::kPrimFloat: {
1735 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1736 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1737 Mips64Label done;
1738 __ CmpEqS(FTMP, lhs, rhs);
1739 __ LoadConst32(res, 0);
1740 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001741 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001742 __ CmpLtS(FTMP, lhs, rhs);
1743 __ LoadConst32(res, -1);
1744 __ Bc1nez(FTMP, &done);
1745 __ LoadConst32(res, 1);
1746 } else {
1747 __ CmpLtS(FTMP, rhs, lhs);
1748 __ LoadConst32(res, 1);
1749 __ Bc1nez(FTMP, &done);
1750 __ LoadConst32(res, -1);
1751 }
1752 __ Bind(&done);
1753 break;
1754 }
1755
Alexey Frunze4dda3372015-06-01 18:31:49 -07001756 case Primitive::kPrimDouble: {
Alexey Frunze299a9392015-12-08 16:08:02 -08001757 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1758 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1759 Mips64Label done;
1760 __ CmpEqD(FTMP, lhs, rhs);
1761 __ LoadConst32(res, 0);
1762 __ Bc1nez(FTMP, &done);
Roland Levillain32ca3752016-02-17 16:49:37 +00001763 if (instruction->IsGtBias()) {
Alexey Frunze299a9392015-12-08 16:08:02 -08001764 __ CmpLtD(FTMP, lhs, rhs);
1765 __ LoadConst32(res, -1);
1766 __ Bc1nez(FTMP, &done);
1767 __ LoadConst32(res, 1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001768 } else {
Alexey Frunze299a9392015-12-08 16:08:02 -08001769 __ CmpLtD(FTMP, rhs, lhs);
1770 __ LoadConst32(res, 1);
1771 __ Bc1nez(FTMP, &done);
1772 __ LoadConst32(res, -1);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001773 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001774 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001775 break;
1776 }
1777
1778 default:
1779 LOG(FATAL) << "Unimplemented compare type " << in_type;
1780 }
1781}
1782
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783void LocationsBuilderMIPS64::HandleCondition(HCondition* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunze299a9392015-12-08 16:08:02 -08001785 switch (instruction->InputAt(0)->GetType()) {
1786 default:
1787 case Primitive::kPrimLong:
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1790 break;
1791
1792 case Primitive::kPrimFloat:
1793 case Primitive::kPrimDouble:
1794 locations->SetInAt(0, Location::RequiresFpuRegister());
1795 locations->SetInAt(1, Location::RequiresFpuRegister());
1796 break;
1797 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001798 if (!instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001799 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1800 }
1801}
1802
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001803void InstructionCodeGeneratorMIPS64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001804 if (instruction->IsEmittedAtUseSite()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001805 return;
1806 }
1807
Alexey Frunze299a9392015-12-08 16:08:02 -08001808 Primitive::Type type = instruction->InputAt(0)->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001809 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001810 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
Alexey Frunze299a9392015-12-08 16:08:02 -08001811 Mips64Label true_label;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001812
Alexey Frunze299a9392015-12-08 16:08:02 -08001813 switch (type) {
1814 default:
1815 // Integer case.
1816 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ false, locations);
1817 return;
1818 case Primitive::kPrimLong:
1819 GenerateIntLongCompare(instruction->GetCondition(), /* is64bit */ true, locations);
1820 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001821
Alexey Frunze299a9392015-12-08 16:08:02 -08001822 case Primitive::kPrimFloat:
1823 case Primitive::kPrimDouble:
1824 // TODO: don't use branches.
1825 GenerateFpCompareAndBranch(instruction->GetCondition(),
1826 instruction->IsGtBias(),
1827 type,
1828 locations,
1829 &true_label);
Aart Bike9f37602015-10-09 11:15:55 -07001830 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001831 }
Alexey Frunze299a9392015-12-08 16:08:02 -08001832
1833 // Convert the branches into the result.
1834 Mips64Label done;
1835
1836 // False case: result = 0.
1837 __ LoadConst32(dst, 0);
1838 __ Bc(&done);
1839
1840 // True case: result = 1.
1841 __ Bind(&true_label);
1842 __ LoadConst32(dst, 1);
1843 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001844}
1845
Alexey Frunzec857c742015-09-23 15:12:39 -07001846void InstructionCodeGeneratorMIPS64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
1847 DCHECK(instruction->IsDiv() || instruction->IsRem());
1848 Primitive::Type type = instruction->GetResultType();
1849
1850 LocationSummary* locations = instruction->GetLocations();
1851 Location second = locations->InAt(1);
1852 DCHECK(second.IsConstant());
1853
1854 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1855 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1856 int64_t imm = Int64FromConstant(second.GetConstant());
1857 DCHECK(imm == 1 || imm == -1);
1858
1859 if (instruction->IsRem()) {
1860 __ Move(out, ZERO);
1861 } else {
1862 if (imm == -1) {
1863 if (type == Primitive::kPrimInt) {
1864 __ Subu(out, ZERO, dividend);
1865 } else {
1866 DCHECK_EQ(type, Primitive::kPrimLong);
1867 __ Dsubu(out, ZERO, dividend);
1868 }
1869 } else if (out != dividend) {
1870 __ Move(out, dividend);
1871 }
1872 }
1873}
1874
1875void InstructionCodeGeneratorMIPS64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
1876 DCHECK(instruction->IsDiv() || instruction->IsRem());
1877 Primitive::Type type = instruction->GetResultType();
1878
1879 LocationSummary* locations = instruction->GetLocations();
1880 Location second = locations->InAt(1);
1881 DCHECK(second.IsConstant());
1882
1883 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1884 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1885 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00001886 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Alexey Frunzec857c742015-09-23 15:12:39 -07001887 int ctz_imm = CTZ(abs_imm);
1888
1889 if (instruction->IsDiv()) {
1890 if (type == Primitive::kPrimInt) {
1891 if (ctz_imm == 1) {
1892 // Fast path for division by +/-2, which is very common.
1893 __ Srl(TMP, dividend, 31);
1894 } else {
1895 __ Sra(TMP, dividend, 31);
1896 __ Srl(TMP, TMP, 32 - ctz_imm);
1897 }
1898 __ Addu(out, dividend, TMP);
1899 __ Sra(out, out, ctz_imm);
1900 if (imm < 0) {
1901 __ Subu(out, ZERO, out);
1902 }
1903 } else {
1904 DCHECK_EQ(type, Primitive::kPrimLong);
1905 if (ctz_imm == 1) {
1906 // Fast path for division by +/-2, which is very common.
1907 __ Dsrl32(TMP, dividend, 31);
1908 } else {
1909 __ Dsra32(TMP, dividend, 31);
1910 if (ctz_imm > 32) {
1911 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1912 } else {
1913 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1914 }
1915 }
1916 __ Daddu(out, dividend, TMP);
1917 if (ctz_imm < 32) {
1918 __ Dsra(out, out, ctz_imm);
1919 } else {
1920 __ Dsra32(out, out, ctz_imm - 32);
1921 }
1922 if (imm < 0) {
1923 __ Dsubu(out, ZERO, out);
1924 }
1925 }
1926 } else {
1927 if (type == Primitive::kPrimInt) {
1928 if (ctz_imm == 1) {
1929 // Fast path for modulo +/-2, which is very common.
1930 __ Sra(TMP, dividend, 31);
1931 __ Subu(out, dividend, TMP);
1932 __ Andi(out, out, 1);
1933 __ Addu(out, out, TMP);
1934 } else {
1935 __ Sra(TMP, dividend, 31);
1936 __ Srl(TMP, TMP, 32 - ctz_imm);
1937 __ Addu(out, dividend, TMP);
1938 if (IsUint<16>(abs_imm - 1)) {
1939 __ Andi(out, out, abs_imm - 1);
1940 } else {
1941 __ Sll(out, out, 32 - ctz_imm);
1942 __ Srl(out, out, 32 - ctz_imm);
1943 }
1944 __ Subu(out, out, TMP);
1945 }
1946 } else {
1947 DCHECK_EQ(type, Primitive::kPrimLong);
1948 if (ctz_imm == 1) {
1949 // Fast path for modulo +/-2, which is very common.
1950 __ Dsra32(TMP, dividend, 31);
1951 __ Dsubu(out, dividend, TMP);
1952 __ Andi(out, out, 1);
1953 __ Daddu(out, out, TMP);
1954 } else {
1955 __ Dsra32(TMP, dividend, 31);
1956 if (ctz_imm > 32) {
1957 __ Dsrl(TMP, TMP, 64 - ctz_imm);
1958 } else {
1959 __ Dsrl32(TMP, TMP, 32 - ctz_imm);
1960 }
1961 __ Daddu(out, dividend, TMP);
1962 if (IsUint<16>(abs_imm - 1)) {
1963 __ Andi(out, out, abs_imm - 1);
1964 } else {
1965 if (ctz_imm > 32) {
1966 __ Dsll(out, out, 64 - ctz_imm);
1967 __ Dsrl(out, out, 64 - ctz_imm);
1968 } else {
1969 __ Dsll32(out, out, 32 - ctz_imm);
1970 __ Dsrl32(out, out, 32 - ctz_imm);
1971 }
1972 }
1973 __ Dsubu(out, out, TMP);
1974 }
1975 }
1976 }
1977}
1978
1979void InstructionCodeGeneratorMIPS64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
1980 DCHECK(instruction->IsDiv() || instruction->IsRem());
1981
1982 LocationSummary* locations = instruction->GetLocations();
1983 Location second = locations->InAt(1);
1984 DCHECK(second.IsConstant());
1985
1986 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1987 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
1988 int64_t imm = Int64FromConstant(second.GetConstant());
1989
1990 Primitive::Type type = instruction->GetResultType();
1991 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
1992
1993 int64_t magic;
1994 int shift;
1995 CalculateMagicAndShiftForDivRem(imm,
1996 (type == Primitive::kPrimLong),
1997 &magic,
1998 &shift);
1999
2000 if (type == Primitive::kPrimInt) {
2001 __ LoadConst32(TMP, magic);
2002 __ MuhR6(TMP, dividend, TMP);
2003
2004 if (imm > 0 && magic < 0) {
2005 __ Addu(TMP, TMP, dividend);
2006 } else if (imm < 0 && magic > 0) {
2007 __ Subu(TMP, TMP, dividend);
2008 }
2009
2010 if (shift != 0) {
2011 __ Sra(TMP, TMP, shift);
2012 }
2013
2014 if (instruction->IsDiv()) {
2015 __ Sra(out, TMP, 31);
2016 __ Subu(out, TMP, out);
2017 } else {
2018 __ Sra(AT, TMP, 31);
2019 __ Subu(AT, TMP, AT);
2020 __ LoadConst32(TMP, imm);
2021 __ MulR6(TMP, AT, TMP);
2022 __ Subu(out, dividend, TMP);
2023 }
2024 } else {
2025 __ LoadConst64(TMP, magic);
2026 __ Dmuh(TMP, dividend, TMP);
2027
2028 if (imm > 0 && magic < 0) {
2029 __ Daddu(TMP, TMP, dividend);
2030 } else if (imm < 0 && magic > 0) {
2031 __ Dsubu(TMP, TMP, dividend);
2032 }
2033
2034 if (shift >= 32) {
2035 __ Dsra32(TMP, TMP, shift - 32);
2036 } else if (shift > 0) {
2037 __ Dsra(TMP, TMP, shift);
2038 }
2039
2040 if (instruction->IsDiv()) {
2041 __ Dsra32(out, TMP, 31);
2042 __ Dsubu(out, TMP, out);
2043 } else {
2044 __ Dsra32(AT, TMP, 31);
2045 __ Dsubu(AT, TMP, AT);
2046 __ LoadConst64(TMP, imm);
2047 __ Dmul(TMP, AT, TMP);
2048 __ Dsubu(out, dividend, TMP);
2049 }
2050 }
2051}
2052
2053void InstructionCodeGeneratorMIPS64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2054 DCHECK(instruction->IsDiv() || instruction->IsRem());
2055 Primitive::Type type = instruction->GetResultType();
2056 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong) << type;
2057
2058 LocationSummary* locations = instruction->GetLocations();
2059 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2060 Location second = locations->InAt(1);
2061
2062 if (second.IsConstant()) {
2063 int64_t imm = Int64FromConstant(second.GetConstant());
2064 if (imm == 0) {
2065 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2066 } else if (imm == 1 || imm == -1) {
2067 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002068 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunzec857c742015-09-23 15:12:39 -07002069 DivRemByPowerOfTwo(instruction);
2070 } else {
2071 DCHECK(imm <= -2 || imm >= 2);
2072 GenerateDivRemWithAnyConstant(instruction);
2073 }
2074 } else {
2075 GpuRegister dividend = locations->InAt(0).AsRegister<GpuRegister>();
2076 GpuRegister divisor = second.AsRegister<GpuRegister>();
2077 if (instruction->IsDiv()) {
2078 if (type == Primitive::kPrimInt)
2079 __ DivR6(out, dividend, divisor);
2080 else
2081 __ Ddiv(out, dividend, divisor);
2082 } else {
2083 if (type == Primitive::kPrimInt)
2084 __ ModR6(out, dividend, divisor);
2085 else
2086 __ Dmod(out, dividend, divisor);
2087 }
2088 }
2089}
2090
Alexey Frunze4dda3372015-06-01 18:31:49 -07002091void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
2092 LocationSummary* locations =
2093 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2094 switch (div->GetResultType()) {
2095 case Primitive::kPrimInt:
2096 case Primitive::kPrimLong:
2097 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07002098 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2100 break;
2101
2102 case Primitive::kPrimFloat:
2103 case Primitive::kPrimDouble:
2104 locations->SetInAt(0, Location::RequiresFpuRegister());
2105 locations->SetInAt(1, Location::RequiresFpuRegister());
2106 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2107 break;
2108
2109 default:
2110 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2111 }
2112}
2113
2114void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
2115 Primitive::Type type = instruction->GetType();
2116 LocationSummary* locations = instruction->GetLocations();
2117
2118 switch (type) {
2119 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07002120 case Primitive::kPrimLong:
2121 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002122 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002123 case Primitive::kPrimFloat:
2124 case Primitive::kPrimDouble: {
2125 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2126 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2127 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2128 if (type == Primitive::kPrimFloat)
2129 __ DivS(dst, lhs, rhs);
2130 else
2131 __ DivD(dst, lhs, rhs);
2132 break;
2133 }
2134 default:
2135 LOG(FATAL) << "Unexpected div type " << type;
2136 }
2137}
2138
2139void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002140 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2141 ? LocationSummary::kCallOnSlowPath
2142 : LocationSummary::kNoCall;
2143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002144 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2145 if (instruction->HasUses()) {
2146 locations->SetOut(Location::SameAsFirstInput());
2147 }
2148}
2149
2150void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2151 SlowPathCodeMIPS64* slow_path =
2152 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
2153 codegen_->AddSlowPath(slow_path);
2154 Location value = instruction->GetLocations()->InAt(0);
2155
2156 Primitive::Type type = instruction->GetType();
2157
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002158 if (!Primitive::IsIntegralType(type)) {
2159 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06002160 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002161 }
2162
2163 if (value.IsConstant()) {
2164 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
2165 if (divisor == 0) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002166 __ Bc(slow_path->GetEntryLabel());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002167 } else {
2168 // A division by a non-null constant is valid. We don't need to perform
2169 // any check, so simply fall through.
2170 }
2171 } else {
2172 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2173 }
2174}
2175
2176void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
2177 LocationSummary* locations =
2178 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2179 locations->SetOut(Location::ConstantLocation(constant));
2180}
2181
2182void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2183 // Will be generated at use site.
2184}
2185
2186void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2187 exit->SetLocations(nullptr);
2188}
2189
2190void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2191}
2192
2193void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2194 LocationSummary* locations =
2195 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2196 locations->SetOut(Location::ConstantLocation(constant));
2197}
2198
2199void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2200 // Will be generated at use site.
2201}
2202
David Brazdilfc6a86a2015-06-26 10:33:45 +00002203void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002204 DCHECK(!successor->IsExitBlock());
2205 HBasicBlock* block = got->GetBlock();
2206 HInstruction* previous = got->GetPrevious();
2207 HLoopInformation* info = block->GetLoopInformation();
2208
2209 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2210 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2211 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2212 return;
2213 }
2214 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2215 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2216 }
2217 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002218 __ Bc(codegen_->GetLabelOf(successor));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002219 }
2220}
2221
David Brazdilfc6a86a2015-06-26 10:33:45 +00002222void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2223 got->SetLocations(nullptr);
2224}
2225
2226void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2227 HandleGoto(got, got->GetSuccessor());
2228}
2229
2230void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2231 try_boundary->SetLocations(nullptr);
2232}
2233
2234void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2235 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2236 if (!successor->IsExitBlock()) {
2237 HandleGoto(try_boundary, successor);
2238 }
2239}
2240
Alexey Frunze299a9392015-12-08 16:08:02 -08002241void InstructionCodeGeneratorMIPS64::GenerateIntLongCompare(IfCondition cond,
2242 bool is64bit,
2243 LocationSummary* locations) {
2244 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2245 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2246 Location rhs_location = locations->InAt(1);
2247 GpuRegister rhs_reg = ZERO;
2248 int64_t rhs_imm = 0;
2249 bool use_imm = rhs_location.IsConstant();
2250 if (use_imm) {
2251 if (is64bit) {
2252 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2253 } else {
2254 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2255 }
2256 } else {
2257 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2258 }
2259 int64_t rhs_imm_plus_one = rhs_imm + UINT64_C(1);
2260
2261 switch (cond) {
2262 case kCondEQ:
2263 case kCondNE:
2264 if (use_imm && IsUint<16>(rhs_imm)) {
2265 __ Xori(dst, lhs, rhs_imm);
2266 } else {
2267 if (use_imm) {
2268 rhs_reg = TMP;
2269 __ LoadConst64(rhs_reg, rhs_imm);
2270 }
2271 __ Xor(dst, lhs, rhs_reg);
2272 }
2273 if (cond == kCondEQ) {
2274 __ Sltiu(dst, dst, 1);
2275 } else {
2276 __ Sltu(dst, ZERO, dst);
2277 }
2278 break;
2279
2280 case kCondLT:
2281 case kCondGE:
2282 if (use_imm && IsInt<16>(rhs_imm)) {
2283 __ Slti(dst, lhs, rhs_imm);
2284 } else {
2285 if (use_imm) {
2286 rhs_reg = TMP;
2287 __ LoadConst64(rhs_reg, rhs_imm);
2288 }
2289 __ Slt(dst, lhs, rhs_reg);
2290 }
2291 if (cond == kCondGE) {
2292 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2293 // only the slt instruction but no sge.
2294 __ Xori(dst, dst, 1);
2295 }
2296 break;
2297
2298 case kCondLE:
2299 case kCondGT:
2300 if (use_imm && IsInt<16>(rhs_imm_plus_one)) {
2301 // Simulate lhs <= rhs via lhs < rhs + 1.
2302 __ Slti(dst, lhs, rhs_imm_plus_one);
2303 if (cond == kCondGT) {
2304 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2305 // only the slti instruction but no sgti.
2306 __ Xori(dst, dst, 1);
2307 }
2308 } else {
2309 if (use_imm) {
2310 rhs_reg = TMP;
2311 __ LoadConst64(rhs_reg, rhs_imm);
2312 }
2313 __ Slt(dst, rhs_reg, lhs);
2314 if (cond == kCondLE) {
2315 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2316 // only the slt instruction but no sle.
2317 __ Xori(dst, dst, 1);
2318 }
2319 }
2320 break;
2321
2322 case kCondB:
2323 case kCondAE:
2324 if (use_imm && IsInt<16>(rhs_imm)) {
2325 // Sltiu sign-extends its 16-bit immediate operand before
2326 // the comparison and thus lets us compare directly with
2327 // unsigned values in the ranges [0, 0x7fff] and
2328 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2329 __ Sltiu(dst, lhs, rhs_imm);
2330 } else {
2331 if (use_imm) {
2332 rhs_reg = TMP;
2333 __ LoadConst64(rhs_reg, rhs_imm);
2334 }
2335 __ Sltu(dst, lhs, rhs_reg);
2336 }
2337 if (cond == kCondAE) {
2338 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2339 // only the sltu instruction but no sgeu.
2340 __ Xori(dst, dst, 1);
2341 }
2342 break;
2343
2344 case kCondBE:
2345 case kCondA:
2346 if (use_imm && (rhs_imm_plus_one != 0) && IsInt<16>(rhs_imm_plus_one)) {
2347 // Simulate lhs <= rhs via lhs < rhs + 1.
2348 // Note that this only works if rhs + 1 does not overflow
2349 // to 0, hence the check above.
2350 // Sltiu sign-extends its 16-bit immediate operand before
2351 // the comparison and thus lets us compare directly with
2352 // unsigned values in the ranges [0, 0x7fff] and
2353 // [0x[ffffffff]ffff8000, 0x[ffffffff]ffffffff].
2354 __ Sltiu(dst, lhs, rhs_imm_plus_one);
2355 if (cond == kCondA) {
2356 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2357 // only the sltiu instruction but no sgtiu.
2358 __ Xori(dst, dst, 1);
2359 }
2360 } else {
2361 if (use_imm) {
2362 rhs_reg = TMP;
2363 __ LoadConst64(rhs_reg, rhs_imm);
2364 }
2365 __ Sltu(dst, rhs_reg, lhs);
2366 if (cond == kCondBE) {
2367 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2368 // only the sltu instruction but no sleu.
2369 __ Xori(dst, dst, 1);
2370 }
2371 }
2372 break;
2373 }
2374}
2375
2376void InstructionCodeGeneratorMIPS64::GenerateIntLongCompareAndBranch(IfCondition cond,
2377 bool is64bit,
2378 LocationSummary* locations,
2379 Mips64Label* label) {
2380 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2381 Location rhs_location = locations->InAt(1);
2382 GpuRegister rhs_reg = ZERO;
2383 int64_t rhs_imm = 0;
2384 bool use_imm = rhs_location.IsConstant();
2385 if (use_imm) {
2386 if (is64bit) {
2387 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
2388 } else {
2389 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2390 }
2391 } else {
2392 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2393 }
2394
2395 if (use_imm && rhs_imm == 0) {
2396 switch (cond) {
2397 case kCondEQ:
2398 case kCondBE: // <= 0 if zero
2399 __ Beqzc(lhs, label);
2400 break;
2401 case kCondNE:
2402 case kCondA: // > 0 if non-zero
2403 __ Bnezc(lhs, label);
2404 break;
2405 case kCondLT:
2406 __ Bltzc(lhs, label);
2407 break;
2408 case kCondGE:
2409 __ Bgezc(lhs, label);
2410 break;
2411 case kCondLE:
2412 __ Blezc(lhs, label);
2413 break;
2414 case kCondGT:
2415 __ Bgtzc(lhs, label);
2416 break;
2417 case kCondB: // always false
2418 break;
2419 case kCondAE: // always true
2420 __ Bc(label);
2421 break;
2422 }
2423 } else {
2424 if (use_imm) {
2425 rhs_reg = TMP;
2426 __ LoadConst64(rhs_reg, rhs_imm);
2427 }
2428 switch (cond) {
2429 case kCondEQ:
2430 __ Beqc(lhs, rhs_reg, label);
2431 break;
2432 case kCondNE:
2433 __ Bnec(lhs, rhs_reg, label);
2434 break;
2435 case kCondLT:
2436 __ Bltc(lhs, rhs_reg, label);
2437 break;
2438 case kCondGE:
2439 __ Bgec(lhs, rhs_reg, label);
2440 break;
2441 case kCondLE:
2442 __ Bgec(rhs_reg, lhs, label);
2443 break;
2444 case kCondGT:
2445 __ Bltc(rhs_reg, lhs, label);
2446 break;
2447 case kCondB:
2448 __ Bltuc(lhs, rhs_reg, label);
2449 break;
2450 case kCondAE:
2451 __ Bgeuc(lhs, rhs_reg, label);
2452 break;
2453 case kCondBE:
2454 __ Bgeuc(rhs_reg, lhs, label);
2455 break;
2456 case kCondA:
2457 __ Bltuc(rhs_reg, lhs, label);
2458 break;
2459 }
2460 }
2461}
2462
2463void InstructionCodeGeneratorMIPS64::GenerateFpCompareAndBranch(IfCondition cond,
2464 bool gt_bias,
2465 Primitive::Type type,
2466 LocationSummary* locations,
2467 Mips64Label* label) {
2468 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2469 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2470 if (type == Primitive::kPrimFloat) {
2471 switch (cond) {
2472 case kCondEQ:
2473 __ CmpEqS(FTMP, lhs, rhs);
2474 __ Bc1nez(FTMP, label);
2475 break;
2476 case kCondNE:
2477 __ CmpEqS(FTMP, lhs, rhs);
2478 __ Bc1eqz(FTMP, label);
2479 break;
2480 case kCondLT:
2481 if (gt_bias) {
2482 __ CmpLtS(FTMP, lhs, rhs);
2483 } else {
2484 __ CmpUltS(FTMP, lhs, rhs);
2485 }
2486 __ Bc1nez(FTMP, label);
2487 break;
2488 case kCondLE:
2489 if (gt_bias) {
2490 __ CmpLeS(FTMP, lhs, rhs);
2491 } else {
2492 __ CmpUleS(FTMP, lhs, rhs);
2493 }
2494 __ Bc1nez(FTMP, label);
2495 break;
2496 case kCondGT:
2497 if (gt_bias) {
2498 __ CmpUltS(FTMP, rhs, lhs);
2499 } else {
2500 __ CmpLtS(FTMP, rhs, lhs);
2501 }
2502 __ Bc1nez(FTMP, label);
2503 break;
2504 case kCondGE:
2505 if (gt_bias) {
2506 __ CmpUleS(FTMP, rhs, lhs);
2507 } else {
2508 __ CmpLeS(FTMP, rhs, lhs);
2509 }
2510 __ Bc1nez(FTMP, label);
2511 break;
2512 default:
2513 LOG(FATAL) << "Unexpected non-floating-point condition";
2514 }
2515 } else {
2516 DCHECK_EQ(type, Primitive::kPrimDouble);
2517 switch (cond) {
2518 case kCondEQ:
2519 __ CmpEqD(FTMP, lhs, rhs);
2520 __ Bc1nez(FTMP, label);
2521 break;
2522 case kCondNE:
2523 __ CmpEqD(FTMP, lhs, rhs);
2524 __ Bc1eqz(FTMP, label);
2525 break;
2526 case kCondLT:
2527 if (gt_bias) {
2528 __ CmpLtD(FTMP, lhs, rhs);
2529 } else {
2530 __ CmpUltD(FTMP, lhs, rhs);
2531 }
2532 __ Bc1nez(FTMP, label);
2533 break;
2534 case kCondLE:
2535 if (gt_bias) {
2536 __ CmpLeD(FTMP, lhs, rhs);
2537 } else {
2538 __ CmpUleD(FTMP, lhs, rhs);
2539 }
2540 __ Bc1nez(FTMP, label);
2541 break;
2542 case kCondGT:
2543 if (gt_bias) {
2544 __ CmpUltD(FTMP, rhs, lhs);
2545 } else {
2546 __ CmpLtD(FTMP, rhs, lhs);
2547 }
2548 __ Bc1nez(FTMP, label);
2549 break;
2550 case kCondGE:
2551 if (gt_bias) {
2552 __ CmpUleD(FTMP, rhs, lhs);
2553 } else {
2554 __ CmpLeD(FTMP, rhs, lhs);
2555 }
2556 __ Bc1nez(FTMP, label);
2557 break;
2558 default:
2559 LOG(FATAL) << "Unexpected non-floating-point condition";
2560 }
2561 }
2562}
2563
Alexey Frunze4dda3372015-06-01 18:31:49 -07002564void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002565 size_t condition_input_index,
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002566 Mips64Label* true_target,
2567 Mips64Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002568 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002569
David Brazdil0debae72015-11-12 18:37:00 +00002570 if (true_target == nullptr && false_target == nullptr) {
2571 // Nothing to do. The code always falls through.
2572 return;
2573 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002574 // Constant condition, statically compared against "true" (integer value 1).
2575 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002576 if (true_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002577 __ Bc(true_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002578 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002579 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002580 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002581 if (false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002582 __ Bc(false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002583 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002584 }
David Brazdil0debae72015-11-12 18:37:00 +00002585 return;
2586 }
2587
2588 // The following code generates these patterns:
2589 // (1) true_target == nullptr && false_target != nullptr
2590 // - opposite condition true => branch to false_target
2591 // (2) true_target != nullptr && false_target == nullptr
2592 // - condition true => branch to true_target
2593 // (3) true_target != nullptr && false_target != nullptr
2594 // - condition true => branch to true_target
2595 // - branch to false_target
2596 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002597 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002598 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002600 if (true_target == nullptr) {
2601 __ Beqzc(cond_val.AsRegister<GpuRegister>(), false_target);
2602 } else {
2603 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2604 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002605 } else {
2606 // The condition instruction has not been materialized, use its inputs as
2607 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002608 HCondition* condition = cond->AsCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002609 Primitive::Type type = condition->InputAt(0)->GetType();
2610 LocationSummary* locations = cond->GetLocations();
2611 IfCondition if_cond = condition->GetCondition();
2612 Mips64Label* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00002613
David Brazdil0debae72015-11-12 18:37:00 +00002614 if (true_target == nullptr) {
2615 if_cond = condition->GetOppositeCondition();
Alexey Frunze299a9392015-12-08 16:08:02 -08002616 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002617 }
2618
Alexey Frunze299a9392015-12-08 16:08:02 -08002619 switch (type) {
2620 default:
2621 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ false, locations, branch_target);
2622 break;
2623 case Primitive::kPrimLong:
2624 GenerateIntLongCompareAndBranch(if_cond, /* is64bit */ true, locations, branch_target);
2625 break;
2626 case Primitive::kPrimFloat:
2627 case Primitive::kPrimDouble:
2628 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
2629 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002630 }
2631 }
David Brazdil0debae72015-11-12 18:37:00 +00002632
2633 // If neither branch falls through (case 3), the conditional branch to `true_target`
2634 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2635 if (true_target != nullptr && false_target != nullptr) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002636 __ Bc(false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002637 }
2638}
2639
2640void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2641 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002642 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002643 locations->SetInAt(0, Location::RequiresRegister());
2644 }
2645}
2646
2647void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002648 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2649 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002650 Mips64Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002651 nullptr : codegen_->GetLabelOf(true_successor);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002652 Mips64Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
David Brazdil0debae72015-11-12 18:37:00 +00002653 nullptr : codegen_->GetLabelOf(false_successor);
2654 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002655}
2656
2657void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2658 LocationSummary* locations = new (GetGraph()->GetArena())
2659 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002660 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002661 locations->SetInAt(0, Location::RequiresRegister());
2662 }
2663}
2664
2665void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002666 SlowPathCodeMIPS64* slow_path =
2667 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002668 GenerateTestAndBranch(deoptimize,
2669 /* condition_input_index */ 0,
2670 slow_path->GetEntryLabel(),
2671 /* false_target */ nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002672}
2673
David Brazdil74eb1b22015-12-14 11:44:01 +00002674void LocationsBuilderMIPS64::VisitSelect(HSelect* select) {
2675 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2676 if (Primitive::IsFloatingPointType(select->GetType())) {
2677 locations->SetInAt(0, Location::RequiresFpuRegister());
2678 locations->SetInAt(1, Location::RequiresFpuRegister());
2679 } else {
2680 locations->SetInAt(0, Location::RequiresRegister());
2681 locations->SetInAt(1, Location::RequiresRegister());
2682 }
2683 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2684 locations->SetInAt(2, Location::RequiresRegister());
2685 }
2686 locations->SetOut(Location::SameAsFirstInput());
2687}
2688
2689void InstructionCodeGeneratorMIPS64::VisitSelect(HSelect* select) {
2690 LocationSummary* locations = select->GetLocations();
2691 Mips64Label false_target;
2692 GenerateTestAndBranch(select,
2693 /* condition_input_index */ 2,
2694 /* true_target */ nullptr,
2695 &false_target);
2696 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2697 __ Bind(&false_target);
2698}
2699
David Srbecky0cf44932015-12-09 14:09:59 +00002700void LocationsBuilderMIPS64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2701 new (GetGraph()->GetArena()) LocationSummary(info);
2702}
2703
David Srbeckyd28f4a02016-03-14 17:14:24 +00002704void InstructionCodeGeneratorMIPS64::VisitNativeDebugInfo(HNativeDebugInfo*) {
2705 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002706}
2707
2708void CodeGeneratorMIPS64::GenerateNop() {
2709 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002710}
2711
Alexey Frunze4dda3372015-06-01 18:31:49 -07002712void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2713 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2716 locations->SetInAt(0, Location::RequiresRegister());
2717 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2718 locations->SetOut(Location::RequiresFpuRegister());
2719 } else {
2720 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2721 }
2722}
2723
2724void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2725 const FieldInfo& field_info) {
2726 Primitive::Type type = field_info.GetFieldType();
2727 LocationSummary* locations = instruction->GetLocations();
2728 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2729 LoadOperandType load_type = kLoadUnsignedByte;
2730 switch (type) {
2731 case Primitive::kPrimBoolean:
2732 load_type = kLoadUnsignedByte;
2733 break;
2734 case Primitive::kPrimByte:
2735 load_type = kLoadSignedByte;
2736 break;
2737 case Primitive::kPrimShort:
2738 load_type = kLoadSignedHalfword;
2739 break;
2740 case Primitive::kPrimChar:
2741 load_type = kLoadUnsignedHalfword;
2742 break;
2743 case Primitive::kPrimInt:
2744 case Primitive::kPrimFloat:
2745 load_type = kLoadWord;
2746 break;
2747 case Primitive::kPrimLong:
2748 case Primitive::kPrimDouble:
2749 load_type = kLoadDoubleword;
2750 break;
2751 case Primitive::kPrimNot:
2752 load_type = kLoadUnsignedWord;
2753 break;
2754 case Primitive::kPrimVoid:
2755 LOG(FATAL) << "Unreachable type " << type;
2756 UNREACHABLE();
2757 }
2758 if (!Primitive::IsFloatingPointType(type)) {
2759 DCHECK(locations->Out().IsRegister());
2760 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2761 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2762 } else {
2763 DCHECK(locations->Out().IsFpuRegister());
2764 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2765 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2766 }
2767
2768 codegen_->MaybeRecordImplicitNullCheck(instruction);
2769 // TODO: memory barrier?
2770}
2771
2772void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2773 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2774 LocationSummary* locations =
2775 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2776 locations->SetInAt(0, Location::RequiresRegister());
2777 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2778 locations->SetInAt(1, Location::RequiresFpuRegister());
2779 } else {
2780 locations->SetInAt(1, Location::RequiresRegister());
2781 }
2782}
2783
2784void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002785 const FieldInfo& field_info,
2786 bool value_can_be_null) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002787 Primitive::Type type = field_info.GetFieldType();
2788 LocationSummary* locations = instruction->GetLocations();
2789 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2790 StoreOperandType store_type = kStoreByte;
2791 switch (type) {
2792 case Primitive::kPrimBoolean:
2793 case Primitive::kPrimByte:
2794 store_type = kStoreByte;
2795 break;
2796 case Primitive::kPrimShort:
2797 case Primitive::kPrimChar:
2798 store_type = kStoreHalfword;
2799 break;
2800 case Primitive::kPrimInt:
2801 case Primitive::kPrimFloat:
2802 case Primitive::kPrimNot:
2803 store_type = kStoreWord;
2804 break;
2805 case Primitive::kPrimLong:
2806 case Primitive::kPrimDouble:
2807 store_type = kStoreDoubleword;
2808 break;
2809 case Primitive::kPrimVoid:
2810 LOG(FATAL) << "Unreachable type " << type;
2811 UNREACHABLE();
2812 }
2813 if (!Primitive::IsFloatingPointType(type)) {
2814 DCHECK(locations->InAt(1).IsRegister());
2815 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2816 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2817 } else {
2818 DCHECK(locations->InAt(1).IsFpuRegister());
2819 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2820 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2821 }
2822
2823 codegen_->MaybeRecordImplicitNullCheck(instruction);
2824 // TODO: memory barriers?
2825 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2826 DCHECK(locations->InAt(1).IsRegister());
2827 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002828 codegen_->MarkGCCard(obj, src, value_can_be_null);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002829 }
2830}
2831
2832void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2833 HandleFieldGet(instruction, instruction->GetFieldInfo());
2834}
2835
2836void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2837 HandleFieldGet(instruction, instruction->GetFieldInfo());
2838}
2839
2840void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2841 HandleFieldSet(instruction, instruction->GetFieldInfo());
2842}
2843
2844void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01002845 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002846}
2847
2848void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2849 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002850 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002851 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2852 locations->SetInAt(0, Location::RequiresRegister());
2853 locations->SetInAt(1, Location::RequiresRegister());
2854 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002855 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002856 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2857}
2858
2859void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2860 LocationSummary* locations = instruction->GetLocations();
2861 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2862 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2863 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2864
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002865 Mips64Label done;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002866
2867 // Return 0 if `obj` is null.
2868 // TODO: Avoid this check if we know `obj` is not null.
2869 __ Move(out, ZERO);
2870 __ Beqzc(obj, &done);
2871
2872 // Compare the class of `obj` with `cls`.
2873 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002874 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002875 // Classes must be equal for the instanceof to succeed.
2876 __ Xor(out, out, cls);
2877 __ Sltiu(out, out, 1);
2878 } else {
2879 // If the classes are not equal, we go into a slow path.
2880 DCHECK(locations->OnlyCallsOnSlowPath());
2881 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002882 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002883 codegen_->AddSlowPath(slow_path);
2884 __ Bnec(out, cls, slow_path->GetEntryLabel());
2885 __ LoadConst32(out, 1);
2886 __ Bind(slow_path->GetExitLabel());
2887 }
2888
2889 __ Bind(&done);
2890}
2891
2892void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2893 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2894 locations->SetOut(Location::ConstantLocation(constant));
2895}
2896
2897void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2898 // Will be generated at use site.
2899}
2900
2901void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2903 locations->SetOut(Location::ConstantLocation(constant));
2904}
2905
2906void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2907 // Will be generated at use site.
2908}
2909
Calin Juravle175dc732015-08-25 15:42:32 +01002910void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2911 // The trampoline uses the same calling convention as dex calling conventions,
2912 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2913 // the method_idx.
2914 HandleInvoke(invoke);
2915}
2916
2917void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2918 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2919}
2920
Alexey Frunze4dda3372015-06-01 18:31:49 -07002921void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2922 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2923 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2924}
2925
2926void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2927 HandleInvoke(invoke);
2928 // The register T0 is required to be used for the hidden argument in
2929 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2930 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2931}
2932
2933void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2934 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2935 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2936 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2937 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2938 Location receiver = invoke->GetLocations()->InAt(0);
2939 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02002940 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941
2942 // Set the hidden argument.
2943 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2944 invoke->GetDexMethodIndex());
2945
2946 // temp = object->GetClass();
2947 if (receiver.IsStackSlot()) {
2948 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2949 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2950 } else {
2951 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2952 }
2953 codegen_->MaybeRecordImplicitNullCheck(invoke);
2954 // temp = temp->GetImtEntryAt(method_offset);
2955 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2956 // T9 = temp->GetEntryPoint();
2957 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2958 // T9();
2959 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07002960 __ Nop();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002961 DCHECK(!codegen_->IsLeafMethod());
2962 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2963}
2964
2965void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002966 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2967 if (intrinsic.TryDispatch(invoke)) {
2968 return;
2969 }
2970
Alexey Frunze4dda3372015-06-01 18:31:49 -07002971 HandleInvoke(invoke);
2972}
2973
2974void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002975 // Explicit clinit checks triggered by static invokes must have been pruned by
2976 // art::PrepareForRegisterAllocation.
2977 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002978
Chris Larsen3039e382015-08-26 07:54:08 -07002979 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2980 if (intrinsic.TryDispatch(invoke)) {
2981 return;
2982 }
2983
Alexey Frunze4dda3372015-06-01 18:31:49 -07002984 HandleInvoke(invoke);
2985
2986 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2987 // clobbering somewhere else, reduce further register pressure by avoiding
2988 // allocation of a register for the current method pointer like on x86 baseline.
2989 // TODO: remove this once all the issues with register saving/restoring are
2990 // sorted out.
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002991 if (invoke->HasCurrentMethodInput()) {
2992 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002993 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002994 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002995 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Vladimir Marko6f6f3592015-11-09 12:54:16 +00002996 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002997 }
2998}
2999
Chris Larsen3039e382015-08-26 07:54:08 -07003000static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003001 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07003002 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
3003 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003004 return true;
3005 }
3006 return false;
3007}
3008
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003009HLoadString::LoadKind CodeGeneratorMIPS64::GetSupportedLoadStringKind(
3010 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3011 // TODO: Implement other kinds.
3012 return HLoadString::LoadKind::kDexCacheViaMethod;
3013}
3014
Vladimir Markodc151b22015-10-15 18:02:30 +01003015HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
3016 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3017 MethodReference target_method ATTRIBUTE_UNUSED) {
3018 switch (desired_dispatch_info.method_load_kind) {
3019 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3020 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3021 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3022 return HInvokeStaticOrDirect::DispatchInfo {
3023 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3024 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3025 0u,
3026 0u
3027 };
3028 default:
3029 break;
3030 }
3031 switch (desired_dispatch_info.code_ptr_location) {
3032 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3033 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3034 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3035 return HInvokeStaticOrDirect::DispatchInfo {
3036 desired_dispatch_info.method_load_kind,
3037 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3038 desired_dispatch_info.method_load_data,
3039 0u
3040 };
3041 default:
3042 return desired_dispatch_info;
3043 }
3044}
3045
Alexey Frunze4dda3372015-06-01 18:31:49 -07003046void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3047 // All registers are assumed to be correctly set up per the calling convention.
3048
Vladimir Marko58155012015-08-19 12:49:41 +00003049 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3050 switch (invoke->GetMethodLoadKind()) {
3051 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3052 // temp = thread->string_init_entrypoint
3053 __ LoadFromOffset(kLoadDoubleword,
3054 temp.AsRegister<GpuRegister>(),
3055 TR,
3056 invoke->GetStringInitOffset());
3057 break;
3058 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003059 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003060 break;
3061 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3062 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
3063 break;
3064 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00003065 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003066 // TODO: Implement these types.
3067 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3068 LOG(FATAL) << "Unsupported";
3069 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003070 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003071 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003072 GpuRegister reg = temp.AsRegister<GpuRegister>();
3073 GpuRegister method_reg;
3074 if (current_method.IsRegister()) {
3075 method_reg = current_method.AsRegister<GpuRegister>();
3076 } else {
3077 // TODO: use the appropriate DCHECK() here if possible.
3078 // DCHECK(invoke->GetLocations()->Intrinsified());
3079 DCHECK(!current_method.IsValid());
3080 method_reg = reg;
3081 __ Ld(reg, SP, kCurrentMethodStackOffset);
3082 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003083
Vladimir Marko58155012015-08-19 12:49:41 +00003084 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003085 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00003086 reg,
3087 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01003088 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003089 // temp = temp[index_in_cache];
3090 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3091 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003092 __ LoadFromOffset(kLoadDoubleword,
3093 reg,
3094 reg,
3095 CodeGenerator::GetCachePointerOffset(index_in_cache));
3096 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003097 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003098 }
3099
Vladimir Marko58155012015-08-19 12:49:41 +00003100 switch (invoke->GetCodePtrLocation()) {
3101 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003102 __ Jialc(&frame_entry_label_, T9);
Vladimir Marko58155012015-08-19 12:49:41 +00003103 break;
3104 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3105 // LR = invoke->GetDirectCodePtr();
3106 __ LoadConst64(T9, invoke->GetDirectCodePtr());
3107 // LR()
3108 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003109 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003110 break;
Vladimir Marko58155012015-08-19 12:49:41 +00003111 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003112 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3113 // TODO: Implement these types.
3114 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3115 LOG(FATAL) << "Unsupported";
3116 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003117 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3118 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3119 __ LoadFromOffset(kLoadDoubleword,
3120 T9,
3121 callee_method.AsRegister<GpuRegister>(),
3122 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Lazar Trsicd9672662015-09-03 17:33:01 +02003123 kMips64DoublewordSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00003124 // T9()
3125 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003126 __ Nop();
Vladimir Marko58155012015-08-19 12:49:41 +00003127 break;
3128 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003129 DCHECK(!IsLeafMethod());
3130}
3131
3132void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003133 // Explicit clinit checks triggered by static invokes must have been pruned by
3134 // art::PrepareForRegisterAllocation.
3135 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003136
3137 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3138 return;
3139 }
3140
3141 LocationSummary* locations = invoke->GetLocations();
3142 codegen_->GenerateStaticOrDirectCall(invoke,
3143 locations->HasTemps()
3144 ? locations->GetTemp(0)
3145 : Location::NoLocation());
3146 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3147}
3148
Alexey Frunze53afca12015-11-05 16:34:23 -08003149void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003150 // Use the calling convention instead of the location of the receiver, as
3151 // intrinsics may have put the receiver in a different register. In the intrinsics
3152 // slow path, the arguments have been moved to the right place, so here we are
3153 // guaranteed that the receiver is the first register of the calling convention.
3154 InvokeDexCallingConvention calling_convention;
3155 GpuRegister receiver = calling_convention.GetRegisterAt(0);
3156
Alexey Frunze53afca12015-11-05 16:34:23 -08003157 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003158 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3159 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
3160 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Lazar Trsicd9672662015-09-03 17:33:01 +02003161 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003162
3163 // temp = object->GetClass();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003164 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver, class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08003165 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003166 // temp = temp->GetMethodAt(method_offset);
3167 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
3168 // T9 = temp->GetEntryPoint();
3169 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
3170 // T9();
3171 __ Jalr(T9);
Alexey Frunzea0e87b02015-09-24 22:57:20 -07003172 __ Nop();
Alexey Frunze53afca12015-11-05 16:34:23 -08003173}
3174
3175void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3176 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3177 return;
3178 }
3179
3180 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003181 DCHECK(!codegen_->IsLeafMethod());
3182 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3183}
3184
3185void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003186 InvokeRuntimeCallingConvention calling_convention;
3187 CodeGenerator::CreateLoadClassLocationSummary(
3188 cls,
3189 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Alexey Frunze00580bd2015-11-11 13:31:12 -08003190 calling_convention.GetReturnLocation(cls->GetType()));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003191}
3192
3193void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
3194 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01003195 if (cls->NeedsAccessCheck()) {
3196 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3197 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3198 cls,
3199 cls->GetDexPc(),
3200 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003201 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003202 return;
3203 }
3204
3205 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3206 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3207 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003208 DCHECK(!cls->CanCallRuntime());
3209 DCHECK(!cls->MustGenerateClinitCheck());
3210 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3211 ArtMethod::DeclaringClassOffset().Int32Value());
3212 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003213 __ LoadFromOffset(kLoadDoubleword, out, current_method,
3214 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003215 __ LoadFromOffset(
3216 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003217 // TODO: We will need a read barrier here.
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003218 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3219 DCHECK(cls->CanCallRuntime());
3220 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
3221 cls,
3222 cls,
3223 cls->GetDexPc(),
3224 cls->MustGenerateClinitCheck());
3225 codegen_->AddSlowPath(slow_path);
3226 if (!cls->IsInDexCache()) {
3227 __ Beqzc(out, slow_path->GetEntryLabel());
3228 }
3229 if (cls->MustGenerateClinitCheck()) {
3230 GenerateClassInitializationCheck(slow_path, out);
3231 } else {
3232 __ Bind(slow_path->GetExitLabel());
3233 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003234 }
3235 }
3236}
3237
David Brazdilcb1c0552015-08-04 16:22:25 +01003238static int32_t GetExceptionTlsOffset() {
Lazar Trsicd9672662015-09-03 17:33:01 +02003239 return Thread::ExceptionOffset<kMips64DoublewordSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01003240}
3241
Alexey Frunze4dda3372015-06-01 18:31:49 -07003242void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
3243 LocationSummary* locations =
3244 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3245 locations->SetOut(Location::RequiresRegister());
3246}
3247
3248void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
3249 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01003250 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
3251}
3252
3253void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
3254 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3255}
3256
3257void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3258 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003259}
3260
Alexey Frunze4dda3372015-06-01 18:31:49 -07003261void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003262 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
3263 ? LocationSummary::kCallOnSlowPath
3264 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003265 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003266 locations->SetInAt(0, Location::RequiresRegister());
3267 locations->SetOut(Location::RequiresRegister());
3268}
3269
3270void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003271 LocationSummary* locations = load->GetLocations();
3272 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
3273 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
3274 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
3275 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01003276 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Roland Levillain698fa972015-12-16 17:06:47 +00003277 __ LoadFromOffset(
3278 kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01003279 // TODO: We will need a read barrier here.
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003280
3281 if (!load->IsInDexCache()) {
3282 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
3283 codegen_->AddSlowPath(slow_path);
3284 __ Beqzc(out, slow_path->GetEntryLabel());
3285 __ Bind(slow_path->GetExitLabel());
3286 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003287}
3288
Alexey Frunze4dda3372015-06-01 18:31:49 -07003289void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
3290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3291 locations->SetOut(Location::ConstantLocation(constant));
3292}
3293
3294void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3295 // Will be generated at use site.
3296}
3297
3298void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3299 LocationSummary* locations =
3300 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3301 InvokeRuntimeCallingConvention calling_convention;
3302 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3303}
3304
3305void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
3306 codegen_->InvokeRuntime(instruction->IsEnter()
3307 ? QUICK_ENTRY_POINT(pLockObject)
3308 : QUICK_ENTRY_POINT(pUnlockObject),
3309 instruction,
3310 instruction->GetDexPc(),
3311 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003312 if (instruction->IsEnter()) {
3313 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3314 } else {
3315 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3316 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003317}
3318
3319void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
3320 LocationSummary* locations =
3321 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3322 switch (mul->GetResultType()) {
3323 case Primitive::kPrimInt:
3324 case Primitive::kPrimLong:
3325 locations->SetInAt(0, Location::RequiresRegister());
3326 locations->SetInAt(1, Location::RequiresRegister());
3327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3328 break;
3329
3330 case Primitive::kPrimFloat:
3331 case Primitive::kPrimDouble:
3332 locations->SetInAt(0, Location::RequiresFpuRegister());
3333 locations->SetInAt(1, Location::RequiresFpuRegister());
3334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3335 break;
3336
3337 default:
3338 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3339 }
3340}
3341
3342void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
3343 Primitive::Type type = instruction->GetType();
3344 LocationSummary* locations = instruction->GetLocations();
3345
3346 switch (type) {
3347 case Primitive::kPrimInt:
3348 case Primitive::kPrimLong: {
3349 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3350 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3351 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3352 if (type == Primitive::kPrimInt)
3353 __ MulR6(dst, lhs, rhs);
3354 else
3355 __ Dmul(dst, lhs, rhs);
3356 break;
3357 }
3358 case Primitive::kPrimFloat:
3359 case Primitive::kPrimDouble: {
3360 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3361 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
3362 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
3363 if (type == Primitive::kPrimFloat)
3364 __ MulS(dst, lhs, rhs);
3365 else
3366 __ MulD(dst, lhs, rhs);
3367 break;
3368 }
3369 default:
3370 LOG(FATAL) << "Unexpected mul type " << type;
3371 }
3372}
3373
3374void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
3375 LocationSummary* locations =
3376 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3377 switch (neg->GetResultType()) {
3378 case Primitive::kPrimInt:
3379 case Primitive::kPrimLong:
3380 locations->SetInAt(0, Location::RequiresRegister());
3381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3382 break;
3383
3384 case Primitive::kPrimFloat:
3385 case Primitive::kPrimDouble:
3386 locations->SetInAt(0, Location::RequiresFpuRegister());
3387 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3388 break;
3389
3390 default:
3391 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3392 }
3393}
3394
3395void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
3396 Primitive::Type type = instruction->GetType();
3397 LocationSummary* locations = instruction->GetLocations();
3398
3399 switch (type) {
3400 case Primitive::kPrimInt:
3401 case Primitive::kPrimLong: {
3402 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3403 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3404 if (type == Primitive::kPrimInt)
3405 __ Subu(dst, ZERO, src);
3406 else
3407 __ Dsubu(dst, ZERO, src);
3408 break;
3409 }
3410 case Primitive::kPrimFloat:
3411 case Primitive::kPrimDouble: {
3412 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3413 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3414 if (type == Primitive::kPrimFloat)
3415 __ NegS(dst, src);
3416 else
3417 __ NegD(dst, src);
3418 break;
3419 }
3420 default:
3421 LOG(FATAL) << "Unexpected neg type " << type;
3422 }
3423}
3424
3425void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
3426 LocationSummary* locations =
3427 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3428 InvokeRuntimeCallingConvention calling_convention;
3429 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3430 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3431 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3432 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3433}
3434
3435void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
3436 LocationSummary* locations = instruction->GetLocations();
3437 // Move an uint16_t value to a register.
3438 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01003439 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3440 instruction,
3441 instruction->GetDexPc(),
3442 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003443 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
3444}
3445
3446void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
3447 LocationSummary* locations =
3448 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3449 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00003450 if (instruction->IsStringAlloc()) {
3451 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3452 } else {
3453 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3454 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3455 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003456 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3457}
3458
3459void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00003460 if (instruction->IsStringAlloc()) {
3461 // String is allocated through StringFactory. Call NewEmptyString entry point.
3462 GpuRegister temp = instruction->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
Lazar Trsicd9672662015-09-03 17:33:01 +02003463 MemberOffset code_offset =
3464 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64DoublewordSize);
David Brazdil6de19382016-01-08 17:37:10 +00003465 __ LoadFromOffset(kLoadDoubleword, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3466 __ LoadFromOffset(kLoadDoubleword, T9, temp, code_offset.Int32Value());
3467 __ Jalr(T9);
3468 __ Nop();
3469 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3470 } else {
3471 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3472 instruction,
3473 instruction->GetDexPc(),
3474 nullptr);
3475 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3476 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003477}
3478
3479void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
3480 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3481 locations->SetInAt(0, Location::RequiresRegister());
3482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3483}
3484
3485void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
3486 Primitive::Type type = instruction->GetType();
3487 LocationSummary* locations = instruction->GetLocations();
3488
3489 switch (type) {
3490 case Primitive::kPrimInt:
3491 case Primitive::kPrimLong: {
3492 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3493 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3494 __ Nor(dst, src, ZERO);
3495 break;
3496 }
3497
3498 default:
3499 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3500 }
3501}
3502
3503void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3505 locations->SetInAt(0, Location::RequiresRegister());
3506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3507}
3508
3509void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3510 LocationSummary* locations = instruction->GetLocations();
3511 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3512 locations->InAt(0).AsRegister<GpuRegister>(),
3513 1);
3514}
3515
3516void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003517 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3518 ? LocationSummary::kCallOnSlowPath
3519 : LocationSummary::kNoCall;
3520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521 locations->SetInAt(0, Location::RequiresRegister());
3522 if (instruction->HasUses()) {
3523 locations->SetOut(Location::SameAsFirstInput());
3524 }
3525}
3526
Calin Juravle2ae48182016-03-16 14:05:09 +00003527void CodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3528 if (CanMoveNullCheckToUser(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003529 return;
3530 }
3531 Location obj = instruction->GetLocations()->InAt(0);
3532
3533 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00003534 RecordPcInfo(instruction, instruction->GetDexPc());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003535}
3536
Calin Juravle2ae48182016-03-16 14:05:09 +00003537void CodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003538 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00003539 AddSlowPath(slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003540
3541 Location obj = instruction->GetLocations()->InAt(0);
3542
3543 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3544}
3545
3546void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00003547 codegen_->GenerateNullCheck(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003548}
3549
3550void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3551 HandleBinaryOp(instruction);
3552}
3553
3554void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3555 HandleBinaryOp(instruction);
3556}
3557
3558void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3559 LOG(FATAL) << "Unreachable";
3560}
3561
3562void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3563 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3564}
3565
3566void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3567 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3568 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3569 if (location.IsStackSlot()) {
3570 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3571 } else if (location.IsDoubleStackSlot()) {
3572 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3573 }
3574 locations->SetOut(location);
3575}
3576
3577void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3578 ATTRIBUTE_UNUSED) {
3579 // Nothing to do, the parameter is already at its location.
3580}
3581
3582void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3585 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3586}
3587
3588void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3589 ATTRIBUTE_UNUSED) {
3590 // Nothing to do, the method is already at its location.
3591}
3592
3593void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3595 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3596 locations->SetInAt(i, Location::Any());
3597 }
3598 locations->SetOut(Location::Any());
3599}
3600
3601void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3602 LOG(FATAL) << "Unreachable";
3603}
3604
3605void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3606 Primitive::Type type = rem->GetResultType();
3607 LocationSummary::CallKind call_kind =
3608 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3610
3611 switch (type) {
3612 case Primitive::kPrimInt:
3613 case Primitive::kPrimLong:
3614 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunzec857c742015-09-23 15:12:39 -07003615 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Alexey Frunze4dda3372015-06-01 18:31:49 -07003616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3617 break;
3618
3619 case Primitive::kPrimFloat:
3620 case Primitive::kPrimDouble: {
3621 InvokeRuntimeCallingConvention calling_convention;
3622 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3623 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3624 locations->SetOut(calling_convention.GetReturnLocation(type));
3625 break;
3626 }
3627
3628 default:
3629 LOG(FATAL) << "Unexpected rem type " << type;
3630 }
3631}
3632
3633void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3634 Primitive::Type type = instruction->GetType();
Alexey Frunze4dda3372015-06-01 18:31:49 -07003635
3636 switch (type) {
3637 case Primitive::kPrimInt:
Alexey Frunzec857c742015-09-23 15:12:39 -07003638 case Primitive::kPrimLong:
3639 GenerateDivRemIntegral(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003640 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07003641
3642 case Primitive::kPrimFloat:
3643 case Primitive::kPrimDouble: {
3644 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3645 : QUICK_ENTRY_POINT(pFmod);
3646 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003647 if (type == Primitive::kPrimFloat) {
3648 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
3649 } else {
3650 CheckEntrypointTypes<kQuickFmod, double, double, double>();
3651 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003652 break;
3653 }
3654 default:
3655 LOG(FATAL) << "Unexpected rem type " << type;
3656 }
3657}
3658
3659void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3660 memory_barrier->SetLocations(nullptr);
3661}
3662
3663void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3664 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3665}
3666
3667void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3669 Primitive::Type return_type = ret->InputAt(0)->GetType();
3670 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3671}
3672
3673void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3674 codegen_->GenerateFrameExit();
3675}
3676
3677void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3678 ret->SetLocations(nullptr);
3679}
3680
3681void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3682 codegen_->GenerateFrameExit();
3683}
3684
Alexey Frunze92d90602015-12-18 18:16:36 -08003685void LocationsBuilderMIPS64::VisitRor(HRor* ror) {
3686 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003687}
3688
Alexey Frunze92d90602015-12-18 18:16:36 -08003689void InstructionCodeGeneratorMIPS64::VisitRor(HRor* ror) {
3690 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003691}
3692
Alexey Frunze4dda3372015-06-01 18:31:49 -07003693void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3694 HandleShift(shl);
3695}
3696
3697void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3698 HandleShift(shl);
3699}
3700
3701void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3702 HandleShift(shr);
3703}
3704
3705void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3706 HandleShift(shr);
3707}
3708
Alexey Frunze4dda3372015-06-01 18:31:49 -07003709void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3710 HandleBinaryOp(instruction);
3711}
3712
3713void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3714 HandleBinaryOp(instruction);
3715}
3716
3717void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3718 HandleFieldGet(instruction, instruction->GetFieldInfo());
3719}
3720
3721void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3722 HandleFieldGet(instruction, instruction->GetFieldInfo());
3723}
3724
3725void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3726 HandleFieldSet(instruction, instruction->GetFieldInfo());
3727}
3728
3729void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevic8ed18262016-01-22 13:01:00 +01003730 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003731}
3732
Calin Juravlee460d1d2015-09-29 04:52:17 +01003733void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3734 HUnresolvedInstanceFieldGet* instruction) {
3735 FieldAccessCallingConventionMIPS64 calling_convention;
3736 codegen_->CreateUnresolvedFieldLocationSummary(
3737 instruction, instruction->GetFieldType(), calling_convention);
3738}
3739
3740void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3741 HUnresolvedInstanceFieldGet* instruction) {
3742 FieldAccessCallingConventionMIPS64 calling_convention;
3743 codegen_->GenerateUnresolvedFieldAccess(instruction,
3744 instruction->GetFieldType(),
3745 instruction->GetFieldIndex(),
3746 instruction->GetDexPc(),
3747 calling_convention);
3748}
3749
3750void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3751 HUnresolvedInstanceFieldSet* instruction) {
3752 FieldAccessCallingConventionMIPS64 calling_convention;
3753 codegen_->CreateUnresolvedFieldLocationSummary(
3754 instruction, instruction->GetFieldType(), calling_convention);
3755}
3756
3757void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3758 HUnresolvedInstanceFieldSet* instruction) {
3759 FieldAccessCallingConventionMIPS64 calling_convention;
3760 codegen_->GenerateUnresolvedFieldAccess(instruction,
3761 instruction->GetFieldType(),
3762 instruction->GetFieldIndex(),
3763 instruction->GetDexPc(),
3764 calling_convention);
3765}
3766
3767void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3768 HUnresolvedStaticFieldGet* instruction) {
3769 FieldAccessCallingConventionMIPS64 calling_convention;
3770 codegen_->CreateUnresolvedFieldLocationSummary(
3771 instruction, instruction->GetFieldType(), calling_convention);
3772}
3773
3774void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3775 HUnresolvedStaticFieldGet* instruction) {
3776 FieldAccessCallingConventionMIPS64 calling_convention;
3777 codegen_->GenerateUnresolvedFieldAccess(instruction,
3778 instruction->GetFieldType(),
3779 instruction->GetFieldIndex(),
3780 instruction->GetDexPc(),
3781 calling_convention);
3782}
3783
3784void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3785 HUnresolvedStaticFieldSet* instruction) {
3786 FieldAccessCallingConventionMIPS64 calling_convention;
3787 codegen_->CreateUnresolvedFieldLocationSummary(
3788 instruction, instruction->GetFieldType(), calling_convention);
3789}
3790
3791void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3792 HUnresolvedStaticFieldSet* instruction) {
3793 FieldAccessCallingConventionMIPS64 calling_convention;
3794 codegen_->GenerateUnresolvedFieldAccess(instruction,
3795 instruction->GetFieldType(),
3796 instruction->GetFieldIndex(),
3797 instruction->GetDexPc(),
3798 calling_convention);
3799}
3800
Alexey Frunze4dda3372015-06-01 18:31:49 -07003801void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3803}
3804
3805void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3806 HBasicBlock* block = instruction->GetBlock();
3807 if (block->GetLoopInformation() != nullptr) {
3808 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3809 // The back edge will generate the suspend check.
3810 return;
3811 }
3812 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3813 // The goto will generate the suspend check.
3814 return;
3815 }
3816 GenerateSuspendCheck(instruction, nullptr);
3817}
3818
Alexey Frunze4dda3372015-06-01 18:31:49 -07003819void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3820 LocationSummary* locations =
3821 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3822 InvokeRuntimeCallingConvention calling_convention;
3823 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3824}
3825
3826void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3827 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3828 instruction,
3829 instruction->GetDexPc(),
3830 nullptr);
3831 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3832}
3833
3834void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3835 Primitive::Type input_type = conversion->GetInputType();
3836 Primitive::Type result_type = conversion->GetResultType();
3837 DCHECK_NE(input_type, result_type);
3838
3839 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3840 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3841 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3842 }
3843
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003844 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion);
3845
3846 if (Primitive::IsFloatingPointType(input_type)) {
3847 locations->SetInAt(0, Location::RequiresFpuRegister());
3848 } else {
3849 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze4dda3372015-06-01 18:31:49 -07003850 }
3851
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003852 if (Primitive::IsFloatingPointType(result_type)) {
3853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003854 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003856 }
3857}
3858
3859void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3860 LocationSummary* locations = conversion->GetLocations();
3861 Primitive::Type result_type = conversion->GetResultType();
3862 Primitive::Type input_type = conversion->GetInputType();
3863
3864 DCHECK_NE(input_type, result_type);
3865
3866 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3867 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3868 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3869
3870 switch (result_type) {
3871 case Primitive::kPrimChar:
3872 __ Andi(dst, src, 0xFFFF);
3873 break;
3874 case Primitive::kPrimByte:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003875 if (input_type == Primitive::kPrimLong) {
3876 // Type conversion from long to types narrower than int is a result of code
3877 // transformations. To avoid unpredictable results for SEB and SEH, we first
3878 // need to sign-extend the low 32-bit value into bits 32 through 63.
3879 __ Sll(dst, src, 0);
3880 __ Seb(dst, dst);
3881 } else {
3882 __ Seb(dst, src);
3883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003884 break;
3885 case Primitive::kPrimShort:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003886 if (input_type == Primitive::kPrimLong) {
3887 // Type conversion from long to types narrower than int is a result of code
3888 // transformations. To avoid unpredictable results for SEB and SEH, we first
3889 // need to sign-extend the low 32-bit value into bits 32 through 63.
3890 __ Sll(dst, src, 0);
3891 __ Seh(dst, dst);
3892 } else {
3893 __ Seh(dst, src);
3894 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003895 break;
3896 case Primitive::kPrimInt:
3897 case Primitive::kPrimLong:
3898 // Sign-extend 32-bit int into bits 32 through 63 for
3899 // int-to-long and long-to-int conversions
3900 __ Sll(dst, src, 0);
3901 break;
3902
3903 default:
3904 LOG(FATAL) << "Unexpected type conversion from " << input_type
3905 << " to " << result_type;
3906 }
3907 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003908 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3909 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3910 if (input_type == Primitive::kPrimLong) {
3911 __ Dmtc1(src, FTMP);
3912 if (result_type == Primitive::kPrimFloat) {
3913 __ Cvtsl(dst, FTMP);
3914 } else {
3915 __ Cvtdl(dst, FTMP);
3916 }
3917 } else {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003918 __ Mtc1(src, FTMP);
3919 if (result_type == Primitive::kPrimFloat) {
3920 __ Cvtsw(dst, FTMP);
3921 } else {
3922 __ Cvtdw(dst, FTMP);
3923 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003924 }
3925 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3926 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003927 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3928 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3929 Mips64Label truncate;
3930 Mips64Label done;
3931
3932 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
3933 // value when the input is either a NaN or is outside of the range of the output type
3934 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
3935 // the same result.
3936 //
3937 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
3938 // value of the output type if the input is outside of the range after the truncation or
3939 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
3940 // results. This matches the desired float/double-to-int/long conversion exactly.
3941 //
3942 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
3943 //
3944 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
3945 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
3946 // even though it must be NAN2008=1 on R6.
3947 //
3948 // The code takes care of the different behaviors by first comparing the input to the
3949 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
3950 // If the input is greater than or equal to the minimum, it procedes to the truncate
3951 // instruction, which will handle such an input the same way irrespective of NAN2008.
3952 // Otherwise the input is compared to itself to determine whether it is a NaN or not
3953 // in order to return either zero or the minimum value.
3954 //
3955 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
3956 // truncate instruction for MIPS64R6.
3957 if (input_type == Primitive::kPrimFloat) {
3958 uint32_t min_val = (result_type == Primitive::kPrimLong)
3959 ? bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min())
3960 : bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
3961 __ LoadConst32(TMP, min_val);
3962 __ Mtc1(TMP, FTMP);
3963 __ CmpLeS(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003964 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003965 uint64_t min_val = (result_type == Primitive::kPrimLong)
3966 ? bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min())
3967 : bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
3968 __ LoadConst64(TMP, min_val);
3969 __ Dmtc1(TMP, FTMP);
3970 __ CmpLeD(FTMP, FTMP, src);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003971 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003972
3973 __ Bc1nez(FTMP, &truncate);
3974
3975 if (input_type == Primitive::kPrimFloat) {
3976 __ CmpEqS(FTMP, src, src);
3977 } else {
3978 __ CmpEqD(FTMP, src, src);
3979 }
3980 if (result_type == Primitive::kPrimLong) {
3981 __ LoadConst64(dst, std::numeric_limits<int64_t>::min());
3982 } else {
3983 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
3984 }
3985 __ Mfc1(TMP, FTMP);
3986 __ And(dst, dst, TMP);
3987
3988 __ Bc(&done);
3989
3990 __ Bind(&truncate);
3991
3992 if (result_type == Primitive::kPrimLong) {
Roland Levillain888d0672015-11-23 18:53:50 +00003993 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003994 __ TruncLS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003995 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003996 __ TruncLD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00003997 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08003998 __ Dmfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00003999 } else {
4000 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004001 __ TruncWS(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004002 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004003 __ TruncWD(FTMP, src);
Roland Levillain888d0672015-11-23 18:53:50 +00004004 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004005 __ Mfc1(dst, FTMP);
Roland Levillain888d0672015-11-23 18:53:50 +00004006 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004007
4008 __ Bind(&done);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004009 } else if (Primitive::IsFloatingPointType(result_type) &&
4010 Primitive::IsFloatingPointType(input_type)) {
4011 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
4012 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
4013 if (result_type == Primitive::kPrimFloat) {
4014 __ Cvtsd(dst, src);
4015 } else {
4016 __ Cvtds(dst, src);
4017 }
4018 } else {
4019 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4020 << " to " << result_type;
4021 }
4022}
4023
4024void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
4025 HandleShift(ushr);
4026}
4027
4028void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
4029 HandleShift(ushr);
4030}
4031
4032void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
4033 HandleBinaryOp(instruction);
4034}
4035
4036void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
4037 HandleBinaryOp(instruction);
4038}
4039
4040void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4041 // Nothing to do, this should be removed during prepare for register allocator.
4042 LOG(FATAL) << "Unreachable";
4043}
4044
4045void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4046 // Nothing to do, this should be removed during prepare for register allocator.
4047 LOG(FATAL) << "Unreachable";
4048}
4049
4050void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004051 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004052}
4053
4054void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004055 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004056}
4057
4058void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004059 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004060}
4061
4062void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004063 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004064}
4065
4066void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004067 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004068}
4069
4070void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004071 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004072}
4073
4074void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004075 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004076}
4077
4078void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004079 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004080}
4081
4082void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004083 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004084}
4085
4086void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004087 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004088}
4089
4090void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004091 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004092}
4093
4094void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004095 HandleCondition(comp);
Alexey Frunze4dda3372015-06-01 18:31:49 -07004096}
4097
Aart Bike9f37602015-10-09 11:15:55 -07004098void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004099 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004100}
4101
4102void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004103 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004104}
4105
4106void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004108}
4109
4110void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004112}
4113
4114void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004116}
4117
4118void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004120}
4121
4122void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004124}
4125
4126void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07004128}
4129
Mark Mendellfe57faa2015-09-18 09:26:15 -04004130// Simple implementation of packed switch - generate cascaded compare/jumps.
4131void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4132 LocationSummary* locations =
4133 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4134 locations->SetInAt(0, Location::RequiresRegister());
4135}
4136
4137void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4138 int32_t lower_bound = switch_instr->GetStartValue();
4139 int32_t num_entries = switch_instr->GetNumEntries();
4140 LocationSummary* locations = switch_instr->GetLocations();
4141 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
4142 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4143
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004144 // Create a set of compare/jumps.
4145 GpuRegister temp_reg = TMP;
4146 if (IsInt<16>(-lower_bound)) {
4147 __ Addiu(temp_reg, value_reg, -lower_bound);
4148 } else {
4149 __ LoadConst32(AT, -lower_bound);
4150 __ Addu(temp_reg, value_reg, AT);
4151 }
4152 // Jump to default if index is negative
4153 // Note: We don't check the case that index is positive while value < lower_bound, because in
4154 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4155 __ Bltzc(temp_reg, codegen_->GetLabelOf(default_block));
4156
Mark Mendellfe57faa2015-09-18 09:26:15 -04004157 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004158 // Jump to successors[0] if value == lower_bound.
4159 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[0]));
4160 int32_t last_index = 0;
4161 for (; num_entries - last_index > 2; last_index += 2) {
4162 __ Addiu(temp_reg, temp_reg, -2);
4163 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4164 __ Bltzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4165 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4166 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4167 }
4168 if (num_entries - last_index == 2) {
4169 // The last missing case_value.
4170 __ Addiu(temp_reg, temp_reg, -1);
4171 __ Beqzc(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004172 }
4173
4174 // And the default for any other value.
4175 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
Alexey Frunzea0e87b02015-09-24 22:57:20 -07004176 __ Bc(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04004177 }
4178}
4179
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004180void LocationsBuilderMIPS64::VisitClassTableGet(HClassTableGet*) {
4181 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4182}
4183
4184void InstructionCodeGeneratorMIPS64::VisitClassTableGet(HClassTableGet*) {
4185 UNIMPLEMENTED(FATAL) << "ClassTableGet is unimplemented on mips64";
4186}
4187
Alexey Frunze4dda3372015-06-01 18:31:49 -07004188} // namespace mips64
4189} // namespace art