blob: c3f425ac0d7d3d9771cf576bf70371786fe26fa9 [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
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700144// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
145#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200146#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
147
148class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
149 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000150 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200151
152 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
153 LocationSummary* locations = instruction_->GetLocations();
154 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
155 __ Bind(GetEntryLabel());
156 if (instruction_->CanThrowIntoCatchBlock()) {
157 // Live registers will be restored in the catch block if caught.
158 SaveLiveRegisters(codegen, instruction_->GetLocations());
159 }
160 // We're moving two locations to locations that could overlap, so we need a parallel
161 // move resolver.
162 InvokeRuntimeCallingConvention calling_convention;
163 codegen->EmitParallelMoves(locations->InAt(0),
164 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
165 Primitive::kPrimInt,
166 locations->InAt(1),
167 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
168 Primitive::kPrimInt);
169 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
170 instruction_,
171 instruction_->GetDexPc(),
172 this,
173 IsDirectEntrypoint(kQuickThrowArrayBounds));
174 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
175 }
176
177 bool IsFatal() const OVERRIDE { return true; }
178
179 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
180
181 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
183};
184
185class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
186 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000187 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188
189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
190 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
191 __ Bind(GetEntryLabel());
192 if (instruction_->CanThrowIntoCatchBlock()) {
193 // Live registers will be restored in the catch block if caught.
194 SaveLiveRegisters(codegen, instruction_->GetLocations());
195 }
196 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
197 instruction_,
198 instruction_->GetDexPc(),
199 this,
200 IsDirectEntrypoint(kQuickThrowDivZero));
201 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
202 }
203
204 bool IsFatal() const OVERRIDE { return true; }
205
206 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
207
208 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200209 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
210};
211
212class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
213 public:
214 LoadClassSlowPathMIPS(HLoadClass* cls,
215 HInstruction* at,
216 uint32_t dex_pc,
217 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000218 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
220 }
221
222 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
223 LocationSummary* locations = at_->GetLocations();
224 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
225
226 __ Bind(GetEntryLabel());
227 SaveLiveRegisters(codegen, locations);
228
229 InvokeRuntimeCallingConvention calling_convention;
230 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
231
232 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
233 : QUICK_ENTRY_POINT(pInitializeType);
234 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
235 : IsDirectEntrypoint(kQuickInitializeType);
236
237 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
238 if (do_clinit_) {
239 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
240 } else {
241 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
242 }
243
244 // Move the class to the desired location.
245 Location out = locations->Out();
246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
248 Primitive::Type type = at_->GetType();
249 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
250 }
251
252 RestoreLiveRegisters(codegen, locations);
253 __ B(GetExitLabel());
254 }
255
256 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
257
258 private:
259 // The class this slow path will load.
260 HLoadClass* const cls_;
261
262 // The instruction where this slow path is happening.
263 // (Might be the load class or an initialization check).
264 HInstruction* const at_;
265
266 // The dex PC of `at_`.
267 const uint32_t dex_pc_;
268
269 // Whether to initialize the class.
270 const bool do_clinit_;
271
272 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
273};
274
275class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
276 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000277 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200278
279 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
280 LocationSummary* locations = instruction_->GetLocations();
281 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
282 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
283
284 __ Bind(GetEntryLabel());
285 SaveLiveRegisters(codegen, locations);
286
287 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000288 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
289 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
291 instruction_,
292 instruction_->GetDexPc(),
293 this,
294 IsDirectEntrypoint(kQuickResolveString));
295 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
296 Primitive::Type type = instruction_->GetType();
297 mips_codegen->MoveLocation(locations->Out(),
298 calling_convention.GetReturnLocation(type),
299 type);
300
301 RestoreLiveRegisters(codegen, locations);
302 __ B(GetExitLabel());
303 }
304
305 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
306
307 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
309};
310
311class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
312 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000313 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200314
315 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
316 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
317 __ Bind(GetEntryLabel());
318 if (instruction_->CanThrowIntoCatchBlock()) {
319 // Live registers will be restored in the catch block if caught.
320 SaveLiveRegisters(codegen, instruction_->GetLocations());
321 }
322 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
323 instruction_,
324 instruction_->GetDexPc(),
325 this,
326 IsDirectEntrypoint(kQuickThrowNullPointer));
327 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
328 }
329
330 bool IsFatal() const OVERRIDE { return true; }
331
332 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
333
334 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
336};
337
338class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
339 public:
340 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000341 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342
343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
344 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
345 __ Bind(GetEntryLabel());
346 SaveLiveRegisters(codegen, instruction_->GetLocations());
347 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
348 instruction_,
349 instruction_->GetDexPc(),
350 this,
351 IsDirectEntrypoint(kQuickTestSuspend));
352 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
353 RestoreLiveRegisters(codegen, instruction_->GetLocations());
354 if (successor_ == nullptr) {
355 __ B(GetReturnLabel());
356 } else {
357 __ B(mips_codegen->GetLabelOf(successor_));
358 }
359 }
360
361 MipsLabel* GetReturnLabel() {
362 DCHECK(successor_ == nullptr);
363 return &return_label_;
364 }
365
366 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
367
368 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200369 // If not null, the block to branch to after the suspend check.
370 HBasicBlock* const successor_;
371
372 // If `successor_` is null, the label to branch to after the suspend check.
373 MipsLabel return_label_;
374
375 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
376};
377
378class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
379 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000380 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381
382 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
383 LocationSummary* locations = instruction_->GetLocations();
384 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
385 uint32_t dex_pc = instruction_->GetDexPc();
386 DCHECK(instruction_->IsCheckCast()
387 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
388 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
389
390 __ Bind(GetEntryLabel());
391 SaveLiveRegisters(codegen, locations);
392
393 // We're moving two locations to locations that could overlap, so we need a parallel
394 // move resolver.
395 InvokeRuntimeCallingConvention calling_convention;
396 codegen->EmitParallelMoves(locations->InAt(1),
397 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
398 Primitive::kPrimNot,
399 object_class,
400 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
401 Primitive::kPrimNot);
402
403 if (instruction_->IsInstanceOf()) {
404 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
405 instruction_,
406 dex_pc,
407 this,
408 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000409 CheckEntrypointTypes<
410 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 Primitive::Type ret_type = instruction_->GetType();
412 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
413 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200414 } else {
415 DCHECK(instruction_->IsCheckCast());
416 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
417 instruction_,
418 dex_pc,
419 this,
420 IsDirectEntrypoint(kQuickCheckCast));
421 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
422 }
423
424 RestoreLiveRegisters(codegen, locations);
425 __ B(GetExitLabel());
426 }
427
428 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
429
430 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200431 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
432};
433
434class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
435 public:
Aart Bik42249c32016-01-07 15:33:50 -0800436 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800440 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441 __ Bind(GetEntryLabel());
442 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
444 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800445 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 this,
447 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000448 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 }
450
451 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
452
453 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
455};
456
457CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
458 const MipsInstructionSetFeatures& isa_features,
459 const CompilerOptions& compiler_options,
460 OptimizingCompilerStats* stats)
461 : CodeGenerator(graph,
462 kNumberOfCoreRegisters,
463 kNumberOfFRegisters,
464 kNumberOfRegisterPairs,
465 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
466 arraysize(kCoreCalleeSaves)),
467 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
468 arraysize(kFpuCalleeSaves)),
469 compiler_options,
470 stats),
471 block_labels_(nullptr),
472 location_builder_(graph, this),
473 instruction_visitor_(graph, this),
474 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100475 assembler_(graph->GetArena(), &isa_features),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200476 isa_features_(isa_features) {
477 // Save RA (containing the return address) to mimic Quick.
478 AddAllocatedRegister(Location::RegisterLocation(RA));
479}
480
481#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700482// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
483#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200484#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
485
486void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
487 // Ensure that we fix up branches.
488 __ FinalizeCode();
489
490 // Adjust native pc offsets in stack maps.
491 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
492 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
493 uint32_t new_position = __ GetAdjustedPosition(old_position);
494 DCHECK_GE(new_position, old_position);
495 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
496 }
497
498 // Adjust pc offsets for the disassembly information.
499 if (disasm_info_ != nullptr) {
500 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
501 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
502 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
503 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
504 it.second.start = __ GetAdjustedPosition(it.second.start);
505 it.second.end = __ GetAdjustedPosition(it.second.end);
506 }
507 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
508 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
509 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
510 }
511 }
512
513 CodeGenerator::Finalize(allocator);
514}
515
516MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
517 return codegen_->GetAssembler();
518}
519
520void ParallelMoveResolverMIPS::EmitMove(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
524}
525
526void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 Primitive::Type type = move->GetType();
530 Location loc1 = move->GetDestination();
531 Location loc2 = move->GetSource();
532
533 DCHECK(!loc1.IsConstant());
534 DCHECK(!loc2.IsConstant());
535
536 if (loc1.Equals(loc2)) {
537 return;
538 }
539
540 if (loc1.IsRegister() && loc2.IsRegister()) {
541 // Swap 2 GPRs.
542 Register r1 = loc1.AsRegister<Register>();
543 Register r2 = loc2.AsRegister<Register>();
544 __ Move(TMP, r2);
545 __ Move(r2, r1);
546 __ Move(r1, TMP);
547 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
548 FRegister f1 = loc1.AsFpuRegister<FRegister>();
549 FRegister f2 = loc2.AsFpuRegister<FRegister>();
550 if (type == Primitive::kPrimFloat) {
551 __ MovS(FTMP, f2);
552 __ MovS(f2, f1);
553 __ MovS(f1, FTMP);
554 } else {
555 DCHECK_EQ(type, Primitive::kPrimDouble);
556 __ MovD(FTMP, f2);
557 __ MovD(f2, f1);
558 __ MovD(f1, FTMP);
559 }
560 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
561 (loc1.IsFpuRegister() && loc2.IsRegister())) {
562 // Swap FPR and GPR.
563 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
564 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
565 : loc2.AsFpuRegister<FRegister>();
566 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
567 : loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Mfc1(r2, f1);
570 __ Mtc1(TMP, f1);
571 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
572 // Swap 2 GPR register pairs.
573 Register r1 = loc1.AsRegisterPairLow<Register>();
574 Register r2 = loc2.AsRegisterPairLow<Register>();
575 __ Move(TMP, r2);
576 __ Move(r2, r1);
577 __ Move(r1, TMP);
578 r1 = loc1.AsRegisterPairHigh<Register>();
579 r2 = loc2.AsRegisterPairHigh<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
584 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
585 // Swap FPR and GPR register pair.
586 DCHECK_EQ(type, Primitive::kPrimDouble);
587 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
588 : loc2.AsFpuRegister<FRegister>();
589 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
590 : loc2.AsRegisterPairLow<Register>();
591 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
592 : loc2.AsRegisterPairHigh<Register>();
593 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
594 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
595 // unpredictable and the following mfch1 will fail.
596 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800599 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200600 __ Move(r2_l, TMP);
601 __ Move(r2_h, AT);
602 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
604 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
605 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
607 (loc1.IsStackSlot() && loc2.IsRegister())) {
608 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
609 : loc2.AsRegister<Register>();
610 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
611 : loc2.GetStackIndex();
612 __ Move(TMP, reg);
613 __ LoadFromOffset(kLoadWord, reg, SP, offset);
614 __ StoreToOffset(kStoreWord, TMP, SP, offset);
615 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
616 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
617 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
618 : loc2.AsRegisterPairLow<Register>();
619 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
620 : loc2.AsRegisterPairHigh<Register>();
621 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
622 : loc2.GetStackIndex();
623 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
624 : loc2.GetHighStackIndex(kMipsWordSize);
625 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000628 __ Move(TMP, reg_h);
629 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
630 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200631 } else {
632 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
633 }
634}
635
636void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
637 __ Pop(static_cast<Register>(reg));
638}
639
640void ParallelMoveResolverMIPS::SpillScratch(int reg) {
641 __ Push(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
645 // Allocate a scratch register other than TMP, if available.
646 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
647 // automatically unspilled when the scratch scope object is destroyed).
648 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
649 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
650 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
651 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
652 __ LoadFromOffset(kLoadWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index1 + stack_offset);
656 __ LoadFromOffset(kLoadWord,
657 TMP,
658 SP,
659 index2 + stack_offset);
660 __ StoreToOffset(kStoreWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
665 }
666}
667
668static dwarf::Reg DWARFReg(Register reg) {
669 return dwarf::Reg::MipsCore(static_cast<int>(reg));
670}
671
672// TODO: mapping of floating-point registers to DWARF.
673
674void CodeGeneratorMIPS::GenerateFrameEntry() {
675 __ Bind(&frame_entry_label_);
676
677 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
678
679 if (do_overflow_check) {
680 __ LoadFromOffset(kLoadWord,
681 ZERO,
682 SP,
683 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
684 RecordPcInfo(nullptr, 0);
685 }
686
687 if (HasEmptyFrame()) {
688 return;
689 }
690
691 // Make sure the frame size isn't unreasonably large.
692 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
693 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
694 }
695
696 // Spill callee-saved registers.
697 // Note that their cumulative size is small and they can be indexed using
698 // 16-bit offsets.
699
700 // TODO: increment/decrement SP in one step instead of two or remove this comment.
701
702 uint32_t ofs = FrameEntrySpillSize();
703 bool unaligned_float = ofs & 0x7;
704 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
705 __ IncreaseFrameSize(ofs);
706
707 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
708 Register reg = kCoreCalleeSaves[i];
709 if (allocated_registers_.ContainsCoreRegister(reg)) {
710 ofs -= kMipsWordSize;
711 __ Sw(reg, SP, ofs);
712 __ cfi().RelOffset(DWARFReg(reg), ofs);
713 }
714 }
715
716 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
717 FRegister reg = kFpuCalleeSaves[i];
718 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
719 ofs -= kMipsDoublewordSize;
720 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
721 if (unaligned_float) {
722 if (fpu_32bit) {
723 __ Swc1(reg, SP, ofs);
724 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
725 } else {
726 __ Mfhc1(TMP, reg);
727 __ Swc1(reg, SP, ofs);
728 __ Sw(TMP, SP, ofs + 4);
729 }
730 } else {
731 __ Sdc1(reg, SP, ofs);
732 }
733 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
734 }
735 }
736
737 // Allocate the rest of the frame and store the current method pointer
738 // at its end.
739
740 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
741
742 static_assert(IsInt<16>(kCurrentMethodStackOffset),
743 "kCurrentMethodStackOffset must fit into int16_t");
744 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
745}
746
747void CodeGeneratorMIPS::GenerateFrameExit() {
748 __ cfi().RememberState();
749
750 if (!HasEmptyFrame()) {
751 // Deallocate the rest of the frame.
752
753 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
754
755 // Restore callee-saved registers.
756 // Note that their cumulative size is small and they can be indexed using
757 // 16-bit offsets.
758
759 // TODO: increment/decrement SP in one step instead of two or remove this comment.
760
761 uint32_t ofs = 0;
762 bool unaligned_float = FrameEntrySpillSize() & 0x7;
763 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
764
765 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
766 FRegister reg = kFpuCalleeSaves[i];
767 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
768 if (unaligned_float) {
769 if (fpu_32bit) {
770 __ Lwc1(reg, SP, ofs);
771 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
772 } else {
773 __ Lwc1(reg, SP, ofs);
774 __ Lw(TMP, SP, ofs + 4);
775 __ Mthc1(TMP, reg);
776 }
777 } else {
778 __ Ldc1(reg, SP, ofs);
779 }
780 ofs += kMipsDoublewordSize;
781 // TODO: __ cfi().Restore(DWARFReg(reg));
782 }
783 }
784
785 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
786 Register reg = kCoreCalleeSaves[i];
787 if (allocated_registers_.ContainsCoreRegister(reg)) {
788 __ Lw(reg, SP, ofs);
789 ofs += kMipsWordSize;
790 __ cfi().Restore(DWARFReg(reg));
791 }
792 }
793
794 DCHECK_EQ(ofs, FrameEntrySpillSize());
795 __ DecreaseFrameSize(ofs);
796 }
797
798 __ Jr(RA);
799 __ Nop();
800
801 __ cfi().RestoreState();
802 __ cfi().DefCFAOffset(GetFrameSize());
803}
804
805void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
806 __ Bind(GetLabelOf(block));
807}
808
809void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
810 if (src.Equals(dst)) {
811 return;
812 }
813
814 if (src.IsConstant()) {
815 MoveConstant(dst, src.GetConstant());
816 } else {
817 if (Primitive::Is64BitType(dst_type)) {
818 Move64(dst, src);
819 } else {
820 Move32(dst, src);
821 }
822 }
823}
824
825void CodeGeneratorMIPS::Move32(Location destination, Location source) {
826 if (source.Equals(destination)) {
827 return;
828 }
829
830 if (destination.IsRegister()) {
831 if (source.IsRegister()) {
832 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
833 } else if (source.IsFpuRegister()) {
834 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
838 }
839 } else if (destination.IsFpuRegister()) {
840 if (source.IsRegister()) {
841 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
842 } else if (source.IsFpuRegister()) {
843 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
844 } else {
845 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
846 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
847 }
848 } else {
849 DCHECK(destination.IsStackSlot()) << destination;
850 if (source.IsRegister()) {
851 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
852 } else if (source.IsFpuRegister()) {
853 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
854 } else {
855 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
856 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
857 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
858 }
859 }
860}
861
862void CodeGeneratorMIPS::Move64(Location destination, Location source) {
863 if (source.Equals(destination)) {
864 return;
865 }
866
867 if (destination.IsRegisterPair()) {
868 if (source.IsRegisterPair()) {
869 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
870 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
871 } else if (source.IsFpuRegister()) {
872 Register dst_high = destination.AsRegisterPairHigh<Register>();
873 Register dst_low = destination.AsRegisterPairLow<Register>();
874 FRegister src = source.AsFpuRegister<FRegister>();
875 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800876 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200877 } else {
878 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
879 int32_t off = source.GetStackIndex();
880 Register r = destination.AsRegisterPairLow<Register>();
881 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
882 }
883 } else if (destination.IsFpuRegister()) {
884 if (source.IsRegisterPair()) {
885 FRegister dst = destination.AsFpuRegister<FRegister>();
886 Register src_high = source.AsRegisterPairHigh<Register>();
887 Register src_low = source.AsRegisterPairLow<Register>();
888 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800889 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200890 } else if (source.IsFpuRegister()) {
891 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
892 } else {
893 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
894 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
895 }
896 } else {
897 DCHECK(destination.IsDoubleStackSlot()) << destination;
898 int32_t off = destination.GetStackIndex();
899 if (source.IsRegisterPair()) {
900 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
901 } else if (source.IsFpuRegister()) {
902 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
903 } else {
904 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
905 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
906 __ StoreToOffset(kStoreWord, TMP, SP, off);
907 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
908 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
909 }
910 }
911}
912
913void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
914 if (c->IsIntConstant() || c->IsNullConstant()) {
915 // Move 32 bit constant.
916 int32_t value = GetInt32ValueOf(c);
917 if (destination.IsRegister()) {
918 Register dst = destination.AsRegister<Register>();
919 __ LoadConst32(dst, value);
920 } else {
921 DCHECK(destination.IsStackSlot())
922 << "Cannot move " << c->DebugName() << " to " << destination;
923 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
924 }
925 } else if (c->IsLongConstant()) {
926 // Move 64 bit constant.
927 int64_t value = GetInt64ValueOf(c);
928 if (destination.IsRegisterPair()) {
929 Register r_h = destination.AsRegisterPairHigh<Register>();
930 Register r_l = destination.AsRegisterPairLow<Register>();
931 __ LoadConst64(r_h, r_l, value);
932 } else {
933 DCHECK(destination.IsDoubleStackSlot())
934 << "Cannot move " << c->DebugName() << " to " << destination;
935 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
936 }
937 } else if (c->IsFloatConstant()) {
938 // Move 32 bit float constant.
939 int32_t value = GetInt32ValueOf(c);
940 if (destination.IsFpuRegister()) {
941 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
942 } else {
943 DCHECK(destination.IsStackSlot())
944 << "Cannot move " << c->DebugName() << " to " << destination;
945 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
946 }
947 } else {
948 // Move 64 bit double constant.
949 DCHECK(c->IsDoubleConstant()) << c->DebugName();
950 int64_t value = GetInt64ValueOf(c);
951 if (destination.IsFpuRegister()) {
952 FRegister fd = destination.AsFpuRegister<FRegister>();
953 __ LoadDConst64(fd, value, TMP);
954 } else {
955 DCHECK(destination.IsDoubleStackSlot())
956 << "Cannot move " << c->DebugName() << " to " << destination;
957 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
958 }
959 }
960}
961
962void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
963 DCHECK(destination.IsRegister());
964 Register dst = destination.AsRegister<Register>();
965 __ LoadConst32(dst, value);
966}
967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200968void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
969 if (location.IsRegister()) {
970 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700971 } else if (location.IsRegisterPair()) {
972 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
973 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200974 } else {
975 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
976 }
977}
978
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200979void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
980 MipsLabel done;
981 Register card = AT;
982 Register temp = TMP;
983 __ Beqz(value, &done);
984 __ LoadFromOffset(kLoadWord,
985 card,
986 TR,
987 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
988 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
989 __ Addu(temp, card, temp);
990 __ Sb(card, temp, 0);
991 __ Bind(&done);
992}
993
David Brazdil58282f42016-01-14 12:45:10 +0000994void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200995 // Don't allocate the dalvik style register pair passing.
996 blocked_register_pairs_[A1_A2] = true;
997
998 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
999 blocked_core_registers_[ZERO] = true;
1000 blocked_core_registers_[K0] = true;
1001 blocked_core_registers_[K1] = true;
1002 blocked_core_registers_[GP] = true;
1003 blocked_core_registers_[SP] = true;
1004 blocked_core_registers_[RA] = true;
1005
1006 // AT and TMP(T8) are used as temporary/scratch registers
1007 // (similar to how AT is used by MIPS assemblers).
1008 blocked_core_registers_[AT] = true;
1009 blocked_core_registers_[TMP] = true;
1010 blocked_fpu_registers_[FTMP] = true;
1011
1012 // Reserve suspend and thread registers.
1013 blocked_core_registers_[S0] = true;
1014 blocked_core_registers_[TR] = true;
1015
1016 // Reserve T9 for function calls
1017 blocked_core_registers_[T9] = true;
1018
1019 // Reserve odd-numbered FPU registers.
1020 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1021 blocked_fpu_registers_[i] = true;
1022 }
1023
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001024 UpdateBlockedPairRegisters();
1025}
1026
1027void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1028 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1029 MipsManagedRegister current =
1030 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1031 if (blocked_core_registers_[current.AsRegisterPairLow()]
1032 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1033 blocked_register_pairs_[i] = true;
1034 }
1035 }
1036}
1037
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001038size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1039 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1040 return kMipsWordSize;
1041}
1042
1043size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1044 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1045 return kMipsWordSize;
1046}
1047
1048size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1049 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1050 return kMipsDoublewordSize;
1051}
1052
1053size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1054 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1055 return kMipsDoublewordSize;
1056}
1057
1058void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001059 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001060}
1061
1062void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001063 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001064}
1065
1066void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1067 HInstruction* instruction,
1068 uint32_t dex_pc,
1069 SlowPathCode* slow_path) {
1070 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1071 instruction,
1072 dex_pc,
1073 slow_path,
1074 IsDirectEntrypoint(entrypoint));
1075}
1076
1077constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1078
1079void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1080 HInstruction* instruction,
1081 uint32_t dex_pc,
1082 SlowPathCode* slow_path,
1083 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001084 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1085 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001086 if (is_direct_entrypoint) {
1087 // Reserve argument space on stack (for $a0-$a3) for
1088 // entrypoints that directly reference native implementations.
1089 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001090 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001091 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001092 } else {
1093 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001094 }
1095 RecordPcInfo(instruction, dex_pc, slow_path);
1096}
1097
1098void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1099 Register class_reg) {
1100 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1101 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1102 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1103 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1104 __ Sync(0);
1105 __ Bind(slow_path->GetExitLabel());
1106}
1107
1108void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1109 __ Sync(0); // Only stype 0 is supported.
1110}
1111
1112void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1113 HBasicBlock* successor) {
1114 SuspendCheckSlowPathMIPS* slow_path =
1115 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1116 codegen_->AddSlowPath(slow_path);
1117
1118 __ LoadFromOffset(kLoadUnsignedHalfword,
1119 TMP,
1120 TR,
1121 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1122 if (successor == nullptr) {
1123 __ Bnez(TMP, slow_path->GetEntryLabel());
1124 __ Bind(slow_path->GetReturnLabel());
1125 } else {
1126 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1127 __ B(slow_path->GetEntryLabel());
1128 // slow_path will return to GetLabelOf(successor).
1129 }
1130}
1131
1132InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1133 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001134 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001135 assembler_(codegen->GetAssembler()),
1136 codegen_(codegen) {}
1137
1138void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1139 DCHECK_EQ(instruction->InputCount(), 2U);
1140 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1141 Primitive::Type type = instruction->GetResultType();
1142 switch (type) {
1143 case Primitive::kPrimInt: {
1144 locations->SetInAt(0, Location::RequiresRegister());
1145 HInstruction* right = instruction->InputAt(1);
1146 bool can_use_imm = false;
1147 if (right->IsConstant()) {
1148 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1149 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1150 can_use_imm = IsUint<16>(imm);
1151 } else if (instruction->IsAdd()) {
1152 can_use_imm = IsInt<16>(imm);
1153 } else {
1154 DCHECK(instruction->IsSub());
1155 can_use_imm = IsInt<16>(-imm);
1156 }
1157 }
1158 if (can_use_imm)
1159 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1160 else
1161 locations->SetInAt(1, Location::RequiresRegister());
1162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1163 break;
1164 }
1165
1166 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001167 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001168 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1169 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001170 break;
1171 }
1172
1173 case Primitive::kPrimFloat:
1174 case Primitive::kPrimDouble:
1175 DCHECK(instruction->IsAdd() || instruction->IsSub());
1176 locations->SetInAt(0, Location::RequiresFpuRegister());
1177 locations->SetInAt(1, Location::RequiresFpuRegister());
1178 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1179 break;
1180
1181 default:
1182 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1183 }
1184}
1185
1186void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1187 Primitive::Type type = instruction->GetType();
1188 LocationSummary* locations = instruction->GetLocations();
1189
1190 switch (type) {
1191 case Primitive::kPrimInt: {
1192 Register dst = locations->Out().AsRegister<Register>();
1193 Register lhs = locations->InAt(0).AsRegister<Register>();
1194 Location rhs_location = locations->InAt(1);
1195
1196 Register rhs_reg = ZERO;
1197 int32_t rhs_imm = 0;
1198 bool use_imm = rhs_location.IsConstant();
1199 if (use_imm) {
1200 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1201 } else {
1202 rhs_reg = rhs_location.AsRegister<Register>();
1203 }
1204
1205 if (instruction->IsAnd()) {
1206 if (use_imm)
1207 __ Andi(dst, lhs, rhs_imm);
1208 else
1209 __ And(dst, lhs, rhs_reg);
1210 } else if (instruction->IsOr()) {
1211 if (use_imm)
1212 __ Ori(dst, lhs, rhs_imm);
1213 else
1214 __ Or(dst, lhs, rhs_reg);
1215 } else if (instruction->IsXor()) {
1216 if (use_imm)
1217 __ Xori(dst, lhs, rhs_imm);
1218 else
1219 __ Xor(dst, lhs, rhs_reg);
1220 } else if (instruction->IsAdd()) {
1221 if (use_imm)
1222 __ Addiu(dst, lhs, rhs_imm);
1223 else
1224 __ Addu(dst, lhs, rhs_reg);
1225 } else {
1226 DCHECK(instruction->IsSub());
1227 if (use_imm)
1228 __ Addiu(dst, lhs, -rhs_imm);
1229 else
1230 __ Subu(dst, lhs, rhs_reg);
1231 }
1232 break;
1233 }
1234
1235 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1237 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1238 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1239 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001240 Location rhs_location = locations->InAt(1);
1241 bool use_imm = rhs_location.IsConstant();
1242 if (!use_imm) {
1243 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1244 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1245 if (instruction->IsAnd()) {
1246 __ And(dst_low, lhs_low, rhs_low);
1247 __ And(dst_high, lhs_high, rhs_high);
1248 } else if (instruction->IsOr()) {
1249 __ Or(dst_low, lhs_low, rhs_low);
1250 __ Or(dst_high, lhs_high, rhs_high);
1251 } else if (instruction->IsXor()) {
1252 __ Xor(dst_low, lhs_low, rhs_low);
1253 __ Xor(dst_high, lhs_high, rhs_high);
1254 } else if (instruction->IsAdd()) {
1255 if (lhs_low == rhs_low) {
1256 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1257 __ Slt(TMP, lhs_low, ZERO);
1258 __ Addu(dst_low, lhs_low, rhs_low);
1259 } else {
1260 __ Addu(dst_low, lhs_low, rhs_low);
1261 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1262 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1263 }
1264 __ Addu(dst_high, lhs_high, rhs_high);
1265 __ Addu(dst_high, dst_high, TMP);
1266 } else {
1267 DCHECK(instruction->IsSub());
1268 __ Sltu(TMP, lhs_low, rhs_low);
1269 __ Subu(dst_low, lhs_low, rhs_low);
1270 __ Subu(dst_high, lhs_high, rhs_high);
1271 __ Subu(dst_high, dst_high, TMP);
1272 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001273 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001274 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1275 if (instruction->IsOr()) {
1276 uint32_t low = Low32Bits(value);
1277 uint32_t high = High32Bits(value);
1278 if (IsUint<16>(low)) {
1279 if (dst_low != lhs_low || low != 0) {
1280 __ Ori(dst_low, lhs_low, low);
1281 }
1282 } else {
1283 __ LoadConst32(TMP, low);
1284 __ Or(dst_low, lhs_low, TMP);
1285 }
1286 if (IsUint<16>(high)) {
1287 if (dst_high != lhs_high || high != 0) {
1288 __ Ori(dst_high, lhs_high, high);
1289 }
1290 } else {
1291 if (high != low) {
1292 __ LoadConst32(TMP, high);
1293 }
1294 __ Or(dst_high, lhs_high, TMP);
1295 }
1296 } else if (instruction->IsXor()) {
1297 uint32_t low = Low32Bits(value);
1298 uint32_t high = High32Bits(value);
1299 if (IsUint<16>(low)) {
1300 if (dst_low != lhs_low || low != 0) {
1301 __ Xori(dst_low, lhs_low, low);
1302 }
1303 } else {
1304 __ LoadConst32(TMP, low);
1305 __ Xor(dst_low, lhs_low, TMP);
1306 }
1307 if (IsUint<16>(high)) {
1308 if (dst_high != lhs_high || high != 0) {
1309 __ Xori(dst_high, lhs_high, high);
1310 }
1311 } else {
1312 if (high != low) {
1313 __ LoadConst32(TMP, high);
1314 }
1315 __ Xor(dst_high, lhs_high, TMP);
1316 }
1317 } else if (instruction->IsAnd()) {
1318 uint32_t low = Low32Bits(value);
1319 uint32_t high = High32Bits(value);
1320 if (IsUint<16>(low)) {
1321 __ Andi(dst_low, lhs_low, low);
1322 } else if (low != 0xFFFFFFFF) {
1323 __ LoadConst32(TMP, low);
1324 __ And(dst_low, lhs_low, TMP);
1325 } else if (dst_low != lhs_low) {
1326 __ Move(dst_low, lhs_low);
1327 }
1328 if (IsUint<16>(high)) {
1329 __ Andi(dst_high, lhs_high, high);
1330 } else if (high != 0xFFFFFFFF) {
1331 if (high != low) {
1332 __ LoadConst32(TMP, high);
1333 }
1334 __ And(dst_high, lhs_high, TMP);
1335 } else if (dst_high != lhs_high) {
1336 __ Move(dst_high, lhs_high);
1337 }
1338 } else {
1339 if (instruction->IsSub()) {
1340 value = -value;
1341 } else {
1342 DCHECK(instruction->IsAdd());
1343 }
1344 int32_t low = Low32Bits(value);
1345 int32_t high = High32Bits(value);
1346 if (IsInt<16>(low)) {
1347 if (dst_low != lhs_low || low != 0) {
1348 __ Addiu(dst_low, lhs_low, low);
1349 }
1350 if (low != 0) {
1351 __ Sltiu(AT, dst_low, low);
1352 }
1353 } else {
1354 __ LoadConst32(TMP, low);
1355 __ Addu(dst_low, lhs_low, TMP);
1356 __ Sltu(AT, dst_low, TMP);
1357 }
1358 if (IsInt<16>(high)) {
1359 if (dst_high != lhs_high || high != 0) {
1360 __ Addiu(dst_high, lhs_high, high);
1361 }
1362 } else {
1363 if (high != low) {
1364 __ LoadConst32(TMP, high);
1365 }
1366 __ Addu(dst_high, lhs_high, TMP);
1367 }
1368 if (low != 0) {
1369 __ Addu(dst_high, dst_high, AT);
1370 }
1371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001372 }
1373 break;
1374 }
1375
1376 case Primitive::kPrimFloat:
1377 case Primitive::kPrimDouble: {
1378 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1379 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1380 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1381 if (instruction->IsAdd()) {
1382 if (type == Primitive::kPrimFloat) {
1383 __ AddS(dst, lhs, rhs);
1384 } else {
1385 __ AddD(dst, lhs, rhs);
1386 }
1387 } else {
1388 DCHECK(instruction->IsSub());
1389 if (type == Primitive::kPrimFloat) {
1390 __ SubS(dst, lhs, rhs);
1391 } else {
1392 __ SubD(dst, lhs, rhs);
1393 }
1394 }
1395 break;
1396 }
1397
1398 default:
1399 LOG(FATAL) << "Unexpected binary operation type " << type;
1400 }
1401}
1402
1403void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001404 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405
1406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1407 Primitive::Type type = instr->GetResultType();
1408 switch (type) {
1409 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001410 locations->SetInAt(0, Location::RequiresRegister());
1411 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1413 break;
1414 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415 locations->SetInAt(0, Location::RequiresRegister());
1416 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1417 locations->SetOut(Location::RequiresRegister());
1418 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 default:
1420 LOG(FATAL) << "Unexpected shift type " << type;
1421 }
1422}
1423
1424static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1425
1426void InstructionCodeGeneratorMIPS::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 LocationSummary* locations = instr->GetLocations();
1429 Primitive::Type type = instr->GetType();
1430
1431 Location rhs_location = locations->InAt(1);
1432 bool use_imm = rhs_location.IsConstant();
1433 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1434 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001435 const uint32_t shift_mask =
1436 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001437 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001438 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1439 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001440
1441 switch (type) {
1442 case Primitive::kPrimInt: {
1443 Register dst = locations->Out().AsRegister<Register>();
1444 Register lhs = locations->InAt(0).AsRegister<Register>();
1445 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001446 if (shift_value == 0) {
1447 if (dst != lhs) {
1448 __ Move(dst, lhs);
1449 }
1450 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451 __ Sll(dst, lhs, shift_value);
1452 } else if (instr->IsShr()) {
1453 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001454 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001455 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001456 } else {
1457 if (has_ins_rotr) {
1458 __ Rotr(dst, lhs, shift_value);
1459 } else {
1460 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1461 __ Srl(dst, lhs, shift_value);
1462 __ Or(dst, dst, TMP);
1463 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001464 }
1465 } else {
1466 if (instr->IsShl()) {
1467 __ Sllv(dst, lhs, rhs_reg);
1468 } else if (instr->IsShr()) {
1469 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001470 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001471 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001472 } else {
1473 if (has_ins_rotr) {
1474 __ Rotrv(dst, lhs, rhs_reg);
1475 } else {
1476 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001477 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1478 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1479 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1480 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1481 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001482 __ Sllv(TMP, lhs, TMP);
1483 __ Srlv(dst, lhs, rhs_reg);
1484 __ Or(dst, dst, TMP);
1485 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001486 }
1487 }
1488 break;
1489 }
1490
1491 case Primitive::kPrimLong: {
1492 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1493 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1494 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1495 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1496 if (use_imm) {
1497 if (shift_value == 0) {
1498 codegen_->Move64(locations->Out(), locations->InAt(0));
1499 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001500 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001501 if (instr->IsShl()) {
1502 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1503 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1504 __ Sll(dst_low, lhs_low, shift_value);
1505 } else if (instr->IsShr()) {
1506 __ Srl(dst_low, lhs_low, shift_value);
1507 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1508 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001509 } else if (instr->IsUShr()) {
1510 __ Srl(dst_low, lhs_low, shift_value);
1511 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1512 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001513 } else {
1514 __ Srl(dst_low, lhs_low, shift_value);
1515 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1516 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001517 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001519 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001520 if (instr->IsShl()) {
1521 __ Sll(dst_low, lhs_low, shift_value);
1522 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1523 __ Sll(dst_high, lhs_high, shift_value);
1524 __ Or(dst_high, dst_high, TMP);
1525 } else if (instr->IsShr()) {
1526 __ Sra(dst_high, lhs_high, shift_value);
1527 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1528 __ Srl(dst_low, lhs_low, shift_value);
1529 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001530 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001531 __ Srl(dst_high, lhs_high, shift_value);
1532 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1533 __ Srl(dst_low, lhs_low, shift_value);
1534 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001535 } else {
1536 __ Srl(TMP, lhs_low, shift_value);
1537 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1538 __ Or(dst_low, dst_low, TMP);
1539 __ Srl(TMP, lhs_high, shift_value);
1540 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1541 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001542 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001543 }
1544 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001545 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001546 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001547 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 __ Move(dst_low, ZERO);
1549 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001550 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001551 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001552 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001553 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001555 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001556 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001557 // 64-bit rotation by 32 is just a swap.
1558 __ Move(dst_low, lhs_high);
1559 __ Move(dst_high, lhs_low);
1560 } else {
1561 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001562 __ Srl(dst_low, lhs_high, shift_value_high);
1563 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1564 __ Srl(dst_high, lhs_low, shift_value_high);
1565 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001566 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001567 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1568 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001569 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001570 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1571 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001572 __ Or(dst_high, dst_high, TMP);
1573 }
1574 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575 }
1576 }
1577 } else {
1578 MipsLabel done;
1579 if (instr->IsShl()) {
1580 __ Sllv(dst_low, lhs_low, rhs_reg);
1581 __ Nor(AT, ZERO, rhs_reg);
1582 __ Srl(TMP, lhs_low, 1);
1583 __ Srlv(TMP, TMP, AT);
1584 __ Sllv(dst_high, lhs_high, rhs_reg);
1585 __ Or(dst_high, dst_high, TMP);
1586 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1587 __ Beqz(TMP, &done);
1588 __ Move(dst_high, dst_low);
1589 __ Move(dst_low, ZERO);
1590 } else if (instr->IsShr()) {
1591 __ Srav(dst_high, lhs_high, rhs_reg);
1592 __ Nor(AT, ZERO, rhs_reg);
1593 __ Sll(TMP, lhs_high, 1);
1594 __ Sllv(TMP, TMP, AT);
1595 __ Srlv(dst_low, lhs_low, rhs_reg);
1596 __ Or(dst_low, dst_low, TMP);
1597 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1598 __ Beqz(TMP, &done);
1599 __ Move(dst_low, dst_high);
1600 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001601 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001602 __ Srlv(dst_high, lhs_high, rhs_reg);
1603 __ Nor(AT, ZERO, rhs_reg);
1604 __ Sll(TMP, lhs_high, 1);
1605 __ Sllv(TMP, TMP, AT);
1606 __ Srlv(dst_low, lhs_low, rhs_reg);
1607 __ Or(dst_low, dst_low, TMP);
1608 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1609 __ Beqz(TMP, &done);
1610 __ Move(dst_low, dst_high);
1611 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001612 } else {
1613 __ Nor(AT, ZERO, rhs_reg);
1614 __ Srlv(TMP, lhs_low, rhs_reg);
1615 __ Sll(dst_low, lhs_high, 1);
1616 __ Sllv(dst_low, dst_low, AT);
1617 __ Or(dst_low, dst_low, TMP);
1618 __ Srlv(TMP, lhs_high, rhs_reg);
1619 __ Sll(dst_high, lhs_low, 1);
1620 __ Sllv(dst_high, dst_high, AT);
1621 __ Or(dst_high, dst_high, TMP);
1622 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1623 __ Beqz(TMP, &done);
1624 __ Move(TMP, dst_high);
1625 __ Move(dst_high, dst_low);
1626 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001627 }
1628 __ Bind(&done);
1629 }
1630 break;
1631 }
1632
1633 default:
1634 LOG(FATAL) << "Unexpected shift operation type " << type;
1635 }
1636}
1637
1638void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1639 HandleBinaryOp(instruction);
1640}
1641
1642void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1643 HandleBinaryOp(instruction);
1644}
1645
1646void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1647 HandleBinaryOp(instruction);
1648}
1649
1650void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1651 HandleBinaryOp(instruction);
1652}
1653
1654void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1655 LocationSummary* locations =
1656 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1657 locations->SetInAt(0, Location::RequiresRegister());
1658 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1659 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1660 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1661 } else {
1662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1663 }
1664}
1665
1666void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1667 LocationSummary* locations = instruction->GetLocations();
1668 Register obj = locations->InAt(0).AsRegister<Register>();
1669 Location index = locations->InAt(1);
1670 Primitive::Type type = instruction->GetType();
1671
1672 switch (type) {
1673 case Primitive::kPrimBoolean: {
1674 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1675 Register out = locations->Out().AsRegister<Register>();
1676 if (index.IsConstant()) {
1677 size_t offset =
1678 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1679 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1680 } else {
1681 __ Addu(TMP, obj, index.AsRegister<Register>());
1682 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1683 }
1684 break;
1685 }
1686
1687 case Primitive::kPrimByte: {
1688 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1689 Register out = locations->Out().AsRegister<Register>();
1690 if (index.IsConstant()) {
1691 size_t offset =
1692 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1693 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1694 } else {
1695 __ Addu(TMP, obj, index.AsRegister<Register>());
1696 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1697 }
1698 break;
1699 }
1700
1701 case Primitive::kPrimShort: {
1702 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1703 Register out = locations->Out().AsRegister<Register>();
1704 if (index.IsConstant()) {
1705 size_t offset =
1706 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1707 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1708 } else {
1709 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1710 __ Addu(TMP, obj, TMP);
1711 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1712 }
1713 break;
1714 }
1715
1716 case Primitive::kPrimChar: {
1717 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1718 Register out = locations->Out().AsRegister<Register>();
1719 if (index.IsConstant()) {
1720 size_t offset =
1721 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1722 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1723 } else {
1724 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1725 __ Addu(TMP, obj, TMP);
1726 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1727 }
1728 break;
1729 }
1730
1731 case Primitive::kPrimInt:
1732 case Primitive::kPrimNot: {
1733 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1734 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1735 Register out = locations->Out().AsRegister<Register>();
1736 if (index.IsConstant()) {
1737 size_t offset =
1738 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1739 __ LoadFromOffset(kLoadWord, out, obj, offset);
1740 } else {
1741 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1742 __ Addu(TMP, obj, TMP);
1743 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1744 }
1745 break;
1746 }
1747
1748 case Primitive::kPrimLong: {
1749 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1750 Register out = locations->Out().AsRegisterPairLow<Register>();
1751 if (index.IsConstant()) {
1752 size_t offset =
1753 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1754 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1755 } else {
1756 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1757 __ Addu(TMP, obj, TMP);
1758 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1759 }
1760 break;
1761 }
1762
1763 case Primitive::kPrimFloat: {
1764 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1765 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1766 if (index.IsConstant()) {
1767 size_t offset =
1768 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1769 __ LoadSFromOffset(out, obj, offset);
1770 } else {
1771 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1772 __ Addu(TMP, obj, TMP);
1773 __ LoadSFromOffset(out, TMP, data_offset);
1774 }
1775 break;
1776 }
1777
1778 case Primitive::kPrimDouble: {
1779 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1780 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1781 if (index.IsConstant()) {
1782 size_t offset =
1783 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1784 __ LoadDFromOffset(out, obj, offset);
1785 } else {
1786 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1787 __ Addu(TMP, obj, TMP);
1788 __ LoadDFromOffset(out, TMP, data_offset);
1789 }
1790 break;
1791 }
1792
1793 case Primitive::kPrimVoid:
1794 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1795 UNREACHABLE();
1796 }
1797 codegen_->MaybeRecordImplicitNullCheck(instruction);
1798}
1799
1800void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1802 locations->SetInAt(0, Location::RequiresRegister());
1803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1804}
1805
1806void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1807 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001808 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001809 Register obj = locations->InAt(0).AsRegister<Register>();
1810 Register out = locations->Out().AsRegister<Register>();
1811 __ LoadFromOffset(kLoadWord, out, obj, offset);
1812 codegen_->MaybeRecordImplicitNullCheck(instruction);
1813}
1814
1815void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001816 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001817 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1818 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001819 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1820 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001821 InvokeRuntimeCallingConvention calling_convention;
1822 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1823 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1824 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1825 } else {
1826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1828 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1829 locations->SetInAt(2, Location::RequiresFpuRegister());
1830 } else {
1831 locations->SetInAt(2, Location::RequiresRegister());
1832 }
1833 }
1834}
1835
1836void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1837 LocationSummary* locations = instruction->GetLocations();
1838 Register obj = locations->InAt(0).AsRegister<Register>();
1839 Location index = locations->InAt(1);
1840 Primitive::Type value_type = instruction->GetComponentType();
1841 bool needs_runtime_call = locations->WillCall();
1842 bool needs_write_barrier =
1843 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1844
1845 switch (value_type) {
1846 case Primitive::kPrimBoolean:
1847 case Primitive::kPrimByte: {
1848 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1849 Register value = locations->InAt(2).AsRegister<Register>();
1850 if (index.IsConstant()) {
1851 size_t offset =
1852 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1853 __ StoreToOffset(kStoreByte, value, obj, offset);
1854 } else {
1855 __ Addu(TMP, obj, index.AsRegister<Register>());
1856 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1857 }
1858 break;
1859 }
1860
1861 case Primitive::kPrimShort:
1862 case Primitive::kPrimChar: {
1863 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1864 Register value = locations->InAt(2).AsRegister<Register>();
1865 if (index.IsConstant()) {
1866 size_t offset =
1867 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1868 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1869 } else {
1870 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1871 __ Addu(TMP, obj, TMP);
1872 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1873 }
1874 break;
1875 }
1876
1877 case Primitive::kPrimInt:
1878 case Primitive::kPrimNot: {
1879 if (!needs_runtime_call) {
1880 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1881 Register value = locations->InAt(2).AsRegister<Register>();
1882 if (index.IsConstant()) {
1883 size_t offset =
1884 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1885 __ StoreToOffset(kStoreWord, value, obj, offset);
1886 } else {
1887 DCHECK(index.IsRegister()) << index;
1888 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1889 __ Addu(TMP, obj, TMP);
1890 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1891 }
1892 codegen_->MaybeRecordImplicitNullCheck(instruction);
1893 if (needs_write_barrier) {
1894 DCHECK_EQ(value_type, Primitive::kPrimNot);
1895 codegen_->MarkGCCard(obj, value);
1896 }
1897 } else {
1898 DCHECK_EQ(value_type, Primitive::kPrimNot);
1899 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1900 instruction,
1901 instruction->GetDexPc(),
1902 nullptr,
1903 IsDirectEntrypoint(kQuickAputObject));
1904 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimLong: {
1910 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1911 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1912 if (index.IsConstant()) {
1913 size_t offset =
1914 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1915 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1916 } else {
1917 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1918 __ Addu(TMP, obj, TMP);
1919 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1920 }
1921 break;
1922 }
1923
1924 case Primitive::kPrimFloat: {
1925 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1926 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1927 DCHECK(locations->InAt(2).IsFpuRegister());
1928 if (index.IsConstant()) {
1929 size_t offset =
1930 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1931 __ StoreSToOffset(value, obj, offset);
1932 } else {
1933 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1934 __ Addu(TMP, obj, TMP);
1935 __ StoreSToOffset(value, TMP, data_offset);
1936 }
1937 break;
1938 }
1939
1940 case Primitive::kPrimDouble: {
1941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1942 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1943 DCHECK(locations->InAt(2).IsFpuRegister());
1944 if (index.IsConstant()) {
1945 size_t offset =
1946 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1947 __ StoreDToOffset(value, obj, offset);
1948 } else {
1949 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1950 __ Addu(TMP, obj, TMP);
1951 __ StoreDToOffset(value, TMP, data_offset);
1952 }
1953 break;
1954 }
1955
1956 case Primitive::kPrimVoid:
1957 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1958 UNREACHABLE();
1959 }
1960
1961 // Ints and objects are handled in the switch.
1962 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1963 codegen_->MaybeRecordImplicitNullCheck(instruction);
1964 }
1965}
1966
1967void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1968 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1969 ? LocationSummary::kCallOnSlowPath
1970 : LocationSummary::kNoCall;
1971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetInAt(1, Location::RequiresRegister());
1974 if (instruction->HasUses()) {
1975 locations->SetOut(Location::SameAsFirstInput());
1976 }
1977}
1978
1979void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1980 LocationSummary* locations = instruction->GetLocations();
1981 BoundsCheckSlowPathMIPS* slow_path =
1982 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1983 codegen_->AddSlowPath(slow_path);
1984
1985 Register index = locations->InAt(0).AsRegister<Register>();
1986 Register length = locations->InAt(1).AsRegister<Register>();
1987
1988 // length is limited by the maximum positive signed 32-bit integer.
1989 // Unsigned comparison of length and index checks for index < 0
1990 // and for length <= index simultaneously.
1991 __ Bgeu(index, length, slow_path->GetEntryLabel());
1992}
1993
1994void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1995 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1996 instruction,
1997 LocationSummary::kCallOnSlowPath);
1998 locations->SetInAt(0, Location::RequiresRegister());
1999 locations->SetInAt(1, Location::RequiresRegister());
2000 // Note that TypeCheckSlowPathMIPS uses this register too.
2001 locations->AddTemp(Location::RequiresRegister());
2002}
2003
2004void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2005 LocationSummary* locations = instruction->GetLocations();
2006 Register obj = locations->InAt(0).AsRegister<Register>();
2007 Register cls = locations->InAt(1).AsRegister<Register>();
2008 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2009
2010 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2011 codegen_->AddSlowPath(slow_path);
2012
2013 // TODO: avoid this check if we know obj is not null.
2014 __ Beqz(obj, slow_path->GetExitLabel());
2015 // Compare the class of `obj` with `cls`.
2016 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2017 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2018 __ Bind(slow_path->GetExitLabel());
2019}
2020
2021void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 if (check->HasUses()) {
2026 locations->SetOut(Location::SameAsFirstInput());
2027 }
2028}
2029
2030void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2031 // We assume the class is not null.
2032 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2033 check->GetLoadClass(),
2034 check,
2035 check->GetDexPc(),
2036 true);
2037 codegen_->AddSlowPath(slow_path);
2038 GenerateClassInitializationCheck(slow_path,
2039 check->GetLocations()->InAt(0).AsRegister<Register>());
2040}
2041
2042void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2043 Primitive::Type in_type = compare->InputAt(0)->GetType();
2044
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002045 LocationSummary* locations =
2046 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047
2048 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002049 case Primitive::kPrimBoolean:
2050 case Primitive::kPrimByte:
2051 case Primitive::kPrimShort:
2052 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002053 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 case Primitive::kPrimLong:
2055 locations->SetInAt(0, Location::RequiresRegister());
2056 locations->SetInAt(1, Location::RequiresRegister());
2057 // Output overlaps because it is written before doing the low comparison.
2058 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2059 break;
2060
2061 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002062 case Primitive::kPrimDouble:
2063 locations->SetInAt(0, Location::RequiresFpuRegister());
2064 locations->SetInAt(1, Location::RequiresFpuRegister());
2065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002067
2068 default:
2069 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2070 }
2071}
2072
2073void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2074 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002075 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002077 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078
2079 // 0 if: left == right
2080 // 1 if: left > right
2081 // -1 if: left < right
2082 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002083 case Primitive::kPrimBoolean:
2084 case Primitive::kPrimByte:
2085 case Primitive::kPrimShort:
2086 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002087 case Primitive::kPrimInt: {
2088 Register lhs = locations->InAt(0).AsRegister<Register>();
2089 Register rhs = locations->InAt(1).AsRegister<Register>();
2090 __ Slt(TMP, lhs, rhs);
2091 __ Slt(res, rhs, lhs);
2092 __ Subu(res, res, TMP);
2093 break;
2094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095 case Primitive::kPrimLong: {
2096 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002097 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2098 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2099 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2100 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2101 // TODO: more efficient (direct) comparison with a constant.
2102 __ Slt(TMP, lhs_high, rhs_high);
2103 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2104 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2105 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2106 __ Sltu(TMP, lhs_low, rhs_low);
2107 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2108 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2109 __ Bind(&done);
2110 break;
2111 }
2112
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002113 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002114 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002115 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2116 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2117 MipsLabel done;
2118 if (isR6) {
2119 __ CmpEqS(FTMP, lhs, rhs);
2120 __ LoadConst32(res, 0);
2121 __ Bc1nez(FTMP, &done);
2122 if (gt_bias) {
2123 __ CmpLtS(FTMP, lhs, rhs);
2124 __ LoadConst32(res, -1);
2125 __ Bc1nez(FTMP, &done);
2126 __ LoadConst32(res, 1);
2127 } else {
2128 __ CmpLtS(FTMP, rhs, lhs);
2129 __ LoadConst32(res, 1);
2130 __ Bc1nez(FTMP, &done);
2131 __ LoadConst32(res, -1);
2132 }
2133 } else {
2134 if (gt_bias) {
2135 __ ColtS(0, lhs, rhs);
2136 __ LoadConst32(res, -1);
2137 __ Bc1t(0, &done);
2138 __ CeqS(0, lhs, rhs);
2139 __ LoadConst32(res, 1);
2140 __ Movt(res, ZERO, 0);
2141 } else {
2142 __ ColtS(0, rhs, lhs);
2143 __ LoadConst32(res, 1);
2144 __ Bc1t(0, &done);
2145 __ CeqS(0, lhs, rhs);
2146 __ LoadConst32(res, -1);
2147 __ Movt(res, ZERO, 0);
2148 }
2149 }
2150 __ Bind(&done);
2151 break;
2152 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002153 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002154 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002155 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2156 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2157 MipsLabel done;
2158 if (isR6) {
2159 __ CmpEqD(FTMP, lhs, rhs);
2160 __ LoadConst32(res, 0);
2161 __ Bc1nez(FTMP, &done);
2162 if (gt_bias) {
2163 __ CmpLtD(FTMP, lhs, rhs);
2164 __ LoadConst32(res, -1);
2165 __ Bc1nez(FTMP, &done);
2166 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002167 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002168 __ CmpLtD(FTMP, rhs, lhs);
2169 __ LoadConst32(res, 1);
2170 __ Bc1nez(FTMP, &done);
2171 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002172 }
2173 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002174 if (gt_bias) {
2175 __ ColtD(0, lhs, rhs);
2176 __ LoadConst32(res, -1);
2177 __ Bc1t(0, &done);
2178 __ CeqD(0, lhs, rhs);
2179 __ LoadConst32(res, 1);
2180 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002181 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002182 __ ColtD(0, rhs, lhs);
2183 __ LoadConst32(res, 1);
2184 __ Bc1t(0, &done);
2185 __ CeqD(0, lhs, rhs);
2186 __ LoadConst32(res, -1);
2187 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188 }
2189 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002190 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002191 break;
2192 }
2193
2194 default:
2195 LOG(FATAL) << "Unimplemented compare type " << in_type;
2196 }
2197}
2198
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002199void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002201 switch (instruction->InputAt(0)->GetType()) {
2202 default:
2203 case Primitive::kPrimLong:
2204 locations->SetInAt(0, Location::RequiresRegister());
2205 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2206 break;
2207
2208 case Primitive::kPrimFloat:
2209 case Primitive::kPrimDouble:
2210 locations->SetInAt(0, Location::RequiresFpuRegister());
2211 locations->SetInAt(1, Location::RequiresFpuRegister());
2212 break;
2213 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002214 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2216 }
2217}
2218
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002219void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002220 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002221 return;
2222 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002223
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002224 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002225 LocationSummary* locations = instruction->GetLocations();
2226 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002227 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002228
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002229 switch (type) {
2230 default:
2231 // Integer case.
2232 GenerateIntCompare(instruction->GetCondition(), locations);
2233 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002234
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002235 case Primitive::kPrimLong:
2236 // TODO: don't use branches.
2237 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002238 break;
2239
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 case Primitive::kPrimFloat:
2241 case Primitive::kPrimDouble:
2242 // TODO: don't use branches.
2243 GenerateFpCompareAndBranch(instruction->GetCondition(),
2244 instruction->IsGtBias(),
2245 type,
2246 locations,
2247 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248 break;
2249 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002250
2251 // Convert the branches into the result.
2252 MipsLabel done;
2253
2254 // False case: result = 0.
2255 __ LoadConst32(dst, 0);
2256 __ B(&done);
2257
2258 // True case: result = 1.
2259 __ Bind(&true_label);
2260 __ LoadConst32(dst, 1);
2261 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262}
2263
Alexey Frunze7e99e052015-11-24 19:28:01 -08002264void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2265 DCHECK(instruction->IsDiv() || instruction->IsRem());
2266 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2267
2268 LocationSummary* locations = instruction->GetLocations();
2269 Location second = locations->InAt(1);
2270 DCHECK(second.IsConstant());
2271
2272 Register out = locations->Out().AsRegister<Register>();
2273 Register dividend = locations->InAt(0).AsRegister<Register>();
2274 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2275 DCHECK(imm == 1 || imm == -1);
2276
2277 if (instruction->IsRem()) {
2278 __ Move(out, ZERO);
2279 } else {
2280 if (imm == -1) {
2281 __ Subu(out, ZERO, dividend);
2282 } else if (out != dividend) {
2283 __ Move(out, dividend);
2284 }
2285 }
2286}
2287
2288void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2289 DCHECK(instruction->IsDiv() || instruction->IsRem());
2290 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2291
2292 LocationSummary* locations = instruction->GetLocations();
2293 Location second = locations->InAt(1);
2294 DCHECK(second.IsConstant());
2295
2296 Register out = locations->Out().AsRegister<Register>();
2297 Register dividend = locations->InAt(0).AsRegister<Register>();
2298 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002299 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002300 int ctz_imm = CTZ(abs_imm);
2301
2302 if (instruction->IsDiv()) {
2303 if (ctz_imm == 1) {
2304 // Fast path for division by +/-2, which is very common.
2305 __ Srl(TMP, dividend, 31);
2306 } else {
2307 __ Sra(TMP, dividend, 31);
2308 __ Srl(TMP, TMP, 32 - ctz_imm);
2309 }
2310 __ Addu(out, dividend, TMP);
2311 __ Sra(out, out, ctz_imm);
2312 if (imm < 0) {
2313 __ Subu(out, ZERO, out);
2314 }
2315 } else {
2316 if (ctz_imm == 1) {
2317 // Fast path for modulo +/-2, which is very common.
2318 __ Sra(TMP, dividend, 31);
2319 __ Subu(out, dividend, TMP);
2320 __ Andi(out, out, 1);
2321 __ Addu(out, out, TMP);
2322 } else {
2323 __ Sra(TMP, dividend, 31);
2324 __ Srl(TMP, TMP, 32 - ctz_imm);
2325 __ Addu(out, dividend, TMP);
2326 if (IsUint<16>(abs_imm - 1)) {
2327 __ Andi(out, out, abs_imm - 1);
2328 } else {
2329 __ Sll(out, out, 32 - ctz_imm);
2330 __ Srl(out, out, 32 - ctz_imm);
2331 }
2332 __ Subu(out, out, TMP);
2333 }
2334 }
2335}
2336
2337void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2338 DCHECK(instruction->IsDiv() || instruction->IsRem());
2339 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2340
2341 LocationSummary* locations = instruction->GetLocations();
2342 Location second = locations->InAt(1);
2343 DCHECK(second.IsConstant());
2344
2345 Register out = locations->Out().AsRegister<Register>();
2346 Register dividend = locations->InAt(0).AsRegister<Register>();
2347 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2348
2349 int64_t magic;
2350 int shift;
2351 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2352
2353 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2354
2355 __ LoadConst32(TMP, magic);
2356 if (isR6) {
2357 __ MuhR6(TMP, dividend, TMP);
2358 } else {
2359 __ MultR2(dividend, TMP);
2360 __ Mfhi(TMP);
2361 }
2362 if (imm > 0 && magic < 0) {
2363 __ Addu(TMP, TMP, dividend);
2364 } else if (imm < 0 && magic > 0) {
2365 __ Subu(TMP, TMP, dividend);
2366 }
2367
2368 if (shift != 0) {
2369 __ Sra(TMP, TMP, shift);
2370 }
2371
2372 if (instruction->IsDiv()) {
2373 __ Sra(out, TMP, 31);
2374 __ Subu(out, TMP, out);
2375 } else {
2376 __ Sra(AT, TMP, 31);
2377 __ Subu(AT, TMP, AT);
2378 __ LoadConst32(TMP, imm);
2379 if (isR6) {
2380 __ MulR6(TMP, AT, TMP);
2381 } else {
2382 __ MulR2(TMP, AT, TMP);
2383 }
2384 __ Subu(out, dividend, TMP);
2385 }
2386}
2387
2388void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2389 DCHECK(instruction->IsDiv() || instruction->IsRem());
2390 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2391
2392 LocationSummary* locations = instruction->GetLocations();
2393 Register out = locations->Out().AsRegister<Register>();
2394 Location second = locations->InAt(1);
2395
2396 if (second.IsConstant()) {
2397 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2398 if (imm == 0) {
2399 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2400 } else if (imm == 1 || imm == -1) {
2401 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002402 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002403 DivRemByPowerOfTwo(instruction);
2404 } else {
2405 DCHECK(imm <= -2 || imm >= 2);
2406 GenerateDivRemWithAnyConstant(instruction);
2407 }
2408 } else {
2409 Register dividend = locations->InAt(0).AsRegister<Register>();
2410 Register divisor = second.AsRegister<Register>();
2411 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2412 if (instruction->IsDiv()) {
2413 if (isR6) {
2414 __ DivR6(out, dividend, divisor);
2415 } else {
2416 __ DivR2(out, dividend, divisor);
2417 }
2418 } else {
2419 if (isR6) {
2420 __ ModR6(out, dividend, divisor);
2421 } else {
2422 __ ModR2(out, dividend, divisor);
2423 }
2424 }
2425 }
2426}
2427
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2429 Primitive::Type type = div->GetResultType();
2430 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2431 ? LocationSummary::kCall
2432 : LocationSummary::kNoCall;
2433
2434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2435
2436 switch (type) {
2437 case Primitive::kPrimInt:
2438 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002439 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2441 break;
2442
2443 case Primitive::kPrimLong: {
2444 InvokeRuntimeCallingConvention calling_convention;
2445 locations->SetInAt(0, Location::RegisterPairLocation(
2446 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2447 locations->SetInAt(1, Location::RegisterPairLocation(
2448 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2449 locations->SetOut(calling_convention.GetReturnLocation(type));
2450 break;
2451 }
2452
2453 case Primitive::kPrimFloat:
2454 case Primitive::kPrimDouble:
2455 locations->SetInAt(0, Location::RequiresFpuRegister());
2456 locations->SetInAt(1, Location::RequiresFpuRegister());
2457 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2458 break;
2459
2460 default:
2461 LOG(FATAL) << "Unexpected div type " << type;
2462 }
2463}
2464
2465void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2466 Primitive::Type type = instruction->GetType();
2467 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002468
2469 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002470 case Primitive::kPrimInt:
2471 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002472 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002473 case Primitive::kPrimLong: {
2474 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2475 instruction,
2476 instruction->GetDexPc(),
2477 nullptr,
2478 IsDirectEntrypoint(kQuickLdiv));
2479 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2480 break;
2481 }
2482 case Primitive::kPrimFloat:
2483 case Primitive::kPrimDouble: {
2484 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2485 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2486 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2487 if (type == Primitive::kPrimFloat) {
2488 __ DivS(dst, lhs, rhs);
2489 } else {
2490 __ DivD(dst, lhs, rhs);
2491 }
2492 break;
2493 }
2494 default:
2495 LOG(FATAL) << "Unexpected div type " << type;
2496 }
2497}
2498
2499void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2500 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2501 ? LocationSummary::kCallOnSlowPath
2502 : LocationSummary::kNoCall;
2503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2504 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2505 if (instruction->HasUses()) {
2506 locations->SetOut(Location::SameAsFirstInput());
2507 }
2508}
2509
2510void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2511 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2512 codegen_->AddSlowPath(slow_path);
2513 Location value = instruction->GetLocations()->InAt(0);
2514 Primitive::Type type = instruction->GetType();
2515
2516 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002517 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518 case Primitive::kPrimByte:
2519 case Primitive::kPrimChar:
2520 case Primitive::kPrimShort:
2521 case Primitive::kPrimInt: {
2522 if (value.IsConstant()) {
2523 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2524 __ B(slow_path->GetEntryLabel());
2525 } else {
2526 // A division by a non-null constant is valid. We don't need to perform
2527 // any check, so simply fall through.
2528 }
2529 } else {
2530 DCHECK(value.IsRegister()) << value;
2531 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2532 }
2533 break;
2534 }
2535 case Primitive::kPrimLong: {
2536 if (value.IsConstant()) {
2537 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2538 __ B(slow_path->GetEntryLabel());
2539 } else {
2540 // A division by a non-null constant is valid. We don't need to perform
2541 // any check, so simply fall through.
2542 }
2543 } else {
2544 DCHECK(value.IsRegisterPair()) << value;
2545 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2546 __ Beqz(TMP, slow_path->GetEntryLabel());
2547 }
2548 break;
2549 }
2550 default:
2551 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2552 }
2553}
2554
2555void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2556 LocationSummary* locations =
2557 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2558 locations->SetOut(Location::ConstantLocation(constant));
2559}
2560
2561void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2562 // Will be generated at use site.
2563}
2564
2565void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2566 exit->SetLocations(nullptr);
2567}
2568
2569void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2570}
2571
2572void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2573 LocationSummary* locations =
2574 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2575 locations->SetOut(Location::ConstantLocation(constant));
2576}
2577
2578void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2579 // Will be generated at use site.
2580}
2581
2582void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2583 got->SetLocations(nullptr);
2584}
2585
2586void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2587 DCHECK(!successor->IsExitBlock());
2588 HBasicBlock* block = got->GetBlock();
2589 HInstruction* previous = got->GetPrevious();
2590 HLoopInformation* info = block->GetLoopInformation();
2591
2592 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2593 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2594 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2595 return;
2596 }
2597 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2598 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2599 }
2600 if (!codegen_->GoesToNextBlock(block, successor)) {
2601 __ B(codegen_->GetLabelOf(successor));
2602 }
2603}
2604
2605void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2606 HandleGoto(got, got->GetSuccessor());
2607}
2608
2609void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2610 try_boundary->SetLocations(nullptr);
2611}
2612
2613void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2614 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2615 if (!successor->IsExitBlock()) {
2616 HandleGoto(try_boundary, successor);
2617 }
2618}
2619
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002620void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2621 LocationSummary* locations) {
2622 Register dst = locations->Out().AsRegister<Register>();
2623 Register lhs = locations->InAt(0).AsRegister<Register>();
2624 Location rhs_location = locations->InAt(1);
2625 Register rhs_reg = ZERO;
2626 int64_t rhs_imm = 0;
2627 bool use_imm = rhs_location.IsConstant();
2628 if (use_imm) {
2629 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2630 } else {
2631 rhs_reg = rhs_location.AsRegister<Register>();
2632 }
2633
2634 switch (cond) {
2635 case kCondEQ:
2636 case kCondNE:
2637 if (use_imm && IsUint<16>(rhs_imm)) {
2638 __ Xori(dst, lhs, rhs_imm);
2639 } else {
2640 if (use_imm) {
2641 rhs_reg = TMP;
2642 __ LoadConst32(rhs_reg, rhs_imm);
2643 }
2644 __ Xor(dst, lhs, rhs_reg);
2645 }
2646 if (cond == kCondEQ) {
2647 __ Sltiu(dst, dst, 1);
2648 } else {
2649 __ Sltu(dst, ZERO, dst);
2650 }
2651 break;
2652
2653 case kCondLT:
2654 case kCondGE:
2655 if (use_imm && IsInt<16>(rhs_imm)) {
2656 __ Slti(dst, lhs, rhs_imm);
2657 } else {
2658 if (use_imm) {
2659 rhs_reg = TMP;
2660 __ LoadConst32(rhs_reg, rhs_imm);
2661 }
2662 __ Slt(dst, lhs, rhs_reg);
2663 }
2664 if (cond == kCondGE) {
2665 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2666 // only the slt instruction but no sge.
2667 __ Xori(dst, dst, 1);
2668 }
2669 break;
2670
2671 case kCondLE:
2672 case kCondGT:
2673 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2674 // Simulate lhs <= rhs via lhs < rhs + 1.
2675 __ Slti(dst, lhs, rhs_imm + 1);
2676 if (cond == kCondGT) {
2677 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2678 // only the slti instruction but no sgti.
2679 __ Xori(dst, dst, 1);
2680 }
2681 } else {
2682 if (use_imm) {
2683 rhs_reg = TMP;
2684 __ LoadConst32(rhs_reg, rhs_imm);
2685 }
2686 __ Slt(dst, rhs_reg, lhs);
2687 if (cond == kCondLE) {
2688 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2689 // only the slt instruction but no sle.
2690 __ Xori(dst, dst, 1);
2691 }
2692 }
2693 break;
2694
2695 case kCondB:
2696 case kCondAE:
2697 if (use_imm && IsInt<16>(rhs_imm)) {
2698 // Sltiu sign-extends its 16-bit immediate operand before
2699 // the comparison and thus lets us compare directly with
2700 // unsigned values in the ranges [0, 0x7fff] and
2701 // [0xffff8000, 0xffffffff].
2702 __ Sltiu(dst, lhs, rhs_imm);
2703 } else {
2704 if (use_imm) {
2705 rhs_reg = TMP;
2706 __ LoadConst32(rhs_reg, rhs_imm);
2707 }
2708 __ Sltu(dst, lhs, rhs_reg);
2709 }
2710 if (cond == kCondAE) {
2711 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2712 // only the sltu instruction but no sgeu.
2713 __ Xori(dst, dst, 1);
2714 }
2715 break;
2716
2717 case kCondBE:
2718 case kCondA:
2719 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2720 // Simulate lhs <= rhs via lhs < rhs + 1.
2721 // Note that this only works if rhs + 1 does not overflow
2722 // to 0, hence the check above.
2723 // Sltiu sign-extends its 16-bit immediate operand before
2724 // the comparison and thus lets us compare directly with
2725 // unsigned values in the ranges [0, 0x7fff] and
2726 // [0xffff8000, 0xffffffff].
2727 __ Sltiu(dst, lhs, rhs_imm + 1);
2728 if (cond == kCondA) {
2729 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2730 // only the sltiu instruction but no sgtiu.
2731 __ Xori(dst, dst, 1);
2732 }
2733 } else {
2734 if (use_imm) {
2735 rhs_reg = TMP;
2736 __ LoadConst32(rhs_reg, rhs_imm);
2737 }
2738 __ Sltu(dst, rhs_reg, lhs);
2739 if (cond == kCondBE) {
2740 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2741 // only the sltu instruction but no sleu.
2742 __ Xori(dst, dst, 1);
2743 }
2744 }
2745 break;
2746 }
2747}
2748
2749void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2750 LocationSummary* locations,
2751 MipsLabel* label) {
2752 Register lhs = locations->InAt(0).AsRegister<Register>();
2753 Location rhs_location = locations->InAt(1);
2754 Register rhs_reg = ZERO;
2755 int32_t rhs_imm = 0;
2756 bool use_imm = rhs_location.IsConstant();
2757 if (use_imm) {
2758 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2759 } else {
2760 rhs_reg = rhs_location.AsRegister<Register>();
2761 }
2762
2763 if (use_imm && rhs_imm == 0) {
2764 switch (cond) {
2765 case kCondEQ:
2766 case kCondBE: // <= 0 if zero
2767 __ Beqz(lhs, label);
2768 break;
2769 case kCondNE:
2770 case kCondA: // > 0 if non-zero
2771 __ Bnez(lhs, label);
2772 break;
2773 case kCondLT:
2774 __ Bltz(lhs, label);
2775 break;
2776 case kCondGE:
2777 __ Bgez(lhs, label);
2778 break;
2779 case kCondLE:
2780 __ Blez(lhs, label);
2781 break;
2782 case kCondGT:
2783 __ Bgtz(lhs, label);
2784 break;
2785 case kCondB: // always false
2786 break;
2787 case kCondAE: // always true
2788 __ B(label);
2789 break;
2790 }
2791 } else {
2792 if (use_imm) {
2793 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2794 rhs_reg = TMP;
2795 __ LoadConst32(rhs_reg, rhs_imm);
2796 }
2797 switch (cond) {
2798 case kCondEQ:
2799 __ Beq(lhs, rhs_reg, label);
2800 break;
2801 case kCondNE:
2802 __ Bne(lhs, rhs_reg, label);
2803 break;
2804 case kCondLT:
2805 __ Blt(lhs, rhs_reg, label);
2806 break;
2807 case kCondGE:
2808 __ Bge(lhs, rhs_reg, label);
2809 break;
2810 case kCondLE:
2811 __ Bge(rhs_reg, lhs, label);
2812 break;
2813 case kCondGT:
2814 __ Blt(rhs_reg, lhs, label);
2815 break;
2816 case kCondB:
2817 __ Bltu(lhs, rhs_reg, label);
2818 break;
2819 case kCondAE:
2820 __ Bgeu(lhs, rhs_reg, label);
2821 break;
2822 case kCondBE:
2823 __ Bgeu(rhs_reg, lhs, label);
2824 break;
2825 case kCondA:
2826 __ Bltu(rhs_reg, lhs, label);
2827 break;
2828 }
2829 }
2830}
2831
2832void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2833 LocationSummary* locations,
2834 MipsLabel* label) {
2835 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2836 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2837 Location rhs_location = locations->InAt(1);
2838 Register rhs_high = ZERO;
2839 Register rhs_low = ZERO;
2840 int64_t imm = 0;
2841 uint32_t imm_high = 0;
2842 uint32_t imm_low = 0;
2843 bool use_imm = rhs_location.IsConstant();
2844 if (use_imm) {
2845 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2846 imm_high = High32Bits(imm);
2847 imm_low = Low32Bits(imm);
2848 } else {
2849 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2850 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2851 }
2852
2853 if (use_imm && imm == 0) {
2854 switch (cond) {
2855 case kCondEQ:
2856 case kCondBE: // <= 0 if zero
2857 __ Or(TMP, lhs_high, lhs_low);
2858 __ Beqz(TMP, label);
2859 break;
2860 case kCondNE:
2861 case kCondA: // > 0 if non-zero
2862 __ Or(TMP, lhs_high, lhs_low);
2863 __ Bnez(TMP, label);
2864 break;
2865 case kCondLT:
2866 __ Bltz(lhs_high, label);
2867 break;
2868 case kCondGE:
2869 __ Bgez(lhs_high, label);
2870 break;
2871 case kCondLE:
2872 __ Or(TMP, lhs_high, lhs_low);
2873 __ Sra(AT, lhs_high, 31);
2874 __ Bgeu(AT, TMP, label);
2875 break;
2876 case kCondGT:
2877 __ Or(TMP, lhs_high, lhs_low);
2878 __ Sra(AT, lhs_high, 31);
2879 __ Bltu(AT, TMP, label);
2880 break;
2881 case kCondB: // always false
2882 break;
2883 case kCondAE: // always true
2884 __ B(label);
2885 break;
2886 }
2887 } else if (use_imm) {
2888 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2889 switch (cond) {
2890 case kCondEQ:
2891 __ LoadConst32(TMP, imm_high);
2892 __ Xor(TMP, TMP, lhs_high);
2893 __ LoadConst32(AT, imm_low);
2894 __ Xor(AT, AT, lhs_low);
2895 __ Or(TMP, TMP, AT);
2896 __ Beqz(TMP, label);
2897 break;
2898 case kCondNE:
2899 __ LoadConst32(TMP, imm_high);
2900 __ Xor(TMP, TMP, lhs_high);
2901 __ LoadConst32(AT, imm_low);
2902 __ Xor(AT, AT, lhs_low);
2903 __ Or(TMP, TMP, AT);
2904 __ Bnez(TMP, label);
2905 break;
2906 case kCondLT:
2907 __ LoadConst32(TMP, imm_high);
2908 __ Blt(lhs_high, TMP, label);
2909 __ Slt(TMP, TMP, lhs_high);
2910 __ LoadConst32(AT, imm_low);
2911 __ Sltu(AT, lhs_low, AT);
2912 __ Blt(TMP, AT, label);
2913 break;
2914 case kCondGE:
2915 __ LoadConst32(TMP, imm_high);
2916 __ Blt(TMP, lhs_high, label);
2917 __ Slt(TMP, lhs_high, TMP);
2918 __ LoadConst32(AT, imm_low);
2919 __ Sltu(AT, lhs_low, AT);
2920 __ Or(TMP, TMP, AT);
2921 __ Beqz(TMP, label);
2922 break;
2923 case kCondLE:
2924 __ LoadConst32(TMP, imm_high);
2925 __ Blt(lhs_high, TMP, label);
2926 __ Slt(TMP, TMP, lhs_high);
2927 __ LoadConst32(AT, imm_low);
2928 __ Sltu(AT, AT, lhs_low);
2929 __ Or(TMP, TMP, AT);
2930 __ Beqz(TMP, label);
2931 break;
2932 case kCondGT:
2933 __ LoadConst32(TMP, imm_high);
2934 __ Blt(TMP, lhs_high, label);
2935 __ Slt(TMP, lhs_high, TMP);
2936 __ LoadConst32(AT, imm_low);
2937 __ Sltu(AT, AT, lhs_low);
2938 __ Blt(TMP, AT, label);
2939 break;
2940 case kCondB:
2941 __ LoadConst32(TMP, imm_high);
2942 __ Bltu(lhs_high, TMP, label);
2943 __ Sltu(TMP, TMP, lhs_high);
2944 __ LoadConst32(AT, imm_low);
2945 __ Sltu(AT, lhs_low, AT);
2946 __ Blt(TMP, AT, label);
2947 break;
2948 case kCondAE:
2949 __ LoadConst32(TMP, imm_high);
2950 __ Bltu(TMP, lhs_high, label);
2951 __ Sltu(TMP, lhs_high, TMP);
2952 __ LoadConst32(AT, imm_low);
2953 __ Sltu(AT, lhs_low, AT);
2954 __ Or(TMP, TMP, AT);
2955 __ Beqz(TMP, label);
2956 break;
2957 case kCondBE:
2958 __ LoadConst32(TMP, imm_high);
2959 __ Bltu(lhs_high, TMP, label);
2960 __ Sltu(TMP, TMP, lhs_high);
2961 __ LoadConst32(AT, imm_low);
2962 __ Sltu(AT, AT, lhs_low);
2963 __ Or(TMP, TMP, AT);
2964 __ Beqz(TMP, label);
2965 break;
2966 case kCondA:
2967 __ LoadConst32(TMP, imm_high);
2968 __ Bltu(TMP, lhs_high, label);
2969 __ Sltu(TMP, lhs_high, TMP);
2970 __ LoadConst32(AT, imm_low);
2971 __ Sltu(AT, AT, lhs_low);
2972 __ Blt(TMP, AT, label);
2973 break;
2974 }
2975 } else {
2976 switch (cond) {
2977 case kCondEQ:
2978 __ Xor(TMP, lhs_high, rhs_high);
2979 __ Xor(AT, lhs_low, rhs_low);
2980 __ Or(TMP, TMP, AT);
2981 __ Beqz(TMP, label);
2982 break;
2983 case kCondNE:
2984 __ Xor(TMP, lhs_high, rhs_high);
2985 __ Xor(AT, lhs_low, rhs_low);
2986 __ Or(TMP, TMP, AT);
2987 __ Bnez(TMP, label);
2988 break;
2989 case kCondLT:
2990 __ Blt(lhs_high, rhs_high, label);
2991 __ Slt(TMP, rhs_high, lhs_high);
2992 __ Sltu(AT, lhs_low, rhs_low);
2993 __ Blt(TMP, AT, label);
2994 break;
2995 case kCondGE:
2996 __ Blt(rhs_high, lhs_high, label);
2997 __ Slt(TMP, lhs_high, rhs_high);
2998 __ Sltu(AT, lhs_low, rhs_low);
2999 __ Or(TMP, TMP, AT);
3000 __ Beqz(TMP, label);
3001 break;
3002 case kCondLE:
3003 __ Blt(lhs_high, rhs_high, label);
3004 __ Slt(TMP, rhs_high, lhs_high);
3005 __ Sltu(AT, rhs_low, lhs_low);
3006 __ Or(TMP, TMP, AT);
3007 __ Beqz(TMP, label);
3008 break;
3009 case kCondGT:
3010 __ Blt(rhs_high, lhs_high, label);
3011 __ Slt(TMP, lhs_high, rhs_high);
3012 __ Sltu(AT, rhs_low, lhs_low);
3013 __ Blt(TMP, AT, label);
3014 break;
3015 case kCondB:
3016 __ Bltu(lhs_high, rhs_high, label);
3017 __ Sltu(TMP, rhs_high, lhs_high);
3018 __ Sltu(AT, lhs_low, rhs_low);
3019 __ Blt(TMP, AT, label);
3020 break;
3021 case kCondAE:
3022 __ Bltu(rhs_high, lhs_high, label);
3023 __ Sltu(TMP, lhs_high, rhs_high);
3024 __ Sltu(AT, lhs_low, rhs_low);
3025 __ Or(TMP, TMP, AT);
3026 __ Beqz(TMP, label);
3027 break;
3028 case kCondBE:
3029 __ Bltu(lhs_high, rhs_high, label);
3030 __ Sltu(TMP, rhs_high, lhs_high);
3031 __ Sltu(AT, rhs_low, lhs_low);
3032 __ Or(TMP, TMP, AT);
3033 __ Beqz(TMP, label);
3034 break;
3035 case kCondA:
3036 __ Bltu(rhs_high, lhs_high, label);
3037 __ Sltu(TMP, lhs_high, rhs_high);
3038 __ Sltu(AT, rhs_low, lhs_low);
3039 __ Blt(TMP, AT, label);
3040 break;
3041 }
3042 }
3043}
3044
3045void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3046 bool gt_bias,
3047 Primitive::Type type,
3048 LocationSummary* locations,
3049 MipsLabel* label) {
3050 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3051 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3052 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3053 if (type == Primitive::kPrimFloat) {
3054 if (isR6) {
3055 switch (cond) {
3056 case kCondEQ:
3057 __ CmpEqS(FTMP, lhs, rhs);
3058 __ Bc1nez(FTMP, label);
3059 break;
3060 case kCondNE:
3061 __ CmpEqS(FTMP, lhs, rhs);
3062 __ Bc1eqz(FTMP, label);
3063 break;
3064 case kCondLT:
3065 if (gt_bias) {
3066 __ CmpLtS(FTMP, lhs, rhs);
3067 } else {
3068 __ CmpUltS(FTMP, lhs, rhs);
3069 }
3070 __ Bc1nez(FTMP, label);
3071 break;
3072 case kCondLE:
3073 if (gt_bias) {
3074 __ CmpLeS(FTMP, lhs, rhs);
3075 } else {
3076 __ CmpUleS(FTMP, lhs, rhs);
3077 }
3078 __ Bc1nez(FTMP, label);
3079 break;
3080 case kCondGT:
3081 if (gt_bias) {
3082 __ CmpUltS(FTMP, rhs, lhs);
3083 } else {
3084 __ CmpLtS(FTMP, rhs, lhs);
3085 }
3086 __ Bc1nez(FTMP, label);
3087 break;
3088 case kCondGE:
3089 if (gt_bias) {
3090 __ CmpUleS(FTMP, rhs, lhs);
3091 } else {
3092 __ CmpLeS(FTMP, rhs, lhs);
3093 }
3094 __ Bc1nez(FTMP, label);
3095 break;
3096 default:
3097 LOG(FATAL) << "Unexpected non-floating-point condition";
3098 }
3099 } else {
3100 switch (cond) {
3101 case kCondEQ:
3102 __ CeqS(0, lhs, rhs);
3103 __ Bc1t(0, label);
3104 break;
3105 case kCondNE:
3106 __ CeqS(0, lhs, rhs);
3107 __ Bc1f(0, label);
3108 break;
3109 case kCondLT:
3110 if (gt_bias) {
3111 __ ColtS(0, lhs, rhs);
3112 } else {
3113 __ CultS(0, lhs, rhs);
3114 }
3115 __ Bc1t(0, label);
3116 break;
3117 case kCondLE:
3118 if (gt_bias) {
3119 __ ColeS(0, lhs, rhs);
3120 } else {
3121 __ CuleS(0, lhs, rhs);
3122 }
3123 __ Bc1t(0, label);
3124 break;
3125 case kCondGT:
3126 if (gt_bias) {
3127 __ CultS(0, rhs, lhs);
3128 } else {
3129 __ ColtS(0, rhs, lhs);
3130 }
3131 __ Bc1t(0, label);
3132 break;
3133 case kCondGE:
3134 if (gt_bias) {
3135 __ CuleS(0, rhs, lhs);
3136 } else {
3137 __ ColeS(0, rhs, lhs);
3138 }
3139 __ Bc1t(0, label);
3140 break;
3141 default:
3142 LOG(FATAL) << "Unexpected non-floating-point condition";
3143 }
3144 }
3145 } else {
3146 DCHECK_EQ(type, Primitive::kPrimDouble);
3147 if (isR6) {
3148 switch (cond) {
3149 case kCondEQ:
3150 __ CmpEqD(FTMP, lhs, rhs);
3151 __ Bc1nez(FTMP, label);
3152 break;
3153 case kCondNE:
3154 __ CmpEqD(FTMP, lhs, rhs);
3155 __ Bc1eqz(FTMP, label);
3156 break;
3157 case kCondLT:
3158 if (gt_bias) {
3159 __ CmpLtD(FTMP, lhs, rhs);
3160 } else {
3161 __ CmpUltD(FTMP, lhs, rhs);
3162 }
3163 __ Bc1nez(FTMP, label);
3164 break;
3165 case kCondLE:
3166 if (gt_bias) {
3167 __ CmpLeD(FTMP, lhs, rhs);
3168 } else {
3169 __ CmpUleD(FTMP, lhs, rhs);
3170 }
3171 __ Bc1nez(FTMP, label);
3172 break;
3173 case kCondGT:
3174 if (gt_bias) {
3175 __ CmpUltD(FTMP, rhs, lhs);
3176 } else {
3177 __ CmpLtD(FTMP, rhs, lhs);
3178 }
3179 __ Bc1nez(FTMP, label);
3180 break;
3181 case kCondGE:
3182 if (gt_bias) {
3183 __ CmpUleD(FTMP, rhs, lhs);
3184 } else {
3185 __ CmpLeD(FTMP, rhs, lhs);
3186 }
3187 __ Bc1nez(FTMP, label);
3188 break;
3189 default:
3190 LOG(FATAL) << "Unexpected non-floating-point condition";
3191 }
3192 } else {
3193 switch (cond) {
3194 case kCondEQ:
3195 __ CeqD(0, lhs, rhs);
3196 __ Bc1t(0, label);
3197 break;
3198 case kCondNE:
3199 __ CeqD(0, lhs, rhs);
3200 __ Bc1f(0, label);
3201 break;
3202 case kCondLT:
3203 if (gt_bias) {
3204 __ ColtD(0, lhs, rhs);
3205 } else {
3206 __ CultD(0, lhs, rhs);
3207 }
3208 __ Bc1t(0, label);
3209 break;
3210 case kCondLE:
3211 if (gt_bias) {
3212 __ ColeD(0, lhs, rhs);
3213 } else {
3214 __ CuleD(0, lhs, rhs);
3215 }
3216 __ Bc1t(0, label);
3217 break;
3218 case kCondGT:
3219 if (gt_bias) {
3220 __ CultD(0, rhs, lhs);
3221 } else {
3222 __ ColtD(0, rhs, lhs);
3223 }
3224 __ Bc1t(0, label);
3225 break;
3226 case kCondGE:
3227 if (gt_bias) {
3228 __ CuleD(0, rhs, lhs);
3229 } else {
3230 __ ColeD(0, rhs, lhs);
3231 }
3232 __ Bc1t(0, label);
3233 break;
3234 default:
3235 LOG(FATAL) << "Unexpected non-floating-point condition";
3236 }
3237 }
3238 }
3239}
3240
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003241void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003242 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003243 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003244 MipsLabel* false_target) {
3245 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003246
David Brazdil0debae72015-11-12 18:37:00 +00003247 if (true_target == nullptr && false_target == nullptr) {
3248 // Nothing to do. The code always falls through.
3249 return;
3250 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003251 // Constant condition, statically compared against "true" (integer value 1).
3252 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003253 if (true_target != nullptr) {
3254 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003255 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003256 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003257 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003258 if (false_target != nullptr) {
3259 __ B(false_target);
3260 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003261 }
David Brazdil0debae72015-11-12 18:37:00 +00003262 return;
3263 }
3264
3265 // The following code generates these patterns:
3266 // (1) true_target == nullptr && false_target != nullptr
3267 // - opposite condition true => branch to false_target
3268 // (2) true_target != nullptr && false_target == nullptr
3269 // - condition true => branch to true_target
3270 // (3) true_target != nullptr && false_target != nullptr
3271 // - condition true => branch to true_target
3272 // - branch to false_target
3273 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003274 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003275 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003276 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003277 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003278 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3279 } else {
3280 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003282 } else {
3283 // The condition instruction has not been materialized, use its inputs as
3284 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003285 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003286 Primitive::Type type = condition->InputAt(0)->GetType();
3287 LocationSummary* locations = cond->GetLocations();
3288 IfCondition if_cond = condition->GetCondition();
3289 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003290
David Brazdil0debae72015-11-12 18:37:00 +00003291 if (true_target == nullptr) {
3292 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003293 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003294 }
3295
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003296 switch (type) {
3297 default:
3298 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3299 break;
3300 case Primitive::kPrimLong:
3301 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3302 break;
3303 case Primitive::kPrimFloat:
3304 case Primitive::kPrimDouble:
3305 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3306 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003307 }
3308 }
David Brazdil0debae72015-11-12 18:37:00 +00003309
3310 // If neither branch falls through (case 3), the conditional branch to `true_target`
3311 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3312 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003313 __ B(false_target);
3314 }
3315}
3316
3317void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003319 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003320 locations->SetInAt(0, Location::RequiresRegister());
3321 }
3322}
3323
3324void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003325 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3326 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3327 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3328 nullptr : codegen_->GetLabelOf(true_successor);
3329 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3330 nullptr : codegen_->GetLabelOf(false_successor);
3331 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003332}
3333
3334void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3335 LocationSummary* locations = new (GetGraph()->GetArena())
3336 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003337 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003338 locations->SetInAt(0, Location::RequiresRegister());
3339 }
3340}
3341
3342void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003343 SlowPathCodeMIPS* slow_path =
3344 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003345 GenerateTestAndBranch(deoptimize,
3346 /* condition_input_index */ 0,
3347 slow_path->GetEntryLabel(),
3348 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003349}
3350
David Brazdil74eb1b22015-12-14 11:44:01 +00003351void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3353 if (Primitive::IsFloatingPointType(select->GetType())) {
3354 locations->SetInAt(0, Location::RequiresFpuRegister());
3355 locations->SetInAt(1, Location::RequiresFpuRegister());
3356 } else {
3357 locations->SetInAt(0, Location::RequiresRegister());
3358 locations->SetInAt(1, Location::RequiresRegister());
3359 }
3360 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3361 locations->SetInAt(2, Location::RequiresRegister());
3362 }
3363 locations->SetOut(Location::SameAsFirstInput());
3364}
3365
3366void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3367 LocationSummary* locations = select->GetLocations();
3368 MipsLabel false_target;
3369 GenerateTestAndBranch(select,
3370 /* condition_input_index */ 2,
3371 /* true_target */ nullptr,
3372 &false_target);
3373 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3374 __ Bind(&false_target);
3375}
3376
David Srbecky0cf44932015-12-09 14:09:59 +00003377void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3378 new (GetGraph()->GetArena()) LocationSummary(info);
3379}
3380
David Srbeckyd28f4a02016-03-14 17:14:24 +00003381void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3382 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003383}
3384
3385void CodeGeneratorMIPS::GenerateNop() {
3386 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003387}
3388
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003389void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3390 Primitive::Type field_type = field_info.GetFieldType();
3391 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3392 bool generate_volatile = field_info.IsVolatile() && is_wide;
3393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3394 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3395
3396 locations->SetInAt(0, Location::RequiresRegister());
3397 if (generate_volatile) {
3398 InvokeRuntimeCallingConvention calling_convention;
3399 // need A0 to hold base + offset
3400 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3401 if (field_type == Primitive::kPrimLong) {
3402 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3403 } else {
3404 locations->SetOut(Location::RequiresFpuRegister());
3405 // Need some temp core regs since FP results are returned in core registers
3406 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3407 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3408 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3409 }
3410 } else {
3411 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3412 locations->SetOut(Location::RequiresFpuRegister());
3413 } else {
3414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3415 }
3416 }
3417}
3418
3419void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3420 const FieldInfo& field_info,
3421 uint32_t dex_pc) {
3422 Primitive::Type type = field_info.GetFieldType();
3423 LocationSummary* locations = instruction->GetLocations();
3424 Register obj = locations->InAt(0).AsRegister<Register>();
3425 LoadOperandType load_type = kLoadUnsignedByte;
3426 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003427 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003428
3429 switch (type) {
3430 case Primitive::kPrimBoolean:
3431 load_type = kLoadUnsignedByte;
3432 break;
3433 case Primitive::kPrimByte:
3434 load_type = kLoadSignedByte;
3435 break;
3436 case Primitive::kPrimShort:
3437 load_type = kLoadSignedHalfword;
3438 break;
3439 case Primitive::kPrimChar:
3440 load_type = kLoadUnsignedHalfword;
3441 break;
3442 case Primitive::kPrimInt:
3443 case Primitive::kPrimFloat:
3444 case Primitive::kPrimNot:
3445 load_type = kLoadWord;
3446 break;
3447 case Primitive::kPrimLong:
3448 case Primitive::kPrimDouble:
3449 load_type = kLoadDoubleword;
3450 break;
3451 case Primitive::kPrimVoid:
3452 LOG(FATAL) << "Unreachable type " << type;
3453 UNREACHABLE();
3454 }
3455
3456 if (is_volatile && load_type == kLoadDoubleword) {
3457 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003458 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003459 // Do implicit Null check
3460 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3461 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3462 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3463 instruction,
3464 dex_pc,
3465 nullptr,
3466 IsDirectEntrypoint(kQuickA64Load));
3467 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3468 if (type == Primitive::kPrimDouble) {
3469 // Need to move to FP regs since FP results are returned in core registers.
3470 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3471 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003472 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3473 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003474 }
3475 } else {
3476 if (!Primitive::IsFloatingPointType(type)) {
3477 Register dst;
3478 if (type == Primitive::kPrimLong) {
3479 DCHECK(locations->Out().IsRegisterPair());
3480 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003481 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3482 if (obj == dst) {
3483 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3484 codegen_->MaybeRecordImplicitNullCheck(instruction);
3485 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3486 } else {
3487 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3488 codegen_->MaybeRecordImplicitNullCheck(instruction);
3489 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3490 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003491 } else {
3492 DCHECK(locations->Out().IsRegister());
3493 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003494 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496 } else {
3497 DCHECK(locations->Out().IsFpuRegister());
3498 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3499 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003500 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003501 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003502 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003503 }
3504 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003505 // Longs are handled earlier.
3506 if (type != Primitive::kPrimLong) {
3507 codegen_->MaybeRecordImplicitNullCheck(instruction);
3508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003509 }
3510
3511 if (is_volatile) {
3512 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3513 }
3514}
3515
3516void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3517 Primitive::Type field_type = field_info.GetFieldType();
3518 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3519 bool generate_volatile = field_info.IsVolatile() && is_wide;
3520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3521 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3522
3523 locations->SetInAt(0, Location::RequiresRegister());
3524 if (generate_volatile) {
3525 InvokeRuntimeCallingConvention calling_convention;
3526 // need A0 to hold base + offset
3527 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3528 if (field_type == Primitive::kPrimLong) {
3529 locations->SetInAt(1, Location::RegisterPairLocation(
3530 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3531 } else {
3532 locations->SetInAt(1, Location::RequiresFpuRegister());
3533 // Pass FP parameters in core registers.
3534 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3535 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3536 }
3537 } else {
3538 if (Primitive::IsFloatingPointType(field_type)) {
3539 locations->SetInAt(1, Location::RequiresFpuRegister());
3540 } else {
3541 locations->SetInAt(1, Location::RequiresRegister());
3542 }
3543 }
3544}
3545
3546void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3547 const FieldInfo& field_info,
3548 uint32_t dex_pc) {
3549 Primitive::Type type = field_info.GetFieldType();
3550 LocationSummary* locations = instruction->GetLocations();
3551 Register obj = locations->InAt(0).AsRegister<Register>();
3552 StoreOperandType store_type = kStoreByte;
3553 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003554 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003555
3556 switch (type) {
3557 case Primitive::kPrimBoolean:
3558 case Primitive::kPrimByte:
3559 store_type = kStoreByte;
3560 break;
3561 case Primitive::kPrimShort:
3562 case Primitive::kPrimChar:
3563 store_type = kStoreHalfword;
3564 break;
3565 case Primitive::kPrimInt:
3566 case Primitive::kPrimFloat:
3567 case Primitive::kPrimNot:
3568 store_type = kStoreWord;
3569 break;
3570 case Primitive::kPrimLong:
3571 case Primitive::kPrimDouble:
3572 store_type = kStoreDoubleword;
3573 break;
3574 case Primitive::kPrimVoid:
3575 LOG(FATAL) << "Unreachable type " << type;
3576 UNREACHABLE();
3577 }
3578
3579 if (is_volatile) {
3580 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3581 }
3582
3583 if (is_volatile && store_type == kStoreDoubleword) {
3584 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003585 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003586 // Do implicit Null check.
3587 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3588 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3589 if (type == Primitive::kPrimDouble) {
3590 // Pass FP parameters in core registers.
3591 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3592 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003593 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3594 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 }
3596 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3597 instruction,
3598 dex_pc,
3599 nullptr,
3600 IsDirectEntrypoint(kQuickA64Store));
3601 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3602 } else {
3603 if (!Primitive::IsFloatingPointType(type)) {
3604 Register src;
3605 if (type == Primitive::kPrimLong) {
3606 DCHECK(locations->InAt(1).IsRegisterPair());
3607 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003608 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3609 __ StoreToOffset(kStoreWord, src, obj, offset);
3610 codegen_->MaybeRecordImplicitNullCheck(instruction);
3611 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003612 } else {
3613 DCHECK(locations->InAt(1).IsRegister());
3614 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003615 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003616 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003617 } else {
3618 DCHECK(locations->InAt(1).IsFpuRegister());
3619 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3620 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003621 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003622 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003623 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003624 }
3625 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003626 // Longs are handled earlier.
3627 if (type != Primitive::kPrimLong) {
3628 codegen_->MaybeRecordImplicitNullCheck(instruction);
3629 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630 }
3631
3632 // TODO: memory barriers?
3633 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3634 DCHECK(locations->InAt(1).IsRegister());
3635 Register src = locations->InAt(1).AsRegister<Register>();
3636 codegen_->MarkGCCard(obj, src);
3637 }
3638
3639 if (is_volatile) {
3640 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3641 }
3642}
3643
3644void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3645 HandleFieldGet(instruction, instruction->GetFieldInfo());
3646}
3647
3648void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3649 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3650}
3651
3652void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3653 HandleFieldSet(instruction, instruction->GetFieldInfo());
3654}
3655
3656void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3657 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3658}
3659
3660void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3661 LocationSummary::CallKind call_kind =
3662 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3664 locations->SetInAt(0, Location::RequiresRegister());
3665 locations->SetInAt(1, Location::RequiresRegister());
3666 // The output does overlap inputs.
3667 // Note that TypeCheckSlowPathMIPS uses this register too.
3668 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3669}
3670
3671void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3672 LocationSummary* locations = instruction->GetLocations();
3673 Register obj = locations->InAt(0).AsRegister<Register>();
3674 Register cls = locations->InAt(1).AsRegister<Register>();
3675 Register out = locations->Out().AsRegister<Register>();
3676
3677 MipsLabel done;
3678
3679 // Return 0 if `obj` is null.
3680 // TODO: Avoid this check if we know `obj` is not null.
3681 __ Move(out, ZERO);
3682 __ Beqz(obj, &done);
3683
3684 // Compare the class of `obj` with `cls`.
3685 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3686 if (instruction->IsExactCheck()) {
3687 // Classes must be equal for the instanceof to succeed.
3688 __ Xor(out, out, cls);
3689 __ Sltiu(out, out, 1);
3690 } else {
3691 // If the classes are not equal, we go into a slow path.
3692 DCHECK(locations->OnlyCallsOnSlowPath());
3693 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3694 codegen_->AddSlowPath(slow_path);
3695 __ Bne(out, cls, slow_path->GetEntryLabel());
3696 __ LoadConst32(out, 1);
3697 __ Bind(slow_path->GetExitLabel());
3698 }
3699
3700 __ Bind(&done);
3701}
3702
3703void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3704 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3705 locations->SetOut(Location::ConstantLocation(constant));
3706}
3707
3708void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3709 // Will be generated at use site.
3710}
3711
3712void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3713 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3714 locations->SetOut(Location::ConstantLocation(constant));
3715}
3716
3717void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3718 // Will be generated at use site.
3719}
3720
3721void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3722 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3723 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3724}
3725
3726void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3727 HandleInvoke(invoke);
3728 // The register T0 is required to be used for the hidden argument in
3729 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3730 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3731}
3732
3733void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3734 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3735 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3736 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3737 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
3738 Location receiver = invoke->GetLocations()->InAt(0);
3739 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3740 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3741
3742 // Set the hidden argument.
3743 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3744 invoke->GetDexMethodIndex());
3745
3746 // temp = object->GetClass();
3747 if (receiver.IsStackSlot()) {
3748 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3749 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3750 } else {
3751 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3752 }
3753 codegen_->MaybeRecordImplicitNullCheck(invoke);
3754 // temp = temp->GetImtEntryAt(method_offset);
3755 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3756 // T9 = temp->GetEntryPoint();
3757 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3758 // T9();
3759 __ Jalr(T9);
3760 __ Nop();
3761 DCHECK(!codegen_->IsLeafMethod());
3762 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3763}
3764
3765void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003766 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3767 if (intrinsic.TryDispatch(invoke)) {
3768 return;
3769 }
3770
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003771 HandleInvoke(invoke);
3772}
3773
3774void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003775 // Explicit clinit checks triggered by static invokes must have been pruned by
3776 // art::PrepareForRegisterAllocation.
3777 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003778
Chris Larsen701566a2015-10-27 15:29:13 -07003779 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3780 if (intrinsic.TryDispatch(invoke)) {
3781 return;
3782 }
3783
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003784 HandleInvoke(invoke);
3785}
3786
Chris Larsen701566a2015-10-27 15:29:13 -07003787static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003788 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003789 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3790 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003791 return true;
3792 }
3793 return false;
3794}
3795
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003796HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3797 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3798 // TODO: Implement other kinds.
3799 return HLoadString::LoadKind::kDexCacheViaMethod;
3800}
3801
Vladimir Markodc151b22015-10-15 18:02:30 +01003802HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3803 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3804 MethodReference target_method ATTRIBUTE_UNUSED) {
3805 switch (desired_dispatch_info.method_load_kind) {
3806 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3807 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3808 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3809 return HInvokeStaticOrDirect::DispatchInfo {
3810 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3811 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3812 0u,
3813 0u
3814 };
3815 default:
3816 break;
3817 }
3818 switch (desired_dispatch_info.code_ptr_location) {
3819 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3820 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3821 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3822 return HInvokeStaticOrDirect::DispatchInfo {
3823 desired_dispatch_info.method_load_kind,
3824 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3825 desired_dispatch_info.method_load_data,
3826 0u
3827 };
3828 default:
3829 return desired_dispatch_info;
3830 }
3831}
3832
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3834 // All registers are assumed to be correctly set up per the calling convention.
3835
3836 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3837 switch (invoke->GetMethodLoadKind()) {
3838 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3839 // temp = thread->string_init_entrypoint
3840 __ LoadFromOffset(kLoadWord,
3841 temp.AsRegister<Register>(),
3842 TR,
3843 invoke->GetStringInitOffset());
3844 break;
3845 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003846 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003847 break;
3848 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3849 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3850 break;
3851 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003852 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003853 // TODO: Implement these types.
3854 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3855 LOG(FATAL) << "Unsupported";
3856 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003857 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003858 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003859 Register reg = temp.AsRegister<Register>();
3860 Register method_reg;
3861 if (current_method.IsRegister()) {
3862 method_reg = current_method.AsRegister<Register>();
3863 } else {
3864 // TODO: use the appropriate DCHECK() here if possible.
3865 // DCHECK(invoke->GetLocations()->Intrinsified());
3866 DCHECK(!current_method.IsValid());
3867 method_reg = reg;
3868 __ Lw(reg, SP, kCurrentMethodStackOffset);
3869 }
3870
3871 // temp = temp->dex_cache_resolved_methods_;
3872 __ LoadFromOffset(kLoadWord,
3873 reg,
3874 method_reg,
3875 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003876 // temp = temp[index_in_cache];
3877 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3878 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003879 __ LoadFromOffset(kLoadWord,
3880 reg,
3881 reg,
3882 CodeGenerator::GetCachePointerOffset(index_in_cache));
3883 break;
3884 }
3885 }
3886
3887 switch (invoke->GetCodePtrLocation()) {
3888 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3889 __ Jalr(&frame_entry_label_, T9);
3890 break;
3891 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3892 // LR = invoke->GetDirectCodePtr();
3893 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3894 // LR()
3895 __ Jalr(T9);
3896 __ Nop();
3897 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003898 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003899 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3900 // TODO: Implement these types.
3901 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3902 LOG(FATAL) << "Unsupported";
3903 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003904 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3905 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003906 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 T9,
3908 callee_method.AsRegister<Register>(),
3909 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3910 kMipsWordSize).Int32Value());
3911 // T9()
3912 __ Jalr(T9);
3913 __ Nop();
3914 break;
3915 }
3916 DCHECK(!IsLeafMethod());
3917}
3918
3919void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003920 // Explicit clinit checks triggered by static invokes must have been pruned by
3921 // art::PrepareForRegisterAllocation.
3922 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003923
3924 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3925 return;
3926 }
3927
3928 LocationSummary* locations = invoke->GetLocations();
3929 codegen_->GenerateStaticOrDirectCall(invoke,
3930 locations->HasTemps()
3931 ? locations->GetTemp(0)
3932 : Location::NoLocation());
3933 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3934}
3935
Chris Larsen3acee732015-11-18 13:31:08 -08003936void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003937 LocationSummary* locations = invoke->GetLocations();
3938 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08003939 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003940 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3941 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3942 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3943 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3944
3945 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08003946 DCHECK(receiver.IsRegister());
3947 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3948 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949 // temp = temp->GetMethodAt(method_offset);
3950 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3951 // T9 = temp->GetEntryPoint();
3952 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3953 // T9();
3954 __ Jalr(T9);
3955 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08003956}
3957
3958void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3959 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3960 return;
3961 }
3962
3963 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003964 DCHECK(!codegen_->IsLeafMethod());
3965 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3966}
3967
3968void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003969 InvokeRuntimeCallingConvention calling_convention;
3970 CodeGenerator::CreateLoadClassLocationSummary(
3971 cls,
3972 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3973 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003974}
3975
3976void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3977 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003978 if (cls->NeedsAccessCheck()) {
3979 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3980 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3981 cls,
3982 cls->GetDexPc(),
3983 nullptr,
3984 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003985 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003986 return;
3987 }
3988
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003989 Register out = locations->Out().AsRegister<Register>();
3990 Register current_method = locations->InAt(0).AsRegister<Register>();
3991 if (cls->IsReferrersClass()) {
3992 DCHECK(!cls->CanCallRuntime());
3993 DCHECK(!cls->MustGenerateClinitCheck());
3994 __ LoadFromOffset(kLoadWord, out, current_method,
3995 ArtMethod::DeclaringClassOffset().Int32Value());
3996 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003997 __ LoadFromOffset(kLoadWord, out, current_method,
3998 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3999 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004000
4001 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4002 DCHECK(cls->CanCallRuntime());
4003 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4004 cls,
4005 cls,
4006 cls->GetDexPc(),
4007 cls->MustGenerateClinitCheck());
4008 codegen_->AddSlowPath(slow_path);
4009 if (!cls->IsInDexCache()) {
4010 __ Beqz(out, slow_path->GetEntryLabel());
4011 }
4012 if (cls->MustGenerateClinitCheck()) {
4013 GenerateClassInitializationCheck(slow_path, out);
4014 } else {
4015 __ Bind(slow_path->GetExitLabel());
4016 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004017 }
4018 }
4019}
4020
4021static int32_t GetExceptionTlsOffset() {
4022 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4023}
4024
4025void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4026 LocationSummary* locations =
4027 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4028 locations->SetOut(Location::RequiresRegister());
4029}
4030
4031void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4032 Register out = load->GetLocations()->Out().AsRegister<Register>();
4033 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4034}
4035
4036void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4037 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4038}
4039
4040void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4041 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4042}
4043
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004044void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004045 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4046 ? LocationSummary::kCallOnSlowPath
4047 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004049 locations->SetInAt(0, Location::RequiresRegister());
4050 locations->SetOut(Location::RequiresRegister());
4051}
4052
4053void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054 LocationSummary* locations = load->GetLocations();
4055 Register out = locations->Out().AsRegister<Register>();
4056 Register current_method = locations->InAt(0).AsRegister<Register>();
4057 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4058 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4059 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004060
4061 if (!load->IsInDexCache()) {
4062 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4063 codegen_->AddSlowPath(slow_path);
4064 __ Beqz(out, slow_path->GetEntryLabel());
4065 __ Bind(slow_path->GetExitLabel());
4066 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067}
4068
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4070 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4071 locations->SetOut(Location::ConstantLocation(constant));
4072}
4073
4074void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4075 // Will be generated at use site.
4076}
4077
4078void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4079 LocationSummary* locations =
4080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4081 InvokeRuntimeCallingConvention calling_convention;
4082 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4083}
4084
4085void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4086 if (instruction->IsEnter()) {
4087 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4088 instruction,
4089 instruction->GetDexPc(),
4090 nullptr,
4091 IsDirectEntrypoint(kQuickLockObject));
4092 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4093 } else {
4094 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4095 instruction,
4096 instruction->GetDexPc(),
4097 nullptr,
4098 IsDirectEntrypoint(kQuickUnlockObject));
4099 }
4100 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4101}
4102
4103void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4104 LocationSummary* locations =
4105 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4106 switch (mul->GetResultType()) {
4107 case Primitive::kPrimInt:
4108 case Primitive::kPrimLong:
4109 locations->SetInAt(0, Location::RequiresRegister());
4110 locations->SetInAt(1, Location::RequiresRegister());
4111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4112 break;
4113
4114 case Primitive::kPrimFloat:
4115 case Primitive::kPrimDouble:
4116 locations->SetInAt(0, Location::RequiresFpuRegister());
4117 locations->SetInAt(1, Location::RequiresFpuRegister());
4118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4119 break;
4120
4121 default:
4122 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4123 }
4124}
4125
4126void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4127 Primitive::Type type = instruction->GetType();
4128 LocationSummary* locations = instruction->GetLocations();
4129 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4130
4131 switch (type) {
4132 case Primitive::kPrimInt: {
4133 Register dst = locations->Out().AsRegister<Register>();
4134 Register lhs = locations->InAt(0).AsRegister<Register>();
4135 Register rhs = locations->InAt(1).AsRegister<Register>();
4136
4137 if (isR6) {
4138 __ MulR6(dst, lhs, rhs);
4139 } else {
4140 __ MulR2(dst, lhs, rhs);
4141 }
4142 break;
4143 }
4144 case Primitive::kPrimLong: {
4145 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4146 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4147 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4148 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4149 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4150 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4151
4152 // Extra checks to protect caused by the existance of A1_A2.
4153 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4154 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4155 DCHECK_NE(dst_high, lhs_low);
4156 DCHECK_NE(dst_high, rhs_low);
4157
4158 // A_B * C_D
4159 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4160 // dst_lo: [ low(B*D) ]
4161 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4162
4163 if (isR6) {
4164 __ MulR6(TMP, lhs_high, rhs_low);
4165 __ MulR6(dst_high, lhs_low, rhs_high);
4166 __ Addu(dst_high, dst_high, TMP);
4167 __ MuhuR6(TMP, lhs_low, rhs_low);
4168 __ Addu(dst_high, dst_high, TMP);
4169 __ MulR6(dst_low, lhs_low, rhs_low);
4170 } else {
4171 __ MulR2(TMP, lhs_high, rhs_low);
4172 __ MulR2(dst_high, lhs_low, rhs_high);
4173 __ Addu(dst_high, dst_high, TMP);
4174 __ MultuR2(lhs_low, rhs_low);
4175 __ Mfhi(TMP);
4176 __ Addu(dst_high, dst_high, TMP);
4177 __ Mflo(dst_low);
4178 }
4179 break;
4180 }
4181 case Primitive::kPrimFloat:
4182 case Primitive::kPrimDouble: {
4183 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4184 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4185 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4186 if (type == Primitive::kPrimFloat) {
4187 __ MulS(dst, lhs, rhs);
4188 } else {
4189 __ MulD(dst, lhs, rhs);
4190 }
4191 break;
4192 }
4193 default:
4194 LOG(FATAL) << "Unexpected mul type " << type;
4195 }
4196}
4197
4198void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4199 LocationSummary* locations =
4200 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4201 switch (neg->GetResultType()) {
4202 case Primitive::kPrimInt:
4203 case Primitive::kPrimLong:
4204 locations->SetInAt(0, Location::RequiresRegister());
4205 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4206 break;
4207
4208 case Primitive::kPrimFloat:
4209 case Primitive::kPrimDouble:
4210 locations->SetInAt(0, Location::RequiresFpuRegister());
4211 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4212 break;
4213
4214 default:
4215 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4216 }
4217}
4218
4219void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4220 Primitive::Type type = instruction->GetType();
4221 LocationSummary* locations = instruction->GetLocations();
4222
4223 switch (type) {
4224 case Primitive::kPrimInt: {
4225 Register dst = locations->Out().AsRegister<Register>();
4226 Register src = locations->InAt(0).AsRegister<Register>();
4227 __ Subu(dst, ZERO, src);
4228 break;
4229 }
4230 case Primitive::kPrimLong: {
4231 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4232 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4233 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4234 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4235 __ Subu(dst_low, ZERO, src_low);
4236 __ Sltu(TMP, ZERO, dst_low);
4237 __ Subu(dst_high, ZERO, src_high);
4238 __ Subu(dst_high, dst_high, TMP);
4239 break;
4240 }
4241 case Primitive::kPrimFloat:
4242 case Primitive::kPrimDouble: {
4243 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4244 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4245 if (type == Primitive::kPrimFloat) {
4246 __ NegS(dst, src);
4247 } else {
4248 __ NegD(dst, src);
4249 }
4250 break;
4251 }
4252 default:
4253 LOG(FATAL) << "Unexpected neg type " << type;
4254 }
4255}
4256
4257void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4258 LocationSummary* locations =
4259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4260 InvokeRuntimeCallingConvention calling_convention;
4261 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4262 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4263 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4264 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4265}
4266
4267void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4268 InvokeRuntimeCallingConvention calling_convention;
4269 Register current_method_register = calling_convention.GetRegisterAt(2);
4270 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4271 // Move an uint16_t value to a register.
4272 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4273 codegen_->InvokeRuntime(
4274 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4275 instruction,
4276 instruction->GetDexPc(),
4277 nullptr,
4278 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4279 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4280 void*, uint32_t, int32_t, ArtMethod*>();
4281}
4282
4283void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4284 LocationSummary* locations =
4285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4286 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004287 if (instruction->IsStringAlloc()) {
4288 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4289 } else {
4290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4291 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4292 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004293 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4294}
4295
4296void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004297 if (instruction->IsStringAlloc()) {
4298 // String is allocated through StringFactory. Call NewEmptyString entry point.
4299 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4300 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4301 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4302 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4303 __ Jalr(T9);
4304 __ Nop();
4305 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4306 } else {
4307 codegen_->InvokeRuntime(
4308 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4309 instruction,
4310 instruction->GetDexPc(),
4311 nullptr,
4312 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4313 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4314 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004315}
4316
4317void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4319 locations->SetInAt(0, Location::RequiresRegister());
4320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4321}
4322
4323void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4324 Primitive::Type type = instruction->GetType();
4325 LocationSummary* locations = instruction->GetLocations();
4326
4327 switch (type) {
4328 case Primitive::kPrimInt: {
4329 Register dst = locations->Out().AsRegister<Register>();
4330 Register src = locations->InAt(0).AsRegister<Register>();
4331 __ Nor(dst, src, ZERO);
4332 break;
4333 }
4334
4335 case Primitive::kPrimLong: {
4336 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4337 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4338 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4339 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4340 __ Nor(dst_high, src_high, ZERO);
4341 __ Nor(dst_low, src_low, ZERO);
4342 break;
4343 }
4344
4345 default:
4346 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4347 }
4348}
4349
4350void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4352 locations->SetInAt(0, Location::RequiresRegister());
4353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4354}
4355
4356void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4357 LocationSummary* locations = instruction->GetLocations();
4358 __ Xori(locations->Out().AsRegister<Register>(),
4359 locations->InAt(0).AsRegister<Register>(),
4360 1);
4361}
4362
4363void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4364 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4365 ? LocationSummary::kCallOnSlowPath
4366 : LocationSummary::kNoCall;
4367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4368 locations->SetInAt(0, Location::RequiresRegister());
4369 if (instruction->HasUses()) {
4370 locations->SetOut(Location::SameAsFirstInput());
4371 }
4372}
4373
Calin Juravle2ae48182016-03-16 14:05:09 +00004374void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4375 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004376 return;
4377 }
4378 Location obj = instruction->GetLocations()->InAt(0);
4379
4380 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004381 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004382}
4383
Calin Juravle2ae48182016-03-16 14:05:09 +00004384void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004385 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004386 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004387
4388 Location obj = instruction->GetLocations()->InAt(0);
4389
4390 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4391}
4392
4393void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004394 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004395}
4396
4397void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4398 HandleBinaryOp(instruction);
4399}
4400
4401void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4402 HandleBinaryOp(instruction);
4403}
4404
4405void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4406 LOG(FATAL) << "Unreachable";
4407}
4408
4409void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4410 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4411}
4412
4413void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4415 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4416 if (location.IsStackSlot()) {
4417 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4418 } else if (location.IsDoubleStackSlot()) {
4419 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4420 }
4421 locations->SetOut(location);
4422}
4423
4424void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4425 ATTRIBUTE_UNUSED) {
4426 // Nothing to do, the parameter is already at its location.
4427}
4428
4429void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4430 LocationSummary* locations =
4431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4432 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4433}
4434
4435void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4436 ATTRIBUTE_UNUSED) {
4437 // Nothing to do, the method is already at its location.
4438}
4439
4440void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4442 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4443 locations->SetInAt(i, Location::Any());
4444 }
4445 locations->SetOut(Location::Any());
4446}
4447
4448void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4449 LOG(FATAL) << "Unreachable";
4450}
4451
4452void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4453 Primitive::Type type = rem->GetResultType();
4454 LocationSummary::CallKind call_kind =
4455 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4456 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4457
4458 switch (type) {
4459 case Primitive::kPrimInt:
4460 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004461 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4463 break;
4464
4465 case Primitive::kPrimLong: {
4466 InvokeRuntimeCallingConvention calling_convention;
4467 locations->SetInAt(0, Location::RegisterPairLocation(
4468 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4469 locations->SetInAt(1, Location::RegisterPairLocation(
4470 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4471 locations->SetOut(calling_convention.GetReturnLocation(type));
4472 break;
4473 }
4474
4475 case Primitive::kPrimFloat:
4476 case Primitive::kPrimDouble: {
4477 InvokeRuntimeCallingConvention calling_convention;
4478 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4479 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4480 locations->SetOut(calling_convention.GetReturnLocation(type));
4481 break;
4482 }
4483
4484 default:
4485 LOG(FATAL) << "Unexpected rem type " << type;
4486 }
4487}
4488
4489void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4490 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004491
4492 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004493 case Primitive::kPrimInt:
4494 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004495 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004496 case Primitive::kPrimLong: {
4497 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4498 instruction,
4499 instruction->GetDexPc(),
4500 nullptr,
4501 IsDirectEntrypoint(kQuickLmod));
4502 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4503 break;
4504 }
4505 case Primitive::kPrimFloat: {
4506 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4507 instruction, instruction->GetDexPc(),
4508 nullptr,
4509 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004510 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004511 break;
4512 }
4513 case Primitive::kPrimDouble: {
4514 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4515 instruction, instruction->GetDexPc(),
4516 nullptr,
4517 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004518 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004519 break;
4520 }
4521 default:
4522 LOG(FATAL) << "Unexpected rem type " << type;
4523 }
4524}
4525
4526void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4527 memory_barrier->SetLocations(nullptr);
4528}
4529
4530void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4531 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4532}
4533
4534void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4535 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4536 Primitive::Type return_type = ret->InputAt(0)->GetType();
4537 locations->SetInAt(0, MipsReturnLocation(return_type));
4538}
4539
4540void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4541 codegen_->GenerateFrameExit();
4542}
4543
4544void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4545 ret->SetLocations(nullptr);
4546}
4547
4548void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4549 codegen_->GenerateFrameExit();
4550}
4551
Alexey Frunze92d90602015-12-18 18:16:36 -08004552void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4553 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004554}
4555
Alexey Frunze92d90602015-12-18 18:16:36 -08004556void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4557 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004558}
4559
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004560void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4561 HandleShift(shl);
4562}
4563
4564void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4565 HandleShift(shl);
4566}
4567
4568void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4569 HandleShift(shr);
4570}
4571
4572void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4573 HandleShift(shr);
4574}
4575
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004576void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4577 HandleBinaryOp(instruction);
4578}
4579
4580void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4581 HandleBinaryOp(instruction);
4582}
4583
4584void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4585 HandleFieldGet(instruction, instruction->GetFieldInfo());
4586}
4587
4588void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4589 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4590}
4591
4592void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4593 HandleFieldSet(instruction, instruction->GetFieldInfo());
4594}
4595
4596void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4597 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4598}
4599
4600void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4601 HUnresolvedInstanceFieldGet* instruction) {
4602 FieldAccessCallingConventionMIPS calling_convention;
4603 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4604 instruction->GetFieldType(),
4605 calling_convention);
4606}
4607
4608void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4609 HUnresolvedInstanceFieldGet* instruction) {
4610 FieldAccessCallingConventionMIPS calling_convention;
4611 codegen_->GenerateUnresolvedFieldAccess(instruction,
4612 instruction->GetFieldType(),
4613 instruction->GetFieldIndex(),
4614 instruction->GetDexPc(),
4615 calling_convention);
4616}
4617
4618void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4619 HUnresolvedInstanceFieldSet* instruction) {
4620 FieldAccessCallingConventionMIPS calling_convention;
4621 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4622 instruction->GetFieldType(),
4623 calling_convention);
4624}
4625
4626void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4627 HUnresolvedInstanceFieldSet* instruction) {
4628 FieldAccessCallingConventionMIPS calling_convention;
4629 codegen_->GenerateUnresolvedFieldAccess(instruction,
4630 instruction->GetFieldType(),
4631 instruction->GetFieldIndex(),
4632 instruction->GetDexPc(),
4633 calling_convention);
4634}
4635
4636void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4637 HUnresolvedStaticFieldGet* instruction) {
4638 FieldAccessCallingConventionMIPS calling_convention;
4639 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4640 instruction->GetFieldType(),
4641 calling_convention);
4642}
4643
4644void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4645 HUnresolvedStaticFieldGet* instruction) {
4646 FieldAccessCallingConventionMIPS calling_convention;
4647 codegen_->GenerateUnresolvedFieldAccess(instruction,
4648 instruction->GetFieldType(),
4649 instruction->GetFieldIndex(),
4650 instruction->GetDexPc(),
4651 calling_convention);
4652}
4653
4654void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4655 HUnresolvedStaticFieldSet* instruction) {
4656 FieldAccessCallingConventionMIPS calling_convention;
4657 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4658 instruction->GetFieldType(),
4659 calling_convention);
4660}
4661
4662void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4663 HUnresolvedStaticFieldSet* instruction) {
4664 FieldAccessCallingConventionMIPS calling_convention;
4665 codegen_->GenerateUnresolvedFieldAccess(instruction,
4666 instruction->GetFieldType(),
4667 instruction->GetFieldIndex(),
4668 instruction->GetDexPc(),
4669 calling_convention);
4670}
4671
4672void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4674}
4675
4676void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4677 HBasicBlock* block = instruction->GetBlock();
4678 if (block->GetLoopInformation() != nullptr) {
4679 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4680 // The back edge will generate the suspend check.
4681 return;
4682 }
4683 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4684 // The goto will generate the suspend check.
4685 return;
4686 }
4687 GenerateSuspendCheck(instruction, nullptr);
4688}
4689
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004690void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4691 LocationSummary* locations =
4692 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4693 InvokeRuntimeCallingConvention calling_convention;
4694 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4695}
4696
4697void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4698 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4699 instruction,
4700 instruction->GetDexPc(),
4701 nullptr,
4702 IsDirectEntrypoint(kQuickDeliverException));
4703 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4704}
4705
4706void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4707 Primitive::Type input_type = conversion->GetInputType();
4708 Primitive::Type result_type = conversion->GetResultType();
4709 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004710 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004711
4712 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4713 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4714 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4715 }
4716
4717 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004718 if (!isR6 &&
4719 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4720 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721 call_kind = LocationSummary::kCall;
4722 }
4723
4724 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4725
4726 if (call_kind == LocationSummary::kNoCall) {
4727 if (Primitive::IsFloatingPointType(input_type)) {
4728 locations->SetInAt(0, Location::RequiresFpuRegister());
4729 } else {
4730 locations->SetInAt(0, Location::RequiresRegister());
4731 }
4732
4733 if (Primitive::IsFloatingPointType(result_type)) {
4734 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4735 } else {
4736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4737 }
4738 } else {
4739 InvokeRuntimeCallingConvention calling_convention;
4740
4741 if (Primitive::IsFloatingPointType(input_type)) {
4742 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4743 } else {
4744 DCHECK_EQ(input_type, Primitive::kPrimLong);
4745 locations->SetInAt(0, Location::RegisterPairLocation(
4746 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4747 }
4748
4749 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4750 }
4751}
4752
4753void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4754 LocationSummary* locations = conversion->GetLocations();
4755 Primitive::Type result_type = conversion->GetResultType();
4756 Primitive::Type input_type = conversion->GetInputType();
4757 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004758 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4759 bool fpu_32bit = codegen_->GetInstructionSetFeatures().Is32BitFloatingPoint();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004760
4761 DCHECK_NE(input_type, result_type);
4762
4763 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4764 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4765 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4766 Register src = locations->InAt(0).AsRegister<Register>();
4767
4768 __ Move(dst_low, src);
4769 __ Sra(dst_high, src, 31);
4770 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4771 Register dst = locations->Out().AsRegister<Register>();
4772 Register src = (input_type == Primitive::kPrimLong)
4773 ? locations->InAt(0).AsRegisterPairLow<Register>()
4774 : locations->InAt(0).AsRegister<Register>();
4775
4776 switch (result_type) {
4777 case Primitive::kPrimChar:
4778 __ Andi(dst, src, 0xFFFF);
4779 break;
4780 case Primitive::kPrimByte:
4781 if (has_sign_extension) {
4782 __ Seb(dst, src);
4783 } else {
4784 __ Sll(dst, src, 24);
4785 __ Sra(dst, dst, 24);
4786 }
4787 break;
4788 case Primitive::kPrimShort:
4789 if (has_sign_extension) {
4790 __ Seh(dst, src);
4791 } else {
4792 __ Sll(dst, src, 16);
4793 __ Sra(dst, dst, 16);
4794 }
4795 break;
4796 case Primitive::kPrimInt:
4797 __ Move(dst, src);
4798 break;
4799
4800 default:
4801 LOG(FATAL) << "Unexpected type conversion from " << input_type
4802 << " to " << result_type;
4803 }
4804 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004805 if (input_type == Primitive::kPrimLong) {
4806 if (isR6) {
4807 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4808 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4809 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4810 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4811 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4812 __ Mtc1(src_low, FTMP);
4813 __ Mthc1(src_high, FTMP);
4814 if (result_type == Primitive::kPrimFloat) {
4815 __ Cvtsl(dst, FTMP);
4816 } else {
4817 __ Cvtdl(dst, FTMP);
4818 }
4819 } else {
4820 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4821 : QUICK_ENTRY_POINT(pL2d);
4822 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4823 : IsDirectEntrypoint(kQuickL2d);
4824 codegen_->InvokeRuntime(entry_offset,
4825 conversion,
4826 conversion->GetDexPc(),
4827 nullptr,
4828 direct);
4829 if (result_type == Primitive::kPrimFloat) {
4830 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4831 } else {
4832 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4833 }
4834 }
4835 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004836 Register src = locations->InAt(0).AsRegister<Register>();
4837 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4838 __ Mtc1(src, FTMP);
4839 if (result_type == Primitive::kPrimFloat) {
4840 __ Cvtsw(dst, FTMP);
4841 } else {
4842 __ Cvtdw(dst, FTMP);
4843 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004844 }
4845 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4846 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004847 if (result_type == Primitive::kPrimLong) {
4848 if (isR6) {
4849 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4850 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4851 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4852 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4853 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4854 MipsLabel truncate;
4855 MipsLabel done;
4856
4857 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4858 // value when the input is either a NaN or is outside of the range of the output type
4859 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4860 // the same result.
4861 //
4862 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4863 // value of the output type if the input is outside of the range after the truncation or
4864 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4865 // results. This matches the desired float/double-to-int/long conversion exactly.
4866 //
4867 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4868 //
4869 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4870 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4871 // even though it must be NAN2008=1 on R6.
4872 //
4873 // The code takes care of the different behaviors by first comparing the input to the
4874 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4875 // If the input is greater than or equal to the minimum, it procedes to the truncate
4876 // instruction, which will handle such an input the same way irrespective of NAN2008.
4877 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4878 // in order to return either zero or the minimum value.
4879 //
4880 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4881 // truncate instruction for MIPS64R6.
4882 if (input_type == Primitive::kPrimFloat) {
4883 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4884 __ LoadConst32(TMP, min_val);
4885 __ Mtc1(TMP, FTMP);
4886 __ CmpLeS(FTMP, FTMP, src);
4887 } else {
4888 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4889 __ LoadConst32(TMP, High32Bits(min_val));
4890 __ Mtc1(ZERO, FTMP);
4891 __ Mthc1(TMP, FTMP);
4892 __ CmpLeD(FTMP, FTMP, src);
4893 }
4894
4895 __ Bc1nez(FTMP, &truncate);
4896
4897 if (input_type == Primitive::kPrimFloat) {
4898 __ CmpEqS(FTMP, src, src);
4899 } else {
4900 __ CmpEqD(FTMP, src, src);
4901 }
4902 __ Move(dst_low, ZERO);
4903 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4904 __ Mfc1(TMP, FTMP);
4905 __ And(dst_high, dst_high, TMP);
4906
4907 __ B(&done);
4908
4909 __ Bind(&truncate);
4910
4911 if (input_type == Primitive::kPrimFloat) {
4912 __ TruncLS(FTMP, src);
4913 } else {
4914 __ TruncLD(FTMP, src);
4915 }
4916 __ Mfc1(dst_low, FTMP);
4917 __ Mfhc1(dst_high, FTMP);
4918
4919 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004920 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004921 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4922 : QUICK_ENTRY_POINT(pD2l);
4923 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4924 : IsDirectEntrypoint(kQuickD2l);
4925 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
4926 if (input_type == Primitive::kPrimFloat) {
4927 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4928 } else {
4929 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4930 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004931 }
4932 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004933 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4934 Register dst = locations->Out().AsRegister<Register>();
4935 MipsLabel truncate;
4936 MipsLabel done;
4937
4938 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4939 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4940 // even though it must be NAN2008=1 on R6.
4941 //
4942 // For details see the large comment above for the truncation of float/double to long on R6.
4943 //
4944 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4945 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004946 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004947 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4948 __ LoadConst32(TMP, min_val);
4949 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004950 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004951 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4952 __ LoadConst32(TMP, High32Bits(min_val));
4953 __ Mtc1(ZERO, FTMP);
4954 if (fpu_32bit) {
4955 __ Mtc1(TMP, static_cast<FRegister>(FTMP + 1));
4956 } else {
4957 __ Mthc1(TMP, FTMP);
4958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004960
4961 if (isR6) {
4962 if (input_type == Primitive::kPrimFloat) {
4963 __ CmpLeS(FTMP, FTMP, src);
4964 } else {
4965 __ CmpLeD(FTMP, FTMP, src);
4966 }
4967 __ Bc1nez(FTMP, &truncate);
4968
4969 if (input_type == Primitive::kPrimFloat) {
4970 __ CmpEqS(FTMP, src, src);
4971 } else {
4972 __ CmpEqD(FTMP, src, src);
4973 }
4974 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4975 __ Mfc1(TMP, FTMP);
4976 __ And(dst, dst, TMP);
4977 } else {
4978 if (input_type == Primitive::kPrimFloat) {
4979 __ ColeS(0, FTMP, src);
4980 } else {
4981 __ ColeD(0, FTMP, src);
4982 }
4983 __ Bc1t(0, &truncate);
4984
4985 if (input_type == Primitive::kPrimFloat) {
4986 __ CeqS(0, src, src);
4987 } else {
4988 __ CeqD(0, src, src);
4989 }
4990 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4991 __ Movf(dst, ZERO, 0);
4992 }
4993
4994 __ B(&done);
4995
4996 __ Bind(&truncate);
4997
4998 if (input_type == Primitive::kPrimFloat) {
4999 __ TruncWS(FTMP, src);
5000 } else {
5001 __ TruncWD(FTMP, src);
5002 }
5003 __ Mfc1(dst, FTMP);
5004
5005 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005006 }
5007 } else if (Primitive::IsFloatingPointType(result_type) &&
5008 Primitive::IsFloatingPointType(input_type)) {
5009 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5010 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5011 if (result_type == Primitive::kPrimFloat) {
5012 __ Cvtsd(dst, src);
5013 } else {
5014 __ Cvtds(dst, src);
5015 }
5016 } else {
5017 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5018 << " to " << result_type;
5019 }
5020}
5021
5022void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5023 HandleShift(ushr);
5024}
5025
5026void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5027 HandleShift(ushr);
5028}
5029
5030void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5031 HandleBinaryOp(instruction);
5032}
5033
5034void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5035 HandleBinaryOp(instruction);
5036}
5037
5038void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5039 // Nothing to do, this should be removed during prepare for register allocator.
5040 LOG(FATAL) << "Unreachable";
5041}
5042
5043void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5044 // Nothing to do, this should be removed during prepare for register allocator.
5045 LOG(FATAL) << "Unreachable";
5046}
5047
5048void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005049 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005050}
5051
5052void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005053 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005054}
5055
5056void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005057 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005058}
5059
5060void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005061 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005062}
5063
5064void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005065 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005066}
5067
5068void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005069 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005070}
5071
5072void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005073 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005074}
5075
5076void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005077 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005078}
5079
5080void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005081 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005082}
5083
5084void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005085 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005086}
5087
5088void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005089 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005090}
5091
5092void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005093 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005094}
5095
5096void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005097 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005098}
5099
5100void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005101 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005102}
5103
5104void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005105 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005106}
5107
5108void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005109 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110}
5111
5112void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005113 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005114}
5115
5116void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005117 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005118}
5119
5120void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005121 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005122}
5123
5124void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005125 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005126}
5127
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005128void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5129 LocationSummary* locations =
5130 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5131 locations->SetInAt(0, Location::RequiresRegister());
5132}
5133
5134void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5135 int32_t lower_bound = switch_instr->GetStartValue();
5136 int32_t num_entries = switch_instr->GetNumEntries();
5137 LocationSummary* locations = switch_instr->GetLocations();
5138 Register value_reg = locations->InAt(0).AsRegister<Register>();
5139 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5140
5141 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005142 Register temp_reg = TMP;
5143 __ Addiu32(temp_reg, value_reg, -lower_bound);
5144 // Jump to default if index is negative
5145 // Note: We don't check the case that index is positive while value < lower_bound, because in
5146 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5147 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005149 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005150 // Jump to successors[0] if value == lower_bound.
5151 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5152 int32_t last_index = 0;
5153 for (; num_entries - last_index > 2; last_index += 2) {
5154 __ Addiu(temp_reg, temp_reg, -2);
5155 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5156 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5157 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5158 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5159 }
5160 if (num_entries - last_index == 2) {
5161 // The last missing case_value.
5162 __ Addiu(temp_reg, temp_reg, -1);
5163 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005164 }
5165
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005166 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005167 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5168 __ B(codegen_->GetLabelOf(default_block));
5169 }
5170}
5171
5172void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5173 // The trampoline uses the same calling convention as dex calling conventions,
5174 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5175 // the method_idx.
5176 HandleInvoke(invoke);
5177}
5178
5179void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5180 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5181}
5182
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005183void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5184 LocationSummary* locations =
5185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5186 locations->SetInAt(0, Location::RequiresRegister());
5187 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005188}
5189
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005190void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5191 LocationSummary* locations = instruction->GetLocations();
5192 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005193 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005194 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5195 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5196 } else {
5197 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5198 instruction->GetIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
5199 }
5200 __ LoadFromOffset(kLoadWord,
5201 locations->Out().AsRegister<Register>(),
5202 locations->InAt(0).AsRegister<Register>(),
5203 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005204}
5205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005206#undef __
5207#undef QUICK_ENTRY_POINT
5208
5209} // namespace mips
5210} // namespace art