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