blob: ae0f2c89356abbd70d72c07203fcbaca1e318aeb [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
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_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020023#include "entrypoints/quick/quick_entrypoints.h"
24#include "entrypoints/quick/quick_entrypoints_enum.h"
25#include "gc/accounting/card_table.h"
26#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070027#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020028#include "mirror/array-inl.h"
29#include "mirror/class-inl.h"
30#include "offsets.h"
31#include "thread.h"
32#include "utils/assembler.h"
33#include "utils/mips/assembler_mips.h"
34#include "utils/stack_checks.h"
35
36namespace art {
37namespace mips {
38
39static constexpr int kCurrentMethodStackOffset = 0;
40static constexpr Register kMethodRegisterArgument = A0;
41
42// We need extra temporary/scratch registers (in addition to AT) in some cases.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020043static constexpr FRegister FTMP = F8;
44
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020045Location MipsReturnLocation(Primitive::Type return_type) {
46 switch (return_type) {
47 case Primitive::kPrimBoolean:
48 case Primitive::kPrimByte:
49 case Primitive::kPrimChar:
50 case Primitive::kPrimShort:
51 case Primitive::kPrimInt:
52 case Primitive::kPrimNot:
53 return Location::RegisterLocation(V0);
54
55 case Primitive::kPrimLong:
56 return Location::RegisterPairLocation(V0, V1);
57
58 case Primitive::kPrimFloat:
59 case Primitive::kPrimDouble:
60 return Location::FpuRegisterLocation(F0);
61
62 case Primitive::kPrimVoid:
63 return Location();
64 }
65 UNREACHABLE();
66}
67
68Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
69 return MipsReturnLocation(type);
70}
71
72Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
73 return Location::RegisterLocation(kMethodRegisterArgument);
74}
75
76Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
77 Location next_location;
78
79 switch (type) {
80 case Primitive::kPrimBoolean:
81 case Primitive::kPrimByte:
82 case Primitive::kPrimChar:
83 case Primitive::kPrimShort:
84 case Primitive::kPrimInt:
85 case Primitive::kPrimNot: {
86 uint32_t gp_index = gp_index_++;
87 if (gp_index < calling_convention.GetNumberOfRegisters()) {
88 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
89 } else {
90 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
91 next_location = Location::StackSlot(stack_offset);
92 }
93 break;
94 }
95
96 case Primitive::kPrimLong: {
97 uint32_t gp_index = gp_index_;
98 gp_index_ += 2;
99 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
100 if (calling_convention.GetRegisterAt(gp_index) == A1) {
101 gp_index_++; // Skip A1, and use A2_A3 instead.
102 gp_index++;
103 }
104 Register low_even = calling_convention.GetRegisterAt(gp_index);
105 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
106 DCHECK_EQ(low_even + 1, high_odd);
107 next_location = Location::RegisterPairLocation(low_even, high_odd);
108 } else {
109 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
110 next_location = Location::DoubleStackSlot(stack_offset);
111 }
112 break;
113 }
114
115 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
116 // will take up the even/odd pair, while floats are stored in even regs only.
117 // On 64 bit FPU, both double and float are stored in even registers only.
118 case Primitive::kPrimFloat:
119 case Primitive::kPrimDouble: {
120 uint32_t float_index = float_index_++;
121 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
122 next_location = Location::FpuRegisterLocation(
123 calling_convention.GetFpuRegisterAt(float_index));
124 } else {
125 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
126 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
127 : Location::StackSlot(stack_offset);
128 }
129 break;
130 }
131
132 case Primitive::kPrimVoid:
133 LOG(FATAL) << "Unexpected parameter type " << type;
134 break;
135 }
136
137 // Space on the stack is reserved for all arguments.
138 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
139
140 return next_location;
141}
142
143Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
144 return MipsReturnLocation(type);
145}
146
147#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()->
148#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
149
150class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
151 public:
152 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : instruction_(instruction) {}
153
154 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
155 LocationSummary* locations = instruction_->GetLocations();
156 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
157 __ Bind(GetEntryLabel());
158 if (instruction_->CanThrowIntoCatchBlock()) {
159 // Live registers will be restored in the catch block if caught.
160 SaveLiveRegisters(codegen, instruction_->GetLocations());
161 }
162 // We're moving two locations to locations that could overlap, so we need a parallel
163 // move resolver.
164 InvokeRuntimeCallingConvention calling_convention;
165 codegen->EmitParallelMoves(locations->InAt(0),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
167 Primitive::kPrimInt,
168 locations->InAt(1),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
170 Primitive::kPrimInt);
171 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
172 instruction_,
173 instruction_->GetDexPc(),
174 this,
175 IsDirectEntrypoint(kQuickThrowArrayBounds));
176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
177 }
178
179 bool IsFatal() const OVERRIDE { return true; }
180
181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
182
183 private:
184 HBoundsCheck* const instruction_;
185
186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
187};
188
189class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
190 public:
191 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : instruction_(instruction) {}
192
193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
194 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
195 __ Bind(GetEntryLabel());
196 if (instruction_->CanThrowIntoCatchBlock()) {
197 // Live registers will be restored in the catch block if caught.
198 SaveLiveRegisters(codegen, instruction_->GetLocations());
199 }
200 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
201 instruction_,
202 instruction_->GetDexPc(),
203 this,
204 IsDirectEntrypoint(kQuickThrowDivZero));
205 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
206 }
207
208 bool IsFatal() const OVERRIDE { return true; }
209
210 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
211
212 private:
213 HDivZeroCheck* const instruction_;
214 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
215};
216
217class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
218 public:
219 LoadClassSlowPathMIPS(HLoadClass* cls,
220 HInstruction* at,
221 uint32_t dex_pc,
222 bool do_clinit)
223 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
224 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
225 }
226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 LocationSummary* locations = at_->GetLocations();
229 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
230
231 __ Bind(GetEntryLabel());
232 SaveLiveRegisters(codegen, locations);
233
234 InvokeRuntimeCallingConvention calling_convention;
235 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
236
237 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
238 : QUICK_ENTRY_POINT(pInitializeType);
239 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
240 : IsDirectEntrypoint(kQuickInitializeType);
241
242 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
243 if (do_clinit_) {
244 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
245 } else {
246 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
247 }
248
249 // Move the class to the desired location.
250 Location out = locations->Out();
251 if (out.IsValid()) {
252 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
253 Primitive::Type type = at_->GetType();
254 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
255 }
256
257 RestoreLiveRegisters(codegen, locations);
258 __ B(GetExitLabel());
259 }
260
261 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
262
263 private:
264 // The class this slow path will load.
265 HLoadClass* const cls_;
266
267 // The instruction where this slow path is happening.
268 // (Might be the load class or an initialization check).
269 HInstruction* const at_;
270
271 // The dex PC of `at_`.
272 const uint32_t dex_pc_;
273
274 // Whether to initialize the class.
275 const bool do_clinit_;
276
277 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
278};
279
280class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
281 public:
282 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : instruction_(instruction) {}
283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
286 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
287 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
288
289 __ Bind(GetEntryLabel());
290 SaveLiveRegisters(codegen, locations);
291
292 InvokeRuntimeCallingConvention calling_convention;
293 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
294 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
295 instruction_,
296 instruction_->GetDexPc(),
297 this,
298 IsDirectEntrypoint(kQuickResolveString));
299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
300 Primitive::Type type = instruction_->GetType();
301 mips_codegen->MoveLocation(locations->Out(),
302 calling_convention.GetReturnLocation(type),
303 type);
304
305 RestoreLiveRegisters(codegen, locations);
306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
312 HLoadString* const instruction_;
313
314 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
315};
316
317class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
318 public:
319 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : instruction_(instr) {}
320
321 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
322 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
323 __ Bind(GetEntryLabel());
324 if (instruction_->CanThrowIntoCatchBlock()) {
325 // Live registers will be restored in the catch block if caught.
326 SaveLiveRegisters(codegen, instruction_->GetLocations());
327 }
328 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
329 instruction_,
330 instruction_->GetDexPc(),
331 this,
332 IsDirectEntrypoint(kQuickThrowNullPointer));
333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
341 HNullCheck* const instruction_;
342
343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
349 : instruction_(instruction), successor_(successor) {}
350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
377 HSuspendCheck* const instruction_;
378 // If not null, the block to branch to after the suspend check.
379 HBasicBlock* const successor_;
380
381 // If `successor_` is null, the label to branch to after the suspend check.
382 MipsLabel return_label_;
383
384 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
385};
386
387class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
388 public:
389 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : instruction_(instruction) {}
390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
392 LocationSummary* locations = instruction_->GetLocations();
393 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
394 uint32_t dex_pc = instruction_->GetDexPc();
395 DCHECK(instruction_->IsCheckCast()
396 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
397 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
398
399 __ Bind(GetEntryLabel());
400 SaveLiveRegisters(codegen, locations);
401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
405 codegen->EmitParallelMoves(locations->InAt(1),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
407 Primitive::kPrimNot,
408 object_class,
409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
410 Primitive::kPrimNot);
411
412 if (instruction_->IsInstanceOf()) {
413 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
414 instruction_,
415 dex_pc,
416 this,
417 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000418 CheckEntrypointTypes<
419 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 Primitive::Type ret_type = instruction_->GetType();
421 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
422 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 } else {
424 DCHECK(instruction_->IsCheckCast());
425 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
426 instruction_,
427 dex_pc,
428 this,
429 IsDirectEntrypoint(kQuickCheckCast));
430 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
431 }
432
433 RestoreLiveRegisters(codegen, locations);
434 __ B(GetExitLabel());
435 }
436
437 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
438
439 private:
440 HInstruction* const instruction_;
441
442 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
443};
444
445class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
446 public:
447 explicit DeoptimizationSlowPathMIPS(HInstruction* instruction)
448 : instruction_(instruction) {}
449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
451 __ Bind(GetEntryLabel());
452 SaveLiveRegisters(codegen, instruction_->GetLocations());
453 DCHECK(instruction_->IsDeoptimize());
454 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
455 uint32_t dex_pc = deoptimize->GetDexPc();
456 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
457 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
458 instruction_,
459 dex_pc,
460 this,
461 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000462 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200463 }
464
465 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
466
467 private:
468 HInstruction* const instruction_;
469 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
470};
471
472CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
473 const MipsInstructionSetFeatures& isa_features,
474 const CompilerOptions& compiler_options,
475 OptimizingCompilerStats* stats)
476 : CodeGenerator(graph,
477 kNumberOfCoreRegisters,
478 kNumberOfFRegisters,
479 kNumberOfRegisterPairs,
480 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
481 arraysize(kCoreCalleeSaves)),
482 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
483 arraysize(kFpuCalleeSaves)),
484 compiler_options,
485 stats),
486 block_labels_(nullptr),
487 location_builder_(graph, this),
488 instruction_visitor_(graph, this),
489 move_resolver_(graph->GetArena(), this),
490 assembler_(&isa_features),
491 isa_features_(isa_features) {
492 // Save RA (containing the return address) to mimic Quick.
493 AddAllocatedRegister(Location::RegisterLocation(RA));
494}
495
496#undef __
497#define __ down_cast<MipsAssembler*>(GetAssembler())->
498#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
499
500void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
501 // Ensure that we fix up branches.
502 __ FinalizeCode();
503
504 // Adjust native pc offsets in stack maps.
505 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
506 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
507 uint32_t new_position = __ GetAdjustedPosition(old_position);
508 DCHECK_GE(new_position, old_position);
509 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
510 }
511
512 // Adjust pc offsets for the disassembly information.
513 if (disasm_info_ != nullptr) {
514 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
515 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
516 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
517 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
518 it.second.start = __ GetAdjustedPosition(it.second.start);
519 it.second.end = __ GetAdjustedPosition(it.second.end);
520 }
521 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
522 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
523 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
524 }
525 }
526
527 CodeGenerator::Finalize(allocator);
528}
529
530MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
531 return codegen_->GetAssembler();
532}
533
534void ParallelMoveResolverMIPS::EmitMove(size_t index) {
535 DCHECK_LT(index, moves_.size());
536 MoveOperands* move = moves_[index];
537 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
538}
539
540void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
541 DCHECK_LT(index, moves_.size());
542 MoveOperands* move = moves_[index];
543 Primitive::Type type = move->GetType();
544 Location loc1 = move->GetDestination();
545 Location loc2 = move->GetSource();
546
547 DCHECK(!loc1.IsConstant());
548 DCHECK(!loc2.IsConstant());
549
550 if (loc1.Equals(loc2)) {
551 return;
552 }
553
554 if (loc1.IsRegister() && loc2.IsRegister()) {
555 // Swap 2 GPRs.
556 Register r1 = loc1.AsRegister<Register>();
557 Register r2 = loc2.AsRegister<Register>();
558 __ Move(TMP, r2);
559 __ Move(r2, r1);
560 __ Move(r1, TMP);
561 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
562 FRegister f1 = loc1.AsFpuRegister<FRegister>();
563 FRegister f2 = loc2.AsFpuRegister<FRegister>();
564 if (type == Primitive::kPrimFloat) {
565 __ MovS(FTMP, f2);
566 __ MovS(f2, f1);
567 __ MovS(f1, FTMP);
568 } else {
569 DCHECK_EQ(type, Primitive::kPrimDouble);
570 __ MovD(FTMP, f2);
571 __ MovD(f2, f1);
572 __ MovD(f1, FTMP);
573 }
574 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
575 (loc1.IsFpuRegister() && loc2.IsRegister())) {
576 // Swap FPR and GPR.
577 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
578 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
579 : loc2.AsFpuRegister<FRegister>();
580 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
581 : loc2.AsRegister<Register>();
582 __ Move(TMP, r2);
583 __ Mfc1(r2, f1);
584 __ Mtc1(TMP, f1);
585 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
586 // Swap 2 GPR register pairs.
587 Register r1 = loc1.AsRegisterPairLow<Register>();
588 Register r2 = loc2.AsRegisterPairLow<Register>();
589 __ Move(TMP, r2);
590 __ Move(r2, r1);
591 __ Move(r1, TMP);
592 r1 = loc1.AsRegisterPairHigh<Register>();
593 r2 = loc2.AsRegisterPairHigh<Register>();
594 __ Move(TMP, r2);
595 __ Move(r2, r1);
596 __ Move(r1, TMP);
597 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
598 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
599 // Swap FPR and GPR register pair.
600 DCHECK_EQ(type, Primitive::kPrimDouble);
601 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
602 : loc2.AsFpuRegister<FRegister>();
603 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
604 : loc2.AsRegisterPairLow<Register>();
605 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
606 : loc2.AsRegisterPairHigh<Register>();
607 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
608 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
609 // unpredictable and the following mfch1 will fail.
610 __ Mfc1(TMP, f1);
611 __ Mfhc1(AT, f1);
612 __ Mtc1(r2_l, f1);
613 __ Mthc1(r2_h, f1);
614 __ Move(r2_l, TMP);
615 __ Move(r2_h, AT);
616 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
617 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
618 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
619 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
620 } else {
621 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
622 }
623}
624
625void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
626 __ Pop(static_cast<Register>(reg));
627}
628
629void ParallelMoveResolverMIPS::SpillScratch(int reg) {
630 __ Push(static_cast<Register>(reg));
631}
632
633void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
634 // Allocate a scratch register other than TMP, if available.
635 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
636 // automatically unspilled when the scratch scope object is destroyed).
637 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
638 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
639 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
640 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
641 __ LoadFromOffset(kLoadWord,
642 Register(ensure_scratch.GetRegister()),
643 SP,
644 index1 + stack_offset);
645 __ LoadFromOffset(kLoadWord,
646 TMP,
647 SP,
648 index2 + stack_offset);
649 __ StoreToOffset(kStoreWord,
650 Register(ensure_scratch.GetRegister()),
651 SP,
652 index2 + stack_offset);
653 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
654 }
655}
656
657static dwarf::Reg DWARFReg(Register reg) {
658 return dwarf::Reg::MipsCore(static_cast<int>(reg));
659}
660
661// TODO: mapping of floating-point registers to DWARF.
662
663void CodeGeneratorMIPS::GenerateFrameEntry() {
664 __ Bind(&frame_entry_label_);
665
666 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
667
668 if (do_overflow_check) {
669 __ LoadFromOffset(kLoadWord,
670 ZERO,
671 SP,
672 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
673 RecordPcInfo(nullptr, 0);
674 }
675
676 if (HasEmptyFrame()) {
677 return;
678 }
679
680 // Make sure the frame size isn't unreasonably large.
681 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
682 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
683 }
684
685 // Spill callee-saved registers.
686 // Note that their cumulative size is small and they can be indexed using
687 // 16-bit offsets.
688
689 // TODO: increment/decrement SP in one step instead of two or remove this comment.
690
691 uint32_t ofs = FrameEntrySpillSize();
692 bool unaligned_float = ofs & 0x7;
693 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
694 __ IncreaseFrameSize(ofs);
695
696 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
697 Register reg = kCoreCalleeSaves[i];
698 if (allocated_registers_.ContainsCoreRegister(reg)) {
699 ofs -= kMipsWordSize;
700 __ Sw(reg, SP, ofs);
701 __ cfi().RelOffset(DWARFReg(reg), ofs);
702 }
703 }
704
705 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
706 FRegister reg = kFpuCalleeSaves[i];
707 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
708 ofs -= kMipsDoublewordSize;
709 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
710 if (unaligned_float) {
711 if (fpu_32bit) {
712 __ Swc1(reg, SP, ofs);
713 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
714 } else {
715 __ Mfhc1(TMP, reg);
716 __ Swc1(reg, SP, ofs);
717 __ Sw(TMP, SP, ofs + 4);
718 }
719 } else {
720 __ Sdc1(reg, SP, ofs);
721 }
722 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
723 }
724 }
725
726 // Allocate the rest of the frame and store the current method pointer
727 // at its end.
728
729 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
730
731 static_assert(IsInt<16>(kCurrentMethodStackOffset),
732 "kCurrentMethodStackOffset must fit into int16_t");
733 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
734}
735
736void CodeGeneratorMIPS::GenerateFrameExit() {
737 __ cfi().RememberState();
738
739 if (!HasEmptyFrame()) {
740 // Deallocate the rest of the frame.
741
742 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
743
744 // Restore callee-saved registers.
745 // Note that their cumulative size is small and they can be indexed using
746 // 16-bit offsets.
747
748 // TODO: increment/decrement SP in one step instead of two or remove this comment.
749
750 uint32_t ofs = 0;
751 bool unaligned_float = FrameEntrySpillSize() & 0x7;
752 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
753
754 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
755 FRegister reg = kFpuCalleeSaves[i];
756 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
757 if (unaligned_float) {
758 if (fpu_32bit) {
759 __ Lwc1(reg, SP, ofs);
760 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
761 } else {
762 __ Lwc1(reg, SP, ofs);
763 __ Lw(TMP, SP, ofs + 4);
764 __ Mthc1(TMP, reg);
765 }
766 } else {
767 __ Ldc1(reg, SP, ofs);
768 }
769 ofs += kMipsDoublewordSize;
770 // TODO: __ cfi().Restore(DWARFReg(reg));
771 }
772 }
773
774 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
775 Register reg = kCoreCalleeSaves[i];
776 if (allocated_registers_.ContainsCoreRegister(reg)) {
777 __ Lw(reg, SP, ofs);
778 ofs += kMipsWordSize;
779 __ cfi().Restore(DWARFReg(reg));
780 }
781 }
782
783 DCHECK_EQ(ofs, FrameEntrySpillSize());
784 __ DecreaseFrameSize(ofs);
785 }
786
787 __ Jr(RA);
788 __ Nop();
789
790 __ cfi().RestoreState();
791 __ cfi().DefCFAOffset(GetFrameSize());
792}
793
794void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
795 __ Bind(GetLabelOf(block));
796}
797
798void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
799 if (src.Equals(dst)) {
800 return;
801 }
802
803 if (src.IsConstant()) {
804 MoveConstant(dst, src.GetConstant());
805 } else {
806 if (Primitive::Is64BitType(dst_type)) {
807 Move64(dst, src);
808 } else {
809 Move32(dst, src);
810 }
811 }
812}
813
814void CodeGeneratorMIPS::Move32(Location destination, Location source) {
815 if (source.Equals(destination)) {
816 return;
817 }
818
819 if (destination.IsRegister()) {
820 if (source.IsRegister()) {
821 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
822 } else if (source.IsFpuRegister()) {
823 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
824 } else {
825 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
826 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
827 }
828 } else if (destination.IsFpuRegister()) {
829 if (source.IsRegister()) {
830 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
831 } else if (source.IsFpuRegister()) {
832 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
836 }
837 } else {
838 DCHECK(destination.IsStackSlot()) << destination;
839 if (source.IsRegister()) {
840 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
841 } else if (source.IsFpuRegister()) {
842 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
843 } else {
844 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
845 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
846 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
847 }
848 }
849}
850
851void CodeGeneratorMIPS::Move64(Location destination, Location source) {
852 if (source.Equals(destination)) {
853 return;
854 }
855
856 if (destination.IsRegisterPair()) {
857 if (source.IsRegisterPair()) {
858 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
859 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
860 } else if (source.IsFpuRegister()) {
861 Register dst_high = destination.AsRegisterPairHigh<Register>();
862 Register dst_low = destination.AsRegisterPairLow<Register>();
863 FRegister src = source.AsFpuRegister<FRegister>();
864 __ Mfc1(dst_low, src);
865 __ Mfhc1(dst_high, src);
866 } else {
867 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
868 int32_t off = source.GetStackIndex();
869 Register r = destination.AsRegisterPairLow<Register>();
870 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
871 }
872 } else if (destination.IsFpuRegister()) {
873 if (source.IsRegisterPair()) {
874 FRegister dst = destination.AsFpuRegister<FRegister>();
875 Register src_high = source.AsRegisterPairHigh<Register>();
876 Register src_low = source.AsRegisterPairLow<Register>();
877 __ Mtc1(src_low, dst);
878 __ Mthc1(src_high, dst);
879 } else if (source.IsFpuRegister()) {
880 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
881 } else {
882 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
883 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
884 }
885 } else {
886 DCHECK(destination.IsDoubleStackSlot()) << destination;
887 int32_t off = destination.GetStackIndex();
888 if (source.IsRegisterPair()) {
889 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
890 } else if (source.IsFpuRegister()) {
891 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
892 } else {
893 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
894 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
895 __ StoreToOffset(kStoreWord, TMP, SP, off);
896 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
897 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
898 }
899 }
900}
901
902void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
903 if (c->IsIntConstant() || c->IsNullConstant()) {
904 // Move 32 bit constant.
905 int32_t value = GetInt32ValueOf(c);
906 if (destination.IsRegister()) {
907 Register dst = destination.AsRegister<Register>();
908 __ LoadConst32(dst, value);
909 } else {
910 DCHECK(destination.IsStackSlot())
911 << "Cannot move " << c->DebugName() << " to " << destination;
912 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
913 }
914 } else if (c->IsLongConstant()) {
915 // Move 64 bit constant.
916 int64_t value = GetInt64ValueOf(c);
917 if (destination.IsRegisterPair()) {
918 Register r_h = destination.AsRegisterPairHigh<Register>();
919 Register r_l = destination.AsRegisterPairLow<Register>();
920 __ LoadConst64(r_h, r_l, value);
921 } else {
922 DCHECK(destination.IsDoubleStackSlot())
923 << "Cannot move " << c->DebugName() << " to " << destination;
924 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
925 }
926 } else if (c->IsFloatConstant()) {
927 // Move 32 bit float constant.
928 int32_t value = GetInt32ValueOf(c);
929 if (destination.IsFpuRegister()) {
930 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
931 } else {
932 DCHECK(destination.IsStackSlot())
933 << "Cannot move " << c->DebugName() << " to " << destination;
934 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
935 }
936 } else {
937 // Move 64 bit double constant.
938 DCHECK(c->IsDoubleConstant()) << c->DebugName();
939 int64_t value = GetInt64ValueOf(c);
940 if (destination.IsFpuRegister()) {
941 FRegister fd = destination.AsFpuRegister<FRegister>();
942 __ LoadDConst64(fd, value, TMP);
943 } else {
944 DCHECK(destination.IsDoubleStackSlot())
945 << "Cannot move " << c->DebugName() << " to " << destination;
946 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
947 }
948 }
949}
950
951void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
952 DCHECK(destination.IsRegister());
953 Register dst = destination.AsRegister<Register>();
954 __ LoadConst32(dst, value);
955}
956
957void CodeGeneratorMIPS::Move(HInstruction* instruction,
958 Location location,
959 HInstruction* move_for) {
960 LocationSummary* locations = instruction->GetLocations();
961 Primitive::Type type = instruction->GetType();
962 DCHECK_NE(type, Primitive::kPrimVoid);
963
964 if (instruction->IsCurrentMethod()) {
965 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
966 } else if (locations != nullptr && locations->Out().Equals(location)) {
967 return;
968 } else if (instruction->IsIntConstant()
969 || instruction->IsLongConstant()
970 || instruction->IsNullConstant()) {
971 MoveConstant(location, instruction->AsConstant());
972 } else if (instruction->IsTemporary()) {
973 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
974 if (temp_location.IsStackSlot()) {
975 Move32(location, temp_location);
976 } else {
977 DCHECK(temp_location.IsDoubleStackSlot());
978 Move64(location, temp_location);
979 }
980 } else if (instruction->IsLoadLocal()) {
981 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
982 if (Primitive::Is64BitType(type)) {
983 Move64(location, Location::DoubleStackSlot(stack_slot));
984 } else {
985 Move32(location, Location::StackSlot(stack_slot));
986 }
987 } else {
988 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
989 if (Primitive::Is64BitType(type)) {
990 Move64(location, locations->Out());
991 } else {
992 Move32(location, locations->Out());
993 }
994 }
995}
996
997void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
998 if (location.IsRegister()) {
999 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001000 } else if (location.IsRegisterPair()) {
1001 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1002 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001003 } else {
1004 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1005 }
1006}
1007
1008Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1009 Primitive::Type type = load->GetType();
1010
1011 switch (type) {
1012 case Primitive::kPrimNot:
1013 case Primitive::kPrimInt:
1014 case Primitive::kPrimFloat:
1015 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1016
1017 case Primitive::kPrimLong:
1018 case Primitive::kPrimDouble:
1019 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1020
1021 case Primitive::kPrimBoolean:
1022 case Primitive::kPrimByte:
1023 case Primitive::kPrimChar:
1024 case Primitive::kPrimShort:
1025 case Primitive::kPrimVoid:
1026 LOG(FATAL) << "Unexpected type " << type;
1027 }
1028
1029 LOG(FATAL) << "Unreachable";
1030 return Location::NoLocation();
1031}
1032
1033void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1034 MipsLabel done;
1035 Register card = AT;
1036 Register temp = TMP;
1037 __ Beqz(value, &done);
1038 __ LoadFromOffset(kLoadWord,
1039 card,
1040 TR,
1041 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1042 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1043 __ Addu(temp, card, temp);
1044 __ Sb(card, temp, 0);
1045 __ Bind(&done);
1046}
1047
1048void CodeGeneratorMIPS::SetupBlockedRegisters(bool is_baseline) const {
1049 // Don't allocate the dalvik style register pair passing.
1050 blocked_register_pairs_[A1_A2] = true;
1051
1052 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1053 blocked_core_registers_[ZERO] = true;
1054 blocked_core_registers_[K0] = true;
1055 blocked_core_registers_[K1] = true;
1056 blocked_core_registers_[GP] = true;
1057 blocked_core_registers_[SP] = true;
1058 blocked_core_registers_[RA] = true;
1059
1060 // AT and TMP(T8) are used as temporary/scratch registers
1061 // (similar to how AT is used by MIPS assemblers).
1062 blocked_core_registers_[AT] = true;
1063 blocked_core_registers_[TMP] = true;
1064 blocked_fpu_registers_[FTMP] = true;
1065
1066 // Reserve suspend and thread registers.
1067 blocked_core_registers_[S0] = true;
1068 blocked_core_registers_[TR] = true;
1069
1070 // Reserve T9 for function calls
1071 blocked_core_registers_[T9] = true;
1072
1073 // Reserve odd-numbered FPU registers.
1074 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1075 blocked_fpu_registers_[i] = true;
1076 }
1077
1078 if (is_baseline) {
1079 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1080 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1081 }
1082
1083 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1084 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1085 }
1086 }
1087
1088 UpdateBlockedPairRegisters();
1089}
1090
1091void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1092 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1093 MipsManagedRegister current =
1094 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1095 if (blocked_core_registers_[current.AsRegisterPairLow()]
1096 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1097 blocked_register_pairs_[i] = true;
1098 }
1099 }
1100}
1101
1102Location CodeGeneratorMIPS::AllocateFreeRegister(Primitive::Type type) const {
1103 switch (type) {
1104 case Primitive::kPrimLong: {
1105 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
1106 MipsManagedRegister pair =
1107 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
1108 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
1109 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
1110
1111 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
1112 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
1113 UpdateBlockedPairRegisters();
1114 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
1115 }
1116
1117 case Primitive::kPrimByte:
1118 case Primitive::kPrimBoolean:
1119 case Primitive::kPrimChar:
1120 case Primitive::kPrimShort:
1121 case Primitive::kPrimInt:
1122 case Primitive::kPrimNot: {
1123 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
1124 // Block all register pairs that contain `reg`.
1125 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1126 MipsManagedRegister current =
1127 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1128 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
1129 blocked_register_pairs_[i] = true;
1130 }
1131 }
1132 return Location::RegisterLocation(reg);
1133 }
1134
1135 case Primitive::kPrimFloat:
1136 case Primitive::kPrimDouble: {
1137 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFRegisters);
1138 return Location::FpuRegisterLocation(reg);
1139 }
1140
1141 case Primitive::kPrimVoid:
1142 LOG(FATAL) << "Unreachable type " << type;
1143 }
1144
1145 UNREACHABLE();
1146}
1147
1148size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1150 return kMipsWordSize;
1151}
1152
1153size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1155 return kMipsWordSize;
1156}
1157
1158size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1159 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1160 return kMipsDoublewordSize;
1161}
1162
1163size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1164 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1165 return kMipsDoublewordSize;
1166}
1167
1168void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1169 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1170}
1171
1172void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1173 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1174}
1175
1176void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1177 HInstruction* instruction,
1178 uint32_t dex_pc,
1179 SlowPathCode* slow_path) {
1180 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1181 instruction,
1182 dex_pc,
1183 slow_path,
1184 IsDirectEntrypoint(entrypoint));
1185}
1186
1187constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1188
1189void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1190 HInstruction* instruction,
1191 uint32_t dex_pc,
1192 SlowPathCode* slow_path,
1193 bool is_direct_entrypoint) {
1194 if (is_direct_entrypoint) {
1195 // Reserve argument space on stack (for $a0-$a3) for
1196 // entrypoints that directly reference native implementations.
1197 // Called function may use this space to store $a0-$a3 regs.
1198 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1199 }
1200 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1201 __ Jalr(T9);
1202 __ Nop();
1203 if (is_direct_entrypoint) {
1204 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1205 }
1206 RecordPcInfo(instruction, dex_pc, slow_path);
1207}
1208
1209void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1210 Register class_reg) {
1211 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1212 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1213 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1214 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1215 __ Sync(0);
1216 __ Bind(slow_path->GetExitLabel());
1217}
1218
1219void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1220 __ Sync(0); // Only stype 0 is supported.
1221}
1222
1223void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1224 HBasicBlock* successor) {
1225 SuspendCheckSlowPathMIPS* slow_path =
1226 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1227 codegen_->AddSlowPath(slow_path);
1228
1229 __ LoadFromOffset(kLoadUnsignedHalfword,
1230 TMP,
1231 TR,
1232 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1233 if (successor == nullptr) {
1234 __ Bnez(TMP, slow_path->GetEntryLabel());
1235 __ Bind(slow_path->GetReturnLabel());
1236 } else {
1237 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1238 __ B(slow_path->GetEntryLabel());
1239 // slow_path will return to GetLabelOf(successor).
1240 }
1241}
1242
1243InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1244 CodeGeneratorMIPS* codegen)
1245 : HGraphVisitor(graph),
1246 assembler_(codegen->GetAssembler()),
1247 codegen_(codegen) {}
1248
1249void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1250 DCHECK_EQ(instruction->InputCount(), 2U);
1251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1252 Primitive::Type type = instruction->GetResultType();
1253 switch (type) {
1254 case Primitive::kPrimInt: {
1255 locations->SetInAt(0, Location::RequiresRegister());
1256 HInstruction* right = instruction->InputAt(1);
1257 bool can_use_imm = false;
1258 if (right->IsConstant()) {
1259 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1260 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1261 can_use_imm = IsUint<16>(imm);
1262 } else if (instruction->IsAdd()) {
1263 can_use_imm = IsInt<16>(imm);
1264 } else {
1265 DCHECK(instruction->IsSub());
1266 can_use_imm = IsInt<16>(-imm);
1267 }
1268 }
1269 if (can_use_imm)
1270 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1271 else
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1274 break;
1275 }
1276
1277 case Primitive::kPrimLong: {
1278 // TODO: can 2nd param be const?
1279 locations->SetInAt(0, Location::RequiresRegister());
1280 locations->SetInAt(1, Location::RequiresRegister());
1281 if (instruction->IsAdd() || instruction->IsSub()) {
1282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1283 } else {
1284 DCHECK(instruction->IsAnd() || instruction->IsOr() || instruction->IsXor());
1285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1286 }
1287 break;
1288 }
1289
1290 case Primitive::kPrimFloat:
1291 case Primitive::kPrimDouble:
1292 DCHECK(instruction->IsAdd() || instruction->IsSub());
1293 locations->SetInAt(0, Location::RequiresFpuRegister());
1294 locations->SetInAt(1, Location::RequiresFpuRegister());
1295 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1296 break;
1297
1298 default:
1299 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1300 }
1301}
1302
1303void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1304 Primitive::Type type = instruction->GetType();
1305 LocationSummary* locations = instruction->GetLocations();
1306
1307 switch (type) {
1308 case Primitive::kPrimInt: {
1309 Register dst = locations->Out().AsRegister<Register>();
1310 Register lhs = locations->InAt(0).AsRegister<Register>();
1311 Location rhs_location = locations->InAt(1);
1312
1313 Register rhs_reg = ZERO;
1314 int32_t rhs_imm = 0;
1315 bool use_imm = rhs_location.IsConstant();
1316 if (use_imm) {
1317 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1318 } else {
1319 rhs_reg = rhs_location.AsRegister<Register>();
1320 }
1321
1322 if (instruction->IsAnd()) {
1323 if (use_imm)
1324 __ Andi(dst, lhs, rhs_imm);
1325 else
1326 __ And(dst, lhs, rhs_reg);
1327 } else if (instruction->IsOr()) {
1328 if (use_imm)
1329 __ Ori(dst, lhs, rhs_imm);
1330 else
1331 __ Or(dst, lhs, rhs_reg);
1332 } else if (instruction->IsXor()) {
1333 if (use_imm)
1334 __ Xori(dst, lhs, rhs_imm);
1335 else
1336 __ Xor(dst, lhs, rhs_reg);
1337 } else if (instruction->IsAdd()) {
1338 if (use_imm)
1339 __ Addiu(dst, lhs, rhs_imm);
1340 else
1341 __ Addu(dst, lhs, rhs_reg);
1342 } else {
1343 DCHECK(instruction->IsSub());
1344 if (use_imm)
1345 __ Addiu(dst, lhs, -rhs_imm);
1346 else
1347 __ Subu(dst, lhs, rhs_reg);
1348 }
1349 break;
1350 }
1351
1352 case Primitive::kPrimLong: {
1353 // TODO: can 2nd param be const?
1354 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1355 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1356 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1357 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1358 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
1359 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
1360
1361 if (instruction->IsAnd()) {
1362 __ And(dst_low, lhs_low, rhs_low);
1363 __ And(dst_high, lhs_high, rhs_high);
1364 } else if (instruction->IsOr()) {
1365 __ Or(dst_low, lhs_low, rhs_low);
1366 __ Or(dst_high, lhs_high, rhs_high);
1367 } else if (instruction->IsXor()) {
1368 __ Xor(dst_low, lhs_low, rhs_low);
1369 __ Xor(dst_high, lhs_high, rhs_high);
1370 } else if (instruction->IsAdd()) {
1371 __ Addu(dst_low, lhs_low, rhs_low);
1372 __ Sltu(TMP, dst_low, lhs_low);
1373 __ Addu(dst_high, lhs_high, rhs_high);
1374 __ Addu(dst_high, dst_high, TMP);
1375 } else {
1376 DCHECK(instruction->IsSub());
1377 __ Subu(dst_low, lhs_low, rhs_low);
1378 __ Sltu(TMP, lhs_low, dst_low);
1379 __ Subu(dst_high, lhs_high, rhs_high);
1380 __ Subu(dst_high, dst_high, TMP);
1381 }
1382 break;
1383 }
1384
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1388 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1389 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1390 if (instruction->IsAdd()) {
1391 if (type == Primitive::kPrimFloat) {
1392 __ AddS(dst, lhs, rhs);
1393 } else {
1394 __ AddD(dst, lhs, rhs);
1395 }
1396 } else {
1397 DCHECK(instruction->IsSub());
1398 if (type == Primitive::kPrimFloat) {
1399 __ SubS(dst, lhs, rhs);
1400 } else {
1401 __ SubD(dst, lhs, rhs);
1402 }
1403 }
1404 break;
1405 }
1406
1407 default:
1408 LOG(FATAL) << "Unexpected binary operation type " << type;
1409 }
1410}
1411
1412void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
1413 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1414
1415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1416 Primitive::Type type = instr->GetResultType();
1417 switch (type) {
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimLong: {
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1422 locations->SetOut(Location::RequiresRegister());
1423 break;
1424 }
1425 default:
1426 LOG(FATAL) << "Unexpected shift type " << type;
1427 }
1428}
1429
1430static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1431
1432void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
1433 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1434 LocationSummary* locations = instr->GetLocations();
1435 Primitive::Type type = instr->GetType();
1436
1437 Location rhs_location = locations->InAt(1);
1438 bool use_imm = rhs_location.IsConstant();
1439 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1440 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
1441 uint32_t shift_mask = (type == Primitive::kPrimInt) ? kMaxIntShiftValue : kMaxLongShiftValue;
1442 uint32_t shift_value = rhs_imm & shift_mask;
1443
1444 switch (type) {
1445 case Primitive::kPrimInt: {
1446 Register dst = locations->Out().AsRegister<Register>();
1447 Register lhs = locations->InAt(0).AsRegister<Register>();
1448 if (use_imm) {
1449 if (instr->IsShl()) {
1450 __ Sll(dst, lhs, shift_value);
1451 } else if (instr->IsShr()) {
1452 __ Sra(dst, lhs, shift_value);
1453 } else {
1454 __ Srl(dst, lhs, shift_value);
1455 }
1456 } else {
1457 if (instr->IsShl()) {
1458 __ Sllv(dst, lhs, rhs_reg);
1459 } else if (instr->IsShr()) {
1460 __ Srav(dst, lhs, rhs_reg);
1461 } else {
1462 __ Srlv(dst, lhs, rhs_reg);
1463 }
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
1469 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1470 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1471 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1472 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1473 if (use_imm) {
1474 if (shift_value == 0) {
1475 codegen_->Move64(locations->Out(), locations->InAt(0));
1476 } else if (shift_value < kMipsBitsPerWord) {
1477 if (instr->IsShl()) {
1478 __ Sll(dst_low, lhs_low, shift_value);
1479 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1480 __ Sll(dst_high, lhs_high, shift_value);
1481 __ Or(dst_high, dst_high, TMP);
1482 } else if (instr->IsShr()) {
1483 __ Sra(dst_high, lhs_high, shift_value);
1484 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1485 __ Srl(dst_low, lhs_low, shift_value);
1486 __ Or(dst_low, dst_low, TMP);
1487 } else {
1488 __ Srl(dst_high, lhs_high, shift_value);
1489 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1490 __ Srl(dst_low, lhs_low, shift_value);
1491 __ Or(dst_low, dst_low, TMP);
1492 }
1493 } else {
1494 shift_value -= kMipsBitsPerWord;
1495 if (instr->IsShl()) {
1496 __ Sll(dst_high, lhs_low, shift_value);
1497 __ Move(dst_low, ZERO);
1498 } else if (instr->IsShr()) {
1499 __ Sra(dst_low, lhs_high, shift_value);
1500 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
1501 } else {
1502 __ Srl(dst_low, lhs_high, shift_value);
1503 __ Move(dst_high, ZERO);
1504 }
1505 }
1506 } else {
1507 MipsLabel done;
1508 if (instr->IsShl()) {
1509 __ Sllv(dst_low, lhs_low, rhs_reg);
1510 __ Nor(AT, ZERO, rhs_reg);
1511 __ Srl(TMP, lhs_low, 1);
1512 __ Srlv(TMP, TMP, AT);
1513 __ Sllv(dst_high, lhs_high, rhs_reg);
1514 __ Or(dst_high, dst_high, TMP);
1515 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1516 __ Beqz(TMP, &done);
1517 __ Move(dst_high, dst_low);
1518 __ Move(dst_low, ZERO);
1519 } else if (instr->IsShr()) {
1520 __ Srav(dst_high, lhs_high, rhs_reg);
1521 __ Nor(AT, ZERO, rhs_reg);
1522 __ Sll(TMP, lhs_high, 1);
1523 __ Sllv(TMP, TMP, AT);
1524 __ Srlv(dst_low, lhs_low, rhs_reg);
1525 __ Or(dst_low, dst_low, TMP);
1526 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1527 __ Beqz(TMP, &done);
1528 __ Move(dst_low, dst_high);
1529 __ Sra(dst_high, dst_high, 31);
1530 } else {
1531 __ Srlv(dst_high, lhs_high, rhs_reg);
1532 __ Nor(AT, ZERO, rhs_reg);
1533 __ Sll(TMP, lhs_high, 1);
1534 __ Sllv(TMP, TMP, AT);
1535 __ Srlv(dst_low, lhs_low, rhs_reg);
1536 __ Or(dst_low, dst_low, TMP);
1537 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1538 __ Beqz(TMP, &done);
1539 __ Move(dst_low, dst_high);
1540 __ Move(dst_high, ZERO);
1541 }
1542 __ Bind(&done);
1543 }
1544 break;
1545 }
1546
1547 default:
1548 LOG(FATAL) << "Unexpected shift operation type " << type;
1549 }
1550}
1551
1552void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1553 HandleBinaryOp(instruction);
1554}
1555
1556void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1557 HandleBinaryOp(instruction);
1558}
1559
1560void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1561 HandleBinaryOp(instruction);
1562}
1563
1564void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1565 HandleBinaryOp(instruction);
1566}
1567
1568void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1571 locations->SetInAt(0, Location::RequiresRegister());
1572 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1573 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1575 } else {
1576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1577 }
1578}
1579
1580void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1581 LocationSummary* locations = instruction->GetLocations();
1582 Register obj = locations->InAt(0).AsRegister<Register>();
1583 Location index = locations->InAt(1);
1584 Primitive::Type type = instruction->GetType();
1585
1586 switch (type) {
1587 case Primitive::kPrimBoolean: {
1588 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1589 Register out = locations->Out().AsRegister<Register>();
1590 if (index.IsConstant()) {
1591 size_t offset =
1592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1593 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1594 } else {
1595 __ Addu(TMP, obj, index.AsRegister<Register>());
1596 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1597 }
1598 break;
1599 }
1600
1601 case Primitive::kPrimByte: {
1602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1603 Register out = locations->Out().AsRegister<Register>();
1604 if (index.IsConstant()) {
1605 size_t offset =
1606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1607 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1608 } else {
1609 __ Addu(TMP, obj, index.AsRegister<Register>());
1610 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimShort: {
1616 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1617 Register out = locations->Out().AsRegister<Register>();
1618 if (index.IsConstant()) {
1619 size_t offset =
1620 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1621 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1622 } else {
1623 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1624 __ Addu(TMP, obj, TMP);
1625 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1626 }
1627 break;
1628 }
1629
1630 case Primitive::kPrimChar: {
1631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1632 Register out = locations->Out().AsRegister<Register>();
1633 if (index.IsConstant()) {
1634 size_t offset =
1635 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1636 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1637 } else {
1638 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1639 __ Addu(TMP, obj, TMP);
1640 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1641 }
1642 break;
1643 }
1644
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimNot: {
1647 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1649 Register out = locations->Out().AsRegister<Register>();
1650 if (index.IsConstant()) {
1651 size_t offset =
1652 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1653 __ LoadFromOffset(kLoadWord, out, obj, offset);
1654 } else {
1655 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1656 __ Addu(TMP, obj, TMP);
1657 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1658 }
1659 break;
1660 }
1661
1662 case Primitive::kPrimLong: {
1663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1664 Register out = locations->Out().AsRegisterPairLow<Register>();
1665 if (index.IsConstant()) {
1666 size_t offset =
1667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1668 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1669 } else {
1670 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1671 __ Addu(TMP, obj, TMP);
1672 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1673 }
1674 break;
1675 }
1676
1677 case Primitive::kPrimFloat: {
1678 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1679 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1680 if (index.IsConstant()) {
1681 size_t offset =
1682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1683 __ LoadSFromOffset(out, obj, offset);
1684 } else {
1685 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1686 __ Addu(TMP, obj, TMP);
1687 __ LoadSFromOffset(out, TMP, data_offset);
1688 }
1689 break;
1690 }
1691
1692 case Primitive::kPrimDouble: {
1693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1694 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1695 if (index.IsConstant()) {
1696 size_t offset =
1697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1698 __ LoadDFromOffset(out, obj, offset);
1699 } else {
1700 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1701 __ Addu(TMP, obj, TMP);
1702 __ LoadDFromOffset(out, TMP, data_offset);
1703 }
1704 break;
1705 }
1706
1707 case Primitive::kPrimVoid:
1708 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1709 UNREACHABLE();
1710 }
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
1712}
1713
1714void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1716 locations->SetInAt(0, Location::RequiresRegister());
1717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1718}
1719
1720void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1721 LocationSummary* locations = instruction->GetLocations();
1722 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1723 Register obj = locations->InAt(0).AsRegister<Register>();
1724 Register out = locations->Out().AsRegister<Register>();
1725 __ LoadFromOffset(kLoadWord, out, obj, offset);
1726 codegen_->MaybeRecordImplicitNullCheck(instruction);
1727}
1728
1729void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001730 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1732 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001733 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1734 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001735 InvokeRuntimeCallingConvention calling_convention;
1736 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1737 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1738 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1739 } else {
1740 locations->SetInAt(0, Location::RequiresRegister());
1741 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1742 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1743 locations->SetInAt(2, Location::RequiresFpuRegister());
1744 } else {
1745 locations->SetInAt(2, Location::RequiresRegister());
1746 }
1747 }
1748}
1749
1750void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1751 LocationSummary* locations = instruction->GetLocations();
1752 Register obj = locations->InAt(0).AsRegister<Register>();
1753 Location index = locations->InAt(1);
1754 Primitive::Type value_type = instruction->GetComponentType();
1755 bool needs_runtime_call = locations->WillCall();
1756 bool needs_write_barrier =
1757 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1758
1759 switch (value_type) {
1760 case Primitive::kPrimBoolean:
1761 case Primitive::kPrimByte: {
1762 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1763 Register value = locations->InAt(2).AsRegister<Register>();
1764 if (index.IsConstant()) {
1765 size_t offset =
1766 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1767 __ StoreToOffset(kStoreByte, value, obj, offset);
1768 } else {
1769 __ Addu(TMP, obj, index.AsRegister<Register>());
1770 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1771 }
1772 break;
1773 }
1774
1775 case Primitive::kPrimShort:
1776 case Primitive::kPrimChar: {
1777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1778 Register value = locations->InAt(2).AsRegister<Register>();
1779 if (index.IsConstant()) {
1780 size_t offset =
1781 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1782 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1783 } else {
1784 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1785 __ Addu(TMP, obj, TMP);
1786 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1787 }
1788 break;
1789 }
1790
1791 case Primitive::kPrimInt:
1792 case Primitive::kPrimNot: {
1793 if (!needs_runtime_call) {
1794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1795 Register value = locations->InAt(2).AsRegister<Register>();
1796 if (index.IsConstant()) {
1797 size_t offset =
1798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1799 __ StoreToOffset(kStoreWord, value, obj, offset);
1800 } else {
1801 DCHECK(index.IsRegister()) << index;
1802 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1803 __ Addu(TMP, obj, TMP);
1804 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1805 }
1806 codegen_->MaybeRecordImplicitNullCheck(instruction);
1807 if (needs_write_barrier) {
1808 DCHECK_EQ(value_type, Primitive::kPrimNot);
1809 codegen_->MarkGCCard(obj, value);
1810 }
1811 } else {
1812 DCHECK_EQ(value_type, Primitive::kPrimNot);
1813 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1814 instruction,
1815 instruction->GetDexPc(),
1816 nullptr,
1817 IsDirectEntrypoint(kQuickAputObject));
1818 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1819 }
1820 break;
1821 }
1822
1823 case Primitive::kPrimLong: {
1824 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1825 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1826 if (index.IsConstant()) {
1827 size_t offset =
1828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1829 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1830 } else {
1831 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1832 __ Addu(TMP, obj, TMP);
1833 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimFloat: {
1839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1840 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1841 DCHECK(locations->InAt(2).IsFpuRegister());
1842 if (index.IsConstant()) {
1843 size_t offset =
1844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1845 __ StoreSToOffset(value, obj, offset);
1846 } else {
1847 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1848 __ Addu(TMP, obj, TMP);
1849 __ StoreSToOffset(value, TMP, data_offset);
1850 }
1851 break;
1852 }
1853
1854 case Primitive::kPrimDouble: {
1855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1856 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1857 DCHECK(locations->InAt(2).IsFpuRegister());
1858 if (index.IsConstant()) {
1859 size_t offset =
1860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1861 __ StoreDToOffset(value, obj, offset);
1862 } else {
1863 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1864 __ Addu(TMP, obj, TMP);
1865 __ StoreDToOffset(value, TMP, data_offset);
1866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimVoid:
1871 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1872 UNREACHABLE();
1873 }
1874
1875 // Ints and objects are handled in the switch.
1876 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1877 codegen_->MaybeRecordImplicitNullCheck(instruction);
1878 }
1879}
1880
1881void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1882 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1883 ? LocationSummary::kCallOnSlowPath
1884 : LocationSummary::kNoCall;
1885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1886 locations->SetInAt(0, Location::RequiresRegister());
1887 locations->SetInAt(1, Location::RequiresRegister());
1888 if (instruction->HasUses()) {
1889 locations->SetOut(Location::SameAsFirstInput());
1890 }
1891}
1892
1893void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1894 LocationSummary* locations = instruction->GetLocations();
1895 BoundsCheckSlowPathMIPS* slow_path =
1896 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1897 codegen_->AddSlowPath(slow_path);
1898
1899 Register index = locations->InAt(0).AsRegister<Register>();
1900 Register length = locations->InAt(1).AsRegister<Register>();
1901
1902 // length is limited by the maximum positive signed 32-bit integer.
1903 // Unsigned comparison of length and index checks for index < 0
1904 // and for length <= index simultaneously.
1905 __ Bgeu(index, length, slow_path->GetEntryLabel());
1906}
1907
1908void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1910 instruction,
1911 LocationSummary::kCallOnSlowPath);
1912 locations->SetInAt(0, Location::RequiresRegister());
1913 locations->SetInAt(1, Location::RequiresRegister());
1914 // Note that TypeCheckSlowPathMIPS uses this register too.
1915 locations->AddTemp(Location::RequiresRegister());
1916}
1917
1918void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
1919 LocationSummary* locations = instruction->GetLocations();
1920 Register obj = locations->InAt(0).AsRegister<Register>();
1921 Register cls = locations->InAt(1).AsRegister<Register>();
1922 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
1923
1924 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
1925 codegen_->AddSlowPath(slow_path);
1926
1927 // TODO: avoid this check if we know obj is not null.
1928 __ Beqz(obj, slow_path->GetExitLabel());
1929 // Compare the class of `obj` with `cls`.
1930 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1931 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
1932 __ Bind(slow_path->GetExitLabel());
1933}
1934
1935void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1938 locations->SetInAt(0, Location::RequiresRegister());
1939 if (check->HasUses()) {
1940 locations->SetOut(Location::SameAsFirstInput());
1941 }
1942}
1943
1944void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
1945 // We assume the class is not null.
1946 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
1947 check->GetLoadClass(),
1948 check,
1949 check->GetDexPc(),
1950 true);
1951 codegen_->AddSlowPath(slow_path);
1952 GenerateClassInitializationCheck(slow_path,
1953 check->GetLocations()->InAt(0).AsRegister<Register>());
1954}
1955
1956void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
1957 Primitive::Type in_type = compare->InputAt(0)->GetType();
1958
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001959 LocationSummary* locations =
1960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961
1962 switch (in_type) {
1963 case Primitive::kPrimLong:
1964 locations->SetInAt(0, Location::RequiresRegister());
1965 locations->SetInAt(1, Location::RequiresRegister());
1966 // Output overlaps because it is written before doing the low comparison.
1967 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1968 break;
1969
1970 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001971 case Primitive::kPrimDouble:
1972 locations->SetInAt(0, Location::RequiresFpuRegister());
1973 locations->SetInAt(1, Location::RequiresFpuRegister());
1974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001976
1977 default:
1978 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1979 }
1980}
1981
1982void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
1983 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001984 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08001986 bool gt_bias = instruction->IsGtBias();
1987 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988
1989 // 0 if: left == right
1990 // 1 if: left > right
1991 // -1 if: left < right
1992 switch (in_type) {
1993 case Primitive::kPrimLong: {
1994 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001995 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1996 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1997 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
1998 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
1999 // TODO: more efficient (direct) comparison with a constant.
2000 __ Slt(TMP, lhs_high, rhs_high);
2001 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2002 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2003 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2004 __ Sltu(TMP, lhs_low, rhs_low);
2005 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2006 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2007 __ Bind(&done);
2008 break;
2009 }
2010
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002011 case Primitive::kPrimFloat: {
2012 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2013 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2014 MipsLabel done;
2015 if (isR6) {
2016 __ CmpEqS(FTMP, lhs, rhs);
2017 __ LoadConst32(res, 0);
2018 __ Bc1nez(FTMP, &done);
2019 if (gt_bias) {
2020 __ CmpLtS(FTMP, lhs, rhs);
2021 __ LoadConst32(res, -1);
2022 __ Bc1nez(FTMP, &done);
2023 __ LoadConst32(res, 1);
2024 } else {
2025 __ CmpLtS(FTMP, rhs, lhs);
2026 __ LoadConst32(res, 1);
2027 __ Bc1nez(FTMP, &done);
2028 __ LoadConst32(res, -1);
2029 }
2030 } else {
2031 if (gt_bias) {
2032 __ ColtS(0, lhs, rhs);
2033 __ LoadConst32(res, -1);
2034 __ Bc1t(0, &done);
2035 __ CeqS(0, lhs, rhs);
2036 __ LoadConst32(res, 1);
2037 __ Movt(res, ZERO, 0);
2038 } else {
2039 __ ColtS(0, rhs, lhs);
2040 __ LoadConst32(res, 1);
2041 __ Bc1t(0, &done);
2042 __ CeqS(0, lhs, rhs);
2043 __ LoadConst32(res, -1);
2044 __ Movt(res, ZERO, 0);
2045 }
2046 }
2047 __ Bind(&done);
2048 break;
2049 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 case Primitive::kPrimDouble: {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002051 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2052 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2053 MipsLabel done;
2054 if (isR6) {
2055 __ CmpEqD(FTMP, lhs, rhs);
2056 __ LoadConst32(res, 0);
2057 __ Bc1nez(FTMP, &done);
2058 if (gt_bias) {
2059 __ CmpLtD(FTMP, lhs, rhs);
2060 __ LoadConst32(res, -1);
2061 __ Bc1nez(FTMP, &done);
2062 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002064 __ CmpLtD(FTMP, rhs, lhs);
2065 __ LoadConst32(res, 1);
2066 __ Bc1nez(FTMP, &done);
2067 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002068 }
2069 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002070 if (gt_bias) {
2071 __ ColtD(0, lhs, rhs);
2072 __ LoadConst32(res, -1);
2073 __ Bc1t(0, &done);
2074 __ CeqD(0, lhs, rhs);
2075 __ LoadConst32(res, 1);
2076 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002077 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002078 __ ColtD(0, rhs, lhs);
2079 __ LoadConst32(res, 1);
2080 __ Bc1t(0, &done);
2081 __ CeqD(0, lhs, rhs);
2082 __ LoadConst32(res, -1);
2083 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 }
2085 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002086 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 break;
2088 }
2089
2090 default:
2091 LOG(FATAL) << "Unimplemented compare type " << in_type;
2092 }
2093}
2094
2095void LocationsBuilderMIPS::VisitCondition(HCondition* instruction) {
2096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002097 switch (instruction->InputAt(0)->GetType()) {
2098 default:
2099 case Primitive::kPrimLong:
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2102 break;
2103
2104 case Primitive::kPrimFloat:
2105 case Primitive::kPrimDouble:
2106 locations->SetInAt(0, Location::RequiresFpuRegister());
2107 locations->SetInAt(1, Location::RequiresFpuRegister());
2108 break;
2109 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002110 if (instruction->NeedsMaterialization()) {
2111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2112 }
2113}
2114
2115void InstructionCodeGeneratorMIPS::VisitCondition(HCondition* instruction) {
2116 if (!instruction->NeedsMaterialization()) {
2117 return;
2118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002119
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002120 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121 LocationSummary* locations = instruction->GetLocations();
2122 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002123 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002125 switch (type) {
2126 default:
2127 // Integer case.
2128 GenerateIntCompare(instruction->GetCondition(), locations);
2129 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002131 case Primitive::kPrimLong:
2132 // TODO: don't use branches.
2133 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002134 break;
2135
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002136 case Primitive::kPrimFloat:
2137 case Primitive::kPrimDouble:
2138 // TODO: don't use branches.
2139 GenerateFpCompareAndBranch(instruction->GetCondition(),
2140 instruction->IsGtBias(),
2141 type,
2142 locations,
2143 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144 break;
2145 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002146
2147 // Convert the branches into the result.
2148 MipsLabel done;
2149
2150 // False case: result = 0.
2151 __ LoadConst32(dst, 0);
2152 __ B(&done);
2153
2154 // True case: result = 1.
2155 __ Bind(&true_label);
2156 __ LoadConst32(dst, 1);
2157 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002158}
2159
Alexey Frunze7e99e052015-11-24 19:28:01 -08002160void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2161 DCHECK(instruction->IsDiv() || instruction->IsRem());
2162 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2163
2164 LocationSummary* locations = instruction->GetLocations();
2165 Location second = locations->InAt(1);
2166 DCHECK(second.IsConstant());
2167
2168 Register out = locations->Out().AsRegister<Register>();
2169 Register dividend = locations->InAt(0).AsRegister<Register>();
2170 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2171 DCHECK(imm == 1 || imm == -1);
2172
2173 if (instruction->IsRem()) {
2174 __ Move(out, ZERO);
2175 } else {
2176 if (imm == -1) {
2177 __ Subu(out, ZERO, dividend);
2178 } else if (out != dividend) {
2179 __ Move(out, dividend);
2180 }
2181 }
2182}
2183
2184void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2185 DCHECK(instruction->IsDiv() || instruction->IsRem());
2186 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2187
2188 LocationSummary* locations = instruction->GetLocations();
2189 Location second = locations->InAt(1);
2190 DCHECK(second.IsConstant());
2191
2192 Register out = locations->Out().AsRegister<Register>();
2193 Register dividend = locations->InAt(0).AsRegister<Register>();
2194 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2195 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm));
2196 DCHECK(IsPowerOfTwo(abs_imm));
2197 int ctz_imm = CTZ(abs_imm);
2198
2199 if (instruction->IsDiv()) {
2200 if (ctz_imm == 1) {
2201 // Fast path for division by +/-2, which is very common.
2202 __ Srl(TMP, dividend, 31);
2203 } else {
2204 __ Sra(TMP, dividend, 31);
2205 __ Srl(TMP, TMP, 32 - ctz_imm);
2206 }
2207 __ Addu(out, dividend, TMP);
2208 __ Sra(out, out, ctz_imm);
2209 if (imm < 0) {
2210 __ Subu(out, ZERO, out);
2211 }
2212 } else {
2213 if (ctz_imm == 1) {
2214 // Fast path for modulo +/-2, which is very common.
2215 __ Sra(TMP, dividend, 31);
2216 __ Subu(out, dividend, TMP);
2217 __ Andi(out, out, 1);
2218 __ Addu(out, out, TMP);
2219 } else {
2220 __ Sra(TMP, dividend, 31);
2221 __ Srl(TMP, TMP, 32 - ctz_imm);
2222 __ Addu(out, dividend, TMP);
2223 if (IsUint<16>(abs_imm - 1)) {
2224 __ Andi(out, out, abs_imm - 1);
2225 } else {
2226 __ Sll(out, out, 32 - ctz_imm);
2227 __ Srl(out, out, 32 - ctz_imm);
2228 }
2229 __ Subu(out, out, TMP);
2230 }
2231 }
2232}
2233
2234void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2235 DCHECK(instruction->IsDiv() || instruction->IsRem());
2236 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2237
2238 LocationSummary* locations = instruction->GetLocations();
2239 Location second = locations->InAt(1);
2240 DCHECK(second.IsConstant());
2241
2242 Register out = locations->Out().AsRegister<Register>();
2243 Register dividend = locations->InAt(0).AsRegister<Register>();
2244 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2245
2246 int64_t magic;
2247 int shift;
2248 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2249
2250 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2251
2252 __ LoadConst32(TMP, magic);
2253 if (isR6) {
2254 __ MuhR6(TMP, dividend, TMP);
2255 } else {
2256 __ MultR2(dividend, TMP);
2257 __ Mfhi(TMP);
2258 }
2259 if (imm > 0 && magic < 0) {
2260 __ Addu(TMP, TMP, dividend);
2261 } else if (imm < 0 && magic > 0) {
2262 __ Subu(TMP, TMP, dividend);
2263 }
2264
2265 if (shift != 0) {
2266 __ Sra(TMP, TMP, shift);
2267 }
2268
2269 if (instruction->IsDiv()) {
2270 __ Sra(out, TMP, 31);
2271 __ Subu(out, TMP, out);
2272 } else {
2273 __ Sra(AT, TMP, 31);
2274 __ Subu(AT, TMP, AT);
2275 __ LoadConst32(TMP, imm);
2276 if (isR6) {
2277 __ MulR6(TMP, AT, TMP);
2278 } else {
2279 __ MulR2(TMP, AT, TMP);
2280 }
2281 __ Subu(out, dividend, TMP);
2282 }
2283}
2284
2285void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2286 DCHECK(instruction->IsDiv() || instruction->IsRem());
2287 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2288
2289 LocationSummary* locations = instruction->GetLocations();
2290 Register out = locations->Out().AsRegister<Register>();
2291 Location second = locations->InAt(1);
2292
2293 if (second.IsConstant()) {
2294 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2295 if (imm == 0) {
2296 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2297 } else if (imm == 1 || imm == -1) {
2298 DivRemOneOrMinusOne(instruction);
2299 } else if (IsPowerOfTwo(std::abs(imm))) {
2300 DivRemByPowerOfTwo(instruction);
2301 } else {
2302 DCHECK(imm <= -2 || imm >= 2);
2303 GenerateDivRemWithAnyConstant(instruction);
2304 }
2305 } else {
2306 Register dividend = locations->InAt(0).AsRegister<Register>();
2307 Register divisor = second.AsRegister<Register>();
2308 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2309 if (instruction->IsDiv()) {
2310 if (isR6) {
2311 __ DivR6(out, dividend, divisor);
2312 } else {
2313 __ DivR2(out, dividend, divisor);
2314 }
2315 } else {
2316 if (isR6) {
2317 __ ModR6(out, dividend, divisor);
2318 } else {
2319 __ ModR2(out, dividend, divisor);
2320 }
2321 }
2322 }
2323}
2324
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2326 Primitive::Type type = div->GetResultType();
2327 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2328 ? LocationSummary::kCall
2329 : LocationSummary::kNoCall;
2330
2331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2332
2333 switch (type) {
2334 case Primitive::kPrimInt:
2335 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002336 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2338 break;
2339
2340 case Primitive::kPrimLong: {
2341 InvokeRuntimeCallingConvention calling_convention;
2342 locations->SetInAt(0, Location::RegisterPairLocation(
2343 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2344 locations->SetInAt(1, Location::RegisterPairLocation(
2345 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2346 locations->SetOut(calling_convention.GetReturnLocation(type));
2347 break;
2348 }
2349
2350 case Primitive::kPrimFloat:
2351 case Primitive::kPrimDouble:
2352 locations->SetInAt(0, Location::RequiresFpuRegister());
2353 locations->SetInAt(1, Location::RequiresFpuRegister());
2354 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected div type " << type;
2359 }
2360}
2361
2362void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2363 Primitive::Type type = instruction->GetType();
2364 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365
2366 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002367 case Primitive::kPrimInt:
2368 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002369 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 case Primitive::kPrimLong: {
2371 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2372 instruction,
2373 instruction->GetDexPc(),
2374 nullptr,
2375 IsDirectEntrypoint(kQuickLdiv));
2376 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2377 break;
2378 }
2379 case Primitive::kPrimFloat:
2380 case Primitive::kPrimDouble: {
2381 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2382 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2383 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2384 if (type == Primitive::kPrimFloat) {
2385 __ DivS(dst, lhs, rhs);
2386 } else {
2387 __ DivD(dst, lhs, rhs);
2388 }
2389 break;
2390 }
2391 default:
2392 LOG(FATAL) << "Unexpected div type " << type;
2393 }
2394}
2395
2396void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2397 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2398 ? LocationSummary::kCallOnSlowPath
2399 : LocationSummary::kNoCall;
2400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2401 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2402 if (instruction->HasUses()) {
2403 locations->SetOut(Location::SameAsFirstInput());
2404 }
2405}
2406
2407void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2408 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2409 codegen_->AddSlowPath(slow_path);
2410 Location value = instruction->GetLocations()->InAt(0);
2411 Primitive::Type type = instruction->GetType();
2412
2413 switch (type) {
2414 case Primitive::kPrimByte:
2415 case Primitive::kPrimChar:
2416 case Primitive::kPrimShort:
2417 case Primitive::kPrimInt: {
2418 if (value.IsConstant()) {
2419 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2420 __ B(slow_path->GetEntryLabel());
2421 } else {
2422 // A division by a non-null constant is valid. We don't need to perform
2423 // any check, so simply fall through.
2424 }
2425 } else {
2426 DCHECK(value.IsRegister()) << value;
2427 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2428 }
2429 break;
2430 }
2431 case Primitive::kPrimLong: {
2432 if (value.IsConstant()) {
2433 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2434 __ B(slow_path->GetEntryLabel());
2435 } else {
2436 // A division by a non-null constant is valid. We don't need to perform
2437 // any check, so simply fall through.
2438 }
2439 } else {
2440 DCHECK(value.IsRegisterPair()) << value;
2441 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2442 __ Beqz(TMP, slow_path->GetEntryLabel());
2443 }
2444 break;
2445 }
2446 default:
2447 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2448 }
2449}
2450
2451void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2452 LocationSummary* locations =
2453 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2454 locations->SetOut(Location::ConstantLocation(constant));
2455}
2456
2457void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2458 // Will be generated at use site.
2459}
2460
2461void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2462 exit->SetLocations(nullptr);
2463}
2464
2465void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2466}
2467
2468void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2469 LocationSummary* locations =
2470 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2471 locations->SetOut(Location::ConstantLocation(constant));
2472}
2473
2474void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2475 // Will be generated at use site.
2476}
2477
2478void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2479 got->SetLocations(nullptr);
2480}
2481
2482void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2483 DCHECK(!successor->IsExitBlock());
2484 HBasicBlock* block = got->GetBlock();
2485 HInstruction* previous = got->GetPrevious();
2486 HLoopInformation* info = block->GetLoopInformation();
2487
2488 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2489 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2490 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2491 return;
2492 }
2493 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2494 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2495 }
2496 if (!codegen_->GoesToNextBlock(block, successor)) {
2497 __ B(codegen_->GetLabelOf(successor));
2498 }
2499}
2500
2501void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2502 HandleGoto(got, got->GetSuccessor());
2503}
2504
2505void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2506 try_boundary->SetLocations(nullptr);
2507}
2508
2509void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2510 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2511 if (!successor->IsExitBlock()) {
2512 HandleGoto(try_boundary, successor);
2513 }
2514}
2515
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002516void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2517 LocationSummary* locations) {
2518 Register dst = locations->Out().AsRegister<Register>();
2519 Register lhs = locations->InAt(0).AsRegister<Register>();
2520 Location rhs_location = locations->InAt(1);
2521 Register rhs_reg = ZERO;
2522 int64_t rhs_imm = 0;
2523 bool use_imm = rhs_location.IsConstant();
2524 if (use_imm) {
2525 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2526 } else {
2527 rhs_reg = rhs_location.AsRegister<Register>();
2528 }
2529
2530 switch (cond) {
2531 case kCondEQ:
2532 case kCondNE:
2533 if (use_imm && IsUint<16>(rhs_imm)) {
2534 __ Xori(dst, lhs, rhs_imm);
2535 } else {
2536 if (use_imm) {
2537 rhs_reg = TMP;
2538 __ LoadConst32(rhs_reg, rhs_imm);
2539 }
2540 __ Xor(dst, lhs, rhs_reg);
2541 }
2542 if (cond == kCondEQ) {
2543 __ Sltiu(dst, dst, 1);
2544 } else {
2545 __ Sltu(dst, ZERO, dst);
2546 }
2547 break;
2548
2549 case kCondLT:
2550 case kCondGE:
2551 if (use_imm && IsInt<16>(rhs_imm)) {
2552 __ Slti(dst, lhs, rhs_imm);
2553 } else {
2554 if (use_imm) {
2555 rhs_reg = TMP;
2556 __ LoadConst32(rhs_reg, rhs_imm);
2557 }
2558 __ Slt(dst, lhs, rhs_reg);
2559 }
2560 if (cond == kCondGE) {
2561 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2562 // only the slt instruction but no sge.
2563 __ Xori(dst, dst, 1);
2564 }
2565 break;
2566
2567 case kCondLE:
2568 case kCondGT:
2569 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2570 // Simulate lhs <= rhs via lhs < rhs + 1.
2571 __ Slti(dst, lhs, rhs_imm + 1);
2572 if (cond == kCondGT) {
2573 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2574 // only the slti instruction but no sgti.
2575 __ Xori(dst, dst, 1);
2576 }
2577 } else {
2578 if (use_imm) {
2579 rhs_reg = TMP;
2580 __ LoadConst32(rhs_reg, rhs_imm);
2581 }
2582 __ Slt(dst, rhs_reg, lhs);
2583 if (cond == kCondLE) {
2584 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2585 // only the slt instruction but no sle.
2586 __ Xori(dst, dst, 1);
2587 }
2588 }
2589 break;
2590
2591 case kCondB:
2592 case kCondAE:
2593 if (use_imm && IsInt<16>(rhs_imm)) {
2594 // Sltiu sign-extends its 16-bit immediate operand before
2595 // the comparison and thus lets us compare directly with
2596 // unsigned values in the ranges [0, 0x7fff] and
2597 // [0xffff8000, 0xffffffff].
2598 __ Sltiu(dst, lhs, rhs_imm);
2599 } else {
2600 if (use_imm) {
2601 rhs_reg = TMP;
2602 __ LoadConst32(rhs_reg, rhs_imm);
2603 }
2604 __ Sltu(dst, lhs, rhs_reg);
2605 }
2606 if (cond == kCondAE) {
2607 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2608 // only the sltu instruction but no sgeu.
2609 __ Xori(dst, dst, 1);
2610 }
2611 break;
2612
2613 case kCondBE:
2614 case kCondA:
2615 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2616 // Simulate lhs <= rhs via lhs < rhs + 1.
2617 // Note that this only works if rhs + 1 does not overflow
2618 // to 0, hence the check above.
2619 // Sltiu sign-extends its 16-bit immediate operand before
2620 // the comparison and thus lets us compare directly with
2621 // unsigned values in the ranges [0, 0x7fff] and
2622 // [0xffff8000, 0xffffffff].
2623 __ Sltiu(dst, lhs, rhs_imm + 1);
2624 if (cond == kCondA) {
2625 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2626 // only the sltiu instruction but no sgtiu.
2627 __ Xori(dst, dst, 1);
2628 }
2629 } else {
2630 if (use_imm) {
2631 rhs_reg = TMP;
2632 __ LoadConst32(rhs_reg, rhs_imm);
2633 }
2634 __ Sltu(dst, rhs_reg, lhs);
2635 if (cond == kCondBE) {
2636 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2637 // only the sltu instruction but no sleu.
2638 __ Xori(dst, dst, 1);
2639 }
2640 }
2641 break;
2642 }
2643}
2644
2645void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2646 LocationSummary* locations,
2647 MipsLabel* label) {
2648 Register lhs = locations->InAt(0).AsRegister<Register>();
2649 Location rhs_location = locations->InAt(1);
2650 Register rhs_reg = ZERO;
2651 int32_t rhs_imm = 0;
2652 bool use_imm = rhs_location.IsConstant();
2653 if (use_imm) {
2654 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2655 } else {
2656 rhs_reg = rhs_location.AsRegister<Register>();
2657 }
2658
2659 if (use_imm && rhs_imm == 0) {
2660 switch (cond) {
2661 case kCondEQ:
2662 case kCondBE: // <= 0 if zero
2663 __ Beqz(lhs, label);
2664 break;
2665 case kCondNE:
2666 case kCondA: // > 0 if non-zero
2667 __ Bnez(lhs, label);
2668 break;
2669 case kCondLT:
2670 __ Bltz(lhs, label);
2671 break;
2672 case kCondGE:
2673 __ Bgez(lhs, label);
2674 break;
2675 case kCondLE:
2676 __ Blez(lhs, label);
2677 break;
2678 case kCondGT:
2679 __ Bgtz(lhs, label);
2680 break;
2681 case kCondB: // always false
2682 break;
2683 case kCondAE: // always true
2684 __ B(label);
2685 break;
2686 }
2687 } else {
2688 if (use_imm) {
2689 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2690 rhs_reg = TMP;
2691 __ LoadConst32(rhs_reg, rhs_imm);
2692 }
2693 switch (cond) {
2694 case kCondEQ:
2695 __ Beq(lhs, rhs_reg, label);
2696 break;
2697 case kCondNE:
2698 __ Bne(lhs, rhs_reg, label);
2699 break;
2700 case kCondLT:
2701 __ Blt(lhs, rhs_reg, label);
2702 break;
2703 case kCondGE:
2704 __ Bge(lhs, rhs_reg, label);
2705 break;
2706 case kCondLE:
2707 __ Bge(rhs_reg, lhs, label);
2708 break;
2709 case kCondGT:
2710 __ Blt(rhs_reg, lhs, label);
2711 break;
2712 case kCondB:
2713 __ Bltu(lhs, rhs_reg, label);
2714 break;
2715 case kCondAE:
2716 __ Bgeu(lhs, rhs_reg, label);
2717 break;
2718 case kCondBE:
2719 __ Bgeu(rhs_reg, lhs, label);
2720 break;
2721 case kCondA:
2722 __ Bltu(rhs_reg, lhs, label);
2723 break;
2724 }
2725 }
2726}
2727
2728void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2729 LocationSummary* locations,
2730 MipsLabel* label) {
2731 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2732 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2733 Location rhs_location = locations->InAt(1);
2734 Register rhs_high = ZERO;
2735 Register rhs_low = ZERO;
2736 int64_t imm = 0;
2737 uint32_t imm_high = 0;
2738 uint32_t imm_low = 0;
2739 bool use_imm = rhs_location.IsConstant();
2740 if (use_imm) {
2741 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2742 imm_high = High32Bits(imm);
2743 imm_low = Low32Bits(imm);
2744 } else {
2745 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2746 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2747 }
2748
2749 if (use_imm && imm == 0) {
2750 switch (cond) {
2751 case kCondEQ:
2752 case kCondBE: // <= 0 if zero
2753 __ Or(TMP, lhs_high, lhs_low);
2754 __ Beqz(TMP, label);
2755 break;
2756 case kCondNE:
2757 case kCondA: // > 0 if non-zero
2758 __ Or(TMP, lhs_high, lhs_low);
2759 __ Bnez(TMP, label);
2760 break;
2761 case kCondLT:
2762 __ Bltz(lhs_high, label);
2763 break;
2764 case kCondGE:
2765 __ Bgez(lhs_high, label);
2766 break;
2767 case kCondLE:
2768 __ Or(TMP, lhs_high, lhs_low);
2769 __ Sra(AT, lhs_high, 31);
2770 __ Bgeu(AT, TMP, label);
2771 break;
2772 case kCondGT:
2773 __ Or(TMP, lhs_high, lhs_low);
2774 __ Sra(AT, lhs_high, 31);
2775 __ Bltu(AT, TMP, label);
2776 break;
2777 case kCondB: // always false
2778 break;
2779 case kCondAE: // always true
2780 __ B(label);
2781 break;
2782 }
2783 } else if (use_imm) {
2784 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2785 switch (cond) {
2786 case kCondEQ:
2787 __ LoadConst32(TMP, imm_high);
2788 __ Xor(TMP, TMP, lhs_high);
2789 __ LoadConst32(AT, imm_low);
2790 __ Xor(AT, AT, lhs_low);
2791 __ Or(TMP, TMP, AT);
2792 __ Beqz(TMP, label);
2793 break;
2794 case kCondNE:
2795 __ LoadConst32(TMP, imm_high);
2796 __ Xor(TMP, TMP, lhs_high);
2797 __ LoadConst32(AT, imm_low);
2798 __ Xor(AT, AT, lhs_low);
2799 __ Or(TMP, TMP, AT);
2800 __ Bnez(TMP, label);
2801 break;
2802 case kCondLT:
2803 __ LoadConst32(TMP, imm_high);
2804 __ Blt(lhs_high, TMP, label);
2805 __ Slt(TMP, TMP, lhs_high);
2806 __ LoadConst32(AT, imm_low);
2807 __ Sltu(AT, lhs_low, AT);
2808 __ Blt(TMP, AT, label);
2809 break;
2810 case kCondGE:
2811 __ LoadConst32(TMP, imm_high);
2812 __ Blt(TMP, lhs_high, label);
2813 __ Slt(TMP, lhs_high, TMP);
2814 __ LoadConst32(AT, imm_low);
2815 __ Sltu(AT, lhs_low, AT);
2816 __ Or(TMP, TMP, AT);
2817 __ Beqz(TMP, label);
2818 break;
2819 case kCondLE:
2820 __ LoadConst32(TMP, imm_high);
2821 __ Blt(lhs_high, TMP, label);
2822 __ Slt(TMP, TMP, lhs_high);
2823 __ LoadConst32(AT, imm_low);
2824 __ Sltu(AT, AT, lhs_low);
2825 __ Or(TMP, TMP, AT);
2826 __ Beqz(TMP, label);
2827 break;
2828 case kCondGT:
2829 __ LoadConst32(TMP, imm_high);
2830 __ Blt(TMP, lhs_high, label);
2831 __ Slt(TMP, lhs_high, TMP);
2832 __ LoadConst32(AT, imm_low);
2833 __ Sltu(AT, AT, lhs_low);
2834 __ Blt(TMP, AT, label);
2835 break;
2836 case kCondB:
2837 __ LoadConst32(TMP, imm_high);
2838 __ Bltu(lhs_high, TMP, label);
2839 __ Sltu(TMP, TMP, lhs_high);
2840 __ LoadConst32(AT, imm_low);
2841 __ Sltu(AT, lhs_low, AT);
2842 __ Blt(TMP, AT, label);
2843 break;
2844 case kCondAE:
2845 __ LoadConst32(TMP, imm_high);
2846 __ Bltu(TMP, lhs_high, label);
2847 __ Sltu(TMP, lhs_high, TMP);
2848 __ LoadConst32(AT, imm_low);
2849 __ Sltu(AT, lhs_low, AT);
2850 __ Or(TMP, TMP, AT);
2851 __ Beqz(TMP, label);
2852 break;
2853 case kCondBE:
2854 __ LoadConst32(TMP, imm_high);
2855 __ Bltu(lhs_high, TMP, label);
2856 __ Sltu(TMP, TMP, lhs_high);
2857 __ LoadConst32(AT, imm_low);
2858 __ Sltu(AT, AT, lhs_low);
2859 __ Or(TMP, TMP, AT);
2860 __ Beqz(TMP, label);
2861 break;
2862 case kCondA:
2863 __ LoadConst32(TMP, imm_high);
2864 __ Bltu(TMP, lhs_high, label);
2865 __ Sltu(TMP, lhs_high, TMP);
2866 __ LoadConst32(AT, imm_low);
2867 __ Sltu(AT, AT, lhs_low);
2868 __ Blt(TMP, AT, label);
2869 break;
2870 }
2871 } else {
2872 switch (cond) {
2873 case kCondEQ:
2874 __ Xor(TMP, lhs_high, rhs_high);
2875 __ Xor(AT, lhs_low, rhs_low);
2876 __ Or(TMP, TMP, AT);
2877 __ Beqz(TMP, label);
2878 break;
2879 case kCondNE:
2880 __ Xor(TMP, lhs_high, rhs_high);
2881 __ Xor(AT, lhs_low, rhs_low);
2882 __ Or(TMP, TMP, AT);
2883 __ Bnez(TMP, label);
2884 break;
2885 case kCondLT:
2886 __ Blt(lhs_high, rhs_high, label);
2887 __ Slt(TMP, rhs_high, lhs_high);
2888 __ Sltu(AT, lhs_low, rhs_low);
2889 __ Blt(TMP, AT, label);
2890 break;
2891 case kCondGE:
2892 __ Blt(rhs_high, lhs_high, label);
2893 __ Slt(TMP, lhs_high, rhs_high);
2894 __ Sltu(AT, lhs_low, rhs_low);
2895 __ Or(TMP, TMP, AT);
2896 __ Beqz(TMP, label);
2897 break;
2898 case kCondLE:
2899 __ Blt(lhs_high, rhs_high, label);
2900 __ Slt(TMP, rhs_high, lhs_high);
2901 __ Sltu(AT, rhs_low, lhs_low);
2902 __ Or(TMP, TMP, AT);
2903 __ Beqz(TMP, label);
2904 break;
2905 case kCondGT:
2906 __ Blt(rhs_high, lhs_high, label);
2907 __ Slt(TMP, lhs_high, rhs_high);
2908 __ Sltu(AT, rhs_low, lhs_low);
2909 __ Blt(TMP, AT, label);
2910 break;
2911 case kCondB:
2912 __ Bltu(lhs_high, rhs_high, label);
2913 __ Sltu(TMP, rhs_high, lhs_high);
2914 __ Sltu(AT, lhs_low, rhs_low);
2915 __ Blt(TMP, AT, label);
2916 break;
2917 case kCondAE:
2918 __ Bltu(rhs_high, lhs_high, label);
2919 __ Sltu(TMP, lhs_high, rhs_high);
2920 __ Sltu(AT, lhs_low, rhs_low);
2921 __ Or(TMP, TMP, AT);
2922 __ Beqz(TMP, label);
2923 break;
2924 case kCondBE:
2925 __ Bltu(lhs_high, rhs_high, label);
2926 __ Sltu(TMP, rhs_high, lhs_high);
2927 __ Sltu(AT, rhs_low, lhs_low);
2928 __ Or(TMP, TMP, AT);
2929 __ Beqz(TMP, label);
2930 break;
2931 case kCondA:
2932 __ Bltu(rhs_high, lhs_high, label);
2933 __ Sltu(TMP, lhs_high, rhs_high);
2934 __ Sltu(AT, rhs_low, lhs_low);
2935 __ Blt(TMP, AT, label);
2936 break;
2937 }
2938 }
2939}
2940
2941void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
2942 bool gt_bias,
2943 Primitive::Type type,
2944 LocationSummary* locations,
2945 MipsLabel* label) {
2946 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2947 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2948 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2949 if (type == Primitive::kPrimFloat) {
2950 if (isR6) {
2951 switch (cond) {
2952 case kCondEQ:
2953 __ CmpEqS(FTMP, lhs, rhs);
2954 __ Bc1nez(FTMP, label);
2955 break;
2956 case kCondNE:
2957 __ CmpEqS(FTMP, lhs, rhs);
2958 __ Bc1eqz(FTMP, label);
2959 break;
2960 case kCondLT:
2961 if (gt_bias) {
2962 __ CmpLtS(FTMP, lhs, rhs);
2963 } else {
2964 __ CmpUltS(FTMP, lhs, rhs);
2965 }
2966 __ Bc1nez(FTMP, label);
2967 break;
2968 case kCondLE:
2969 if (gt_bias) {
2970 __ CmpLeS(FTMP, lhs, rhs);
2971 } else {
2972 __ CmpUleS(FTMP, lhs, rhs);
2973 }
2974 __ Bc1nez(FTMP, label);
2975 break;
2976 case kCondGT:
2977 if (gt_bias) {
2978 __ CmpUltS(FTMP, rhs, lhs);
2979 } else {
2980 __ CmpLtS(FTMP, rhs, lhs);
2981 }
2982 __ Bc1nez(FTMP, label);
2983 break;
2984 case kCondGE:
2985 if (gt_bias) {
2986 __ CmpUleS(FTMP, rhs, lhs);
2987 } else {
2988 __ CmpLeS(FTMP, rhs, lhs);
2989 }
2990 __ Bc1nez(FTMP, label);
2991 break;
2992 default:
2993 LOG(FATAL) << "Unexpected non-floating-point condition";
2994 }
2995 } else {
2996 switch (cond) {
2997 case kCondEQ:
2998 __ CeqS(0, lhs, rhs);
2999 __ Bc1t(0, label);
3000 break;
3001 case kCondNE:
3002 __ CeqS(0, lhs, rhs);
3003 __ Bc1f(0, label);
3004 break;
3005 case kCondLT:
3006 if (gt_bias) {
3007 __ ColtS(0, lhs, rhs);
3008 } else {
3009 __ CultS(0, lhs, rhs);
3010 }
3011 __ Bc1t(0, label);
3012 break;
3013 case kCondLE:
3014 if (gt_bias) {
3015 __ ColeS(0, lhs, rhs);
3016 } else {
3017 __ CuleS(0, lhs, rhs);
3018 }
3019 __ Bc1t(0, label);
3020 break;
3021 case kCondGT:
3022 if (gt_bias) {
3023 __ CultS(0, rhs, lhs);
3024 } else {
3025 __ ColtS(0, rhs, lhs);
3026 }
3027 __ Bc1t(0, label);
3028 break;
3029 case kCondGE:
3030 if (gt_bias) {
3031 __ CuleS(0, rhs, lhs);
3032 } else {
3033 __ ColeS(0, rhs, lhs);
3034 }
3035 __ Bc1t(0, label);
3036 break;
3037 default:
3038 LOG(FATAL) << "Unexpected non-floating-point condition";
3039 }
3040 }
3041 } else {
3042 DCHECK_EQ(type, Primitive::kPrimDouble);
3043 if (isR6) {
3044 switch (cond) {
3045 case kCondEQ:
3046 __ CmpEqD(FTMP, lhs, rhs);
3047 __ Bc1nez(FTMP, label);
3048 break;
3049 case kCondNE:
3050 __ CmpEqD(FTMP, lhs, rhs);
3051 __ Bc1eqz(FTMP, label);
3052 break;
3053 case kCondLT:
3054 if (gt_bias) {
3055 __ CmpLtD(FTMP, lhs, rhs);
3056 } else {
3057 __ CmpUltD(FTMP, lhs, rhs);
3058 }
3059 __ Bc1nez(FTMP, label);
3060 break;
3061 case kCondLE:
3062 if (gt_bias) {
3063 __ CmpLeD(FTMP, lhs, rhs);
3064 } else {
3065 __ CmpUleD(FTMP, lhs, rhs);
3066 }
3067 __ Bc1nez(FTMP, label);
3068 break;
3069 case kCondGT:
3070 if (gt_bias) {
3071 __ CmpUltD(FTMP, rhs, lhs);
3072 } else {
3073 __ CmpLtD(FTMP, rhs, lhs);
3074 }
3075 __ Bc1nez(FTMP, label);
3076 break;
3077 case kCondGE:
3078 if (gt_bias) {
3079 __ CmpUleD(FTMP, rhs, lhs);
3080 } else {
3081 __ CmpLeD(FTMP, rhs, lhs);
3082 }
3083 __ Bc1nez(FTMP, label);
3084 break;
3085 default:
3086 LOG(FATAL) << "Unexpected non-floating-point condition";
3087 }
3088 } else {
3089 switch (cond) {
3090 case kCondEQ:
3091 __ CeqD(0, lhs, rhs);
3092 __ Bc1t(0, label);
3093 break;
3094 case kCondNE:
3095 __ CeqD(0, lhs, rhs);
3096 __ Bc1f(0, label);
3097 break;
3098 case kCondLT:
3099 if (gt_bias) {
3100 __ ColtD(0, lhs, rhs);
3101 } else {
3102 __ CultD(0, lhs, rhs);
3103 }
3104 __ Bc1t(0, label);
3105 break;
3106 case kCondLE:
3107 if (gt_bias) {
3108 __ ColeD(0, lhs, rhs);
3109 } else {
3110 __ CuleD(0, lhs, rhs);
3111 }
3112 __ Bc1t(0, label);
3113 break;
3114 case kCondGT:
3115 if (gt_bias) {
3116 __ CultD(0, rhs, lhs);
3117 } else {
3118 __ ColtD(0, rhs, lhs);
3119 }
3120 __ Bc1t(0, label);
3121 break;
3122 case kCondGE:
3123 if (gt_bias) {
3124 __ CuleD(0, rhs, lhs);
3125 } else {
3126 __ ColeD(0, rhs, lhs);
3127 }
3128 __ Bc1t(0, label);
3129 break;
3130 default:
3131 LOG(FATAL) << "Unexpected non-floating-point condition";
3132 }
3133 }
3134 }
3135}
3136
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003137void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003138 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003139 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003140 MipsLabel* false_target) {
3141 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003142
David Brazdil0debae72015-11-12 18:37:00 +00003143 if (true_target == nullptr && false_target == nullptr) {
3144 // Nothing to do. The code always falls through.
3145 return;
3146 } else if (cond->IsIntConstant()) {
3147 // Constant condition, statically compared against 1.
3148 if (cond->AsIntConstant()->IsOne()) {
3149 if (true_target != nullptr) {
3150 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003151 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003152 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003153 DCHECK(cond->AsIntConstant()->IsZero());
3154 if (false_target != nullptr) {
3155 __ B(false_target);
3156 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003157 }
David Brazdil0debae72015-11-12 18:37:00 +00003158 return;
3159 }
3160
3161 // The following code generates these patterns:
3162 // (1) true_target == nullptr && false_target != nullptr
3163 // - opposite condition true => branch to false_target
3164 // (2) true_target != nullptr && false_target == nullptr
3165 // - condition true => branch to true_target
3166 // (3) true_target != nullptr && false_target != nullptr
3167 // - condition true => branch to true_target
3168 // - branch to false_target
3169 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003170 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003171 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003173 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003174 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3175 } else {
3176 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3177 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003178 } else {
3179 // The condition instruction has not been materialized, use its inputs as
3180 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003181 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003182 Primitive::Type type = condition->InputAt(0)->GetType();
3183 LocationSummary* locations = cond->GetLocations();
3184 IfCondition if_cond = condition->GetCondition();
3185 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003186
David Brazdil0debae72015-11-12 18:37:00 +00003187 if (true_target == nullptr) {
3188 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003189 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003190 }
3191
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003192 switch (type) {
3193 default:
3194 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3195 break;
3196 case Primitive::kPrimLong:
3197 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3198 break;
3199 case Primitive::kPrimFloat:
3200 case Primitive::kPrimDouble:
3201 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3202 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003203 }
3204 }
David Brazdil0debae72015-11-12 18:37:00 +00003205
3206 // If neither branch falls through (case 3), the conditional branch to `true_target`
3207 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3208 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003209 __ B(false_target);
3210 }
3211}
3212
3213void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003215 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003216 locations->SetInAt(0, Location::RequiresRegister());
3217 }
3218}
3219
3220void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003221 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3222 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3223 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3224 nullptr : codegen_->GetLabelOf(true_successor);
3225 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3226 nullptr : codegen_->GetLabelOf(false_successor);
3227 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003228}
3229
3230void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3231 LocationSummary* locations = new (GetGraph()->GetArena())
3232 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003233 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003234 locations->SetInAt(0, Location::RequiresRegister());
3235 }
3236}
3237
3238void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
David Brazdil0debae72015-11-12 18:37:00 +00003239 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DeoptimizationSlowPathMIPS(deoptimize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00003241 GenerateTestAndBranch(deoptimize,
3242 /* condition_input_index */ 0,
3243 slow_path->GetEntryLabel(),
3244 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003245}
3246
3247void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3248 Primitive::Type field_type = field_info.GetFieldType();
3249 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3250 bool generate_volatile = field_info.IsVolatile() && is_wide;
3251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3252 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3253
3254 locations->SetInAt(0, Location::RequiresRegister());
3255 if (generate_volatile) {
3256 InvokeRuntimeCallingConvention calling_convention;
3257 // need A0 to hold base + offset
3258 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3259 if (field_type == Primitive::kPrimLong) {
3260 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3261 } else {
3262 locations->SetOut(Location::RequiresFpuRegister());
3263 // Need some temp core regs since FP results are returned in core registers
3264 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3265 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3266 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3267 }
3268 } else {
3269 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3270 locations->SetOut(Location::RequiresFpuRegister());
3271 } else {
3272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3273 }
3274 }
3275}
3276
3277void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3278 const FieldInfo& field_info,
3279 uint32_t dex_pc) {
3280 Primitive::Type type = field_info.GetFieldType();
3281 LocationSummary* locations = instruction->GetLocations();
3282 Register obj = locations->InAt(0).AsRegister<Register>();
3283 LoadOperandType load_type = kLoadUnsignedByte;
3284 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003285 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003286
3287 switch (type) {
3288 case Primitive::kPrimBoolean:
3289 load_type = kLoadUnsignedByte;
3290 break;
3291 case Primitive::kPrimByte:
3292 load_type = kLoadSignedByte;
3293 break;
3294 case Primitive::kPrimShort:
3295 load_type = kLoadSignedHalfword;
3296 break;
3297 case Primitive::kPrimChar:
3298 load_type = kLoadUnsignedHalfword;
3299 break;
3300 case Primitive::kPrimInt:
3301 case Primitive::kPrimFloat:
3302 case Primitive::kPrimNot:
3303 load_type = kLoadWord;
3304 break;
3305 case Primitive::kPrimLong:
3306 case Primitive::kPrimDouble:
3307 load_type = kLoadDoubleword;
3308 break;
3309 case Primitive::kPrimVoid:
3310 LOG(FATAL) << "Unreachable type " << type;
3311 UNREACHABLE();
3312 }
3313
3314 if (is_volatile && load_type == kLoadDoubleword) {
3315 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003316 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003317 // Do implicit Null check
3318 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3319 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3320 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3321 instruction,
3322 dex_pc,
3323 nullptr,
3324 IsDirectEntrypoint(kQuickA64Load));
3325 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3326 if (type == Primitive::kPrimDouble) {
3327 // Need to move to FP regs since FP results are returned in core registers.
3328 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3329 locations->Out().AsFpuRegister<FRegister>());
3330 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
3331 locations->Out().AsFpuRegister<FRegister>());
3332 }
3333 } else {
3334 if (!Primitive::IsFloatingPointType(type)) {
3335 Register dst;
3336 if (type == Primitive::kPrimLong) {
3337 DCHECK(locations->Out().IsRegisterPair());
3338 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003339 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3340 if (obj == dst) {
3341 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3342 codegen_->MaybeRecordImplicitNullCheck(instruction);
3343 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3344 } else {
3345 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3346 codegen_->MaybeRecordImplicitNullCheck(instruction);
3347 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003349 } else {
3350 DCHECK(locations->Out().IsRegister());
3351 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003352 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003354 } else {
3355 DCHECK(locations->Out().IsFpuRegister());
3356 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3357 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003358 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003359 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003360 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 }
3362 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003363 // Longs are handled earlier.
3364 if (type != Primitive::kPrimLong) {
3365 codegen_->MaybeRecordImplicitNullCheck(instruction);
3366 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003367 }
3368
3369 if (is_volatile) {
3370 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3371 }
3372}
3373
3374void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3375 Primitive::Type field_type = field_info.GetFieldType();
3376 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3377 bool generate_volatile = field_info.IsVolatile() && is_wide;
3378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3379 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3380
3381 locations->SetInAt(0, Location::RequiresRegister());
3382 if (generate_volatile) {
3383 InvokeRuntimeCallingConvention calling_convention;
3384 // need A0 to hold base + offset
3385 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3386 if (field_type == Primitive::kPrimLong) {
3387 locations->SetInAt(1, Location::RegisterPairLocation(
3388 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3389 } else {
3390 locations->SetInAt(1, Location::RequiresFpuRegister());
3391 // Pass FP parameters in core registers.
3392 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3393 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3394 }
3395 } else {
3396 if (Primitive::IsFloatingPointType(field_type)) {
3397 locations->SetInAt(1, Location::RequiresFpuRegister());
3398 } else {
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 }
3401 }
3402}
3403
3404void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3405 const FieldInfo& field_info,
3406 uint32_t dex_pc) {
3407 Primitive::Type type = field_info.GetFieldType();
3408 LocationSummary* locations = instruction->GetLocations();
3409 Register obj = locations->InAt(0).AsRegister<Register>();
3410 StoreOperandType store_type = kStoreByte;
3411 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003412 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003413
3414 switch (type) {
3415 case Primitive::kPrimBoolean:
3416 case Primitive::kPrimByte:
3417 store_type = kStoreByte;
3418 break;
3419 case Primitive::kPrimShort:
3420 case Primitive::kPrimChar:
3421 store_type = kStoreHalfword;
3422 break;
3423 case Primitive::kPrimInt:
3424 case Primitive::kPrimFloat:
3425 case Primitive::kPrimNot:
3426 store_type = kStoreWord;
3427 break;
3428 case Primitive::kPrimLong:
3429 case Primitive::kPrimDouble:
3430 store_type = kStoreDoubleword;
3431 break;
3432 case Primitive::kPrimVoid:
3433 LOG(FATAL) << "Unreachable type " << type;
3434 UNREACHABLE();
3435 }
3436
3437 if (is_volatile) {
3438 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3439 }
3440
3441 if (is_volatile && store_type == kStoreDoubleword) {
3442 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003443 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444 // Do implicit Null check.
3445 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3446 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3447 if (type == Primitive::kPrimDouble) {
3448 // Pass FP parameters in core registers.
3449 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3450 locations->InAt(1).AsFpuRegister<FRegister>());
3451 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
3452 locations->InAt(1).AsFpuRegister<FRegister>());
3453 }
3454 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3455 instruction,
3456 dex_pc,
3457 nullptr,
3458 IsDirectEntrypoint(kQuickA64Store));
3459 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3460 } else {
3461 if (!Primitive::IsFloatingPointType(type)) {
3462 Register src;
3463 if (type == Primitive::kPrimLong) {
3464 DCHECK(locations->InAt(1).IsRegisterPair());
3465 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003466 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3467 __ StoreToOffset(kStoreWord, src, obj, offset);
3468 codegen_->MaybeRecordImplicitNullCheck(instruction);
3469 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470 } else {
3471 DCHECK(locations->InAt(1).IsRegister());
3472 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003473 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003474 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003475 } else {
3476 DCHECK(locations->InAt(1).IsFpuRegister());
3477 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3478 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003479 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003480 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003481 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482 }
3483 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003484 // Longs are handled earlier.
3485 if (type != Primitive::kPrimLong) {
3486 codegen_->MaybeRecordImplicitNullCheck(instruction);
3487 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003488 }
3489
3490 // TODO: memory barriers?
3491 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3492 DCHECK(locations->InAt(1).IsRegister());
3493 Register src = locations->InAt(1).AsRegister<Register>();
3494 codegen_->MarkGCCard(obj, src);
3495 }
3496
3497 if (is_volatile) {
3498 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3499 }
3500}
3501
3502void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3503 HandleFieldGet(instruction, instruction->GetFieldInfo());
3504}
3505
3506void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3507 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3508}
3509
3510void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3511 HandleFieldSet(instruction, instruction->GetFieldInfo());
3512}
3513
3514void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3515 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3516}
3517
3518void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3519 LocationSummary::CallKind call_kind =
3520 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3522 locations->SetInAt(0, Location::RequiresRegister());
3523 locations->SetInAt(1, Location::RequiresRegister());
3524 // The output does overlap inputs.
3525 // Note that TypeCheckSlowPathMIPS uses this register too.
3526 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3527}
3528
3529void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3530 LocationSummary* locations = instruction->GetLocations();
3531 Register obj = locations->InAt(0).AsRegister<Register>();
3532 Register cls = locations->InAt(1).AsRegister<Register>();
3533 Register out = locations->Out().AsRegister<Register>();
3534
3535 MipsLabel done;
3536
3537 // Return 0 if `obj` is null.
3538 // TODO: Avoid this check if we know `obj` is not null.
3539 __ Move(out, ZERO);
3540 __ Beqz(obj, &done);
3541
3542 // Compare the class of `obj` with `cls`.
3543 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3544 if (instruction->IsExactCheck()) {
3545 // Classes must be equal for the instanceof to succeed.
3546 __ Xor(out, out, cls);
3547 __ Sltiu(out, out, 1);
3548 } else {
3549 // If the classes are not equal, we go into a slow path.
3550 DCHECK(locations->OnlyCallsOnSlowPath());
3551 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3552 codegen_->AddSlowPath(slow_path);
3553 __ Bne(out, cls, slow_path->GetEntryLabel());
3554 __ LoadConst32(out, 1);
3555 __ Bind(slow_path->GetExitLabel());
3556 }
3557
3558 __ Bind(&done);
3559}
3560
3561void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3562 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3563 locations->SetOut(Location::ConstantLocation(constant));
3564}
3565
3566void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3567 // Will be generated at use site.
3568}
3569
3570void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3571 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3572 locations->SetOut(Location::ConstantLocation(constant));
3573}
3574
3575void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3576 // Will be generated at use site.
3577}
3578
3579void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3580 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3581 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3582}
3583
3584void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3585 HandleInvoke(invoke);
3586 // The register T0 is required to be used for the hidden argument in
3587 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3588 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3589}
3590
3591void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3592 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3593 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3594 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3595 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3596 Location receiver = invoke->GetLocations()->InAt(0);
3597 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3598 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3599
3600 // Set the hidden argument.
3601 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3602 invoke->GetDexMethodIndex());
3603
3604 // temp = object->GetClass();
3605 if (receiver.IsStackSlot()) {
3606 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3607 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3608 } else {
3609 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3610 }
3611 codegen_->MaybeRecordImplicitNullCheck(invoke);
3612 // temp = temp->GetImtEntryAt(method_offset);
3613 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3614 // T9 = temp->GetEntryPoint();
3615 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3616 // T9();
3617 __ Jalr(T9);
3618 __ Nop();
3619 DCHECK(!codegen_->IsLeafMethod());
3620 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3621}
3622
3623void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003624 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3625 if (intrinsic.TryDispatch(invoke)) {
3626 return;
3627 }
3628
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003629 HandleInvoke(invoke);
3630}
3631
3632void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3633 // When we do not run baseline, explicit clinit checks triggered by static
3634 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3635 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3636
Chris Larsen701566a2015-10-27 15:29:13 -07003637 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3638 if (intrinsic.TryDispatch(invoke)) {
3639 return;
3640 }
3641
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 HandleInvoke(invoke);
3643}
3644
Chris Larsen701566a2015-10-27 15:29:13 -07003645static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003646 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003647 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3648 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649 return true;
3650 }
3651 return false;
3652}
3653
Vladimir Markodc151b22015-10-15 18:02:30 +01003654HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3655 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3656 MethodReference target_method ATTRIBUTE_UNUSED) {
3657 switch (desired_dispatch_info.method_load_kind) {
3658 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3659 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3660 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3661 return HInvokeStaticOrDirect::DispatchInfo {
3662 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3663 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3664 0u,
3665 0u
3666 };
3667 default:
3668 break;
3669 }
3670 switch (desired_dispatch_info.code_ptr_location) {
3671 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3672 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3673 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3674 return HInvokeStaticOrDirect::DispatchInfo {
3675 desired_dispatch_info.method_load_kind,
3676 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3677 desired_dispatch_info.method_load_data,
3678 0u
3679 };
3680 default:
3681 return desired_dispatch_info;
3682 }
3683}
3684
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3686 // All registers are assumed to be correctly set up per the calling convention.
3687
3688 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3689 switch (invoke->GetMethodLoadKind()) {
3690 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3691 // temp = thread->string_init_entrypoint
3692 __ LoadFromOffset(kLoadWord,
3693 temp.AsRegister<Register>(),
3694 TR,
3695 invoke->GetStringInitOffset());
3696 break;
3697 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003698 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 break;
3700 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3701 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3702 break;
3703 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003705 // TODO: Implement these types.
3706 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3707 LOG(FATAL) << "Unsupported";
3708 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003710 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003711 Register reg = temp.AsRegister<Register>();
3712 Register method_reg;
3713 if (current_method.IsRegister()) {
3714 method_reg = current_method.AsRegister<Register>();
3715 } else {
3716 // TODO: use the appropriate DCHECK() here if possible.
3717 // DCHECK(invoke->GetLocations()->Intrinsified());
3718 DCHECK(!current_method.IsValid());
3719 method_reg = reg;
3720 __ Lw(reg, SP, kCurrentMethodStackOffset);
3721 }
3722
3723 // temp = temp->dex_cache_resolved_methods_;
3724 __ LoadFromOffset(kLoadWord,
3725 reg,
3726 method_reg,
3727 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3728 // temp = temp[index_in_cache]
3729 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3730 __ LoadFromOffset(kLoadWord,
3731 reg,
3732 reg,
3733 CodeGenerator::GetCachePointerOffset(index_in_cache));
3734 break;
3735 }
3736 }
3737
3738 switch (invoke->GetCodePtrLocation()) {
3739 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3740 __ Jalr(&frame_entry_label_, T9);
3741 break;
3742 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3743 // LR = invoke->GetDirectCodePtr();
3744 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3745 // LR()
3746 __ Jalr(T9);
3747 __ Nop();
3748 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003749 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003750 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3751 // TODO: Implement these types.
3752 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3753 LOG(FATAL) << "Unsupported";
3754 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3756 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003757 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003758 T9,
3759 callee_method.AsRegister<Register>(),
3760 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3761 kMipsWordSize).Int32Value());
3762 // T9()
3763 __ Jalr(T9);
3764 __ Nop();
3765 break;
3766 }
3767 DCHECK(!IsLeafMethod());
3768}
3769
3770void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3771 // When we do not run baseline, explicit clinit checks triggered by static
3772 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3773 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3774
3775 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3776 return;
3777 }
3778
3779 LocationSummary* locations = invoke->GetLocations();
3780 codegen_->GenerateStaticOrDirectCall(invoke,
3781 locations->HasTemps()
3782 ? locations->GetTemp(0)
3783 : Location::NoLocation());
3784 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3785}
3786
3787void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003788 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3789 return;
3790 }
3791
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003792 LocationSummary* locations = invoke->GetLocations();
3793 Location receiver = locations->InAt(0);
3794 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3795 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3796 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3797 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3798 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3799
3800 // temp = object->GetClass();
3801 if (receiver.IsStackSlot()) {
3802 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3803 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3804 } else {
3805 DCHECK(receiver.IsRegister());
3806 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3807 }
3808 codegen_->MaybeRecordImplicitNullCheck(invoke);
3809 // temp = temp->GetMethodAt(method_offset);
3810 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3811 // T9 = temp->GetEntryPoint();
3812 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3813 // T9();
3814 __ Jalr(T9);
3815 __ Nop();
3816 DCHECK(!codegen_->IsLeafMethod());
3817 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3818}
3819
3820void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003821 InvokeRuntimeCallingConvention calling_convention;
3822 CodeGenerator::CreateLoadClassLocationSummary(
3823 cls,
3824 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3825 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826}
3827
3828void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3829 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003830 if (cls->NeedsAccessCheck()) {
3831 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3832 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3833 cls,
3834 cls->GetDexPc(),
3835 nullptr,
3836 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003837 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003838 return;
3839 }
3840
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003841 Register out = locations->Out().AsRegister<Register>();
3842 Register current_method = locations->InAt(0).AsRegister<Register>();
3843 if (cls->IsReferrersClass()) {
3844 DCHECK(!cls->CanCallRuntime());
3845 DCHECK(!cls->MustGenerateClinitCheck());
3846 __ LoadFromOffset(kLoadWord, out, current_method,
3847 ArtMethod::DeclaringClassOffset().Int32Value());
3848 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003849 __ LoadFromOffset(kLoadWord, out, current_method,
3850 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3851 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003852
3853 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3854 DCHECK(cls->CanCallRuntime());
3855 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3856 cls,
3857 cls,
3858 cls->GetDexPc(),
3859 cls->MustGenerateClinitCheck());
3860 codegen_->AddSlowPath(slow_path);
3861 if (!cls->IsInDexCache()) {
3862 __ Beqz(out, slow_path->GetEntryLabel());
3863 }
3864 if (cls->MustGenerateClinitCheck()) {
3865 GenerateClassInitializationCheck(slow_path, out);
3866 } else {
3867 __ Bind(slow_path->GetExitLabel());
3868 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003869 }
3870 }
3871}
3872
3873static int32_t GetExceptionTlsOffset() {
3874 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3875}
3876
3877void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
3878 LocationSummary* locations =
3879 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3880 locations->SetOut(Location::RequiresRegister());
3881}
3882
3883void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
3884 Register out = load->GetLocations()->Out().AsRegister<Register>();
3885 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
3886}
3887
3888void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
3889 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3890}
3891
3892void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3893 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
3894}
3895
3896void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
3897 load->SetLocations(nullptr);
3898}
3899
3900void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3901 // Nothing to do, this is driven by the code generator.
3902}
3903
3904void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00003905 LocationSummary::CallKind call_kind = load->IsInDexCache()
3906 ? LocationSummary::kNoCall
3907 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003908 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 locations->SetInAt(0, Location::RequiresRegister());
3910 locations->SetOut(Location::RequiresRegister());
3911}
3912
3913void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 LocationSummary* locations = load->GetLocations();
3915 Register out = locations->Out().AsRegister<Register>();
3916 Register current_method = locations->InAt(0).AsRegister<Register>();
3917 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
3918 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
3919 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003920
3921 if (!load->IsInDexCache()) {
3922 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
3923 codegen_->AddSlowPath(slow_path);
3924 __ Beqz(out, slow_path->GetEntryLabel());
3925 __ Bind(slow_path->GetExitLabel());
3926 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927}
3928
3929void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
3930 local->SetLocations(nullptr);
3931}
3932
3933void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
3934 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3935}
3936
3937void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
3938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3939 locations->SetOut(Location::ConstantLocation(constant));
3940}
3941
3942void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3943 // Will be generated at use site.
3944}
3945
3946void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3947 LocationSummary* locations =
3948 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3949 InvokeRuntimeCallingConvention calling_convention;
3950 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3951}
3952
3953void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3954 if (instruction->IsEnter()) {
3955 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
3956 instruction,
3957 instruction->GetDexPc(),
3958 nullptr,
3959 IsDirectEntrypoint(kQuickLockObject));
3960 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3961 } else {
3962 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
3963 instruction,
3964 instruction->GetDexPc(),
3965 nullptr,
3966 IsDirectEntrypoint(kQuickUnlockObject));
3967 }
3968 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3969}
3970
3971void LocationsBuilderMIPS::VisitMul(HMul* mul) {
3972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3974 switch (mul->GetResultType()) {
3975 case Primitive::kPrimInt:
3976 case Primitive::kPrimLong:
3977 locations->SetInAt(0, Location::RequiresRegister());
3978 locations->SetInAt(1, Location::RequiresRegister());
3979 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3980 break;
3981
3982 case Primitive::kPrimFloat:
3983 case Primitive::kPrimDouble:
3984 locations->SetInAt(0, Location::RequiresFpuRegister());
3985 locations->SetInAt(1, Location::RequiresFpuRegister());
3986 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3987 break;
3988
3989 default:
3990 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3991 }
3992}
3993
3994void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
3995 Primitive::Type type = instruction->GetType();
3996 LocationSummary* locations = instruction->GetLocations();
3997 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3998
3999 switch (type) {
4000 case Primitive::kPrimInt: {
4001 Register dst = locations->Out().AsRegister<Register>();
4002 Register lhs = locations->InAt(0).AsRegister<Register>();
4003 Register rhs = locations->InAt(1).AsRegister<Register>();
4004
4005 if (isR6) {
4006 __ MulR6(dst, lhs, rhs);
4007 } else {
4008 __ MulR2(dst, lhs, rhs);
4009 }
4010 break;
4011 }
4012 case Primitive::kPrimLong: {
4013 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4014 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4015 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4016 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4017 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4018 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4019
4020 // Extra checks to protect caused by the existance of A1_A2.
4021 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4022 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4023 DCHECK_NE(dst_high, lhs_low);
4024 DCHECK_NE(dst_high, rhs_low);
4025
4026 // A_B * C_D
4027 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4028 // dst_lo: [ low(B*D) ]
4029 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4030
4031 if (isR6) {
4032 __ MulR6(TMP, lhs_high, rhs_low);
4033 __ MulR6(dst_high, lhs_low, rhs_high);
4034 __ Addu(dst_high, dst_high, TMP);
4035 __ MuhuR6(TMP, lhs_low, rhs_low);
4036 __ Addu(dst_high, dst_high, TMP);
4037 __ MulR6(dst_low, lhs_low, rhs_low);
4038 } else {
4039 __ MulR2(TMP, lhs_high, rhs_low);
4040 __ MulR2(dst_high, lhs_low, rhs_high);
4041 __ Addu(dst_high, dst_high, TMP);
4042 __ MultuR2(lhs_low, rhs_low);
4043 __ Mfhi(TMP);
4044 __ Addu(dst_high, dst_high, TMP);
4045 __ Mflo(dst_low);
4046 }
4047 break;
4048 }
4049 case Primitive::kPrimFloat:
4050 case Primitive::kPrimDouble: {
4051 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4052 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4053 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4054 if (type == Primitive::kPrimFloat) {
4055 __ MulS(dst, lhs, rhs);
4056 } else {
4057 __ MulD(dst, lhs, rhs);
4058 }
4059 break;
4060 }
4061 default:
4062 LOG(FATAL) << "Unexpected mul type " << type;
4063 }
4064}
4065
4066void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4067 LocationSummary* locations =
4068 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4069 switch (neg->GetResultType()) {
4070 case Primitive::kPrimInt:
4071 case Primitive::kPrimLong:
4072 locations->SetInAt(0, Location::RequiresRegister());
4073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4074 break;
4075
4076 case Primitive::kPrimFloat:
4077 case Primitive::kPrimDouble:
4078 locations->SetInAt(0, Location::RequiresFpuRegister());
4079 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4080 break;
4081
4082 default:
4083 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4084 }
4085}
4086
4087void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4088 Primitive::Type type = instruction->GetType();
4089 LocationSummary* locations = instruction->GetLocations();
4090
4091 switch (type) {
4092 case Primitive::kPrimInt: {
4093 Register dst = locations->Out().AsRegister<Register>();
4094 Register src = locations->InAt(0).AsRegister<Register>();
4095 __ Subu(dst, ZERO, src);
4096 break;
4097 }
4098 case Primitive::kPrimLong: {
4099 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4100 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4101 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4102 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4103 __ Subu(dst_low, ZERO, src_low);
4104 __ Sltu(TMP, ZERO, dst_low);
4105 __ Subu(dst_high, ZERO, src_high);
4106 __ Subu(dst_high, dst_high, TMP);
4107 break;
4108 }
4109 case Primitive::kPrimFloat:
4110 case Primitive::kPrimDouble: {
4111 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4112 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4113 if (type == Primitive::kPrimFloat) {
4114 __ NegS(dst, src);
4115 } else {
4116 __ NegD(dst, src);
4117 }
4118 break;
4119 }
4120 default:
4121 LOG(FATAL) << "Unexpected neg type " << type;
4122 }
4123}
4124
4125void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4126 LocationSummary* locations =
4127 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4128 InvokeRuntimeCallingConvention calling_convention;
4129 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4130 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4131 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4132 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4133}
4134
4135void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4136 InvokeRuntimeCallingConvention calling_convention;
4137 Register current_method_register = calling_convention.GetRegisterAt(2);
4138 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4139 // Move an uint16_t value to a register.
4140 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4141 codegen_->InvokeRuntime(
4142 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4143 instruction,
4144 instruction->GetDexPc(),
4145 nullptr,
4146 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4147 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4148 void*, uint32_t, int32_t, ArtMethod*>();
4149}
4150
4151void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4152 LocationSummary* locations =
4153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4154 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004155 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4156 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004157 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4158}
4159
4160void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004161 codegen_->InvokeRuntime(
4162 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4163 instruction,
4164 instruction->GetDexPc(),
4165 nullptr,
4166 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4167 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4168}
4169
4170void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4172 locations->SetInAt(0, Location::RequiresRegister());
4173 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4174}
4175
4176void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4177 Primitive::Type type = instruction->GetType();
4178 LocationSummary* locations = instruction->GetLocations();
4179
4180 switch (type) {
4181 case Primitive::kPrimInt: {
4182 Register dst = locations->Out().AsRegister<Register>();
4183 Register src = locations->InAt(0).AsRegister<Register>();
4184 __ Nor(dst, src, ZERO);
4185 break;
4186 }
4187
4188 case Primitive::kPrimLong: {
4189 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4190 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4191 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4192 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4193 __ Nor(dst_high, src_high, ZERO);
4194 __ Nor(dst_low, src_low, ZERO);
4195 break;
4196 }
4197
4198 default:
4199 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4200 }
4201}
4202
4203void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4204 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4205 locations->SetInAt(0, Location::RequiresRegister());
4206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4207}
4208
4209void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4210 LocationSummary* locations = instruction->GetLocations();
4211 __ Xori(locations->Out().AsRegister<Register>(),
4212 locations->InAt(0).AsRegister<Register>(),
4213 1);
4214}
4215
4216void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4217 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4218 ? LocationSummary::kCallOnSlowPath
4219 : LocationSummary::kNoCall;
4220 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4221 locations->SetInAt(0, Location::RequiresRegister());
4222 if (instruction->HasUses()) {
4223 locations->SetOut(Location::SameAsFirstInput());
4224 }
4225}
4226
4227void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4228 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4229 return;
4230 }
4231 Location obj = instruction->GetLocations()->InAt(0);
4232
4233 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
4234 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4235}
4236
4237void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
4238 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
4239 codegen_->AddSlowPath(slow_path);
4240
4241 Location obj = instruction->GetLocations()->InAt(0);
4242
4243 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4244}
4245
4246void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
4247 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
4248 GenerateImplicitNullCheck(instruction);
4249 } else {
4250 GenerateExplicitNullCheck(instruction);
4251 }
4252}
4253
4254void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4255 HandleBinaryOp(instruction);
4256}
4257
4258void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4259 HandleBinaryOp(instruction);
4260}
4261
4262void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4263 LOG(FATAL) << "Unreachable";
4264}
4265
4266void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4267 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4268}
4269
4270void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4271 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4272 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4273 if (location.IsStackSlot()) {
4274 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4275 } else if (location.IsDoubleStackSlot()) {
4276 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4277 }
4278 locations->SetOut(location);
4279}
4280
4281void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4282 ATTRIBUTE_UNUSED) {
4283 // Nothing to do, the parameter is already at its location.
4284}
4285
4286void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4287 LocationSummary* locations =
4288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4289 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4290}
4291
4292void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4293 ATTRIBUTE_UNUSED) {
4294 // Nothing to do, the method is already at its location.
4295}
4296
4297void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4299 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4300 locations->SetInAt(i, Location::Any());
4301 }
4302 locations->SetOut(Location::Any());
4303}
4304
4305void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4306 LOG(FATAL) << "Unreachable";
4307}
4308
4309void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4310 Primitive::Type type = rem->GetResultType();
4311 LocationSummary::CallKind call_kind =
4312 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4314
4315 switch (type) {
4316 case Primitive::kPrimInt:
4317 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004318 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4320 break;
4321
4322 case Primitive::kPrimLong: {
4323 InvokeRuntimeCallingConvention calling_convention;
4324 locations->SetInAt(0, Location::RegisterPairLocation(
4325 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4326 locations->SetInAt(1, Location::RegisterPairLocation(
4327 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4328 locations->SetOut(calling_convention.GetReturnLocation(type));
4329 break;
4330 }
4331
4332 case Primitive::kPrimFloat:
4333 case Primitive::kPrimDouble: {
4334 InvokeRuntimeCallingConvention calling_convention;
4335 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4336 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4337 locations->SetOut(calling_convention.GetReturnLocation(type));
4338 break;
4339 }
4340
4341 default:
4342 LOG(FATAL) << "Unexpected rem type " << type;
4343 }
4344}
4345
4346void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4347 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004348
4349 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004350 case Primitive::kPrimInt:
4351 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004352 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004353 case Primitive::kPrimLong: {
4354 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4355 instruction,
4356 instruction->GetDexPc(),
4357 nullptr,
4358 IsDirectEntrypoint(kQuickLmod));
4359 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4360 break;
4361 }
4362 case Primitive::kPrimFloat: {
4363 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4364 instruction, instruction->GetDexPc(),
4365 nullptr,
4366 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004367 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004368 break;
4369 }
4370 case Primitive::kPrimDouble: {
4371 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4372 instruction, instruction->GetDexPc(),
4373 nullptr,
4374 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004375 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004376 break;
4377 }
4378 default:
4379 LOG(FATAL) << "Unexpected rem type " << type;
4380 }
4381}
4382
4383void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4384 memory_barrier->SetLocations(nullptr);
4385}
4386
4387void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4388 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4389}
4390
4391void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4393 Primitive::Type return_type = ret->InputAt(0)->GetType();
4394 locations->SetInAt(0, MipsReturnLocation(return_type));
4395}
4396
4397void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4398 codegen_->GenerateFrameExit();
4399}
4400
4401void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4402 ret->SetLocations(nullptr);
4403}
4404
4405void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4406 codegen_->GenerateFrameExit();
4407}
4408
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004409void LocationsBuilderMIPS::VisitRor(HRor* ror ATTRIBUTE_UNUSED) {
4410 LOG(FATAL) << "Unreachable";
4411 UNREACHABLE();
4412}
4413
4414void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror ATTRIBUTE_UNUSED) {
4415 LOG(FATAL) << "Unreachable";
4416 UNREACHABLE();
4417}
4418
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004419void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4420 HandleShift(shl);
4421}
4422
4423void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4424 HandleShift(shl);
4425}
4426
4427void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4428 HandleShift(shr);
4429}
4430
4431void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4432 HandleShift(shr);
4433}
4434
4435void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4437 Primitive::Type field_type = store->InputAt(1)->GetType();
4438 switch (field_type) {
4439 case Primitive::kPrimNot:
4440 case Primitive::kPrimBoolean:
4441 case Primitive::kPrimByte:
4442 case Primitive::kPrimChar:
4443 case Primitive::kPrimShort:
4444 case Primitive::kPrimInt:
4445 case Primitive::kPrimFloat:
4446 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4447 break;
4448
4449 case Primitive::kPrimLong:
4450 case Primitive::kPrimDouble:
4451 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4452 break;
4453
4454 default:
4455 LOG(FATAL) << "Unimplemented local type " << field_type;
4456 }
4457}
4458
4459void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4460}
4461
4462void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4463 HandleBinaryOp(instruction);
4464}
4465
4466void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4467 HandleBinaryOp(instruction);
4468}
4469
4470void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4471 HandleFieldGet(instruction, instruction->GetFieldInfo());
4472}
4473
4474void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4475 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4476}
4477
4478void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4479 HandleFieldSet(instruction, instruction->GetFieldInfo());
4480}
4481
4482void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4483 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4484}
4485
4486void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4487 HUnresolvedInstanceFieldGet* instruction) {
4488 FieldAccessCallingConventionMIPS calling_convention;
4489 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4490 instruction->GetFieldType(),
4491 calling_convention);
4492}
4493
4494void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4495 HUnresolvedInstanceFieldGet* instruction) {
4496 FieldAccessCallingConventionMIPS calling_convention;
4497 codegen_->GenerateUnresolvedFieldAccess(instruction,
4498 instruction->GetFieldType(),
4499 instruction->GetFieldIndex(),
4500 instruction->GetDexPc(),
4501 calling_convention);
4502}
4503
4504void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4505 HUnresolvedInstanceFieldSet* instruction) {
4506 FieldAccessCallingConventionMIPS calling_convention;
4507 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4508 instruction->GetFieldType(),
4509 calling_convention);
4510}
4511
4512void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4513 HUnresolvedInstanceFieldSet* instruction) {
4514 FieldAccessCallingConventionMIPS calling_convention;
4515 codegen_->GenerateUnresolvedFieldAccess(instruction,
4516 instruction->GetFieldType(),
4517 instruction->GetFieldIndex(),
4518 instruction->GetDexPc(),
4519 calling_convention);
4520}
4521
4522void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4523 HUnresolvedStaticFieldGet* instruction) {
4524 FieldAccessCallingConventionMIPS calling_convention;
4525 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4526 instruction->GetFieldType(),
4527 calling_convention);
4528}
4529
4530void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4531 HUnresolvedStaticFieldGet* instruction) {
4532 FieldAccessCallingConventionMIPS calling_convention;
4533 codegen_->GenerateUnresolvedFieldAccess(instruction,
4534 instruction->GetFieldType(),
4535 instruction->GetFieldIndex(),
4536 instruction->GetDexPc(),
4537 calling_convention);
4538}
4539
4540void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4541 HUnresolvedStaticFieldSet* instruction) {
4542 FieldAccessCallingConventionMIPS calling_convention;
4543 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4544 instruction->GetFieldType(),
4545 calling_convention);
4546}
4547
4548void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4549 HUnresolvedStaticFieldSet* instruction) {
4550 FieldAccessCallingConventionMIPS calling_convention;
4551 codegen_->GenerateUnresolvedFieldAccess(instruction,
4552 instruction->GetFieldType(),
4553 instruction->GetFieldIndex(),
4554 instruction->GetDexPc(),
4555 calling_convention);
4556}
4557
4558void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4560}
4561
4562void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4563 HBasicBlock* block = instruction->GetBlock();
4564 if (block->GetLoopInformation() != nullptr) {
4565 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4566 // The back edge will generate the suspend check.
4567 return;
4568 }
4569 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4570 // The goto will generate the suspend check.
4571 return;
4572 }
4573 GenerateSuspendCheck(instruction, nullptr);
4574}
4575
4576void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4577 temp->SetLocations(nullptr);
4578}
4579
4580void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4581 // Nothing to do, this is driven by the code generator.
4582}
4583
4584void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4585 LocationSummary* locations =
4586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4587 InvokeRuntimeCallingConvention calling_convention;
4588 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4589}
4590
4591void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4592 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4593 instruction,
4594 instruction->GetDexPc(),
4595 nullptr,
4596 IsDirectEntrypoint(kQuickDeliverException));
4597 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4598}
4599
4600void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4601 Primitive::Type input_type = conversion->GetInputType();
4602 Primitive::Type result_type = conversion->GetResultType();
4603 DCHECK_NE(input_type, result_type);
4604
4605 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4606 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4607 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4608 }
4609
4610 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4611 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4612 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
4613 call_kind = LocationSummary::kCall;
4614 }
4615
4616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4617
4618 if (call_kind == LocationSummary::kNoCall) {
4619 if (Primitive::IsFloatingPointType(input_type)) {
4620 locations->SetInAt(0, Location::RequiresFpuRegister());
4621 } else {
4622 locations->SetInAt(0, Location::RequiresRegister());
4623 }
4624
4625 if (Primitive::IsFloatingPointType(result_type)) {
4626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4627 } else {
4628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4629 }
4630 } else {
4631 InvokeRuntimeCallingConvention calling_convention;
4632
4633 if (Primitive::IsFloatingPointType(input_type)) {
4634 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4635 } else {
4636 DCHECK_EQ(input_type, Primitive::kPrimLong);
4637 locations->SetInAt(0, Location::RegisterPairLocation(
4638 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4639 }
4640
4641 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4642 }
4643}
4644
4645void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4646 LocationSummary* locations = conversion->GetLocations();
4647 Primitive::Type result_type = conversion->GetResultType();
4648 Primitive::Type input_type = conversion->GetInputType();
4649 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
4650
4651 DCHECK_NE(input_type, result_type);
4652
4653 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4654 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4655 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4656 Register src = locations->InAt(0).AsRegister<Register>();
4657
4658 __ Move(dst_low, src);
4659 __ Sra(dst_high, src, 31);
4660 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4661 Register dst = locations->Out().AsRegister<Register>();
4662 Register src = (input_type == Primitive::kPrimLong)
4663 ? locations->InAt(0).AsRegisterPairLow<Register>()
4664 : locations->InAt(0).AsRegister<Register>();
4665
4666 switch (result_type) {
4667 case Primitive::kPrimChar:
4668 __ Andi(dst, src, 0xFFFF);
4669 break;
4670 case Primitive::kPrimByte:
4671 if (has_sign_extension) {
4672 __ Seb(dst, src);
4673 } else {
4674 __ Sll(dst, src, 24);
4675 __ Sra(dst, dst, 24);
4676 }
4677 break;
4678 case Primitive::kPrimShort:
4679 if (has_sign_extension) {
4680 __ Seh(dst, src);
4681 } else {
4682 __ Sll(dst, src, 16);
4683 __ Sra(dst, dst, 16);
4684 }
4685 break;
4686 case Primitive::kPrimInt:
4687 __ Move(dst, src);
4688 break;
4689
4690 default:
4691 LOG(FATAL) << "Unexpected type conversion from " << input_type
4692 << " to " << result_type;
4693 }
4694 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
4695 if (input_type != Primitive::kPrimLong) {
4696 Register src = locations->InAt(0).AsRegister<Register>();
4697 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4698 __ Mtc1(src, FTMP);
4699 if (result_type == Primitive::kPrimFloat) {
4700 __ Cvtsw(dst, FTMP);
4701 } else {
4702 __ Cvtdw(dst, FTMP);
4703 }
4704 } else {
4705 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4706 : QUICK_ENTRY_POINT(pL2d);
4707 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4708 : IsDirectEntrypoint(kQuickL2d);
4709 codegen_->InvokeRuntime(entry_offset,
4710 conversion,
4711 conversion->GetDexPc(),
4712 nullptr,
4713 direct);
4714 if (result_type == Primitive::kPrimFloat) {
4715 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4716 } else {
4717 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4718 }
4719 }
4720 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4721 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4722 int32_t entry_offset;
4723 bool direct;
4724 if (result_type != Primitive::kPrimLong) {
4725 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4726 : QUICK_ENTRY_POINT(pD2iz);
4727 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
4728 : IsDirectEntrypoint(kQuickD2iz);
4729 } else {
4730 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4731 : QUICK_ENTRY_POINT(pD2l);
4732 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4733 : IsDirectEntrypoint(kQuickD2l);
4734 }
4735 codegen_->InvokeRuntime(entry_offset,
4736 conversion,
4737 conversion->GetDexPc(),
4738 nullptr,
4739 direct);
4740 if (result_type != Primitive::kPrimLong) {
4741 if (input_type == Primitive::kPrimFloat) {
4742 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4743 } else {
4744 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4745 }
4746 } else {
4747 if (input_type == Primitive::kPrimFloat) {
4748 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4749 } else {
4750 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4751 }
4752 }
4753 } else if (Primitive::IsFloatingPointType(result_type) &&
4754 Primitive::IsFloatingPointType(input_type)) {
4755 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4756 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4757 if (result_type == Primitive::kPrimFloat) {
4758 __ Cvtsd(dst, src);
4759 } else {
4760 __ Cvtds(dst, src);
4761 }
4762 } else {
4763 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4764 << " to " << result_type;
4765 }
4766}
4767
4768void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4769 HandleShift(ushr);
4770}
4771
4772void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4773 HandleShift(ushr);
4774}
4775
4776void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4777 HandleBinaryOp(instruction);
4778}
4779
4780void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4781 HandleBinaryOp(instruction);
4782}
4783
4784void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4785 // Nothing to do, this should be removed during prepare for register allocator.
4786 LOG(FATAL) << "Unreachable";
4787}
4788
4789void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4790 // Nothing to do, this should be removed during prepare for register allocator.
4791 LOG(FATAL) << "Unreachable";
4792}
4793
4794void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
4795 VisitCondition(comp);
4796}
4797
4798void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
4799 VisitCondition(comp);
4800}
4801
4802void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
4803 VisitCondition(comp);
4804}
4805
4806void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
4807 VisitCondition(comp);
4808}
4809
4810void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
4811 VisitCondition(comp);
4812}
4813
4814void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
4815 VisitCondition(comp);
4816}
4817
4818void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4819 VisitCondition(comp);
4820}
4821
4822void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4823 VisitCondition(comp);
4824}
4825
4826void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
4827 VisitCondition(comp);
4828}
4829
4830void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
4831 VisitCondition(comp);
4832}
4833
4834void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4835 VisitCondition(comp);
4836}
4837
4838void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4839 VisitCondition(comp);
4840}
4841
4842void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
4843 VisitCondition(comp);
4844}
4845
4846void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
4847 VisitCondition(comp);
4848}
4849
4850void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4851 VisitCondition(comp);
4852}
4853
4854void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4855 VisitCondition(comp);
4856}
4857
4858void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
4859 VisitCondition(comp);
4860}
4861
4862void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
4863 VisitCondition(comp);
4864}
4865
4866void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4867 VisitCondition(comp);
4868}
4869
4870void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4871 VisitCondition(comp);
4872}
4873
4874void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
4875 DCHECK(codegen_->IsBaseline());
4876 LocationSummary* locations =
4877 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4878 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4879}
4880
4881void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4882 DCHECK(codegen_->IsBaseline());
4883 // Will be generated at use site.
4884}
4885
4886void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4887 LocationSummary* locations =
4888 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4889 locations->SetInAt(0, Location::RequiresRegister());
4890}
4891
4892void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4893 int32_t lower_bound = switch_instr->GetStartValue();
4894 int32_t num_entries = switch_instr->GetNumEntries();
4895 LocationSummary* locations = switch_instr->GetLocations();
4896 Register value_reg = locations->InAt(0).AsRegister<Register>();
4897 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4898
4899 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004900 Register temp_reg = TMP;
4901 __ Addiu32(temp_reg, value_reg, -lower_bound);
4902 // Jump to default if index is negative
4903 // Note: We don't check the case that index is positive while value < lower_bound, because in
4904 // this case, index >= num_entries must be true. So that we can save one branch instruction.
4905 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
4906
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004907 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004908 // Jump to successors[0] if value == lower_bound.
4909 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
4910 int32_t last_index = 0;
4911 for (; num_entries - last_index > 2; last_index += 2) {
4912 __ Addiu(temp_reg, temp_reg, -2);
4913 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4914 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
4915 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4916 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
4917 }
4918 if (num_entries - last_index == 2) {
4919 // The last missing case_value.
4920 __ Addiu(temp_reg, temp_reg, -1);
4921 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004922 }
4923
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004924 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004925 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4926 __ B(codegen_->GetLabelOf(default_block));
4927 }
4928}
4929
4930void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4931 // The trampoline uses the same calling convention as dex calling conventions,
4932 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4933 // the method_idx.
4934 HandleInvoke(invoke);
4935}
4936
4937void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4938 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4939}
4940
4941#undef __
4942#undef QUICK_ENTRY_POINT
4943
4944} // namespace mips
4945} // namespace art