blob: 2e2ceaaecfdba3b813726361297691597928da5b [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
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020042Location MipsReturnLocation(Primitive::Type return_type) {
43 switch (return_type) {
44 case Primitive::kPrimBoolean:
45 case Primitive::kPrimByte:
46 case Primitive::kPrimChar:
47 case Primitive::kPrimShort:
48 case Primitive::kPrimInt:
49 case Primitive::kPrimNot:
50 return Location::RegisterLocation(V0);
51
52 case Primitive::kPrimLong:
53 return Location::RegisterPairLocation(V0, V1);
54
55 case Primitive::kPrimFloat:
56 case Primitive::kPrimDouble:
57 return Location::FpuRegisterLocation(F0);
58
59 case Primitive::kPrimVoid:
60 return Location();
61 }
62 UNREACHABLE();
63}
64
65Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
66 return MipsReturnLocation(type);
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
70 return Location::RegisterLocation(kMethodRegisterArgument);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
74 Location next_location;
75
76 switch (type) {
77 case Primitive::kPrimBoolean:
78 case Primitive::kPrimByte:
79 case Primitive::kPrimChar:
80 case Primitive::kPrimShort:
81 case Primitive::kPrimInt:
82 case Primitive::kPrimNot: {
83 uint32_t gp_index = gp_index_++;
84 if (gp_index < calling_convention.GetNumberOfRegisters()) {
85 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
86 } else {
87 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
88 next_location = Location::StackSlot(stack_offset);
89 }
90 break;
91 }
92
93 case Primitive::kPrimLong: {
94 uint32_t gp_index = gp_index_;
95 gp_index_ += 2;
96 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
97 if (calling_convention.GetRegisterAt(gp_index) == A1) {
98 gp_index_++; // Skip A1, and use A2_A3 instead.
99 gp_index++;
100 }
101 Register low_even = calling_convention.GetRegisterAt(gp_index);
102 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
103 DCHECK_EQ(low_even + 1, high_odd);
104 next_location = Location::RegisterPairLocation(low_even, high_odd);
105 } else {
106 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
107 next_location = Location::DoubleStackSlot(stack_offset);
108 }
109 break;
110 }
111
112 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
113 // will take up the even/odd pair, while floats are stored in even regs only.
114 // On 64 bit FPU, both double and float are stored in even registers only.
115 case Primitive::kPrimFloat:
116 case Primitive::kPrimDouble: {
117 uint32_t float_index = float_index_++;
118 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
119 next_location = Location::FpuRegisterLocation(
120 calling_convention.GetFpuRegisterAt(float_index));
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
124 : Location::StackSlot(stack_offset);
125 }
126 break;
127 }
128
129 case Primitive::kPrimVoid:
130 LOG(FATAL) << "Unexpected parameter type " << type;
131 break;
132 }
133
134 // Space on the stack is reserved for all arguments.
135 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
136
137 return next_location;
138}
139
140Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
141 return MipsReturnLocation(type);
142}
143
144#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()->
145#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
146
147class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
148 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000149 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
152 LocationSummary* locations = instruction_->GetLocations();
153 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
154 __ Bind(GetEntryLabel());
155 if (instruction_->CanThrowIntoCatchBlock()) {
156 // Live registers will be restored in the catch block if caught.
157 SaveLiveRegisters(codegen, instruction_->GetLocations());
158 }
159 // We're moving two locations to locations that could overlap, so we need a parallel
160 // move resolver.
161 InvokeRuntimeCallingConvention calling_convention;
162 codegen->EmitParallelMoves(locations->InAt(0),
163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
164 Primitive::kPrimInt,
165 locations->InAt(1),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
167 Primitive::kPrimInt);
168 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
169 instruction_,
170 instruction_->GetDexPc(),
171 this,
172 IsDirectEntrypoint(kQuickThrowArrayBounds));
173 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
174 }
175
176 bool IsFatal() const OVERRIDE { return true; }
177
178 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
179
180 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200181 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
182};
183
184class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
185 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000186 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
189 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
190 __ Bind(GetEntryLabel());
191 if (instruction_->CanThrowIntoCatchBlock()) {
192 // Live registers will be restored in the catch block if caught.
193 SaveLiveRegisters(codegen, instruction_->GetLocations());
194 }
195 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
196 instruction_,
197 instruction_->GetDexPc(),
198 this,
199 IsDirectEntrypoint(kQuickThrowDivZero));
200 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
201 }
202
203 bool IsFatal() const OVERRIDE { return true; }
204
205 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
206
207 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200208 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
209};
210
211class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
212 public:
213 LoadClassSlowPathMIPS(HLoadClass* cls,
214 HInstruction* at,
215 uint32_t dex_pc,
216 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000217 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200218 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
219 }
220
221 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
222 LocationSummary* locations = at_->GetLocations();
223 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
224
225 __ Bind(GetEntryLabel());
226 SaveLiveRegisters(codegen, locations);
227
228 InvokeRuntimeCallingConvention calling_convention;
229 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
230
231 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
232 : QUICK_ENTRY_POINT(pInitializeType);
233 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
234 : IsDirectEntrypoint(kQuickInitializeType);
235
236 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
237 if (do_clinit_) {
238 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
239 } else {
240 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
241 }
242
243 // Move the class to the desired location.
244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 Primitive::Type type = at_->GetType();
248 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
249 }
250
251 RestoreLiveRegisters(codegen, locations);
252 __ B(GetExitLabel());
253 }
254
255 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
256
257 private:
258 // The class this slow path will load.
259 HLoadClass* const cls_;
260
261 // The instruction where this slow path is happening.
262 // (Might be the load class or an initialization check).
263 HInstruction* const at_;
264
265 // The dex PC of `at_`.
266 const uint32_t dex_pc_;
267
268 // Whether to initialize the class.
269 const bool do_clinit_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
272};
273
274class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
275 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000276 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200277
278 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
279 LocationSummary* locations = instruction_->GetLocations();
280 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
281 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
282
283 __ Bind(GetEntryLabel());
284 SaveLiveRegisters(codegen, locations);
285
286 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000287 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
288 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
290 instruction_,
291 instruction_->GetDexPc(),
292 this,
293 IsDirectEntrypoint(kQuickResolveString));
294 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
295 Primitive::Type type = instruction_->GetType();
296 mips_codegen->MoveLocation(locations->Out(),
297 calling_convention.GetReturnLocation(type),
298 type);
299
300 RestoreLiveRegisters(codegen, locations);
301 __ B(GetExitLabel());
302 }
303
304 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
305
306 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
308};
309
310class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
311 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000312 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200313
314 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
315 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
316 __ Bind(GetEntryLabel());
317 if (instruction_->CanThrowIntoCatchBlock()) {
318 // Live registers will be restored in the catch block if caught.
319 SaveLiveRegisters(codegen, instruction_->GetLocations());
320 }
321 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
322 instruction_,
323 instruction_->GetDexPc(),
324 this,
325 IsDirectEntrypoint(kQuickThrowNullPointer));
326 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
327 }
328
329 bool IsFatal() const OVERRIDE { return true; }
330
331 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
332
333 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
335};
336
337class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
338 public:
339 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
344 __ Bind(GetEntryLabel());
345 SaveLiveRegisters(codegen, instruction_->GetLocations());
346 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
347 instruction_,
348 instruction_->GetDexPc(),
349 this,
350 IsDirectEntrypoint(kQuickTestSuspend));
351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
352 RestoreLiveRegisters(codegen, instruction_->GetLocations());
353 if (successor_ == nullptr) {
354 __ B(GetReturnLabel());
355 } else {
356 __ B(mips_codegen->GetLabelOf(successor_));
357 }
358 }
359
360 MipsLabel* GetReturnLabel() {
361 DCHECK(successor_ == nullptr);
362 return &return_label_;
363 }
364
365 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
366
367 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200368 // If not null, the block to branch to after the suspend check.
369 HBasicBlock* const successor_;
370
371 // If `successor_` is null, the label to branch to after the suspend check.
372 MipsLabel return_label_;
373
374 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
375};
376
377class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
378 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000379 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200380
381 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
382 LocationSummary* locations = instruction_->GetLocations();
383 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
384 uint32_t dex_pc = instruction_->GetDexPc();
385 DCHECK(instruction_->IsCheckCast()
386 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
387 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
388
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 // We're moving two locations to locations that could overlap, so we need a parallel
393 // move resolver.
394 InvokeRuntimeCallingConvention calling_convention;
395 codegen->EmitParallelMoves(locations->InAt(1),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 object_class,
399 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
400 Primitive::kPrimNot);
401
402 if (instruction_->IsInstanceOf()) {
403 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
404 instruction_,
405 dex_pc,
406 this,
407 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000408 CheckEntrypointTypes<
409 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Primitive::Type ret_type = instruction_->GetType();
411 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
412 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 } else {
414 DCHECK(instruction_->IsCheckCast());
415 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
416 instruction_,
417 dex_pc,
418 this,
419 IsDirectEntrypoint(kQuickCheckCast));
420 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
421 }
422
423 RestoreLiveRegisters(codegen, locations);
424 __ B(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
428
429 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
431};
432
433class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
434 public:
Aart Bik42249c32016-01-07 15:33:50 -0800435 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800439 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 __ Bind(GetEntryLabel());
441 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
443 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800444 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 this,
446 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000447 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200448 }
449
450 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
451
452 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200453 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
454};
455
456CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
457 const MipsInstructionSetFeatures& isa_features,
458 const CompilerOptions& compiler_options,
459 OptimizingCompilerStats* stats)
460 : CodeGenerator(graph,
461 kNumberOfCoreRegisters,
462 kNumberOfFRegisters,
463 kNumberOfRegisterPairs,
464 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
465 arraysize(kCoreCalleeSaves)),
466 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
467 arraysize(kFpuCalleeSaves)),
468 compiler_options,
469 stats),
470 block_labels_(nullptr),
471 location_builder_(graph, this),
472 instruction_visitor_(graph, this),
473 move_resolver_(graph->GetArena(), this),
474 assembler_(&isa_features),
475 isa_features_(isa_features) {
476 // Save RA (containing the return address) to mimic Quick.
477 AddAllocatedRegister(Location::RegisterLocation(RA));
478}
479
480#undef __
481#define __ down_cast<MipsAssembler*>(GetAssembler())->
482#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
483
484void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
485 // Ensure that we fix up branches.
486 __ FinalizeCode();
487
488 // Adjust native pc offsets in stack maps.
489 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
490 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
491 uint32_t new_position = __ GetAdjustedPosition(old_position);
492 DCHECK_GE(new_position, old_position);
493 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
494 }
495
496 // Adjust pc offsets for the disassembly information.
497 if (disasm_info_ != nullptr) {
498 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
499 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
500 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
501 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
502 it.second.start = __ GetAdjustedPosition(it.second.start);
503 it.second.end = __ GetAdjustedPosition(it.second.end);
504 }
505 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
506 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
507 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
508 }
509 }
510
511 CodeGenerator::Finalize(allocator);
512}
513
514MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
515 return codegen_->GetAssembler();
516}
517
518void ParallelMoveResolverMIPS::EmitMove(size_t index) {
519 DCHECK_LT(index, moves_.size());
520 MoveOperands* move = moves_[index];
521 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
522}
523
524void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
525 DCHECK_LT(index, moves_.size());
526 MoveOperands* move = moves_[index];
527 Primitive::Type type = move->GetType();
528 Location loc1 = move->GetDestination();
529 Location loc2 = move->GetSource();
530
531 DCHECK(!loc1.IsConstant());
532 DCHECK(!loc2.IsConstant());
533
534 if (loc1.Equals(loc2)) {
535 return;
536 }
537
538 if (loc1.IsRegister() && loc2.IsRegister()) {
539 // Swap 2 GPRs.
540 Register r1 = loc1.AsRegister<Register>();
541 Register r2 = loc2.AsRegister<Register>();
542 __ Move(TMP, r2);
543 __ Move(r2, r1);
544 __ Move(r1, TMP);
545 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
546 FRegister f1 = loc1.AsFpuRegister<FRegister>();
547 FRegister f2 = loc2.AsFpuRegister<FRegister>();
548 if (type == Primitive::kPrimFloat) {
549 __ MovS(FTMP, f2);
550 __ MovS(f2, f1);
551 __ MovS(f1, FTMP);
552 } else {
553 DCHECK_EQ(type, Primitive::kPrimDouble);
554 __ MovD(FTMP, f2);
555 __ MovD(f2, f1);
556 __ MovD(f1, FTMP);
557 }
558 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
559 (loc1.IsFpuRegister() && loc2.IsRegister())) {
560 // Swap FPR and GPR.
561 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
562 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
563 : loc2.AsFpuRegister<FRegister>();
564 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
565 : loc2.AsRegister<Register>();
566 __ Move(TMP, r2);
567 __ Mfc1(r2, f1);
568 __ Mtc1(TMP, f1);
569 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
570 // Swap 2 GPR register pairs.
571 Register r1 = loc1.AsRegisterPairLow<Register>();
572 Register r2 = loc2.AsRegisterPairLow<Register>();
573 __ Move(TMP, r2);
574 __ Move(r2, r1);
575 __ Move(r1, TMP);
576 r1 = loc1.AsRegisterPairHigh<Register>();
577 r2 = loc2.AsRegisterPairHigh<Register>();
578 __ Move(TMP, r2);
579 __ Move(r2, r1);
580 __ Move(r1, TMP);
581 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
582 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
583 // Swap FPR and GPR register pair.
584 DCHECK_EQ(type, Primitive::kPrimDouble);
585 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
586 : loc2.AsFpuRegister<FRegister>();
587 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
588 : loc2.AsRegisterPairLow<Register>();
589 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
590 : loc2.AsRegisterPairHigh<Register>();
591 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
592 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
593 // unpredictable and the following mfch1 will fail.
594 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800595 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200596 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Move(r2_l, TMP);
599 __ Move(r2_h, AT);
600 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
601 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
602 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000604 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
605 (loc1.IsStackSlot() && loc2.IsRegister())) {
606 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
607 : loc2.AsRegister<Register>();
608 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
609 : loc2.GetStackIndex();
610 __ Move(TMP, reg);
611 __ LoadFromOffset(kLoadWord, reg, SP, offset);
612 __ StoreToOffset(kStoreWord, TMP, SP, offset);
613 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
614 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
615 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
616 : loc2.AsRegisterPairLow<Register>();
617 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
618 : loc2.AsRegisterPairHigh<Register>();
619 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
620 : loc2.GetStackIndex();
621 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
622 : loc2.GetHighStackIndex(kMipsWordSize);
623 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000624 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000625 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000626 __ Move(TMP, reg_h);
627 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
628 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200629 } else {
630 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
631 }
632}
633
634void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
635 __ Pop(static_cast<Register>(reg));
636}
637
638void ParallelMoveResolverMIPS::SpillScratch(int reg) {
639 __ Push(static_cast<Register>(reg));
640}
641
642void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
643 // Allocate a scratch register other than TMP, if available.
644 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
645 // automatically unspilled when the scratch scope object is destroyed).
646 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
647 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
648 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
649 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
650 __ LoadFromOffset(kLoadWord,
651 Register(ensure_scratch.GetRegister()),
652 SP,
653 index1 + stack_offset);
654 __ LoadFromOffset(kLoadWord,
655 TMP,
656 SP,
657 index2 + stack_offset);
658 __ StoreToOffset(kStoreWord,
659 Register(ensure_scratch.GetRegister()),
660 SP,
661 index2 + stack_offset);
662 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
663 }
664}
665
666static dwarf::Reg DWARFReg(Register reg) {
667 return dwarf::Reg::MipsCore(static_cast<int>(reg));
668}
669
670// TODO: mapping of floating-point registers to DWARF.
671
672void CodeGeneratorMIPS::GenerateFrameEntry() {
673 __ Bind(&frame_entry_label_);
674
675 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
676
677 if (do_overflow_check) {
678 __ LoadFromOffset(kLoadWord,
679 ZERO,
680 SP,
681 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
682 RecordPcInfo(nullptr, 0);
683 }
684
685 if (HasEmptyFrame()) {
686 return;
687 }
688
689 // Make sure the frame size isn't unreasonably large.
690 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
691 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
692 }
693
694 // Spill callee-saved registers.
695 // Note that their cumulative size is small and they can be indexed using
696 // 16-bit offsets.
697
698 // TODO: increment/decrement SP in one step instead of two or remove this comment.
699
700 uint32_t ofs = FrameEntrySpillSize();
701 bool unaligned_float = ofs & 0x7;
702 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
703 __ IncreaseFrameSize(ofs);
704
705 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
706 Register reg = kCoreCalleeSaves[i];
707 if (allocated_registers_.ContainsCoreRegister(reg)) {
708 ofs -= kMipsWordSize;
709 __ Sw(reg, SP, ofs);
710 __ cfi().RelOffset(DWARFReg(reg), ofs);
711 }
712 }
713
714 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
715 FRegister reg = kFpuCalleeSaves[i];
716 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
717 ofs -= kMipsDoublewordSize;
718 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
719 if (unaligned_float) {
720 if (fpu_32bit) {
721 __ Swc1(reg, SP, ofs);
722 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
723 } else {
724 __ Mfhc1(TMP, reg);
725 __ Swc1(reg, SP, ofs);
726 __ Sw(TMP, SP, ofs + 4);
727 }
728 } else {
729 __ Sdc1(reg, SP, ofs);
730 }
731 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
732 }
733 }
734
735 // Allocate the rest of the frame and store the current method pointer
736 // at its end.
737
738 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
739
740 static_assert(IsInt<16>(kCurrentMethodStackOffset),
741 "kCurrentMethodStackOffset must fit into int16_t");
742 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
743}
744
745void CodeGeneratorMIPS::GenerateFrameExit() {
746 __ cfi().RememberState();
747
748 if (!HasEmptyFrame()) {
749 // Deallocate the rest of the frame.
750
751 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
752
753 // Restore callee-saved registers.
754 // Note that their cumulative size is small and they can be indexed using
755 // 16-bit offsets.
756
757 // TODO: increment/decrement SP in one step instead of two or remove this comment.
758
759 uint32_t ofs = 0;
760 bool unaligned_float = FrameEntrySpillSize() & 0x7;
761 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
762
763 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
764 FRegister reg = kFpuCalleeSaves[i];
765 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
766 if (unaligned_float) {
767 if (fpu_32bit) {
768 __ Lwc1(reg, SP, ofs);
769 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
770 } else {
771 __ Lwc1(reg, SP, ofs);
772 __ Lw(TMP, SP, ofs + 4);
773 __ Mthc1(TMP, reg);
774 }
775 } else {
776 __ Ldc1(reg, SP, ofs);
777 }
778 ofs += kMipsDoublewordSize;
779 // TODO: __ cfi().Restore(DWARFReg(reg));
780 }
781 }
782
783 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
784 Register reg = kCoreCalleeSaves[i];
785 if (allocated_registers_.ContainsCoreRegister(reg)) {
786 __ Lw(reg, SP, ofs);
787 ofs += kMipsWordSize;
788 __ cfi().Restore(DWARFReg(reg));
789 }
790 }
791
792 DCHECK_EQ(ofs, FrameEntrySpillSize());
793 __ DecreaseFrameSize(ofs);
794 }
795
796 __ Jr(RA);
797 __ Nop();
798
799 __ cfi().RestoreState();
800 __ cfi().DefCFAOffset(GetFrameSize());
801}
802
803void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
804 __ Bind(GetLabelOf(block));
805}
806
807void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
808 if (src.Equals(dst)) {
809 return;
810 }
811
812 if (src.IsConstant()) {
813 MoveConstant(dst, src.GetConstant());
814 } else {
815 if (Primitive::Is64BitType(dst_type)) {
816 Move64(dst, src);
817 } else {
818 Move32(dst, src);
819 }
820 }
821}
822
823void CodeGeneratorMIPS::Move32(Location destination, Location source) {
824 if (source.Equals(destination)) {
825 return;
826 }
827
828 if (destination.IsRegister()) {
829 if (source.IsRegister()) {
830 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
831 } else if (source.IsFpuRegister()) {
832 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
833 } else {
834 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
835 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
836 }
837 } else if (destination.IsFpuRegister()) {
838 if (source.IsRegister()) {
839 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
840 } else if (source.IsFpuRegister()) {
841 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
842 } else {
843 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
844 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
845 }
846 } else {
847 DCHECK(destination.IsStackSlot()) << destination;
848 if (source.IsRegister()) {
849 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
850 } else if (source.IsFpuRegister()) {
851 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
852 } else {
853 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
854 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
855 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
856 }
857 }
858}
859
860void CodeGeneratorMIPS::Move64(Location destination, Location source) {
861 if (source.Equals(destination)) {
862 return;
863 }
864
865 if (destination.IsRegisterPair()) {
866 if (source.IsRegisterPair()) {
867 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
868 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
869 } else if (source.IsFpuRegister()) {
870 Register dst_high = destination.AsRegisterPairHigh<Register>();
871 Register dst_low = destination.AsRegisterPairLow<Register>();
872 FRegister src = source.AsFpuRegister<FRegister>();
873 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800874 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200875 } else {
876 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
877 int32_t off = source.GetStackIndex();
878 Register r = destination.AsRegisterPairLow<Register>();
879 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
880 }
881 } else if (destination.IsFpuRegister()) {
882 if (source.IsRegisterPair()) {
883 FRegister dst = destination.AsFpuRegister<FRegister>();
884 Register src_high = source.AsRegisterPairHigh<Register>();
885 Register src_low = source.AsRegisterPairLow<Register>();
886 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800887 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200888 } else if (source.IsFpuRegister()) {
889 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
890 } else {
891 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
892 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
893 }
894 } else {
895 DCHECK(destination.IsDoubleStackSlot()) << destination;
896 int32_t off = destination.GetStackIndex();
897 if (source.IsRegisterPair()) {
898 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
899 } else if (source.IsFpuRegister()) {
900 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
901 } else {
902 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
903 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
904 __ StoreToOffset(kStoreWord, TMP, SP, off);
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
906 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
907 }
908 }
909}
910
911void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
912 if (c->IsIntConstant() || c->IsNullConstant()) {
913 // Move 32 bit constant.
914 int32_t value = GetInt32ValueOf(c);
915 if (destination.IsRegister()) {
916 Register dst = destination.AsRegister<Register>();
917 __ LoadConst32(dst, value);
918 } else {
919 DCHECK(destination.IsStackSlot())
920 << "Cannot move " << c->DebugName() << " to " << destination;
921 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
922 }
923 } else if (c->IsLongConstant()) {
924 // Move 64 bit constant.
925 int64_t value = GetInt64ValueOf(c);
926 if (destination.IsRegisterPair()) {
927 Register r_h = destination.AsRegisterPairHigh<Register>();
928 Register r_l = destination.AsRegisterPairLow<Register>();
929 __ LoadConst64(r_h, r_l, value);
930 } else {
931 DCHECK(destination.IsDoubleStackSlot())
932 << "Cannot move " << c->DebugName() << " to " << destination;
933 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
934 }
935 } else if (c->IsFloatConstant()) {
936 // Move 32 bit float constant.
937 int32_t value = GetInt32ValueOf(c);
938 if (destination.IsFpuRegister()) {
939 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
940 } else {
941 DCHECK(destination.IsStackSlot())
942 << "Cannot move " << c->DebugName() << " to " << destination;
943 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
944 }
945 } else {
946 // Move 64 bit double constant.
947 DCHECK(c->IsDoubleConstant()) << c->DebugName();
948 int64_t value = GetInt64ValueOf(c);
949 if (destination.IsFpuRegister()) {
950 FRegister fd = destination.AsFpuRegister<FRegister>();
951 __ LoadDConst64(fd, value, TMP);
952 } else {
953 DCHECK(destination.IsDoubleStackSlot())
954 << "Cannot move " << c->DebugName() << " to " << destination;
955 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
956 }
957 }
958}
959
960void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
961 DCHECK(destination.IsRegister());
962 Register dst = destination.AsRegister<Register>();
963 __ LoadConst32(dst, value);
964}
965
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200966void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
967 if (location.IsRegister()) {
968 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700969 } else if (location.IsRegisterPair()) {
970 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
971 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200972 } else {
973 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
974 }
975}
976
977Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
978 Primitive::Type type = load->GetType();
979
980 switch (type) {
981 case Primitive::kPrimNot:
982 case Primitive::kPrimInt:
983 case Primitive::kPrimFloat:
984 return Location::StackSlot(GetStackSlot(load->GetLocal()));
985
986 case Primitive::kPrimLong:
987 case Primitive::kPrimDouble:
988 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
989
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimVoid:
995 LOG(FATAL) << "Unexpected type " << type;
996 }
997
998 LOG(FATAL) << "Unreachable";
999 return Location::NoLocation();
1000}
1001
1002void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1003 MipsLabel done;
1004 Register card = AT;
1005 Register temp = TMP;
1006 __ Beqz(value, &done);
1007 __ LoadFromOffset(kLoadWord,
1008 card,
1009 TR,
1010 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1011 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1012 __ Addu(temp, card, temp);
1013 __ Sb(card, temp, 0);
1014 __ Bind(&done);
1015}
1016
David Brazdil58282f42016-01-14 12:45:10 +00001017void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001018 // Don't allocate the dalvik style register pair passing.
1019 blocked_register_pairs_[A1_A2] = true;
1020
1021 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1022 blocked_core_registers_[ZERO] = true;
1023 blocked_core_registers_[K0] = true;
1024 blocked_core_registers_[K1] = true;
1025 blocked_core_registers_[GP] = true;
1026 blocked_core_registers_[SP] = true;
1027 blocked_core_registers_[RA] = true;
1028
1029 // AT and TMP(T8) are used as temporary/scratch registers
1030 // (similar to how AT is used by MIPS assemblers).
1031 blocked_core_registers_[AT] = true;
1032 blocked_core_registers_[TMP] = true;
1033 blocked_fpu_registers_[FTMP] = true;
1034
1035 // Reserve suspend and thread registers.
1036 blocked_core_registers_[S0] = true;
1037 blocked_core_registers_[TR] = true;
1038
1039 // Reserve T9 for function calls
1040 blocked_core_registers_[T9] = true;
1041
1042 // Reserve odd-numbered FPU registers.
1043 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1044 blocked_fpu_registers_[i] = true;
1045 }
1046
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 UpdateBlockedPairRegisters();
1048}
1049
1050void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1051 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1052 MipsManagedRegister current =
1053 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1054 if (blocked_core_registers_[current.AsRegisterPairLow()]
1055 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1056 blocked_register_pairs_[i] = true;
1057 }
1058 }
1059}
1060
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001061size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1062 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1063 return kMipsWordSize;
1064}
1065
1066size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1067 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1068 return kMipsWordSize;
1069}
1070
1071size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1072 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1073 return kMipsDoublewordSize;
1074}
1075
1076size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1077 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1078 return kMipsDoublewordSize;
1079}
1080
1081void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001082 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001083}
1084
1085void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001086 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001087}
1088
1089void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1090 HInstruction* instruction,
1091 uint32_t dex_pc,
1092 SlowPathCode* slow_path) {
1093 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1094 instruction,
1095 dex_pc,
1096 slow_path,
1097 IsDirectEntrypoint(entrypoint));
1098}
1099
1100constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1101
1102void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1103 HInstruction* instruction,
1104 uint32_t dex_pc,
1105 SlowPathCode* slow_path,
1106 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001107 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1108 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001109 if (is_direct_entrypoint) {
1110 // Reserve argument space on stack (for $a0-$a3) for
1111 // entrypoints that directly reference native implementations.
1112 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001113 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001115 } else {
1116 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001117 }
1118 RecordPcInfo(instruction, dex_pc, slow_path);
1119}
1120
1121void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1122 Register class_reg) {
1123 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1124 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1125 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1126 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1127 __ Sync(0);
1128 __ Bind(slow_path->GetExitLabel());
1129}
1130
1131void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1132 __ Sync(0); // Only stype 0 is supported.
1133}
1134
1135void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1136 HBasicBlock* successor) {
1137 SuspendCheckSlowPathMIPS* slow_path =
1138 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1139 codegen_->AddSlowPath(slow_path);
1140
1141 __ LoadFromOffset(kLoadUnsignedHalfword,
1142 TMP,
1143 TR,
1144 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1145 if (successor == nullptr) {
1146 __ Bnez(TMP, slow_path->GetEntryLabel());
1147 __ Bind(slow_path->GetReturnLabel());
1148 } else {
1149 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1150 __ B(slow_path->GetEntryLabel());
1151 // slow_path will return to GetLabelOf(successor).
1152 }
1153}
1154
1155InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1156 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001157 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 assembler_(codegen->GetAssembler()),
1159 codegen_(codegen) {}
1160
1161void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1162 DCHECK_EQ(instruction->InputCount(), 2U);
1163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1164 Primitive::Type type = instruction->GetResultType();
1165 switch (type) {
1166 case Primitive::kPrimInt: {
1167 locations->SetInAt(0, Location::RequiresRegister());
1168 HInstruction* right = instruction->InputAt(1);
1169 bool can_use_imm = false;
1170 if (right->IsConstant()) {
1171 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1172 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1173 can_use_imm = IsUint<16>(imm);
1174 } else if (instruction->IsAdd()) {
1175 can_use_imm = IsInt<16>(imm);
1176 } else {
1177 DCHECK(instruction->IsSub());
1178 can_use_imm = IsInt<16>(-imm);
1179 }
1180 }
1181 if (can_use_imm)
1182 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1183 else
1184 locations->SetInAt(1, Location::RequiresRegister());
1185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1186 break;
1187 }
1188
1189 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001190 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001191 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1192 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001193 break;
1194 }
1195
1196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble:
1198 DCHECK(instruction->IsAdd() || instruction->IsSub());
1199 locations->SetInAt(0, Location::RequiresFpuRegister());
1200 locations->SetInAt(1, Location::RequiresFpuRegister());
1201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1202 break;
1203
1204 default:
1205 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1206 }
1207}
1208
1209void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1210 Primitive::Type type = instruction->GetType();
1211 LocationSummary* locations = instruction->GetLocations();
1212
1213 switch (type) {
1214 case Primitive::kPrimInt: {
1215 Register dst = locations->Out().AsRegister<Register>();
1216 Register lhs = locations->InAt(0).AsRegister<Register>();
1217 Location rhs_location = locations->InAt(1);
1218
1219 Register rhs_reg = ZERO;
1220 int32_t rhs_imm = 0;
1221 bool use_imm = rhs_location.IsConstant();
1222 if (use_imm) {
1223 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1224 } else {
1225 rhs_reg = rhs_location.AsRegister<Register>();
1226 }
1227
1228 if (instruction->IsAnd()) {
1229 if (use_imm)
1230 __ Andi(dst, lhs, rhs_imm);
1231 else
1232 __ And(dst, lhs, rhs_reg);
1233 } else if (instruction->IsOr()) {
1234 if (use_imm)
1235 __ Ori(dst, lhs, rhs_imm);
1236 else
1237 __ Or(dst, lhs, rhs_reg);
1238 } else if (instruction->IsXor()) {
1239 if (use_imm)
1240 __ Xori(dst, lhs, rhs_imm);
1241 else
1242 __ Xor(dst, lhs, rhs_reg);
1243 } else if (instruction->IsAdd()) {
1244 if (use_imm)
1245 __ Addiu(dst, lhs, rhs_imm);
1246 else
1247 __ Addu(dst, lhs, rhs_reg);
1248 } else {
1249 DCHECK(instruction->IsSub());
1250 if (use_imm)
1251 __ Addiu(dst, lhs, -rhs_imm);
1252 else
1253 __ Subu(dst, lhs, rhs_reg);
1254 }
1255 break;
1256 }
1257
1258 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1260 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1261 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1262 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001263 Location rhs_location = locations->InAt(1);
1264 bool use_imm = rhs_location.IsConstant();
1265 if (!use_imm) {
1266 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1267 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1268 if (instruction->IsAnd()) {
1269 __ And(dst_low, lhs_low, rhs_low);
1270 __ And(dst_high, lhs_high, rhs_high);
1271 } else if (instruction->IsOr()) {
1272 __ Or(dst_low, lhs_low, rhs_low);
1273 __ Or(dst_high, lhs_high, rhs_high);
1274 } else if (instruction->IsXor()) {
1275 __ Xor(dst_low, lhs_low, rhs_low);
1276 __ Xor(dst_high, lhs_high, rhs_high);
1277 } else if (instruction->IsAdd()) {
1278 if (lhs_low == rhs_low) {
1279 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1280 __ Slt(TMP, lhs_low, ZERO);
1281 __ Addu(dst_low, lhs_low, rhs_low);
1282 } else {
1283 __ Addu(dst_low, lhs_low, rhs_low);
1284 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1285 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1286 }
1287 __ Addu(dst_high, lhs_high, rhs_high);
1288 __ Addu(dst_high, dst_high, TMP);
1289 } else {
1290 DCHECK(instruction->IsSub());
1291 __ Sltu(TMP, lhs_low, rhs_low);
1292 __ Subu(dst_low, lhs_low, rhs_low);
1293 __ Subu(dst_high, lhs_high, rhs_high);
1294 __ Subu(dst_high, dst_high, TMP);
1295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001297 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1298 if (instruction->IsOr()) {
1299 uint32_t low = Low32Bits(value);
1300 uint32_t high = High32Bits(value);
1301 if (IsUint<16>(low)) {
1302 if (dst_low != lhs_low || low != 0) {
1303 __ Ori(dst_low, lhs_low, low);
1304 }
1305 } else {
1306 __ LoadConst32(TMP, low);
1307 __ Or(dst_low, lhs_low, TMP);
1308 }
1309 if (IsUint<16>(high)) {
1310 if (dst_high != lhs_high || high != 0) {
1311 __ Ori(dst_high, lhs_high, high);
1312 }
1313 } else {
1314 if (high != low) {
1315 __ LoadConst32(TMP, high);
1316 }
1317 __ Or(dst_high, lhs_high, TMP);
1318 }
1319 } else if (instruction->IsXor()) {
1320 uint32_t low = Low32Bits(value);
1321 uint32_t high = High32Bits(value);
1322 if (IsUint<16>(low)) {
1323 if (dst_low != lhs_low || low != 0) {
1324 __ Xori(dst_low, lhs_low, low);
1325 }
1326 } else {
1327 __ LoadConst32(TMP, low);
1328 __ Xor(dst_low, lhs_low, TMP);
1329 }
1330 if (IsUint<16>(high)) {
1331 if (dst_high != lhs_high || high != 0) {
1332 __ Xori(dst_high, lhs_high, high);
1333 }
1334 } else {
1335 if (high != low) {
1336 __ LoadConst32(TMP, high);
1337 }
1338 __ Xor(dst_high, lhs_high, TMP);
1339 }
1340 } else if (instruction->IsAnd()) {
1341 uint32_t low = Low32Bits(value);
1342 uint32_t high = High32Bits(value);
1343 if (IsUint<16>(low)) {
1344 __ Andi(dst_low, lhs_low, low);
1345 } else if (low != 0xFFFFFFFF) {
1346 __ LoadConst32(TMP, low);
1347 __ And(dst_low, lhs_low, TMP);
1348 } else if (dst_low != lhs_low) {
1349 __ Move(dst_low, lhs_low);
1350 }
1351 if (IsUint<16>(high)) {
1352 __ Andi(dst_high, lhs_high, high);
1353 } else if (high != 0xFFFFFFFF) {
1354 if (high != low) {
1355 __ LoadConst32(TMP, high);
1356 }
1357 __ And(dst_high, lhs_high, TMP);
1358 } else if (dst_high != lhs_high) {
1359 __ Move(dst_high, lhs_high);
1360 }
1361 } else {
1362 if (instruction->IsSub()) {
1363 value = -value;
1364 } else {
1365 DCHECK(instruction->IsAdd());
1366 }
1367 int32_t low = Low32Bits(value);
1368 int32_t high = High32Bits(value);
1369 if (IsInt<16>(low)) {
1370 if (dst_low != lhs_low || low != 0) {
1371 __ Addiu(dst_low, lhs_low, low);
1372 }
1373 if (low != 0) {
1374 __ Sltiu(AT, dst_low, low);
1375 }
1376 } else {
1377 __ LoadConst32(TMP, low);
1378 __ Addu(dst_low, lhs_low, TMP);
1379 __ Sltu(AT, dst_low, TMP);
1380 }
1381 if (IsInt<16>(high)) {
1382 if (dst_high != lhs_high || high != 0) {
1383 __ Addiu(dst_high, lhs_high, high);
1384 }
1385 } else {
1386 if (high != low) {
1387 __ LoadConst32(TMP, high);
1388 }
1389 __ Addu(dst_high, lhs_high, TMP);
1390 }
1391 if (low != 0) {
1392 __ Addu(dst_high, dst_high, AT);
1393 }
1394 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 }
1396 break;
1397 }
1398
1399 case Primitive::kPrimFloat:
1400 case Primitive::kPrimDouble: {
1401 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1402 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1403 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1404 if (instruction->IsAdd()) {
1405 if (type == Primitive::kPrimFloat) {
1406 __ AddS(dst, lhs, rhs);
1407 } else {
1408 __ AddD(dst, lhs, rhs);
1409 }
1410 } else {
1411 DCHECK(instruction->IsSub());
1412 if (type == Primitive::kPrimFloat) {
1413 __ SubS(dst, lhs, rhs);
1414 } else {
1415 __ SubD(dst, lhs, rhs);
1416 }
1417 }
1418 break;
1419 }
1420
1421 default:
1422 LOG(FATAL) << "Unexpected binary operation type " << type;
1423 }
1424}
1425
1426void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001427 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001428
1429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1430 Primitive::Type type = instr->GetResultType();
1431 switch (type) {
1432 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001433 locations->SetInAt(0, Location::RequiresRegister());
1434 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1435 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1436 break;
1437 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001438 locations->SetInAt(0, Location::RequiresRegister());
1439 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1440 locations->SetOut(Location::RequiresRegister());
1441 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001442 default:
1443 LOG(FATAL) << "Unexpected shift type " << type;
1444 }
1445}
1446
1447static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1448
1449void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001450 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451 LocationSummary* locations = instr->GetLocations();
1452 Primitive::Type type = instr->GetType();
1453
1454 Location rhs_location = locations->InAt(1);
1455 bool use_imm = rhs_location.IsConstant();
1456 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1457 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001458 const uint32_t shift_mask = (type == Primitive::kPrimInt)
1459 ? kMaxIntShiftValue
1460 : kMaxLongShiftValue;
1461 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001462 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1463 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001464
1465 switch (type) {
1466 case Primitive::kPrimInt: {
1467 Register dst = locations->Out().AsRegister<Register>();
1468 Register lhs = locations->InAt(0).AsRegister<Register>();
1469 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001470 if (shift_value == 0) {
1471 if (dst != lhs) {
1472 __ Move(dst, lhs);
1473 }
1474 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001475 __ Sll(dst, lhs, shift_value);
1476 } else if (instr->IsShr()) {
1477 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001478 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001480 } else {
1481 if (has_ins_rotr) {
1482 __ Rotr(dst, lhs, shift_value);
1483 } else {
1484 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1485 __ Srl(dst, lhs, shift_value);
1486 __ Or(dst, dst, TMP);
1487 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001488 }
1489 } else {
1490 if (instr->IsShl()) {
1491 __ Sllv(dst, lhs, rhs_reg);
1492 } else if (instr->IsShr()) {
1493 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001494 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001495 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001496 } else {
1497 if (has_ins_rotr) {
1498 __ Rotrv(dst, lhs, rhs_reg);
1499 } else {
1500 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001501 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1502 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1503 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1504 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1505 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001506 __ Sllv(TMP, lhs, TMP);
1507 __ Srlv(dst, lhs, rhs_reg);
1508 __ Or(dst, dst, TMP);
1509 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001510 }
1511 }
1512 break;
1513 }
1514
1515 case Primitive::kPrimLong: {
1516 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1517 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1518 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1519 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1520 if (use_imm) {
1521 if (shift_value == 0) {
1522 codegen_->Move64(locations->Out(), locations->InAt(0));
1523 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001524 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001525 if (instr->IsShl()) {
1526 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1527 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1528 __ Sll(dst_low, lhs_low, shift_value);
1529 } else if (instr->IsShr()) {
1530 __ Srl(dst_low, lhs_low, shift_value);
1531 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1532 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001533 } else if (instr->IsUShr()) {
1534 __ Srl(dst_low, lhs_low, shift_value);
1535 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1536 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001537 } else {
1538 __ Srl(dst_low, lhs_low, shift_value);
1539 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1540 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001541 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001542 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001543 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001544 if (instr->IsShl()) {
1545 __ Sll(dst_low, lhs_low, shift_value);
1546 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1547 __ Sll(dst_high, lhs_high, shift_value);
1548 __ Or(dst_high, dst_high, TMP);
1549 } else if (instr->IsShr()) {
1550 __ Sra(dst_high, lhs_high, shift_value);
1551 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1552 __ Srl(dst_low, lhs_low, shift_value);
1553 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001554 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001555 __ Srl(dst_high, lhs_high, shift_value);
1556 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1557 __ Srl(dst_low, lhs_low, shift_value);
1558 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001559 } else {
1560 __ Srl(TMP, lhs_low, shift_value);
1561 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1562 __ Or(dst_low, dst_low, TMP);
1563 __ Srl(TMP, lhs_high, shift_value);
1564 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1565 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001566 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001567 }
1568 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001569 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001571 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001572 __ Move(dst_low, ZERO);
1573 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001574 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001576 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001577 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001578 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001579 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001580 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001581 // 64-bit rotation by 32 is just a swap.
1582 __ Move(dst_low, lhs_high);
1583 __ Move(dst_high, lhs_low);
1584 } else {
1585 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001586 __ Srl(dst_low, lhs_high, shift_value_high);
1587 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1588 __ Srl(dst_high, lhs_low, shift_value_high);
1589 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001590 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001591 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1592 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001593 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001594 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1595 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001596 __ Or(dst_high, dst_high, TMP);
1597 }
1598 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001599 }
1600 }
1601 } else {
1602 MipsLabel done;
1603 if (instr->IsShl()) {
1604 __ Sllv(dst_low, lhs_low, rhs_reg);
1605 __ Nor(AT, ZERO, rhs_reg);
1606 __ Srl(TMP, lhs_low, 1);
1607 __ Srlv(TMP, TMP, AT);
1608 __ Sllv(dst_high, lhs_high, rhs_reg);
1609 __ Or(dst_high, dst_high, TMP);
1610 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1611 __ Beqz(TMP, &done);
1612 __ Move(dst_high, dst_low);
1613 __ Move(dst_low, ZERO);
1614 } else if (instr->IsShr()) {
1615 __ Srav(dst_high, lhs_high, rhs_reg);
1616 __ Nor(AT, ZERO, rhs_reg);
1617 __ Sll(TMP, lhs_high, 1);
1618 __ Sllv(TMP, TMP, AT);
1619 __ Srlv(dst_low, lhs_low, rhs_reg);
1620 __ Or(dst_low, dst_low, TMP);
1621 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1622 __ Beqz(TMP, &done);
1623 __ Move(dst_low, dst_high);
1624 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001625 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 __ Srlv(dst_high, lhs_high, rhs_reg);
1627 __ Nor(AT, ZERO, rhs_reg);
1628 __ Sll(TMP, lhs_high, 1);
1629 __ Sllv(TMP, TMP, AT);
1630 __ Srlv(dst_low, lhs_low, rhs_reg);
1631 __ Or(dst_low, dst_low, TMP);
1632 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1633 __ Beqz(TMP, &done);
1634 __ Move(dst_low, dst_high);
1635 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001636 } else {
1637 __ Nor(AT, ZERO, rhs_reg);
1638 __ Srlv(TMP, lhs_low, rhs_reg);
1639 __ Sll(dst_low, lhs_high, 1);
1640 __ Sllv(dst_low, dst_low, AT);
1641 __ Or(dst_low, dst_low, TMP);
1642 __ Srlv(TMP, lhs_high, rhs_reg);
1643 __ Sll(dst_high, lhs_low, 1);
1644 __ Sllv(dst_high, dst_high, AT);
1645 __ Or(dst_high, dst_high, TMP);
1646 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1647 __ Beqz(TMP, &done);
1648 __ Move(TMP, dst_high);
1649 __ Move(dst_high, dst_low);
1650 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001651 }
1652 __ Bind(&done);
1653 }
1654 break;
1655 }
1656
1657 default:
1658 LOG(FATAL) << "Unexpected shift operation type " << type;
1659 }
1660}
1661
1662void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1663 HandleBinaryOp(instruction);
1664}
1665
1666void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1667 HandleBinaryOp(instruction);
1668}
1669
1670void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1671 HandleBinaryOp(instruction);
1672}
1673
1674void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1675 HandleBinaryOp(instruction);
1676}
1677
1678void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1679 LocationSummary* locations =
1680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1681 locations->SetInAt(0, Location::RequiresRegister());
1682 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1683 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1684 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1685 } else {
1686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1687 }
1688}
1689
1690void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1691 LocationSummary* locations = instruction->GetLocations();
1692 Register obj = locations->InAt(0).AsRegister<Register>();
1693 Location index = locations->InAt(1);
1694 Primitive::Type type = instruction->GetType();
1695
1696 switch (type) {
1697 case Primitive::kPrimBoolean: {
1698 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1699 Register out = locations->Out().AsRegister<Register>();
1700 if (index.IsConstant()) {
1701 size_t offset =
1702 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1703 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1704 } else {
1705 __ Addu(TMP, obj, index.AsRegister<Register>());
1706 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1707 }
1708 break;
1709 }
1710
1711 case Primitive::kPrimByte: {
1712 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1713 Register out = locations->Out().AsRegister<Register>();
1714 if (index.IsConstant()) {
1715 size_t offset =
1716 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1717 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1718 } else {
1719 __ Addu(TMP, obj, index.AsRegister<Register>());
1720 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1721 }
1722 break;
1723 }
1724
1725 case Primitive::kPrimShort: {
1726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1727 Register out = locations->Out().AsRegister<Register>();
1728 if (index.IsConstant()) {
1729 size_t offset =
1730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1731 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1732 } else {
1733 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1734 __ Addu(TMP, obj, TMP);
1735 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1736 }
1737 break;
1738 }
1739
1740 case Primitive::kPrimChar: {
1741 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1742 Register out = locations->Out().AsRegister<Register>();
1743 if (index.IsConstant()) {
1744 size_t offset =
1745 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1746 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1747 } else {
1748 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1749 __ Addu(TMP, obj, TMP);
1750 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1751 }
1752 break;
1753 }
1754
1755 case Primitive::kPrimInt:
1756 case Primitive::kPrimNot: {
1757 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1758 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1759 Register out = locations->Out().AsRegister<Register>();
1760 if (index.IsConstant()) {
1761 size_t offset =
1762 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1763 __ LoadFromOffset(kLoadWord, out, obj, offset);
1764 } else {
1765 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1766 __ Addu(TMP, obj, TMP);
1767 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1768 }
1769 break;
1770 }
1771
1772 case Primitive::kPrimLong: {
1773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1774 Register out = locations->Out().AsRegisterPairLow<Register>();
1775 if (index.IsConstant()) {
1776 size_t offset =
1777 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1778 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1779 } else {
1780 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1781 __ Addu(TMP, obj, TMP);
1782 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1783 }
1784 break;
1785 }
1786
1787 case Primitive::kPrimFloat: {
1788 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1789 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1790 if (index.IsConstant()) {
1791 size_t offset =
1792 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1793 __ LoadSFromOffset(out, obj, offset);
1794 } else {
1795 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1796 __ Addu(TMP, obj, TMP);
1797 __ LoadSFromOffset(out, TMP, data_offset);
1798 }
1799 break;
1800 }
1801
1802 case Primitive::kPrimDouble: {
1803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1804 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1805 if (index.IsConstant()) {
1806 size_t offset =
1807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1808 __ LoadDFromOffset(out, obj, offset);
1809 } else {
1810 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1811 __ Addu(TMP, obj, TMP);
1812 __ LoadDFromOffset(out, TMP, data_offset);
1813 }
1814 break;
1815 }
1816
1817 case Primitive::kPrimVoid:
1818 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1819 UNREACHABLE();
1820 }
1821 codegen_->MaybeRecordImplicitNullCheck(instruction);
1822}
1823
1824void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1825 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1828}
1829
1830void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1831 LocationSummary* locations = instruction->GetLocations();
1832 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1833 Register obj = locations->InAt(0).AsRegister<Register>();
1834 Register out = locations->Out().AsRegister<Register>();
1835 __ LoadFromOffset(kLoadWord, out, obj, offset);
1836 codegen_->MaybeRecordImplicitNullCheck(instruction);
1837}
1838
1839void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001840 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1842 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001843 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1844 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001845 InvokeRuntimeCallingConvention calling_convention;
1846 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1847 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1848 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1849 } else {
1850 locations->SetInAt(0, Location::RequiresRegister());
1851 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1852 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1853 locations->SetInAt(2, Location::RequiresFpuRegister());
1854 } else {
1855 locations->SetInAt(2, Location::RequiresRegister());
1856 }
1857 }
1858}
1859
1860void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1861 LocationSummary* locations = instruction->GetLocations();
1862 Register obj = locations->InAt(0).AsRegister<Register>();
1863 Location index = locations->InAt(1);
1864 Primitive::Type value_type = instruction->GetComponentType();
1865 bool needs_runtime_call = locations->WillCall();
1866 bool needs_write_barrier =
1867 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1868
1869 switch (value_type) {
1870 case Primitive::kPrimBoolean:
1871 case Primitive::kPrimByte: {
1872 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1873 Register value = locations->InAt(2).AsRegister<Register>();
1874 if (index.IsConstant()) {
1875 size_t offset =
1876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1877 __ StoreToOffset(kStoreByte, value, obj, offset);
1878 } else {
1879 __ Addu(TMP, obj, index.AsRegister<Register>());
1880 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimShort:
1886 case Primitive::kPrimChar: {
1887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1888 Register value = locations->InAt(2).AsRegister<Register>();
1889 if (index.IsConstant()) {
1890 size_t offset =
1891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1892 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1893 } else {
1894 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1895 __ Addu(TMP, obj, TMP);
1896 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1897 }
1898 break;
1899 }
1900
1901 case Primitive::kPrimInt:
1902 case Primitive::kPrimNot: {
1903 if (!needs_runtime_call) {
1904 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1905 Register value = locations->InAt(2).AsRegister<Register>();
1906 if (index.IsConstant()) {
1907 size_t offset =
1908 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1909 __ StoreToOffset(kStoreWord, value, obj, offset);
1910 } else {
1911 DCHECK(index.IsRegister()) << index;
1912 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1913 __ Addu(TMP, obj, TMP);
1914 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1915 }
1916 codegen_->MaybeRecordImplicitNullCheck(instruction);
1917 if (needs_write_barrier) {
1918 DCHECK_EQ(value_type, Primitive::kPrimNot);
1919 codegen_->MarkGCCard(obj, value);
1920 }
1921 } else {
1922 DCHECK_EQ(value_type, Primitive::kPrimNot);
1923 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1924 instruction,
1925 instruction->GetDexPc(),
1926 nullptr,
1927 IsDirectEntrypoint(kQuickAputObject));
1928 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1929 }
1930 break;
1931 }
1932
1933 case Primitive::kPrimLong: {
1934 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1935 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1936 if (index.IsConstant()) {
1937 size_t offset =
1938 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1939 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1940 } else {
1941 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1942 __ Addu(TMP, obj, TMP);
1943 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1944 }
1945 break;
1946 }
1947
1948 case Primitive::kPrimFloat: {
1949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1950 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1951 DCHECK(locations->InAt(2).IsFpuRegister());
1952 if (index.IsConstant()) {
1953 size_t offset =
1954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1955 __ StoreSToOffset(value, obj, offset);
1956 } else {
1957 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1958 __ Addu(TMP, obj, TMP);
1959 __ StoreSToOffset(value, TMP, data_offset);
1960 }
1961 break;
1962 }
1963
1964 case Primitive::kPrimDouble: {
1965 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1966 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1967 DCHECK(locations->InAt(2).IsFpuRegister());
1968 if (index.IsConstant()) {
1969 size_t offset =
1970 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1971 __ StoreDToOffset(value, obj, offset);
1972 } else {
1973 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1974 __ Addu(TMP, obj, TMP);
1975 __ StoreDToOffset(value, TMP, data_offset);
1976 }
1977 break;
1978 }
1979
1980 case Primitive::kPrimVoid:
1981 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1982 UNREACHABLE();
1983 }
1984
1985 // Ints and objects are handled in the switch.
1986 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988 }
1989}
1990
1991void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1992 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1993 ? LocationSummary::kCallOnSlowPath
1994 : LocationSummary::kNoCall;
1995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1996 locations->SetInAt(0, Location::RequiresRegister());
1997 locations->SetInAt(1, Location::RequiresRegister());
1998 if (instruction->HasUses()) {
1999 locations->SetOut(Location::SameAsFirstInput());
2000 }
2001}
2002
2003void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2004 LocationSummary* locations = instruction->GetLocations();
2005 BoundsCheckSlowPathMIPS* slow_path =
2006 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2007 codegen_->AddSlowPath(slow_path);
2008
2009 Register index = locations->InAt(0).AsRegister<Register>();
2010 Register length = locations->InAt(1).AsRegister<Register>();
2011
2012 // length is limited by the maximum positive signed 32-bit integer.
2013 // Unsigned comparison of length and index checks for index < 0
2014 // and for length <= index simultaneously.
2015 __ Bgeu(index, length, slow_path->GetEntryLabel());
2016}
2017
2018void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2020 instruction,
2021 LocationSummary::kCallOnSlowPath);
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::RequiresRegister());
2024 // Note that TypeCheckSlowPathMIPS uses this register too.
2025 locations->AddTemp(Location::RequiresRegister());
2026}
2027
2028void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2029 LocationSummary* locations = instruction->GetLocations();
2030 Register obj = locations->InAt(0).AsRegister<Register>();
2031 Register cls = locations->InAt(1).AsRegister<Register>();
2032 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2033
2034 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2035 codegen_->AddSlowPath(slow_path);
2036
2037 // TODO: avoid this check if we know obj is not null.
2038 __ Beqz(obj, slow_path->GetExitLabel());
2039 // Compare the class of `obj` with `cls`.
2040 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2041 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2042 __ Bind(slow_path->GetExitLabel());
2043}
2044
2045void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2048 locations->SetInAt(0, Location::RequiresRegister());
2049 if (check->HasUses()) {
2050 locations->SetOut(Location::SameAsFirstInput());
2051 }
2052}
2053
2054void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2055 // We assume the class is not null.
2056 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2057 check->GetLoadClass(),
2058 check,
2059 check->GetDexPc(),
2060 true);
2061 codegen_->AddSlowPath(slow_path);
2062 GenerateClassInitializationCheck(slow_path,
2063 check->GetLocations()->InAt(0).AsRegister<Register>());
2064}
2065
2066void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2067 Primitive::Type in_type = compare->InputAt(0)->GetType();
2068
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002069 LocationSummary* locations =
2070 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071
2072 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08002073 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002074 case Primitive::kPrimLong:
2075 locations->SetInAt(0, Location::RequiresRegister());
2076 locations->SetInAt(1, Location::RequiresRegister());
2077 // Output overlaps because it is written before doing the low comparison.
2078 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2079 break;
2080
2081 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002082 case Primitive::kPrimDouble:
2083 locations->SetInAt(0, Location::RequiresFpuRegister());
2084 locations->SetInAt(1, Location::RequiresFpuRegister());
2085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087
2088 default:
2089 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2090 }
2091}
2092
2093void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2094 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002095 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002096 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002097 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002098
2099 // 0 if: left == right
2100 // 1 if: left > right
2101 // -1 if: left < right
2102 switch (in_type) {
Aart Bika19616e2016-02-01 18:57:58 -08002103 case Primitive::kPrimInt: {
2104 Register lhs = locations->InAt(0).AsRegister<Register>();
2105 Register rhs = locations->InAt(1).AsRegister<Register>();
2106 __ Slt(TMP, lhs, rhs);
2107 __ Slt(res, rhs, lhs);
2108 __ Subu(res, res, TMP);
2109 break;
2110 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002111 case Primitive::kPrimLong: {
2112 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002113 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2114 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2115 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2116 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2117 // TODO: more efficient (direct) comparison with a constant.
2118 __ Slt(TMP, lhs_high, rhs_high);
2119 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2120 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2121 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2122 __ Sltu(TMP, lhs_low, rhs_low);
2123 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2124 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2125 __ Bind(&done);
2126 break;
2127 }
2128
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002129 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002130 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002131 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2132 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2133 MipsLabel done;
2134 if (isR6) {
2135 __ CmpEqS(FTMP, lhs, rhs);
2136 __ LoadConst32(res, 0);
2137 __ Bc1nez(FTMP, &done);
2138 if (gt_bias) {
2139 __ CmpLtS(FTMP, lhs, rhs);
2140 __ LoadConst32(res, -1);
2141 __ Bc1nez(FTMP, &done);
2142 __ LoadConst32(res, 1);
2143 } else {
2144 __ CmpLtS(FTMP, rhs, lhs);
2145 __ LoadConst32(res, 1);
2146 __ Bc1nez(FTMP, &done);
2147 __ LoadConst32(res, -1);
2148 }
2149 } else {
2150 if (gt_bias) {
2151 __ ColtS(0, lhs, rhs);
2152 __ LoadConst32(res, -1);
2153 __ Bc1t(0, &done);
2154 __ CeqS(0, lhs, rhs);
2155 __ LoadConst32(res, 1);
2156 __ Movt(res, ZERO, 0);
2157 } else {
2158 __ ColtS(0, rhs, lhs);
2159 __ LoadConst32(res, 1);
2160 __ Bc1t(0, &done);
2161 __ CeqS(0, lhs, rhs);
2162 __ LoadConst32(res, -1);
2163 __ Movt(res, ZERO, 0);
2164 }
2165 }
2166 __ Bind(&done);
2167 break;
2168 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002169 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002170 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002171 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2172 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2173 MipsLabel done;
2174 if (isR6) {
2175 __ CmpEqD(FTMP, lhs, rhs);
2176 __ LoadConst32(res, 0);
2177 __ Bc1nez(FTMP, &done);
2178 if (gt_bias) {
2179 __ CmpLtD(FTMP, lhs, rhs);
2180 __ LoadConst32(res, -1);
2181 __ Bc1nez(FTMP, &done);
2182 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002183 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002184 __ CmpLtD(FTMP, rhs, lhs);
2185 __ LoadConst32(res, 1);
2186 __ Bc1nez(FTMP, &done);
2187 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188 }
2189 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002190 if (gt_bias) {
2191 __ ColtD(0, lhs, rhs);
2192 __ LoadConst32(res, -1);
2193 __ Bc1t(0, &done);
2194 __ CeqD(0, lhs, rhs);
2195 __ LoadConst32(res, 1);
2196 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002197 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002198 __ ColtD(0, rhs, lhs);
2199 __ LoadConst32(res, 1);
2200 __ Bc1t(0, &done);
2201 __ CeqD(0, lhs, rhs);
2202 __ LoadConst32(res, -1);
2203 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002204 }
2205 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002206 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002207 break;
2208 }
2209
2210 default:
2211 LOG(FATAL) << "Unimplemented compare type " << in_type;
2212 }
2213}
2214
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002215void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002217 switch (instruction->InputAt(0)->GetType()) {
2218 default:
2219 case Primitive::kPrimLong:
2220 locations->SetInAt(0, Location::RequiresRegister());
2221 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2222 break;
2223
2224 case Primitive::kPrimFloat:
2225 case Primitive::kPrimDouble:
2226 locations->SetInAt(0, Location::RequiresFpuRegister());
2227 locations->SetInAt(1, Location::RequiresFpuRegister());
2228 break;
2229 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002230 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 }
2233}
2234
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002235void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002236 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002237 return;
2238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241 LocationSummary* locations = instruction->GetLocations();
2242 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002243 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002244
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002245 switch (type) {
2246 default:
2247 // Integer case.
2248 GenerateIntCompare(instruction->GetCondition(), locations);
2249 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002251 case Primitive::kPrimLong:
2252 // TODO: don't use branches.
2253 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 break;
2255
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002256 case Primitive::kPrimFloat:
2257 case Primitive::kPrimDouble:
2258 // TODO: don't use branches.
2259 GenerateFpCompareAndBranch(instruction->GetCondition(),
2260 instruction->IsGtBias(),
2261 type,
2262 locations,
2263 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 break;
2265 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002266
2267 // Convert the branches into the result.
2268 MipsLabel done;
2269
2270 // False case: result = 0.
2271 __ LoadConst32(dst, 0);
2272 __ B(&done);
2273
2274 // True case: result = 1.
2275 __ Bind(&true_label);
2276 __ LoadConst32(dst, 1);
2277 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278}
2279
Alexey Frunze7e99e052015-11-24 19:28:01 -08002280void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2281 DCHECK(instruction->IsDiv() || instruction->IsRem());
2282 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2283
2284 LocationSummary* locations = instruction->GetLocations();
2285 Location second = locations->InAt(1);
2286 DCHECK(second.IsConstant());
2287
2288 Register out = locations->Out().AsRegister<Register>();
2289 Register dividend = locations->InAt(0).AsRegister<Register>();
2290 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2291 DCHECK(imm == 1 || imm == -1);
2292
2293 if (instruction->IsRem()) {
2294 __ Move(out, ZERO);
2295 } else {
2296 if (imm == -1) {
2297 __ Subu(out, ZERO, dividend);
2298 } else if (out != dividend) {
2299 __ Move(out, dividend);
2300 }
2301 }
2302}
2303
2304void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2305 DCHECK(instruction->IsDiv() || instruction->IsRem());
2306 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2307
2308 LocationSummary* locations = instruction->GetLocations();
2309 Location second = locations->InAt(1);
2310 DCHECK(second.IsConstant());
2311
2312 Register out = locations->Out().AsRegister<Register>();
2313 Register dividend = locations->InAt(0).AsRegister<Register>();
2314 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002315 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002316 int ctz_imm = CTZ(abs_imm);
2317
2318 if (instruction->IsDiv()) {
2319 if (ctz_imm == 1) {
2320 // Fast path for division by +/-2, which is very common.
2321 __ Srl(TMP, dividend, 31);
2322 } else {
2323 __ Sra(TMP, dividend, 31);
2324 __ Srl(TMP, TMP, 32 - ctz_imm);
2325 }
2326 __ Addu(out, dividend, TMP);
2327 __ Sra(out, out, ctz_imm);
2328 if (imm < 0) {
2329 __ Subu(out, ZERO, out);
2330 }
2331 } else {
2332 if (ctz_imm == 1) {
2333 // Fast path for modulo +/-2, which is very common.
2334 __ Sra(TMP, dividend, 31);
2335 __ Subu(out, dividend, TMP);
2336 __ Andi(out, out, 1);
2337 __ Addu(out, out, TMP);
2338 } else {
2339 __ Sra(TMP, dividend, 31);
2340 __ Srl(TMP, TMP, 32 - ctz_imm);
2341 __ Addu(out, dividend, TMP);
2342 if (IsUint<16>(abs_imm - 1)) {
2343 __ Andi(out, out, abs_imm - 1);
2344 } else {
2345 __ Sll(out, out, 32 - ctz_imm);
2346 __ Srl(out, out, 32 - ctz_imm);
2347 }
2348 __ Subu(out, out, TMP);
2349 }
2350 }
2351}
2352
2353void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2354 DCHECK(instruction->IsDiv() || instruction->IsRem());
2355 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2356
2357 LocationSummary* locations = instruction->GetLocations();
2358 Location second = locations->InAt(1);
2359 DCHECK(second.IsConstant());
2360
2361 Register out = locations->Out().AsRegister<Register>();
2362 Register dividend = locations->InAt(0).AsRegister<Register>();
2363 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2364
2365 int64_t magic;
2366 int shift;
2367 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2368
2369 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2370
2371 __ LoadConst32(TMP, magic);
2372 if (isR6) {
2373 __ MuhR6(TMP, dividend, TMP);
2374 } else {
2375 __ MultR2(dividend, TMP);
2376 __ Mfhi(TMP);
2377 }
2378 if (imm > 0 && magic < 0) {
2379 __ Addu(TMP, TMP, dividend);
2380 } else if (imm < 0 && magic > 0) {
2381 __ Subu(TMP, TMP, dividend);
2382 }
2383
2384 if (shift != 0) {
2385 __ Sra(TMP, TMP, shift);
2386 }
2387
2388 if (instruction->IsDiv()) {
2389 __ Sra(out, TMP, 31);
2390 __ Subu(out, TMP, out);
2391 } else {
2392 __ Sra(AT, TMP, 31);
2393 __ Subu(AT, TMP, AT);
2394 __ LoadConst32(TMP, imm);
2395 if (isR6) {
2396 __ MulR6(TMP, AT, TMP);
2397 } else {
2398 __ MulR2(TMP, AT, TMP);
2399 }
2400 __ Subu(out, dividend, TMP);
2401 }
2402}
2403
2404void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2405 DCHECK(instruction->IsDiv() || instruction->IsRem());
2406 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2407
2408 LocationSummary* locations = instruction->GetLocations();
2409 Register out = locations->Out().AsRegister<Register>();
2410 Location second = locations->InAt(1);
2411
2412 if (second.IsConstant()) {
2413 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2414 if (imm == 0) {
2415 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2416 } else if (imm == 1 || imm == -1) {
2417 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002418 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002419 DivRemByPowerOfTwo(instruction);
2420 } else {
2421 DCHECK(imm <= -2 || imm >= 2);
2422 GenerateDivRemWithAnyConstant(instruction);
2423 }
2424 } else {
2425 Register dividend = locations->InAt(0).AsRegister<Register>();
2426 Register divisor = second.AsRegister<Register>();
2427 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2428 if (instruction->IsDiv()) {
2429 if (isR6) {
2430 __ DivR6(out, dividend, divisor);
2431 } else {
2432 __ DivR2(out, dividend, divisor);
2433 }
2434 } else {
2435 if (isR6) {
2436 __ ModR6(out, dividend, divisor);
2437 } else {
2438 __ ModR2(out, dividend, divisor);
2439 }
2440 }
2441 }
2442}
2443
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2445 Primitive::Type type = div->GetResultType();
2446 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2447 ? LocationSummary::kCall
2448 : LocationSummary::kNoCall;
2449
2450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2451
2452 switch (type) {
2453 case Primitive::kPrimInt:
2454 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002455 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2457 break;
2458
2459 case Primitive::kPrimLong: {
2460 InvokeRuntimeCallingConvention calling_convention;
2461 locations->SetInAt(0, Location::RegisterPairLocation(
2462 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2463 locations->SetInAt(1, Location::RegisterPairLocation(
2464 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2465 locations->SetOut(calling_convention.GetReturnLocation(type));
2466 break;
2467 }
2468
2469 case Primitive::kPrimFloat:
2470 case Primitive::kPrimDouble:
2471 locations->SetInAt(0, Location::RequiresFpuRegister());
2472 locations->SetInAt(1, Location::RequiresFpuRegister());
2473 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2474 break;
2475
2476 default:
2477 LOG(FATAL) << "Unexpected div type " << type;
2478 }
2479}
2480
2481void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2482 Primitive::Type type = instruction->GetType();
2483 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484
2485 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002486 case Primitive::kPrimInt:
2487 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002489 case Primitive::kPrimLong: {
2490 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2491 instruction,
2492 instruction->GetDexPc(),
2493 nullptr,
2494 IsDirectEntrypoint(kQuickLdiv));
2495 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2496 break;
2497 }
2498 case Primitive::kPrimFloat:
2499 case Primitive::kPrimDouble: {
2500 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2501 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2502 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2503 if (type == Primitive::kPrimFloat) {
2504 __ DivS(dst, lhs, rhs);
2505 } else {
2506 __ DivD(dst, lhs, rhs);
2507 }
2508 break;
2509 }
2510 default:
2511 LOG(FATAL) << "Unexpected div type " << type;
2512 }
2513}
2514
2515void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2516 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2517 ? LocationSummary::kCallOnSlowPath
2518 : LocationSummary::kNoCall;
2519 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2520 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2521 if (instruction->HasUses()) {
2522 locations->SetOut(Location::SameAsFirstInput());
2523 }
2524}
2525
2526void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2527 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2528 codegen_->AddSlowPath(slow_path);
2529 Location value = instruction->GetLocations()->InAt(0);
2530 Primitive::Type type = instruction->GetType();
2531
2532 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002533 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002534 case Primitive::kPrimByte:
2535 case Primitive::kPrimChar:
2536 case Primitive::kPrimShort:
2537 case Primitive::kPrimInt: {
2538 if (value.IsConstant()) {
2539 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2540 __ B(slow_path->GetEntryLabel());
2541 } else {
2542 // A division by a non-null constant is valid. We don't need to perform
2543 // any check, so simply fall through.
2544 }
2545 } else {
2546 DCHECK(value.IsRegister()) << value;
2547 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2548 }
2549 break;
2550 }
2551 case Primitive::kPrimLong: {
2552 if (value.IsConstant()) {
2553 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2554 __ B(slow_path->GetEntryLabel());
2555 } else {
2556 // A division by a non-null constant is valid. We don't need to perform
2557 // any check, so simply fall through.
2558 }
2559 } else {
2560 DCHECK(value.IsRegisterPair()) << value;
2561 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2562 __ Beqz(TMP, slow_path->GetEntryLabel());
2563 }
2564 break;
2565 }
2566 default:
2567 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2568 }
2569}
2570
2571void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2572 LocationSummary* locations =
2573 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2574 locations->SetOut(Location::ConstantLocation(constant));
2575}
2576
2577void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2578 // Will be generated at use site.
2579}
2580
2581void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2582 exit->SetLocations(nullptr);
2583}
2584
2585void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2586}
2587
2588void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2591 locations->SetOut(Location::ConstantLocation(constant));
2592}
2593
2594void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2595 // Will be generated at use site.
2596}
2597
2598void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2599 got->SetLocations(nullptr);
2600}
2601
2602void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2603 DCHECK(!successor->IsExitBlock());
2604 HBasicBlock* block = got->GetBlock();
2605 HInstruction* previous = got->GetPrevious();
2606 HLoopInformation* info = block->GetLoopInformation();
2607
2608 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2609 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2610 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2611 return;
2612 }
2613 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2614 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2615 }
2616 if (!codegen_->GoesToNextBlock(block, successor)) {
2617 __ B(codegen_->GetLabelOf(successor));
2618 }
2619}
2620
2621void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2622 HandleGoto(got, got->GetSuccessor());
2623}
2624
2625void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2626 try_boundary->SetLocations(nullptr);
2627}
2628
2629void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2630 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2631 if (!successor->IsExitBlock()) {
2632 HandleGoto(try_boundary, successor);
2633 }
2634}
2635
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002636void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2637 LocationSummary* locations) {
2638 Register dst = locations->Out().AsRegister<Register>();
2639 Register lhs = locations->InAt(0).AsRegister<Register>();
2640 Location rhs_location = locations->InAt(1);
2641 Register rhs_reg = ZERO;
2642 int64_t rhs_imm = 0;
2643 bool use_imm = rhs_location.IsConstant();
2644 if (use_imm) {
2645 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2646 } else {
2647 rhs_reg = rhs_location.AsRegister<Register>();
2648 }
2649
2650 switch (cond) {
2651 case kCondEQ:
2652 case kCondNE:
2653 if (use_imm && IsUint<16>(rhs_imm)) {
2654 __ Xori(dst, lhs, rhs_imm);
2655 } else {
2656 if (use_imm) {
2657 rhs_reg = TMP;
2658 __ LoadConst32(rhs_reg, rhs_imm);
2659 }
2660 __ Xor(dst, lhs, rhs_reg);
2661 }
2662 if (cond == kCondEQ) {
2663 __ Sltiu(dst, dst, 1);
2664 } else {
2665 __ Sltu(dst, ZERO, dst);
2666 }
2667 break;
2668
2669 case kCondLT:
2670 case kCondGE:
2671 if (use_imm && IsInt<16>(rhs_imm)) {
2672 __ Slti(dst, lhs, rhs_imm);
2673 } else {
2674 if (use_imm) {
2675 rhs_reg = TMP;
2676 __ LoadConst32(rhs_reg, rhs_imm);
2677 }
2678 __ Slt(dst, lhs, rhs_reg);
2679 }
2680 if (cond == kCondGE) {
2681 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2682 // only the slt instruction but no sge.
2683 __ Xori(dst, dst, 1);
2684 }
2685 break;
2686
2687 case kCondLE:
2688 case kCondGT:
2689 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2690 // Simulate lhs <= rhs via lhs < rhs + 1.
2691 __ Slti(dst, lhs, rhs_imm + 1);
2692 if (cond == kCondGT) {
2693 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2694 // only the slti instruction but no sgti.
2695 __ Xori(dst, dst, 1);
2696 }
2697 } else {
2698 if (use_imm) {
2699 rhs_reg = TMP;
2700 __ LoadConst32(rhs_reg, rhs_imm);
2701 }
2702 __ Slt(dst, rhs_reg, lhs);
2703 if (cond == kCondLE) {
2704 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2705 // only the slt instruction but no sle.
2706 __ Xori(dst, dst, 1);
2707 }
2708 }
2709 break;
2710
2711 case kCondB:
2712 case kCondAE:
2713 if (use_imm && IsInt<16>(rhs_imm)) {
2714 // Sltiu sign-extends its 16-bit immediate operand before
2715 // the comparison and thus lets us compare directly with
2716 // unsigned values in the ranges [0, 0x7fff] and
2717 // [0xffff8000, 0xffffffff].
2718 __ Sltiu(dst, lhs, rhs_imm);
2719 } else {
2720 if (use_imm) {
2721 rhs_reg = TMP;
2722 __ LoadConst32(rhs_reg, rhs_imm);
2723 }
2724 __ Sltu(dst, lhs, rhs_reg);
2725 }
2726 if (cond == kCondAE) {
2727 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2728 // only the sltu instruction but no sgeu.
2729 __ Xori(dst, dst, 1);
2730 }
2731 break;
2732
2733 case kCondBE:
2734 case kCondA:
2735 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2736 // Simulate lhs <= rhs via lhs < rhs + 1.
2737 // Note that this only works if rhs + 1 does not overflow
2738 // to 0, hence the check above.
2739 // Sltiu sign-extends its 16-bit immediate operand before
2740 // the comparison and thus lets us compare directly with
2741 // unsigned values in the ranges [0, 0x7fff] and
2742 // [0xffff8000, 0xffffffff].
2743 __ Sltiu(dst, lhs, rhs_imm + 1);
2744 if (cond == kCondA) {
2745 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2746 // only the sltiu instruction but no sgtiu.
2747 __ Xori(dst, dst, 1);
2748 }
2749 } else {
2750 if (use_imm) {
2751 rhs_reg = TMP;
2752 __ LoadConst32(rhs_reg, rhs_imm);
2753 }
2754 __ Sltu(dst, rhs_reg, lhs);
2755 if (cond == kCondBE) {
2756 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2757 // only the sltu instruction but no sleu.
2758 __ Xori(dst, dst, 1);
2759 }
2760 }
2761 break;
2762 }
2763}
2764
2765void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2766 LocationSummary* locations,
2767 MipsLabel* label) {
2768 Register lhs = locations->InAt(0).AsRegister<Register>();
2769 Location rhs_location = locations->InAt(1);
2770 Register rhs_reg = ZERO;
2771 int32_t rhs_imm = 0;
2772 bool use_imm = rhs_location.IsConstant();
2773 if (use_imm) {
2774 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2775 } else {
2776 rhs_reg = rhs_location.AsRegister<Register>();
2777 }
2778
2779 if (use_imm && rhs_imm == 0) {
2780 switch (cond) {
2781 case kCondEQ:
2782 case kCondBE: // <= 0 if zero
2783 __ Beqz(lhs, label);
2784 break;
2785 case kCondNE:
2786 case kCondA: // > 0 if non-zero
2787 __ Bnez(lhs, label);
2788 break;
2789 case kCondLT:
2790 __ Bltz(lhs, label);
2791 break;
2792 case kCondGE:
2793 __ Bgez(lhs, label);
2794 break;
2795 case kCondLE:
2796 __ Blez(lhs, label);
2797 break;
2798 case kCondGT:
2799 __ Bgtz(lhs, label);
2800 break;
2801 case kCondB: // always false
2802 break;
2803 case kCondAE: // always true
2804 __ B(label);
2805 break;
2806 }
2807 } else {
2808 if (use_imm) {
2809 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2810 rhs_reg = TMP;
2811 __ LoadConst32(rhs_reg, rhs_imm);
2812 }
2813 switch (cond) {
2814 case kCondEQ:
2815 __ Beq(lhs, rhs_reg, label);
2816 break;
2817 case kCondNE:
2818 __ Bne(lhs, rhs_reg, label);
2819 break;
2820 case kCondLT:
2821 __ Blt(lhs, rhs_reg, label);
2822 break;
2823 case kCondGE:
2824 __ Bge(lhs, rhs_reg, label);
2825 break;
2826 case kCondLE:
2827 __ Bge(rhs_reg, lhs, label);
2828 break;
2829 case kCondGT:
2830 __ Blt(rhs_reg, lhs, label);
2831 break;
2832 case kCondB:
2833 __ Bltu(lhs, rhs_reg, label);
2834 break;
2835 case kCondAE:
2836 __ Bgeu(lhs, rhs_reg, label);
2837 break;
2838 case kCondBE:
2839 __ Bgeu(rhs_reg, lhs, label);
2840 break;
2841 case kCondA:
2842 __ Bltu(rhs_reg, lhs, label);
2843 break;
2844 }
2845 }
2846}
2847
2848void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2849 LocationSummary* locations,
2850 MipsLabel* label) {
2851 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2852 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2853 Location rhs_location = locations->InAt(1);
2854 Register rhs_high = ZERO;
2855 Register rhs_low = ZERO;
2856 int64_t imm = 0;
2857 uint32_t imm_high = 0;
2858 uint32_t imm_low = 0;
2859 bool use_imm = rhs_location.IsConstant();
2860 if (use_imm) {
2861 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2862 imm_high = High32Bits(imm);
2863 imm_low = Low32Bits(imm);
2864 } else {
2865 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2866 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2867 }
2868
2869 if (use_imm && imm == 0) {
2870 switch (cond) {
2871 case kCondEQ:
2872 case kCondBE: // <= 0 if zero
2873 __ Or(TMP, lhs_high, lhs_low);
2874 __ Beqz(TMP, label);
2875 break;
2876 case kCondNE:
2877 case kCondA: // > 0 if non-zero
2878 __ Or(TMP, lhs_high, lhs_low);
2879 __ Bnez(TMP, label);
2880 break;
2881 case kCondLT:
2882 __ Bltz(lhs_high, label);
2883 break;
2884 case kCondGE:
2885 __ Bgez(lhs_high, label);
2886 break;
2887 case kCondLE:
2888 __ Or(TMP, lhs_high, lhs_low);
2889 __ Sra(AT, lhs_high, 31);
2890 __ Bgeu(AT, TMP, label);
2891 break;
2892 case kCondGT:
2893 __ Or(TMP, lhs_high, lhs_low);
2894 __ Sra(AT, lhs_high, 31);
2895 __ Bltu(AT, TMP, label);
2896 break;
2897 case kCondB: // always false
2898 break;
2899 case kCondAE: // always true
2900 __ B(label);
2901 break;
2902 }
2903 } else if (use_imm) {
2904 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2905 switch (cond) {
2906 case kCondEQ:
2907 __ LoadConst32(TMP, imm_high);
2908 __ Xor(TMP, TMP, lhs_high);
2909 __ LoadConst32(AT, imm_low);
2910 __ Xor(AT, AT, lhs_low);
2911 __ Or(TMP, TMP, AT);
2912 __ Beqz(TMP, label);
2913 break;
2914 case kCondNE:
2915 __ LoadConst32(TMP, imm_high);
2916 __ Xor(TMP, TMP, lhs_high);
2917 __ LoadConst32(AT, imm_low);
2918 __ Xor(AT, AT, lhs_low);
2919 __ Or(TMP, TMP, AT);
2920 __ Bnez(TMP, label);
2921 break;
2922 case kCondLT:
2923 __ LoadConst32(TMP, imm_high);
2924 __ Blt(lhs_high, TMP, label);
2925 __ Slt(TMP, TMP, lhs_high);
2926 __ LoadConst32(AT, imm_low);
2927 __ Sltu(AT, lhs_low, AT);
2928 __ Blt(TMP, AT, label);
2929 break;
2930 case kCondGE:
2931 __ LoadConst32(TMP, imm_high);
2932 __ Blt(TMP, lhs_high, label);
2933 __ Slt(TMP, lhs_high, TMP);
2934 __ LoadConst32(AT, imm_low);
2935 __ Sltu(AT, lhs_low, AT);
2936 __ Or(TMP, TMP, AT);
2937 __ Beqz(TMP, label);
2938 break;
2939 case kCondLE:
2940 __ LoadConst32(TMP, imm_high);
2941 __ Blt(lhs_high, TMP, label);
2942 __ Slt(TMP, TMP, lhs_high);
2943 __ LoadConst32(AT, imm_low);
2944 __ Sltu(AT, AT, lhs_low);
2945 __ Or(TMP, TMP, AT);
2946 __ Beqz(TMP, label);
2947 break;
2948 case kCondGT:
2949 __ LoadConst32(TMP, imm_high);
2950 __ Blt(TMP, lhs_high, label);
2951 __ Slt(TMP, lhs_high, TMP);
2952 __ LoadConst32(AT, imm_low);
2953 __ Sltu(AT, AT, lhs_low);
2954 __ Blt(TMP, AT, label);
2955 break;
2956 case kCondB:
2957 __ LoadConst32(TMP, imm_high);
2958 __ Bltu(lhs_high, TMP, label);
2959 __ Sltu(TMP, TMP, lhs_high);
2960 __ LoadConst32(AT, imm_low);
2961 __ Sltu(AT, lhs_low, AT);
2962 __ Blt(TMP, AT, label);
2963 break;
2964 case kCondAE:
2965 __ LoadConst32(TMP, imm_high);
2966 __ Bltu(TMP, lhs_high, label);
2967 __ Sltu(TMP, lhs_high, TMP);
2968 __ LoadConst32(AT, imm_low);
2969 __ Sltu(AT, lhs_low, AT);
2970 __ Or(TMP, TMP, AT);
2971 __ Beqz(TMP, label);
2972 break;
2973 case kCondBE:
2974 __ LoadConst32(TMP, imm_high);
2975 __ Bltu(lhs_high, TMP, label);
2976 __ Sltu(TMP, TMP, lhs_high);
2977 __ LoadConst32(AT, imm_low);
2978 __ Sltu(AT, AT, lhs_low);
2979 __ Or(TMP, TMP, AT);
2980 __ Beqz(TMP, label);
2981 break;
2982 case kCondA:
2983 __ LoadConst32(TMP, imm_high);
2984 __ Bltu(TMP, lhs_high, label);
2985 __ Sltu(TMP, lhs_high, TMP);
2986 __ LoadConst32(AT, imm_low);
2987 __ Sltu(AT, AT, lhs_low);
2988 __ Blt(TMP, AT, label);
2989 break;
2990 }
2991 } else {
2992 switch (cond) {
2993 case kCondEQ:
2994 __ Xor(TMP, lhs_high, rhs_high);
2995 __ Xor(AT, lhs_low, rhs_low);
2996 __ Or(TMP, TMP, AT);
2997 __ Beqz(TMP, label);
2998 break;
2999 case kCondNE:
3000 __ Xor(TMP, lhs_high, rhs_high);
3001 __ Xor(AT, lhs_low, rhs_low);
3002 __ Or(TMP, TMP, AT);
3003 __ Bnez(TMP, label);
3004 break;
3005 case kCondLT:
3006 __ Blt(lhs_high, rhs_high, label);
3007 __ Slt(TMP, rhs_high, lhs_high);
3008 __ Sltu(AT, lhs_low, rhs_low);
3009 __ Blt(TMP, AT, label);
3010 break;
3011 case kCondGE:
3012 __ Blt(rhs_high, lhs_high, label);
3013 __ Slt(TMP, lhs_high, rhs_high);
3014 __ Sltu(AT, lhs_low, rhs_low);
3015 __ Or(TMP, TMP, AT);
3016 __ Beqz(TMP, label);
3017 break;
3018 case kCondLE:
3019 __ Blt(lhs_high, rhs_high, label);
3020 __ Slt(TMP, rhs_high, lhs_high);
3021 __ Sltu(AT, rhs_low, lhs_low);
3022 __ Or(TMP, TMP, AT);
3023 __ Beqz(TMP, label);
3024 break;
3025 case kCondGT:
3026 __ Blt(rhs_high, lhs_high, label);
3027 __ Slt(TMP, lhs_high, rhs_high);
3028 __ Sltu(AT, rhs_low, lhs_low);
3029 __ Blt(TMP, AT, label);
3030 break;
3031 case kCondB:
3032 __ Bltu(lhs_high, rhs_high, label);
3033 __ Sltu(TMP, rhs_high, lhs_high);
3034 __ Sltu(AT, lhs_low, rhs_low);
3035 __ Blt(TMP, AT, label);
3036 break;
3037 case kCondAE:
3038 __ Bltu(rhs_high, lhs_high, label);
3039 __ Sltu(TMP, lhs_high, rhs_high);
3040 __ Sltu(AT, lhs_low, rhs_low);
3041 __ Or(TMP, TMP, AT);
3042 __ Beqz(TMP, label);
3043 break;
3044 case kCondBE:
3045 __ Bltu(lhs_high, rhs_high, label);
3046 __ Sltu(TMP, rhs_high, lhs_high);
3047 __ Sltu(AT, rhs_low, lhs_low);
3048 __ Or(TMP, TMP, AT);
3049 __ Beqz(TMP, label);
3050 break;
3051 case kCondA:
3052 __ Bltu(rhs_high, lhs_high, label);
3053 __ Sltu(TMP, lhs_high, rhs_high);
3054 __ Sltu(AT, rhs_low, lhs_low);
3055 __ Blt(TMP, AT, label);
3056 break;
3057 }
3058 }
3059}
3060
3061void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3062 bool gt_bias,
3063 Primitive::Type type,
3064 LocationSummary* locations,
3065 MipsLabel* label) {
3066 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3067 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3068 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3069 if (type == Primitive::kPrimFloat) {
3070 if (isR6) {
3071 switch (cond) {
3072 case kCondEQ:
3073 __ CmpEqS(FTMP, lhs, rhs);
3074 __ Bc1nez(FTMP, label);
3075 break;
3076 case kCondNE:
3077 __ CmpEqS(FTMP, lhs, rhs);
3078 __ Bc1eqz(FTMP, label);
3079 break;
3080 case kCondLT:
3081 if (gt_bias) {
3082 __ CmpLtS(FTMP, lhs, rhs);
3083 } else {
3084 __ CmpUltS(FTMP, lhs, rhs);
3085 }
3086 __ Bc1nez(FTMP, label);
3087 break;
3088 case kCondLE:
3089 if (gt_bias) {
3090 __ CmpLeS(FTMP, lhs, rhs);
3091 } else {
3092 __ CmpUleS(FTMP, lhs, rhs);
3093 }
3094 __ Bc1nez(FTMP, label);
3095 break;
3096 case kCondGT:
3097 if (gt_bias) {
3098 __ CmpUltS(FTMP, rhs, lhs);
3099 } else {
3100 __ CmpLtS(FTMP, rhs, lhs);
3101 }
3102 __ Bc1nez(FTMP, label);
3103 break;
3104 case kCondGE:
3105 if (gt_bias) {
3106 __ CmpUleS(FTMP, rhs, lhs);
3107 } else {
3108 __ CmpLeS(FTMP, rhs, lhs);
3109 }
3110 __ Bc1nez(FTMP, label);
3111 break;
3112 default:
3113 LOG(FATAL) << "Unexpected non-floating-point condition";
3114 }
3115 } else {
3116 switch (cond) {
3117 case kCondEQ:
3118 __ CeqS(0, lhs, rhs);
3119 __ Bc1t(0, label);
3120 break;
3121 case kCondNE:
3122 __ CeqS(0, lhs, rhs);
3123 __ Bc1f(0, label);
3124 break;
3125 case kCondLT:
3126 if (gt_bias) {
3127 __ ColtS(0, lhs, rhs);
3128 } else {
3129 __ CultS(0, lhs, rhs);
3130 }
3131 __ Bc1t(0, label);
3132 break;
3133 case kCondLE:
3134 if (gt_bias) {
3135 __ ColeS(0, lhs, rhs);
3136 } else {
3137 __ CuleS(0, lhs, rhs);
3138 }
3139 __ Bc1t(0, label);
3140 break;
3141 case kCondGT:
3142 if (gt_bias) {
3143 __ CultS(0, rhs, lhs);
3144 } else {
3145 __ ColtS(0, rhs, lhs);
3146 }
3147 __ Bc1t(0, label);
3148 break;
3149 case kCondGE:
3150 if (gt_bias) {
3151 __ CuleS(0, rhs, lhs);
3152 } else {
3153 __ ColeS(0, rhs, lhs);
3154 }
3155 __ Bc1t(0, label);
3156 break;
3157 default:
3158 LOG(FATAL) << "Unexpected non-floating-point condition";
3159 }
3160 }
3161 } else {
3162 DCHECK_EQ(type, Primitive::kPrimDouble);
3163 if (isR6) {
3164 switch (cond) {
3165 case kCondEQ:
3166 __ CmpEqD(FTMP, lhs, rhs);
3167 __ Bc1nez(FTMP, label);
3168 break;
3169 case kCondNE:
3170 __ CmpEqD(FTMP, lhs, rhs);
3171 __ Bc1eqz(FTMP, label);
3172 break;
3173 case kCondLT:
3174 if (gt_bias) {
3175 __ CmpLtD(FTMP, lhs, rhs);
3176 } else {
3177 __ CmpUltD(FTMP, lhs, rhs);
3178 }
3179 __ Bc1nez(FTMP, label);
3180 break;
3181 case kCondLE:
3182 if (gt_bias) {
3183 __ CmpLeD(FTMP, lhs, rhs);
3184 } else {
3185 __ CmpUleD(FTMP, lhs, rhs);
3186 }
3187 __ Bc1nez(FTMP, label);
3188 break;
3189 case kCondGT:
3190 if (gt_bias) {
3191 __ CmpUltD(FTMP, rhs, lhs);
3192 } else {
3193 __ CmpLtD(FTMP, rhs, lhs);
3194 }
3195 __ Bc1nez(FTMP, label);
3196 break;
3197 case kCondGE:
3198 if (gt_bias) {
3199 __ CmpUleD(FTMP, rhs, lhs);
3200 } else {
3201 __ CmpLeD(FTMP, rhs, lhs);
3202 }
3203 __ Bc1nez(FTMP, label);
3204 break;
3205 default:
3206 LOG(FATAL) << "Unexpected non-floating-point condition";
3207 }
3208 } else {
3209 switch (cond) {
3210 case kCondEQ:
3211 __ CeqD(0, lhs, rhs);
3212 __ Bc1t(0, label);
3213 break;
3214 case kCondNE:
3215 __ CeqD(0, lhs, rhs);
3216 __ Bc1f(0, label);
3217 break;
3218 case kCondLT:
3219 if (gt_bias) {
3220 __ ColtD(0, lhs, rhs);
3221 } else {
3222 __ CultD(0, lhs, rhs);
3223 }
3224 __ Bc1t(0, label);
3225 break;
3226 case kCondLE:
3227 if (gt_bias) {
3228 __ ColeD(0, lhs, rhs);
3229 } else {
3230 __ CuleD(0, lhs, rhs);
3231 }
3232 __ Bc1t(0, label);
3233 break;
3234 case kCondGT:
3235 if (gt_bias) {
3236 __ CultD(0, rhs, lhs);
3237 } else {
3238 __ ColtD(0, rhs, lhs);
3239 }
3240 __ Bc1t(0, label);
3241 break;
3242 case kCondGE:
3243 if (gt_bias) {
3244 __ CuleD(0, rhs, lhs);
3245 } else {
3246 __ ColeD(0, rhs, lhs);
3247 }
3248 __ Bc1t(0, label);
3249 break;
3250 default:
3251 LOG(FATAL) << "Unexpected non-floating-point condition";
3252 }
3253 }
3254 }
3255}
3256
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003257void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003258 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003259 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003260 MipsLabel* false_target) {
3261 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003262
David Brazdil0debae72015-11-12 18:37:00 +00003263 if (true_target == nullptr && false_target == nullptr) {
3264 // Nothing to do. The code always falls through.
3265 return;
3266 } else if (cond->IsIntConstant()) {
3267 // Constant condition, statically compared against 1.
3268 if (cond->AsIntConstant()->IsOne()) {
3269 if (true_target != nullptr) {
3270 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003271 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272 } else {
David Brazdil0debae72015-11-12 18:37:00 +00003273 DCHECK(cond->AsIntConstant()->IsZero());
3274 if (false_target != nullptr) {
3275 __ B(false_target);
3276 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003277 }
David Brazdil0debae72015-11-12 18:37:00 +00003278 return;
3279 }
3280
3281 // The following code generates these patterns:
3282 // (1) true_target == nullptr && false_target != nullptr
3283 // - opposite condition true => branch to false_target
3284 // (2) true_target != nullptr && false_target == nullptr
3285 // - condition true => branch to true_target
3286 // (3) true_target != nullptr && false_target != nullptr
3287 // - condition true => branch to true_target
3288 // - branch to false_target
3289 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003290 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003291 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003292 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003293 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003294 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3295 } else {
3296 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3297 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298 } else {
3299 // The condition instruction has not been materialized, use its inputs as
3300 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003301 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003302 Primitive::Type type = condition->InputAt(0)->GetType();
3303 LocationSummary* locations = cond->GetLocations();
3304 IfCondition if_cond = condition->GetCondition();
3305 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003306
David Brazdil0debae72015-11-12 18:37:00 +00003307 if (true_target == nullptr) {
3308 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003309 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003310 }
3311
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003312 switch (type) {
3313 default:
3314 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3315 break;
3316 case Primitive::kPrimLong:
3317 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3318 break;
3319 case Primitive::kPrimFloat:
3320 case Primitive::kPrimDouble:
3321 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3322 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003323 }
3324 }
David Brazdil0debae72015-11-12 18:37:00 +00003325
3326 // If neither branch falls through (case 3), the conditional branch to `true_target`
3327 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3328 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003329 __ B(false_target);
3330 }
3331}
3332
3333void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003335 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003336 locations->SetInAt(0, Location::RequiresRegister());
3337 }
3338}
3339
3340void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003341 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3342 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3343 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3344 nullptr : codegen_->GetLabelOf(true_successor);
3345 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3346 nullptr : codegen_->GetLabelOf(false_successor);
3347 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003348}
3349
3350void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3351 LocationSummary* locations = new (GetGraph()->GetArena())
3352 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003353 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003354 locations->SetInAt(0, Location::RequiresRegister());
3355 }
3356}
3357
3358void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003359 SlowPathCodeMIPS* slow_path =
3360 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003361 GenerateTestAndBranch(deoptimize,
3362 /* condition_input_index */ 0,
3363 slow_path->GetEntryLabel(),
3364 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003365}
3366
David Brazdil74eb1b22015-12-14 11:44:01 +00003367void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3368 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3369 if (Primitive::IsFloatingPointType(select->GetType())) {
3370 locations->SetInAt(0, Location::RequiresFpuRegister());
3371 locations->SetInAt(1, Location::RequiresFpuRegister());
3372 } else {
3373 locations->SetInAt(0, Location::RequiresRegister());
3374 locations->SetInAt(1, Location::RequiresRegister());
3375 }
3376 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3377 locations->SetInAt(2, Location::RequiresRegister());
3378 }
3379 locations->SetOut(Location::SameAsFirstInput());
3380}
3381
3382void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3383 LocationSummary* locations = select->GetLocations();
3384 MipsLabel false_target;
3385 GenerateTestAndBranch(select,
3386 /* condition_input_index */ 2,
3387 /* true_target */ nullptr,
3388 &false_target);
3389 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3390 __ Bind(&false_target);
3391}
3392
David Srbecky0cf44932015-12-09 14:09:59 +00003393void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3394 new (GetGraph()->GetArena()) LocationSummary(info);
3395}
3396
3397void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyc7098ff2016-02-09 14:30:11 +00003398 codegen_->MaybeRecordNativeDebugInfo(info, info->GetDexPc());
3399}
3400
3401void CodeGeneratorMIPS::GenerateNop() {
3402 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003403}
3404
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003405void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3406 Primitive::Type field_type = field_info.GetFieldType();
3407 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3408 bool generate_volatile = field_info.IsVolatile() && is_wide;
3409 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3410 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3411
3412 locations->SetInAt(0, Location::RequiresRegister());
3413 if (generate_volatile) {
3414 InvokeRuntimeCallingConvention calling_convention;
3415 // need A0 to hold base + offset
3416 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3417 if (field_type == Primitive::kPrimLong) {
3418 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3419 } else {
3420 locations->SetOut(Location::RequiresFpuRegister());
3421 // Need some temp core regs since FP results are returned in core registers
3422 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3423 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3424 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3425 }
3426 } else {
3427 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3428 locations->SetOut(Location::RequiresFpuRegister());
3429 } else {
3430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3431 }
3432 }
3433}
3434
3435void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3436 const FieldInfo& field_info,
3437 uint32_t dex_pc) {
3438 Primitive::Type type = field_info.GetFieldType();
3439 LocationSummary* locations = instruction->GetLocations();
3440 Register obj = locations->InAt(0).AsRegister<Register>();
3441 LoadOperandType load_type = kLoadUnsignedByte;
3442 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003443 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444
3445 switch (type) {
3446 case Primitive::kPrimBoolean:
3447 load_type = kLoadUnsignedByte;
3448 break;
3449 case Primitive::kPrimByte:
3450 load_type = kLoadSignedByte;
3451 break;
3452 case Primitive::kPrimShort:
3453 load_type = kLoadSignedHalfword;
3454 break;
3455 case Primitive::kPrimChar:
3456 load_type = kLoadUnsignedHalfword;
3457 break;
3458 case Primitive::kPrimInt:
3459 case Primitive::kPrimFloat:
3460 case Primitive::kPrimNot:
3461 load_type = kLoadWord;
3462 break;
3463 case Primitive::kPrimLong:
3464 case Primitive::kPrimDouble:
3465 load_type = kLoadDoubleword;
3466 break;
3467 case Primitive::kPrimVoid:
3468 LOG(FATAL) << "Unreachable type " << type;
3469 UNREACHABLE();
3470 }
3471
3472 if (is_volatile && load_type == kLoadDoubleword) {
3473 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003474 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003475 // Do implicit Null check
3476 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3477 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3478 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3479 instruction,
3480 dex_pc,
3481 nullptr,
3482 IsDirectEntrypoint(kQuickA64Load));
3483 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3484 if (type == Primitive::kPrimDouble) {
3485 // Need to move to FP regs since FP results are returned in core registers.
3486 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3487 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003488 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3489 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003490 }
3491 } else {
3492 if (!Primitive::IsFloatingPointType(type)) {
3493 Register dst;
3494 if (type == Primitive::kPrimLong) {
3495 DCHECK(locations->Out().IsRegisterPair());
3496 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003497 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3498 if (obj == dst) {
3499 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3500 codegen_->MaybeRecordImplicitNullCheck(instruction);
3501 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3502 } else {
3503 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3504 codegen_->MaybeRecordImplicitNullCheck(instruction);
3505 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507 } else {
3508 DCHECK(locations->Out().IsRegister());
3509 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003510 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003511 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003512 } else {
3513 DCHECK(locations->Out().IsFpuRegister());
3514 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3515 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003516 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003517 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003518 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003519 }
3520 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003521 // Longs are handled earlier.
3522 if (type != Primitive::kPrimLong) {
3523 codegen_->MaybeRecordImplicitNullCheck(instruction);
3524 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003525 }
3526
3527 if (is_volatile) {
3528 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3529 }
3530}
3531
3532void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3533 Primitive::Type field_type = field_info.GetFieldType();
3534 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3535 bool generate_volatile = field_info.IsVolatile() && is_wide;
3536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3537 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3538
3539 locations->SetInAt(0, Location::RequiresRegister());
3540 if (generate_volatile) {
3541 InvokeRuntimeCallingConvention calling_convention;
3542 // need A0 to hold base + offset
3543 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3544 if (field_type == Primitive::kPrimLong) {
3545 locations->SetInAt(1, Location::RegisterPairLocation(
3546 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3547 } else {
3548 locations->SetInAt(1, Location::RequiresFpuRegister());
3549 // Pass FP parameters in core registers.
3550 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3551 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3552 }
3553 } else {
3554 if (Primitive::IsFloatingPointType(field_type)) {
3555 locations->SetInAt(1, Location::RequiresFpuRegister());
3556 } else {
3557 locations->SetInAt(1, Location::RequiresRegister());
3558 }
3559 }
3560}
3561
3562void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3563 const FieldInfo& field_info,
3564 uint32_t dex_pc) {
3565 Primitive::Type type = field_info.GetFieldType();
3566 LocationSummary* locations = instruction->GetLocations();
3567 Register obj = locations->InAt(0).AsRegister<Register>();
3568 StoreOperandType store_type = kStoreByte;
3569 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003570 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003571
3572 switch (type) {
3573 case Primitive::kPrimBoolean:
3574 case Primitive::kPrimByte:
3575 store_type = kStoreByte;
3576 break;
3577 case Primitive::kPrimShort:
3578 case Primitive::kPrimChar:
3579 store_type = kStoreHalfword;
3580 break;
3581 case Primitive::kPrimInt:
3582 case Primitive::kPrimFloat:
3583 case Primitive::kPrimNot:
3584 store_type = kStoreWord;
3585 break;
3586 case Primitive::kPrimLong:
3587 case Primitive::kPrimDouble:
3588 store_type = kStoreDoubleword;
3589 break;
3590 case Primitive::kPrimVoid:
3591 LOG(FATAL) << "Unreachable type " << type;
3592 UNREACHABLE();
3593 }
3594
3595 if (is_volatile) {
3596 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3597 }
3598
3599 if (is_volatile && store_type == kStoreDoubleword) {
3600 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003601 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003602 // Do implicit Null check.
3603 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3604 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3605 if (type == Primitive::kPrimDouble) {
3606 // Pass FP parameters in core registers.
3607 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3608 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003609 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3610 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003611 }
3612 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3613 instruction,
3614 dex_pc,
3615 nullptr,
3616 IsDirectEntrypoint(kQuickA64Store));
3617 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3618 } else {
3619 if (!Primitive::IsFloatingPointType(type)) {
3620 Register src;
3621 if (type == Primitive::kPrimLong) {
3622 DCHECK(locations->InAt(1).IsRegisterPair());
3623 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003624 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3625 __ StoreToOffset(kStoreWord, src, obj, offset);
3626 codegen_->MaybeRecordImplicitNullCheck(instruction);
3627 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 } else {
3629 DCHECK(locations->InAt(1).IsRegister());
3630 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003631 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633 } else {
3634 DCHECK(locations->InAt(1).IsFpuRegister());
3635 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3636 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003637 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003638 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003639 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640 }
3641 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003642 // Longs are handled earlier.
3643 if (type != Primitive::kPrimLong) {
3644 codegen_->MaybeRecordImplicitNullCheck(instruction);
3645 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003646 }
3647
3648 // TODO: memory barriers?
3649 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3650 DCHECK(locations->InAt(1).IsRegister());
3651 Register src = locations->InAt(1).AsRegister<Register>();
3652 codegen_->MarkGCCard(obj, src);
3653 }
3654
3655 if (is_volatile) {
3656 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3657 }
3658}
3659
3660void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3661 HandleFieldGet(instruction, instruction->GetFieldInfo());
3662}
3663
3664void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3665 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3666}
3667
3668void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3669 HandleFieldSet(instruction, instruction->GetFieldInfo());
3670}
3671
3672void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3673 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3674}
3675
3676void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3677 LocationSummary::CallKind call_kind =
3678 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3680 locations->SetInAt(0, Location::RequiresRegister());
3681 locations->SetInAt(1, Location::RequiresRegister());
3682 // The output does overlap inputs.
3683 // Note that TypeCheckSlowPathMIPS uses this register too.
3684 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3685}
3686
3687void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3688 LocationSummary* locations = instruction->GetLocations();
3689 Register obj = locations->InAt(0).AsRegister<Register>();
3690 Register cls = locations->InAt(1).AsRegister<Register>();
3691 Register out = locations->Out().AsRegister<Register>();
3692
3693 MipsLabel done;
3694
3695 // Return 0 if `obj` is null.
3696 // TODO: Avoid this check if we know `obj` is not null.
3697 __ Move(out, ZERO);
3698 __ Beqz(obj, &done);
3699
3700 // Compare the class of `obj` with `cls`.
3701 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3702 if (instruction->IsExactCheck()) {
3703 // Classes must be equal for the instanceof to succeed.
3704 __ Xor(out, out, cls);
3705 __ Sltiu(out, out, 1);
3706 } else {
3707 // If the classes are not equal, we go into a slow path.
3708 DCHECK(locations->OnlyCallsOnSlowPath());
3709 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3710 codegen_->AddSlowPath(slow_path);
3711 __ Bne(out, cls, slow_path->GetEntryLabel());
3712 __ LoadConst32(out, 1);
3713 __ Bind(slow_path->GetExitLabel());
3714 }
3715
3716 __ Bind(&done);
3717}
3718
3719void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3721 locations->SetOut(Location::ConstantLocation(constant));
3722}
3723
3724void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3725 // Will be generated at use site.
3726}
3727
3728void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3729 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3730 locations->SetOut(Location::ConstantLocation(constant));
3731}
3732
3733void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3734 // Will be generated at use site.
3735}
3736
3737void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3738 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3739 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3740}
3741
3742void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3743 HandleInvoke(invoke);
3744 // The register T0 is required to be used for the hidden argument in
3745 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3746 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3747}
3748
3749void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3750 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3751 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3752 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3753 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3754 Location receiver = invoke->GetLocations()->InAt(0);
3755 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3756 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3757
3758 // Set the hidden argument.
3759 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3760 invoke->GetDexMethodIndex());
3761
3762 // temp = object->GetClass();
3763 if (receiver.IsStackSlot()) {
3764 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3765 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3766 } else {
3767 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3768 }
3769 codegen_->MaybeRecordImplicitNullCheck(invoke);
3770 // temp = temp->GetImtEntryAt(method_offset);
3771 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3772 // T9 = temp->GetEntryPoint();
3773 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3774 // T9();
3775 __ Jalr(T9);
3776 __ Nop();
3777 DCHECK(!codegen_->IsLeafMethod());
3778 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3779}
3780
3781void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003782 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3783 if (intrinsic.TryDispatch(invoke)) {
3784 return;
3785 }
3786
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003787 HandleInvoke(invoke);
3788}
3789
3790void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003791 // Explicit clinit checks triggered by static invokes must have been pruned by
3792 // art::PrepareForRegisterAllocation.
3793 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003794
Chris Larsen701566a2015-10-27 15:29:13 -07003795 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3796 if (intrinsic.TryDispatch(invoke)) {
3797 return;
3798 }
3799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800 HandleInvoke(invoke);
3801}
3802
Chris Larsen701566a2015-10-27 15:29:13 -07003803static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003804 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003805 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3806 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003807 return true;
3808 }
3809 return false;
3810}
3811
Vladimir Markodc151b22015-10-15 18:02:30 +01003812HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3813 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3814 MethodReference target_method ATTRIBUTE_UNUSED) {
3815 switch (desired_dispatch_info.method_load_kind) {
3816 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3817 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3818 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3819 return HInvokeStaticOrDirect::DispatchInfo {
3820 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3821 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3822 0u,
3823 0u
3824 };
3825 default:
3826 break;
3827 }
3828 switch (desired_dispatch_info.code_ptr_location) {
3829 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3830 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3831 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3832 return HInvokeStaticOrDirect::DispatchInfo {
3833 desired_dispatch_info.method_load_kind,
3834 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3835 desired_dispatch_info.method_load_data,
3836 0u
3837 };
3838 default:
3839 return desired_dispatch_info;
3840 }
3841}
3842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003843void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3844 // All registers are assumed to be correctly set up per the calling convention.
3845
3846 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3847 switch (invoke->GetMethodLoadKind()) {
3848 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3849 // temp = thread->string_init_entrypoint
3850 __ LoadFromOffset(kLoadWord,
3851 temp.AsRegister<Register>(),
3852 TR,
3853 invoke->GetStringInitOffset());
3854 break;
3855 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003856 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857 break;
3858 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3859 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3860 break;
3861 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003862 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003863 // TODO: Implement these types.
3864 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3865 LOG(FATAL) << "Unsupported";
3866 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003867 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003868 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003869 Register reg = temp.AsRegister<Register>();
3870 Register method_reg;
3871 if (current_method.IsRegister()) {
3872 method_reg = current_method.AsRegister<Register>();
3873 } else {
3874 // TODO: use the appropriate DCHECK() here if possible.
3875 // DCHECK(invoke->GetLocations()->Intrinsified());
3876 DCHECK(!current_method.IsValid());
3877 method_reg = reg;
3878 __ Lw(reg, SP, kCurrentMethodStackOffset);
3879 }
3880
3881 // temp = temp->dex_cache_resolved_methods_;
3882 __ LoadFromOffset(kLoadWord,
3883 reg,
3884 method_reg,
3885 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
3886 // temp = temp[index_in_cache]
3887 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3888 __ LoadFromOffset(kLoadWord,
3889 reg,
3890 reg,
3891 CodeGenerator::GetCachePointerOffset(index_in_cache));
3892 break;
3893 }
3894 }
3895
3896 switch (invoke->GetCodePtrLocation()) {
3897 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3898 __ Jalr(&frame_entry_label_, T9);
3899 break;
3900 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3901 // LR = invoke->GetDirectCodePtr();
3902 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3903 // LR()
3904 __ Jalr(T9);
3905 __ Nop();
3906 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003908 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3909 // TODO: Implement these types.
3910 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3911 LOG(FATAL) << "Unsupported";
3912 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003913 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3914 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003915 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916 T9,
3917 callee_method.AsRegister<Register>(),
3918 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3919 kMipsWordSize).Int32Value());
3920 // T9()
3921 __ Jalr(T9);
3922 __ Nop();
3923 break;
3924 }
3925 DCHECK(!IsLeafMethod());
3926}
3927
3928void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003929 // Explicit clinit checks triggered by static invokes must have been pruned by
3930 // art::PrepareForRegisterAllocation.
3931 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932
3933 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3934 return;
3935 }
3936
3937 LocationSummary* locations = invoke->GetLocations();
3938 codegen_->GenerateStaticOrDirectCall(invoke,
3939 locations->HasTemps()
3940 ? locations->GetTemp(0)
3941 : Location::NoLocation());
3942 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3943}
3944
Chris Larsen3acee732015-11-18 13:31:08 -08003945void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 LocationSummary* locations = invoke->GetLocations();
3947 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08003948 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3950 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3952 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3953
3954 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08003955 DCHECK(receiver.IsRegister());
3956 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3957 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003958 // temp = temp->GetMethodAt(method_offset);
3959 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3960 // T9 = temp->GetEntryPoint();
3961 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3962 // T9();
3963 __ Jalr(T9);
3964 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08003965}
3966
3967void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3968 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3969 return;
3970 }
3971
3972 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003973 DCHECK(!codegen_->IsLeafMethod());
3974 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3975}
3976
3977void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003978 InvokeRuntimeCallingConvention calling_convention;
3979 CodeGenerator::CreateLoadClassLocationSummary(
3980 cls,
3981 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3982 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003983}
3984
3985void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3986 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003987 if (cls->NeedsAccessCheck()) {
3988 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3989 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3990 cls,
3991 cls->GetDexPc(),
3992 nullptr,
3993 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003994 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003995 return;
3996 }
3997
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003998 Register out = locations->Out().AsRegister<Register>();
3999 Register current_method = locations->InAt(0).AsRegister<Register>();
4000 if (cls->IsReferrersClass()) {
4001 DCHECK(!cls->CanCallRuntime());
4002 DCHECK(!cls->MustGenerateClinitCheck());
4003 __ LoadFromOffset(kLoadWord, out, current_method,
4004 ArtMethod::DeclaringClassOffset().Int32Value());
4005 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004006 __ LoadFromOffset(kLoadWord, out, current_method,
4007 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4008 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004009
4010 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4011 DCHECK(cls->CanCallRuntime());
4012 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4013 cls,
4014 cls,
4015 cls->GetDexPc(),
4016 cls->MustGenerateClinitCheck());
4017 codegen_->AddSlowPath(slow_path);
4018 if (!cls->IsInDexCache()) {
4019 __ Beqz(out, slow_path->GetEntryLabel());
4020 }
4021 if (cls->MustGenerateClinitCheck()) {
4022 GenerateClassInitializationCheck(slow_path, out);
4023 } else {
4024 __ Bind(slow_path->GetExitLabel());
4025 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026 }
4027 }
4028}
4029
4030static int32_t GetExceptionTlsOffset() {
4031 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4032}
4033
4034void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4035 LocationSummary* locations =
4036 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4037 locations->SetOut(Location::RequiresRegister());
4038}
4039
4040void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4041 Register out = load->GetLocations()->Out().AsRegister<Register>();
4042 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4043}
4044
4045void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4046 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4047}
4048
4049void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4050 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4051}
4052
4053void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
4054 load->SetLocations(nullptr);
4055}
4056
4057void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
4058 // Nothing to do, this is driven by the code generator.
4059}
4060
4061void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Roland Levillain698fa972015-12-16 17:06:47 +00004062 LocationSummary::CallKind call_kind = load->IsInDexCache()
4063 ? LocationSummary::kNoCall
4064 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004066 locations->SetInAt(0, Location::RequiresRegister());
4067 locations->SetOut(Location::RequiresRegister());
4068}
4069
4070void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004071 LocationSummary* locations = load->GetLocations();
4072 Register out = locations->Out().AsRegister<Register>();
4073 Register current_method = locations->InAt(0).AsRegister<Register>();
4074 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4075 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4076 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004077
4078 if (!load->IsInDexCache()) {
4079 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4080 codegen_->AddSlowPath(slow_path);
4081 __ Beqz(out, slow_path->GetEntryLabel());
4082 __ Bind(slow_path->GetExitLabel());
4083 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084}
4085
4086void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
4087 local->SetLocations(nullptr);
4088}
4089
4090void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
4091 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
4092}
4093
4094void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4096 locations->SetOut(Location::ConstantLocation(constant));
4097}
4098
4099void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4100 // Will be generated at use site.
4101}
4102
4103void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4106 InvokeRuntimeCallingConvention calling_convention;
4107 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4108}
4109
4110void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4111 if (instruction->IsEnter()) {
4112 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4113 instruction,
4114 instruction->GetDexPc(),
4115 nullptr,
4116 IsDirectEntrypoint(kQuickLockObject));
4117 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4118 } else {
4119 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4120 instruction,
4121 instruction->GetDexPc(),
4122 nullptr,
4123 IsDirectEntrypoint(kQuickUnlockObject));
4124 }
4125 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4126}
4127
4128void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4129 LocationSummary* locations =
4130 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4131 switch (mul->GetResultType()) {
4132 case Primitive::kPrimInt:
4133 case Primitive::kPrimLong:
4134 locations->SetInAt(0, Location::RequiresRegister());
4135 locations->SetInAt(1, Location::RequiresRegister());
4136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4137 break;
4138
4139 case Primitive::kPrimFloat:
4140 case Primitive::kPrimDouble:
4141 locations->SetInAt(0, Location::RequiresFpuRegister());
4142 locations->SetInAt(1, Location::RequiresFpuRegister());
4143 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4144 break;
4145
4146 default:
4147 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4148 }
4149}
4150
4151void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4152 Primitive::Type type = instruction->GetType();
4153 LocationSummary* locations = instruction->GetLocations();
4154 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4155
4156 switch (type) {
4157 case Primitive::kPrimInt: {
4158 Register dst = locations->Out().AsRegister<Register>();
4159 Register lhs = locations->InAt(0).AsRegister<Register>();
4160 Register rhs = locations->InAt(1).AsRegister<Register>();
4161
4162 if (isR6) {
4163 __ MulR6(dst, lhs, rhs);
4164 } else {
4165 __ MulR2(dst, lhs, rhs);
4166 }
4167 break;
4168 }
4169 case Primitive::kPrimLong: {
4170 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4171 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4172 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4173 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4174 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4175 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4176
4177 // Extra checks to protect caused by the existance of A1_A2.
4178 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4179 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4180 DCHECK_NE(dst_high, lhs_low);
4181 DCHECK_NE(dst_high, rhs_low);
4182
4183 // A_B * C_D
4184 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4185 // dst_lo: [ low(B*D) ]
4186 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4187
4188 if (isR6) {
4189 __ MulR6(TMP, lhs_high, rhs_low);
4190 __ MulR6(dst_high, lhs_low, rhs_high);
4191 __ Addu(dst_high, dst_high, TMP);
4192 __ MuhuR6(TMP, lhs_low, rhs_low);
4193 __ Addu(dst_high, dst_high, TMP);
4194 __ MulR6(dst_low, lhs_low, rhs_low);
4195 } else {
4196 __ MulR2(TMP, lhs_high, rhs_low);
4197 __ MulR2(dst_high, lhs_low, rhs_high);
4198 __ Addu(dst_high, dst_high, TMP);
4199 __ MultuR2(lhs_low, rhs_low);
4200 __ Mfhi(TMP);
4201 __ Addu(dst_high, dst_high, TMP);
4202 __ Mflo(dst_low);
4203 }
4204 break;
4205 }
4206 case Primitive::kPrimFloat:
4207 case Primitive::kPrimDouble: {
4208 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4209 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4210 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4211 if (type == Primitive::kPrimFloat) {
4212 __ MulS(dst, lhs, rhs);
4213 } else {
4214 __ MulD(dst, lhs, rhs);
4215 }
4216 break;
4217 }
4218 default:
4219 LOG(FATAL) << "Unexpected mul type " << type;
4220 }
4221}
4222
4223void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4226 switch (neg->GetResultType()) {
4227 case Primitive::kPrimInt:
4228 case Primitive::kPrimLong:
4229 locations->SetInAt(0, Location::RequiresRegister());
4230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4231 break;
4232
4233 case Primitive::kPrimFloat:
4234 case Primitive::kPrimDouble:
4235 locations->SetInAt(0, Location::RequiresFpuRegister());
4236 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4237 break;
4238
4239 default:
4240 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4241 }
4242}
4243
4244void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4245 Primitive::Type type = instruction->GetType();
4246 LocationSummary* locations = instruction->GetLocations();
4247
4248 switch (type) {
4249 case Primitive::kPrimInt: {
4250 Register dst = locations->Out().AsRegister<Register>();
4251 Register src = locations->InAt(0).AsRegister<Register>();
4252 __ Subu(dst, ZERO, src);
4253 break;
4254 }
4255 case Primitive::kPrimLong: {
4256 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4257 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4258 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4259 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4260 __ Subu(dst_low, ZERO, src_low);
4261 __ Sltu(TMP, ZERO, dst_low);
4262 __ Subu(dst_high, ZERO, src_high);
4263 __ Subu(dst_high, dst_high, TMP);
4264 break;
4265 }
4266 case Primitive::kPrimFloat:
4267 case Primitive::kPrimDouble: {
4268 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4269 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4270 if (type == Primitive::kPrimFloat) {
4271 __ NegS(dst, src);
4272 } else {
4273 __ NegD(dst, src);
4274 }
4275 break;
4276 }
4277 default:
4278 LOG(FATAL) << "Unexpected neg type " << type;
4279 }
4280}
4281
4282void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4283 LocationSummary* locations =
4284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4285 InvokeRuntimeCallingConvention calling_convention;
4286 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4287 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4288 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4289 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4290}
4291
4292void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4293 InvokeRuntimeCallingConvention calling_convention;
4294 Register current_method_register = calling_convention.GetRegisterAt(2);
4295 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4296 // Move an uint16_t value to a register.
4297 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4298 codegen_->InvokeRuntime(
4299 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4300 instruction,
4301 instruction->GetDexPc(),
4302 nullptr,
4303 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4304 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4305 void*, uint32_t, int32_t, ArtMethod*>();
4306}
4307
4308void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4309 LocationSummary* locations =
4310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4311 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004312 if (instruction->IsStringAlloc()) {
4313 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4314 } else {
4315 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4316 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4317 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004318 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4319}
4320
4321void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004322 if (instruction->IsStringAlloc()) {
4323 // String is allocated through StringFactory. Call NewEmptyString entry point.
4324 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4325 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4326 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4327 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4328 __ Jalr(T9);
4329 __ Nop();
4330 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4331 } else {
4332 codegen_->InvokeRuntime(
4333 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4334 instruction,
4335 instruction->GetDexPc(),
4336 nullptr,
4337 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4338 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4339 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004340}
4341
4342void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4344 locations->SetInAt(0, Location::RequiresRegister());
4345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4346}
4347
4348void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4349 Primitive::Type type = instruction->GetType();
4350 LocationSummary* locations = instruction->GetLocations();
4351
4352 switch (type) {
4353 case Primitive::kPrimInt: {
4354 Register dst = locations->Out().AsRegister<Register>();
4355 Register src = locations->InAt(0).AsRegister<Register>();
4356 __ Nor(dst, src, ZERO);
4357 break;
4358 }
4359
4360 case Primitive::kPrimLong: {
4361 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4362 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4363 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4364 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4365 __ Nor(dst_high, src_high, ZERO);
4366 __ Nor(dst_low, src_low, ZERO);
4367 break;
4368 }
4369
4370 default:
4371 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4372 }
4373}
4374
4375void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4376 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4379}
4380
4381void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4382 LocationSummary* locations = instruction->GetLocations();
4383 __ Xori(locations->Out().AsRegister<Register>(),
4384 locations->InAt(0).AsRegister<Register>(),
4385 1);
4386}
4387
4388void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4389 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4390 ? LocationSummary::kCallOnSlowPath
4391 : LocationSummary::kNoCall;
4392 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4393 locations->SetInAt(0, Location::RequiresRegister());
4394 if (instruction->HasUses()) {
4395 locations->SetOut(Location::SameAsFirstInput());
4396 }
4397}
4398
Calin Juravle2ae48182016-03-16 14:05:09 +00004399void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4400 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004401 return;
4402 }
4403 Location obj = instruction->GetLocations()->InAt(0);
4404
4405 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004406 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004407}
4408
Calin Juravle2ae48182016-03-16 14:05:09 +00004409void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004410 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004411 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004412
4413 Location obj = instruction->GetLocations()->InAt(0);
4414
4415 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4416}
4417
4418void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004419 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004420}
4421
4422void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4423 HandleBinaryOp(instruction);
4424}
4425
4426void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4427 HandleBinaryOp(instruction);
4428}
4429
4430void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4431 LOG(FATAL) << "Unreachable";
4432}
4433
4434void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4435 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4436}
4437
4438void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4439 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4440 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4441 if (location.IsStackSlot()) {
4442 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4443 } else if (location.IsDoubleStackSlot()) {
4444 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4445 }
4446 locations->SetOut(location);
4447}
4448
4449void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4450 ATTRIBUTE_UNUSED) {
4451 // Nothing to do, the parameter is already at its location.
4452}
4453
4454void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4455 LocationSummary* locations =
4456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4457 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4458}
4459
4460void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4461 ATTRIBUTE_UNUSED) {
4462 // Nothing to do, the method is already at its location.
4463}
4464
4465void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4466 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4467 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4468 locations->SetInAt(i, Location::Any());
4469 }
4470 locations->SetOut(Location::Any());
4471}
4472
4473void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4474 LOG(FATAL) << "Unreachable";
4475}
4476
4477void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4478 Primitive::Type type = rem->GetResultType();
4479 LocationSummary::CallKind call_kind =
4480 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4482
4483 switch (type) {
4484 case Primitive::kPrimInt:
4485 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004486 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4488 break;
4489
4490 case Primitive::kPrimLong: {
4491 InvokeRuntimeCallingConvention calling_convention;
4492 locations->SetInAt(0, Location::RegisterPairLocation(
4493 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4494 locations->SetInAt(1, Location::RegisterPairLocation(
4495 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4496 locations->SetOut(calling_convention.GetReturnLocation(type));
4497 break;
4498 }
4499
4500 case Primitive::kPrimFloat:
4501 case Primitive::kPrimDouble: {
4502 InvokeRuntimeCallingConvention calling_convention;
4503 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4504 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4505 locations->SetOut(calling_convention.GetReturnLocation(type));
4506 break;
4507 }
4508
4509 default:
4510 LOG(FATAL) << "Unexpected rem type " << type;
4511 }
4512}
4513
4514void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4515 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004516
4517 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004518 case Primitive::kPrimInt:
4519 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004520 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004521 case Primitive::kPrimLong: {
4522 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4523 instruction,
4524 instruction->GetDexPc(),
4525 nullptr,
4526 IsDirectEntrypoint(kQuickLmod));
4527 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4528 break;
4529 }
4530 case Primitive::kPrimFloat: {
4531 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4532 instruction, instruction->GetDexPc(),
4533 nullptr,
4534 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004535 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004536 break;
4537 }
4538 case Primitive::kPrimDouble: {
4539 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4540 instruction, instruction->GetDexPc(),
4541 nullptr,
4542 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004543 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004544 break;
4545 }
4546 default:
4547 LOG(FATAL) << "Unexpected rem type " << type;
4548 }
4549}
4550
4551void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4552 memory_barrier->SetLocations(nullptr);
4553}
4554
4555void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4556 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4557}
4558
4559void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4561 Primitive::Type return_type = ret->InputAt(0)->GetType();
4562 locations->SetInAt(0, MipsReturnLocation(return_type));
4563}
4564
4565void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4566 codegen_->GenerateFrameExit();
4567}
4568
4569void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4570 ret->SetLocations(nullptr);
4571}
4572
4573void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4574 codegen_->GenerateFrameExit();
4575}
4576
Alexey Frunze92d90602015-12-18 18:16:36 -08004577void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4578 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004579}
4580
Alexey Frunze92d90602015-12-18 18:16:36 -08004581void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4582 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004583}
4584
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004585void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4586 HandleShift(shl);
4587}
4588
4589void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4590 HandleShift(shl);
4591}
4592
4593void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4594 HandleShift(shr);
4595}
4596
4597void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4598 HandleShift(shr);
4599}
4600
4601void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
4602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
4603 Primitive::Type field_type = store->InputAt(1)->GetType();
4604 switch (field_type) {
4605 case Primitive::kPrimNot:
4606 case Primitive::kPrimBoolean:
4607 case Primitive::kPrimByte:
4608 case Primitive::kPrimChar:
4609 case Primitive::kPrimShort:
4610 case Primitive::kPrimInt:
4611 case Primitive::kPrimFloat:
4612 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
4613 break;
4614
4615 case Primitive::kPrimLong:
4616 case Primitive::kPrimDouble:
4617 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
4618 break;
4619
4620 default:
4621 LOG(FATAL) << "Unimplemented local type " << field_type;
4622 }
4623}
4624
4625void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
4626}
4627
4628void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4629 HandleBinaryOp(instruction);
4630}
4631
4632void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4633 HandleBinaryOp(instruction);
4634}
4635
4636void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4637 HandleFieldGet(instruction, instruction->GetFieldInfo());
4638}
4639
4640void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4641 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4642}
4643
4644void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4645 HandleFieldSet(instruction, instruction->GetFieldInfo());
4646}
4647
4648void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4649 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4650}
4651
4652void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4653 HUnresolvedInstanceFieldGet* instruction) {
4654 FieldAccessCallingConventionMIPS calling_convention;
4655 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4656 instruction->GetFieldType(),
4657 calling_convention);
4658}
4659
4660void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4661 HUnresolvedInstanceFieldGet* instruction) {
4662 FieldAccessCallingConventionMIPS calling_convention;
4663 codegen_->GenerateUnresolvedFieldAccess(instruction,
4664 instruction->GetFieldType(),
4665 instruction->GetFieldIndex(),
4666 instruction->GetDexPc(),
4667 calling_convention);
4668}
4669
4670void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4671 HUnresolvedInstanceFieldSet* instruction) {
4672 FieldAccessCallingConventionMIPS calling_convention;
4673 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4674 instruction->GetFieldType(),
4675 calling_convention);
4676}
4677
4678void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4679 HUnresolvedInstanceFieldSet* instruction) {
4680 FieldAccessCallingConventionMIPS calling_convention;
4681 codegen_->GenerateUnresolvedFieldAccess(instruction,
4682 instruction->GetFieldType(),
4683 instruction->GetFieldIndex(),
4684 instruction->GetDexPc(),
4685 calling_convention);
4686}
4687
4688void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4689 HUnresolvedStaticFieldGet* instruction) {
4690 FieldAccessCallingConventionMIPS calling_convention;
4691 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4692 instruction->GetFieldType(),
4693 calling_convention);
4694}
4695
4696void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4697 HUnresolvedStaticFieldGet* instruction) {
4698 FieldAccessCallingConventionMIPS calling_convention;
4699 codegen_->GenerateUnresolvedFieldAccess(instruction,
4700 instruction->GetFieldType(),
4701 instruction->GetFieldIndex(),
4702 instruction->GetDexPc(),
4703 calling_convention);
4704}
4705
4706void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4707 HUnresolvedStaticFieldSet* instruction) {
4708 FieldAccessCallingConventionMIPS calling_convention;
4709 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4710 instruction->GetFieldType(),
4711 calling_convention);
4712}
4713
4714void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4715 HUnresolvedStaticFieldSet* instruction) {
4716 FieldAccessCallingConventionMIPS calling_convention;
4717 codegen_->GenerateUnresolvedFieldAccess(instruction,
4718 instruction->GetFieldType(),
4719 instruction->GetFieldIndex(),
4720 instruction->GetDexPc(),
4721 calling_convention);
4722}
4723
4724void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4726}
4727
4728void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4729 HBasicBlock* block = instruction->GetBlock();
4730 if (block->GetLoopInformation() != nullptr) {
4731 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4732 // The back edge will generate the suspend check.
4733 return;
4734 }
4735 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4736 // The goto will generate the suspend check.
4737 return;
4738 }
4739 GenerateSuspendCheck(instruction, nullptr);
4740}
4741
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004742void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4743 LocationSummary* locations =
4744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4745 InvokeRuntimeCallingConvention calling_convention;
4746 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4747}
4748
4749void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4750 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4751 instruction,
4752 instruction->GetDexPc(),
4753 nullptr,
4754 IsDirectEntrypoint(kQuickDeliverException));
4755 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4756}
4757
4758void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4759 Primitive::Type input_type = conversion->GetInputType();
4760 Primitive::Type result_type = conversion->GetResultType();
4761 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004762 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004763
4764 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4765 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4766 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4767 }
4768
4769 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004770 if (!isR6 &&
4771 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4772 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004773 call_kind = LocationSummary::kCall;
4774 }
4775
4776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4777
4778 if (call_kind == LocationSummary::kNoCall) {
4779 if (Primitive::IsFloatingPointType(input_type)) {
4780 locations->SetInAt(0, Location::RequiresFpuRegister());
4781 } else {
4782 locations->SetInAt(0, Location::RequiresRegister());
4783 }
4784
4785 if (Primitive::IsFloatingPointType(result_type)) {
4786 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4787 } else {
4788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4789 }
4790 } else {
4791 InvokeRuntimeCallingConvention calling_convention;
4792
4793 if (Primitive::IsFloatingPointType(input_type)) {
4794 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4795 } else {
4796 DCHECK_EQ(input_type, Primitive::kPrimLong);
4797 locations->SetInAt(0, Location::RegisterPairLocation(
4798 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4799 }
4800
4801 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4802 }
4803}
4804
4805void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4806 LocationSummary* locations = conversion->GetLocations();
4807 Primitive::Type result_type = conversion->GetResultType();
4808 Primitive::Type input_type = conversion->GetInputType();
4809 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004810 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4811 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004812
4813 DCHECK_NE(input_type, result_type);
4814
4815 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4816 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4817 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4818 Register src = locations->InAt(0).AsRegister<Register>();
4819
4820 __ Move(dst_low, src);
4821 __ Sra(dst_high, src, 31);
4822 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4823 Register dst = locations->Out().AsRegister<Register>();
4824 Register src = (input_type == Primitive::kPrimLong)
4825 ? locations->InAt(0).AsRegisterPairLow<Register>()
4826 : locations->InAt(0).AsRegister<Register>();
4827
4828 switch (result_type) {
4829 case Primitive::kPrimChar:
4830 __ Andi(dst, src, 0xFFFF);
4831 break;
4832 case Primitive::kPrimByte:
4833 if (has_sign_extension) {
4834 __ Seb(dst, src);
4835 } else {
4836 __ Sll(dst, src, 24);
4837 __ Sra(dst, dst, 24);
4838 }
4839 break;
4840 case Primitive::kPrimShort:
4841 if (has_sign_extension) {
4842 __ Seh(dst, src);
4843 } else {
4844 __ Sll(dst, src, 16);
4845 __ Sra(dst, dst, 16);
4846 }
4847 break;
4848 case Primitive::kPrimInt:
4849 __ Move(dst, src);
4850 break;
4851
4852 default:
4853 LOG(FATAL) << "Unexpected type conversion from " << input_type
4854 << " to " << result_type;
4855 }
4856 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004857 if (input_type == Primitive::kPrimLong) {
4858 if (isR6) {
4859 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4860 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4861 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4862 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4863 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4864 __ Mtc1(src_low, FTMP);
4865 __ Mthc1(src_high, FTMP);
4866 if (result_type == Primitive::kPrimFloat) {
4867 __ Cvtsl(dst, FTMP);
4868 } else {
4869 __ Cvtdl(dst, FTMP);
4870 }
4871 } else {
4872 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4873 : QUICK_ENTRY_POINT(pL2d);
4874 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4875 : IsDirectEntrypoint(kQuickL2d);
4876 codegen_->InvokeRuntime(entry_offset,
4877 conversion,
4878 conversion->GetDexPc(),
4879 nullptr,
4880 direct);
4881 if (result_type == Primitive::kPrimFloat) {
4882 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4883 } else {
4884 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4885 }
4886 }
4887 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004888 Register src = locations->InAt(0).AsRegister<Register>();
4889 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4890 __ Mtc1(src, FTMP);
4891 if (result_type == Primitive::kPrimFloat) {
4892 __ Cvtsw(dst, FTMP);
4893 } else {
4894 __ Cvtdw(dst, FTMP);
4895 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004896 }
4897 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4898 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004899 if (result_type == Primitive::kPrimLong) {
4900 if (isR6) {
4901 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4902 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4903 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4904 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4905 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4906 MipsLabel truncate;
4907 MipsLabel done;
4908
4909 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4910 // value when the input is either a NaN or is outside of the range of the output type
4911 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4912 // the same result.
4913 //
4914 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4915 // value of the output type if the input is outside of the range after the truncation or
4916 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4917 // results. This matches the desired float/double-to-int/long conversion exactly.
4918 //
4919 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4920 //
4921 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4922 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4923 // even though it must be NAN2008=1 on R6.
4924 //
4925 // The code takes care of the different behaviors by first comparing the input to the
4926 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4927 // If the input is greater than or equal to the minimum, it procedes to the truncate
4928 // instruction, which will handle such an input the same way irrespective of NAN2008.
4929 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4930 // in order to return either zero or the minimum value.
4931 //
4932 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4933 // truncate instruction for MIPS64R6.
4934 if (input_type == Primitive::kPrimFloat) {
4935 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4936 __ LoadConst32(TMP, min_val);
4937 __ Mtc1(TMP, FTMP);
4938 __ CmpLeS(FTMP, FTMP, src);
4939 } else {
4940 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4941 __ LoadConst32(TMP, High32Bits(min_val));
4942 __ Mtc1(ZERO, FTMP);
4943 __ Mthc1(TMP, FTMP);
4944 __ CmpLeD(FTMP, FTMP, src);
4945 }
4946
4947 __ Bc1nez(FTMP, &truncate);
4948
4949 if (input_type == Primitive::kPrimFloat) {
4950 __ CmpEqS(FTMP, src, src);
4951 } else {
4952 __ CmpEqD(FTMP, src, src);
4953 }
4954 __ Move(dst_low, ZERO);
4955 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4956 __ Mfc1(TMP, FTMP);
4957 __ And(dst_high, dst_high, TMP);
4958
4959 __ B(&done);
4960
4961 __ Bind(&truncate);
4962
4963 if (input_type == Primitive::kPrimFloat) {
4964 __ TruncLS(FTMP, src);
4965 } else {
4966 __ TruncLD(FTMP, src);
4967 }
4968 __ Mfc1(dst_low, FTMP);
4969 __ Mfhc1(dst_high, FTMP);
4970
4971 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004973 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4974 : QUICK_ENTRY_POINT(pD2l);
4975 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4976 : IsDirectEntrypoint(kQuickD2l);
4977 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
4978 if (input_type == Primitive::kPrimFloat) {
4979 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4980 } else {
4981 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4982 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004983 }
4984 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004985 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4986 Register dst = locations->Out().AsRegister<Register>();
4987 MipsLabel truncate;
4988 MipsLabel done;
4989
4990 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4991 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4992 // even though it must be NAN2008=1 on R6.
4993 //
4994 // For details see the large comment above for the truncation of float/double to long on R6.
4995 //
4996 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4997 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004998 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004999 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5000 __ LoadConst32(TMP, min_val);
5001 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005002 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005003 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5004 __ LoadConst32(TMP, High32Bits(min_val));
5005 __ Mtc1(ZERO, FTMP);
5006 if (fpu_32bit) {
5007 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
5008 } else {
5009 __ Mthc1(TMP, FTMP);
5010 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005011 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005012
5013 if (isR6) {
5014 if (input_type == Primitive::kPrimFloat) {
5015 __ CmpLeS(FTMP, FTMP, src);
5016 } else {
5017 __ CmpLeD(FTMP, FTMP, src);
5018 }
5019 __ Bc1nez(FTMP, &truncate);
5020
5021 if (input_type == Primitive::kPrimFloat) {
5022 __ CmpEqS(FTMP, src, src);
5023 } else {
5024 __ CmpEqD(FTMP, src, src);
5025 }
5026 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5027 __ Mfc1(TMP, FTMP);
5028 __ And(dst, dst, TMP);
5029 } else {
5030 if (input_type == Primitive::kPrimFloat) {
5031 __ ColeS(0, FTMP, src);
5032 } else {
5033 __ ColeD(0, FTMP, src);
5034 }
5035 __ Bc1t(0, &truncate);
5036
5037 if (input_type == Primitive::kPrimFloat) {
5038 __ CeqS(0, src, src);
5039 } else {
5040 __ CeqD(0, src, src);
5041 }
5042 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5043 __ Movf(dst, ZERO, 0);
5044 }
5045
5046 __ B(&done);
5047
5048 __ Bind(&truncate);
5049
5050 if (input_type == Primitive::kPrimFloat) {
5051 __ TruncWS(FTMP, src);
5052 } else {
5053 __ TruncWD(FTMP, src);
5054 }
5055 __ Mfc1(dst, FTMP);
5056
5057 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005058 }
5059 } else if (Primitive::IsFloatingPointType(result_type) &&
5060 Primitive::IsFloatingPointType(input_type)) {
5061 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5062 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5063 if (result_type == Primitive::kPrimFloat) {
5064 __ Cvtsd(dst, src);
5065 } else {
5066 __ Cvtds(dst, src);
5067 }
5068 } else {
5069 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5070 << " to " << result_type;
5071 }
5072}
5073
5074void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5075 HandleShift(ushr);
5076}
5077
5078void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5079 HandleShift(ushr);
5080}
5081
5082void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5083 HandleBinaryOp(instruction);
5084}
5085
5086void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5087 HandleBinaryOp(instruction);
5088}
5089
5090void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5091 // Nothing to do, this should be removed during prepare for register allocator.
5092 LOG(FATAL) << "Unreachable";
5093}
5094
5095void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5096 // Nothing to do, this should be removed during prepare for register allocator.
5097 LOG(FATAL) << "Unreachable";
5098}
5099
5100void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005101 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005102}
5103
5104void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005105 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005106}
5107
5108void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005109 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110}
5111
5112void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005113 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005114}
5115
5116void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005117 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005118}
5119
5120void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005121 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005122}
5123
5124void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005125 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126}
5127
5128void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005129 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005130}
5131
5132void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005133 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005134}
5135
5136void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005137 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138}
5139
5140void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005141 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005142}
5143
5144void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005145 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146}
5147
5148void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005149 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005150}
5151
5152void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005153 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005154}
5155
5156void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005157 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005158}
5159
5160void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005161 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005162}
5163
5164void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005165 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005166}
5167
5168void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005169 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005170}
5171
5172void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005173 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005174}
5175
5176void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005177 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005178}
5179
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005180void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5181 LocationSummary* locations =
5182 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5183 locations->SetInAt(0, Location::RequiresRegister());
5184}
5185
5186void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5187 int32_t lower_bound = switch_instr->GetStartValue();
5188 int32_t num_entries = switch_instr->GetNumEntries();
5189 LocationSummary* locations = switch_instr->GetLocations();
5190 Register value_reg = locations->InAt(0).AsRegister<Register>();
5191 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5192
5193 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005194 Register temp_reg = TMP;
5195 __ Addiu32(temp_reg, value_reg, -lower_bound);
5196 // Jump to default if index is negative
5197 // Note: We don't check the case that index is positive while value < lower_bound, because in
5198 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5199 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5200
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005201 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005202 // Jump to successors[0] if value == lower_bound.
5203 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5204 int32_t last_index = 0;
5205 for (; num_entries - last_index > 2; last_index += 2) {
5206 __ Addiu(temp_reg, temp_reg, -2);
5207 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5208 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5209 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5210 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5211 }
5212 if (num_entries - last_index == 2) {
5213 // The last missing case_value.
5214 __ Addiu(temp_reg, temp_reg, -1);
5215 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005216 }
5217
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005218 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005219 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5220 __ B(codegen_->GetLabelOf(default_block));
5221 }
5222}
5223
5224void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5225 // The trampoline uses the same calling convention as dex calling conventions,
5226 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5227 // the method_idx.
5228 HandleInvoke(invoke);
5229}
5230
5231void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5232 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5233}
5234
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005235void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5236 LocationSummary* locations =
5237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5238 locations->SetInAt(0, Location::RequiresRegister());
5239 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005240}
5241
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005242void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5243 LocationSummary* locations = instruction->GetLocations();
5244 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005246 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5247 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5248 } else {
5249 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5250 instruction->GetIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
5251 }
5252 __ LoadFromOffset(kLoadWord,
5253 locations->Out().AsRegister<Register>(),
5254 locations->InAt(0).AsRegister<Register>(),
5255 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005256}
5257
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005258#undef __
5259#undef QUICK_ENTRY_POINT
5260
5261} // namespace mips
5262} // namespace art