blob: 75f5fb3bab5d70405c2b886d14350773e08bb7b5 [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:
Aart Bik42249c32016-01-07 15:33:50 -0800447 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 : instruction_(instruction) {}
449
450 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800451 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452 __ Bind(GetEntryLabel());
453 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
455 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800456 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 this,
458 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000459 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 }
461
462 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
463
464 private:
Aart Bik42249c32016-01-07 15:33:50 -0800465 HDeoptimize* const instruction_;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
467};
468
469CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
470 const MipsInstructionSetFeatures& isa_features,
471 const CompilerOptions& compiler_options,
472 OptimizingCompilerStats* stats)
473 : CodeGenerator(graph,
474 kNumberOfCoreRegisters,
475 kNumberOfFRegisters,
476 kNumberOfRegisterPairs,
477 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
478 arraysize(kCoreCalleeSaves)),
479 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
480 arraysize(kFpuCalleeSaves)),
481 compiler_options,
482 stats),
483 block_labels_(nullptr),
484 location_builder_(graph, this),
485 instruction_visitor_(graph, this),
486 move_resolver_(graph->GetArena(), this),
487 assembler_(&isa_features),
488 isa_features_(isa_features) {
489 // Save RA (containing the return address) to mimic Quick.
490 AddAllocatedRegister(Location::RegisterLocation(RA));
491}
492
493#undef __
494#define __ down_cast<MipsAssembler*>(GetAssembler())->
495#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
496
497void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
498 // Ensure that we fix up branches.
499 __ FinalizeCode();
500
501 // Adjust native pc offsets in stack maps.
502 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
503 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
504 uint32_t new_position = __ GetAdjustedPosition(old_position);
505 DCHECK_GE(new_position, old_position);
506 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
507 }
508
509 // Adjust pc offsets for the disassembly information.
510 if (disasm_info_ != nullptr) {
511 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
512 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
513 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
514 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
515 it.second.start = __ GetAdjustedPosition(it.second.start);
516 it.second.end = __ GetAdjustedPosition(it.second.end);
517 }
518 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
519 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
520 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
521 }
522 }
523
524 CodeGenerator::Finalize(allocator);
525}
526
527MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
528 return codegen_->GetAssembler();
529}
530
531void ParallelMoveResolverMIPS::EmitMove(size_t index) {
532 DCHECK_LT(index, moves_.size());
533 MoveOperands* move = moves_[index];
534 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
535}
536
537void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
538 DCHECK_LT(index, moves_.size());
539 MoveOperands* move = moves_[index];
540 Primitive::Type type = move->GetType();
541 Location loc1 = move->GetDestination();
542 Location loc2 = move->GetSource();
543
544 DCHECK(!loc1.IsConstant());
545 DCHECK(!loc2.IsConstant());
546
547 if (loc1.Equals(loc2)) {
548 return;
549 }
550
551 if (loc1.IsRegister() && loc2.IsRegister()) {
552 // Swap 2 GPRs.
553 Register r1 = loc1.AsRegister<Register>();
554 Register r2 = loc2.AsRegister<Register>();
555 __ Move(TMP, r2);
556 __ Move(r2, r1);
557 __ Move(r1, TMP);
558 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
559 FRegister f1 = loc1.AsFpuRegister<FRegister>();
560 FRegister f2 = loc2.AsFpuRegister<FRegister>();
561 if (type == Primitive::kPrimFloat) {
562 __ MovS(FTMP, f2);
563 __ MovS(f2, f1);
564 __ MovS(f1, FTMP);
565 } else {
566 DCHECK_EQ(type, Primitive::kPrimDouble);
567 __ MovD(FTMP, f2);
568 __ MovD(f2, f1);
569 __ MovD(f1, FTMP);
570 }
571 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
572 (loc1.IsFpuRegister() && loc2.IsRegister())) {
573 // Swap FPR and GPR.
574 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
575 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
576 : loc2.AsFpuRegister<FRegister>();
577 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
578 : loc2.AsRegister<Register>();
579 __ Move(TMP, r2);
580 __ Mfc1(r2, f1);
581 __ Mtc1(TMP, f1);
582 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
583 // Swap 2 GPR register pairs.
584 Register r1 = loc1.AsRegisterPairLow<Register>();
585 Register r2 = loc2.AsRegisterPairLow<Register>();
586 __ Move(TMP, r2);
587 __ Move(r2, r1);
588 __ Move(r1, TMP);
589 r1 = loc1.AsRegisterPairHigh<Register>();
590 r2 = loc2.AsRegisterPairHigh<Register>();
591 __ Move(TMP, r2);
592 __ Move(r2, r1);
593 __ Move(r1, TMP);
594 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
595 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
596 // Swap FPR and GPR register pair.
597 DCHECK_EQ(type, Primitive::kPrimDouble);
598 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
599 : loc2.AsFpuRegister<FRegister>();
600 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
601 : loc2.AsRegisterPairLow<Register>();
602 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
603 : loc2.AsRegisterPairHigh<Register>();
604 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
605 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
606 // unpredictable and the following mfch1 will fail.
607 __ Mfc1(TMP, f1);
608 __ Mfhc1(AT, f1);
609 __ Mtc1(r2_l, f1);
610 __ Mthc1(r2_h, f1);
611 __ Move(r2_l, TMP);
612 __ Move(r2_h, AT);
613 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
614 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
615 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
617 } else {
618 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
619 }
620}
621
622void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
623 __ Pop(static_cast<Register>(reg));
624}
625
626void ParallelMoveResolverMIPS::SpillScratch(int reg) {
627 __ Push(static_cast<Register>(reg));
628}
629
630void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
631 // Allocate a scratch register other than TMP, if available.
632 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
633 // automatically unspilled when the scratch scope object is destroyed).
634 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
635 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
636 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
637 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
638 __ LoadFromOffset(kLoadWord,
639 Register(ensure_scratch.GetRegister()),
640 SP,
641 index1 + stack_offset);
642 __ LoadFromOffset(kLoadWord,
643 TMP,
644 SP,
645 index2 + stack_offset);
646 __ StoreToOffset(kStoreWord,
647 Register(ensure_scratch.GetRegister()),
648 SP,
649 index2 + stack_offset);
650 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
651 }
652}
653
654static dwarf::Reg DWARFReg(Register reg) {
655 return dwarf::Reg::MipsCore(static_cast<int>(reg));
656}
657
658// TODO: mapping of floating-point registers to DWARF.
659
660void CodeGeneratorMIPS::GenerateFrameEntry() {
661 __ Bind(&frame_entry_label_);
662
663 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
664
665 if (do_overflow_check) {
666 __ LoadFromOffset(kLoadWord,
667 ZERO,
668 SP,
669 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
670 RecordPcInfo(nullptr, 0);
671 }
672
673 if (HasEmptyFrame()) {
674 return;
675 }
676
677 // Make sure the frame size isn't unreasonably large.
678 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
679 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
680 }
681
682 // Spill callee-saved registers.
683 // Note that their cumulative size is small and they can be indexed using
684 // 16-bit offsets.
685
686 // TODO: increment/decrement SP in one step instead of two or remove this comment.
687
688 uint32_t ofs = FrameEntrySpillSize();
689 bool unaligned_float = ofs & 0x7;
690 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
691 __ IncreaseFrameSize(ofs);
692
693 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
694 Register reg = kCoreCalleeSaves[i];
695 if (allocated_registers_.ContainsCoreRegister(reg)) {
696 ofs -= kMipsWordSize;
697 __ Sw(reg, SP, ofs);
698 __ cfi().RelOffset(DWARFReg(reg), ofs);
699 }
700 }
701
702 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
703 FRegister reg = kFpuCalleeSaves[i];
704 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
705 ofs -= kMipsDoublewordSize;
706 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
707 if (unaligned_float) {
708 if (fpu_32bit) {
709 __ Swc1(reg, SP, ofs);
710 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
711 } else {
712 __ Mfhc1(TMP, reg);
713 __ Swc1(reg, SP, ofs);
714 __ Sw(TMP, SP, ofs + 4);
715 }
716 } else {
717 __ Sdc1(reg, SP, ofs);
718 }
719 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
720 }
721 }
722
723 // Allocate the rest of the frame and store the current method pointer
724 // at its end.
725
726 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
727
728 static_assert(IsInt<16>(kCurrentMethodStackOffset),
729 "kCurrentMethodStackOffset must fit into int16_t");
730 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
731}
732
733void CodeGeneratorMIPS::GenerateFrameExit() {
734 __ cfi().RememberState();
735
736 if (!HasEmptyFrame()) {
737 // Deallocate the rest of the frame.
738
739 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
740
741 // Restore callee-saved registers.
742 // Note that their cumulative size is small and they can be indexed using
743 // 16-bit offsets.
744
745 // TODO: increment/decrement SP in one step instead of two or remove this comment.
746
747 uint32_t ofs = 0;
748 bool unaligned_float = FrameEntrySpillSize() & 0x7;
749 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
750
751 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
752 FRegister reg = kFpuCalleeSaves[i];
753 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
754 if (unaligned_float) {
755 if (fpu_32bit) {
756 __ Lwc1(reg, SP, ofs);
757 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
758 } else {
759 __ Lwc1(reg, SP, ofs);
760 __ Lw(TMP, SP, ofs + 4);
761 __ Mthc1(TMP, reg);
762 }
763 } else {
764 __ Ldc1(reg, SP, ofs);
765 }
766 ofs += kMipsDoublewordSize;
767 // TODO: __ cfi().Restore(DWARFReg(reg));
768 }
769 }
770
771 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
772 Register reg = kCoreCalleeSaves[i];
773 if (allocated_registers_.ContainsCoreRegister(reg)) {
774 __ Lw(reg, SP, ofs);
775 ofs += kMipsWordSize;
776 __ cfi().Restore(DWARFReg(reg));
777 }
778 }
779
780 DCHECK_EQ(ofs, FrameEntrySpillSize());
781 __ DecreaseFrameSize(ofs);
782 }
783
784 __ Jr(RA);
785 __ Nop();
786
787 __ cfi().RestoreState();
788 __ cfi().DefCFAOffset(GetFrameSize());
789}
790
791void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
792 __ Bind(GetLabelOf(block));
793}
794
795void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
796 if (src.Equals(dst)) {
797 return;
798 }
799
800 if (src.IsConstant()) {
801 MoveConstant(dst, src.GetConstant());
802 } else {
803 if (Primitive::Is64BitType(dst_type)) {
804 Move64(dst, src);
805 } else {
806 Move32(dst, src);
807 }
808 }
809}
810
811void CodeGeneratorMIPS::Move32(Location destination, Location source) {
812 if (source.Equals(destination)) {
813 return;
814 }
815
816 if (destination.IsRegister()) {
817 if (source.IsRegister()) {
818 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
819 } else if (source.IsFpuRegister()) {
820 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
821 } else {
822 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
823 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
824 }
825 } else if (destination.IsFpuRegister()) {
826 if (source.IsRegister()) {
827 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
828 } else if (source.IsFpuRegister()) {
829 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
833 }
834 } else {
835 DCHECK(destination.IsStackSlot()) << destination;
836 if (source.IsRegister()) {
837 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
838 } else if (source.IsFpuRegister()) {
839 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
840 } else {
841 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
842 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
843 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
844 }
845 }
846}
847
848void CodeGeneratorMIPS::Move64(Location destination, Location source) {
849 if (source.Equals(destination)) {
850 return;
851 }
852
853 if (destination.IsRegisterPair()) {
854 if (source.IsRegisterPair()) {
855 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
856 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
857 } else if (source.IsFpuRegister()) {
858 Register dst_high = destination.AsRegisterPairHigh<Register>();
859 Register dst_low = destination.AsRegisterPairLow<Register>();
860 FRegister src = source.AsFpuRegister<FRegister>();
861 __ Mfc1(dst_low, src);
862 __ Mfhc1(dst_high, src);
863 } else {
864 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
865 int32_t off = source.GetStackIndex();
866 Register r = destination.AsRegisterPairLow<Register>();
867 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
868 }
869 } else if (destination.IsFpuRegister()) {
870 if (source.IsRegisterPair()) {
871 FRegister dst = destination.AsFpuRegister<FRegister>();
872 Register src_high = source.AsRegisterPairHigh<Register>();
873 Register src_low = source.AsRegisterPairLow<Register>();
874 __ Mtc1(src_low, dst);
875 __ Mthc1(src_high, dst);
876 } else if (source.IsFpuRegister()) {
877 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
878 } else {
879 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
880 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
881 }
882 } else {
883 DCHECK(destination.IsDoubleStackSlot()) << destination;
884 int32_t off = destination.GetStackIndex();
885 if (source.IsRegisterPair()) {
886 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
887 } else if (source.IsFpuRegister()) {
888 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
889 } else {
890 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
891 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
892 __ StoreToOffset(kStoreWord, TMP, SP, off);
893 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
894 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
895 }
896 }
897}
898
899void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
900 if (c->IsIntConstant() || c->IsNullConstant()) {
901 // Move 32 bit constant.
902 int32_t value = GetInt32ValueOf(c);
903 if (destination.IsRegister()) {
904 Register dst = destination.AsRegister<Register>();
905 __ LoadConst32(dst, value);
906 } else {
907 DCHECK(destination.IsStackSlot())
908 << "Cannot move " << c->DebugName() << " to " << destination;
909 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
910 }
911 } else if (c->IsLongConstant()) {
912 // Move 64 bit constant.
913 int64_t value = GetInt64ValueOf(c);
914 if (destination.IsRegisterPair()) {
915 Register r_h = destination.AsRegisterPairHigh<Register>();
916 Register r_l = destination.AsRegisterPairLow<Register>();
917 __ LoadConst64(r_h, r_l, value);
918 } else {
919 DCHECK(destination.IsDoubleStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
921 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
922 }
923 } else if (c->IsFloatConstant()) {
924 // Move 32 bit float constant.
925 int32_t value = GetInt32ValueOf(c);
926 if (destination.IsFpuRegister()) {
927 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
928 } else {
929 DCHECK(destination.IsStackSlot())
930 << "Cannot move " << c->DebugName() << " to " << destination;
931 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
932 }
933 } else {
934 // Move 64 bit double constant.
935 DCHECK(c->IsDoubleConstant()) << c->DebugName();
936 int64_t value = GetInt64ValueOf(c);
937 if (destination.IsFpuRegister()) {
938 FRegister fd = destination.AsFpuRegister<FRegister>();
939 __ LoadDConst64(fd, value, TMP);
940 } else {
941 DCHECK(destination.IsDoubleStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
943 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
944 }
945 }
946}
947
948void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
949 DCHECK(destination.IsRegister());
950 Register dst = destination.AsRegister<Register>();
951 __ LoadConst32(dst, value);
952}
953
954void CodeGeneratorMIPS::Move(HInstruction* instruction,
955 Location location,
956 HInstruction* move_for) {
957 LocationSummary* locations = instruction->GetLocations();
958 Primitive::Type type = instruction->GetType();
959 DCHECK_NE(type, Primitive::kPrimVoid);
960
961 if (instruction->IsCurrentMethod()) {
962 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
963 } else if (locations != nullptr && locations->Out().Equals(location)) {
964 return;
965 } else if (instruction->IsIntConstant()
966 || instruction->IsLongConstant()
967 || instruction->IsNullConstant()) {
968 MoveConstant(location, instruction->AsConstant());
969 } else if (instruction->IsTemporary()) {
970 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
971 if (temp_location.IsStackSlot()) {
972 Move32(location, temp_location);
973 } else {
974 DCHECK(temp_location.IsDoubleStackSlot());
975 Move64(location, temp_location);
976 }
977 } else if (instruction->IsLoadLocal()) {
978 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
979 if (Primitive::Is64BitType(type)) {
980 Move64(location, Location::DoubleStackSlot(stack_slot));
981 } else {
982 Move32(location, Location::StackSlot(stack_slot));
983 }
984 } else {
985 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
986 if (Primitive::Is64BitType(type)) {
987 Move64(location, locations->Out());
988 } else {
989 Move32(location, locations->Out());
990 }
991 }
992}
993
994void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
995 if (location.IsRegister()) {
996 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700997 } else if (location.IsRegisterPair()) {
998 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
999 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001000 } else {
1001 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1002 }
1003}
1004
1005Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1006 Primitive::Type type = load->GetType();
1007
1008 switch (type) {
1009 case Primitive::kPrimNot:
1010 case Primitive::kPrimInt:
1011 case Primitive::kPrimFloat:
1012 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1013
1014 case Primitive::kPrimLong:
1015 case Primitive::kPrimDouble:
1016 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1017
1018 case Primitive::kPrimBoolean:
1019 case Primitive::kPrimByte:
1020 case Primitive::kPrimChar:
1021 case Primitive::kPrimShort:
1022 case Primitive::kPrimVoid:
1023 LOG(FATAL) << "Unexpected type " << type;
1024 }
1025
1026 LOG(FATAL) << "Unreachable";
1027 return Location::NoLocation();
1028}
1029
1030void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1031 MipsLabel done;
1032 Register card = AT;
1033 Register temp = TMP;
1034 __ Beqz(value, &done);
1035 __ LoadFromOffset(kLoadWord,
1036 card,
1037 TR,
1038 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1039 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1040 __ Addu(temp, card, temp);
1041 __ Sb(card, temp, 0);
1042 __ Bind(&done);
1043}
1044
1045void CodeGeneratorMIPS::SetupBlockedRegisters(bool is_baseline) const {
1046 // Don't allocate the dalvik style register pair passing.
1047 blocked_register_pairs_[A1_A2] = true;
1048
1049 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1050 blocked_core_registers_[ZERO] = true;
1051 blocked_core_registers_[K0] = true;
1052 blocked_core_registers_[K1] = true;
1053 blocked_core_registers_[GP] = true;
1054 blocked_core_registers_[SP] = true;
1055 blocked_core_registers_[RA] = true;
1056
1057 // AT and TMP(T8) are used as temporary/scratch registers
1058 // (similar to how AT is used by MIPS assemblers).
1059 blocked_core_registers_[AT] = true;
1060 blocked_core_registers_[TMP] = true;
1061 blocked_fpu_registers_[FTMP] = true;
1062
1063 // Reserve suspend and thread registers.
1064 blocked_core_registers_[S0] = true;
1065 blocked_core_registers_[TR] = true;
1066
1067 // Reserve T9 for function calls
1068 blocked_core_registers_[T9] = true;
1069
1070 // Reserve odd-numbered FPU registers.
1071 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1072 blocked_fpu_registers_[i] = true;
1073 }
1074
1075 if (is_baseline) {
1076 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1077 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1078 }
1079
1080 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1081 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1082 }
1083 }
1084
1085 UpdateBlockedPairRegisters();
1086}
1087
1088void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1089 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1090 MipsManagedRegister current =
1091 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1092 if (blocked_core_registers_[current.AsRegisterPairLow()]
1093 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1094 blocked_register_pairs_[i] = true;
1095 }
1096 }
1097}
1098
1099Location CodeGeneratorMIPS::AllocateFreeRegister(Primitive::Type type) const {
1100 switch (type) {
1101 case Primitive::kPrimLong: {
1102 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
1103 MipsManagedRegister pair =
1104 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
1105 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
1106 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
1107
1108 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
1109 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
1110 UpdateBlockedPairRegisters();
1111 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
1112 }
1113
1114 case Primitive::kPrimByte:
1115 case Primitive::kPrimBoolean:
1116 case Primitive::kPrimChar:
1117 case Primitive::kPrimShort:
1118 case Primitive::kPrimInt:
1119 case Primitive::kPrimNot: {
1120 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
1121 // Block all register pairs that contain `reg`.
1122 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1123 MipsManagedRegister current =
1124 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1125 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
1126 blocked_register_pairs_[i] = true;
1127 }
1128 }
1129 return Location::RegisterLocation(reg);
1130 }
1131
1132 case Primitive::kPrimFloat:
1133 case Primitive::kPrimDouble: {
1134 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFRegisters);
1135 return Location::FpuRegisterLocation(reg);
1136 }
1137
1138 case Primitive::kPrimVoid:
1139 LOG(FATAL) << "Unreachable type " << type;
1140 }
1141
1142 UNREACHABLE();
1143}
1144
1145size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1146 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1147 return kMipsWordSize;
1148}
1149
1150size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1151 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1152 return kMipsWordSize;
1153}
1154
1155size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1156 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1157 return kMipsDoublewordSize;
1158}
1159
1160size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1162 return kMipsDoublewordSize;
1163}
1164
1165void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1166 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1167}
1168
1169void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1170 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1171}
1172
1173void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1174 HInstruction* instruction,
1175 uint32_t dex_pc,
1176 SlowPathCode* slow_path) {
1177 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1178 instruction,
1179 dex_pc,
1180 slow_path,
1181 IsDirectEntrypoint(entrypoint));
1182}
1183
1184constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1185
1186void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1187 HInstruction* instruction,
1188 uint32_t dex_pc,
1189 SlowPathCode* slow_path,
1190 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001191 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1192 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001193 if (is_direct_entrypoint) {
1194 // Reserve argument space on stack (for $a0-$a3) for
1195 // entrypoints that directly reference native implementations.
1196 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001197 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001198 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001199 } else {
1200 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001201 }
1202 RecordPcInfo(instruction, dex_pc, slow_path);
1203}
1204
1205void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1206 Register class_reg) {
1207 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1208 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1209 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1210 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1211 __ Sync(0);
1212 __ Bind(slow_path->GetExitLabel());
1213}
1214
1215void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1216 __ Sync(0); // Only stype 0 is supported.
1217}
1218
1219void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1220 HBasicBlock* successor) {
1221 SuspendCheckSlowPathMIPS* slow_path =
1222 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1223 codegen_->AddSlowPath(slow_path);
1224
1225 __ LoadFromOffset(kLoadUnsignedHalfword,
1226 TMP,
1227 TR,
1228 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1229 if (successor == nullptr) {
1230 __ Bnez(TMP, slow_path->GetEntryLabel());
1231 __ Bind(slow_path->GetReturnLabel());
1232 } else {
1233 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1234 __ B(slow_path->GetEntryLabel());
1235 // slow_path will return to GetLabelOf(successor).
1236 }
1237}
1238
1239InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1240 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001241 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242 assembler_(codegen->GetAssembler()),
1243 codegen_(codegen) {}
1244
1245void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1246 DCHECK_EQ(instruction->InputCount(), 2U);
1247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1248 Primitive::Type type = instruction->GetResultType();
1249 switch (type) {
1250 case Primitive::kPrimInt: {
1251 locations->SetInAt(0, Location::RequiresRegister());
1252 HInstruction* right = instruction->InputAt(1);
1253 bool can_use_imm = false;
1254 if (right->IsConstant()) {
1255 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1256 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1257 can_use_imm = IsUint<16>(imm);
1258 } else if (instruction->IsAdd()) {
1259 can_use_imm = IsInt<16>(imm);
1260 } else {
1261 DCHECK(instruction->IsSub());
1262 can_use_imm = IsInt<16>(-imm);
1263 }
1264 }
1265 if (can_use_imm)
1266 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1267 else
1268 locations->SetInAt(1, Location::RequiresRegister());
1269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1270 break;
1271 }
1272
1273 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001275 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001277 break;
1278 }
1279
1280 case Primitive::kPrimFloat:
1281 case Primitive::kPrimDouble:
1282 DCHECK(instruction->IsAdd() || instruction->IsSub());
1283 locations->SetInAt(0, Location::RequiresFpuRegister());
1284 locations->SetInAt(1, Location::RequiresFpuRegister());
1285 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1286 break;
1287
1288 default:
1289 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1290 }
1291}
1292
1293void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1294 Primitive::Type type = instruction->GetType();
1295 LocationSummary* locations = instruction->GetLocations();
1296
1297 switch (type) {
1298 case Primitive::kPrimInt: {
1299 Register dst = locations->Out().AsRegister<Register>();
1300 Register lhs = locations->InAt(0).AsRegister<Register>();
1301 Location rhs_location = locations->InAt(1);
1302
1303 Register rhs_reg = ZERO;
1304 int32_t rhs_imm = 0;
1305 bool use_imm = rhs_location.IsConstant();
1306 if (use_imm) {
1307 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1308 } else {
1309 rhs_reg = rhs_location.AsRegister<Register>();
1310 }
1311
1312 if (instruction->IsAnd()) {
1313 if (use_imm)
1314 __ Andi(dst, lhs, rhs_imm);
1315 else
1316 __ And(dst, lhs, rhs_reg);
1317 } else if (instruction->IsOr()) {
1318 if (use_imm)
1319 __ Ori(dst, lhs, rhs_imm);
1320 else
1321 __ Or(dst, lhs, rhs_reg);
1322 } else if (instruction->IsXor()) {
1323 if (use_imm)
1324 __ Xori(dst, lhs, rhs_imm);
1325 else
1326 __ Xor(dst, lhs, rhs_reg);
1327 } else if (instruction->IsAdd()) {
1328 if (use_imm)
1329 __ Addiu(dst, lhs, rhs_imm);
1330 else
1331 __ Addu(dst, lhs, rhs_reg);
1332 } else {
1333 DCHECK(instruction->IsSub());
1334 if (use_imm)
1335 __ Addiu(dst, lhs, -rhs_imm);
1336 else
1337 __ Subu(dst, lhs, rhs_reg);
1338 }
1339 break;
1340 }
1341
1342 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001343 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1344 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1345 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1346 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001347 Location rhs_location = locations->InAt(1);
1348 bool use_imm = rhs_location.IsConstant();
1349 if (!use_imm) {
1350 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1351 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1352 if (instruction->IsAnd()) {
1353 __ And(dst_low, lhs_low, rhs_low);
1354 __ And(dst_high, lhs_high, rhs_high);
1355 } else if (instruction->IsOr()) {
1356 __ Or(dst_low, lhs_low, rhs_low);
1357 __ Or(dst_high, lhs_high, rhs_high);
1358 } else if (instruction->IsXor()) {
1359 __ Xor(dst_low, lhs_low, rhs_low);
1360 __ Xor(dst_high, lhs_high, rhs_high);
1361 } else if (instruction->IsAdd()) {
1362 if (lhs_low == rhs_low) {
1363 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1364 __ Slt(TMP, lhs_low, ZERO);
1365 __ Addu(dst_low, lhs_low, rhs_low);
1366 } else {
1367 __ Addu(dst_low, lhs_low, rhs_low);
1368 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1369 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1370 }
1371 __ Addu(dst_high, lhs_high, rhs_high);
1372 __ Addu(dst_high, dst_high, TMP);
1373 } else {
1374 DCHECK(instruction->IsSub());
1375 __ Sltu(TMP, lhs_low, rhs_low);
1376 __ Subu(dst_low, lhs_low, rhs_low);
1377 __ Subu(dst_high, lhs_high, rhs_high);
1378 __ Subu(dst_high, dst_high, TMP);
1379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001380 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001381 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1382 if (instruction->IsOr()) {
1383 uint32_t low = Low32Bits(value);
1384 uint32_t high = High32Bits(value);
1385 if (IsUint<16>(low)) {
1386 if (dst_low != lhs_low || low != 0) {
1387 __ Ori(dst_low, lhs_low, low);
1388 }
1389 } else {
1390 __ LoadConst32(TMP, low);
1391 __ Or(dst_low, lhs_low, TMP);
1392 }
1393 if (IsUint<16>(high)) {
1394 if (dst_high != lhs_high || high != 0) {
1395 __ Ori(dst_high, lhs_high, high);
1396 }
1397 } else {
1398 if (high != low) {
1399 __ LoadConst32(TMP, high);
1400 }
1401 __ Or(dst_high, lhs_high, TMP);
1402 }
1403 } else if (instruction->IsXor()) {
1404 uint32_t low = Low32Bits(value);
1405 uint32_t high = High32Bits(value);
1406 if (IsUint<16>(low)) {
1407 if (dst_low != lhs_low || low != 0) {
1408 __ Xori(dst_low, lhs_low, low);
1409 }
1410 } else {
1411 __ LoadConst32(TMP, low);
1412 __ Xor(dst_low, lhs_low, TMP);
1413 }
1414 if (IsUint<16>(high)) {
1415 if (dst_high != lhs_high || high != 0) {
1416 __ Xori(dst_high, lhs_high, high);
1417 }
1418 } else {
1419 if (high != low) {
1420 __ LoadConst32(TMP, high);
1421 }
1422 __ Xor(dst_high, lhs_high, TMP);
1423 }
1424 } else if (instruction->IsAnd()) {
1425 uint32_t low = Low32Bits(value);
1426 uint32_t high = High32Bits(value);
1427 if (IsUint<16>(low)) {
1428 __ Andi(dst_low, lhs_low, low);
1429 } else if (low != 0xFFFFFFFF) {
1430 __ LoadConst32(TMP, low);
1431 __ And(dst_low, lhs_low, TMP);
1432 } else if (dst_low != lhs_low) {
1433 __ Move(dst_low, lhs_low);
1434 }
1435 if (IsUint<16>(high)) {
1436 __ Andi(dst_high, lhs_high, high);
1437 } else if (high != 0xFFFFFFFF) {
1438 if (high != low) {
1439 __ LoadConst32(TMP, high);
1440 }
1441 __ And(dst_high, lhs_high, TMP);
1442 } else if (dst_high != lhs_high) {
1443 __ Move(dst_high, lhs_high);
1444 }
1445 } else {
1446 if (instruction->IsSub()) {
1447 value = -value;
1448 } else {
1449 DCHECK(instruction->IsAdd());
1450 }
1451 int32_t low = Low32Bits(value);
1452 int32_t high = High32Bits(value);
1453 if (IsInt<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Addiu(dst_low, lhs_low, low);
1456 }
1457 if (low != 0) {
1458 __ Sltiu(AT, dst_low, low);
1459 }
1460 } else {
1461 __ LoadConst32(TMP, low);
1462 __ Addu(dst_low, lhs_low, TMP);
1463 __ Sltu(AT, dst_low, TMP);
1464 }
1465 if (IsInt<16>(high)) {
1466 if (dst_high != lhs_high || high != 0) {
1467 __ Addiu(dst_high, lhs_high, high);
1468 }
1469 } else {
1470 if (high != low) {
1471 __ LoadConst32(TMP, high);
1472 }
1473 __ Addu(dst_high, lhs_high, TMP);
1474 }
1475 if (low != 0) {
1476 __ Addu(dst_high, dst_high, AT);
1477 }
1478 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 }
1480 break;
1481 }
1482
1483 case Primitive::kPrimFloat:
1484 case Primitive::kPrimDouble: {
1485 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1486 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1487 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1488 if (instruction->IsAdd()) {
1489 if (type == Primitive::kPrimFloat) {
1490 __ AddS(dst, lhs, rhs);
1491 } else {
1492 __ AddD(dst, lhs, rhs);
1493 }
1494 } else {
1495 DCHECK(instruction->IsSub());
1496 if (type == Primitive::kPrimFloat) {
1497 __ SubS(dst, lhs, rhs);
1498 } else {
1499 __ SubD(dst, lhs, rhs);
1500 }
1501 }
1502 break;
1503 }
1504
1505 default:
1506 LOG(FATAL) << "Unexpected binary operation type " << type;
1507 }
1508}
1509
1510void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001511 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001512
1513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1514 Primitive::Type type = instr->GetResultType();
1515 switch (type) {
1516 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001517 locations->SetInAt(0, Location::RequiresRegister());
1518 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1520 break;
1521 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1524 locations->SetOut(Location::RequiresRegister());
1525 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 default:
1527 LOG(FATAL) << "Unexpected shift type " << type;
1528 }
1529}
1530
1531static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1532
1533void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001534 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001535 LocationSummary* locations = instr->GetLocations();
1536 Primitive::Type type = instr->GetType();
1537
1538 Location rhs_location = locations->InAt(1);
1539 bool use_imm = rhs_location.IsConstant();
1540 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1541 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
1542 uint32_t shift_mask = (type == Primitive::kPrimInt) ? kMaxIntShiftValue : kMaxLongShiftValue;
1543 uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001544 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1545 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001546
1547 switch (type) {
1548 case Primitive::kPrimInt: {
1549 Register dst = locations->Out().AsRegister<Register>();
1550 Register lhs = locations->InAt(0).AsRegister<Register>();
1551 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001552 if (shift_value == 0) {
1553 if (dst != lhs) {
1554 __ Move(dst, lhs);
1555 }
1556 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001557 __ Sll(dst, lhs, shift_value);
1558 } else if (instr->IsShr()) {
1559 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001560 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001561 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001562 } else {
1563 if (has_ins_rotr) {
1564 __ Rotr(dst, lhs, shift_value);
1565 } else {
1566 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1567 __ Srl(dst, lhs, shift_value);
1568 __ Or(dst, dst, TMP);
1569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570 }
1571 } else {
1572 if (instr->IsShl()) {
1573 __ Sllv(dst, lhs, rhs_reg);
1574 } else if (instr->IsShr()) {
1575 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001576 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001577 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001578 } else {
1579 if (has_ins_rotr) {
1580 __ Rotrv(dst, lhs, rhs_reg);
1581 } else {
1582 __ Subu(TMP, ZERO, rhs_reg);
1583 __ Sllv(TMP, lhs, TMP);
1584 __ Srlv(dst, lhs, rhs_reg);
1585 __ Or(dst, dst, TMP);
1586 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001587 }
1588 }
1589 break;
1590 }
1591
1592 case Primitive::kPrimLong: {
1593 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1594 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1595 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1596 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1597 if (use_imm) {
1598 if (shift_value == 0) {
1599 codegen_->Move64(locations->Out(), locations->InAt(0));
1600 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001601 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001602 if (instr->IsShl()) {
1603 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1604 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1605 __ Sll(dst_low, lhs_low, shift_value);
1606 } else if (instr->IsShr()) {
1607 __ Srl(dst_low, lhs_low, shift_value);
1608 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1609 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001610 } else if (instr->IsUShr()) {
1611 __ Srl(dst_low, lhs_low, shift_value);
1612 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1613 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001614 } else {
1615 __ Srl(dst_low, lhs_low, shift_value);
1616 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1617 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001620 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001621 if (instr->IsShl()) {
1622 __ Sll(dst_low, lhs_low, shift_value);
1623 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1624 __ Sll(dst_high, lhs_high, shift_value);
1625 __ Or(dst_high, dst_high, TMP);
1626 } else if (instr->IsShr()) {
1627 __ Sra(dst_high, lhs_high, shift_value);
1628 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1629 __ Srl(dst_low, lhs_low, shift_value);
1630 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001632 __ Srl(dst_high, lhs_high, shift_value);
1633 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1634 __ Srl(dst_low, lhs_low, shift_value);
1635 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001636 } else {
1637 __ Srl(TMP, lhs_low, shift_value);
1638 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1639 __ Or(dst_low, dst_low, TMP);
1640 __ Srl(TMP, lhs_high, shift_value);
1641 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1642 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001643 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001644 }
1645 } else {
1646 shift_value -= kMipsBitsPerWord;
1647 if (instr->IsShl()) {
1648 __ Sll(dst_high, lhs_low, shift_value);
1649 __ Move(dst_low, ZERO);
1650 } else if (instr->IsShr()) {
1651 __ Sra(dst_low, lhs_high, shift_value);
1652 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001653 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001654 __ Srl(dst_low, lhs_high, shift_value);
1655 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001656 } else {
1657 if (shift_value == 0) {
1658 // 64-bit rotation by 32 is just a swap.
1659 __ Move(dst_low, lhs_high);
1660 __ Move(dst_high, lhs_low);
1661 } else {
1662 if (has_ins_rotr) {
1663 __ Srl(dst_low, lhs_high, shift_value);
1664 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
1665 __ Srl(dst_high, lhs_low, shift_value);
1666 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1667 } else {
1668 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1669 __ Srl(dst_low, lhs_high, shift_value);
1670 __ Or(dst_low, dst_low, TMP);
1671 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1672 __ Srl(dst_high, lhs_low, shift_value);
1673 __ Or(dst_high, dst_high, TMP);
1674 }
1675 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001676 }
1677 }
1678 } else {
1679 MipsLabel done;
1680 if (instr->IsShl()) {
1681 __ Sllv(dst_low, lhs_low, rhs_reg);
1682 __ Nor(AT, ZERO, rhs_reg);
1683 __ Srl(TMP, lhs_low, 1);
1684 __ Srlv(TMP, TMP, AT);
1685 __ Sllv(dst_high, lhs_high, rhs_reg);
1686 __ Or(dst_high, dst_high, TMP);
1687 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1688 __ Beqz(TMP, &done);
1689 __ Move(dst_high, dst_low);
1690 __ Move(dst_low, ZERO);
1691 } else if (instr->IsShr()) {
1692 __ Srav(dst_high, lhs_high, rhs_reg);
1693 __ Nor(AT, ZERO, rhs_reg);
1694 __ Sll(TMP, lhs_high, 1);
1695 __ Sllv(TMP, TMP, AT);
1696 __ Srlv(dst_low, lhs_low, rhs_reg);
1697 __ Or(dst_low, dst_low, TMP);
1698 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1699 __ Beqz(TMP, &done);
1700 __ Move(dst_low, dst_high);
1701 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001702 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001703 __ Srlv(dst_high, lhs_high, rhs_reg);
1704 __ Nor(AT, ZERO, rhs_reg);
1705 __ Sll(TMP, lhs_high, 1);
1706 __ Sllv(TMP, TMP, AT);
1707 __ Srlv(dst_low, lhs_low, rhs_reg);
1708 __ Or(dst_low, dst_low, TMP);
1709 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1710 __ Beqz(TMP, &done);
1711 __ Move(dst_low, dst_high);
1712 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001713 } else {
1714 __ Nor(AT, ZERO, rhs_reg);
1715 __ Srlv(TMP, lhs_low, rhs_reg);
1716 __ Sll(dst_low, lhs_high, 1);
1717 __ Sllv(dst_low, dst_low, AT);
1718 __ Or(dst_low, dst_low, TMP);
1719 __ Srlv(TMP, lhs_high, rhs_reg);
1720 __ Sll(dst_high, lhs_low, 1);
1721 __ Sllv(dst_high, dst_high, AT);
1722 __ Or(dst_high, dst_high, TMP);
1723 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1724 __ Beqz(TMP, &done);
1725 __ Move(TMP, dst_high);
1726 __ Move(dst_high, dst_low);
1727 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001728 }
1729 __ Bind(&done);
1730 }
1731 break;
1732 }
1733
1734 default:
1735 LOG(FATAL) << "Unexpected shift operation type " << type;
1736 }
1737}
1738
1739void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1740 HandleBinaryOp(instruction);
1741}
1742
1743void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1744 HandleBinaryOp(instruction);
1745}
1746
1747void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1748 HandleBinaryOp(instruction);
1749}
1750
1751void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1752 HandleBinaryOp(instruction);
1753}
1754
1755void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1756 LocationSummary* locations =
1757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1758 locations->SetInAt(0, Location::RequiresRegister());
1759 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1760 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1761 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1762 } else {
1763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1764 }
1765}
1766
1767void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1768 LocationSummary* locations = instruction->GetLocations();
1769 Register obj = locations->InAt(0).AsRegister<Register>();
1770 Location index = locations->InAt(1);
1771 Primitive::Type type = instruction->GetType();
1772
1773 switch (type) {
1774 case Primitive::kPrimBoolean: {
1775 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1776 Register out = locations->Out().AsRegister<Register>();
1777 if (index.IsConstant()) {
1778 size_t offset =
1779 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1780 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1781 } else {
1782 __ Addu(TMP, obj, index.AsRegister<Register>());
1783 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1784 }
1785 break;
1786 }
1787
1788 case Primitive::kPrimByte: {
1789 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1790 Register out = locations->Out().AsRegister<Register>();
1791 if (index.IsConstant()) {
1792 size_t offset =
1793 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1794 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1795 } else {
1796 __ Addu(TMP, obj, index.AsRegister<Register>());
1797 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1798 }
1799 break;
1800 }
1801
1802 case Primitive::kPrimShort: {
1803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1804 Register out = locations->Out().AsRegister<Register>();
1805 if (index.IsConstant()) {
1806 size_t offset =
1807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1808 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1809 } else {
1810 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1811 __ Addu(TMP, obj, TMP);
1812 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1813 }
1814 break;
1815 }
1816
1817 case Primitive::kPrimChar: {
1818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1819 Register out = locations->Out().AsRegister<Register>();
1820 if (index.IsConstant()) {
1821 size_t offset =
1822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1823 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1824 } else {
1825 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1826 __ Addu(TMP, obj, TMP);
1827 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1828 }
1829 break;
1830 }
1831
1832 case Primitive::kPrimInt:
1833 case Primitive::kPrimNot: {
1834 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1835 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1836 Register out = locations->Out().AsRegister<Register>();
1837 if (index.IsConstant()) {
1838 size_t offset =
1839 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1840 __ LoadFromOffset(kLoadWord, out, obj, offset);
1841 } else {
1842 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1843 __ Addu(TMP, obj, TMP);
1844 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1845 }
1846 break;
1847 }
1848
1849 case Primitive::kPrimLong: {
1850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1851 Register out = locations->Out().AsRegisterPairLow<Register>();
1852 if (index.IsConstant()) {
1853 size_t offset =
1854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1855 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1856 } else {
1857 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1858 __ Addu(TMP, obj, TMP);
1859 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1860 }
1861 break;
1862 }
1863
1864 case Primitive::kPrimFloat: {
1865 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1866 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1867 if (index.IsConstant()) {
1868 size_t offset =
1869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1870 __ LoadSFromOffset(out, obj, offset);
1871 } else {
1872 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1873 __ Addu(TMP, obj, TMP);
1874 __ LoadSFromOffset(out, TMP, data_offset);
1875 }
1876 break;
1877 }
1878
1879 case Primitive::kPrimDouble: {
1880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1881 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1882 if (index.IsConstant()) {
1883 size_t offset =
1884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1885 __ LoadDFromOffset(out, obj, offset);
1886 } else {
1887 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1888 __ Addu(TMP, obj, TMP);
1889 __ LoadDFromOffset(out, TMP, data_offset);
1890 }
1891 break;
1892 }
1893
1894 case Primitive::kPrimVoid:
1895 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1896 UNREACHABLE();
1897 }
1898 codegen_->MaybeRecordImplicitNullCheck(instruction);
1899}
1900
1901void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1903 locations->SetInAt(0, Location::RequiresRegister());
1904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1905}
1906
1907void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1908 LocationSummary* locations = instruction->GetLocations();
1909 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1910 Register obj = locations->InAt(0).AsRegister<Register>();
1911 Register out = locations->Out().AsRegister<Register>();
1912 __ LoadFromOffset(kLoadWord, out, obj, offset);
1913 codegen_->MaybeRecordImplicitNullCheck(instruction);
1914}
1915
1916void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001917 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1919 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001920 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1921 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001922 InvokeRuntimeCallingConvention calling_convention;
1923 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1924 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1925 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1926 } else {
1927 locations->SetInAt(0, Location::RequiresRegister());
1928 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1929 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1930 locations->SetInAt(2, Location::RequiresFpuRegister());
1931 } else {
1932 locations->SetInAt(2, Location::RequiresRegister());
1933 }
1934 }
1935}
1936
1937void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1938 LocationSummary* locations = instruction->GetLocations();
1939 Register obj = locations->InAt(0).AsRegister<Register>();
1940 Location index = locations->InAt(1);
1941 Primitive::Type value_type = instruction->GetComponentType();
1942 bool needs_runtime_call = locations->WillCall();
1943 bool needs_write_barrier =
1944 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1945
1946 switch (value_type) {
1947 case Primitive::kPrimBoolean:
1948 case Primitive::kPrimByte: {
1949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1950 Register value = locations->InAt(2).AsRegister<Register>();
1951 if (index.IsConstant()) {
1952 size_t offset =
1953 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1954 __ StoreToOffset(kStoreByte, value, obj, offset);
1955 } else {
1956 __ Addu(TMP, obj, index.AsRegister<Register>());
1957 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1958 }
1959 break;
1960 }
1961
1962 case Primitive::kPrimShort:
1963 case Primitive::kPrimChar: {
1964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1965 Register value = locations->InAt(2).AsRegister<Register>();
1966 if (index.IsConstant()) {
1967 size_t offset =
1968 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1969 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1970 } else {
1971 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1972 __ Addu(TMP, obj, TMP);
1973 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1974 }
1975 break;
1976 }
1977
1978 case Primitive::kPrimInt:
1979 case Primitive::kPrimNot: {
1980 if (!needs_runtime_call) {
1981 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1982 Register value = locations->InAt(2).AsRegister<Register>();
1983 if (index.IsConstant()) {
1984 size_t offset =
1985 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1986 __ StoreToOffset(kStoreWord, value, obj, offset);
1987 } else {
1988 DCHECK(index.IsRegister()) << index;
1989 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1990 __ Addu(TMP, obj, TMP);
1991 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1992 }
1993 codegen_->MaybeRecordImplicitNullCheck(instruction);
1994 if (needs_write_barrier) {
1995 DCHECK_EQ(value_type, Primitive::kPrimNot);
1996 codegen_->MarkGCCard(obj, value);
1997 }
1998 } else {
1999 DCHECK_EQ(value_type, Primitive::kPrimNot);
2000 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
2001 instruction,
2002 instruction->GetDexPc(),
2003 nullptr,
2004 IsDirectEntrypoint(kQuickAputObject));
2005 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2006 }
2007 break;
2008 }
2009
2010 case Primitive::kPrimLong: {
2011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
2012 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
2013 if (index.IsConstant()) {
2014 size_t offset =
2015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2016 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
2017 } else {
2018 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2019 __ Addu(TMP, obj, TMP);
2020 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
2021 }
2022 break;
2023 }
2024
2025 case Primitive::kPrimFloat: {
2026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2027 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2028 DCHECK(locations->InAt(2).IsFpuRegister());
2029 if (index.IsConstant()) {
2030 size_t offset =
2031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2032 __ StoreSToOffset(value, obj, offset);
2033 } else {
2034 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
2035 __ Addu(TMP, obj, TMP);
2036 __ StoreSToOffset(value, TMP, data_offset);
2037 }
2038 break;
2039 }
2040
2041 case Primitive::kPrimDouble: {
2042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2043 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
2044 DCHECK(locations->InAt(2).IsFpuRegister());
2045 if (index.IsConstant()) {
2046 size_t offset =
2047 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2048 __ StoreDToOffset(value, obj, offset);
2049 } else {
2050 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
2051 __ Addu(TMP, obj, TMP);
2052 __ StoreDToOffset(value, TMP, data_offset);
2053 }
2054 break;
2055 }
2056
2057 case Primitive::kPrimVoid:
2058 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2059 UNREACHABLE();
2060 }
2061
2062 // Ints and objects are handled in the switch.
2063 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2064 codegen_->MaybeRecordImplicitNullCheck(instruction);
2065 }
2066}
2067
2068void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2069 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2070 ? LocationSummary::kCallOnSlowPath
2071 : LocationSummary::kNoCall;
2072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2073 locations->SetInAt(0, Location::RequiresRegister());
2074 locations->SetInAt(1, Location::RequiresRegister());
2075 if (instruction->HasUses()) {
2076 locations->SetOut(Location::SameAsFirstInput());
2077 }
2078}
2079
2080void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2081 LocationSummary* locations = instruction->GetLocations();
2082 BoundsCheckSlowPathMIPS* slow_path =
2083 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2084 codegen_->AddSlowPath(slow_path);
2085
2086 Register index = locations->InAt(0).AsRegister<Register>();
2087 Register length = locations->InAt(1).AsRegister<Register>();
2088
2089 // length is limited by the maximum positive signed 32-bit integer.
2090 // Unsigned comparison of length and index checks for index < 0
2091 // and for length <= index simultaneously.
2092 __ Bgeu(index, length, slow_path->GetEntryLabel());
2093}
2094
2095void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2096 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2097 instruction,
2098 LocationSummary::kCallOnSlowPath);
2099 locations->SetInAt(0, Location::RequiresRegister());
2100 locations->SetInAt(1, Location::RequiresRegister());
2101 // Note that TypeCheckSlowPathMIPS uses this register too.
2102 locations->AddTemp(Location::RequiresRegister());
2103}
2104
2105void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2106 LocationSummary* locations = instruction->GetLocations();
2107 Register obj = locations->InAt(0).AsRegister<Register>();
2108 Register cls = locations->InAt(1).AsRegister<Register>();
2109 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2110
2111 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2112 codegen_->AddSlowPath(slow_path);
2113
2114 // TODO: avoid this check if we know obj is not null.
2115 __ Beqz(obj, slow_path->GetExitLabel());
2116 // Compare the class of `obj` with `cls`.
2117 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2118 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2119 __ Bind(slow_path->GetExitLabel());
2120}
2121
2122void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2123 LocationSummary* locations =
2124 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2125 locations->SetInAt(0, Location::RequiresRegister());
2126 if (check->HasUses()) {
2127 locations->SetOut(Location::SameAsFirstInput());
2128 }
2129}
2130
2131void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2132 // We assume the class is not null.
2133 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2134 check->GetLoadClass(),
2135 check,
2136 check->GetDexPc(),
2137 true);
2138 codegen_->AddSlowPath(slow_path);
2139 GenerateClassInitializationCheck(slow_path,
2140 check->GetLocations()->InAt(0).AsRegister<Register>());
2141}
2142
2143void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2144 Primitive::Type in_type = compare->InputAt(0)->GetType();
2145
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002146 LocationSummary* locations =
2147 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148
2149 switch (in_type) {
2150 case Primitive::kPrimLong:
2151 locations->SetInAt(0, Location::RequiresRegister());
2152 locations->SetInAt(1, Location::RequiresRegister());
2153 // Output overlaps because it is written before doing the low comparison.
2154 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2155 break;
2156
2157 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002158 case Primitive::kPrimDouble:
2159 locations->SetInAt(0, Location::RequiresFpuRegister());
2160 locations->SetInAt(1, Location::RequiresFpuRegister());
2161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002162 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002163
2164 default:
2165 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2166 }
2167}
2168
2169void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2170 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002171 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002172 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002173 bool gt_bias = instruction->IsGtBias();
2174 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002175
2176 // 0 if: left == right
2177 // 1 if: left > right
2178 // -1 if: left < right
2179 switch (in_type) {
2180 case Primitive::kPrimLong: {
2181 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002182 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2183 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2184 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2185 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2186 // TODO: more efficient (direct) comparison with a constant.
2187 __ Slt(TMP, lhs_high, rhs_high);
2188 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2189 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2190 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2191 __ Sltu(TMP, lhs_low, rhs_low);
2192 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2193 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2194 __ Bind(&done);
2195 break;
2196 }
2197
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002198 case Primitive::kPrimFloat: {
2199 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2200 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2201 MipsLabel done;
2202 if (isR6) {
2203 __ CmpEqS(FTMP, lhs, rhs);
2204 __ LoadConst32(res, 0);
2205 __ Bc1nez(FTMP, &done);
2206 if (gt_bias) {
2207 __ CmpLtS(FTMP, lhs, rhs);
2208 __ LoadConst32(res, -1);
2209 __ Bc1nez(FTMP, &done);
2210 __ LoadConst32(res, 1);
2211 } else {
2212 __ CmpLtS(FTMP, rhs, lhs);
2213 __ LoadConst32(res, 1);
2214 __ Bc1nez(FTMP, &done);
2215 __ LoadConst32(res, -1);
2216 }
2217 } else {
2218 if (gt_bias) {
2219 __ ColtS(0, lhs, rhs);
2220 __ LoadConst32(res, -1);
2221 __ Bc1t(0, &done);
2222 __ CeqS(0, lhs, rhs);
2223 __ LoadConst32(res, 1);
2224 __ Movt(res, ZERO, 0);
2225 } else {
2226 __ ColtS(0, rhs, lhs);
2227 __ LoadConst32(res, 1);
2228 __ Bc1t(0, &done);
2229 __ CeqS(0, lhs, rhs);
2230 __ LoadConst32(res, -1);
2231 __ Movt(res, ZERO, 0);
2232 }
2233 }
2234 __ Bind(&done);
2235 break;
2236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 case Primitive::kPrimDouble: {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002238 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2239 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2240 MipsLabel done;
2241 if (isR6) {
2242 __ CmpEqD(FTMP, lhs, rhs);
2243 __ LoadConst32(res, 0);
2244 __ Bc1nez(FTMP, &done);
2245 if (gt_bias) {
2246 __ CmpLtD(FTMP, lhs, rhs);
2247 __ LoadConst32(res, -1);
2248 __ Bc1nez(FTMP, &done);
2249 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002251 __ CmpLtD(FTMP, rhs, lhs);
2252 __ LoadConst32(res, 1);
2253 __ Bc1nez(FTMP, &done);
2254 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 }
2256 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002257 if (gt_bias) {
2258 __ ColtD(0, lhs, rhs);
2259 __ LoadConst32(res, -1);
2260 __ Bc1t(0, &done);
2261 __ CeqD(0, lhs, rhs);
2262 __ LoadConst32(res, 1);
2263 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002265 __ ColtD(0, rhs, lhs);
2266 __ LoadConst32(res, 1);
2267 __ Bc1t(0, &done);
2268 __ CeqD(0, lhs, rhs);
2269 __ LoadConst32(res, -1);
2270 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 }
2272 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002273 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 break;
2275 }
2276
2277 default:
2278 LOG(FATAL) << "Unimplemented compare type " << in_type;
2279 }
2280}
2281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002284 switch (instruction->InputAt(0)->GetType()) {
2285 default:
2286 case Primitive::kPrimLong:
2287 locations->SetInAt(0, Location::RequiresRegister());
2288 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2289 break;
2290
2291 case Primitive::kPrimFloat:
2292 case Primitive::kPrimDouble:
2293 locations->SetInAt(0, Location::RequiresFpuRegister());
2294 locations->SetInAt(1, Location::RequiresFpuRegister());
2295 break;
2296 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297 if (instruction->NeedsMaterialization()) {
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2299 }
2300}
2301
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002302void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 if (!instruction->NeedsMaterialization()) {
2304 return;
2305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002306
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002307 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002308 LocationSummary* locations = instruction->GetLocations();
2309 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002310 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002311
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002312 switch (type) {
2313 default:
2314 // Integer case.
2315 GenerateIntCompare(instruction->GetCondition(), locations);
2316 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002317
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002318 case Primitive::kPrimLong:
2319 // TODO: don't use branches.
2320 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 break;
2322
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002323 case Primitive::kPrimFloat:
2324 case Primitive::kPrimDouble:
2325 // TODO: don't use branches.
2326 GenerateFpCompareAndBranch(instruction->GetCondition(),
2327 instruction->IsGtBias(),
2328 type,
2329 locations,
2330 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002331 break;
2332 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002333
2334 // Convert the branches into the result.
2335 MipsLabel done;
2336
2337 // False case: result = 0.
2338 __ LoadConst32(dst, 0);
2339 __ B(&done);
2340
2341 // True case: result = 1.
2342 __ Bind(&true_label);
2343 __ LoadConst32(dst, 1);
2344 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002345}
2346
Alexey Frunze7e99e052015-11-24 19:28:01 -08002347void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2348 DCHECK(instruction->IsDiv() || instruction->IsRem());
2349 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2350
2351 LocationSummary* locations = instruction->GetLocations();
2352 Location second = locations->InAt(1);
2353 DCHECK(second.IsConstant());
2354
2355 Register out = locations->Out().AsRegister<Register>();
2356 Register dividend = locations->InAt(0).AsRegister<Register>();
2357 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2358 DCHECK(imm == 1 || imm == -1);
2359
2360 if (instruction->IsRem()) {
2361 __ Move(out, ZERO);
2362 } else {
2363 if (imm == -1) {
2364 __ Subu(out, ZERO, dividend);
2365 } else if (out != dividend) {
2366 __ Move(out, dividend);
2367 }
2368 }
2369}
2370
2371void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2372 DCHECK(instruction->IsDiv() || instruction->IsRem());
2373 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2374
2375 LocationSummary* locations = instruction->GetLocations();
2376 Location second = locations->InAt(1);
2377 DCHECK(second.IsConstant());
2378
2379 Register out = locations->Out().AsRegister<Register>();
2380 Register dividend = locations->InAt(0).AsRegister<Register>();
2381 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002382 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002383 int ctz_imm = CTZ(abs_imm);
2384
2385 if (instruction->IsDiv()) {
2386 if (ctz_imm == 1) {
2387 // Fast path for division by +/-2, which is very common.
2388 __ Srl(TMP, dividend, 31);
2389 } else {
2390 __ Sra(TMP, dividend, 31);
2391 __ Srl(TMP, TMP, 32 - ctz_imm);
2392 }
2393 __ Addu(out, dividend, TMP);
2394 __ Sra(out, out, ctz_imm);
2395 if (imm < 0) {
2396 __ Subu(out, ZERO, out);
2397 }
2398 } else {
2399 if (ctz_imm == 1) {
2400 // Fast path for modulo +/-2, which is very common.
2401 __ Sra(TMP, dividend, 31);
2402 __ Subu(out, dividend, TMP);
2403 __ Andi(out, out, 1);
2404 __ Addu(out, out, TMP);
2405 } else {
2406 __ Sra(TMP, dividend, 31);
2407 __ Srl(TMP, TMP, 32 - ctz_imm);
2408 __ Addu(out, dividend, TMP);
2409 if (IsUint<16>(abs_imm - 1)) {
2410 __ Andi(out, out, abs_imm - 1);
2411 } else {
2412 __ Sll(out, out, 32 - ctz_imm);
2413 __ Srl(out, out, 32 - ctz_imm);
2414 }
2415 __ Subu(out, out, TMP);
2416 }
2417 }
2418}
2419
2420void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2421 DCHECK(instruction->IsDiv() || instruction->IsRem());
2422 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2423
2424 LocationSummary* locations = instruction->GetLocations();
2425 Location second = locations->InAt(1);
2426 DCHECK(second.IsConstant());
2427
2428 Register out = locations->Out().AsRegister<Register>();
2429 Register dividend = locations->InAt(0).AsRegister<Register>();
2430 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2431
2432 int64_t magic;
2433 int shift;
2434 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2435
2436 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2437
2438 __ LoadConst32(TMP, magic);
2439 if (isR6) {
2440 __ MuhR6(TMP, dividend, TMP);
2441 } else {
2442 __ MultR2(dividend, TMP);
2443 __ Mfhi(TMP);
2444 }
2445 if (imm > 0 && magic < 0) {
2446 __ Addu(TMP, TMP, dividend);
2447 } else if (imm < 0 && magic > 0) {
2448 __ Subu(TMP, TMP, dividend);
2449 }
2450
2451 if (shift != 0) {
2452 __ Sra(TMP, TMP, shift);
2453 }
2454
2455 if (instruction->IsDiv()) {
2456 __ Sra(out, TMP, 31);
2457 __ Subu(out, TMP, out);
2458 } else {
2459 __ Sra(AT, TMP, 31);
2460 __ Subu(AT, TMP, AT);
2461 __ LoadConst32(TMP, imm);
2462 if (isR6) {
2463 __ MulR6(TMP, AT, TMP);
2464 } else {
2465 __ MulR2(TMP, AT, TMP);
2466 }
2467 __ Subu(out, dividend, TMP);
2468 }
2469}
2470
2471void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2472 DCHECK(instruction->IsDiv() || instruction->IsRem());
2473 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2474
2475 LocationSummary* locations = instruction->GetLocations();
2476 Register out = locations->Out().AsRegister<Register>();
2477 Location second = locations->InAt(1);
2478
2479 if (second.IsConstant()) {
2480 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2481 if (imm == 0) {
2482 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2483 } else if (imm == 1 || imm == -1) {
2484 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002485 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002486 DivRemByPowerOfTwo(instruction);
2487 } else {
2488 DCHECK(imm <= -2 || imm >= 2);
2489 GenerateDivRemWithAnyConstant(instruction);
2490 }
2491 } else {
2492 Register dividend = locations->InAt(0).AsRegister<Register>();
2493 Register divisor = second.AsRegister<Register>();
2494 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2495 if (instruction->IsDiv()) {
2496 if (isR6) {
2497 __ DivR6(out, dividend, divisor);
2498 } else {
2499 __ DivR2(out, dividend, divisor);
2500 }
2501 } else {
2502 if (isR6) {
2503 __ ModR6(out, dividend, divisor);
2504 } else {
2505 __ ModR2(out, dividend, divisor);
2506 }
2507 }
2508 }
2509}
2510
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2512 Primitive::Type type = div->GetResultType();
2513 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2514 ? LocationSummary::kCall
2515 : LocationSummary::kNoCall;
2516
2517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2518
2519 switch (type) {
2520 case Primitive::kPrimInt:
2521 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002522 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 break;
2525
2526 case Primitive::kPrimLong: {
2527 InvokeRuntimeCallingConvention calling_convention;
2528 locations->SetInAt(0, Location::RegisterPairLocation(
2529 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2530 locations->SetInAt(1, Location::RegisterPairLocation(
2531 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2532 locations->SetOut(calling_convention.GetReturnLocation(type));
2533 break;
2534 }
2535
2536 case Primitive::kPrimFloat:
2537 case Primitive::kPrimDouble:
2538 locations->SetInAt(0, Location::RequiresFpuRegister());
2539 locations->SetInAt(1, Location::RequiresFpuRegister());
2540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2541 break;
2542
2543 default:
2544 LOG(FATAL) << "Unexpected div type " << type;
2545 }
2546}
2547
2548void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2549 Primitive::Type type = instruction->GetType();
2550 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551
2552 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002553 case Primitive::kPrimInt:
2554 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002555 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002556 case Primitive::kPrimLong: {
2557 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2558 instruction,
2559 instruction->GetDexPc(),
2560 nullptr,
2561 IsDirectEntrypoint(kQuickLdiv));
2562 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2563 break;
2564 }
2565 case Primitive::kPrimFloat:
2566 case Primitive::kPrimDouble: {
2567 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2568 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2569 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2570 if (type == Primitive::kPrimFloat) {
2571 __ DivS(dst, lhs, rhs);
2572 } else {
2573 __ DivD(dst, lhs, rhs);
2574 }
2575 break;
2576 }
2577 default:
2578 LOG(FATAL) << "Unexpected div type " << type;
2579 }
2580}
2581
2582void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2583 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2584 ? LocationSummary::kCallOnSlowPath
2585 : LocationSummary::kNoCall;
2586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2587 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2588 if (instruction->HasUses()) {
2589 locations->SetOut(Location::SameAsFirstInput());
2590 }
2591}
2592
2593void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2594 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2595 codegen_->AddSlowPath(slow_path);
2596 Location value = instruction->GetLocations()->InAt(0);
2597 Primitive::Type type = instruction->GetType();
2598
2599 switch (type) {
2600 case Primitive::kPrimByte:
2601 case Primitive::kPrimChar:
2602 case Primitive::kPrimShort:
2603 case Primitive::kPrimInt: {
2604 if (value.IsConstant()) {
2605 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2606 __ B(slow_path->GetEntryLabel());
2607 } else {
2608 // A division by a non-null constant is valid. We don't need to perform
2609 // any check, so simply fall through.
2610 }
2611 } else {
2612 DCHECK(value.IsRegister()) << value;
2613 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2614 }
2615 break;
2616 }
2617 case Primitive::kPrimLong: {
2618 if (value.IsConstant()) {
2619 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2620 __ B(slow_path->GetEntryLabel());
2621 } else {
2622 // A division by a non-null constant is valid. We don't need to perform
2623 // any check, so simply fall through.
2624 }
2625 } else {
2626 DCHECK(value.IsRegisterPair()) << value;
2627 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2628 __ Beqz(TMP, slow_path->GetEntryLabel());
2629 }
2630 break;
2631 }
2632 default:
2633 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2634 }
2635}
2636
2637void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2638 LocationSummary* locations =
2639 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2640 locations->SetOut(Location::ConstantLocation(constant));
2641}
2642
2643void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2644 // Will be generated at use site.
2645}
2646
2647void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2648 exit->SetLocations(nullptr);
2649}
2650
2651void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2652}
2653
2654void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2655 LocationSummary* locations =
2656 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2657 locations->SetOut(Location::ConstantLocation(constant));
2658}
2659
2660void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2661 // Will be generated at use site.
2662}
2663
2664void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2665 got->SetLocations(nullptr);
2666}
2667
2668void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2669 DCHECK(!successor->IsExitBlock());
2670 HBasicBlock* block = got->GetBlock();
2671 HInstruction* previous = got->GetPrevious();
2672 HLoopInformation* info = block->GetLoopInformation();
2673
2674 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2675 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2676 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2677 return;
2678 }
2679 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2680 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2681 }
2682 if (!codegen_->GoesToNextBlock(block, successor)) {
2683 __ B(codegen_->GetLabelOf(successor));
2684 }
2685}
2686
2687void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2688 HandleGoto(got, got->GetSuccessor());
2689}
2690
2691void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2692 try_boundary->SetLocations(nullptr);
2693}
2694
2695void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2696 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2697 if (!successor->IsExitBlock()) {
2698 HandleGoto(try_boundary, successor);
2699 }
2700}
2701
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002702void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2703 LocationSummary* locations) {
2704 Register dst = locations->Out().AsRegister<Register>();
2705 Register lhs = locations->InAt(0).AsRegister<Register>();
2706 Location rhs_location = locations->InAt(1);
2707 Register rhs_reg = ZERO;
2708 int64_t rhs_imm = 0;
2709 bool use_imm = rhs_location.IsConstant();
2710 if (use_imm) {
2711 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2712 } else {
2713 rhs_reg = rhs_location.AsRegister<Register>();
2714 }
2715
2716 switch (cond) {
2717 case kCondEQ:
2718 case kCondNE:
2719 if (use_imm && IsUint<16>(rhs_imm)) {
2720 __ Xori(dst, lhs, rhs_imm);
2721 } else {
2722 if (use_imm) {
2723 rhs_reg = TMP;
2724 __ LoadConst32(rhs_reg, rhs_imm);
2725 }
2726 __ Xor(dst, lhs, rhs_reg);
2727 }
2728 if (cond == kCondEQ) {
2729 __ Sltiu(dst, dst, 1);
2730 } else {
2731 __ Sltu(dst, ZERO, dst);
2732 }
2733 break;
2734
2735 case kCondLT:
2736 case kCondGE:
2737 if (use_imm && IsInt<16>(rhs_imm)) {
2738 __ Slti(dst, lhs, rhs_imm);
2739 } else {
2740 if (use_imm) {
2741 rhs_reg = TMP;
2742 __ LoadConst32(rhs_reg, rhs_imm);
2743 }
2744 __ Slt(dst, lhs, rhs_reg);
2745 }
2746 if (cond == kCondGE) {
2747 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2748 // only the slt instruction but no sge.
2749 __ Xori(dst, dst, 1);
2750 }
2751 break;
2752
2753 case kCondLE:
2754 case kCondGT:
2755 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2756 // Simulate lhs <= rhs via lhs < rhs + 1.
2757 __ Slti(dst, lhs, rhs_imm + 1);
2758 if (cond == kCondGT) {
2759 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2760 // only the slti instruction but no sgti.
2761 __ Xori(dst, dst, 1);
2762 }
2763 } else {
2764 if (use_imm) {
2765 rhs_reg = TMP;
2766 __ LoadConst32(rhs_reg, rhs_imm);
2767 }
2768 __ Slt(dst, rhs_reg, lhs);
2769 if (cond == kCondLE) {
2770 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2771 // only the slt instruction but no sle.
2772 __ Xori(dst, dst, 1);
2773 }
2774 }
2775 break;
2776
2777 case kCondB:
2778 case kCondAE:
2779 if (use_imm && IsInt<16>(rhs_imm)) {
2780 // Sltiu sign-extends its 16-bit immediate operand before
2781 // the comparison and thus lets us compare directly with
2782 // unsigned values in the ranges [0, 0x7fff] and
2783 // [0xffff8000, 0xffffffff].
2784 __ Sltiu(dst, lhs, rhs_imm);
2785 } else {
2786 if (use_imm) {
2787 rhs_reg = TMP;
2788 __ LoadConst32(rhs_reg, rhs_imm);
2789 }
2790 __ Sltu(dst, lhs, rhs_reg);
2791 }
2792 if (cond == kCondAE) {
2793 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2794 // only the sltu instruction but no sgeu.
2795 __ Xori(dst, dst, 1);
2796 }
2797 break;
2798
2799 case kCondBE:
2800 case kCondA:
2801 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2802 // Simulate lhs <= rhs via lhs < rhs + 1.
2803 // Note that this only works if rhs + 1 does not overflow
2804 // to 0, hence the check above.
2805 // Sltiu sign-extends its 16-bit immediate operand before
2806 // the comparison and thus lets us compare directly with
2807 // unsigned values in the ranges [0, 0x7fff] and
2808 // [0xffff8000, 0xffffffff].
2809 __ Sltiu(dst, lhs, rhs_imm + 1);
2810 if (cond == kCondA) {
2811 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2812 // only the sltiu instruction but no sgtiu.
2813 __ Xori(dst, dst, 1);
2814 }
2815 } else {
2816 if (use_imm) {
2817 rhs_reg = TMP;
2818 __ LoadConst32(rhs_reg, rhs_imm);
2819 }
2820 __ Sltu(dst, rhs_reg, lhs);
2821 if (cond == kCondBE) {
2822 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2823 // only the sltu instruction but no sleu.
2824 __ Xori(dst, dst, 1);
2825 }
2826 }
2827 break;
2828 }
2829}
2830
2831void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2832 LocationSummary* locations,
2833 MipsLabel* label) {
2834 Register lhs = locations->InAt(0).AsRegister<Register>();
2835 Location rhs_location = locations->InAt(1);
2836 Register rhs_reg = ZERO;
2837 int32_t rhs_imm = 0;
2838 bool use_imm = rhs_location.IsConstant();
2839 if (use_imm) {
2840 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2841 } else {
2842 rhs_reg = rhs_location.AsRegister<Register>();
2843 }
2844
2845 if (use_imm && rhs_imm == 0) {
2846 switch (cond) {
2847 case kCondEQ:
2848 case kCondBE: // <= 0 if zero
2849 __ Beqz(lhs, label);
2850 break;
2851 case kCondNE:
2852 case kCondA: // > 0 if non-zero
2853 __ Bnez(lhs, label);
2854 break;
2855 case kCondLT:
2856 __ Bltz(lhs, label);
2857 break;
2858 case kCondGE:
2859 __ Bgez(lhs, label);
2860 break;
2861 case kCondLE:
2862 __ Blez(lhs, label);
2863 break;
2864 case kCondGT:
2865 __ Bgtz(lhs, label);
2866 break;
2867 case kCondB: // always false
2868 break;
2869 case kCondAE: // always true
2870 __ B(label);
2871 break;
2872 }
2873 } else {
2874 if (use_imm) {
2875 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2876 rhs_reg = TMP;
2877 __ LoadConst32(rhs_reg, rhs_imm);
2878 }
2879 switch (cond) {
2880 case kCondEQ:
2881 __ Beq(lhs, rhs_reg, label);
2882 break;
2883 case kCondNE:
2884 __ Bne(lhs, rhs_reg, label);
2885 break;
2886 case kCondLT:
2887 __ Blt(lhs, rhs_reg, label);
2888 break;
2889 case kCondGE:
2890 __ Bge(lhs, rhs_reg, label);
2891 break;
2892 case kCondLE:
2893 __ Bge(rhs_reg, lhs, label);
2894 break;
2895 case kCondGT:
2896 __ Blt(rhs_reg, lhs, label);
2897 break;
2898 case kCondB:
2899 __ Bltu(lhs, rhs_reg, label);
2900 break;
2901 case kCondAE:
2902 __ Bgeu(lhs, rhs_reg, label);
2903 break;
2904 case kCondBE:
2905 __ Bgeu(rhs_reg, lhs, label);
2906 break;
2907 case kCondA:
2908 __ Bltu(rhs_reg, lhs, label);
2909 break;
2910 }
2911 }
2912}
2913
2914void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2915 LocationSummary* locations,
2916 MipsLabel* label) {
2917 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2918 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2919 Location rhs_location = locations->InAt(1);
2920 Register rhs_high = ZERO;
2921 Register rhs_low = ZERO;
2922 int64_t imm = 0;
2923 uint32_t imm_high = 0;
2924 uint32_t imm_low = 0;
2925 bool use_imm = rhs_location.IsConstant();
2926 if (use_imm) {
2927 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2928 imm_high = High32Bits(imm);
2929 imm_low = Low32Bits(imm);
2930 } else {
2931 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2932 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2933 }
2934
2935 if (use_imm && imm == 0) {
2936 switch (cond) {
2937 case kCondEQ:
2938 case kCondBE: // <= 0 if zero
2939 __ Or(TMP, lhs_high, lhs_low);
2940 __ Beqz(TMP, label);
2941 break;
2942 case kCondNE:
2943 case kCondA: // > 0 if non-zero
2944 __ Or(TMP, lhs_high, lhs_low);
2945 __ Bnez(TMP, label);
2946 break;
2947 case kCondLT:
2948 __ Bltz(lhs_high, label);
2949 break;
2950 case kCondGE:
2951 __ Bgez(lhs_high, label);
2952 break;
2953 case kCondLE:
2954 __ Or(TMP, lhs_high, lhs_low);
2955 __ Sra(AT, lhs_high, 31);
2956 __ Bgeu(AT, TMP, label);
2957 break;
2958 case kCondGT:
2959 __ Or(TMP, lhs_high, lhs_low);
2960 __ Sra(AT, lhs_high, 31);
2961 __ Bltu(AT, TMP, label);
2962 break;
2963 case kCondB: // always false
2964 break;
2965 case kCondAE: // always true
2966 __ B(label);
2967 break;
2968 }
2969 } else if (use_imm) {
2970 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2971 switch (cond) {
2972 case kCondEQ:
2973 __ LoadConst32(TMP, imm_high);
2974 __ Xor(TMP, TMP, lhs_high);
2975 __ LoadConst32(AT, imm_low);
2976 __ Xor(AT, AT, lhs_low);
2977 __ Or(TMP, TMP, AT);
2978 __ Beqz(TMP, label);
2979 break;
2980 case kCondNE:
2981 __ LoadConst32(TMP, imm_high);
2982 __ Xor(TMP, TMP, lhs_high);
2983 __ LoadConst32(AT, imm_low);
2984 __ Xor(AT, AT, lhs_low);
2985 __ Or(TMP, TMP, AT);
2986 __ Bnez(TMP, label);
2987 break;
2988 case kCondLT:
2989 __ LoadConst32(TMP, imm_high);
2990 __ Blt(lhs_high, TMP, label);
2991 __ Slt(TMP, TMP, lhs_high);
2992 __ LoadConst32(AT, imm_low);
2993 __ Sltu(AT, lhs_low, AT);
2994 __ Blt(TMP, AT, label);
2995 break;
2996 case kCondGE:
2997 __ LoadConst32(TMP, imm_high);
2998 __ Blt(TMP, lhs_high, label);
2999 __ Slt(TMP, lhs_high, TMP);
3000 __ LoadConst32(AT, imm_low);
3001 __ Sltu(AT, lhs_low, AT);
3002 __ Or(TMP, TMP, AT);
3003 __ Beqz(TMP, label);
3004 break;
3005 case kCondLE:
3006 __ LoadConst32(TMP, imm_high);
3007 __ Blt(lhs_high, TMP, label);
3008 __ Slt(TMP, TMP, lhs_high);
3009 __ LoadConst32(AT, imm_low);
3010 __ Sltu(AT, AT, lhs_low);
3011 __ Or(TMP, TMP, AT);
3012 __ Beqz(TMP, label);
3013 break;
3014 case kCondGT:
3015 __ LoadConst32(TMP, imm_high);
3016 __ Blt(TMP, lhs_high, label);
3017 __ Slt(TMP, lhs_high, TMP);
3018 __ LoadConst32(AT, imm_low);
3019 __ Sltu(AT, AT, lhs_low);
3020 __ Blt(TMP, AT, label);
3021 break;
3022 case kCondB:
3023 __ LoadConst32(TMP, imm_high);
3024 __ Bltu(lhs_high, TMP, label);
3025 __ Sltu(TMP, TMP, lhs_high);
3026 __ LoadConst32(AT, imm_low);
3027 __ Sltu(AT, lhs_low, AT);
3028 __ Blt(TMP, AT, label);
3029 break;
3030 case kCondAE:
3031 __ LoadConst32(TMP, imm_high);
3032 __ Bltu(TMP, lhs_high, label);
3033 __ Sltu(TMP, lhs_high, TMP);
3034 __ LoadConst32(AT, imm_low);
3035 __ Sltu(AT, lhs_low, AT);
3036 __ Or(TMP, TMP, AT);
3037 __ Beqz(TMP, label);
3038 break;
3039 case kCondBE:
3040 __ LoadConst32(TMP, imm_high);
3041 __ Bltu(lhs_high, TMP, label);
3042 __ Sltu(TMP, TMP, lhs_high);
3043 __ LoadConst32(AT, imm_low);
3044 __ Sltu(AT, AT, lhs_low);
3045 __ Or(TMP, TMP, AT);
3046 __ Beqz(TMP, label);
3047 break;
3048 case kCondA:
3049 __ LoadConst32(TMP, imm_high);
3050 __ Bltu(TMP, lhs_high, label);
3051 __ Sltu(TMP, lhs_high, TMP);
3052 __ LoadConst32(AT, imm_low);
3053 __ Sltu(AT, AT, lhs_low);
3054 __ Blt(TMP, AT, label);
3055 break;
3056 }
3057 } else {
3058 switch (cond) {
3059 case kCondEQ:
3060 __ Xor(TMP, lhs_high, rhs_high);
3061 __ Xor(AT, lhs_low, rhs_low);
3062 __ Or(TMP, TMP, AT);
3063 __ Beqz(TMP, label);
3064 break;
3065 case kCondNE:
3066 __ Xor(TMP, lhs_high, rhs_high);
3067 __ Xor(AT, lhs_low, rhs_low);
3068 __ Or(TMP, TMP, AT);
3069 __ Bnez(TMP, label);
3070 break;
3071 case kCondLT:
3072 __ Blt(lhs_high, rhs_high, label);
3073 __ Slt(TMP, rhs_high, lhs_high);
3074 __ Sltu(AT, lhs_low, rhs_low);
3075 __ Blt(TMP, AT, label);
3076 break;
3077 case kCondGE:
3078 __ Blt(rhs_high, lhs_high, label);
3079 __ Slt(TMP, lhs_high, rhs_high);
3080 __ Sltu(AT, lhs_low, rhs_low);
3081 __ Or(TMP, TMP, AT);
3082 __ Beqz(TMP, label);
3083 break;
3084 case kCondLE:
3085 __ Blt(lhs_high, rhs_high, label);
3086 __ Slt(TMP, rhs_high, lhs_high);
3087 __ Sltu(AT, rhs_low, lhs_low);
3088 __ Or(TMP, TMP, AT);
3089 __ Beqz(TMP, label);
3090 break;
3091 case kCondGT:
3092 __ Blt(rhs_high, lhs_high, label);
3093 __ Slt(TMP, lhs_high, rhs_high);
3094 __ Sltu(AT, rhs_low, lhs_low);
3095 __ Blt(TMP, AT, label);
3096 break;
3097 case kCondB:
3098 __ Bltu(lhs_high, rhs_high, label);
3099 __ Sltu(TMP, rhs_high, lhs_high);
3100 __ Sltu(AT, lhs_low, rhs_low);
3101 __ Blt(TMP, AT, label);
3102 break;
3103 case kCondAE:
3104 __ Bltu(rhs_high, lhs_high, label);
3105 __ Sltu(TMP, lhs_high, rhs_high);
3106 __ Sltu(AT, lhs_low, rhs_low);
3107 __ Or(TMP, TMP, AT);
3108 __ Beqz(TMP, label);
3109 break;
3110 case kCondBE:
3111 __ Bltu(lhs_high, rhs_high, label);
3112 __ Sltu(TMP, rhs_high, lhs_high);
3113 __ Sltu(AT, rhs_low, lhs_low);
3114 __ Or(TMP, TMP, AT);
3115 __ Beqz(TMP, label);
3116 break;
3117 case kCondA:
3118 __ Bltu(rhs_high, lhs_high, label);
3119 __ Sltu(TMP, lhs_high, rhs_high);
3120 __ Sltu(AT, rhs_low, lhs_low);
3121 __ Blt(TMP, AT, label);
3122 break;
3123 }
3124 }
3125}
3126
3127void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3128 bool gt_bias,
3129 Primitive::Type type,
3130 LocationSummary* locations,
3131 MipsLabel* label) {
3132 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3133 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3134 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3135 if (type == Primitive::kPrimFloat) {
3136 if (isR6) {
3137 switch (cond) {
3138 case kCondEQ:
3139 __ CmpEqS(FTMP, lhs, rhs);
3140 __ Bc1nez(FTMP, label);
3141 break;
3142 case kCondNE:
3143 __ CmpEqS(FTMP, lhs, rhs);
3144 __ Bc1eqz(FTMP, label);
3145 break;
3146 case kCondLT:
3147 if (gt_bias) {
3148 __ CmpLtS(FTMP, lhs, rhs);
3149 } else {
3150 __ CmpUltS(FTMP, lhs, rhs);
3151 }
3152 __ Bc1nez(FTMP, label);
3153 break;
3154 case kCondLE:
3155 if (gt_bias) {
3156 __ CmpLeS(FTMP, lhs, rhs);
3157 } else {
3158 __ CmpUleS(FTMP, lhs, rhs);
3159 }
3160 __ Bc1nez(FTMP, label);
3161 break;
3162 case kCondGT:
3163 if (gt_bias) {
3164 __ CmpUltS(FTMP, rhs, lhs);
3165 } else {
3166 __ CmpLtS(FTMP, rhs, lhs);
3167 }
3168 __ Bc1nez(FTMP, label);
3169 break;
3170 case kCondGE:
3171 if (gt_bias) {
3172 __ CmpUleS(FTMP, rhs, lhs);
3173 } else {
3174 __ CmpLeS(FTMP, rhs, lhs);
3175 }
3176 __ Bc1nez(FTMP, label);
3177 break;
3178 default:
3179 LOG(FATAL) << "Unexpected non-floating-point condition";
3180 }
3181 } else {
3182 switch (cond) {
3183 case kCondEQ:
3184 __ CeqS(0, lhs, rhs);
3185 __ Bc1t(0, label);
3186 break;
3187 case kCondNE:
3188 __ CeqS(0, lhs, rhs);
3189 __ Bc1f(0, label);
3190 break;
3191 case kCondLT:
3192 if (gt_bias) {
3193 __ ColtS(0, lhs, rhs);
3194 } else {
3195 __ CultS(0, lhs, rhs);
3196 }
3197 __ Bc1t(0, label);
3198 break;
3199 case kCondLE:
3200 if (gt_bias) {
3201 __ ColeS(0, lhs, rhs);
3202 } else {
3203 __ CuleS(0, lhs, rhs);
3204 }
3205 __ Bc1t(0, label);
3206 break;
3207 case kCondGT:
3208 if (gt_bias) {
3209 __ CultS(0, rhs, lhs);
3210 } else {
3211 __ ColtS(0, rhs, lhs);
3212 }
3213 __ Bc1t(0, label);
3214 break;
3215 case kCondGE:
3216 if (gt_bias) {
3217 __ CuleS(0, rhs, lhs);
3218 } else {
3219 __ ColeS(0, rhs, lhs);
3220 }
3221 __ Bc1t(0, label);
3222 break;
3223 default:
3224 LOG(FATAL) << "Unexpected non-floating-point condition";
3225 }
3226 }
3227 } else {
3228 DCHECK_EQ(type, Primitive::kPrimDouble);
3229 if (isR6) {
3230 switch (cond) {
3231 case kCondEQ:
3232 __ CmpEqD(FTMP, lhs, rhs);
3233 __ Bc1nez(FTMP, label);
3234 break;
3235 case kCondNE:
3236 __ CmpEqD(FTMP, lhs, rhs);
3237 __ Bc1eqz(FTMP, label);
3238 break;
3239 case kCondLT:
3240 if (gt_bias) {
3241 __ CmpLtD(FTMP, lhs, rhs);
3242 } else {
3243 __ CmpUltD(FTMP, lhs, rhs);
3244 }
3245 __ Bc1nez(FTMP, label);
3246 break;
3247 case kCondLE:
3248 if (gt_bias) {
3249 __ CmpLeD(FTMP, lhs, rhs);
3250 } else {
3251 __ CmpUleD(FTMP, lhs, rhs);
3252 }
3253 __ Bc1nez(FTMP, label);
3254 break;
3255 case kCondGT:
3256 if (gt_bias) {
3257 __ CmpUltD(FTMP, rhs, lhs);
3258 } else {
3259 __ CmpLtD(FTMP, rhs, lhs);
3260 }
3261 __ Bc1nez(FTMP, label);
3262 break;
3263 case kCondGE:
3264 if (gt_bias) {
3265 __ CmpUleD(FTMP, rhs, lhs);
3266 } else {
3267 __ CmpLeD(FTMP, rhs, lhs);
3268 }
3269 __ Bc1nez(FTMP, label);
3270 break;
3271 default:
3272 LOG(FATAL) << "Unexpected non-floating-point condition";
3273 }
3274 } else {
3275 switch (cond) {
3276 case kCondEQ:
3277 __ CeqD(0, lhs, rhs);
3278 __ Bc1t(0, label);
3279 break;
3280 case kCondNE:
3281 __ CeqD(0, lhs, rhs);
3282 __ Bc1f(0, label);
3283 break;
3284 case kCondLT:
3285 if (gt_bias) {
3286 __ ColtD(0, lhs, rhs);
3287 } else {
3288 __ CultD(0, lhs, rhs);
3289 }
3290 __ Bc1t(0, label);
3291 break;
3292 case kCondLE:
3293 if (gt_bias) {
3294 __ ColeD(0, lhs, rhs);
3295 } else {
3296 __ CuleD(0, lhs, rhs);
3297 }
3298 __ Bc1t(0, label);
3299 break;
3300 case kCondGT:
3301 if (gt_bias) {
3302 __ CultD(0, rhs, lhs);
3303 } else {
3304 __ ColtD(0, rhs, lhs);
3305 }
3306 __ Bc1t(0, label);
3307 break;
3308 case kCondGE:
3309 if (gt_bias) {
3310 __ CuleD(0, rhs, lhs);
3311 } else {
3312 __ ColeD(0, rhs, lhs);
3313 }
3314 __ Bc1t(0, label);
3315 break;
3316 default:
3317 LOG(FATAL) << "Unexpected non-floating-point condition";
3318 }
3319 }
3320 }
3321}
3322
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003323void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003324 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003325 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003326 MipsLabel* false_target) {
3327 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003328
David Brazdil0debae72015-11-12 18:37:00 +00003329 if (true_target == nullptr && false_target == nullptr) {
3330 // Nothing to do. The code always falls through.
3331 return;
3332 } else if (cond->IsIntConstant()) {
3333 // Constant condition, statically compared against 1.
3334 if (cond->AsIntConstant()->IsOne()) {
3335 if (true_target != nullptr) {
3336 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003338 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003339 DCHECK(cond->AsIntConstant()->IsZero());
3340 if (false_target != nullptr) {
3341 __ B(false_target);
3342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003343 }
David Brazdil0debae72015-11-12 18:37:00 +00003344 return;
3345 }
3346
3347 // The following code generates these patterns:
3348 // (1) true_target == nullptr && false_target != nullptr
3349 // - opposite condition true => branch to false_target
3350 // (2) true_target != nullptr && false_target == nullptr
3351 // - condition true => branch to true_target
3352 // (3) true_target != nullptr && false_target != nullptr
3353 // - condition true => branch to true_target
3354 // - branch to false_target
3355 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003356 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003357 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003358 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003359 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003360 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3361 } else {
3362 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003364 } else {
3365 // The condition instruction has not been materialized, use its inputs as
3366 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003367 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003368 Primitive::Type type = condition->InputAt(0)->GetType();
3369 LocationSummary* locations = cond->GetLocations();
3370 IfCondition if_cond = condition->GetCondition();
3371 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003372
David Brazdil0debae72015-11-12 18:37:00 +00003373 if (true_target == nullptr) {
3374 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003375 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003376 }
3377
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003378 switch (type) {
3379 default:
3380 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3381 break;
3382 case Primitive::kPrimLong:
3383 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3384 break;
3385 case Primitive::kPrimFloat:
3386 case Primitive::kPrimDouble:
3387 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3388 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003389 }
3390 }
David Brazdil0debae72015-11-12 18:37:00 +00003391
3392 // If neither branch falls through (case 3), the conditional branch to `true_target`
3393 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3394 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003395 __ B(false_target);
3396 }
3397}
3398
3399void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003401 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003402 locations->SetInAt(0, Location::RequiresRegister());
3403 }
3404}
3405
3406void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003407 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3408 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3409 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3410 nullptr : codegen_->GetLabelOf(true_successor);
3411 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3412 nullptr : codegen_->GetLabelOf(false_successor);
3413 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003414}
3415
3416void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3417 LocationSummary* locations = new (GetGraph()->GetArena())
3418 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003419 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420 locations->SetInAt(0, Location::RequiresRegister());
3421 }
3422}
3423
3424void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003425 SlowPathCodeMIPS* slow_path =
3426 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003427 GenerateTestAndBranch(deoptimize,
3428 /* condition_input_index */ 0,
3429 slow_path->GetEntryLabel(),
3430 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003431}
3432
David Srbecky0cf44932015-12-09 14:09:59 +00003433void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3434 new (GetGraph()->GetArena()) LocationSummary(info);
3435}
3436
3437void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00003438 if (codegen_->HasStackMapAtCurrentPc()) {
3439 // Ensure that we do not collide with the stack map of the previous instruction.
3440 __ Nop();
3441 }
David Srbecky0cf44932015-12-09 14:09:59 +00003442 codegen_->RecordPcInfo(info, info->GetDexPc());
3443}
3444
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3446 Primitive::Type field_type = field_info.GetFieldType();
3447 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3448 bool generate_volatile = field_info.IsVolatile() && is_wide;
3449 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3450 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3451
3452 locations->SetInAt(0, Location::RequiresRegister());
3453 if (generate_volatile) {
3454 InvokeRuntimeCallingConvention calling_convention;
3455 // need A0 to hold base + offset
3456 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3457 if (field_type == Primitive::kPrimLong) {
3458 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3459 } else {
3460 locations->SetOut(Location::RequiresFpuRegister());
3461 // Need some temp core regs since FP results are returned in core registers
3462 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3463 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3464 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3465 }
3466 } else {
3467 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3468 locations->SetOut(Location::RequiresFpuRegister());
3469 } else {
3470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3471 }
3472 }
3473}
3474
3475void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3476 const FieldInfo& field_info,
3477 uint32_t dex_pc) {
3478 Primitive::Type type = field_info.GetFieldType();
3479 LocationSummary* locations = instruction->GetLocations();
3480 Register obj = locations->InAt(0).AsRegister<Register>();
3481 LoadOperandType load_type = kLoadUnsignedByte;
3482 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003483 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003484
3485 switch (type) {
3486 case Primitive::kPrimBoolean:
3487 load_type = kLoadUnsignedByte;
3488 break;
3489 case Primitive::kPrimByte:
3490 load_type = kLoadSignedByte;
3491 break;
3492 case Primitive::kPrimShort:
3493 load_type = kLoadSignedHalfword;
3494 break;
3495 case Primitive::kPrimChar:
3496 load_type = kLoadUnsignedHalfword;
3497 break;
3498 case Primitive::kPrimInt:
3499 case Primitive::kPrimFloat:
3500 case Primitive::kPrimNot:
3501 load_type = kLoadWord;
3502 break;
3503 case Primitive::kPrimLong:
3504 case Primitive::kPrimDouble:
3505 load_type = kLoadDoubleword;
3506 break;
3507 case Primitive::kPrimVoid:
3508 LOG(FATAL) << "Unreachable type " << type;
3509 UNREACHABLE();
3510 }
3511
3512 if (is_volatile && load_type == kLoadDoubleword) {
3513 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003514 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003515 // Do implicit Null check
3516 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3517 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3518 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3519 instruction,
3520 dex_pc,
3521 nullptr,
3522 IsDirectEntrypoint(kQuickA64Load));
3523 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3524 if (type == Primitive::kPrimDouble) {
3525 // Need to move to FP regs since FP results are returned in core registers.
3526 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3527 locations->Out().AsFpuRegister<FRegister>());
3528 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
3529 locations->Out().AsFpuRegister<FRegister>());
3530 }
3531 } else {
3532 if (!Primitive::IsFloatingPointType(type)) {
3533 Register dst;
3534 if (type == Primitive::kPrimLong) {
3535 DCHECK(locations->Out().IsRegisterPair());
3536 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003537 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3538 if (obj == dst) {
3539 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3540 codegen_->MaybeRecordImplicitNullCheck(instruction);
3541 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3542 } else {
3543 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3544 codegen_->MaybeRecordImplicitNullCheck(instruction);
3545 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003547 } else {
3548 DCHECK(locations->Out().IsRegister());
3549 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003550 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003551 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552 } else {
3553 DCHECK(locations->Out().IsFpuRegister());
3554 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3555 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003556 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003557 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003558 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003559 }
3560 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003561 // Longs are handled earlier.
3562 if (type != Primitive::kPrimLong) {
3563 codegen_->MaybeRecordImplicitNullCheck(instruction);
3564 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003565 }
3566
3567 if (is_volatile) {
3568 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3569 }
3570}
3571
3572void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3573 Primitive::Type field_type = field_info.GetFieldType();
3574 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3575 bool generate_volatile = field_info.IsVolatile() && is_wide;
3576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3577 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3578
3579 locations->SetInAt(0, Location::RequiresRegister());
3580 if (generate_volatile) {
3581 InvokeRuntimeCallingConvention calling_convention;
3582 // need A0 to hold base + offset
3583 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3584 if (field_type == Primitive::kPrimLong) {
3585 locations->SetInAt(1, Location::RegisterPairLocation(
3586 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3587 } else {
3588 locations->SetInAt(1, Location::RequiresFpuRegister());
3589 // Pass FP parameters in core registers.
3590 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3591 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3592 }
3593 } else {
3594 if (Primitive::IsFloatingPointType(field_type)) {
3595 locations->SetInAt(1, Location::RequiresFpuRegister());
3596 } else {
3597 locations->SetInAt(1, Location::RequiresRegister());
3598 }
3599 }
3600}
3601
3602void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3603 const FieldInfo& field_info,
3604 uint32_t dex_pc) {
3605 Primitive::Type type = field_info.GetFieldType();
3606 LocationSummary* locations = instruction->GetLocations();
3607 Register obj = locations->InAt(0).AsRegister<Register>();
3608 StoreOperandType store_type = kStoreByte;
3609 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003610 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003611
3612 switch (type) {
3613 case Primitive::kPrimBoolean:
3614 case Primitive::kPrimByte:
3615 store_type = kStoreByte;
3616 break;
3617 case Primitive::kPrimShort:
3618 case Primitive::kPrimChar:
3619 store_type = kStoreHalfword;
3620 break;
3621 case Primitive::kPrimInt:
3622 case Primitive::kPrimFloat:
3623 case Primitive::kPrimNot:
3624 store_type = kStoreWord;
3625 break;
3626 case Primitive::kPrimLong:
3627 case Primitive::kPrimDouble:
3628 store_type = kStoreDoubleword;
3629 break;
3630 case Primitive::kPrimVoid:
3631 LOG(FATAL) << "Unreachable type " << type;
3632 UNREACHABLE();
3633 }
3634
3635 if (is_volatile) {
3636 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3637 }
3638
3639 if (is_volatile && store_type == kStoreDoubleword) {
3640 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003641 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 // Do implicit Null check.
3643 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3644 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3645 if (type == Primitive::kPrimDouble) {
3646 // Pass FP parameters in core registers.
3647 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3648 locations->InAt(1).AsFpuRegister<FRegister>());
3649 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
3650 locations->InAt(1).AsFpuRegister<FRegister>());
3651 }
3652 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3653 instruction,
3654 dex_pc,
3655 nullptr,
3656 IsDirectEntrypoint(kQuickA64Store));
3657 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3658 } else {
3659 if (!Primitive::IsFloatingPointType(type)) {
3660 Register src;
3661 if (type == Primitive::kPrimLong) {
3662 DCHECK(locations->InAt(1).IsRegisterPair());
3663 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003664 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3665 __ StoreToOffset(kStoreWord, src, obj, offset);
3666 codegen_->MaybeRecordImplicitNullCheck(instruction);
3667 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003668 } else {
3669 DCHECK(locations->InAt(1).IsRegister());
3670 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003671 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673 } else {
3674 DCHECK(locations->InAt(1).IsFpuRegister());
3675 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3676 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003677 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003678 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003679 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680 }
3681 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003682 // Longs are handled earlier.
3683 if (type != Primitive::kPrimLong) {
3684 codegen_->MaybeRecordImplicitNullCheck(instruction);
3685 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 }
3687
3688 // TODO: memory barriers?
3689 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3690 DCHECK(locations->InAt(1).IsRegister());
3691 Register src = locations->InAt(1).AsRegister<Register>();
3692 codegen_->MarkGCCard(obj, src);
3693 }
3694
3695 if (is_volatile) {
3696 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3697 }
3698}
3699
3700void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3701 HandleFieldGet(instruction, instruction->GetFieldInfo());
3702}
3703
3704void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3705 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3706}
3707
3708void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3709 HandleFieldSet(instruction, instruction->GetFieldInfo());
3710}
3711
3712void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3713 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3714}
3715
3716void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3717 LocationSummary::CallKind call_kind =
3718 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3720 locations->SetInAt(0, Location::RequiresRegister());
3721 locations->SetInAt(1, Location::RequiresRegister());
3722 // The output does overlap inputs.
3723 // Note that TypeCheckSlowPathMIPS uses this register too.
3724 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3725}
3726
3727void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3728 LocationSummary* locations = instruction->GetLocations();
3729 Register obj = locations->InAt(0).AsRegister<Register>();
3730 Register cls = locations->InAt(1).AsRegister<Register>();
3731 Register out = locations->Out().AsRegister<Register>();
3732
3733 MipsLabel done;
3734
3735 // Return 0 if `obj` is null.
3736 // TODO: Avoid this check if we know `obj` is not null.
3737 __ Move(out, ZERO);
3738 __ Beqz(obj, &done);
3739
3740 // Compare the class of `obj` with `cls`.
3741 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3742 if (instruction->IsExactCheck()) {
3743 // Classes must be equal for the instanceof to succeed.
3744 __ Xor(out, out, cls);
3745 __ Sltiu(out, out, 1);
3746 } else {
3747 // If the classes are not equal, we go into a slow path.
3748 DCHECK(locations->OnlyCallsOnSlowPath());
3749 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3750 codegen_->AddSlowPath(slow_path);
3751 __ Bne(out, cls, slow_path->GetEntryLabel());
3752 __ LoadConst32(out, 1);
3753 __ Bind(slow_path->GetExitLabel());
3754 }
3755
3756 __ Bind(&done);
3757}
3758
3759void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3761 locations->SetOut(Location::ConstantLocation(constant));
3762}
3763
3764void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3765 // Will be generated at use site.
3766}
3767
3768void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3769 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3770 locations->SetOut(Location::ConstantLocation(constant));
3771}
3772
3773void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3774 // Will be generated at use site.
3775}
3776
3777void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3778 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3779 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3780}
3781
3782void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3783 HandleInvoke(invoke);
3784 // The register T0 is required to be used for the hidden argument in
3785 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3786 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3787}
3788
3789void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3790 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3791 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3792 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3793 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3794 Location receiver = invoke->GetLocations()->InAt(0);
3795 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3796 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3797
3798 // Set the hidden argument.
3799 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3800 invoke->GetDexMethodIndex());
3801
3802 // temp = object->GetClass();
3803 if (receiver.IsStackSlot()) {
3804 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3805 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3806 } else {
3807 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3808 }
3809 codegen_->MaybeRecordImplicitNullCheck(invoke);
3810 // temp = temp->GetImtEntryAt(method_offset);
3811 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3812 // T9 = temp->GetEntryPoint();
3813 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3814 // T9();
3815 __ Jalr(T9);
3816 __ Nop();
3817 DCHECK(!codegen_->IsLeafMethod());
3818 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3819}
3820
3821void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003822 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3823 if (intrinsic.TryDispatch(invoke)) {
3824 return;
3825 }
3826
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003827 HandleInvoke(invoke);
3828}
3829
3830void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3831 // When we do not run baseline, explicit clinit checks triggered by static
3832 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3833 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3834
Chris Larsen701566a2015-10-27 15:29:13 -07003835 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3836 if (intrinsic.TryDispatch(invoke)) {
3837 return;
3838 }
3839
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003840 HandleInvoke(invoke);
3841}
3842
Chris Larsen701566a2015-10-27 15:29:13 -07003843static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003844 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003845 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3846 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003847 return true;
3848 }
3849 return false;
3850}
3851
Vladimir Markodc151b22015-10-15 18:02:30 +01003852HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3853 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3854 MethodReference target_method ATTRIBUTE_UNUSED) {
3855 switch (desired_dispatch_info.method_load_kind) {
3856 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3857 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3858 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3859 return HInvokeStaticOrDirect::DispatchInfo {
3860 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3861 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3862 0u,
3863 0u
3864 };
3865 default:
3866 break;
3867 }
3868 switch (desired_dispatch_info.code_ptr_location) {
3869 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3870 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3871 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3872 return HInvokeStaticOrDirect::DispatchInfo {
3873 desired_dispatch_info.method_load_kind,
3874 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3875 desired_dispatch_info.method_load_data,
3876 0u
3877 };
3878 default:
3879 return desired_dispatch_info;
3880 }
3881}
3882
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003883void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3884 // All registers are assumed to be correctly set up per the calling convention.
3885
3886 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3887 switch (invoke->GetMethodLoadKind()) {
3888 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3889 // temp = thread->string_init_entrypoint
3890 __ LoadFromOffset(kLoadWord,
3891 temp.AsRegister<Register>(),
3892 TR,
3893 invoke->GetStringInitOffset());
3894 break;
3895 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003896 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897 break;
3898 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3899 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3900 break;
3901 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003902 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003903 // TODO: Implement these types.
3904 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3905 LOG(FATAL) << "Unsupported";
3906 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003908 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003909 Register reg = temp.AsRegister<Register>();
3910 Register method_reg;
3911 if (current_method.IsRegister()) {
3912 method_reg = current_method.AsRegister<Register>();
3913 } else {
3914 // TODO: use the appropriate DCHECK() here if possible.
3915 // DCHECK(invoke->GetLocations()->Intrinsified());
3916 DCHECK(!current_method.IsValid());
3917 method_reg = reg;
3918 __ Lw(reg, SP, kCurrentMethodStackOffset);
3919 }
3920
3921 // temp = temp->dex_cache_resolved_methods_;
3922 __ LoadFromOffset(kLoadWord,
3923 reg,
3924 method_reg,
3925 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3926 // temp = temp[index_in_cache]
3927 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3928 __ LoadFromOffset(kLoadWord,
3929 reg,
3930 reg,
3931 CodeGenerator::GetCachePointerOffset(index_in_cache));
3932 break;
3933 }
3934 }
3935
3936 switch (invoke->GetCodePtrLocation()) {
3937 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3938 __ Jalr(&frame_entry_label_, T9);
3939 break;
3940 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3941 // LR = invoke->GetDirectCodePtr();
3942 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3943 // LR()
3944 __ Jalr(T9);
3945 __ Nop();
3946 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003947 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003948 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3949 // TODO: Implement these types.
3950 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3951 LOG(FATAL) << "Unsupported";
3952 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003953 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3954 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003955 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003956 T9,
3957 callee_method.AsRegister<Register>(),
3958 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3959 kMipsWordSize).Int32Value());
3960 // T9()
3961 __ Jalr(T9);
3962 __ Nop();
3963 break;
3964 }
3965 DCHECK(!IsLeafMethod());
3966}
3967
3968void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3969 // When we do not run baseline, explicit clinit checks triggered by static
3970 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3971 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3972
3973 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3974 return;
3975 }
3976
3977 LocationSummary* locations = invoke->GetLocations();
3978 codegen_->GenerateStaticOrDirectCall(invoke,
3979 locations->HasTemps()
3980 ? locations->GetTemp(0)
3981 : Location::NoLocation());
3982 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3983}
3984
3985void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003986 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3987 return;
3988 }
3989
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003990 LocationSummary* locations = invoke->GetLocations();
3991 Location receiver = locations->InAt(0);
3992 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3993 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3994 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3995 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3996 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3997
3998 // temp = object->GetClass();
3999 if (receiver.IsStackSlot()) {
4000 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4001 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4002 } else {
4003 DCHECK(receiver.IsRegister());
4004 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4005 }
4006 codegen_->MaybeRecordImplicitNullCheck(invoke);
4007 // temp = temp->GetMethodAt(method_offset);
4008 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4009 // T9 = temp->GetEntryPoint();
4010 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4011 // T9();
4012 __ Jalr(T9);
4013 __ Nop();
4014 DCHECK(!codegen_->IsLeafMethod());
4015 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4016}
4017
4018void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004019 InvokeRuntimeCallingConvention calling_convention;
4020 CodeGenerator::CreateLoadClassLocationSummary(
4021 cls,
4022 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4023 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004024}
4025
4026void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4027 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004028 if (cls->NeedsAccessCheck()) {
4029 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4030 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4031 cls,
4032 cls->GetDexPc(),
4033 nullptr,
4034 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004035 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004036 return;
4037 }
4038
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004039 Register out = locations->Out().AsRegister<Register>();
4040 Register current_method = locations->InAt(0).AsRegister<Register>();
4041 if (cls->IsReferrersClass()) {
4042 DCHECK(!cls->CanCallRuntime());
4043 DCHECK(!cls->MustGenerateClinitCheck());
4044 __ LoadFromOffset(kLoadWord, out, current_method,
4045 ArtMethod::DeclaringClassOffset().Int32Value());
4046 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047 __ LoadFromOffset(kLoadWord, out, current_method,
4048 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4049 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004050
4051 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4052 DCHECK(cls->CanCallRuntime());
4053 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4054 cls,
4055 cls,
4056 cls->GetDexPc(),
4057 cls->MustGenerateClinitCheck());
4058 codegen_->AddSlowPath(slow_path);
4059 if (!cls->IsInDexCache()) {
4060 __ Beqz(out, slow_path->GetEntryLabel());
4061 }
4062 if (cls->MustGenerateClinitCheck()) {
4063 GenerateClassInitializationCheck(slow_path, out);
4064 } else {
4065 __ Bind(slow_path->GetExitLabel());
4066 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067 }
4068 }
4069}
4070
4071static int32_t GetExceptionTlsOffset() {
4072 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4073}
4074
4075void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4076 LocationSummary* locations =
4077 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4078 locations->SetOut(Location::RequiresRegister());
4079}
4080
4081void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4082 Register out = load->GetLocations()->Out().AsRegister<Register>();
4083 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4084}
4085
4086void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4087 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4088}
4089
4090void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4091 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4092}
4093
4094void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
4095 load->SetLocations(nullptr);
4096}
4097
4098void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4099 // Nothing to do, this is driven by the code generator.
4100}
4101
4102void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00004103 LocationSummary::CallKind call_kind = load->IsInDexCache()
4104 ? LocationSummary::kNoCall
4105 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetOut(Location::RequiresRegister());
4109}
4110
4111void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112 LocationSummary* locations = load->GetLocations();
4113 Register out = locations->Out().AsRegister<Register>();
4114 Register current_method = locations->InAt(0).AsRegister<Register>();
4115 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4116 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4117 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004118
4119 if (!load->IsInDexCache()) {
4120 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4121 codegen_->AddSlowPath(slow_path);
4122 __ Beqz(out, slow_path->GetEntryLabel());
4123 __ Bind(slow_path->GetExitLabel());
4124 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125}
4126
4127void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
4128 local->SetLocations(nullptr);
4129}
4130
4131void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
4132 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4133}
4134
4135void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4137 locations->SetOut(Location::ConstantLocation(constant));
4138}
4139
4140void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4141 // Will be generated at use site.
4142}
4143
4144void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4145 LocationSummary* locations =
4146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4147 InvokeRuntimeCallingConvention calling_convention;
4148 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4149}
4150
4151void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4152 if (instruction->IsEnter()) {
4153 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4154 instruction,
4155 instruction->GetDexPc(),
4156 nullptr,
4157 IsDirectEntrypoint(kQuickLockObject));
4158 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4159 } else {
4160 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4161 instruction,
4162 instruction->GetDexPc(),
4163 nullptr,
4164 IsDirectEntrypoint(kQuickUnlockObject));
4165 }
4166 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4167}
4168
4169void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4170 LocationSummary* locations =
4171 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4172 switch (mul->GetResultType()) {
4173 case Primitive::kPrimInt:
4174 case Primitive::kPrimLong:
4175 locations->SetInAt(0, Location::RequiresRegister());
4176 locations->SetInAt(1, Location::RequiresRegister());
4177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4178 break;
4179
4180 case Primitive::kPrimFloat:
4181 case Primitive::kPrimDouble:
4182 locations->SetInAt(0, Location::RequiresFpuRegister());
4183 locations->SetInAt(1, Location::RequiresFpuRegister());
4184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4185 break;
4186
4187 default:
4188 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4189 }
4190}
4191
4192void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4193 Primitive::Type type = instruction->GetType();
4194 LocationSummary* locations = instruction->GetLocations();
4195 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4196
4197 switch (type) {
4198 case Primitive::kPrimInt: {
4199 Register dst = locations->Out().AsRegister<Register>();
4200 Register lhs = locations->InAt(0).AsRegister<Register>();
4201 Register rhs = locations->InAt(1).AsRegister<Register>();
4202
4203 if (isR6) {
4204 __ MulR6(dst, lhs, rhs);
4205 } else {
4206 __ MulR2(dst, lhs, rhs);
4207 }
4208 break;
4209 }
4210 case Primitive::kPrimLong: {
4211 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4212 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4213 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4214 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4215 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4216 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4217
4218 // Extra checks to protect caused by the existance of A1_A2.
4219 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4220 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4221 DCHECK_NE(dst_high, lhs_low);
4222 DCHECK_NE(dst_high, rhs_low);
4223
4224 // A_B * C_D
4225 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4226 // dst_lo: [ low(B*D) ]
4227 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4228
4229 if (isR6) {
4230 __ MulR6(TMP, lhs_high, rhs_low);
4231 __ MulR6(dst_high, lhs_low, rhs_high);
4232 __ Addu(dst_high, dst_high, TMP);
4233 __ MuhuR6(TMP, lhs_low, rhs_low);
4234 __ Addu(dst_high, dst_high, TMP);
4235 __ MulR6(dst_low, lhs_low, rhs_low);
4236 } else {
4237 __ MulR2(TMP, lhs_high, rhs_low);
4238 __ MulR2(dst_high, lhs_low, rhs_high);
4239 __ Addu(dst_high, dst_high, TMP);
4240 __ MultuR2(lhs_low, rhs_low);
4241 __ Mfhi(TMP);
4242 __ Addu(dst_high, dst_high, TMP);
4243 __ Mflo(dst_low);
4244 }
4245 break;
4246 }
4247 case Primitive::kPrimFloat:
4248 case Primitive::kPrimDouble: {
4249 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4250 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4251 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4252 if (type == Primitive::kPrimFloat) {
4253 __ MulS(dst, lhs, rhs);
4254 } else {
4255 __ MulD(dst, lhs, rhs);
4256 }
4257 break;
4258 }
4259 default:
4260 LOG(FATAL) << "Unexpected mul type " << type;
4261 }
4262}
4263
4264void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4265 LocationSummary* locations =
4266 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4267 switch (neg->GetResultType()) {
4268 case Primitive::kPrimInt:
4269 case Primitive::kPrimLong:
4270 locations->SetInAt(0, Location::RequiresRegister());
4271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4272 break;
4273
4274 case Primitive::kPrimFloat:
4275 case Primitive::kPrimDouble:
4276 locations->SetInAt(0, Location::RequiresFpuRegister());
4277 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4278 break;
4279
4280 default:
4281 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4282 }
4283}
4284
4285void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4286 Primitive::Type type = instruction->GetType();
4287 LocationSummary* locations = instruction->GetLocations();
4288
4289 switch (type) {
4290 case Primitive::kPrimInt: {
4291 Register dst = locations->Out().AsRegister<Register>();
4292 Register src = locations->InAt(0).AsRegister<Register>();
4293 __ Subu(dst, ZERO, src);
4294 break;
4295 }
4296 case Primitive::kPrimLong: {
4297 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4298 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4299 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4300 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4301 __ Subu(dst_low, ZERO, src_low);
4302 __ Sltu(TMP, ZERO, dst_low);
4303 __ Subu(dst_high, ZERO, src_high);
4304 __ Subu(dst_high, dst_high, TMP);
4305 break;
4306 }
4307 case Primitive::kPrimFloat:
4308 case Primitive::kPrimDouble: {
4309 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4310 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4311 if (type == Primitive::kPrimFloat) {
4312 __ NegS(dst, src);
4313 } else {
4314 __ NegD(dst, src);
4315 }
4316 break;
4317 }
4318 default:
4319 LOG(FATAL) << "Unexpected neg type " << type;
4320 }
4321}
4322
4323void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4324 LocationSummary* locations =
4325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4326 InvokeRuntimeCallingConvention calling_convention;
4327 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4328 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4329 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4330 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4331}
4332
4333void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4334 InvokeRuntimeCallingConvention calling_convention;
4335 Register current_method_register = calling_convention.GetRegisterAt(2);
4336 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4337 // Move an uint16_t value to a register.
4338 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4339 codegen_->InvokeRuntime(
4340 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4341 instruction,
4342 instruction->GetDexPc(),
4343 nullptr,
4344 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4345 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4346 void*, uint32_t, int32_t, ArtMethod*>();
4347}
4348
4349void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4350 LocationSummary* locations =
4351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4352 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00004353 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4354 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004355 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4356}
4357
4358void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004359 codegen_->InvokeRuntime(
4360 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4361 instruction,
4362 instruction->GetDexPc(),
4363 nullptr,
4364 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4365 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4366}
4367
4368void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4370 locations->SetInAt(0, Location::RequiresRegister());
4371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4372}
4373
4374void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4375 Primitive::Type type = instruction->GetType();
4376 LocationSummary* locations = instruction->GetLocations();
4377
4378 switch (type) {
4379 case Primitive::kPrimInt: {
4380 Register dst = locations->Out().AsRegister<Register>();
4381 Register src = locations->InAt(0).AsRegister<Register>();
4382 __ Nor(dst, src, ZERO);
4383 break;
4384 }
4385
4386 case Primitive::kPrimLong: {
4387 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4388 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4389 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4390 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4391 __ Nor(dst_high, src_high, ZERO);
4392 __ Nor(dst_low, src_low, ZERO);
4393 break;
4394 }
4395
4396 default:
4397 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4398 }
4399}
4400
4401void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4403 locations->SetInAt(0, Location::RequiresRegister());
4404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4405}
4406
4407void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4408 LocationSummary* locations = instruction->GetLocations();
4409 __ Xori(locations->Out().AsRegister<Register>(),
4410 locations->InAt(0).AsRegister<Register>(),
4411 1);
4412}
4413
4414void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4415 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4416 ? LocationSummary::kCallOnSlowPath
4417 : LocationSummary::kNoCall;
4418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4419 locations->SetInAt(0, Location::RequiresRegister());
4420 if (instruction->HasUses()) {
4421 locations->SetOut(Location::SameAsFirstInput());
4422 }
4423}
4424
4425void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4426 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4427 return;
4428 }
4429 Location obj = instruction->GetLocations()->InAt(0);
4430
4431 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
4432 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4433}
4434
4435void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
4436 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
4437 codegen_->AddSlowPath(slow_path);
4438
4439 Location obj = instruction->GetLocations()->InAt(0);
4440
4441 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4442}
4443
4444void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
4445 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
4446 GenerateImplicitNullCheck(instruction);
4447 } else {
4448 GenerateExplicitNullCheck(instruction);
4449 }
4450}
4451
4452void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4453 HandleBinaryOp(instruction);
4454}
4455
4456void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4457 HandleBinaryOp(instruction);
4458}
4459
4460void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4461 LOG(FATAL) << "Unreachable";
4462}
4463
4464void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4465 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4466}
4467
4468void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4470 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4471 if (location.IsStackSlot()) {
4472 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4473 } else if (location.IsDoubleStackSlot()) {
4474 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4475 }
4476 locations->SetOut(location);
4477}
4478
4479void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4480 ATTRIBUTE_UNUSED) {
4481 // Nothing to do, the parameter is already at its location.
4482}
4483
4484void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4485 LocationSummary* locations =
4486 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4487 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4488}
4489
4490void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4491 ATTRIBUTE_UNUSED) {
4492 // Nothing to do, the method is already at its location.
4493}
4494
4495void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4497 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4498 locations->SetInAt(i, Location::Any());
4499 }
4500 locations->SetOut(Location::Any());
4501}
4502
4503void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4504 LOG(FATAL) << "Unreachable";
4505}
4506
4507void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4508 Primitive::Type type = rem->GetResultType();
4509 LocationSummary::CallKind call_kind =
4510 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4512
4513 switch (type) {
4514 case Primitive::kPrimInt:
4515 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004516 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4518 break;
4519
4520 case Primitive::kPrimLong: {
4521 InvokeRuntimeCallingConvention calling_convention;
4522 locations->SetInAt(0, Location::RegisterPairLocation(
4523 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4524 locations->SetInAt(1, Location::RegisterPairLocation(
4525 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4526 locations->SetOut(calling_convention.GetReturnLocation(type));
4527 break;
4528 }
4529
4530 case Primitive::kPrimFloat:
4531 case Primitive::kPrimDouble: {
4532 InvokeRuntimeCallingConvention calling_convention;
4533 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4534 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4535 locations->SetOut(calling_convention.GetReturnLocation(type));
4536 break;
4537 }
4538
4539 default:
4540 LOG(FATAL) << "Unexpected rem type " << type;
4541 }
4542}
4543
4544void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4545 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004546
4547 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004548 case Primitive::kPrimInt:
4549 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004551 case Primitive::kPrimLong: {
4552 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4553 instruction,
4554 instruction->GetDexPc(),
4555 nullptr,
4556 IsDirectEntrypoint(kQuickLmod));
4557 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4558 break;
4559 }
4560 case Primitive::kPrimFloat: {
4561 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4562 instruction, instruction->GetDexPc(),
4563 nullptr,
4564 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004565 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004566 break;
4567 }
4568 case Primitive::kPrimDouble: {
4569 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4570 instruction, instruction->GetDexPc(),
4571 nullptr,
4572 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004573 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004574 break;
4575 }
4576 default:
4577 LOG(FATAL) << "Unexpected rem type " << type;
4578 }
4579}
4580
4581void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4582 memory_barrier->SetLocations(nullptr);
4583}
4584
4585void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4586 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4587}
4588
4589void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4591 Primitive::Type return_type = ret->InputAt(0)->GetType();
4592 locations->SetInAt(0, MipsReturnLocation(return_type));
4593}
4594
4595void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4596 codegen_->GenerateFrameExit();
4597}
4598
4599void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4600 ret->SetLocations(nullptr);
4601}
4602
4603void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4604 codegen_->GenerateFrameExit();
4605}
4606
Alexey Frunze92d90602015-12-18 18:16:36 -08004607void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4608 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004609}
4610
Alexey Frunze92d90602015-12-18 18:16:36 -08004611void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4612 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004613}
4614
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004615void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4616 HandleShift(shl);
4617}
4618
4619void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4620 HandleShift(shl);
4621}
4622
4623void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4624 HandleShift(shr);
4625}
4626
4627void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4628 HandleShift(shr);
4629}
4630
4631void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4632 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4633 Primitive::Type field_type = store->InputAt(1)->GetType();
4634 switch (field_type) {
4635 case Primitive::kPrimNot:
4636 case Primitive::kPrimBoolean:
4637 case Primitive::kPrimByte:
4638 case Primitive::kPrimChar:
4639 case Primitive::kPrimShort:
4640 case Primitive::kPrimInt:
4641 case Primitive::kPrimFloat:
4642 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4643 break;
4644
4645 case Primitive::kPrimLong:
4646 case Primitive::kPrimDouble:
4647 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4648 break;
4649
4650 default:
4651 LOG(FATAL) << "Unimplemented local type " << field_type;
4652 }
4653}
4654
4655void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4656}
4657
4658void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4659 HandleBinaryOp(instruction);
4660}
4661
4662void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4663 HandleBinaryOp(instruction);
4664}
4665
4666void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4667 HandleFieldGet(instruction, instruction->GetFieldInfo());
4668}
4669
4670void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4671 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4672}
4673
4674void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4675 HandleFieldSet(instruction, instruction->GetFieldInfo());
4676}
4677
4678void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4679 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4680}
4681
4682void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4683 HUnresolvedInstanceFieldGet* instruction) {
4684 FieldAccessCallingConventionMIPS calling_convention;
4685 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4686 instruction->GetFieldType(),
4687 calling_convention);
4688}
4689
4690void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4691 HUnresolvedInstanceFieldGet* instruction) {
4692 FieldAccessCallingConventionMIPS calling_convention;
4693 codegen_->GenerateUnresolvedFieldAccess(instruction,
4694 instruction->GetFieldType(),
4695 instruction->GetFieldIndex(),
4696 instruction->GetDexPc(),
4697 calling_convention);
4698}
4699
4700void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4701 HUnresolvedInstanceFieldSet* instruction) {
4702 FieldAccessCallingConventionMIPS calling_convention;
4703 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4704 instruction->GetFieldType(),
4705 calling_convention);
4706}
4707
4708void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4709 HUnresolvedInstanceFieldSet* instruction) {
4710 FieldAccessCallingConventionMIPS calling_convention;
4711 codegen_->GenerateUnresolvedFieldAccess(instruction,
4712 instruction->GetFieldType(),
4713 instruction->GetFieldIndex(),
4714 instruction->GetDexPc(),
4715 calling_convention);
4716}
4717
4718void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4719 HUnresolvedStaticFieldGet* instruction) {
4720 FieldAccessCallingConventionMIPS calling_convention;
4721 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4722 instruction->GetFieldType(),
4723 calling_convention);
4724}
4725
4726void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4727 HUnresolvedStaticFieldGet* instruction) {
4728 FieldAccessCallingConventionMIPS calling_convention;
4729 codegen_->GenerateUnresolvedFieldAccess(instruction,
4730 instruction->GetFieldType(),
4731 instruction->GetFieldIndex(),
4732 instruction->GetDexPc(),
4733 calling_convention);
4734}
4735
4736void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4737 HUnresolvedStaticFieldSet* instruction) {
4738 FieldAccessCallingConventionMIPS calling_convention;
4739 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4740 instruction->GetFieldType(),
4741 calling_convention);
4742}
4743
4744void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4745 HUnresolvedStaticFieldSet* instruction) {
4746 FieldAccessCallingConventionMIPS calling_convention;
4747 codegen_->GenerateUnresolvedFieldAccess(instruction,
4748 instruction->GetFieldType(),
4749 instruction->GetFieldIndex(),
4750 instruction->GetDexPc(),
4751 calling_convention);
4752}
4753
4754void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4755 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4756}
4757
4758void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4759 HBasicBlock* block = instruction->GetBlock();
4760 if (block->GetLoopInformation() != nullptr) {
4761 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4762 // The back edge will generate the suspend check.
4763 return;
4764 }
4765 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4766 // The goto will generate the suspend check.
4767 return;
4768 }
4769 GenerateSuspendCheck(instruction, nullptr);
4770}
4771
4772void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
4773 temp->SetLocations(nullptr);
4774}
4775
4776void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
4777 // Nothing to do, this is driven by the code generator.
4778}
4779
4780void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4781 LocationSummary* locations =
4782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4783 InvokeRuntimeCallingConvention calling_convention;
4784 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4785}
4786
4787void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4788 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4789 instruction,
4790 instruction->GetDexPc(),
4791 nullptr,
4792 IsDirectEntrypoint(kQuickDeliverException));
4793 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4794}
4795
4796void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4797 Primitive::Type input_type = conversion->GetInputType();
4798 Primitive::Type result_type = conversion->GetResultType();
4799 DCHECK_NE(input_type, result_type);
4800
4801 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4802 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4803 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4804 }
4805
4806 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4807 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4808 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
4809 call_kind = LocationSummary::kCall;
4810 }
4811
4812 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4813
4814 if (call_kind == LocationSummary::kNoCall) {
4815 if (Primitive::IsFloatingPointType(input_type)) {
4816 locations->SetInAt(0, Location::RequiresFpuRegister());
4817 } else {
4818 locations->SetInAt(0, Location::RequiresRegister());
4819 }
4820
4821 if (Primitive::IsFloatingPointType(result_type)) {
4822 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4823 } else {
4824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4825 }
4826 } else {
4827 InvokeRuntimeCallingConvention calling_convention;
4828
4829 if (Primitive::IsFloatingPointType(input_type)) {
4830 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4831 } else {
4832 DCHECK_EQ(input_type, Primitive::kPrimLong);
4833 locations->SetInAt(0, Location::RegisterPairLocation(
4834 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4835 }
4836
4837 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4838 }
4839}
4840
4841void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4842 LocationSummary* locations = conversion->GetLocations();
4843 Primitive::Type result_type = conversion->GetResultType();
4844 Primitive::Type input_type = conversion->GetInputType();
4845 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
4846
4847 DCHECK_NE(input_type, result_type);
4848
4849 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4850 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4851 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4852 Register src = locations->InAt(0).AsRegister<Register>();
4853
4854 __ Move(dst_low, src);
4855 __ Sra(dst_high, src, 31);
4856 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4857 Register dst = locations->Out().AsRegister<Register>();
4858 Register src = (input_type == Primitive::kPrimLong)
4859 ? locations->InAt(0).AsRegisterPairLow<Register>()
4860 : locations->InAt(0).AsRegister<Register>();
4861
4862 switch (result_type) {
4863 case Primitive::kPrimChar:
4864 __ Andi(dst, src, 0xFFFF);
4865 break;
4866 case Primitive::kPrimByte:
4867 if (has_sign_extension) {
4868 __ Seb(dst, src);
4869 } else {
4870 __ Sll(dst, src, 24);
4871 __ Sra(dst, dst, 24);
4872 }
4873 break;
4874 case Primitive::kPrimShort:
4875 if (has_sign_extension) {
4876 __ Seh(dst, src);
4877 } else {
4878 __ Sll(dst, src, 16);
4879 __ Sra(dst, dst, 16);
4880 }
4881 break;
4882 case Primitive::kPrimInt:
4883 __ Move(dst, src);
4884 break;
4885
4886 default:
4887 LOG(FATAL) << "Unexpected type conversion from " << input_type
4888 << " to " << result_type;
4889 }
4890 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
4891 if (input_type != Primitive::kPrimLong) {
4892 Register src = locations->InAt(0).AsRegister<Register>();
4893 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4894 __ Mtc1(src, FTMP);
4895 if (result_type == Primitive::kPrimFloat) {
4896 __ Cvtsw(dst, FTMP);
4897 } else {
4898 __ Cvtdw(dst, FTMP);
4899 }
4900 } else {
4901 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4902 : QUICK_ENTRY_POINT(pL2d);
4903 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4904 : IsDirectEntrypoint(kQuickL2d);
4905 codegen_->InvokeRuntime(entry_offset,
4906 conversion,
4907 conversion->GetDexPc(),
4908 nullptr,
4909 direct);
4910 if (result_type == Primitive::kPrimFloat) {
4911 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4912 } else {
4913 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4914 }
4915 }
4916 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4917 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4918 int32_t entry_offset;
4919 bool direct;
4920 if (result_type != Primitive::kPrimLong) {
4921 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
4922 : QUICK_ENTRY_POINT(pD2iz);
4923 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
4924 : IsDirectEntrypoint(kQuickD2iz);
4925 } else {
4926 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4927 : QUICK_ENTRY_POINT(pD2l);
4928 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4929 : IsDirectEntrypoint(kQuickD2l);
4930 }
4931 codegen_->InvokeRuntime(entry_offset,
4932 conversion,
4933 conversion->GetDexPc(),
4934 nullptr,
4935 direct);
4936 if (result_type != Primitive::kPrimLong) {
4937 if (input_type == Primitive::kPrimFloat) {
4938 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
4939 } else {
4940 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
4941 }
4942 } else {
4943 if (input_type == Primitive::kPrimFloat) {
4944 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4945 } else {
4946 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4947 }
4948 }
4949 } else if (Primitive::IsFloatingPointType(result_type) &&
4950 Primitive::IsFloatingPointType(input_type)) {
4951 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4952 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4953 if (result_type == Primitive::kPrimFloat) {
4954 __ Cvtsd(dst, src);
4955 } else {
4956 __ Cvtds(dst, src);
4957 }
4958 } else {
4959 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4960 << " to " << result_type;
4961 }
4962}
4963
4964void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4965 HandleShift(ushr);
4966}
4967
4968void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4969 HandleShift(ushr);
4970}
4971
4972void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4973 HandleBinaryOp(instruction);
4974}
4975
4976void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4977 HandleBinaryOp(instruction);
4978}
4979
4980void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4981 // Nothing to do, this should be removed during prepare for register allocator.
4982 LOG(FATAL) << "Unreachable";
4983}
4984
4985void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4986 // Nothing to do, this should be removed during prepare for register allocator.
4987 LOG(FATAL) << "Unreachable";
4988}
4989
4990void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004991 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004992}
4993
4994void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004995 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004996}
4997
4998void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004999 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005000}
5001
5002void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005003 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005004}
5005
5006void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005007 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005008}
5009
5010void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005011 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005012}
5013
5014void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005015 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005016}
5017
5018void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005019 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005020}
5021
5022void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005023 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005024}
5025
5026void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005027 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005028}
5029
5030void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005031 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005032}
5033
5034void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005035 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005036}
5037
5038void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005039 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005040}
5041
5042void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005043 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005044}
5045
5046void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005047 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005048}
5049
5050void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005051 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005052}
5053
5054void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005055 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005056}
5057
5058void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005059 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005060}
5061
5062void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005063 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064}
5065
5066void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005067 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005068}
5069
5070void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
5071 DCHECK(codegen_->IsBaseline());
5072 LocationSummary* locations =
5073 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5074 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5075}
5076
5077void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5078 DCHECK(codegen_->IsBaseline());
5079 // Will be generated at use site.
5080}
5081
5082void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5083 LocationSummary* locations =
5084 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5085 locations->SetInAt(0, Location::RequiresRegister());
5086}
5087
5088void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5089 int32_t lower_bound = switch_instr->GetStartValue();
5090 int32_t num_entries = switch_instr->GetNumEntries();
5091 LocationSummary* locations = switch_instr->GetLocations();
5092 Register value_reg = locations->InAt(0).AsRegister<Register>();
5093 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5094
5095 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005096 Register temp_reg = TMP;
5097 __ Addiu32(temp_reg, value_reg, -lower_bound);
5098 // Jump to default if index is negative
5099 // Note: We don't check the case that index is positive while value < lower_bound, because in
5100 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5101 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5102
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005104 // Jump to successors[0] if value == lower_bound.
5105 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5106 int32_t last_index = 0;
5107 for (; num_entries - last_index > 2; last_index += 2) {
5108 __ Addiu(temp_reg, temp_reg, -2);
5109 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5110 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5111 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5112 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5113 }
5114 if (num_entries - last_index == 2) {
5115 // The last missing case_value.
5116 __ Addiu(temp_reg, temp_reg, -1);
5117 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005118 }
5119
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005120 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005121 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5122 __ B(codegen_->GetLabelOf(default_block));
5123 }
5124}
5125
5126void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5127 // The trampoline uses the same calling convention as dex calling conventions,
5128 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5129 // the method_idx.
5130 HandleInvoke(invoke);
5131}
5132
5133void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5134 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5135}
5136
5137#undef __
5138#undef QUICK_ENTRY_POINT
5139
5140} // namespace mips
5141} // namespace art