blob: eb20291e20d3723b4e14e9c9caff5319762e2daa [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
19#include "entrypoints/quick/quick_entrypoints.h"
20#include "entrypoints/quick/quick_entrypoints_enum.h"
21#include "gc/accounting/card_table.h"
22#include "intrinsics.h"
Chris Larsen3039e382015-08-26 07:54:08 -070023#include "intrinsics_mips64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070024#include "art_method.h"
Chris Larsen3039e382015-08-26 07:54:08 -070025#include "code_generator_utils.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"
30#include "utils/mips64/assembler_mips64.h"
31#include "utils/assembler.h"
32#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips64 {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr GpuRegister kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
Alexey Frunze4dda3372015-06-01 18:31:49 -070041static constexpr FpuRegister FTMP = F8;
42
Alexey Frunze4dda3372015-06-01 18:31:49 -070043Location Mips64ReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 case Primitive::kPrimLong:
52 return Location::RegisterLocation(V0);
53
54 case Primitive::kPrimFloat:
55 case Primitive::kPrimDouble:
56 return Location::FpuRegisterLocation(F0);
57
58 case Primitive::kPrimVoid:
59 return Location();
60 }
61 UNREACHABLE();
62}
63
64Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
65 return Mips64ReturnLocation(type);
66}
67
68Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
69 return Location::RegisterLocation(kMethodRegisterArgument);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
73 Location next_location;
74 if (type == Primitive::kPrimVoid) {
75 LOG(FATAL) << "Unexpected parameter type " << type;
76 }
77
78 if (Primitive::IsFloatingPointType(type) &&
79 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
80 next_location = Location::FpuRegisterLocation(
81 calling_convention.GetFpuRegisterAt(float_index_++));
82 gp_index_++;
83 } else if (!Primitive::IsFloatingPointType(type) &&
84 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
86 float_index_++;
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
90 : Location::StackSlot(stack_offset);
91 }
92
93 // Space on the stack is reserved for all arguments.
94 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
95
96 // TODO: review
97
98 // TODO: shouldn't we use a whole machine word per argument on the stack?
99 // Implicit 4-byte method pointer (and such) will cause misalignment.
100
101 return next_location;
102}
103
104Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
105 return Mips64ReturnLocation(type);
106}
107
108#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
109#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
110
111class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
112 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100113 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700114
115 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100116 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700117 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
118 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000119 if (instruction_->CanThrowIntoCatchBlock()) {
120 // Live registers will be restored in the catch block if caught.
121 SaveLiveRegisters(codegen, instruction_->GetLocations());
122 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700123 // We're moving two locations to locations that could overlap, so we need a parallel
124 // move resolver.
125 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100126 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700127 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
128 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700130 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
131 Primitive::kPrimInt);
132 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
133 instruction_,
134 instruction_->GetDexPc(),
135 this);
136 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
137 }
138
Alexandre Rames8158f282015-08-07 10:26:17 +0100139 bool IsFatal() const OVERRIDE { return true; }
140
Roland Levillain46648892015-06-19 16:07:18 +0100141 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
142
Alexey Frunze4dda3372015-06-01 18:31:49 -0700143 private:
144 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145
146 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
147};
148
149class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
150 public:
151 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
152
153 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
154 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
155 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700160 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
161 instruction_,
162 instruction_->GetDexPc(),
163 this);
164 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
165 }
166
Alexandre Rames8158f282015-08-07 10:26:17 +0100167 bool IsFatal() const OVERRIDE { return true; }
168
Roland Levillain46648892015-06-19 16:07:18 +0100169 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
170
Alexey Frunze4dda3372015-06-01 18:31:49 -0700171 private:
172 HDivZeroCheck* const instruction_;
173 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
174};
175
176class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
177 public:
178 LoadClassSlowPathMIPS64(HLoadClass* cls,
179 HInstruction* at,
180 uint32_t dex_pc,
181 bool do_clinit)
182 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
183 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
184 }
185
186 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
187 LocationSummary* locations = at_->GetLocations();
188 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
189
190 __ Bind(GetEntryLabel());
191 SaveLiveRegisters(codegen, locations);
192
193 InvokeRuntimeCallingConvention calling_convention;
194 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
195 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
196 : QUICK_ENTRY_POINT(pInitializeType);
197 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
198 if (do_clinit_) {
199 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
200 } else {
201 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
202 }
203
204 // Move the class to the desired location.
205 Location out = locations->Out();
206 if (out.IsValid()) {
207 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
208 Primitive::Type type = at_->GetType();
209 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
210 }
211
212 RestoreLiveRegisters(codegen, locations);
213 __ B(GetExitLabel());
214 }
215
Roland Levillain46648892015-06-19 16:07:18 +0100216 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
217
Alexey Frunze4dda3372015-06-01 18:31:49 -0700218 private:
219 // The class this slow path will load.
220 HLoadClass* const cls_;
221
222 // The instruction where this slow path is happening.
223 // (Might be the load class or an initialization check).
224 HInstruction* const at_;
225
226 // The dex PC of `at_`.
227 const uint32_t dex_pc_;
228
229 // Whether to initialize the class.
230 const bool do_clinit_;
231
232 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
233};
234
235class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
236 public:
237 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = instruction_->GetLocations();
241 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
242 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
243
244 __ Bind(GetEntryLabel());
245 SaveLiveRegisters(codegen, locations);
246
247 InvokeRuntimeCallingConvention calling_convention;
248 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
249 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
250 instruction_,
251 instruction_->GetDexPc(),
252 this);
253 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
254 Primitive::Type type = instruction_->GetType();
255 mips64_codegen->MoveLocation(locations->Out(),
256 calling_convention.GetReturnLocation(type),
257 type);
258
259 RestoreLiveRegisters(codegen, locations);
260 __ B(GetExitLabel());
261 }
262
Roland Levillain46648892015-06-19 16:07:18 +0100263 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
264
Alexey Frunze4dda3372015-06-01 18:31:49 -0700265 private:
266 HLoadString* const instruction_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
269};
270
271class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
272 public:
273 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
274
275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
276 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
277 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700282 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
283 instruction_,
284 instruction_->GetDexPc(),
285 this);
286 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
287 }
288
Alexandre Rames8158f282015-08-07 10:26:17 +0100289 bool IsFatal() const OVERRIDE { return true; }
290
Roland Levillain46648892015-06-19 16:07:18 +0100291 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
292
Alexey Frunze4dda3372015-06-01 18:31:49 -0700293 private:
294 HNullCheck* const instruction_;
295
296 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
297};
298
299class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
300 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100301 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700302 : instruction_(instruction), successor_(successor) {}
303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
305 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
306 __ Bind(GetEntryLabel());
307 SaveLiveRegisters(codegen, instruction_->GetLocations());
308 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
309 instruction_,
310 instruction_->GetDexPc(),
311 this);
312 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
313 RestoreLiveRegisters(codegen, instruction_->GetLocations());
314 if (successor_ == nullptr) {
315 __ B(GetReturnLabel());
316 } else {
317 __ B(mips64_codegen->GetLabelOf(successor_));
318 }
319 }
320
321 Label* GetReturnLabel() {
322 DCHECK(successor_ == nullptr);
323 return &return_label_;
324 }
325
Roland Levillain46648892015-06-19 16:07:18 +0100326 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
327
Alexey Frunze4dda3372015-06-01 18:31:49 -0700328 private:
329 HSuspendCheck* const instruction_;
330 // If not null, the block to branch to after the suspend check.
331 HBasicBlock* const successor_;
332
333 // If `successor_` is null, the label to branch to after the suspend check.
334 Label return_label_;
335
336 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
337};
338
339class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
340 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100341 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100345 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
346 : locations->Out();
347 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700348 DCHECK(instruction_->IsCheckCast()
349 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
350 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
351
352 __ Bind(GetEntryLabel());
353 SaveLiveRegisters(codegen, locations);
354
355 // We're moving two locations to locations that could overlap, so we need a parallel
356 // move resolver.
357 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100358 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700359 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
360 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100361 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700362 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
363 Primitive::kPrimNot);
364
365 if (instruction_->IsInstanceOf()) {
366 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
367 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100368 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700369 this);
370 Primitive::Type ret_type = instruction_->GetType();
371 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
372 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
373 CheckEntrypointTypes<kQuickInstanceofNonTrivial,
374 uint32_t,
375 const mirror::Class*,
376 const mirror::Class*>();
377 } else {
378 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100379 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700380 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
381 }
382
383 RestoreLiveRegisters(codegen, locations);
384 __ B(GetExitLabel());
385 }
386
Roland Levillain46648892015-06-19 16:07:18 +0100387 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
388
Alexey Frunze4dda3372015-06-01 18:31:49 -0700389 private:
390 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391
392 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
393};
394
395class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
396 public:
397 explicit DeoptimizationSlowPathMIPS64(HInstruction* instruction)
398 : instruction_(instruction) {}
399
400 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
401 __ Bind(GetEntryLabel());
402 SaveLiveRegisters(codegen, instruction_->GetLocations());
403 DCHECK(instruction_->IsDeoptimize());
404 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
405 uint32_t dex_pc = deoptimize->GetDexPc();
406 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
407 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
408 }
409
Roland Levillain46648892015-06-19 16:07:18 +0100410 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
411
Alexey Frunze4dda3372015-06-01 18:31:49 -0700412 private:
413 HInstruction* const instruction_;
414 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
415};
416
417CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
418 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100419 const CompilerOptions& compiler_options,
420 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700421 : CodeGenerator(graph,
422 kNumberOfGpuRegisters,
423 kNumberOfFpuRegisters,
424 0, // kNumberOfRegisterPairs
425 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
426 arraysize(kCoreCalleeSaves)),
427 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
428 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100429 compiler_options,
430 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100431 block_labels_(nullptr),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700432 location_builder_(graph, this),
433 instruction_visitor_(graph, this),
434 move_resolver_(graph->GetArena(), this),
435 isa_features_(isa_features) {
436 // Save RA (containing the return address) to mimic Quick.
437 AddAllocatedRegister(Location::RegisterLocation(RA));
438}
439
440#undef __
441#define __ down_cast<Mips64Assembler*>(GetAssembler())->
442#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
443
444void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
445 CodeGenerator::Finalize(allocator);
446}
447
448Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
449 return codegen_->GetAssembler();
450}
451
452void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100453 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700454 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
455}
456
457void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100458 MoveOperands* move = moves_[index];
Alexey Frunze4dda3372015-06-01 18:31:49 -0700459 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
460}
461
462void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
463 // Pop reg
464 __ Ld(GpuRegister(reg), SP, 0);
465 __ DecreaseFrameSize(kMips64WordSize);
466}
467
468void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
469 // Push reg
470 __ IncreaseFrameSize(kMips64WordSize);
471 __ Sd(GpuRegister(reg), SP, 0);
472}
473
474void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
475 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
476 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
477 // Allocate a scratch register other than TMP, if available.
478 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
479 // automatically unspilled when the scratch scope object is destroyed).
480 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
481 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
482 int stack_offset = ensure_scratch.IsSpilled() ? kMips64WordSize : 0;
483 __ LoadFromOffset(load_type,
484 GpuRegister(ensure_scratch.GetRegister()),
485 SP,
486 index1 + stack_offset);
487 __ LoadFromOffset(load_type,
488 TMP,
489 SP,
490 index2 + stack_offset);
491 __ StoreToOffset(store_type,
492 GpuRegister(ensure_scratch.GetRegister()),
493 SP,
494 index2 + stack_offset);
495 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
496}
497
498static dwarf::Reg DWARFReg(GpuRegister reg) {
499 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
500}
501
502// TODO: mapping of floating-point registers to DWARF
503
504void CodeGeneratorMIPS64::GenerateFrameEntry() {
505 __ Bind(&frame_entry_label_);
506
507 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
508
509 if (do_overflow_check) {
510 __ LoadFromOffset(kLoadWord,
511 ZERO,
512 SP,
513 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
514 RecordPcInfo(nullptr, 0);
515 }
516
517 // TODO: anything related to T9/GP/GOT/PIC/.so's?
518
519 if (HasEmptyFrame()) {
520 return;
521 }
522
523 // Make sure the frame size isn't unreasonably large. Per the various APIs
524 // it looks like it should always be less than 2GB in size, which allows
525 // us using 32-bit signed offsets from the stack pointer.
526 if (GetFrameSize() > 0x7FFFFFFF)
527 LOG(FATAL) << "Stack frame larger than 2GB";
528
529 // Spill callee-saved registers.
530 // Note that their cumulative size is small and they can be indexed using
531 // 16-bit offsets.
532
533 // TODO: increment/decrement SP in one step instead of two or remove this comment.
534
535 uint32_t ofs = FrameEntrySpillSize();
536 __ IncreaseFrameSize(ofs);
537
538 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
539 GpuRegister reg = kCoreCalleeSaves[i];
540 if (allocated_registers_.ContainsCoreRegister(reg)) {
541 ofs -= kMips64WordSize;
542 __ Sd(reg, SP, ofs);
543 __ cfi().RelOffset(DWARFReg(reg), ofs);
544 }
545 }
546
547 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
548 FpuRegister reg = kFpuCalleeSaves[i];
549 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
550 ofs -= kMips64WordSize;
551 __ Sdc1(reg, SP, ofs);
552 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
553 }
554 }
555
556 // Allocate the rest of the frame and store the current method pointer
557 // at its end.
558
559 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
560
561 static_assert(IsInt<16>(kCurrentMethodStackOffset),
562 "kCurrentMethodStackOffset must fit into int16_t");
563 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
564}
565
566void CodeGeneratorMIPS64::GenerateFrameExit() {
567 __ cfi().RememberState();
568
569 // TODO: anything related to T9/GP/GOT/PIC/.so's?
570
571 if (!HasEmptyFrame()) {
572 // Deallocate the rest of the frame.
573
574 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
575
576 // Restore callee-saved registers.
577 // Note that their cumulative size is small and they can be indexed using
578 // 16-bit offsets.
579
580 // TODO: increment/decrement SP in one step instead of two or remove this comment.
581
582 uint32_t ofs = 0;
583
584 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
585 FpuRegister reg = kFpuCalleeSaves[i];
586 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
587 __ Ldc1(reg, SP, ofs);
588 ofs += kMips64WordSize;
589 // TODO: __ cfi().Restore(DWARFReg(reg));
590 }
591 }
592
593 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
594 GpuRegister reg = kCoreCalleeSaves[i];
595 if (allocated_registers_.ContainsCoreRegister(reg)) {
596 __ Ld(reg, SP, ofs);
597 ofs += kMips64WordSize;
598 __ cfi().Restore(DWARFReg(reg));
599 }
600 }
601
602 DCHECK_EQ(ofs, FrameEntrySpillSize());
603 __ DecreaseFrameSize(ofs);
604 }
605
606 __ Jr(RA);
607
608 __ cfi().RestoreState();
609 __ cfi().DefCFAOffset(GetFrameSize());
610}
611
612void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
613 __ Bind(GetLabelOf(block));
614}
615
616void CodeGeneratorMIPS64::MoveLocation(Location destination,
617 Location source,
Calin Juravlee460d1d2015-09-29 04:52:17 +0100618 Primitive::Type dst_type) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700619 if (source.Equals(destination)) {
620 return;
621 }
622
623 // A valid move can always be inferred from the destination and source
624 // locations. When moving from and to a register, the argument type can be
625 // used to generate 32bit instead of 64bit moves.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100626 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700627 DCHECK_EQ(unspecified_type, false);
628
629 if (destination.IsRegister() || destination.IsFpuRegister()) {
630 if (unspecified_type) {
631 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
632 if (source.IsStackSlot() ||
633 (src_cst != nullptr && (src_cst->IsIntConstant()
634 || src_cst->IsFloatConstant()
635 || src_cst->IsNullConstant()))) {
636 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100637 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700638 } else {
639 // If the source is a double stack slot or a 64bit constant, a 64bit
640 // type is appropriate. Else the source is a register, and since the
641 // type has not been specified, we chose a 64bit type to force a 64bit
642 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +0100643 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700644 }
645 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100646 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
647 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700648 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
649 // Move to GPR/FPR from stack
650 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100651 if (Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700652 __ LoadFpuFromOffset(load_type,
653 destination.AsFpuRegister<FpuRegister>(),
654 SP,
655 source.GetStackIndex());
656 } else {
657 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
658 __ LoadFromOffset(load_type,
659 destination.AsRegister<GpuRegister>(),
660 SP,
661 source.GetStackIndex());
662 }
663 } else if (source.IsConstant()) {
664 // Move to GPR/FPR from constant
665 GpuRegister gpr = AT;
Calin Juravlee460d1d2015-09-29 04:52:17 +0100666 if (!Primitive::IsFloatingPointType(dst_type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700667 gpr = destination.AsRegister<GpuRegister>();
668 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100669 if (dst_type == Primitive::kPrimInt || dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700670 __ LoadConst32(gpr, GetInt32ValueOf(source.GetConstant()->AsConstant()));
671 } else {
672 __ LoadConst64(gpr, GetInt64ValueOf(source.GetConstant()->AsConstant()));
673 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100674 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700675 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
Calin Juravlee460d1d2015-09-29 04:52:17 +0100676 } else if (dst_type == Primitive::kPrimDouble) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700677 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
678 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100679 } else if (source.IsRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700680 if (destination.IsRegister()) {
681 // Move to GPR from GPR
682 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
683 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100684 DCHECK(destination.IsFpuRegister());
685 if (Primitive::Is64BitType(dst_type)) {
686 __ Dmtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
687 } else {
688 __ Mtc1(source.AsRegister<GpuRegister>(), destination.AsFpuRegister<FpuRegister>());
689 }
690 }
691 } else if (source.IsFpuRegister()) {
692 if (destination.IsFpuRegister()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700693 // Move to FPR from FPR
Calin Juravlee460d1d2015-09-29 04:52:17 +0100694 if (dst_type == Primitive::kPrimFloat) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700695 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
696 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100697 DCHECK_EQ(dst_type, Primitive::kPrimDouble);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700698 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
699 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100700 } else {
701 DCHECK(destination.IsRegister());
702 if (Primitive::Is64BitType(dst_type)) {
703 __ Dmfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
704 } else {
705 __ Mfc1(destination.AsRegister<GpuRegister>(), source.AsFpuRegister<FpuRegister>());
706 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700707 }
708 }
709 } else { // The destination is not a register. It must be a stack slot.
710 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
711 if (source.IsRegister() || source.IsFpuRegister()) {
712 if (unspecified_type) {
713 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100714 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700715 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100716 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 }
718 }
Calin Juravlee460d1d2015-09-29 04:52:17 +0100719 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
720 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
Alexey Frunze4dda3372015-06-01 18:31:49 -0700721 // Move to stack from GPR/FPR
722 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
723 if (source.IsRegister()) {
724 __ StoreToOffset(store_type,
725 source.AsRegister<GpuRegister>(),
726 SP,
727 destination.GetStackIndex());
728 } else {
729 __ StoreFpuToOffset(store_type,
730 source.AsFpuRegister<FpuRegister>(),
731 SP,
732 destination.GetStackIndex());
733 }
734 } else if (source.IsConstant()) {
735 // Move to stack from constant
736 HConstant* src_cst = source.GetConstant();
737 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
738 if (destination.IsStackSlot()) {
739 __ LoadConst32(TMP, GetInt32ValueOf(src_cst->AsConstant()));
740 } else {
741 __ LoadConst64(TMP, GetInt64ValueOf(src_cst->AsConstant()));
742 }
743 __ StoreToOffset(store_type, TMP, SP, destination.GetStackIndex());
744 } else {
745 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
746 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
747 // Move to stack from stack
748 if (destination.IsStackSlot()) {
749 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
750 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
751 } else {
752 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
753 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
754 }
755 }
756 }
757}
758
759void CodeGeneratorMIPS64::SwapLocations(Location loc1,
760 Location loc2,
761 Primitive::Type type ATTRIBUTE_UNUSED) {
762 DCHECK(!loc1.IsConstant());
763 DCHECK(!loc2.IsConstant());
764
765 if (loc1.Equals(loc2)) {
766 return;
767 }
768
769 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
770 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
771 bool is_fp_reg1 = loc1.IsFpuRegister();
772 bool is_fp_reg2 = loc2.IsFpuRegister();
773
774 if (loc2.IsRegister() && loc1.IsRegister()) {
775 // Swap 2 GPRs
776 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
777 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
778 __ Move(TMP, r2);
779 __ Move(r2, r1);
780 __ Move(r1, TMP);
781 } else if (is_fp_reg2 && is_fp_reg1) {
782 // Swap 2 FPRs
783 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
784 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
785 // TODO: Can MOV.S/MOV.D be used here to save one instruction?
786 // Need to distinguish float from double, right?
787 __ Dmfc1(TMP, r2);
788 __ Dmfc1(AT, r1);
789 __ Dmtc1(TMP, r1);
790 __ Dmtc1(AT, r2);
791 } else if (is_slot1 != is_slot2) {
792 // Swap GPR/FPR and stack slot
793 Location reg_loc = is_slot1 ? loc2 : loc1;
794 Location mem_loc = is_slot1 ? loc1 : loc2;
795 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
796 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
797 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
798 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
799 if (reg_loc.IsFpuRegister()) {
800 __ StoreFpuToOffset(store_type,
801 reg_loc.AsFpuRegister<FpuRegister>(),
802 SP,
803 mem_loc.GetStackIndex());
804 // TODO: review this MTC1/DMTC1 move
805 if (mem_loc.IsStackSlot()) {
806 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
807 } else {
808 DCHECK(mem_loc.IsDoubleStackSlot());
809 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
810 }
811 } else {
812 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
813 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
814 }
815 } else if (is_slot1 && is_slot2) {
816 move_resolver_.Exchange(loc1.GetStackIndex(),
817 loc2.GetStackIndex(),
818 loc1.IsDoubleStackSlot());
819 } else {
820 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
821 }
822}
823
824void CodeGeneratorMIPS64::Move(HInstruction* instruction,
825 Location location,
826 HInstruction* move_for) {
827 LocationSummary* locations = instruction->GetLocations();
828 Primitive::Type type = instruction->GetType();
829 DCHECK_NE(type, Primitive::kPrimVoid);
830
831 if (instruction->IsCurrentMethod()) {
832 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
833 } else if (locations != nullptr && locations->Out().Equals(location)) {
834 return;
835 } else if (instruction->IsIntConstant()
836 || instruction->IsLongConstant()
837 || instruction->IsNullConstant()) {
838 if (location.IsRegister()) {
839 // Move to GPR from constant
840 GpuRegister dst = location.AsRegister<GpuRegister>();
841 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
842 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
843 } else {
844 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
845 }
846 } else {
847 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
848 // Move to stack from constant
849 if (location.IsStackSlot()) {
850 __ LoadConst32(TMP, GetInt32ValueOf(instruction->AsConstant()));
851 __ StoreToOffset(kStoreWord, TMP, SP, location.GetStackIndex());
852 } else {
853 __ LoadConst64(TMP, instruction->AsLongConstant()->GetValue());
854 __ StoreToOffset(kStoreDoubleword, TMP, SP, location.GetStackIndex());
855 }
856 }
857 } else if (instruction->IsTemporary()) {
858 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
859 MoveLocation(location, temp_location, type);
860 } else if (instruction->IsLoadLocal()) {
861 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
862 if (Primitive::Is64BitType(type)) {
863 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
864 } else {
865 MoveLocation(location, Location::StackSlot(stack_slot), type);
866 }
867 } else {
868 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
869 MoveLocation(location, locations->Out(), type);
870 }
871}
872
Calin Juravle175dc732015-08-25 15:42:32 +0100873void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
874 DCHECK(location.IsRegister());
875 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
876}
877
Calin Juravlee460d1d2015-09-29 04:52:17 +0100878void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
879 if (location.IsRegister()) {
880 locations->AddTemp(location);
881 } else {
882 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
883 }
884}
885
Alexey Frunze4dda3372015-06-01 18:31:49 -0700886Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
887 Primitive::Type type = load->GetType();
888
889 switch (type) {
890 case Primitive::kPrimNot:
891 case Primitive::kPrimInt:
892 case Primitive::kPrimFloat:
893 return Location::StackSlot(GetStackSlot(load->GetLocal()));
894
895 case Primitive::kPrimLong:
896 case Primitive::kPrimDouble:
897 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
898
899 case Primitive::kPrimBoolean:
900 case Primitive::kPrimByte:
901 case Primitive::kPrimChar:
902 case Primitive::kPrimShort:
903 case Primitive::kPrimVoid:
904 LOG(FATAL) << "Unexpected type " << type;
905 }
906
907 LOG(FATAL) << "Unreachable";
908 return Location::NoLocation();
909}
910
911void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object, GpuRegister value) {
912 Label done;
913 GpuRegister card = AT;
914 GpuRegister temp = TMP;
915 __ Beqzc(value, &done);
916 __ LoadFromOffset(kLoadDoubleword,
917 card,
918 TR,
919 Thread::CardTableOffset<kMips64WordSize>().Int32Value());
920 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
921 __ Daddu(temp, card, temp);
922 __ Sb(card, temp, 0);
923 __ Bind(&done);
924}
925
926void CodeGeneratorMIPS64::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
927 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
928 blocked_core_registers_[ZERO] = true;
929 blocked_core_registers_[K0] = true;
930 blocked_core_registers_[K1] = true;
931 blocked_core_registers_[GP] = true;
932 blocked_core_registers_[SP] = true;
933 blocked_core_registers_[RA] = true;
934
935 // AT and TMP(T8) are used as temporary/scratch registers
936 // (similar to how AT is used by MIPS assemblers).
937 blocked_core_registers_[AT] = true;
938 blocked_core_registers_[TMP] = true;
939 blocked_fpu_registers_[FTMP] = true;
940
941 // Reserve suspend and thread registers.
942 blocked_core_registers_[S0] = true;
943 blocked_core_registers_[TR] = true;
944
945 // Reserve T9 for function calls
946 blocked_core_registers_[T9] = true;
947
948 // TODO: review; anything else?
949
950 // TODO: make these two for's conditional on is_baseline once
951 // all the issues with register saving/restoring are sorted out.
952 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
953 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
954 }
955
956 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
957 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
958 }
959}
960
961Location CodeGeneratorMIPS64::AllocateFreeRegister(Primitive::Type type) const {
962 if (type == Primitive::kPrimVoid) {
963 LOG(FATAL) << "Unreachable type " << type;
964 }
965
966 if (Primitive::IsFloatingPointType(type)) {
967 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFpuRegisters);
968 return Location::FpuRegisterLocation(reg);
969 } else {
970 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfGpuRegisters);
971 return Location::RegisterLocation(reg);
972 }
973}
974
975size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
976 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
977 return kMips64WordSize;
978}
979
980size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
981 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
982 return kMips64WordSize;
983}
984
985size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
986 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
987 return kMips64WordSize;
988}
989
990size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
991 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
992 return kMips64WordSize;
993}
994
995void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +0100996 stream << GpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700997}
998
999void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdil9f0dece2015-09-21 18:20:26 +01001000 stream << FpuRegister(reg);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001001}
1002
Calin Juravle175dc732015-08-25 15:42:32 +01001003void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1004 HInstruction* instruction,
1005 uint32_t dex_pc,
1006 SlowPathCode* slow_path) {
1007 InvokeRuntime(GetThreadOffset<kMips64WordSize>(entrypoint).Int32Value(),
1008 instruction,
1009 dex_pc,
1010 slow_path);
1011}
1012
Alexey Frunze4dda3372015-06-01 18:31:49 -07001013void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1014 HInstruction* instruction,
1015 uint32_t dex_pc,
1016 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001017 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001018 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1019 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1020 __ Jalr(T9);
1021 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001022}
1023
1024void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1025 GpuRegister class_reg) {
1026 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1027 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1028 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1029 // TODO: barrier needed?
1030 __ Bind(slow_path->GetExitLabel());
1031}
1032
1033void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1034 __ Sync(0); // only stype 0 is supported
1035}
1036
1037void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1038 HBasicBlock* successor) {
1039 SuspendCheckSlowPathMIPS64* slow_path =
1040 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1041 codegen_->AddSlowPath(slow_path);
1042
1043 __ LoadFromOffset(kLoadUnsignedHalfword,
1044 TMP,
1045 TR,
1046 Thread::ThreadFlagsOffset<kMips64WordSize>().Int32Value());
1047 if (successor == nullptr) {
1048 __ Bnezc(TMP, slow_path->GetEntryLabel());
1049 __ Bind(slow_path->GetReturnLabel());
1050 } else {
1051 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
1052 __ B(slow_path->GetEntryLabel());
1053 // slow_path will return to GetLabelOf(successor).
1054 }
1055}
1056
1057InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1058 CodeGeneratorMIPS64* codegen)
1059 : HGraphVisitor(graph),
1060 assembler_(codegen->GetAssembler()),
1061 codegen_(codegen) {}
1062
1063void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1064 DCHECK_EQ(instruction->InputCount(), 2U);
1065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1066 Primitive::Type type = instruction->GetResultType();
1067 switch (type) {
1068 case Primitive::kPrimInt:
1069 case Primitive::kPrimLong: {
1070 locations->SetInAt(0, Location::RequiresRegister());
1071 HInstruction* right = instruction->InputAt(1);
1072 bool can_use_imm = false;
1073 if (right->IsConstant()) {
1074 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1075 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1076 can_use_imm = IsUint<16>(imm);
1077 } else if (instruction->IsAdd()) {
1078 can_use_imm = IsInt<16>(imm);
1079 } else {
1080 DCHECK(instruction->IsSub());
1081 can_use_imm = IsInt<16>(-imm);
1082 }
1083 }
1084 if (can_use_imm)
1085 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1086 else
1087 locations->SetInAt(1, Location::RequiresRegister());
1088 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1089 }
1090 break;
1091
1092 case Primitive::kPrimFloat:
1093 case Primitive::kPrimDouble:
1094 locations->SetInAt(0, Location::RequiresFpuRegister());
1095 locations->SetInAt(1, Location::RequiresFpuRegister());
1096 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1097 break;
1098
1099 default:
1100 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1101 }
1102}
1103
1104void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1105 Primitive::Type type = instruction->GetType();
1106 LocationSummary* locations = instruction->GetLocations();
1107
1108 switch (type) {
1109 case Primitive::kPrimInt:
1110 case Primitive::kPrimLong: {
1111 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1112 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1113 Location rhs_location = locations->InAt(1);
1114
1115 GpuRegister rhs_reg = ZERO;
1116 int64_t rhs_imm = 0;
1117 bool use_imm = rhs_location.IsConstant();
1118 if (use_imm) {
1119 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1120 } else {
1121 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1122 }
1123
1124 if (instruction->IsAnd()) {
1125 if (use_imm)
1126 __ Andi(dst, lhs, rhs_imm);
1127 else
1128 __ And(dst, lhs, rhs_reg);
1129 } else if (instruction->IsOr()) {
1130 if (use_imm)
1131 __ Ori(dst, lhs, rhs_imm);
1132 else
1133 __ Or(dst, lhs, rhs_reg);
1134 } else if (instruction->IsXor()) {
1135 if (use_imm)
1136 __ Xori(dst, lhs, rhs_imm);
1137 else
1138 __ Xor(dst, lhs, rhs_reg);
1139 } else if (instruction->IsAdd()) {
1140 if (type == Primitive::kPrimInt) {
1141 if (use_imm)
1142 __ Addiu(dst, lhs, rhs_imm);
1143 else
1144 __ Addu(dst, lhs, rhs_reg);
1145 } else {
1146 if (use_imm)
1147 __ Daddiu(dst, lhs, rhs_imm);
1148 else
1149 __ Daddu(dst, lhs, rhs_reg);
1150 }
1151 } else {
1152 DCHECK(instruction->IsSub());
1153 if (type == Primitive::kPrimInt) {
1154 if (use_imm)
1155 __ Addiu(dst, lhs, -rhs_imm);
1156 else
1157 __ Subu(dst, lhs, rhs_reg);
1158 } else {
1159 if (use_imm)
1160 __ Daddiu(dst, lhs, -rhs_imm);
1161 else
1162 __ Dsubu(dst, lhs, rhs_reg);
1163 }
1164 }
1165 break;
1166 }
1167 case Primitive::kPrimFloat:
1168 case Primitive::kPrimDouble: {
1169 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1170 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1171 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1172 if (instruction->IsAdd()) {
1173 if (type == Primitive::kPrimFloat)
1174 __ AddS(dst, lhs, rhs);
1175 else
1176 __ AddD(dst, lhs, rhs);
1177 } else if (instruction->IsSub()) {
1178 if (type == Primitive::kPrimFloat)
1179 __ SubS(dst, lhs, rhs);
1180 else
1181 __ SubD(dst, lhs, rhs);
1182 } else {
1183 LOG(FATAL) << "Unexpected floating-point binary operation";
1184 }
1185 break;
1186 }
1187 default:
1188 LOG(FATAL) << "Unexpected binary operation type " << type;
1189 }
1190}
1191
1192void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
1193 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1194
1195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1196 Primitive::Type type = instr->GetResultType();
1197 switch (type) {
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimLong: {
1200 locations->SetInAt(0, Location::RequiresRegister());
1201 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1202 locations->SetOut(Location::RequiresRegister());
1203 break;
1204 }
1205 default:
1206 LOG(FATAL) << "Unexpected shift type " << type;
1207 }
1208}
1209
1210void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
1211 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1212 LocationSummary* locations = instr->GetLocations();
1213 Primitive::Type type = instr->GetType();
1214
1215 switch (type) {
1216 case Primitive::kPrimInt:
1217 case Primitive::kPrimLong: {
1218 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1219 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1220 Location rhs_location = locations->InAt(1);
1221
1222 GpuRegister rhs_reg = ZERO;
1223 int64_t rhs_imm = 0;
1224 bool use_imm = rhs_location.IsConstant();
1225 if (use_imm) {
1226 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1227 } else {
1228 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1229 }
1230
1231 if (use_imm) {
1232 uint32_t shift_value = (type == Primitive::kPrimInt)
1233 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1234 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1235
1236 if (type == Primitive::kPrimInt) {
1237 if (instr->IsShl()) {
1238 __ Sll(dst, lhs, shift_value);
1239 } else if (instr->IsShr()) {
1240 __ Sra(dst, lhs, shift_value);
1241 } else {
1242 __ Srl(dst, lhs, shift_value);
1243 }
1244 } else {
1245 if (shift_value < 32) {
1246 if (instr->IsShl()) {
1247 __ Dsll(dst, lhs, shift_value);
1248 } else if (instr->IsShr()) {
1249 __ Dsra(dst, lhs, shift_value);
1250 } else {
1251 __ Dsrl(dst, lhs, shift_value);
1252 }
1253 } else {
1254 shift_value -= 32;
1255 if (instr->IsShl()) {
1256 __ Dsll32(dst, lhs, shift_value);
1257 } else if (instr->IsShr()) {
1258 __ Dsra32(dst, lhs, shift_value);
1259 } else {
1260 __ Dsrl32(dst, lhs, shift_value);
1261 }
1262 }
1263 }
1264 } else {
1265 if (type == Primitive::kPrimInt) {
1266 if (instr->IsShl()) {
1267 __ Sllv(dst, lhs, rhs_reg);
1268 } else if (instr->IsShr()) {
1269 __ Srav(dst, lhs, rhs_reg);
1270 } else {
1271 __ Srlv(dst, lhs, rhs_reg);
1272 }
1273 } else {
1274 if (instr->IsShl()) {
1275 __ Dsllv(dst, lhs, rhs_reg);
1276 } else if (instr->IsShr()) {
1277 __ Dsrav(dst, lhs, rhs_reg);
1278 } else {
1279 __ Dsrlv(dst, lhs, rhs_reg);
1280 }
1281 }
1282 }
1283 break;
1284 }
1285 default:
1286 LOG(FATAL) << "Unexpected shift operation type " << type;
1287 }
1288}
1289
1290void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1291 HandleBinaryOp(instruction);
1292}
1293
1294void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1295 HandleBinaryOp(instruction);
1296}
1297
1298void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1299 HandleBinaryOp(instruction);
1300}
1301
1302void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1303 HandleBinaryOp(instruction);
1304}
1305
1306void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1307 LocationSummary* locations =
1308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1309 locations->SetInAt(0, Location::RequiresRegister());
1310 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1311 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1312 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1313 } else {
1314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1315 }
1316}
1317
1318void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1319 LocationSummary* locations = instruction->GetLocations();
1320 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1321 Location index = locations->InAt(1);
1322 Primitive::Type type = instruction->GetType();
1323
1324 switch (type) {
1325 case Primitive::kPrimBoolean: {
1326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1327 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1328 if (index.IsConstant()) {
1329 size_t offset =
1330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1331 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1332 } else {
1333 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1334 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1335 }
1336 break;
1337 }
1338
1339 case Primitive::kPrimByte: {
1340 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1341 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1342 if (index.IsConstant()) {
1343 size_t offset =
1344 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1345 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1346 } else {
1347 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1348 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1349 }
1350 break;
1351 }
1352
1353 case Primitive::kPrimShort: {
1354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1355 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1356 if (index.IsConstant()) {
1357 size_t offset =
1358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1359 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1360 } else {
1361 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1362 __ Daddu(TMP, obj, TMP);
1363 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1364 }
1365 break;
1366 }
1367
1368 case Primitive::kPrimChar: {
1369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1370 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1371 if (index.IsConstant()) {
1372 size_t offset =
1373 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1374 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1375 } else {
1376 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1377 __ Daddu(TMP, obj, TMP);
1378 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1379 }
1380 break;
1381 }
1382
1383 case Primitive::kPrimInt:
1384 case Primitive::kPrimNot: {
1385 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1386 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1387 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1388 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1389 if (index.IsConstant()) {
1390 size_t offset =
1391 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1392 __ LoadFromOffset(load_type, out, obj, offset);
1393 } else {
1394 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1395 __ Daddu(TMP, obj, TMP);
1396 __ LoadFromOffset(load_type, out, TMP, data_offset);
1397 }
1398 break;
1399 }
1400
1401 case Primitive::kPrimLong: {
1402 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1403 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1404 if (index.IsConstant()) {
1405 size_t offset =
1406 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1407 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1408 } else {
1409 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1410 __ Daddu(TMP, obj, TMP);
1411 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1412 }
1413 break;
1414 }
1415
1416 case Primitive::kPrimFloat: {
1417 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1418 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1419 if (index.IsConstant()) {
1420 size_t offset =
1421 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1422 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1423 } else {
1424 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1425 __ Daddu(TMP, obj, TMP);
1426 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1427 }
1428 break;
1429 }
1430
1431 case Primitive::kPrimDouble: {
1432 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1433 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1434 if (index.IsConstant()) {
1435 size_t offset =
1436 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1437 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1438 } else {
1439 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1440 __ Daddu(TMP, obj, TMP);
1441 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1442 }
1443 break;
1444 }
1445
1446 case Primitive::kPrimVoid:
1447 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1448 UNREACHABLE();
1449 }
1450 codegen_->MaybeRecordImplicitNullCheck(instruction);
1451}
1452
1453void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1454 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1455 locations->SetInAt(0, Location::RequiresRegister());
1456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1457}
1458
1459void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1460 LocationSummary* locations = instruction->GetLocations();
1461 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1462 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1463 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1464 __ LoadFromOffset(kLoadWord, out, obj, offset);
1465 codegen_->MaybeRecordImplicitNullCheck(instruction);
1466}
1467
1468void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
David Brazdilbb3d5052015-09-21 18:39:16 +01001469 bool needs_runtime_call = instruction->NeedsTypeCheck();
Alexey Frunze4dda3372015-06-01 18:31:49 -07001470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1471 instruction,
David Brazdilbb3d5052015-09-21 18:39:16 +01001472 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1473 if (needs_runtime_call) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001474 InvokeRuntimeCallingConvention calling_convention;
1475 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1476 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1477 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1478 } else {
1479 locations->SetInAt(0, Location::RequiresRegister());
1480 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1481 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1482 locations->SetInAt(2, Location::RequiresFpuRegister());
1483 } else {
1484 locations->SetInAt(2, Location::RequiresRegister());
1485 }
1486 }
1487}
1488
1489void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1490 LocationSummary* locations = instruction->GetLocations();
1491 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1492 Location index = locations->InAt(1);
1493 Primitive::Type value_type = instruction->GetComponentType();
1494 bool needs_runtime_call = locations->WillCall();
1495 bool needs_write_barrier =
1496 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1497
1498 switch (value_type) {
1499 case Primitive::kPrimBoolean:
1500 case Primitive::kPrimByte: {
1501 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1502 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1503 if (index.IsConstant()) {
1504 size_t offset =
1505 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1506 __ StoreToOffset(kStoreByte, value, obj, offset);
1507 } else {
1508 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1509 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1510 }
1511 break;
1512 }
1513
1514 case Primitive::kPrimShort:
1515 case Primitive::kPrimChar: {
1516 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1517 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1518 if (index.IsConstant()) {
1519 size_t offset =
1520 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1521 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1522 } else {
1523 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1524 __ Daddu(TMP, obj, TMP);
1525 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1526 }
1527 break;
1528 }
1529
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimNot: {
1532 if (!needs_runtime_call) {
1533 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1534 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1535 if (index.IsConstant()) {
1536 size_t offset =
1537 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1538 __ StoreToOffset(kStoreWord, value, obj, offset);
1539 } else {
1540 DCHECK(index.IsRegister()) << index;
1541 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1542 __ Daddu(TMP, obj, TMP);
1543 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1544 }
1545 codegen_->MaybeRecordImplicitNullCheck(instruction);
1546 if (needs_write_barrier) {
1547 DCHECK_EQ(value_type, Primitive::kPrimNot);
1548 codegen_->MarkGCCard(obj, value);
1549 }
1550 } else {
1551 DCHECK_EQ(value_type, Primitive::kPrimNot);
1552 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1553 instruction,
1554 instruction->GetDexPc(),
1555 nullptr);
1556 }
1557 break;
1558 }
1559
1560 case Primitive::kPrimLong: {
1561 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1562 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1563 if (index.IsConstant()) {
1564 size_t offset =
1565 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1566 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1567 } else {
1568 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1569 __ Daddu(TMP, obj, TMP);
1570 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1571 }
1572 break;
1573 }
1574
1575 case Primitive::kPrimFloat: {
1576 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1577 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1578 DCHECK(locations->InAt(2).IsFpuRegister());
1579 if (index.IsConstant()) {
1580 size_t offset =
1581 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1582 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1583 } else {
1584 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1585 __ Daddu(TMP, obj, TMP);
1586 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1587 }
1588 break;
1589 }
1590
1591 case Primitive::kPrimDouble: {
1592 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1593 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1594 DCHECK(locations->InAt(2).IsFpuRegister());
1595 if (index.IsConstant()) {
1596 size_t offset =
1597 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1598 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1599 } else {
1600 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1601 __ Daddu(TMP, obj, TMP);
1602 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1603 }
1604 break;
1605 }
1606
1607 case Primitive::kPrimVoid:
1608 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1609 UNREACHABLE();
1610 }
1611
1612 // Ints and objects are handled in the switch.
1613 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1614 codegen_->MaybeRecordImplicitNullCheck(instruction);
1615 }
1616}
1617
1618void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001619 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1620 ? LocationSummary::kCallOnSlowPath
1621 : LocationSummary::kNoCall;
1622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001623 locations->SetInAt(0, Location::RequiresRegister());
1624 locations->SetInAt(1, Location::RequiresRegister());
1625 if (instruction->HasUses()) {
1626 locations->SetOut(Location::SameAsFirstInput());
1627 }
1628}
1629
1630void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1631 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001632 BoundsCheckSlowPathMIPS64* slow_path =
1633 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001634 codegen_->AddSlowPath(slow_path);
1635
1636 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1637 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1638
1639 // length is limited by the maximum positive signed 32-bit integer.
1640 // Unsigned comparison of length and index checks for index < 0
1641 // and for length <= index simultaneously.
1642 // Mips R6 requires lhs != rhs for compact branches.
1643 if (index == length) {
1644 __ B(slow_path->GetEntryLabel());
1645 } else {
1646 __ Bgeuc(index, length, slow_path->GetEntryLabel());
1647 }
1648}
1649
1650void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1652 instruction,
1653 LocationSummary::kCallOnSlowPath);
1654 locations->SetInAt(0, Location::RequiresRegister());
1655 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001656 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001657 locations->AddTemp(Location::RequiresRegister());
1658}
1659
1660void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1661 LocationSummary* locations = instruction->GetLocations();
1662 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1663 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1664 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1665
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001666 SlowPathCodeMIPS64* slow_path =
1667 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001668 codegen_->AddSlowPath(slow_path);
1669
1670 // TODO: avoid this check if we know obj is not null.
1671 __ Beqzc(obj, slow_path->GetExitLabel());
1672 // Compare the class of `obj` with `cls`.
1673 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1674 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1675 __ Bind(slow_path->GetExitLabel());
1676}
1677
1678void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1679 LocationSummary* locations =
1680 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1681 locations->SetInAt(0, Location::RequiresRegister());
1682 if (check->HasUses()) {
1683 locations->SetOut(Location::SameAsFirstInput());
1684 }
1685}
1686
1687void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1688 // We assume the class is not null.
1689 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1690 check->GetLoadClass(),
1691 check,
1692 check->GetDexPc(),
1693 true);
1694 codegen_->AddSlowPath(slow_path);
1695 GenerateClassInitializationCheck(slow_path,
1696 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1697}
1698
1699void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1700 Primitive::Type in_type = compare->InputAt(0)->GetType();
1701
1702 LocationSummary::CallKind call_kind = Primitive::IsFloatingPointType(in_type)
1703 ? LocationSummary::kCall
1704 : LocationSummary::kNoCall;
1705
1706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare, call_kind);
1707
1708 switch (in_type) {
1709 case Primitive::kPrimLong:
1710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetInAt(1, Location::RequiresRegister());
1712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1713 break;
1714
1715 case Primitive::kPrimFloat:
1716 case Primitive::kPrimDouble: {
1717 InvokeRuntimeCallingConvention calling_convention;
1718 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1719 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1720 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1721 break;
1722 }
1723
1724 default:
1725 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1726 }
1727}
1728
1729void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1730 LocationSummary* locations = instruction->GetLocations();
1731 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1732
1733 // 0 if: left == right
1734 // 1 if: left > right
1735 // -1 if: left < right
1736 switch (in_type) {
1737 case Primitive::kPrimLong: {
1738 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1739 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1740 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
1741 // TODO: more efficient (direct) comparison with a constant
1742 __ Slt(TMP, lhs, rhs);
1743 __ Slt(dst, rhs, lhs);
1744 __ Subu(dst, dst, TMP);
1745 break;
1746 }
1747
1748 case Primitive::kPrimFloat:
1749 case Primitive::kPrimDouble: {
1750 int32_t entry_point_offset;
1751 if (in_type == Primitive::kPrimFloat) {
1752 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgFloat)
1753 : QUICK_ENTRY_POINT(pCmplFloat);
1754 } else {
1755 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgDouble)
1756 : QUICK_ENTRY_POINT(pCmplDouble);
1757 }
1758 codegen_->InvokeRuntime(entry_point_offset, instruction, instruction->GetDexPc(), nullptr);
1759 break;
1760 }
1761
1762 default:
1763 LOG(FATAL) << "Unimplemented compare type " << in_type;
1764 }
1765}
1766
1767void LocationsBuilderMIPS64::VisitCondition(HCondition* instruction) {
1768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1769 locations->SetInAt(0, Location::RequiresRegister());
1770 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1771 if (instruction->NeedsMaterialization()) {
1772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1773 }
1774}
1775
1776void InstructionCodeGeneratorMIPS64::VisitCondition(HCondition* instruction) {
1777 if (!instruction->NeedsMaterialization()) {
1778 return;
1779 }
1780
Aart Bike9f37602015-10-09 11:15:55 -07001781 // TODO: generalize to long
1782 DCHECK_NE(instruction->InputAt(0)->GetType(), Primitive::kPrimLong);
1783
Alexey Frunze4dda3372015-06-01 18:31:49 -07001784 LocationSummary* locations = instruction->GetLocations();
1785
1786 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1787 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1788 Location rhs_location = locations->InAt(1);
1789
1790 GpuRegister rhs_reg = ZERO;
1791 int64_t rhs_imm = 0;
1792 bool use_imm = rhs_location.IsConstant();
1793 if (use_imm) {
1794 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1795 } else {
1796 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1797 }
1798
1799 IfCondition if_cond = instruction->GetCondition();
1800
1801 switch (if_cond) {
1802 case kCondEQ:
1803 case kCondNE:
1804 if (use_imm && IsUint<16>(rhs_imm)) {
1805 __ Xori(dst, lhs, rhs_imm);
1806 } else {
1807 if (use_imm) {
1808 rhs_reg = TMP;
1809 __ LoadConst32(rhs_reg, rhs_imm);
1810 }
1811 __ Xor(dst, lhs, rhs_reg);
1812 }
1813 if (if_cond == kCondEQ) {
1814 __ Sltiu(dst, dst, 1);
1815 } else {
1816 __ Sltu(dst, ZERO, dst);
1817 }
1818 break;
1819
1820 case kCondLT:
1821 case kCondGE:
1822 if (use_imm && IsInt<16>(rhs_imm)) {
1823 __ Slti(dst, lhs, rhs_imm);
1824 } else {
1825 if (use_imm) {
1826 rhs_reg = TMP;
1827 __ LoadConst32(rhs_reg, rhs_imm);
1828 }
1829 __ Slt(dst, lhs, rhs_reg);
1830 }
1831 if (if_cond == kCondGE) {
1832 // Simulate lhs >= rhs via !(lhs < rhs) since there's
1833 // only the slt instruction but no sge.
1834 __ Xori(dst, dst, 1);
1835 }
1836 break;
1837
1838 case kCondLE:
1839 case kCondGT:
1840 if (use_imm && IsInt<16>(rhs_imm + 1)) {
1841 // Simulate lhs <= rhs via lhs < rhs + 1.
1842 __ Slti(dst, lhs, rhs_imm + 1);
1843 if (if_cond == kCondGT) {
1844 // Simulate lhs > rhs via !(lhs <= rhs) since there's
1845 // only the slti instruction but no sgti.
1846 __ Xori(dst, dst, 1);
1847 }
1848 } else {
1849 if (use_imm) {
1850 rhs_reg = TMP;
1851 __ LoadConst32(rhs_reg, rhs_imm);
1852 }
1853 __ Slt(dst, rhs_reg, lhs);
1854 if (if_cond == kCondLE) {
1855 // Simulate lhs <= rhs via !(rhs < lhs) since there's
1856 // only the slt instruction but no sle.
1857 __ Xori(dst, dst, 1);
1858 }
1859 }
1860 break;
Aart Bike9f37602015-10-09 11:15:55 -07001861
1862 case kCondB:
1863 case kCondAE:
1864 if (use_imm && 0 <= rhs_imm && rhs_imm <= 0x7fff) {
1865 __ Sltiu(dst, lhs, rhs_imm);
1866 } else {
1867 if (use_imm) {
1868 rhs_reg = TMP;
1869 __ LoadConst32(rhs_reg, rhs_imm);
1870 }
1871 __ Sltu(dst, lhs, rhs_reg);
1872 }
1873 if (if_cond == kCondAE) {
1874 // Simulate lhs >= rhs via !(lhs < rhs) since there's
1875 // only the sltu instruction but no sgeu.
1876 __ Xori(dst, dst, 1);
1877 }
1878 break;
1879
1880 case kCondBE:
1881 case kCondA:
1882 if (use_imm && 0 <= rhs_imm && rhs_imm <= 0x7ffe) {
1883 // Simulate lhs <= rhs via lhs < rhs + 1.
1884 __ Sltiu(dst, lhs, rhs_imm + 1);
1885 if (if_cond == kCondA) {
1886 // Simulate lhs > rhs via !(lhs <= rhs) since there's
1887 // only the sltiu instruction but no sgtiu.
1888 __ Xori(dst, dst, 1);
1889 }
1890 } else {
1891 if (use_imm) {
1892 rhs_reg = TMP;
1893 __ LoadConst32(rhs_reg, rhs_imm);
1894 }
1895 __ Sltu(dst, rhs_reg, lhs);
1896 if (if_cond == kCondBE) {
1897 // Simulate lhs <= rhs via !(rhs < lhs) since there's
1898 // only the sltu instruction but no sleu.
1899 __ Xori(dst, dst, 1);
1900 }
1901 }
1902 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001903 }
1904}
1905
1906void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
1907 LocationSummary* locations =
1908 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1909 switch (div->GetResultType()) {
1910 case Primitive::kPrimInt:
1911 case Primitive::kPrimLong:
1912 locations->SetInAt(0, Location::RequiresRegister());
1913 locations->SetInAt(1, Location::RequiresRegister());
1914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1915 break;
1916
1917 case Primitive::kPrimFloat:
1918 case Primitive::kPrimDouble:
1919 locations->SetInAt(0, Location::RequiresFpuRegister());
1920 locations->SetInAt(1, Location::RequiresFpuRegister());
1921 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1922 break;
1923
1924 default:
1925 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1926 }
1927}
1928
1929void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
1930 Primitive::Type type = instruction->GetType();
1931 LocationSummary* locations = instruction->GetLocations();
1932
1933 switch (type) {
1934 case Primitive::kPrimInt:
1935 case Primitive::kPrimLong: {
1936 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1937 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1938 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
1939 if (type == Primitive::kPrimInt)
1940 __ DivR6(dst, lhs, rhs);
1941 else
1942 __ Ddiv(dst, lhs, rhs);
1943 break;
1944 }
1945 case Primitive::kPrimFloat:
1946 case Primitive::kPrimDouble: {
1947 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1948 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1949 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1950 if (type == Primitive::kPrimFloat)
1951 __ DivS(dst, lhs, rhs);
1952 else
1953 __ DivD(dst, lhs, rhs);
1954 break;
1955 }
1956 default:
1957 LOG(FATAL) << "Unexpected div type " << type;
1958 }
1959}
1960
1961void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001962 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1963 ? LocationSummary::kCallOnSlowPath
1964 : LocationSummary::kNoCall;
1965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001966 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1967 if (instruction->HasUses()) {
1968 locations->SetOut(Location::SameAsFirstInput());
1969 }
1970}
1971
1972void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1973 SlowPathCodeMIPS64* slow_path =
1974 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
1975 codegen_->AddSlowPath(slow_path);
1976 Location value = instruction->GetLocations()->InAt(0);
1977
1978 Primitive::Type type = instruction->GetType();
1979
Serguei Katkov8c0676c2015-08-03 13:55:33 +06001980 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001981 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06001982 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001983 }
1984
1985 if (value.IsConstant()) {
1986 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
1987 if (divisor == 0) {
1988 __ B(slow_path->GetEntryLabel());
1989 } else {
1990 // A division by a non-null constant is valid. We don't need to perform
1991 // any check, so simply fall through.
1992 }
1993 } else {
1994 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
1995 }
1996}
1997
1998void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
1999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2001 locations->SetOut(Location::ConstantLocation(constant));
2002}
2003
2004void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2005 // Will be generated at use site.
2006}
2007
2008void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
2009 exit->SetLocations(nullptr);
2010}
2011
2012void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2013}
2014
2015void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
2016 LocationSummary* locations =
2017 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2018 locations->SetOut(Location::ConstantLocation(constant));
2019}
2020
2021void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2022 // Will be generated at use site.
2023}
2024
David Brazdilfc6a86a2015-06-26 10:33:45 +00002025void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002026 DCHECK(!successor->IsExitBlock());
2027 HBasicBlock* block = got->GetBlock();
2028 HInstruction* previous = got->GetPrevious();
2029 HLoopInformation* info = block->GetLoopInformation();
2030
2031 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2032 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2033 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2034 return;
2035 }
2036 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2037 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2038 }
2039 if (!codegen_->GoesToNextBlock(block, successor)) {
2040 __ B(codegen_->GetLabelOf(successor));
2041 }
2042}
2043
David Brazdilfc6a86a2015-06-26 10:33:45 +00002044void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2045 got->SetLocations(nullptr);
2046}
2047
2048void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2049 HandleGoto(got, got->GetSuccessor());
2050}
2051
2052void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2053 try_boundary->SetLocations(nullptr);
2054}
2055
2056void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2057 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2058 if (!successor->IsExitBlock()) {
2059 HandleGoto(try_boundary, successor);
2060 }
2061}
2062
Alexey Frunze4dda3372015-06-01 18:31:49 -07002063void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
2064 Label* true_target,
2065 Label* false_target,
2066 Label* always_true_target) {
2067 HInstruction* cond = instruction->InputAt(0);
2068 HCondition* condition = cond->AsCondition();
2069
2070 if (cond->IsIntConstant()) {
2071 int32_t cond_value = cond->AsIntConstant()->GetValue();
2072 if (cond_value == 1) {
2073 if (always_true_target != nullptr) {
2074 __ B(always_true_target);
2075 }
2076 return;
2077 } else {
2078 DCHECK_EQ(cond_value, 0);
2079 }
2080 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
2081 // The condition instruction has been materialized, compare the output to 0.
2082 Location cond_val = instruction->GetLocations()->InAt(0);
2083 DCHECK(cond_val.IsRegister());
2084 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2085 } else {
2086 // The condition instruction has not been materialized, use its inputs as
2087 // the comparison and its condition as the branch condition.
2088 GpuRegister lhs = condition->GetLocations()->InAt(0).AsRegister<GpuRegister>();
2089 Location rhs_location = condition->GetLocations()->InAt(1);
2090 GpuRegister rhs_reg = ZERO;
2091 int32_t rhs_imm = 0;
2092 bool use_imm = rhs_location.IsConstant();
2093 if (use_imm) {
2094 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2095 } else {
2096 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2097 }
2098
2099 IfCondition if_cond = condition->GetCondition();
2100 if (use_imm && rhs_imm == 0) {
2101 switch (if_cond) {
2102 case kCondEQ:
2103 __ Beqzc(lhs, true_target);
2104 break;
2105 case kCondNE:
2106 __ Bnezc(lhs, true_target);
2107 break;
2108 case kCondLT:
2109 __ Bltzc(lhs, true_target);
2110 break;
2111 case kCondGE:
2112 __ Bgezc(lhs, true_target);
2113 break;
2114 case kCondLE:
2115 __ Blezc(lhs, true_target);
2116 break;
2117 case kCondGT:
2118 __ Bgtzc(lhs, true_target);
2119 break;
Aart Bike9f37602015-10-09 11:15:55 -07002120 case kCondB:
2121 break; // always false
2122 case kCondBE:
2123 __ Beqzc(lhs, true_target); // <= 0 if zero
2124 break;
2125 case kCondA:
2126 __ Bnezc(lhs, true_target); // > 0 if non-zero
2127 break;
2128 case kCondAE:
2129 __ B(true_target); // always true
2130 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002131 }
2132 } else {
2133 if (use_imm) {
2134 rhs_reg = TMP;
2135 __ LoadConst32(rhs_reg, rhs_imm);
2136 }
2137 // It looks like we can get here with lhs == rhs. Should that be possible at all?
2138 // Mips R6 requires lhs != rhs for compact branches.
2139 if (lhs == rhs_reg) {
2140 DCHECK(!use_imm);
2141 switch (if_cond) {
2142 case kCondEQ:
2143 case kCondGE:
2144 case kCondLE:
Aart Bike9f37602015-10-09 11:15:55 -07002145 case kCondBE:
2146 case kCondAE:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002147 // if lhs == rhs for a positive condition, then it is a branch
2148 __ B(true_target);
2149 break;
2150 case kCondNE:
2151 case kCondLT:
2152 case kCondGT:
Aart Bike9f37602015-10-09 11:15:55 -07002153 case kCondB:
2154 case kCondA:
Alexey Frunze4dda3372015-06-01 18:31:49 -07002155 // if lhs == rhs for a negative condition, then it is a NOP
2156 break;
2157 }
2158 } else {
2159 switch (if_cond) {
2160 case kCondEQ:
2161 __ Beqc(lhs, rhs_reg, true_target);
2162 break;
2163 case kCondNE:
2164 __ Bnec(lhs, rhs_reg, true_target);
2165 break;
2166 case kCondLT:
2167 __ Bltc(lhs, rhs_reg, true_target);
2168 break;
2169 case kCondGE:
2170 __ Bgec(lhs, rhs_reg, true_target);
2171 break;
2172 case kCondLE:
2173 __ Bgec(rhs_reg, lhs, true_target);
2174 break;
2175 case kCondGT:
2176 __ Bltc(rhs_reg, lhs, true_target);
2177 break;
Aart Bike9f37602015-10-09 11:15:55 -07002178 case kCondB:
2179 __ Bltuc(lhs, rhs_reg, true_target);
2180 break;
2181 case kCondAE:
2182 __ Bgeuc(lhs, rhs_reg, true_target);
2183 break;
2184 case kCondBE:
2185 __ Bgeuc(rhs_reg, lhs, true_target);
2186 break;
2187 case kCondA:
2188 __ Bltuc(rhs_reg, lhs, true_target);
2189 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002190 }
2191 }
2192 }
2193 }
2194 if (false_target != nullptr) {
2195 __ B(false_target);
2196 }
2197}
2198
2199void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2201 HInstruction* cond = if_instr->InputAt(0);
2202 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2203 locations->SetInAt(0, Location::RequiresRegister());
2204 }
2205}
2206
2207void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
2208 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2209 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2210 Label* always_true_target = true_target;
2211 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2212 if_instr->IfTrueSuccessor())) {
2213 always_true_target = nullptr;
2214 }
2215 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2216 if_instr->IfFalseSuccessor())) {
2217 false_target = nullptr;
2218 }
2219 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2220}
2221
2222void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2223 LocationSummary* locations = new (GetGraph()->GetArena())
2224 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2225 HInstruction* cond = deoptimize->InputAt(0);
2226 DCHECK(cond->IsCondition());
2227 if (cond->AsCondition()->NeedsMaterialization()) {
2228 locations->SetInAt(0, Location::RequiresRegister());
2229 }
2230}
2231
2232void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2233 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
2234 DeoptimizationSlowPathMIPS64(deoptimize);
2235 codegen_->AddSlowPath(slow_path);
2236 Label* slow_path_entry = slow_path->GetEntryLabel();
2237 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2238}
2239
2240void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2241 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2242 LocationSummary* locations =
2243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2244 locations->SetInAt(0, Location::RequiresRegister());
2245 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2246 locations->SetOut(Location::RequiresFpuRegister());
2247 } else {
2248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2249 }
2250}
2251
2252void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2253 const FieldInfo& field_info) {
2254 Primitive::Type type = field_info.GetFieldType();
2255 LocationSummary* locations = instruction->GetLocations();
2256 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2257 LoadOperandType load_type = kLoadUnsignedByte;
2258 switch (type) {
2259 case Primitive::kPrimBoolean:
2260 load_type = kLoadUnsignedByte;
2261 break;
2262 case Primitive::kPrimByte:
2263 load_type = kLoadSignedByte;
2264 break;
2265 case Primitive::kPrimShort:
2266 load_type = kLoadSignedHalfword;
2267 break;
2268 case Primitive::kPrimChar:
2269 load_type = kLoadUnsignedHalfword;
2270 break;
2271 case Primitive::kPrimInt:
2272 case Primitive::kPrimFloat:
2273 load_type = kLoadWord;
2274 break;
2275 case Primitive::kPrimLong:
2276 case Primitive::kPrimDouble:
2277 load_type = kLoadDoubleword;
2278 break;
2279 case Primitive::kPrimNot:
2280 load_type = kLoadUnsignedWord;
2281 break;
2282 case Primitive::kPrimVoid:
2283 LOG(FATAL) << "Unreachable type " << type;
2284 UNREACHABLE();
2285 }
2286 if (!Primitive::IsFloatingPointType(type)) {
2287 DCHECK(locations->Out().IsRegister());
2288 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2289 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2290 } else {
2291 DCHECK(locations->Out().IsFpuRegister());
2292 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2293 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2294 }
2295
2296 codegen_->MaybeRecordImplicitNullCheck(instruction);
2297 // TODO: memory barrier?
2298}
2299
2300void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2301 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2302 LocationSummary* locations =
2303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2304 locations->SetInAt(0, Location::RequiresRegister());
2305 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2306 locations->SetInAt(1, Location::RequiresFpuRegister());
2307 } else {
2308 locations->SetInAt(1, Location::RequiresRegister());
2309 }
2310}
2311
2312void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
2313 const FieldInfo& field_info) {
2314 Primitive::Type type = field_info.GetFieldType();
2315 LocationSummary* locations = instruction->GetLocations();
2316 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2317 StoreOperandType store_type = kStoreByte;
2318 switch (type) {
2319 case Primitive::kPrimBoolean:
2320 case Primitive::kPrimByte:
2321 store_type = kStoreByte;
2322 break;
2323 case Primitive::kPrimShort:
2324 case Primitive::kPrimChar:
2325 store_type = kStoreHalfword;
2326 break;
2327 case Primitive::kPrimInt:
2328 case Primitive::kPrimFloat:
2329 case Primitive::kPrimNot:
2330 store_type = kStoreWord;
2331 break;
2332 case Primitive::kPrimLong:
2333 case Primitive::kPrimDouble:
2334 store_type = kStoreDoubleword;
2335 break;
2336 case Primitive::kPrimVoid:
2337 LOG(FATAL) << "Unreachable type " << type;
2338 UNREACHABLE();
2339 }
2340 if (!Primitive::IsFloatingPointType(type)) {
2341 DCHECK(locations->InAt(1).IsRegister());
2342 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2343 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2344 } else {
2345 DCHECK(locations->InAt(1).IsFpuRegister());
2346 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2347 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2348 }
2349
2350 codegen_->MaybeRecordImplicitNullCheck(instruction);
2351 // TODO: memory barriers?
2352 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2353 DCHECK(locations->InAt(1).IsRegister());
2354 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2355 codegen_->MarkGCCard(obj, src);
2356 }
2357}
2358
2359void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2360 HandleFieldGet(instruction, instruction->GetFieldInfo());
2361}
2362
2363void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2364 HandleFieldGet(instruction, instruction->GetFieldInfo());
2365}
2366
2367void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2368 HandleFieldSet(instruction, instruction->GetFieldInfo());
2369}
2370
2371void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2372 HandleFieldSet(instruction, instruction->GetFieldInfo());
2373}
2374
2375void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2376 LocationSummary::CallKind call_kind =
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002377 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2379 locations->SetInAt(0, Location::RequiresRegister());
2380 locations->SetInAt(1, Location::RequiresRegister());
2381 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002382 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002383 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2384}
2385
2386void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2387 LocationSummary* locations = instruction->GetLocations();
2388 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2389 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2390 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2391
2392 Label done;
2393
2394 // Return 0 if `obj` is null.
2395 // TODO: Avoid this check if we know `obj` is not null.
2396 __ Move(out, ZERO);
2397 __ Beqzc(obj, &done);
2398
2399 // Compare the class of `obj` with `cls`.
2400 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00002401 if (instruction->IsExactCheck()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002402 // Classes must be equal for the instanceof to succeed.
2403 __ Xor(out, out, cls);
2404 __ Sltiu(out, out, 1);
2405 } else {
2406 // If the classes are not equal, we go into a slow path.
2407 DCHECK(locations->OnlyCallsOnSlowPath());
2408 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002409 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002410 codegen_->AddSlowPath(slow_path);
2411 __ Bnec(out, cls, slow_path->GetEntryLabel());
2412 __ LoadConst32(out, 1);
2413 __ Bind(slow_path->GetExitLabel());
2414 }
2415
2416 __ Bind(&done);
2417}
2418
2419void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2420 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2421 locations->SetOut(Location::ConstantLocation(constant));
2422}
2423
2424void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2425 // Will be generated at use site.
2426}
2427
2428void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2430 locations->SetOut(Location::ConstantLocation(constant));
2431}
2432
2433void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2434 // Will be generated at use site.
2435}
2436
Calin Juravle175dc732015-08-25 15:42:32 +01002437void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2438 // The trampoline uses the same calling convention as dex calling conventions,
2439 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2440 // the method_idx.
2441 HandleInvoke(invoke);
2442}
2443
2444void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2445 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2446}
2447
Alexey Frunze4dda3372015-06-01 18:31:49 -07002448void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2449 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2450 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2451}
2452
2453void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2454 HandleInvoke(invoke);
2455 // The register T0 is required to be used for the hidden argument in
2456 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2457 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2458}
2459
2460void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2461 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2462 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2463 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2464 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2465 Location receiver = invoke->GetLocations()->InAt(0);
2466 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2467 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2468
2469 // Set the hidden argument.
2470 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2471 invoke->GetDexMethodIndex());
2472
2473 // temp = object->GetClass();
2474 if (receiver.IsStackSlot()) {
2475 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2476 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2477 } else {
2478 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2479 }
2480 codegen_->MaybeRecordImplicitNullCheck(invoke);
2481 // temp = temp->GetImtEntryAt(method_offset);
2482 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2483 // T9 = temp->GetEntryPoint();
2484 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2485 // T9();
2486 __ Jalr(T9);
2487 DCHECK(!codegen_->IsLeafMethod());
2488 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2489}
2490
2491void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002492 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2493 if (intrinsic.TryDispatch(invoke)) {
2494 return;
2495 }
2496
Alexey Frunze4dda3372015-06-01 18:31:49 -07002497 HandleInvoke(invoke);
2498}
2499
2500void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2501 // When we do not run baseline, explicit clinit checks triggered by static
2502 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2503 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2504
Chris Larsen3039e382015-08-26 07:54:08 -07002505 IntrinsicLocationsBuilderMIPS64 intrinsic(codegen_);
2506 if (intrinsic.TryDispatch(invoke)) {
2507 return;
2508 }
2509
Alexey Frunze4dda3372015-06-01 18:31:49 -07002510 HandleInvoke(invoke);
2511
2512 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2513 // clobbering somewhere else, reduce further register pressure by avoiding
2514 // allocation of a register for the current method pointer like on x86 baseline.
2515 // TODO: remove this once all the issues with register saving/restoring are
2516 // sorted out.
2517 LocationSummary* locations = invoke->GetLocations();
2518 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
2519 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
2520 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
2521 }
2522}
2523
Chris Larsen3039e382015-08-26 07:54:08 -07002524static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS64* codegen) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002525 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen3039e382015-08-26 07:54:08 -07002526 IntrinsicCodeGeneratorMIPS64 intrinsic(codegen);
2527 intrinsic.Dispatch(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002528 return true;
2529 }
2530 return false;
2531}
2532
2533void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
2534 // All registers are assumed to be correctly set up per the calling convention.
2535
Vladimir Marko58155012015-08-19 12:49:41 +00002536 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2537 switch (invoke->GetMethodLoadKind()) {
2538 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2539 // temp = thread->string_init_entrypoint
2540 __ LoadFromOffset(kLoadDoubleword,
2541 temp.AsRegister<GpuRegister>(),
2542 TR,
2543 invoke->GetStringInitOffset());
2544 break;
2545 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2546 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2547 break;
2548 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2549 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
2550 break;
2551 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2552 // TODO: Implement this type. (Needs literal support.) At the moment, the
2553 // CompilerDriver will not direct the backend to use this type for MIPS.
2554 LOG(FATAL) << "Unsupported!";
2555 UNREACHABLE();
2556 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2557 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
2558 FALLTHROUGH_INTENDED;
2559 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2560 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2561 GpuRegister reg = temp.AsRegister<GpuRegister>();
2562 GpuRegister method_reg;
2563 if (current_method.IsRegister()) {
2564 method_reg = current_method.AsRegister<GpuRegister>();
2565 } else {
2566 // TODO: use the appropriate DCHECK() here if possible.
2567 // DCHECK(invoke->GetLocations()->Intrinsified());
2568 DCHECK(!current_method.IsValid());
2569 method_reg = reg;
2570 __ Ld(reg, SP, kCurrentMethodStackOffset);
2571 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002572
Vladimir Marko58155012015-08-19 12:49:41 +00002573 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002574 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00002575 reg,
2576 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01002577 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00002578 // temp = temp[index_in_cache]
2579 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2580 __ LoadFromOffset(kLoadDoubleword,
2581 reg,
2582 reg,
2583 CodeGenerator::GetCachePointerOffset(index_in_cache));
2584 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002585 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002586 }
2587
Vladimir Marko58155012015-08-19 12:49:41 +00002588 switch (invoke->GetCodePtrLocation()) {
2589 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2590 __ Jalr(&frame_entry_label_, T9);
2591 break;
2592 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2593 // LR = invoke->GetDirectCodePtr();
2594 __ LoadConst64(T9, invoke->GetDirectCodePtr());
2595 // LR()
2596 __ Jalr(T9);
2597 break;
2598 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2599 // TODO: Implement kCallPCRelative. For the moment, we fall back to kMethodCode.
2600 FALLTHROUGH_INTENDED;
2601 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2602 // TODO: Implement kDirectCodeFixup. For the moment, we fall back to kMethodCode.
2603 FALLTHROUGH_INTENDED;
2604 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2605 // T9 = callee_method->entry_point_from_quick_compiled_code_;
2606 __ LoadFromOffset(kLoadDoubleword,
2607 T9,
2608 callee_method.AsRegister<GpuRegister>(),
2609 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2610 kMips64WordSize).Int32Value());
2611 // T9()
2612 __ Jalr(T9);
2613 break;
2614 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002615 DCHECK(!IsLeafMethod());
2616}
2617
2618void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2619 // When we do not run baseline, explicit clinit checks triggered by static
2620 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2621 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2622
2623 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2624 return;
2625 }
2626
2627 LocationSummary* locations = invoke->GetLocations();
2628 codegen_->GenerateStaticOrDirectCall(invoke,
2629 locations->HasTemps()
2630 ? locations->GetTemp(0)
2631 : Location::NoLocation());
2632 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2633}
2634
2635void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen3039e382015-08-26 07:54:08 -07002636 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2637 return;
2638 }
2639
Alexey Frunze4dda3372015-06-01 18:31:49 -07002640 LocationSummary* locations = invoke->GetLocations();
2641 Location receiver = locations->InAt(0);
2642 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2643 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2644 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
2645 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2646 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2647
2648 // temp = object->GetClass();
2649 DCHECK(receiver.IsRegister());
2650 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2651 codegen_->MaybeRecordImplicitNullCheck(invoke);
2652 // temp = temp->GetMethodAt(method_offset);
2653 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2654 // T9 = temp->GetEntryPoint();
2655 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2656 // T9();
2657 __ Jalr(T9);
2658 DCHECK(!codegen_->IsLeafMethod());
2659 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2660}
2661
2662void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01002663 InvokeRuntimeCallingConvention calling_convention;
2664 CodeGenerator::CreateLoadClassLocationSummary(
2665 cls,
2666 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
2667 Location::RegisterLocation(A0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002668}
2669
2670void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
2671 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01002672 if (cls->NeedsAccessCheck()) {
2673 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
2674 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
2675 cls,
2676 cls->GetDexPc(),
2677 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01002678 return;
2679 }
2680
2681 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2682 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2683 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002684 DCHECK(!cls->CanCallRuntime());
2685 DCHECK(!cls->MustGenerateClinitCheck());
2686 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2687 ArtMethod::DeclaringClassOffset().Int32Value());
2688 } else {
2689 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002690 __ LoadFromOffset(kLoadDoubleword, out, current_method,
2691 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002693 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002694 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2695 cls,
2696 cls,
2697 cls->GetDexPc(),
2698 cls->MustGenerateClinitCheck());
2699 codegen_->AddSlowPath(slow_path);
2700 __ Beqzc(out, slow_path->GetEntryLabel());
2701 if (cls->MustGenerateClinitCheck()) {
2702 GenerateClassInitializationCheck(slow_path, out);
2703 } else {
2704 __ Bind(slow_path->GetExitLabel());
2705 }
2706 }
2707}
2708
David Brazdilcb1c0552015-08-04 16:22:25 +01002709static int32_t GetExceptionTlsOffset() {
2710 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
2711}
2712
Alexey Frunze4dda3372015-06-01 18:31:49 -07002713void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
2714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2716 locations->SetOut(Location::RequiresRegister());
2717}
2718
2719void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
2720 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01002721 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
2722}
2723
2724void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
2725 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2726}
2727
2728void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2729 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002730}
2731
2732void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
2733 load->SetLocations(nullptr);
2734}
2735
2736void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
2737 // Nothing to do, this is driven by the code generator.
2738}
2739
2740void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
2741 LocationSummary* locations =
2742 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetOut(Location::RequiresRegister());
2745}
2746
2747void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
2748 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
2749 codegen_->AddSlowPath(slow_path);
2750
2751 LocationSummary* locations = load->GetLocations();
2752 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2753 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2754 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2755 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01002756 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002757 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002758 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002759 __ Beqzc(out, slow_path->GetEntryLabel());
2760 __ Bind(slow_path->GetExitLabel());
2761}
2762
2763void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
2764 local->SetLocations(nullptr);
2765}
2766
2767void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
2768 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2769}
2770
2771void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
2772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2773 locations->SetOut(Location::ConstantLocation(constant));
2774}
2775
2776void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2777 // Will be generated at use site.
2778}
2779
2780void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2783 InvokeRuntimeCallingConvention calling_convention;
2784 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2785}
2786
2787void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2788 codegen_->InvokeRuntime(instruction->IsEnter()
2789 ? QUICK_ENTRY_POINT(pLockObject)
2790 : QUICK_ENTRY_POINT(pUnlockObject),
2791 instruction,
2792 instruction->GetDexPc(),
2793 nullptr);
2794 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
2795}
2796
2797void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
2798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2800 switch (mul->GetResultType()) {
2801 case Primitive::kPrimInt:
2802 case Primitive::kPrimLong:
2803 locations->SetInAt(0, Location::RequiresRegister());
2804 locations->SetInAt(1, Location::RequiresRegister());
2805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2806 break;
2807
2808 case Primitive::kPrimFloat:
2809 case Primitive::kPrimDouble:
2810 locations->SetInAt(0, Location::RequiresFpuRegister());
2811 locations->SetInAt(1, Location::RequiresFpuRegister());
2812 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2813 break;
2814
2815 default:
2816 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2817 }
2818}
2819
2820void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
2821 Primitive::Type type = instruction->GetType();
2822 LocationSummary* locations = instruction->GetLocations();
2823
2824 switch (type) {
2825 case Primitive::kPrimInt:
2826 case Primitive::kPrimLong: {
2827 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2828 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2829 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
2830 if (type == Primitive::kPrimInt)
2831 __ MulR6(dst, lhs, rhs);
2832 else
2833 __ Dmul(dst, lhs, rhs);
2834 break;
2835 }
2836 case Primitive::kPrimFloat:
2837 case Primitive::kPrimDouble: {
2838 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2839 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2840 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2841 if (type == Primitive::kPrimFloat)
2842 __ MulS(dst, lhs, rhs);
2843 else
2844 __ MulD(dst, lhs, rhs);
2845 break;
2846 }
2847 default:
2848 LOG(FATAL) << "Unexpected mul type " << type;
2849 }
2850}
2851
2852void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
2853 LocationSummary* locations =
2854 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2855 switch (neg->GetResultType()) {
2856 case Primitive::kPrimInt:
2857 case Primitive::kPrimLong:
2858 locations->SetInAt(0, Location::RequiresRegister());
2859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2860 break;
2861
2862 case Primitive::kPrimFloat:
2863 case Primitive::kPrimDouble:
2864 locations->SetInAt(0, Location::RequiresFpuRegister());
2865 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2866 break;
2867
2868 default:
2869 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2870 }
2871}
2872
2873void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
2874 Primitive::Type type = instruction->GetType();
2875 LocationSummary* locations = instruction->GetLocations();
2876
2877 switch (type) {
2878 case Primitive::kPrimInt:
2879 case Primitive::kPrimLong: {
2880 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2881 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2882 if (type == Primitive::kPrimInt)
2883 __ Subu(dst, ZERO, src);
2884 else
2885 __ Dsubu(dst, ZERO, src);
2886 break;
2887 }
2888 case Primitive::kPrimFloat:
2889 case Primitive::kPrimDouble: {
2890 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2891 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
2892 if (type == Primitive::kPrimFloat)
2893 __ NegS(dst, src);
2894 else
2895 __ NegD(dst, src);
2896 break;
2897 }
2898 default:
2899 LOG(FATAL) << "Unexpected neg type " << type;
2900 }
2901}
2902
2903void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
2904 LocationSummary* locations =
2905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2906 InvokeRuntimeCallingConvention calling_convention;
2907 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2908 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2909 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2910 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2911}
2912
2913void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
2914 LocationSummary* locations = instruction->GetLocations();
2915 // Move an uint16_t value to a register.
2916 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002917 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2918 instruction,
2919 instruction->GetDexPc(),
2920 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002921 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
2922}
2923
2924void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
2925 LocationSummary* locations =
2926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2927 InvokeRuntimeCallingConvention calling_convention;
2928 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2929 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2930 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2931}
2932
2933void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
2934 LocationSummary* locations = instruction->GetLocations();
2935 // Move an uint16_t value to a register.
2936 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002937 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2938 instruction,
2939 instruction->GetDexPc(),
2940 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002941 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
2942}
2943
2944void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
2945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2946 locations->SetInAt(0, Location::RequiresRegister());
2947 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2948}
2949
2950void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
2951 Primitive::Type type = instruction->GetType();
2952 LocationSummary* locations = instruction->GetLocations();
2953
2954 switch (type) {
2955 case Primitive::kPrimInt:
2956 case Primitive::kPrimLong: {
2957 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2958 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2959 __ Nor(dst, src, ZERO);
2960 break;
2961 }
2962
2963 default:
2964 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2965 }
2966}
2967
2968void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
2969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2970 locations->SetInAt(0, Location::RequiresRegister());
2971 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2972}
2973
2974void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
2975 LocationSummary* locations = instruction->GetLocations();
2976 __ Xori(locations->Out().AsRegister<GpuRegister>(),
2977 locations->InAt(0).AsRegister<GpuRegister>(),
2978 1);
2979}
2980
2981void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002982 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2983 ? LocationSummary::kCallOnSlowPath
2984 : LocationSummary::kNoCall;
2985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002986 locations->SetInAt(0, Location::RequiresRegister());
2987 if (instruction->HasUses()) {
2988 locations->SetOut(Location::SameAsFirstInput());
2989 }
2990}
2991
2992void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
2993 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2994 return;
2995 }
2996 Location obj = instruction->GetLocations()->InAt(0);
2997
2998 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
2999 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3000}
3001
3002void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3003 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3004 codegen_->AddSlowPath(slow_path);
3005
3006 Location obj = instruction->GetLocations()->InAt(0);
3007
3008 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3009}
3010
3011void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003012 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003013 GenerateImplicitNullCheck(instruction);
3014 } else {
3015 GenerateExplicitNullCheck(instruction);
3016 }
3017}
3018
3019void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3020 HandleBinaryOp(instruction);
3021}
3022
3023void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3024 HandleBinaryOp(instruction);
3025}
3026
3027void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3028 LOG(FATAL) << "Unreachable";
3029}
3030
3031void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3032 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3033}
3034
3035void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3036 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3037 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3038 if (location.IsStackSlot()) {
3039 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3040 } else if (location.IsDoubleStackSlot()) {
3041 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3042 }
3043 locations->SetOut(location);
3044}
3045
3046void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3047 ATTRIBUTE_UNUSED) {
3048 // Nothing to do, the parameter is already at its location.
3049}
3050
3051void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3052 LocationSummary* locations =
3053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3054 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3055}
3056
3057void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3058 ATTRIBUTE_UNUSED) {
3059 // Nothing to do, the method is already at its location.
3060}
3061
3062void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3064 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3065 locations->SetInAt(i, Location::Any());
3066 }
3067 locations->SetOut(Location::Any());
3068}
3069
3070void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3071 LOG(FATAL) << "Unreachable";
3072}
3073
3074void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3075 Primitive::Type type = rem->GetResultType();
3076 LocationSummary::CallKind call_kind =
3077 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3079
3080 switch (type) {
3081 case Primitive::kPrimInt:
3082 case Primitive::kPrimLong:
3083 locations->SetInAt(0, Location::RequiresRegister());
3084 locations->SetInAt(1, Location::RequiresRegister());
3085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3086 break;
3087
3088 case Primitive::kPrimFloat:
3089 case Primitive::kPrimDouble: {
3090 InvokeRuntimeCallingConvention calling_convention;
3091 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3092 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3093 locations->SetOut(calling_convention.GetReturnLocation(type));
3094 break;
3095 }
3096
3097 default:
3098 LOG(FATAL) << "Unexpected rem type " << type;
3099 }
3100}
3101
3102void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3103 Primitive::Type type = instruction->GetType();
3104 LocationSummary* locations = instruction->GetLocations();
3105
3106 switch (type) {
3107 case Primitive::kPrimInt:
3108 case Primitive::kPrimLong: {
3109 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3110 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3111 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3112 if (type == Primitive::kPrimInt)
3113 __ ModR6(dst, lhs, rhs);
3114 else
3115 __ Dmod(dst, lhs, rhs);
3116 break;
3117 }
3118
3119 case Primitive::kPrimFloat:
3120 case Primitive::kPrimDouble: {
3121 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3122 : QUICK_ENTRY_POINT(pFmod);
3123 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
3124 break;
3125 }
3126 default:
3127 LOG(FATAL) << "Unexpected rem type " << type;
3128 }
3129}
3130
3131void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3132 memory_barrier->SetLocations(nullptr);
3133}
3134
3135void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3136 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3137}
3138
3139void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3141 Primitive::Type return_type = ret->InputAt(0)->GetType();
3142 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3143}
3144
3145void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3146 codegen_->GenerateFrameExit();
3147}
3148
3149void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3150 ret->SetLocations(nullptr);
3151}
3152
3153void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3154 codegen_->GenerateFrameExit();
3155}
3156
3157void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3158 HandleShift(shl);
3159}
3160
3161void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3162 HandleShift(shl);
3163}
3164
3165void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3166 HandleShift(shr);
3167}
3168
3169void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3170 HandleShift(shr);
3171}
3172
3173void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3175 Primitive::Type field_type = store->InputAt(1)->GetType();
3176 switch (field_type) {
3177 case Primitive::kPrimNot:
3178 case Primitive::kPrimBoolean:
3179 case Primitive::kPrimByte:
3180 case Primitive::kPrimChar:
3181 case Primitive::kPrimShort:
3182 case Primitive::kPrimInt:
3183 case Primitive::kPrimFloat:
3184 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3185 break;
3186
3187 case Primitive::kPrimLong:
3188 case Primitive::kPrimDouble:
3189 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3190 break;
3191
3192 default:
3193 LOG(FATAL) << "Unimplemented local type " << field_type;
3194 }
3195}
3196
3197void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3198}
3199
3200void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3201 HandleBinaryOp(instruction);
3202}
3203
3204void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3205 HandleBinaryOp(instruction);
3206}
3207
3208void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3209 HandleFieldGet(instruction, instruction->GetFieldInfo());
3210}
3211
3212void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3213 HandleFieldGet(instruction, instruction->GetFieldInfo());
3214}
3215
3216void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3217 HandleFieldSet(instruction, instruction->GetFieldInfo());
3218}
3219
3220void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3221 HandleFieldSet(instruction, instruction->GetFieldInfo());
3222}
3223
Calin Juravlee460d1d2015-09-29 04:52:17 +01003224void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3225 HUnresolvedInstanceFieldGet* instruction) {
3226 FieldAccessCallingConventionMIPS64 calling_convention;
3227 codegen_->CreateUnresolvedFieldLocationSummary(
3228 instruction, instruction->GetFieldType(), calling_convention);
3229}
3230
3231void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3232 HUnresolvedInstanceFieldGet* instruction) {
3233 FieldAccessCallingConventionMIPS64 calling_convention;
3234 codegen_->GenerateUnresolvedFieldAccess(instruction,
3235 instruction->GetFieldType(),
3236 instruction->GetFieldIndex(),
3237 instruction->GetDexPc(),
3238 calling_convention);
3239}
3240
3241void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3242 HUnresolvedInstanceFieldSet* instruction) {
3243 FieldAccessCallingConventionMIPS64 calling_convention;
3244 codegen_->CreateUnresolvedFieldLocationSummary(
3245 instruction, instruction->GetFieldType(), calling_convention);
3246}
3247
3248void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3249 HUnresolvedInstanceFieldSet* instruction) {
3250 FieldAccessCallingConventionMIPS64 calling_convention;
3251 codegen_->GenerateUnresolvedFieldAccess(instruction,
3252 instruction->GetFieldType(),
3253 instruction->GetFieldIndex(),
3254 instruction->GetDexPc(),
3255 calling_convention);
3256}
3257
3258void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3259 HUnresolvedStaticFieldGet* instruction) {
3260 FieldAccessCallingConventionMIPS64 calling_convention;
3261 codegen_->CreateUnresolvedFieldLocationSummary(
3262 instruction, instruction->GetFieldType(), calling_convention);
3263}
3264
3265void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3266 HUnresolvedStaticFieldGet* instruction) {
3267 FieldAccessCallingConventionMIPS64 calling_convention;
3268 codegen_->GenerateUnresolvedFieldAccess(instruction,
3269 instruction->GetFieldType(),
3270 instruction->GetFieldIndex(),
3271 instruction->GetDexPc(),
3272 calling_convention);
3273}
3274
3275void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3276 HUnresolvedStaticFieldSet* instruction) {
3277 FieldAccessCallingConventionMIPS64 calling_convention;
3278 codegen_->CreateUnresolvedFieldLocationSummary(
3279 instruction, instruction->GetFieldType(), calling_convention);
3280}
3281
3282void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3283 HUnresolvedStaticFieldSet* instruction) {
3284 FieldAccessCallingConventionMIPS64 calling_convention;
3285 codegen_->GenerateUnresolvedFieldAccess(instruction,
3286 instruction->GetFieldType(),
3287 instruction->GetFieldIndex(),
3288 instruction->GetDexPc(),
3289 calling_convention);
3290}
3291
Alexey Frunze4dda3372015-06-01 18:31:49 -07003292void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3294}
3295
3296void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3297 HBasicBlock* block = instruction->GetBlock();
3298 if (block->GetLoopInformation() != nullptr) {
3299 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3300 // The back edge will generate the suspend check.
3301 return;
3302 }
3303 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3304 // The goto will generate the suspend check.
3305 return;
3306 }
3307 GenerateSuspendCheck(instruction, nullptr);
3308}
3309
3310void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3311 temp->SetLocations(nullptr);
3312}
3313
3314void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3315 // Nothing to do, this is driven by the code generator.
3316}
3317
3318void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3319 LocationSummary* locations =
3320 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3321 InvokeRuntimeCallingConvention calling_convention;
3322 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3323}
3324
3325void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3326 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3327 instruction,
3328 instruction->GetDexPc(),
3329 nullptr);
3330 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3331}
3332
3333void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3334 Primitive::Type input_type = conversion->GetInputType();
3335 Primitive::Type result_type = conversion->GetResultType();
3336 DCHECK_NE(input_type, result_type);
3337
3338 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3339 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3340 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3341 }
3342
3343 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3344 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3345 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3346 call_kind = LocationSummary::kCall;
3347 }
3348
3349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3350
3351 if (call_kind == LocationSummary::kNoCall) {
3352 if (Primitive::IsFloatingPointType(input_type)) {
3353 locations->SetInAt(0, Location::RequiresFpuRegister());
3354 } else {
3355 locations->SetInAt(0, Location::RequiresRegister());
3356 }
3357
3358 if (Primitive::IsFloatingPointType(result_type)) {
3359 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3360 } else {
3361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3362 }
3363 } else {
3364 InvokeRuntimeCallingConvention calling_convention;
3365
3366 if (Primitive::IsFloatingPointType(input_type)) {
3367 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3368 } else {
3369 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3370 }
3371
3372 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3373 }
3374}
3375
3376void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3377 LocationSummary* locations = conversion->GetLocations();
3378 Primitive::Type result_type = conversion->GetResultType();
3379 Primitive::Type input_type = conversion->GetInputType();
3380
3381 DCHECK_NE(input_type, result_type);
3382
3383 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3384 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3385 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3386
3387 switch (result_type) {
3388 case Primitive::kPrimChar:
3389 __ Andi(dst, src, 0xFFFF);
3390 break;
3391 case Primitive::kPrimByte:
3392 // long is never converted into types narrower than int directly,
3393 // so SEB and SEH can be used without ever causing unpredictable results
3394 // on 64-bit inputs
3395 DCHECK(input_type != Primitive::kPrimLong);
3396 __ Seb(dst, src);
3397 break;
3398 case Primitive::kPrimShort:
3399 // long is never converted into types narrower than int directly,
3400 // so SEB and SEH can be used without ever causing unpredictable results
3401 // on 64-bit inputs
3402 DCHECK(input_type != Primitive::kPrimLong);
3403 __ Seh(dst, src);
3404 break;
3405 case Primitive::kPrimInt:
3406 case Primitive::kPrimLong:
3407 // Sign-extend 32-bit int into bits 32 through 63 for
3408 // int-to-long and long-to-int conversions
3409 __ Sll(dst, src, 0);
3410 break;
3411
3412 default:
3413 LOG(FATAL) << "Unexpected type conversion from " << input_type
3414 << " to " << result_type;
3415 }
3416 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3417 if (input_type != Primitive::kPrimLong) {
3418 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3419 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3420 __ Mtc1(src, FTMP);
3421 if (result_type == Primitive::kPrimFloat) {
3422 __ Cvtsw(dst, FTMP);
3423 } else {
3424 __ Cvtdw(dst, FTMP);
3425 }
3426 } else {
3427 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
3428 : QUICK_ENTRY_POINT(pL2d);
3429 codegen_->InvokeRuntime(entry_offset,
3430 conversion,
3431 conversion->GetDexPc(),
3432 nullptr);
3433 }
3434 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3435 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3436 int32_t entry_offset;
3437 if (result_type != Primitive::kPrimLong) {
3438 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
3439 : QUICK_ENTRY_POINT(pD2iz);
3440 } else {
3441 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
3442 : QUICK_ENTRY_POINT(pD2l);
3443 }
3444 codegen_->InvokeRuntime(entry_offset,
3445 conversion,
3446 conversion->GetDexPc(),
3447 nullptr);
3448 } else if (Primitive::IsFloatingPointType(result_type) &&
3449 Primitive::IsFloatingPointType(input_type)) {
3450 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3451 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3452 if (result_type == Primitive::kPrimFloat) {
3453 __ Cvtsd(dst, src);
3454 } else {
3455 __ Cvtds(dst, src);
3456 }
3457 } else {
3458 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3459 << " to " << result_type;
3460 }
3461}
3462
3463void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3464 HandleShift(ushr);
3465}
3466
3467void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3468 HandleShift(ushr);
3469}
3470
3471void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3472 HandleBinaryOp(instruction);
3473}
3474
3475void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3476 HandleBinaryOp(instruction);
3477}
3478
3479void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3480 // Nothing to do, this should be removed during prepare for register allocator.
3481 LOG(FATAL) << "Unreachable";
3482}
3483
3484void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3485 // Nothing to do, this should be removed during prepare for register allocator.
3486 LOG(FATAL) << "Unreachable";
3487}
3488
3489void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
3490 VisitCondition(comp);
3491}
3492
3493void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
3494 VisitCondition(comp);
3495}
3496
3497void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
3498 VisitCondition(comp);
3499}
3500
3501void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
3502 VisitCondition(comp);
3503}
3504
3505void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
3506 VisitCondition(comp);
3507}
3508
3509void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
3510 VisitCondition(comp);
3511}
3512
3513void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3514 VisitCondition(comp);
3515}
3516
3517void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3518 VisitCondition(comp);
3519}
3520
3521void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3522 VisitCondition(comp);
3523}
3524
3525void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3526 VisitCondition(comp);
3527}
3528
3529void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3530 VisitCondition(comp);
3531}
3532
3533void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3534 VisitCondition(comp);
3535}
3536
Aart Bike9f37602015-10-09 11:15:55 -07003537void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
3538 VisitCondition(comp);
3539}
3540
3541void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
3542 VisitCondition(comp);
3543}
3544
3545void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3546 VisitCondition(comp);
3547}
3548
3549void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3550 VisitCondition(comp);
3551}
3552
3553void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
3554 VisitCondition(comp);
3555}
3556
3557void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
3558 VisitCondition(comp);
3559}
3560
3561void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3562 VisitCondition(comp);
3563}
3564
3565void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3566 VisitCondition(comp);
3567}
3568
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003569void LocationsBuilderMIPS64::VisitFakeString(HFakeString* instruction) {
3570 DCHECK(codegen_->IsBaseline());
3571 LocationSummary* locations =
3572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3573 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3574}
3575
3576void InstructionCodeGeneratorMIPS64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3577 DCHECK(codegen_->IsBaseline());
3578 // Will be generated at use site.
3579}
3580
Mark Mendellfe57faa2015-09-18 09:26:15 -04003581// Simple implementation of packed switch - generate cascaded compare/jumps.
3582void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3583 LocationSummary* locations =
3584 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3585 locations->SetInAt(0, Location::RequiresRegister());
3586}
3587
3588void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3589 int32_t lower_bound = switch_instr->GetStartValue();
3590 int32_t num_entries = switch_instr->GetNumEntries();
3591 LocationSummary* locations = switch_instr->GetLocations();
3592 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
3593 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3594
3595 // Create a series of compare/jumps.
3596 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3597 for (int32_t i = 0; i < num_entries; i++) {
3598 int32_t case_value = lower_bound + i;
Vladimir Markoec7802a2015-10-01 20:57:57 +01003599 Label* succ = codegen_->GetLabelOf(successors[i]);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003600 if (case_value == 0) {
3601 __ Beqzc(value_reg, succ);
3602 } else {
3603 __ LoadConst32(TMP, case_value);
3604 __ Beqc(value_reg, TMP, succ);
3605 }
3606 }
3607
3608 // And the default for any other value.
3609 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3610 __ B(codegen_->GetLabelOf(default_block));
3611 }
3612}
3613
Alexey Frunze4dda3372015-06-01 18:31:49 -07003614} // namespace mips64
3615} // namespace art