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