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