blob: d4fcaf93217de43958bf4f91a176a1cf265c2218 [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
Vladimir Markodc151b22015-10-15 18:02:30 +01002531HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS64::GetSupportedInvokeStaticOrDirectDispatch(
2532 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
2533 MethodReference target_method ATTRIBUTE_UNUSED) {
2534 switch (desired_dispatch_info.method_load_kind) {
2535 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2536 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2537 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
2538 return HInvokeStaticOrDirect::DispatchInfo {
2539 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
2540 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2541 0u,
2542 0u
2543 };
2544 default:
2545 break;
2546 }
2547 switch (desired_dispatch_info.code_ptr_location) {
2548 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2549 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2550 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
2551 return HInvokeStaticOrDirect::DispatchInfo {
2552 desired_dispatch_info.method_load_kind,
2553 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
2554 desired_dispatch_info.method_load_data,
2555 0u
2556 };
2557 default:
2558 return desired_dispatch_info;
2559 }
2560}
2561
Alexey Frunze4dda3372015-06-01 18:31:49 -07002562void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
2563 // All registers are assumed to be correctly set up per the calling convention.
2564
Vladimir Marko58155012015-08-19 12:49:41 +00002565 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2566 switch (invoke->GetMethodLoadKind()) {
2567 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2568 // temp = thread->string_init_entrypoint
2569 __ LoadFromOffset(kLoadDoubleword,
2570 temp.AsRegister<GpuRegister>(),
2571 TR,
2572 invoke->GetStringInitOffset());
2573 break;
2574 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2575 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2576 break;
2577 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2578 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
2579 break;
2580 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Marko58155012015-08-19 12:49:41 +00002581 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01002582 // TODO: Implement these types.
2583 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
2584 LOG(FATAL) << "Unsupported";
2585 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00002586 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2587 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2588 GpuRegister reg = temp.AsRegister<GpuRegister>();
2589 GpuRegister method_reg;
2590 if (current_method.IsRegister()) {
2591 method_reg = current_method.AsRegister<GpuRegister>();
2592 } else {
2593 // TODO: use the appropriate DCHECK() here if possible.
2594 // DCHECK(invoke->GetLocations()->Intrinsified());
2595 DCHECK(!current_method.IsValid());
2596 method_reg = reg;
2597 __ Ld(reg, SP, kCurrentMethodStackOffset);
2598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002599
Vladimir Marko58155012015-08-19 12:49:41 +00002600 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002601 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00002602 reg,
2603 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01002604 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00002605 // temp = temp[index_in_cache]
2606 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2607 __ LoadFromOffset(kLoadDoubleword,
2608 reg,
2609 reg,
2610 CodeGenerator::GetCachePointerOffset(index_in_cache));
2611 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002612 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002613 }
2614
Vladimir Marko58155012015-08-19 12:49:41 +00002615 switch (invoke->GetCodePtrLocation()) {
2616 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2617 __ Jalr(&frame_entry_label_, T9);
2618 break;
2619 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2620 // LR = invoke->GetDirectCodePtr();
2621 __ LoadConst64(T9, invoke->GetDirectCodePtr());
2622 // LR()
2623 __ Jalr(T9);
2624 break;
Vladimir Marko58155012015-08-19 12:49:41 +00002625 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01002626 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2627 // TODO: Implement these types.
2628 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
2629 LOG(FATAL) << "Unsupported";
2630 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00002631 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2632 // T9 = callee_method->entry_point_from_quick_compiled_code_;
2633 __ LoadFromOffset(kLoadDoubleword,
2634 T9,
2635 callee_method.AsRegister<GpuRegister>(),
2636 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2637 kMips64WordSize).Int32Value());
2638 // T9()
2639 __ Jalr(T9);
2640 break;
2641 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002642 DCHECK(!IsLeafMethod());
2643}
2644
2645void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2646 // When we do not run baseline, explicit clinit checks triggered by static
2647 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2648 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2649
2650 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2651 return;
2652 }
2653
2654 LocationSummary* locations = invoke->GetLocations();
2655 codegen_->GenerateStaticOrDirectCall(invoke,
2656 locations->HasTemps()
2657 ? locations->GetTemp(0)
2658 : Location::NoLocation());
2659 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2660}
2661
Alexey Frunze53afca12015-11-05 16:34:23 -08002662void CodeGeneratorMIPS64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663 LocationSummary* locations = invoke->GetLocations();
2664 Location receiver = locations->InAt(0);
Alexey Frunze53afca12015-11-05 16:34:23 -08002665 GpuRegister temp = temp_location.AsRegister<GpuRegister>();
Alexey Frunze4dda3372015-06-01 18:31:49 -07002666 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2667 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
2668 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2669 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2670
2671 // temp = object->GetClass();
2672 DCHECK(receiver.IsRegister());
2673 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
Alexey Frunze53afca12015-11-05 16:34:23 -08002674 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002675 // temp = temp->GetMethodAt(method_offset);
2676 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2677 // T9 = temp->GetEntryPoint();
2678 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2679 // T9();
2680 __ Jalr(T9);
Alexey Frunze53afca12015-11-05 16:34:23 -08002681}
2682
2683void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2684 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2685 return;
2686 }
2687
2688 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002689 DCHECK(!codegen_->IsLeafMethod());
2690 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2691}
2692
2693void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01002694 InvokeRuntimeCallingConvention calling_convention;
2695 CodeGenerator::CreateLoadClassLocationSummary(
2696 cls,
2697 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
2698 Location::RegisterLocation(A0));
Alexey Frunze4dda3372015-06-01 18:31:49 -07002699}
2700
2701void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
2702 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01002703 if (cls->NeedsAccessCheck()) {
2704 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
2705 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
2706 cls,
2707 cls->GetDexPc(),
2708 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01002709 return;
2710 }
2711
2712 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2713 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2714 if (cls->IsReferrersClass()) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002715 DCHECK(!cls->CanCallRuntime());
2716 DCHECK(!cls->MustGenerateClinitCheck());
2717 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2718 ArtMethod::DeclaringClassOffset().Int32Value());
2719 } else {
2720 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002721 __ LoadFromOffset(kLoadDoubleword, out, current_method,
2722 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002723 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002724 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002725 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2726 cls,
2727 cls,
2728 cls->GetDexPc(),
2729 cls->MustGenerateClinitCheck());
2730 codegen_->AddSlowPath(slow_path);
2731 __ Beqzc(out, slow_path->GetEntryLabel());
2732 if (cls->MustGenerateClinitCheck()) {
2733 GenerateClassInitializationCheck(slow_path, out);
2734 } else {
2735 __ Bind(slow_path->GetExitLabel());
2736 }
2737 }
2738}
2739
David Brazdilcb1c0552015-08-04 16:22:25 +01002740static int32_t GetExceptionTlsOffset() {
2741 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
2742}
2743
Alexey Frunze4dda3372015-06-01 18:31:49 -07002744void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2747 locations->SetOut(Location::RequiresRegister());
2748}
2749
2750void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
2751 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01002752 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
2753}
2754
2755void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
2756 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2757}
2758
2759void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2760 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002761}
2762
2763void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
2764 load->SetLocations(nullptr);
2765}
2766
2767void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
2768 // Nothing to do, this is driven by the code generator.
2769}
2770
2771void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
2772 LocationSummary* locations =
2773 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2774 locations->SetInAt(0, Location::RequiresRegister());
2775 locations->SetOut(Location::RequiresRegister());
2776}
2777
2778void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
2779 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
2780 codegen_->AddSlowPath(slow_path);
2781
2782 LocationSummary* locations = load->GetLocations();
2783 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2784 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2785 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2786 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01002787 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002788 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002789 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002790 __ Beqzc(out, slow_path->GetEntryLabel());
2791 __ Bind(slow_path->GetExitLabel());
2792}
2793
2794void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
2795 local->SetLocations(nullptr);
2796}
2797
2798void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
2799 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2800}
2801
2802void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
2803 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2804 locations->SetOut(Location::ConstantLocation(constant));
2805}
2806
2807void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2808 // Will be generated at use site.
2809}
2810
2811void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2814 InvokeRuntimeCallingConvention calling_convention;
2815 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2816}
2817
2818void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2819 codegen_->InvokeRuntime(instruction->IsEnter()
2820 ? QUICK_ENTRY_POINT(pLockObject)
2821 : QUICK_ENTRY_POINT(pUnlockObject),
2822 instruction,
2823 instruction->GetDexPc(),
2824 nullptr);
2825 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
2826}
2827
2828void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
2829 LocationSummary* locations =
2830 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2831 switch (mul->GetResultType()) {
2832 case Primitive::kPrimInt:
2833 case Primitive::kPrimLong:
2834 locations->SetInAt(0, Location::RequiresRegister());
2835 locations->SetInAt(1, Location::RequiresRegister());
2836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2837 break;
2838
2839 case Primitive::kPrimFloat:
2840 case Primitive::kPrimDouble:
2841 locations->SetInAt(0, Location::RequiresFpuRegister());
2842 locations->SetInAt(1, Location::RequiresFpuRegister());
2843 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2844 break;
2845
2846 default:
2847 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2848 }
2849}
2850
2851void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
2852 Primitive::Type type = instruction->GetType();
2853 LocationSummary* locations = instruction->GetLocations();
2854
2855 switch (type) {
2856 case Primitive::kPrimInt:
2857 case Primitive::kPrimLong: {
2858 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2859 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2860 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
2861 if (type == Primitive::kPrimInt)
2862 __ MulR6(dst, lhs, rhs);
2863 else
2864 __ Dmul(dst, lhs, rhs);
2865 break;
2866 }
2867 case Primitive::kPrimFloat:
2868 case Primitive::kPrimDouble: {
2869 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2870 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2871 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2872 if (type == Primitive::kPrimFloat)
2873 __ MulS(dst, lhs, rhs);
2874 else
2875 __ MulD(dst, lhs, rhs);
2876 break;
2877 }
2878 default:
2879 LOG(FATAL) << "Unexpected mul type " << type;
2880 }
2881}
2882
2883void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
2884 LocationSummary* locations =
2885 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2886 switch (neg->GetResultType()) {
2887 case Primitive::kPrimInt:
2888 case Primitive::kPrimLong:
2889 locations->SetInAt(0, Location::RequiresRegister());
2890 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2891 break;
2892
2893 case Primitive::kPrimFloat:
2894 case Primitive::kPrimDouble:
2895 locations->SetInAt(0, Location::RequiresFpuRegister());
2896 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2897 break;
2898
2899 default:
2900 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2901 }
2902}
2903
2904void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
2905 Primitive::Type type = instruction->GetType();
2906 LocationSummary* locations = instruction->GetLocations();
2907
2908 switch (type) {
2909 case Primitive::kPrimInt:
2910 case Primitive::kPrimLong: {
2911 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2912 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2913 if (type == Primitive::kPrimInt)
2914 __ Subu(dst, ZERO, src);
2915 else
2916 __ Dsubu(dst, ZERO, src);
2917 break;
2918 }
2919 case Primitive::kPrimFloat:
2920 case Primitive::kPrimDouble: {
2921 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2922 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
2923 if (type == Primitive::kPrimFloat)
2924 __ NegS(dst, src);
2925 else
2926 __ NegD(dst, src);
2927 break;
2928 }
2929 default:
2930 LOG(FATAL) << "Unexpected neg type " << type;
2931 }
2932}
2933
2934void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
2935 LocationSummary* locations =
2936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2937 InvokeRuntimeCallingConvention calling_convention;
2938 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2939 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2940 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2941 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2942}
2943
2944void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
2945 LocationSummary* locations = instruction->GetLocations();
2946 // Move an uint16_t value to a register.
2947 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002948 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2949 instruction,
2950 instruction->GetDexPc(),
2951 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002952 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
2953}
2954
2955void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
2956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2958 InvokeRuntimeCallingConvention calling_convention;
2959 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2960 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2961 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2962}
2963
2964void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
2965 LocationSummary* locations = instruction->GetLocations();
2966 // Move an uint16_t value to a register.
2967 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002968 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2969 instruction,
2970 instruction->GetDexPc(),
2971 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002972 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
2973}
2974
2975void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
2976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2977 locations->SetInAt(0, Location::RequiresRegister());
2978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2979}
2980
2981void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
2982 Primitive::Type type = instruction->GetType();
2983 LocationSummary* locations = instruction->GetLocations();
2984
2985 switch (type) {
2986 case Primitive::kPrimInt:
2987 case Primitive::kPrimLong: {
2988 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2989 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2990 __ Nor(dst, src, ZERO);
2991 break;
2992 }
2993
2994 default:
2995 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2996 }
2997}
2998
2999void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3001 locations->SetInAt(0, Location::RequiresRegister());
3002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3003}
3004
3005void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
3006 LocationSummary* locations = instruction->GetLocations();
3007 __ Xori(locations->Out().AsRegister<GpuRegister>(),
3008 locations->InAt(0).AsRegister<GpuRegister>(),
3009 1);
3010}
3011
3012void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003013 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3014 ? LocationSummary::kCallOnSlowPath
3015 : LocationSummary::kNoCall;
3016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07003017 locations->SetInAt(0, Location::RequiresRegister());
3018 if (instruction->HasUses()) {
3019 locations->SetOut(Location::SameAsFirstInput());
3020 }
3021}
3022
3023void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
3024 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3025 return;
3026 }
3027 Location obj = instruction->GetLocations()->InAt(0);
3028
3029 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
3030 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3031}
3032
3033void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
3034 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
3035 codegen_->AddSlowPath(slow_path);
3036
3037 Location obj = instruction->GetLocations()->InAt(0);
3038
3039 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
3040}
3041
3042void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003043 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07003044 GenerateImplicitNullCheck(instruction);
3045 } else {
3046 GenerateExplicitNullCheck(instruction);
3047 }
3048}
3049
3050void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
3051 HandleBinaryOp(instruction);
3052}
3053
3054void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
3055 HandleBinaryOp(instruction);
3056}
3057
3058void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3059 LOG(FATAL) << "Unreachable";
3060}
3061
3062void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
3063 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3064}
3065
3066void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
3067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3068 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3069 if (location.IsStackSlot()) {
3070 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3071 } else if (location.IsDoubleStackSlot()) {
3072 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3073 }
3074 locations->SetOut(location);
3075}
3076
3077void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
3078 ATTRIBUTE_UNUSED) {
3079 // Nothing to do, the parameter is already at its location.
3080}
3081
3082void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
3083 LocationSummary* locations =
3084 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3085 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3086}
3087
3088void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
3089 ATTRIBUTE_UNUSED) {
3090 // Nothing to do, the method is already at its location.
3091}
3092
3093void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
3094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3095 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3096 locations->SetInAt(i, Location::Any());
3097 }
3098 locations->SetOut(Location::Any());
3099}
3100
3101void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3102 LOG(FATAL) << "Unreachable";
3103}
3104
3105void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3106 Primitive::Type type = rem->GetResultType();
3107 LocationSummary::CallKind call_kind =
3108 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3109 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3110
3111 switch (type) {
3112 case Primitive::kPrimInt:
3113 case Primitive::kPrimLong:
3114 locations->SetInAt(0, Location::RequiresRegister());
3115 locations->SetInAt(1, Location::RequiresRegister());
3116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3117 break;
3118
3119 case Primitive::kPrimFloat:
3120 case Primitive::kPrimDouble: {
3121 InvokeRuntimeCallingConvention calling_convention;
3122 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3123 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3124 locations->SetOut(calling_convention.GetReturnLocation(type));
3125 break;
3126 }
3127
3128 default:
3129 LOG(FATAL) << "Unexpected rem type " << type;
3130 }
3131}
3132
3133void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3134 Primitive::Type type = instruction->GetType();
3135 LocationSummary* locations = instruction->GetLocations();
3136
3137 switch (type) {
3138 case Primitive::kPrimInt:
3139 case Primitive::kPrimLong: {
3140 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3141 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3142 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3143 if (type == Primitive::kPrimInt)
3144 __ ModR6(dst, lhs, rhs);
3145 else
3146 __ Dmod(dst, lhs, rhs);
3147 break;
3148 }
3149
3150 case Primitive::kPrimFloat:
3151 case Primitive::kPrimDouble: {
3152 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3153 : QUICK_ENTRY_POINT(pFmod);
3154 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
3155 break;
3156 }
3157 default:
3158 LOG(FATAL) << "Unexpected rem type " << type;
3159 }
3160}
3161
3162void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3163 memory_barrier->SetLocations(nullptr);
3164}
3165
3166void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3167 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3168}
3169
3170void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3172 Primitive::Type return_type = ret->InputAt(0)->GetType();
3173 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3174}
3175
3176void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3177 codegen_->GenerateFrameExit();
3178}
3179
3180void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3181 ret->SetLocations(nullptr);
3182}
3183
3184void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3185 codegen_->GenerateFrameExit();
3186}
3187
3188void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3189 HandleShift(shl);
3190}
3191
3192void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3193 HandleShift(shl);
3194}
3195
3196void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3197 HandleShift(shr);
3198}
3199
3200void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3201 HandleShift(shr);
3202}
3203
3204void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3205 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3206 Primitive::Type field_type = store->InputAt(1)->GetType();
3207 switch (field_type) {
3208 case Primitive::kPrimNot:
3209 case Primitive::kPrimBoolean:
3210 case Primitive::kPrimByte:
3211 case Primitive::kPrimChar:
3212 case Primitive::kPrimShort:
3213 case Primitive::kPrimInt:
3214 case Primitive::kPrimFloat:
3215 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3216 break;
3217
3218 case Primitive::kPrimLong:
3219 case Primitive::kPrimDouble:
3220 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3221 break;
3222
3223 default:
3224 LOG(FATAL) << "Unimplemented local type " << field_type;
3225 }
3226}
3227
3228void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3229}
3230
3231void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3232 HandleBinaryOp(instruction);
3233}
3234
3235void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3236 HandleBinaryOp(instruction);
3237}
3238
3239void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3240 HandleFieldGet(instruction, instruction->GetFieldInfo());
3241}
3242
3243void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3244 HandleFieldGet(instruction, instruction->GetFieldInfo());
3245}
3246
3247void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3248 HandleFieldSet(instruction, instruction->GetFieldInfo());
3249}
3250
3251void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3252 HandleFieldSet(instruction, instruction->GetFieldInfo());
3253}
3254
Calin Juravlee460d1d2015-09-29 04:52:17 +01003255void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3256 HUnresolvedInstanceFieldGet* instruction) {
3257 FieldAccessCallingConventionMIPS64 calling_convention;
3258 codegen_->CreateUnresolvedFieldLocationSummary(
3259 instruction, instruction->GetFieldType(), calling_convention);
3260}
3261
3262void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3263 HUnresolvedInstanceFieldGet* instruction) {
3264 FieldAccessCallingConventionMIPS64 calling_convention;
3265 codegen_->GenerateUnresolvedFieldAccess(instruction,
3266 instruction->GetFieldType(),
3267 instruction->GetFieldIndex(),
3268 instruction->GetDexPc(),
3269 calling_convention);
3270}
3271
3272void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3273 HUnresolvedInstanceFieldSet* instruction) {
3274 FieldAccessCallingConventionMIPS64 calling_convention;
3275 codegen_->CreateUnresolvedFieldLocationSummary(
3276 instruction, instruction->GetFieldType(), calling_convention);
3277}
3278
3279void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3280 HUnresolvedInstanceFieldSet* instruction) {
3281 FieldAccessCallingConventionMIPS64 calling_convention;
3282 codegen_->GenerateUnresolvedFieldAccess(instruction,
3283 instruction->GetFieldType(),
3284 instruction->GetFieldIndex(),
3285 instruction->GetDexPc(),
3286 calling_convention);
3287}
3288
3289void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3290 HUnresolvedStaticFieldGet* instruction) {
3291 FieldAccessCallingConventionMIPS64 calling_convention;
3292 codegen_->CreateUnresolvedFieldLocationSummary(
3293 instruction, instruction->GetFieldType(), calling_convention);
3294}
3295
3296void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3297 HUnresolvedStaticFieldGet* instruction) {
3298 FieldAccessCallingConventionMIPS64 calling_convention;
3299 codegen_->GenerateUnresolvedFieldAccess(instruction,
3300 instruction->GetFieldType(),
3301 instruction->GetFieldIndex(),
3302 instruction->GetDexPc(),
3303 calling_convention);
3304}
3305
3306void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3307 HUnresolvedStaticFieldSet* instruction) {
3308 FieldAccessCallingConventionMIPS64 calling_convention;
3309 codegen_->CreateUnresolvedFieldLocationSummary(
3310 instruction, instruction->GetFieldType(), calling_convention);
3311}
3312
3313void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3314 HUnresolvedStaticFieldSet* instruction) {
3315 FieldAccessCallingConventionMIPS64 calling_convention;
3316 codegen_->GenerateUnresolvedFieldAccess(instruction,
3317 instruction->GetFieldType(),
3318 instruction->GetFieldIndex(),
3319 instruction->GetDexPc(),
3320 calling_convention);
3321}
3322
Alexey Frunze4dda3372015-06-01 18:31:49 -07003323void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3324 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3325}
3326
3327void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3328 HBasicBlock* block = instruction->GetBlock();
3329 if (block->GetLoopInformation() != nullptr) {
3330 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3331 // The back edge will generate the suspend check.
3332 return;
3333 }
3334 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3335 // The goto will generate the suspend check.
3336 return;
3337 }
3338 GenerateSuspendCheck(instruction, nullptr);
3339}
3340
3341void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3342 temp->SetLocations(nullptr);
3343}
3344
3345void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3346 // Nothing to do, this is driven by the code generator.
3347}
3348
3349void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3350 LocationSummary* locations =
3351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3352 InvokeRuntimeCallingConvention calling_convention;
3353 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3354}
3355
3356void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3357 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3358 instruction,
3359 instruction->GetDexPc(),
3360 nullptr);
3361 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3362}
3363
3364void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3365 Primitive::Type input_type = conversion->GetInputType();
3366 Primitive::Type result_type = conversion->GetResultType();
3367 DCHECK_NE(input_type, result_type);
3368
3369 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3370 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3371 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3372 }
3373
3374 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3375 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3376 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3377 call_kind = LocationSummary::kCall;
3378 }
3379
3380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3381
3382 if (call_kind == LocationSummary::kNoCall) {
3383 if (Primitive::IsFloatingPointType(input_type)) {
3384 locations->SetInAt(0, Location::RequiresFpuRegister());
3385 } else {
3386 locations->SetInAt(0, Location::RequiresRegister());
3387 }
3388
3389 if (Primitive::IsFloatingPointType(result_type)) {
3390 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3391 } else {
3392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3393 }
3394 } else {
3395 InvokeRuntimeCallingConvention calling_convention;
3396
3397 if (Primitive::IsFloatingPointType(input_type)) {
3398 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3399 } else {
3400 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3401 }
3402
3403 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3404 }
3405}
3406
3407void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3408 LocationSummary* locations = conversion->GetLocations();
3409 Primitive::Type result_type = conversion->GetResultType();
3410 Primitive::Type input_type = conversion->GetInputType();
3411
3412 DCHECK_NE(input_type, result_type);
3413
3414 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3415 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3416 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3417
3418 switch (result_type) {
3419 case Primitive::kPrimChar:
3420 __ Andi(dst, src, 0xFFFF);
3421 break;
3422 case Primitive::kPrimByte:
3423 // long is never converted into types narrower than int directly,
3424 // so SEB and SEH can be used without ever causing unpredictable results
3425 // on 64-bit inputs
3426 DCHECK(input_type != Primitive::kPrimLong);
3427 __ Seb(dst, src);
3428 break;
3429 case Primitive::kPrimShort:
3430 // long is never converted into types narrower than int directly,
3431 // so SEB and SEH can be used without ever causing unpredictable results
3432 // on 64-bit inputs
3433 DCHECK(input_type != Primitive::kPrimLong);
3434 __ Seh(dst, src);
3435 break;
3436 case Primitive::kPrimInt:
3437 case Primitive::kPrimLong:
3438 // Sign-extend 32-bit int into bits 32 through 63 for
3439 // int-to-long and long-to-int conversions
3440 __ Sll(dst, src, 0);
3441 break;
3442
3443 default:
3444 LOG(FATAL) << "Unexpected type conversion from " << input_type
3445 << " to " << result_type;
3446 }
3447 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3448 if (input_type != Primitive::kPrimLong) {
3449 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3450 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3451 __ Mtc1(src, FTMP);
3452 if (result_type == Primitive::kPrimFloat) {
3453 __ Cvtsw(dst, FTMP);
3454 } else {
3455 __ Cvtdw(dst, FTMP);
3456 }
3457 } else {
3458 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
3459 : QUICK_ENTRY_POINT(pL2d);
3460 codegen_->InvokeRuntime(entry_offset,
3461 conversion,
3462 conversion->GetDexPc(),
3463 nullptr);
3464 }
3465 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3466 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3467 int32_t entry_offset;
3468 if (result_type != Primitive::kPrimLong) {
3469 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
3470 : QUICK_ENTRY_POINT(pD2iz);
3471 } else {
3472 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
3473 : QUICK_ENTRY_POINT(pD2l);
3474 }
3475 codegen_->InvokeRuntime(entry_offset,
3476 conversion,
3477 conversion->GetDexPc(),
3478 nullptr);
3479 } else if (Primitive::IsFloatingPointType(result_type) &&
3480 Primitive::IsFloatingPointType(input_type)) {
3481 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3482 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3483 if (result_type == Primitive::kPrimFloat) {
3484 __ Cvtsd(dst, src);
3485 } else {
3486 __ Cvtds(dst, src);
3487 }
3488 } else {
3489 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3490 << " to " << result_type;
3491 }
3492}
3493
3494void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3495 HandleShift(ushr);
3496}
3497
3498void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3499 HandleShift(ushr);
3500}
3501
3502void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3503 HandleBinaryOp(instruction);
3504}
3505
3506void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3507 HandleBinaryOp(instruction);
3508}
3509
3510void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3511 // Nothing to do, this should be removed during prepare for register allocator.
3512 LOG(FATAL) << "Unreachable";
3513}
3514
3515void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3516 // Nothing to do, this should be removed during prepare for register allocator.
3517 LOG(FATAL) << "Unreachable";
3518}
3519
3520void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
3521 VisitCondition(comp);
3522}
3523
3524void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
3525 VisitCondition(comp);
3526}
3527
3528void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
3529 VisitCondition(comp);
3530}
3531
3532void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
3533 VisitCondition(comp);
3534}
3535
3536void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
3537 VisitCondition(comp);
3538}
3539
3540void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
3541 VisitCondition(comp);
3542}
3543
3544void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3545 VisitCondition(comp);
3546}
3547
3548void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3549 VisitCondition(comp);
3550}
3551
3552void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3553 VisitCondition(comp);
3554}
3555
3556void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3557 VisitCondition(comp);
3558}
3559
3560void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3561 VisitCondition(comp);
3562}
3563
3564void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3565 VisitCondition(comp);
3566}
3567
Aart Bike9f37602015-10-09 11:15:55 -07003568void LocationsBuilderMIPS64::VisitBelow(HBelow* comp) {
3569 VisitCondition(comp);
3570}
3571
3572void InstructionCodeGeneratorMIPS64::VisitBelow(HBelow* comp) {
3573 VisitCondition(comp);
3574}
3575
3576void LocationsBuilderMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3577 VisitCondition(comp);
3578}
3579
3580void InstructionCodeGeneratorMIPS64::VisitBelowOrEqual(HBelowOrEqual* comp) {
3581 VisitCondition(comp);
3582}
3583
3584void LocationsBuilderMIPS64::VisitAbove(HAbove* comp) {
3585 VisitCondition(comp);
3586}
3587
3588void InstructionCodeGeneratorMIPS64::VisitAbove(HAbove* comp) {
3589 VisitCondition(comp);
3590}
3591
3592void LocationsBuilderMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3593 VisitCondition(comp);
3594}
3595
3596void InstructionCodeGeneratorMIPS64::VisitAboveOrEqual(HAboveOrEqual* comp) {
3597 VisitCondition(comp);
3598}
3599
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003600void LocationsBuilderMIPS64::VisitFakeString(HFakeString* instruction) {
3601 DCHECK(codegen_->IsBaseline());
3602 LocationSummary* locations =
3603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3604 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3605}
3606
3607void InstructionCodeGeneratorMIPS64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3608 DCHECK(codegen_->IsBaseline());
3609 // Will be generated at use site.
3610}
3611
Mark Mendellfe57faa2015-09-18 09:26:15 -04003612// Simple implementation of packed switch - generate cascaded compare/jumps.
3613void LocationsBuilderMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3614 LocationSummary* locations =
3615 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
3616 locations->SetInAt(0, Location::RequiresRegister());
3617}
3618
3619void InstructionCodeGeneratorMIPS64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
3620 int32_t lower_bound = switch_instr->GetStartValue();
3621 int32_t num_entries = switch_instr->GetNumEntries();
3622 LocationSummary* locations = switch_instr->GetLocations();
3623 GpuRegister value_reg = locations->InAt(0).AsRegister<GpuRegister>();
3624 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
3625
3626 // Create a series of compare/jumps.
3627 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
3628 for (int32_t i = 0; i < num_entries; i++) {
3629 int32_t case_value = lower_bound + i;
Vladimir Markoec7802a2015-10-01 20:57:57 +01003630 Label* succ = codegen_->GetLabelOf(successors[i]);
Mark Mendellfe57faa2015-09-18 09:26:15 -04003631 if (case_value == 0) {
3632 __ Beqzc(value_reg, succ);
3633 } else {
3634 __ LoadConst32(TMP, case_value);
3635 __ Beqc(value_reg, TMP, succ);
3636 }
3637 }
3638
3639 // And the default for any other value.
3640 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
3641 __ B(codegen_->GetLabelOf(default_block));
3642 }
3643}
3644
Alexey Frunze4dda3372015-06-01 18:31:49 -07003645} // namespace mips64
3646} // namespace art