blob: 810db208889cc24d5fd3040768c9826e8dc0c058 [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);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100169 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
170 ? QUICK_ENTRY_POINT(pThrowStringBounds)
171 : QUICK_ENTRY_POINT(pThrowArrayBounds);
172 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200173 instruction_,
174 instruction_->GetDexPc(),
175 this,
176 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100177 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200178 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
179 }
180
181 bool IsFatal() const OVERRIDE { return true; }
182
183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
184
185 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200186 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
187};
188
189class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
190 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000191 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192
193 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
194 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
195 __ Bind(GetEntryLabel());
196 if (instruction_->CanThrowIntoCatchBlock()) {
197 // Live registers will be restored in the catch block if caught.
198 SaveLiveRegisters(codegen, instruction_->GetLocations());
199 }
200 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
201 instruction_,
202 instruction_->GetDexPc(),
203 this,
204 IsDirectEntrypoint(kQuickThrowDivZero));
205 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
206 }
207
208 bool IsFatal() const OVERRIDE { return true; }
209
210 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
211
212 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200213 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
214};
215
216class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
217 public:
218 LoadClassSlowPathMIPS(HLoadClass* cls,
219 HInstruction* at,
220 uint32_t dex_pc,
221 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200223 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
224 }
225
226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
227 LocationSummary* locations = at_->GetLocations();
228 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
229
230 __ Bind(GetEntryLabel());
231 SaveLiveRegisters(codegen, locations);
232
233 InvokeRuntimeCallingConvention calling_convention;
234 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
235
236 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
237 : QUICK_ENTRY_POINT(pInitializeType);
238 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
239 : IsDirectEntrypoint(kQuickInitializeType);
240
241 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
242 if (do_clinit_) {
243 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
244 } else {
245 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
246 }
247
248 // Move the class to the desired location.
249 Location out = locations->Out();
250 if (out.IsValid()) {
251 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
252 Primitive::Type type = at_->GetType();
253 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
254 }
255
256 RestoreLiveRegisters(codegen, locations);
257 __ B(GetExitLabel());
258 }
259
260 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
261
262 private:
263 // The class this slow path will load.
264 HLoadClass* const cls_;
265
266 // The instruction where this slow path is happening.
267 // (Might be the load class or an initialization check).
268 HInstruction* const at_;
269
270 // The dex PC of `at_`.
271 const uint32_t dex_pc_;
272
273 // Whether to initialize the class.
274 const bool do_clinit_;
275
276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
277};
278
279class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
280 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000281 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282
283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = instruction_->GetLocations();
285 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
286 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
287
288 __ Bind(GetEntryLabel());
289 SaveLiveRegisters(codegen, locations);
290
291 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000292 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
293 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200294 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
295 instruction_,
296 instruction_->GetDexPc(),
297 this,
298 IsDirectEntrypoint(kQuickResolveString));
299 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
300 Primitive::Type type = instruction_->GetType();
301 mips_codegen->MoveLocation(locations->Out(),
302 calling_convention.GetReturnLocation(type),
303 type);
304
305 RestoreLiveRegisters(codegen, locations);
306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
326 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
327 instruction_,
328 instruction_->GetDexPc(),
329 this,
330 IsDirectEntrypoint(kQuickThrowNullPointer));
331 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
332 }
333
334 bool IsFatal() const OVERRIDE { return true; }
335
336 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
337
338 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
340};
341
342class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
343 public:
344 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000345 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
349 __ Bind(GetEntryLabel());
350 SaveLiveRegisters(codegen, instruction_->GetLocations());
351 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
352 instruction_,
353 instruction_->GetDexPc(),
354 this,
355 IsDirectEntrypoint(kQuickTestSuspend));
356 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
357 RestoreLiveRegisters(codegen, instruction_->GetLocations());
358 if (successor_ == nullptr) {
359 __ B(GetReturnLabel());
360 } else {
361 __ B(mips_codegen->GetLabelOf(successor_));
362 }
363 }
364
365 MipsLabel* GetReturnLabel() {
366 DCHECK(successor_ == nullptr);
367 return &return_label_;
368 }
369
370 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
371
372 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200373 // If not null, the block to branch to after the suspend check.
374 HBasicBlock* const successor_;
375
376 // If `successor_` is null, the label to branch to after the suspend check.
377 MipsLabel return_label_;
378
379 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
380};
381
382class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
383 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
388 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
389 uint32_t dex_pc = instruction_->GetDexPc();
390 DCHECK(instruction_->IsCheckCast()
391 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
392 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
393
394 __ Bind(GetEntryLabel());
395 SaveLiveRegisters(codegen, locations);
396
397 // We're moving two locations to locations that could overlap, so we need a parallel
398 // move resolver.
399 InvokeRuntimeCallingConvention calling_convention;
400 codegen->EmitParallelMoves(locations->InAt(1),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
402 Primitive::kPrimNot,
403 object_class,
404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimNot);
406
407 if (instruction_->IsInstanceOf()) {
408 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
409 instruction_,
410 dex_pc,
411 this,
412 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000413 CheckEntrypointTypes<
414 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Primitive::Type ret_type = instruction_->GetType();
416 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
417 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 } else {
419 DCHECK(instruction_->IsCheckCast());
420 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
421 instruction_,
422 dex_pc,
423 this,
424 IsDirectEntrypoint(kQuickCheckCast));
425 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
426 }
427
428 RestoreLiveRegisters(codegen, locations);
429 __ B(GetExitLabel());
430 }
431
432 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
433
434 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200435 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
436};
437
438class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
439 public:
Aart Bik42249c32016-01-07 15:33:50 -0800440 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000441 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800444 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 __ Bind(GetEntryLabel());
446 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
448 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800449 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 this,
451 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000452 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200453 }
454
455 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
456
457 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200458 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
459};
460
461CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
462 const MipsInstructionSetFeatures& isa_features,
463 const CompilerOptions& compiler_options,
464 OptimizingCompilerStats* stats)
465 : CodeGenerator(graph,
466 kNumberOfCoreRegisters,
467 kNumberOfFRegisters,
468 kNumberOfRegisterPairs,
469 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
470 arraysize(kCoreCalleeSaves)),
471 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
472 arraysize(kFpuCalleeSaves)),
473 compiler_options,
474 stats),
475 block_labels_(nullptr),
476 location_builder_(graph, this),
477 instruction_visitor_(graph, this),
478 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100479 assembler_(graph->GetArena(), &isa_features),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200480 isa_features_(isa_features) {
481 // Save RA (containing the return address) to mimic Quick.
482 AddAllocatedRegister(Location::RegisterLocation(RA));
483}
484
485#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700486// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
487#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200488#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
489
490void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
491 // Ensure that we fix up branches.
492 __ FinalizeCode();
493
494 // Adjust native pc offsets in stack maps.
495 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
496 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
497 uint32_t new_position = __ GetAdjustedPosition(old_position);
498 DCHECK_GE(new_position, old_position);
499 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
500 }
501
502 // Adjust pc offsets for the disassembly information.
503 if (disasm_info_ != nullptr) {
504 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
505 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
506 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
507 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
508 it.second.start = __ GetAdjustedPosition(it.second.start);
509 it.second.end = __ GetAdjustedPosition(it.second.end);
510 }
511 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
512 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
513 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
514 }
515 }
516
517 CodeGenerator::Finalize(allocator);
518}
519
520MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
521 return codegen_->GetAssembler();
522}
523
524void ParallelMoveResolverMIPS::EmitMove(size_t index) {
525 DCHECK_LT(index, moves_.size());
526 MoveOperands* move = moves_[index];
527 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
528}
529
530void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
531 DCHECK_LT(index, moves_.size());
532 MoveOperands* move = moves_[index];
533 Primitive::Type type = move->GetType();
534 Location loc1 = move->GetDestination();
535 Location loc2 = move->GetSource();
536
537 DCHECK(!loc1.IsConstant());
538 DCHECK(!loc2.IsConstant());
539
540 if (loc1.Equals(loc2)) {
541 return;
542 }
543
544 if (loc1.IsRegister() && loc2.IsRegister()) {
545 // Swap 2 GPRs.
546 Register r1 = loc1.AsRegister<Register>();
547 Register r2 = loc2.AsRegister<Register>();
548 __ Move(TMP, r2);
549 __ Move(r2, r1);
550 __ Move(r1, TMP);
551 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
552 FRegister f1 = loc1.AsFpuRegister<FRegister>();
553 FRegister f2 = loc2.AsFpuRegister<FRegister>();
554 if (type == Primitive::kPrimFloat) {
555 __ MovS(FTMP, f2);
556 __ MovS(f2, f1);
557 __ MovS(f1, FTMP);
558 } else {
559 DCHECK_EQ(type, Primitive::kPrimDouble);
560 __ MovD(FTMP, f2);
561 __ MovD(f2, f1);
562 __ MovD(f1, FTMP);
563 }
564 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
565 (loc1.IsFpuRegister() && loc2.IsRegister())) {
566 // Swap FPR and GPR.
567 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
568 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
569 : loc2.AsFpuRegister<FRegister>();
570 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
571 : loc2.AsRegister<Register>();
572 __ Move(TMP, r2);
573 __ Mfc1(r2, f1);
574 __ Mtc1(TMP, f1);
575 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
576 // Swap 2 GPR register pairs.
577 Register r1 = loc1.AsRegisterPairLow<Register>();
578 Register r2 = loc2.AsRegisterPairLow<Register>();
579 __ Move(TMP, r2);
580 __ Move(r2, r1);
581 __ Move(r1, TMP);
582 r1 = loc1.AsRegisterPairHigh<Register>();
583 r2 = loc2.AsRegisterPairHigh<Register>();
584 __ Move(TMP, r2);
585 __ Move(r2, r1);
586 __ Move(r1, TMP);
587 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
588 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
589 // Swap FPR and GPR register pair.
590 DCHECK_EQ(type, Primitive::kPrimDouble);
591 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
592 : loc2.AsFpuRegister<FRegister>();
593 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
594 : loc2.AsRegisterPairLow<Register>();
595 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
596 : loc2.AsRegisterPairHigh<Register>();
597 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
598 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
599 // unpredictable and the following mfch1 will fail.
600 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800601 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200602 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800603 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200604 __ Move(r2_l, TMP);
605 __ Move(r2_h, AT);
606 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
607 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
608 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
609 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000610 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
611 (loc1.IsStackSlot() && loc2.IsRegister())) {
612 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
613 : loc2.AsRegister<Register>();
614 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
615 : loc2.GetStackIndex();
616 __ Move(TMP, reg);
617 __ LoadFromOffset(kLoadWord, reg, SP, offset);
618 __ StoreToOffset(kStoreWord, TMP, SP, offset);
619 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
620 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
621 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
622 : loc2.AsRegisterPairLow<Register>();
623 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
624 : loc2.AsRegisterPairHigh<Register>();
625 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
626 : loc2.GetStackIndex();
627 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
628 : loc2.GetHighStackIndex(kMipsWordSize);
629 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000630 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000631 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000632 __ Move(TMP, reg_h);
633 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
634 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200635 } else {
636 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
637 }
638}
639
640void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
641 __ Pop(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::SpillScratch(int reg) {
645 __ Push(static_cast<Register>(reg));
646}
647
648void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
649 // Allocate a scratch register other than TMP, if available.
650 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
651 // automatically unspilled when the scratch scope object is destroyed).
652 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
653 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
654 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
655 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
656 __ LoadFromOffset(kLoadWord,
657 Register(ensure_scratch.GetRegister()),
658 SP,
659 index1 + stack_offset);
660 __ LoadFromOffset(kLoadWord,
661 TMP,
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord,
665 Register(ensure_scratch.GetRegister()),
666 SP,
667 index2 + stack_offset);
668 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
669 }
670}
671
Alexey Frunze73296a72016-06-03 22:51:46 -0700672void CodeGeneratorMIPS::ComputeSpillMask() {
673 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
674 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
675 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
676 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
677 // registers, include the ZERO register to force alignment of FPU callee-saved registers
678 // within the stack frame.
679 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
680 core_spill_mask_ |= (1 << ZERO);
681 }
682}
683
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200684static dwarf::Reg DWARFReg(Register reg) {
685 return dwarf::Reg::MipsCore(static_cast<int>(reg));
686}
687
688// TODO: mapping of floating-point registers to DWARF.
689
690void CodeGeneratorMIPS::GenerateFrameEntry() {
691 __ Bind(&frame_entry_label_);
692
693 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
694
695 if (do_overflow_check) {
696 __ LoadFromOffset(kLoadWord,
697 ZERO,
698 SP,
699 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
700 RecordPcInfo(nullptr, 0);
701 }
702
703 if (HasEmptyFrame()) {
704 return;
705 }
706
707 // Make sure the frame size isn't unreasonably large.
708 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
709 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
710 }
711
712 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200713
Alexey Frunze73296a72016-06-03 22:51:46 -0700714 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200715 __ IncreaseFrameSize(ofs);
716
Alexey Frunze73296a72016-06-03 22:51:46 -0700717 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
718 Register reg = static_cast<Register>(MostSignificantBit(mask));
719 mask ^= 1u << reg;
720 ofs -= kMipsWordSize;
721 // The ZERO register is only included for alignment.
722 if (reg != ZERO) {
723 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200724 __ cfi().RelOffset(DWARFReg(reg), ofs);
725 }
726 }
727
Alexey Frunze73296a72016-06-03 22:51:46 -0700728 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
729 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
730 mask ^= 1u << reg;
731 ofs -= kMipsDoublewordSize;
732 __ StoreDToOffset(reg, SP, ofs);
733 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 }
735
Alexey Frunze73296a72016-06-03 22:51:46 -0700736 // Store the current method pointer.
737 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200738}
739
740void CodeGeneratorMIPS::GenerateFrameExit() {
741 __ cfi().RememberState();
742
743 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 // For better instruction scheduling restore RA before other registers.
747 uint32_t ofs = GetFrameSize();
748 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
749 Register reg = static_cast<Register>(MostSignificantBit(mask));
750 mask ^= 1u << reg;
751 ofs -= kMipsWordSize;
752 // The ZERO register is only included for alignment.
753 if (reg != ZERO) {
754 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 __ cfi().Restore(DWARFReg(reg));
756 }
757 }
758
Alexey Frunze73296a72016-06-03 22:51:46 -0700759 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
760 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsDoublewordSize;
763 __ LoadDFromOffset(reg, SP, ofs);
764 // TODO: __ cfi().Restore(DWARFReg(reg));
765 }
766
767 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200768 }
769
770 __ Jr(RA);
771 __ Nop();
772
773 __ cfi().RestoreState();
774 __ cfi().DefCFAOffset(GetFrameSize());
775}
776
777void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
778 __ Bind(GetLabelOf(block));
779}
780
781void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
782 if (src.Equals(dst)) {
783 return;
784 }
785
786 if (src.IsConstant()) {
787 MoveConstant(dst, src.GetConstant());
788 } else {
789 if (Primitive::Is64BitType(dst_type)) {
790 Move64(dst, src);
791 } else {
792 Move32(dst, src);
793 }
794 }
795}
796
797void CodeGeneratorMIPS::Move32(Location destination, Location source) {
798 if (source.Equals(destination)) {
799 return;
800 }
801
802 if (destination.IsRegister()) {
803 if (source.IsRegister()) {
804 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
805 } else if (source.IsFpuRegister()) {
806 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
807 } else {
808 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
809 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
810 }
811 } else if (destination.IsFpuRegister()) {
812 if (source.IsRegister()) {
813 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
814 } else if (source.IsFpuRegister()) {
815 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
816 } else {
817 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
818 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
819 }
820 } else {
821 DCHECK(destination.IsStackSlot()) << destination;
822 if (source.IsRegister()) {
823 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
824 } else if (source.IsFpuRegister()) {
825 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
826 } else {
827 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
828 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
829 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
830 }
831 }
832}
833
834void CodeGeneratorMIPS::Move64(Location destination, Location source) {
835 if (source.Equals(destination)) {
836 return;
837 }
838
839 if (destination.IsRegisterPair()) {
840 if (source.IsRegisterPair()) {
841 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
842 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
843 } else if (source.IsFpuRegister()) {
844 Register dst_high = destination.AsRegisterPairHigh<Register>();
845 Register dst_low = destination.AsRegisterPairLow<Register>();
846 FRegister src = source.AsFpuRegister<FRegister>();
847 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800848 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200849 } else {
850 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
851 int32_t off = source.GetStackIndex();
852 Register r = destination.AsRegisterPairLow<Register>();
853 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
854 }
855 } else if (destination.IsFpuRegister()) {
856 if (source.IsRegisterPair()) {
857 FRegister dst = destination.AsFpuRegister<FRegister>();
858 Register src_high = source.AsRegisterPairHigh<Register>();
859 Register src_low = source.AsRegisterPairLow<Register>();
860 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800861 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200862 } else if (source.IsFpuRegister()) {
863 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
864 } else {
865 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
866 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
867 }
868 } else {
869 DCHECK(destination.IsDoubleStackSlot()) << destination;
870 int32_t off = destination.GetStackIndex();
871 if (source.IsRegisterPair()) {
872 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
873 } else if (source.IsFpuRegister()) {
874 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
875 } else {
876 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
877 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
878 __ StoreToOffset(kStoreWord, TMP, SP, off);
879 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
880 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
881 }
882 }
883}
884
885void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
886 if (c->IsIntConstant() || c->IsNullConstant()) {
887 // Move 32 bit constant.
888 int32_t value = GetInt32ValueOf(c);
889 if (destination.IsRegister()) {
890 Register dst = destination.AsRegister<Register>();
891 __ LoadConst32(dst, value);
892 } else {
893 DCHECK(destination.IsStackSlot())
894 << "Cannot move " << c->DebugName() << " to " << destination;
895 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
896 }
897 } else if (c->IsLongConstant()) {
898 // Move 64 bit constant.
899 int64_t value = GetInt64ValueOf(c);
900 if (destination.IsRegisterPair()) {
901 Register r_h = destination.AsRegisterPairHigh<Register>();
902 Register r_l = destination.AsRegisterPairLow<Register>();
903 __ LoadConst64(r_h, r_l, value);
904 } else {
905 DCHECK(destination.IsDoubleStackSlot())
906 << "Cannot move " << c->DebugName() << " to " << destination;
907 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
908 }
909 } else if (c->IsFloatConstant()) {
910 // Move 32 bit float constant.
911 int32_t value = GetInt32ValueOf(c);
912 if (destination.IsFpuRegister()) {
913 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
914 } else {
915 DCHECK(destination.IsStackSlot())
916 << "Cannot move " << c->DebugName() << " to " << destination;
917 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
918 }
919 } else {
920 // Move 64 bit double constant.
921 DCHECK(c->IsDoubleConstant()) << c->DebugName();
922 int64_t value = GetInt64ValueOf(c);
923 if (destination.IsFpuRegister()) {
924 FRegister fd = destination.AsFpuRegister<FRegister>();
925 __ LoadDConst64(fd, value, TMP);
926 } else {
927 DCHECK(destination.IsDoubleStackSlot())
928 << "Cannot move " << c->DebugName() << " to " << destination;
929 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
930 }
931 }
932}
933
934void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
935 DCHECK(destination.IsRegister());
936 Register dst = destination.AsRegister<Register>();
937 __ LoadConst32(dst, value);
938}
939
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200940void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
941 if (location.IsRegister()) {
942 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700943 } else if (location.IsRegisterPair()) {
944 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
945 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200946 } else {
947 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
948 }
949}
950
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200951void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
952 MipsLabel done;
953 Register card = AT;
954 Register temp = TMP;
955 __ Beqz(value, &done);
956 __ LoadFromOffset(kLoadWord,
957 card,
958 TR,
959 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
960 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
961 __ Addu(temp, card, temp);
962 __ Sb(card, temp, 0);
963 __ Bind(&done);
964}
965
David Brazdil58282f42016-01-14 12:45:10 +0000966void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200967 // Don't allocate the dalvik style register pair passing.
968 blocked_register_pairs_[A1_A2] = true;
969
970 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
971 blocked_core_registers_[ZERO] = true;
972 blocked_core_registers_[K0] = true;
973 blocked_core_registers_[K1] = true;
974 blocked_core_registers_[GP] = true;
975 blocked_core_registers_[SP] = true;
976 blocked_core_registers_[RA] = true;
977
978 // AT and TMP(T8) are used as temporary/scratch registers
979 // (similar to how AT is used by MIPS assemblers).
980 blocked_core_registers_[AT] = true;
981 blocked_core_registers_[TMP] = true;
982 blocked_fpu_registers_[FTMP] = true;
983
984 // Reserve suspend and thread registers.
985 blocked_core_registers_[S0] = true;
986 blocked_core_registers_[TR] = true;
987
988 // Reserve T9 for function calls
989 blocked_core_registers_[T9] = true;
990
991 // Reserve odd-numbered FPU registers.
992 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
993 blocked_fpu_registers_[i] = true;
994 }
995
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200996 UpdateBlockedPairRegisters();
997}
998
999void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1000 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1001 MipsManagedRegister current =
1002 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1003 if (blocked_core_registers_[current.AsRegisterPairLow()]
1004 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1005 blocked_register_pairs_[i] = true;
1006 }
1007 }
1008}
1009
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001010size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1011 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1012 return kMipsWordSize;
1013}
1014
1015size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1016 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1017 return kMipsWordSize;
1018}
1019
1020size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1021 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1022 return kMipsDoublewordSize;
1023}
1024
1025size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1026 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1027 return kMipsDoublewordSize;
1028}
1029
1030void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001031 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001032}
1033
1034void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001035 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001036}
1037
1038void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1039 HInstruction* instruction,
1040 uint32_t dex_pc,
1041 SlowPathCode* slow_path) {
1042 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1043 instruction,
1044 dex_pc,
1045 slow_path,
1046 IsDirectEntrypoint(entrypoint));
1047}
1048
1049constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1050
1051void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1052 HInstruction* instruction,
1053 uint32_t dex_pc,
1054 SlowPathCode* slow_path,
1055 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001056 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1057 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001058 if (is_direct_entrypoint) {
1059 // Reserve argument space on stack (for $a0-$a3) for
1060 // entrypoints that directly reference native implementations.
1061 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001062 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001063 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001064 } else {
1065 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001066 }
1067 RecordPcInfo(instruction, dex_pc, slow_path);
1068}
1069
1070void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1071 Register class_reg) {
1072 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1073 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1074 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1075 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1076 __ Sync(0);
1077 __ Bind(slow_path->GetExitLabel());
1078}
1079
1080void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1081 __ Sync(0); // Only stype 0 is supported.
1082}
1083
1084void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1085 HBasicBlock* successor) {
1086 SuspendCheckSlowPathMIPS* slow_path =
1087 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1088 codegen_->AddSlowPath(slow_path);
1089
1090 __ LoadFromOffset(kLoadUnsignedHalfword,
1091 TMP,
1092 TR,
1093 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1094 if (successor == nullptr) {
1095 __ Bnez(TMP, slow_path->GetEntryLabel());
1096 __ Bind(slow_path->GetReturnLabel());
1097 } else {
1098 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1099 __ B(slow_path->GetEntryLabel());
1100 // slow_path will return to GetLabelOf(successor).
1101 }
1102}
1103
1104InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1105 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001106 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001107 assembler_(codegen->GetAssembler()),
1108 codegen_(codegen) {}
1109
1110void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1111 DCHECK_EQ(instruction->InputCount(), 2U);
1112 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1113 Primitive::Type type = instruction->GetResultType();
1114 switch (type) {
1115 case Primitive::kPrimInt: {
1116 locations->SetInAt(0, Location::RequiresRegister());
1117 HInstruction* right = instruction->InputAt(1);
1118 bool can_use_imm = false;
1119 if (right->IsConstant()) {
1120 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1121 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1122 can_use_imm = IsUint<16>(imm);
1123 } else if (instruction->IsAdd()) {
1124 can_use_imm = IsInt<16>(imm);
1125 } else {
1126 DCHECK(instruction->IsSub());
1127 can_use_imm = IsInt<16>(-imm);
1128 }
1129 }
1130 if (can_use_imm)
1131 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1132 else
1133 locations->SetInAt(1, Location::RequiresRegister());
1134 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1135 break;
1136 }
1137
1138 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001139 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001140 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142 break;
1143 }
1144
1145 case Primitive::kPrimFloat:
1146 case Primitive::kPrimDouble:
1147 DCHECK(instruction->IsAdd() || instruction->IsSub());
1148 locations->SetInAt(0, Location::RequiresFpuRegister());
1149 locations->SetInAt(1, Location::RequiresFpuRegister());
1150 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1151 break;
1152
1153 default:
1154 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1155 }
1156}
1157
1158void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1159 Primitive::Type type = instruction->GetType();
1160 LocationSummary* locations = instruction->GetLocations();
1161
1162 switch (type) {
1163 case Primitive::kPrimInt: {
1164 Register dst = locations->Out().AsRegister<Register>();
1165 Register lhs = locations->InAt(0).AsRegister<Register>();
1166 Location rhs_location = locations->InAt(1);
1167
1168 Register rhs_reg = ZERO;
1169 int32_t rhs_imm = 0;
1170 bool use_imm = rhs_location.IsConstant();
1171 if (use_imm) {
1172 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1173 } else {
1174 rhs_reg = rhs_location.AsRegister<Register>();
1175 }
1176
1177 if (instruction->IsAnd()) {
1178 if (use_imm)
1179 __ Andi(dst, lhs, rhs_imm);
1180 else
1181 __ And(dst, lhs, rhs_reg);
1182 } else if (instruction->IsOr()) {
1183 if (use_imm)
1184 __ Ori(dst, lhs, rhs_imm);
1185 else
1186 __ Or(dst, lhs, rhs_reg);
1187 } else if (instruction->IsXor()) {
1188 if (use_imm)
1189 __ Xori(dst, lhs, rhs_imm);
1190 else
1191 __ Xor(dst, lhs, rhs_reg);
1192 } else if (instruction->IsAdd()) {
1193 if (use_imm)
1194 __ Addiu(dst, lhs, rhs_imm);
1195 else
1196 __ Addu(dst, lhs, rhs_reg);
1197 } else {
1198 DCHECK(instruction->IsSub());
1199 if (use_imm)
1200 __ Addiu(dst, lhs, -rhs_imm);
1201 else
1202 __ Subu(dst, lhs, rhs_reg);
1203 }
1204 break;
1205 }
1206
1207 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001208 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1209 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1210 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1211 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001212 Location rhs_location = locations->InAt(1);
1213 bool use_imm = rhs_location.IsConstant();
1214 if (!use_imm) {
1215 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1216 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1217 if (instruction->IsAnd()) {
1218 __ And(dst_low, lhs_low, rhs_low);
1219 __ And(dst_high, lhs_high, rhs_high);
1220 } else if (instruction->IsOr()) {
1221 __ Or(dst_low, lhs_low, rhs_low);
1222 __ Or(dst_high, lhs_high, rhs_high);
1223 } else if (instruction->IsXor()) {
1224 __ Xor(dst_low, lhs_low, rhs_low);
1225 __ Xor(dst_high, lhs_high, rhs_high);
1226 } else if (instruction->IsAdd()) {
1227 if (lhs_low == rhs_low) {
1228 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1229 __ Slt(TMP, lhs_low, ZERO);
1230 __ Addu(dst_low, lhs_low, rhs_low);
1231 } else {
1232 __ Addu(dst_low, lhs_low, rhs_low);
1233 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1234 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1235 }
1236 __ Addu(dst_high, lhs_high, rhs_high);
1237 __ Addu(dst_high, dst_high, TMP);
1238 } else {
1239 DCHECK(instruction->IsSub());
1240 __ Sltu(TMP, lhs_low, rhs_low);
1241 __ Subu(dst_low, lhs_low, rhs_low);
1242 __ Subu(dst_high, lhs_high, rhs_high);
1243 __ Subu(dst_high, dst_high, TMP);
1244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001246 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1247 if (instruction->IsOr()) {
1248 uint32_t low = Low32Bits(value);
1249 uint32_t high = High32Bits(value);
1250 if (IsUint<16>(low)) {
1251 if (dst_low != lhs_low || low != 0) {
1252 __ Ori(dst_low, lhs_low, low);
1253 }
1254 } else {
1255 __ LoadConst32(TMP, low);
1256 __ Or(dst_low, lhs_low, TMP);
1257 }
1258 if (IsUint<16>(high)) {
1259 if (dst_high != lhs_high || high != 0) {
1260 __ Ori(dst_high, lhs_high, high);
1261 }
1262 } else {
1263 if (high != low) {
1264 __ LoadConst32(TMP, high);
1265 }
1266 __ Or(dst_high, lhs_high, TMP);
1267 }
1268 } else if (instruction->IsXor()) {
1269 uint32_t low = Low32Bits(value);
1270 uint32_t high = High32Bits(value);
1271 if (IsUint<16>(low)) {
1272 if (dst_low != lhs_low || low != 0) {
1273 __ Xori(dst_low, lhs_low, low);
1274 }
1275 } else {
1276 __ LoadConst32(TMP, low);
1277 __ Xor(dst_low, lhs_low, TMP);
1278 }
1279 if (IsUint<16>(high)) {
1280 if (dst_high != lhs_high || high != 0) {
1281 __ Xori(dst_high, lhs_high, high);
1282 }
1283 } else {
1284 if (high != low) {
1285 __ LoadConst32(TMP, high);
1286 }
1287 __ Xor(dst_high, lhs_high, TMP);
1288 }
1289 } else if (instruction->IsAnd()) {
1290 uint32_t low = Low32Bits(value);
1291 uint32_t high = High32Bits(value);
1292 if (IsUint<16>(low)) {
1293 __ Andi(dst_low, lhs_low, low);
1294 } else if (low != 0xFFFFFFFF) {
1295 __ LoadConst32(TMP, low);
1296 __ And(dst_low, lhs_low, TMP);
1297 } else if (dst_low != lhs_low) {
1298 __ Move(dst_low, lhs_low);
1299 }
1300 if (IsUint<16>(high)) {
1301 __ Andi(dst_high, lhs_high, high);
1302 } else if (high != 0xFFFFFFFF) {
1303 if (high != low) {
1304 __ LoadConst32(TMP, high);
1305 }
1306 __ And(dst_high, lhs_high, TMP);
1307 } else if (dst_high != lhs_high) {
1308 __ Move(dst_high, lhs_high);
1309 }
1310 } else {
1311 if (instruction->IsSub()) {
1312 value = -value;
1313 } else {
1314 DCHECK(instruction->IsAdd());
1315 }
1316 int32_t low = Low32Bits(value);
1317 int32_t high = High32Bits(value);
1318 if (IsInt<16>(low)) {
1319 if (dst_low != lhs_low || low != 0) {
1320 __ Addiu(dst_low, lhs_low, low);
1321 }
1322 if (low != 0) {
1323 __ Sltiu(AT, dst_low, low);
1324 }
1325 } else {
1326 __ LoadConst32(TMP, low);
1327 __ Addu(dst_low, lhs_low, TMP);
1328 __ Sltu(AT, dst_low, TMP);
1329 }
1330 if (IsInt<16>(high)) {
1331 if (dst_high != lhs_high || high != 0) {
1332 __ Addiu(dst_high, lhs_high, high);
1333 }
1334 } else {
1335 if (high != low) {
1336 __ LoadConst32(TMP, high);
1337 }
1338 __ Addu(dst_high, lhs_high, TMP);
1339 }
1340 if (low != 0) {
1341 __ Addu(dst_high, dst_high, AT);
1342 }
1343 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 }
1345 break;
1346 }
1347
1348 case Primitive::kPrimFloat:
1349 case Primitive::kPrimDouble: {
1350 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1351 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1352 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1353 if (instruction->IsAdd()) {
1354 if (type == Primitive::kPrimFloat) {
1355 __ AddS(dst, lhs, rhs);
1356 } else {
1357 __ AddD(dst, lhs, rhs);
1358 }
1359 } else {
1360 DCHECK(instruction->IsSub());
1361 if (type == Primitive::kPrimFloat) {
1362 __ SubS(dst, lhs, rhs);
1363 } else {
1364 __ SubD(dst, lhs, rhs);
1365 }
1366 }
1367 break;
1368 }
1369
1370 default:
1371 LOG(FATAL) << "Unexpected binary operation type " << type;
1372 }
1373}
1374
1375void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001376 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001377
1378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1379 Primitive::Type type = instr->GetResultType();
1380 switch (type) {
1381 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001382 locations->SetInAt(0, Location::RequiresRegister());
1383 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1385 break;
1386 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001387 locations->SetInAt(0, Location::RequiresRegister());
1388 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1389 locations->SetOut(Location::RequiresRegister());
1390 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001391 default:
1392 LOG(FATAL) << "Unexpected shift type " << type;
1393 }
1394}
1395
1396static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1397
1398void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001399 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001400 LocationSummary* locations = instr->GetLocations();
1401 Primitive::Type type = instr->GetType();
1402
1403 Location rhs_location = locations->InAt(1);
1404 bool use_imm = rhs_location.IsConstant();
1405 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1406 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001407 const uint32_t shift_mask =
1408 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001409 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001410 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1411 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412
1413 switch (type) {
1414 case Primitive::kPrimInt: {
1415 Register dst = locations->Out().AsRegister<Register>();
1416 Register lhs = locations->InAt(0).AsRegister<Register>();
1417 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001418 if (shift_value == 0) {
1419 if (dst != lhs) {
1420 __ Move(dst, lhs);
1421 }
1422 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001423 __ Sll(dst, lhs, shift_value);
1424 } else if (instr->IsShr()) {
1425 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001426 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001427 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001428 } else {
1429 if (has_ins_rotr) {
1430 __ Rotr(dst, lhs, shift_value);
1431 } else {
1432 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1433 __ Srl(dst, lhs, shift_value);
1434 __ Or(dst, dst, TMP);
1435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001436 }
1437 } else {
1438 if (instr->IsShl()) {
1439 __ Sllv(dst, lhs, rhs_reg);
1440 } else if (instr->IsShr()) {
1441 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001442 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001444 } else {
1445 if (has_ins_rotr) {
1446 __ Rotrv(dst, lhs, rhs_reg);
1447 } else {
1448 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001449 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1450 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1451 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1452 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1453 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001454 __ Sllv(TMP, lhs, TMP);
1455 __ Srlv(dst, lhs, rhs_reg);
1456 __ Or(dst, dst, TMP);
1457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001458 }
1459 }
1460 break;
1461 }
1462
1463 case Primitive::kPrimLong: {
1464 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1465 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1466 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1467 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1468 if (use_imm) {
1469 if (shift_value == 0) {
1470 codegen_->Move64(locations->Out(), locations->InAt(0));
1471 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001472 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001473 if (instr->IsShl()) {
1474 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1475 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1476 __ Sll(dst_low, lhs_low, shift_value);
1477 } else if (instr->IsShr()) {
1478 __ Srl(dst_low, lhs_low, shift_value);
1479 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1480 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001481 } else if (instr->IsUShr()) {
1482 __ Srl(dst_low, lhs_low, shift_value);
1483 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1484 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001485 } else {
1486 __ Srl(dst_low, lhs_low, shift_value);
1487 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1488 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001489 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001490 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001491 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001492 if (instr->IsShl()) {
1493 __ Sll(dst_low, lhs_low, shift_value);
1494 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1495 __ Sll(dst_high, lhs_high, shift_value);
1496 __ Or(dst_high, dst_high, TMP);
1497 } else if (instr->IsShr()) {
1498 __ Sra(dst_high, lhs_high, shift_value);
1499 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1500 __ Srl(dst_low, lhs_low, shift_value);
1501 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001502 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001503 __ Srl(dst_high, lhs_high, shift_value);
1504 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1505 __ Srl(dst_low, lhs_low, shift_value);
1506 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001507 } else {
1508 __ Srl(TMP, lhs_low, shift_value);
1509 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1510 __ Or(dst_low, dst_low, TMP);
1511 __ Srl(TMP, lhs_high, shift_value);
1512 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1513 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001515 }
1516 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001517 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001518 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001519 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001520 __ Move(dst_low, ZERO);
1521 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001522 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001523 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001524 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001525 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001527 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001528 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001529 // 64-bit rotation by 32 is just a swap.
1530 __ Move(dst_low, lhs_high);
1531 __ Move(dst_high, lhs_low);
1532 } else {
1533 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001534 __ Srl(dst_low, lhs_high, shift_value_high);
1535 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1536 __ Srl(dst_high, lhs_low, shift_value_high);
1537 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001538 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001539 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1540 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001541 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001542 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1543 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001544 __ Or(dst_high, dst_high, TMP);
1545 }
1546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 }
1549 } else {
1550 MipsLabel done;
1551 if (instr->IsShl()) {
1552 __ Sllv(dst_low, lhs_low, rhs_reg);
1553 __ Nor(AT, ZERO, rhs_reg);
1554 __ Srl(TMP, lhs_low, 1);
1555 __ Srlv(TMP, TMP, AT);
1556 __ Sllv(dst_high, lhs_high, rhs_reg);
1557 __ Or(dst_high, dst_high, TMP);
1558 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1559 __ Beqz(TMP, &done);
1560 __ Move(dst_high, dst_low);
1561 __ Move(dst_low, ZERO);
1562 } else if (instr->IsShr()) {
1563 __ Srav(dst_high, lhs_high, rhs_reg);
1564 __ Nor(AT, ZERO, rhs_reg);
1565 __ Sll(TMP, lhs_high, 1);
1566 __ Sllv(TMP, TMP, AT);
1567 __ Srlv(dst_low, lhs_low, rhs_reg);
1568 __ Or(dst_low, dst_low, TMP);
1569 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1570 __ Beqz(TMP, &done);
1571 __ Move(dst_low, dst_high);
1572 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001573 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001574 __ Srlv(dst_high, lhs_high, rhs_reg);
1575 __ Nor(AT, ZERO, rhs_reg);
1576 __ Sll(TMP, lhs_high, 1);
1577 __ Sllv(TMP, TMP, AT);
1578 __ Srlv(dst_low, lhs_low, rhs_reg);
1579 __ Or(dst_low, dst_low, TMP);
1580 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1581 __ Beqz(TMP, &done);
1582 __ Move(dst_low, dst_high);
1583 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001584 } else {
1585 __ Nor(AT, ZERO, rhs_reg);
1586 __ Srlv(TMP, lhs_low, rhs_reg);
1587 __ Sll(dst_low, lhs_high, 1);
1588 __ Sllv(dst_low, dst_low, AT);
1589 __ Or(dst_low, dst_low, TMP);
1590 __ Srlv(TMP, lhs_high, rhs_reg);
1591 __ Sll(dst_high, lhs_low, 1);
1592 __ Sllv(dst_high, dst_high, AT);
1593 __ Or(dst_high, dst_high, TMP);
1594 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1595 __ Beqz(TMP, &done);
1596 __ Move(TMP, dst_high);
1597 __ Move(dst_high, dst_low);
1598 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001599 }
1600 __ Bind(&done);
1601 }
1602 break;
1603 }
1604
1605 default:
1606 LOG(FATAL) << "Unexpected shift operation type " << type;
1607 }
1608}
1609
1610void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1611 HandleBinaryOp(instruction);
1612}
1613
1614void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1615 HandleBinaryOp(instruction);
1616}
1617
1618void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1619 HandleBinaryOp(instruction);
1620}
1621
1622void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1623 HandleBinaryOp(instruction);
1624}
1625
1626void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1627 LocationSummary* locations =
1628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1629 locations->SetInAt(0, Location::RequiresRegister());
1630 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1631 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1632 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1633 } else {
1634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1635 }
1636}
1637
1638void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1639 LocationSummary* locations = instruction->GetLocations();
1640 Register obj = locations->InAt(0).AsRegister<Register>();
1641 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001642 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001643
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001644 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001645 switch (type) {
1646 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001647 Register out = locations->Out().AsRegister<Register>();
1648 if (index.IsConstant()) {
1649 size_t offset =
1650 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1651 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1652 } else {
1653 __ Addu(TMP, obj, index.AsRegister<Register>());
1654 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1655 }
1656 break;
1657 }
1658
1659 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001660 Register out = locations->Out().AsRegister<Register>();
1661 if (index.IsConstant()) {
1662 size_t offset =
1663 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1664 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1665 } else {
1666 __ Addu(TMP, obj, index.AsRegister<Register>());
1667 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1668 }
1669 break;
1670 }
1671
1672 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001673 Register out = locations->Out().AsRegister<Register>();
1674 if (index.IsConstant()) {
1675 size_t offset =
1676 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1677 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1678 } else {
1679 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1680 __ Addu(TMP, obj, TMP);
1681 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1682 }
1683 break;
1684 }
1685
1686 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001687 Register out = locations->Out().AsRegister<Register>();
1688 if (index.IsConstant()) {
1689 size_t offset =
1690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1691 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1692 } else {
1693 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1694 __ Addu(TMP, obj, TMP);
1695 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1696 }
1697 break;
1698 }
1699
1700 case Primitive::kPrimInt:
1701 case Primitive::kPrimNot: {
1702 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001703 Register out = locations->Out().AsRegister<Register>();
1704 if (index.IsConstant()) {
1705 size_t offset =
1706 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1707 __ LoadFromOffset(kLoadWord, out, obj, offset);
1708 } else {
1709 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1710 __ Addu(TMP, obj, TMP);
1711 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1712 }
1713 break;
1714 }
1715
1716 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001717 Register out = locations->Out().AsRegisterPairLow<Register>();
1718 if (index.IsConstant()) {
1719 size_t offset =
1720 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1721 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1722 } else {
1723 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1724 __ Addu(TMP, obj, TMP);
1725 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1726 }
1727 break;
1728 }
1729
1730 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001731 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1732 if (index.IsConstant()) {
1733 size_t offset =
1734 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1735 __ LoadSFromOffset(out, obj, offset);
1736 } else {
1737 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1738 __ Addu(TMP, obj, TMP);
1739 __ LoadSFromOffset(out, TMP, data_offset);
1740 }
1741 break;
1742 }
1743
1744 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1746 if (index.IsConstant()) {
1747 size_t offset =
1748 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1749 __ LoadDFromOffset(out, obj, offset);
1750 } else {
1751 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1752 __ Addu(TMP, obj, TMP);
1753 __ LoadDFromOffset(out, TMP, data_offset);
1754 }
1755 break;
1756 }
1757
1758 case Primitive::kPrimVoid:
1759 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1760 UNREACHABLE();
1761 }
1762 codegen_->MaybeRecordImplicitNullCheck(instruction);
1763}
1764
1765void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1767 locations->SetInAt(0, Location::RequiresRegister());
1768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1769}
1770
1771void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1772 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001773 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001774 Register obj = locations->InAt(0).AsRegister<Register>();
1775 Register out = locations->Out().AsRegister<Register>();
1776 __ LoadFromOffset(kLoadWord, out, obj, offset);
1777 codegen_->MaybeRecordImplicitNullCheck(instruction);
1778}
1779
1780void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001781 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1783 instruction,
Pavle Batuta934808f2015-11-03 13:23:54 +01001784 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
1785 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001786 InvokeRuntimeCallingConvention calling_convention;
1787 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1788 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1789 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1790 } else {
1791 locations->SetInAt(0, Location::RequiresRegister());
1792 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1793 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1794 locations->SetInAt(2, Location::RequiresFpuRegister());
1795 } else {
1796 locations->SetInAt(2, Location::RequiresRegister());
1797 }
1798 }
1799}
1800
1801void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1802 LocationSummary* locations = instruction->GetLocations();
1803 Register obj = locations->InAt(0).AsRegister<Register>();
1804 Location index = locations->InAt(1);
1805 Primitive::Type value_type = instruction->GetComponentType();
1806 bool needs_runtime_call = locations->WillCall();
1807 bool needs_write_barrier =
1808 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1809
1810 switch (value_type) {
1811 case Primitive::kPrimBoolean:
1812 case Primitive::kPrimByte: {
1813 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1814 Register value = locations->InAt(2).AsRegister<Register>();
1815 if (index.IsConstant()) {
1816 size_t offset =
1817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1818 __ StoreToOffset(kStoreByte, value, obj, offset);
1819 } else {
1820 __ Addu(TMP, obj, index.AsRegister<Register>());
1821 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1822 }
1823 break;
1824 }
1825
1826 case Primitive::kPrimShort:
1827 case Primitive::kPrimChar: {
1828 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1829 Register value = locations->InAt(2).AsRegister<Register>();
1830 if (index.IsConstant()) {
1831 size_t offset =
1832 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1833 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1834 } else {
1835 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1836 __ Addu(TMP, obj, TMP);
1837 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1838 }
1839 break;
1840 }
1841
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimNot: {
1844 if (!needs_runtime_call) {
1845 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1846 Register value = locations->InAt(2).AsRegister<Register>();
1847 if (index.IsConstant()) {
1848 size_t offset =
1849 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1850 __ StoreToOffset(kStoreWord, value, obj, offset);
1851 } else {
1852 DCHECK(index.IsRegister()) << index;
1853 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1854 __ Addu(TMP, obj, TMP);
1855 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1856 }
1857 codegen_->MaybeRecordImplicitNullCheck(instruction);
1858 if (needs_write_barrier) {
1859 DCHECK_EQ(value_type, Primitive::kPrimNot);
1860 codegen_->MarkGCCard(obj, value);
1861 }
1862 } else {
1863 DCHECK_EQ(value_type, Primitive::kPrimNot);
1864 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1865 instruction,
1866 instruction->GetDexPc(),
1867 nullptr,
1868 IsDirectEntrypoint(kQuickAputObject));
1869 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1870 }
1871 break;
1872 }
1873
1874 case Primitive::kPrimLong: {
1875 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1876 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1877 if (index.IsConstant()) {
1878 size_t offset =
1879 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1880 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1881 } else {
1882 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1883 __ Addu(TMP, obj, TMP);
1884 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1885 }
1886 break;
1887 }
1888
1889 case Primitive::kPrimFloat: {
1890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1891 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1892 DCHECK(locations->InAt(2).IsFpuRegister());
1893 if (index.IsConstant()) {
1894 size_t offset =
1895 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1896 __ StoreSToOffset(value, obj, offset);
1897 } else {
1898 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1899 __ Addu(TMP, obj, TMP);
1900 __ StoreSToOffset(value, TMP, data_offset);
1901 }
1902 break;
1903 }
1904
1905 case Primitive::kPrimDouble: {
1906 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1907 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1908 DCHECK(locations->InAt(2).IsFpuRegister());
1909 if (index.IsConstant()) {
1910 size_t offset =
1911 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1912 __ StoreDToOffset(value, obj, offset);
1913 } else {
1914 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1915 __ Addu(TMP, obj, TMP);
1916 __ StoreDToOffset(value, TMP, data_offset);
1917 }
1918 break;
1919 }
1920
1921 case Primitive::kPrimVoid:
1922 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1923 UNREACHABLE();
1924 }
1925
1926 // Ints and objects are handled in the switch.
1927 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1928 codegen_->MaybeRecordImplicitNullCheck(instruction);
1929 }
1930}
1931
1932void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1933 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1934 ? LocationSummary::kCallOnSlowPath
1935 : LocationSummary::kNoCall;
1936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1937 locations->SetInAt(0, Location::RequiresRegister());
1938 locations->SetInAt(1, Location::RequiresRegister());
1939 if (instruction->HasUses()) {
1940 locations->SetOut(Location::SameAsFirstInput());
1941 }
1942}
1943
1944void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1945 LocationSummary* locations = instruction->GetLocations();
1946 BoundsCheckSlowPathMIPS* slow_path =
1947 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1948 codegen_->AddSlowPath(slow_path);
1949
1950 Register index = locations->InAt(0).AsRegister<Register>();
1951 Register length = locations->InAt(1).AsRegister<Register>();
1952
1953 // length is limited by the maximum positive signed 32-bit integer.
1954 // Unsigned comparison of length and index checks for index < 0
1955 // and for length <= index simultaneously.
1956 __ Bgeu(index, length, slow_path->GetEntryLabel());
1957}
1958
1959void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1961 instruction,
1962 LocationSummary::kCallOnSlowPath);
1963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetInAt(1, Location::RequiresRegister());
1965 // Note that TypeCheckSlowPathMIPS uses this register too.
1966 locations->AddTemp(Location::RequiresRegister());
1967}
1968
1969void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
1970 LocationSummary* locations = instruction->GetLocations();
1971 Register obj = locations->InAt(0).AsRegister<Register>();
1972 Register cls = locations->InAt(1).AsRegister<Register>();
1973 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
1974
1975 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
1976 codegen_->AddSlowPath(slow_path);
1977
1978 // TODO: avoid this check if we know obj is not null.
1979 __ Beqz(obj, slow_path->GetExitLabel());
1980 // Compare the class of `obj` with `cls`.
1981 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1982 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
1983 __ Bind(slow_path->GetExitLabel());
1984}
1985
1986void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
1987 LocationSummary* locations =
1988 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 if (check->HasUses()) {
1991 locations->SetOut(Location::SameAsFirstInput());
1992 }
1993}
1994
1995void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
1996 // We assume the class is not null.
1997 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
1998 check->GetLoadClass(),
1999 check,
2000 check->GetDexPc(),
2001 true);
2002 codegen_->AddSlowPath(slow_path);
2003 GenerateClassInitializationCheck(slow_path,
2004 check->GetLocations()->InAt(0).AsRegister<Register>());
2005}
2006
2007void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2008 Primitive::Type in_type = compare->InputAt(0)->GetType();
2009
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002012
2013 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002014 case Primitive::kPrimBoolean:
2015 case Primitive::kPrimByte:
2016 case Primitive::kPrimShort:
2017 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002018 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 case Primitive::kPrimLong:
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RequiresRegister());
2022 // Output overlaps because it is written before doing the low comparison.
2023 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2024 break;
2025
2026 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002027 case Primitive::kPrimDouble:
2028 locations->SetInAt(0, Location::RequiresFpuRegister());
2029 locations->SetInAt(1, Location::RequiresFpuRegister());
2030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032
2033 default:
2034 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2035 }
2036}
2037
2038void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2039 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002040 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002042 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043
2044 // 0 if: left == right
2045 // 1 if: left > right
2046 // -1 if: left < right
2047 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002048 case Primitive::kPrimBoolean:
2049 case Primitive::kPrimByte:
2050 case Primitive::kPrimShort:
2051 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002052 case Primitive::kPrimInt: {
2053 Register lhs = locations->InAt(0).AsRegister<Register>();
2054 Register rhs = locations->InAt(1).AsRegister<Register>();
2055 __ Slt(TMP, lhs, rhs);
2056 __ Slt(res, rhs, lhs);
2057 __ Subu(res, res, TMP);
2058 break;
2059 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 case Primitive::kPrimLong: {
2061 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2063 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2064 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2065 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2066 // TODO: more efficient (direct) comparison with a constant.
2067 __ Slt(TMP, lhs_high, rhs_high);
2068 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2069 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2070 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2071 __ Sltu(TMP, lhs_low, rhs_low);
2072 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2073 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2074 __ Bind(&done);
2075 break;
2076 }
2077
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002078 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002079 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002080 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2081 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2082 MipsLabel done;
2083 if (isR6) {
2084 __ CmpEqS(FTMP, lhs, rhs);
2085 __ LoadConst32(res, 0);
2086 __ Bc1nez(FTMP, &done);
2087 if (gt_bias) {
2088 __ CmpLtS(FTMP, lhs, rhs);
2089 __ LoadConst32(res, -1);
2090 __ Bc1nez(FTMP, &done);
2091 __ LoadConst32(res, 1);
2092 } else {
2093 __ CmpLtS(FTMP, rhs, lhs);
2094 __ LoadConst32(res, 1);
2095 __ Bc1nez(FTMP, &done);
2096 __ LoadConst32(res, -1);
2097 }
2098 } else {
2099 if (gt_bias) {
2100 __ ColtS(0, lhs, rhs);
2101 __ LoadConst32(res, -1);
2102 __ Bc1t(0, &done);
2103 __ CeqS(0, lhs, rhs);
2104 __ LoadConst32(res, 1);
2105 __ Movt(res, ZERO, 0);
2106 } else {
2107 __ ColtS(0, rhs, lhs);
2108 __ LoadConst32(res, 1);
2109 __ Bc1t(0, &done);
2110 __ CeqS(0, lhs, rhs);
2111 __ LoadConst32(res, -1);
2112 __ Movt(res, ZERO, 0);
2113 }
2114 }
2115 __ Bind(&done);
2116 break;
2117 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002119 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002120 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2121 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2122 MipsLabel done;
2123 if (isR6) {
2124 __ CmpEqD(FTMP, lhs, rhs);
2125 __ LoadConst32(res, 0);
2126 __ Bc1nez(FTMP, &done);
2127 if (gt_bias) {
2128 __ CmpLtD(FTMP, lhs, rhs);
2129 __ LoadConst32(res, -1);
2130 __ Bc1nez(FTMP, &done);
2131 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002133 __ CmpLtD(FTMP, rhs, lhs);
2134 __ LoadConst32(res, 1);
2135 __ Bc1nez(FTMP, &done);
2136 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002137 }
2138 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002139 if (gt_bias) {
2140 __ ColtD(0, lhs, rhs);
2141 __ LoadConst32(res, -1);
2142 __ Bc1t(0, &done);
2143 __ CeqD(0, lhs, rhs);
2144 __ LoadConst32(res, 1);
2145 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002146 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002147 __ ColtD(0, rhs, lhs);
2148 __ LoadConst32(res, 1);
2149 __ Bc1t(0, &done);
2150 __ CeqD(0, lhs, rhs);
2151 __ LoadConst32(res, -1);
2152 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002153 }
2154 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002155 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002156 break;
2157 }
2158
2159 default:
2160 LOG(FATAL) << "Unimplemented compare type " << in_type;
2161 }
2162}
2163
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002164void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002166 switch (instruction->InputAt(0)->GetType()) {
2167 default:
2168 case Primitive::kPrimLong:
2169 locations->SetInAt(0, Location::RequiresRegister());
2170 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2171 break;
2172
2173 case Primitive::kPrimFloat:
2174 case Primitive::kPrimDouble:
2175 locations->SetInAt(0, Location::RequiresFpuRegister());
2176 locations->SetInAt(1, Location::RequiresFpuRegister());
2177 break;
2178 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002179 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2181 }
2182}
2183
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002184void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002185 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002186 return;
2187 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002188
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002189 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002190 LocationSummary* locations = instruction->GetLocations();
2191 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002192 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002193
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002194 switch (type) {
2195 default:
2196 // Integer case.
2197 GenerateIntCompare(instruction->GetCondition(), locations);
2198 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002199
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002200 case Primitive::kPrimLong:
2201 // TODO: don't use branches.
2202 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002203 break;
2204
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002205 case Primitive::kPrimFloat:
2206 case Primitive::kPrimDouble:
2207 // TODO: don't use branches.
2208 GenerateFpCompareAndBranch(instruction->GetCondition(),
2209 instruction->IsGtBias(),
2210 type,
2211 locations,
2212 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002213 break;
2214 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002215
2216 // Convert the branches into the result.
2217 MipsLabel done;
2218
2219 // False case: result = 0.
2220 __ LoadConst32(dst, 0);
2221 __ B(&done);
2222
2223 // True case: result = 1.
2224 __ Bind(&true_label);
2225 __ LoadConst32(dst, 1);
2226 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002227}
2228
Alexey Frunze7e99e052015-11-24 19:28:01 -08002229void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2230 DCHECK(instruction->IsDiv() || instruction->IsRem());
2231 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2232
2233 LocationSummary* locations = instruction->GetLocations();
2234 Location second = locations->InAt(1);
2235 DCHECK(second.IsConstant());
2236
2237 Register out = locations->Out().AsRegister<Register>();
2238 Register dividend = locations->InAt(0).AsRegister<Register>();
2239 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2240 DCHECK(imm == 1 || imm == -1);
2241
2242 if (instruction->IsRem()) {
2243 __ Move(out, ZERO);
2244 } else {
2245 if (imm == -1) {
2246 __ Subu(out, ZERO, dividend);
2247 } else if (out != dividend) {
2248 __ Move(out, dividend);
2249 }
2250 }
2251}
2252
2253void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2254 DCHECK(instruction->IsDiv() || instruction->IsRem());
2255 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2256
2257 LocationSummary* locations = instruction->GetLocations();
2258 Location second = locations->InAt(1);
2259 DCHECK(second.IsConstant());
2260
2261 Register out = locations->Out().AsRegister<Register>();
2262 Register dividend = locations->InAt(0).AsRegister<Register>();
2263 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002264 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002265 int ctz_imm = CTZ(abs_imm);
2266
2267 if (instruction->IsDiv()) {
2268 if (ctz_imm == 1) {
2269 // Fast path for division by +/-2, which is very common.
2270 __ Srl(TMP, dividend, 31);
2271 } else {
2272 __ Sra(TMP, dividend, 31);
2273 __ Srl(TMP, TMP, 32 - ctz_imm);
2274 }
2275 __ Addu(out, dividend, TMP);
2276 __ Sra(out, out, ctz_imm);
2277 if (imm < 0) {
2278 __ Subu(out, ZERO, out);
2279 }
2280 } else {
2281 if (ctz_imm == 1) {
2282 // Fast path for modulo +/-2, which is very common.
2283 __ Sra(TMP, dividend, 31);
2284 __ Subu(out, dividend, TMP);
2285 __ Andi(out, out, 1);
2286 __ Addu(out, out, TMP);
2287 } else {
2288 __ Sra(TMP, dividend, 31);
2289 __ Srl(TMP, TMP, 32 - ctz_imm);
2290 __ Addu(out, dividend, TMP);
2291 if (IsUint<16>(abs_imm - 1)) {
2292 __ Andi(out, out, abs_imm - 1);
2293 } else {
2294 __ Sll(out, out, 32 - ctz_imm);
2295 __ Srl(out, out, 32 - ctz_imm);
2296 }
2297 __ Subu(out, out, TMP);
2298 }
2299 }
2300}
2301
2302void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2303 DCHECK(instruction->IsDiv() || instruction->IsRem());
2304 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2305
2306 LocationSummary* locations = instruction->GetLocations();
2307 Location second = locations->InAt(1);
2308 DCHECK(second.IsConstant());
2309
2310 Register out = locations->Out().AsRegister<Register>();
2311 Register dividend = locations->InAt(0).AsRegister<Register>();
2312 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2313
2314 int64_t magic;
2315 int shift;
2316 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2317
2318 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2319
2320 __ LoadConst32(TMP, magic);
2321 if (isR6) {
2322 __ MuhR6(TMP, dividend, TMP);
2323 } else {
2324 __ MultR2(dividend, TMP);
2325 __ Mfhi(TMP);
2326 }
2327 if (imm > 0 && magic < 0) {
2328 __ Addu(TMP, TMP, dividend);
2329 } else if (imm < 0 && magic > 0) {
2330 __ Subu(TMP, TMP, dividend);
2331 }
2332
2333 if (shift != 0) {
2334 __ Sra(TMP, TMP, shift);
2335 }
2336
2337 if (instruction->IsDiv()) {
2338 __ Sra(out, TMP, 31);
2339 __ Subu(out, TMP, out);
2340 } else {
2341 __ Sra(AT, TMP, 31);
2342 __ Subu(AT, TMP, AT);
2343 __ LoadConst32(TMP, imm);
2344 if (isR6) {
2345 __ MulR6(TMP, AT, TMP);
2346 } else {
2347 __ MulR2(TMP, AT, TMP);
2348 }
2349 __ Subu(out, dividend, TMP);
2350 }
2351}
2352
2353void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2354 DCHECK(instruction->IsDiv() || instruction->IsRem());
2355 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2356
2357 LocationSummary* locations = instruction->GetLocations();
2358 Register out = locations->Out().AsRegister<Register>();
2359 Location second = locations->InAt(1);
2360
2361 if (second.IsConstant()) {
2362 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2363 if (imm == 0) {
2364 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2365 } else if (imm == 1 || imm == -1) {
2366 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002367 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002368 DivRemByPowerOfTwo(instruction);
2369 } else {
2370 DCHECK(imm <= -2 || imm >= 2);
2371 GenerateDivRemWithAnyConstant(instruction);
2372 }
2373 } else {
2374 Register dividend = locations->InAt(0).AsRegister<Register>();
2375 Register divisor = second.AsRegister<Register>();
2376 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2377 if (instruction->IsDiv()) {
2378 if (isR6) {
2379 __ DivR6(out, dividend, divisor);
2380 } else {
2381 __ DivR2(out, dividend, divisor);
2382 }
2383 } else {
2384 if (isR6) {
2385 __ ModR6(out, dividend, divisor);
2386 } else {
2387 __ ModR2(out, dividend, divisor);
2388 }
2389 }
2390 }
2391}
2392
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002393void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2394 Primitive::Type type = div->GetResultType();
2395 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2396 ? LocationSummary::kCall
2397 : LocationSummary::kNoCall;
2398
2399 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2400
2401 switch (type) {
2402 case Primitive::kPrimInt:
2403 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002404 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2406 break;
2407
2408 case Primitive::kPrimLong: {
2409 InvokeRuntimeCallingConvention calling_convention;
2410 locations->SetInAt(0, Location::RegisterPairLocation(
2411 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2412 locations->SetInAt(1, Location::RegisterPairLocation(
2413 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2414 locations->SetOut(calling_convention.GetReturnLocation(type));
2415 break;
2416 }
2417
2418 case Primitive::kPrimFloat:
2419 case Primitive::kPrimDouble:
2420 locations->SetInAt(0, Location::RequiresFpuRegister());
2421 locations->SetInAt(1, Location::RequiresFpuRegister());
2422 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2423 break;
2424
2425 default:
2426 LOG(FATAL) << "Unexpected div type " << type;
2427 }
2428}
2429
2430void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2431 Primitive::Type type = instruction->GetType();
2432 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433
2434 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002435 case Primitive::kPrimInt:
2436 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 case Primitive::kPrimLong: {
2439 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2440 instruction,
2441 instruction->GetDexPc(),
2442 nullptr,
2443 IsDirectEntrypoint(kQuickLdiv));
2444 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2445 break;
2446 }
2447 case Primitive::kPrimFloat:
2448 case Primitive::kPrimDouble: {
2449 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2450 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2451 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2452 if (type == Primitive::kPrimFloat) {
2453 __ DivS(dst, lhs, rhs);
2454 } else {
2455 __ DivD(dst, lhs, rhs);
2456 }
2457 break;
2458 }
2459 default:
2460 LOG(FATAL) << "Unexpected div type " << type;
2461 }
2462}
2463
2464void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2465 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2466 ? LocationSummary::kCallOnSlowPath
2467 : LocationSummary::kNoCall;
2468 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2469 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2470 if (instruction->HasUses()) {
2471 locations->SetOut(Location::SameAsFirstInput());
2472 }
2473}
2474
2475void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2476 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2477 codegen_->AddSlowPath(slow_path);
2478 Location value = instruction->GetLocations()->InAt(0);
2479 Primitive::Type type = instruction->GetType();
2480
2481 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002482 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimChar:
2485 case Primitive::kPrimShort:
2486 case Primitive::kPrimInt: {
2487 if (value.IsConstant()) {
2488 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2489 __ B(slow_path->GetEntryLabel());
2490 } else {
2491 // A division by a non-null constant is valid. We don't need to perform
2492 // any check, so simply fall through.
2493 }
2494 } else {
2495 DCHECK(value.IsRegister()) << value;
2496 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2497 }
2498 break;
2499 }
2500 case Primitive::kPrimLong: {
2501 if (value.IsConstant()) {
2502 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2503 __ B(slow_path->GetEntryLabel());
2504 } else {
2505 // A division by a non-null constant is valid. We don't need to perform
2506 // any check, so simply fall through.
2507 }
2508 } else {
2509 DCHECK(value.IsRegisterPair()) << value;
2510 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2511 __ Beqz(TMP, slow_path->GetEntryLabel());
2512 }
2513 break;
2514 }
2515 default:
2516 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2517 }
2518}
2519
2520void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2521 LocationSummary* locations =
2522 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2523 locations->SetOut(Location::ConstantLocation(constant));
2524}
2525
2526void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2527 // Will be generated at use site.
2528}
2529
2530void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2531 exit->SetLocations(nullptr);
2532}
2533
2534void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2535}
2536
2537void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2538 LocationSummary* locations =
2539 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2540 locations->SetOut(Location::ConstantLocation(constant));
2541}
2542
2543void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2544 // Will be generated at use site.
2545}
2546
2547void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2548 got->SetLocations(nullptr);
2549}
2550
2551void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2552 DCHECK(!successor->IsExitBlock());
2553 HBasicBlock* block = got->GetBlock();
2554 HInstruction* previous = got->GetPrevious();
2555 HLoopInformation* info = block->GetLoopInformation();
2556
2557 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2558 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2559 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2560 return;
2561 }
2562 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2563 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2564 }
2565 if (!codegen_->GoesToNextBlock(block, successor)) {
2566 __ B(codegen_->GetLabelOf(successor));
2567 }
2568}
2569
2570void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2571 HandleGoto(got, got->GetSuccessor());
2572}
2573
2574void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2575 try_boundary->SetLocations(nullptr);
2576}
2577
2578void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2579 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2580 if (!successor->IsExitBlock()) {
2581 HandleGoto(try_boundary, successor);
2582 }
2583}
2584
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002585void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2586 LocationSummary* locations) {
2587 Register dst = locations->Out().AsRegister<Register>();
2588 Register lhs = locations->InAt(0).AsRegister<Register>();
2589 Location rhs_location = locations->InAt(1);
2590 Register rhs_reg = ZERO;
2591 int64_t rhs_imm = 0;
2592 bool use_imm = rhs_location.IsConstant();
2593 if (use_imm) {
2594 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2595 } else {
2596 rhs_reg = rhs_location.AsRegister<Register>();
2597 }
2598
2599 switch (cond) {
2600 case kCondEQ:
2601 case kCondNE:
2602 if (use_imm && IsUint<16>(rhs_imm)) {
2603 __ Xori(dst, lhs, rhs_imm);
2604 } else {
2605 if (use_imm) {
2606 rhs_reg = TMP;
2607 __ LoadConst32(rhs_reg, rhs_imm);
2608 }
2609 __ Xor(dst, lhs, rhs_reg);
2610 }
2611 if (cond == kCondEQ) {
2612 __ Sltiu(dst, dst, 1);
2613 } else {
2614 __ Sltu(dst, ZERO, dst);
2615 }
2616 break;
2617
2618 case kCondLT:
2619 case kCondGE:
2620 if (use_imm && IsInt<16>(rhs_imm)) {
2621 __ Slti(dst, lhs, rhs_imm);
2622 } else {
2623 if (use_imm) {
2624 rhs_reg = TMP;
2625 __ LoadConst32(rhs_reg, rhs_imm);
2626 }
2627 __ Slt(dst, lhs, rhs_reg);
2628 }
2629 if (cond == kCondGE) {
2630 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2631 // only the slt instruction but no sge.
2632 __ Xori(dst, dst, 1);
2633 }
2634 break;
2635
2636 case kCondLE:
2637 case kCondGT:
2638 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2639 // Simulate lhs <= rhs via lhs < rhs + 1.
2640 __ Slti(dst, lhs, rhs_imm + 1);
2641 if (cond == kCondGT) {
2642 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2643 // only the slti instruction but no sgti.
2644 __ Xori(dst, dst, 1);
2645 }
2646 } else {
2647 if (use_imm) {
2648 rhs_reg = TMP;
2649 __ LoadConst32(rhs_reg, rhs_imm);
2650 }
2651 __ Slt(dst, rhs_reg, lhs);
2652 if (cond == kCondLE) {
2653 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2654 // only the slt instruction but no sle.
2655 __ Xori(dst, dst, 1);
2656 }
2657 }
2658 break;
2659
2660 case kCondB:
2661 case kCondAE:
2662 if (use_imm && IsInt<16>(rhs_imm)) {
2663 // Sltiu sign-extends its 16-bit immediate operand before
2664 // the comparison and thus lets us compare directly with
2665 // unsigned values in the ranges [0, 0x7fff] and
2666 // [0xffff8000, 0xffffffff].
2667 __ Sltiu(dst, lhs, rhs_imm);
2668 } else {
2669 if (use_imm) {
2670 rhs_reg = TMP;
2671 __ LoadConst32(rhs_reg, rhs_imm);
2672 }
2673 __ Sltu(dst, lhs, rhs_reg);
2674 }
2675 if (cond == kCondAE) {
2676 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2677 // only the sltu instruction but no sgeu.
2678 __ Xori(dst, dst, 1);
2679 }
2680 break;
2681
2682 case kCondBE:
2683 case kCondA:
2684 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2685 // Simulate lhs <= rhs via lhs < rhs + 1.
2686 // Note that this only works if rhs + 1 does not overflow
2687 // to 0, hence the check above.
2688 // Sltiu sign-extends its 16-bit immediate operand before
2689 // the comparison and thus lets us compare directly with
2690 // unsigned values in the ranges [0, 0x7fff] and
2691 // [0xffff8000, 0xffffffff].
2692 __ Sltiu(dst, lhs, rhs_imm + 1);
2693 if (cond == kCondA) {
2694 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2695 // only the sltiu instruction but no sgtiu.
2696 __ Xori(dst, dst, 1);
2697 }
2698 } else {
2699 if (use_imm) {
2700 rhs_reg = TMP;
2701 __ LoadConst32(rhs_reg, rhs_imm);
2702 }
2703 __ Sltu(dst, rhs_reg, lhs);
2704 if (cond == kCondBE) {
2705 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2706 // only the sltu instruction but no sleu.
2707 __ Xori(dst, dst, 1);
2708 }
2709 }
2710 break;
2711 }
2712}
2713
2714void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2715 LocationSummary* locations,
2716 MipsLabel* label) {
2717 Register lhs = locations->InAt(0).AsRegister<Register>();
2718 Location rhs_location = locations->InAt(1);
2719 Register rhs_reg = ZERO;
2720 int32_t rhs_imm = 0;
2721 bool use_imm = rhs_location.IsConstant();
2722 if (use_imm) {
2723 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2724 } else {
2725 rhs_reg = rhs_location.AsRegister<Register>();
2726 }
2727
2728 if (use_imm && rhs_imm == 0) {
2729 switch (cond) {
2730 case kCondEQ:
2731 case kCondBE: // <= 0 if zero
2732 __ Beqz(lhs, label);
2733 break;
2734 case kCondNE:
2735 case kCondA: // > 0 if non-zero
2736 __ Bnez(lhs, label);
2737 break;
2738 case kCondLT:
2739 __ Bltz(lhs, label);
2740 break;
2741 case kCondGE:
2742 __ Bgez(lhs, label);
2743 break;
2744 case kCondLE:
2745 __ Blez(lhs, label);
2746 break;
2747 case kCondGT:
2748 __ Bgtz(lhs, label);
2749 break;
2750 case kCondB: // always false
2751 break;
2752 case kCondAE: // always true
2753 __ B(label);
2754 break;
2755 }
2756 } else {
2757 if (use_imm) {
2758 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2759 rhs_reg = TMP;
2760 __ LoadConst32(rhs_reg, rhs_imm);
2761 }
2762 switch (cond) {
2763 case kCondEQ:
2764 __ Beq(lhs, rhs_reg, label);
2765 break;
2766 case kCondNE:
2767 __ Bne(lhs, rhs_reg, label);
2768 break;
2769 case kCondLT:
2770 __ Blt(lhs, rhs_reg, label);
2771 break;
2772 case kCondGE:
2773 __ Bge(lhs, rhs_reg, label);
2774 break;
2775 case kCondLE:
2776 __ Bge(rhs_reg, lhs, label);
2777 break;
2778 case kCondGT:
2779 __ Blt(rhs_reg, lhs, label);
2780 break;
2781 case kCondB:
2782 __ Bltu(lhs, rhs_reg, label);
2783 break;
2784 case kCondAE:
2785 __ Bgeu(lhs, rhs_reg, label);
2786 break;
2787 case kCondBE:
2788 __ Bgeu(rhs_reg, lhs, label);
2789 break;
2790 case kCondA:
2791 __ Bltu(rhs_reg, lhs, label);
2792 break;
2793 }
2794 }
2795}
2796
2797void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2798 LocationSummary* locations,
2799 MipsLabel* label) {
2800 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2801 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2802 Location rhs_location = locations->InAt(1);
2803 Register rhs_high = ZERO;
2804 Register rhs_low = ZERO;
2805 int64_t imm = 0;
2806 uint32_t imm_high = 0;
2807 uint32_t imm_low = 0;
2808 bool use_imm = rhs_location.IsConstant();
2809 if (use_imm) {
2810 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2811 imm_high = High32Bits(imm);
2812 imm_low = Low32Bits(imm);
2813 } else {
2814 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2815 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2816 }
2817
2818 if (use_imm && imm == 0) {
2819 switch (cond) {
2820 case kCondEQ:
2821 case kCondBE: // <= 0 if zero
2822 __ Or(TMP, lhs_high, lhs_low);
2823 __ Beqz(TMP, label);
2824 break;
2825 case kCondNE:
2826 case kCondA: // > 0 if non-zero
2827 __ Or(TMP, lhs_high, lhs_low);
2828 __ Bnez(TMP, label);
2829 break;
2830 case kCondLT:
2831 __ Bltz(lhs_high, label);
2832 break;
2833 case kCondGE:
2834 __ Bgez(lhs_high, label);
2835 break;
2836 case kCondLE:
2837 __ Or(TMP, lhs_high, lhs_low);
2838 __ Sra(AT, lhs_high, 31);
2839 __ Bgeu(AT, TMP, label);
2840 break;
2841 case kCondGT:
2842 __ Or(TMP, lhs_high, lhs_low);
2843 __ Sra(AT, lhs_high, 31);
2844 __ Bltu(AT, TMP, label);
2845 break;
2846 case kCondB: // always false
2847 break;
2848 case kCondAE: // always true
2849 __ B(label);
2850 break;
2851 }
2852 } else if (use_imm) {
2853 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2854 switch (cond) {
2855 case kCondEQ:
2856 __ LoadConst32(TMP, imm_high);
2857 __ Xor(TMP, TMP, lhs_high);
2858 __ LoadConst32(AT, imm_low);
2859 __ Xor(AT, AT, lhs_low);
2860 __ Or(TMP, TMP, AT);
2861 __ Beqz(TMP, label);
2862 break;
2863 case kCondNE:
2864 __ LoadConst32(TMP, imm_high);
2865 __ Xor(TMP, TMP, lhs_high);
2866 __ LoadConst32(AT, imm_low);
2867 __ Xor(AT, AT, lhs_low);
2868 __ Or(TMP, TMP, AT);
2869 __ Bnez(TMP, label);
2870 break;
2871 case kCondLT:
2872 __ LoadConst32(TMP, imm_high);
2873 __ Blt(lhs_high, TMP, label);
2874 __ Slt(TMP, TMP, lhs_high);
2875 __ LoadConst32(AT, imm_low);
2876 __ Sltu(AT, lhs_low, AT);
2877 __ Blt(TMP, AT, label);
2878 break;
2879 case kCondGE:
2880 __ LoadConst32(TMP, imm_high);
2881 __ Blt(TMP, lhs_high, label);
2882 __ Slt(TMP, lhs_high, TMP);
2883 __ LoadConst32(AT, imm_low);
2884 __ Sltu(AT, lhs_low, AT);
2885 __ Or(TMP, TMP, AT);
2886 __ Beqz(TMP, label);
2887 break;
2888 case kCondLE:
2889 __ LoadConst32(TMP, imm_high);
2890 __ Blt(lhs_high, TMP, label);
2891 __ Slt(TMP, TMP, lhs_high);
2892 __ LoadConst32(AT, imm_low);
2893 __ Sltu(AT, AT, lhs_low);
2894 __ Or(TMP, TMP, AT);
2895 __ Beqz(TMP, label);
2896 break;
2897 case kCondGT:
2898 __ LoadConst32(TMP, imm_high);
2899 __ Blt(TMP, lhs_high, label);
2900 __ Slt(TMP, lhs_high, TMP);
2901 __ LoadConst32(AT, imm_low);
2902 __ Sltu(AT, AT, lhs_low);
2903 __ Blt(TMP, AT, label);
2904 break;
2905 case kCondB:
2906 __ LoadConst32(TMP, imm_high);
2907 __ Bltu(lhs_high, TMP, label);
2908 __ Sltu(TMP, TMP, lhs_high);
2909 __ LoadConst32(AT, imm_low);
2910 __ Sltu(AT, lhs_low, AT);
2911 __ Blt(TMP, AT, label);
2912 break;
2913 case kCondAE:
2914 __ LoadConst32(TMP, imm_high);
2915 __ Bltu(TMP, lhs_high, label);
2916 __ Sltu(TMP, lhs_high, TMP);
2917 __ LoadConst32(AT, imm_low);
2918 __ Sltu(AT, lhs_low, AT);
2919 __ Or(TMP, TMP, AT);
2920 __ Beqz(TMP, label);
2921 break;
2922 case kCondBE:
2923 __ LoadConst32(TMP, imm_high);
2924 __ Bltu(lhs_high, TMP, label);
2925 __ Sltu(TMP, TMP, lhs_high);
2926 __ LoadConst32(AT, imm_low);
2927 __ Sltu(AT, AT, lhs_low);
2928 __ Or(TMP, TMP, AT);
2929 __ Beqz(TMP, label);
2930 break;
2931 case kCondA:
2932 __ LoadConst32(TMP, imm_high);
2933 __ Bltu(TMP, lhs_high, label);
2934 __ Sltu(TMP, lhs_high, TMP);
2935 __ LoadConst32(AT, imm_low);
2936 __ Sltu(AT, AT, lhs_low);
2937 __ Blt(TMP, AT, label);
2938 break;
2939 }
2940 } else {
2941 switch (cond) {
2942 case kCondEQ:
2943 __ Xor(TMP, lhs_high, rhs_high);
2944 __ Xor(AT, lhs_low, rhs_low);
2945 __ Or(TMP, TMP, AT);
2946 __ Beqz(TMP, label);
2947 break;
2948 case kCondNE:
2949 __ Xor(TMP, lhs_high, rhs_high);
2950 __ Xor(AT, lhs_low, rhs_low);
2951 __ Or(TMP, TMP, AT);
2952 __ Bnez(TMP, label);
2953 break;
2954 case kCondLT:
2955 __ Blt(lhs_high, rhs_high, label);
2956 __ Slt(TMP, rhs_high, lhs_high);
2957 __ Sltu(AT, lhs_low, rhs_low);
2958 __ Blt(TMP, AT, label);
2959 break;
2960 case kCondGE:
2961 __ Blt(rhs_high, lhs_high, label);
2962 __ Slt(TMP, lhs_high, rhs_high);
2963 __ Sltu(AT, lhs_low, rhs_low);
2964 __ Or(TMP, TMP, AT);
2965 __ Beqz(TMP, label);
2966 break;
2967 case kCondLE:
2968 __ Blt(lhs_high, rhs_high, label);
2969 __ Slt(TMP, rhs_high, lhs_high);
2970 __ Sltu(AT, rhs_low, lhs_low);
2971 __ Or(TMP, TMP, AT);
2972 __ Beqz(TMP, label);
2973 break;
2974 case kCondGT:
2975 __ Blt(rhs_high, lhs_high, label);
2976 __ Slt(TMP, lhs_high, rhs_high);
2977 __ Sltu(AT, rhs_low, lhs_low);
2978 __ Blt(TMP, AT, label);
2979 break;
2980 case kCondB:
2981 __ Bltu(lhs_high, rhs_high, label);
2982 __ Sltu(TMP, rhs_high, lhs_high);
2983 __ Sltu(AT, lhs_low, rhs_low);
2984 __ Blt(TMP, AT, label);
2985 break;
2986 case kCondAE:
2987 __ Bltu(rhs_high, lhs_high, label);
2988 __ Sltu(TMP, lhs_high, rhs_high);
2989 __ Sltu(AT, lhs_low, rhs_low);
2990 __ Or(TMP, TMP, AT);
2991 __ Beqz(TMP, label);
2992 break;
2993 case kCondBE:
2994 __ Bltu(lhs_high, rhs_high, label);
2995 __ Sltu(TMP, rhs_high, lhs_high);
2996 __ Sltu(AT, rhs_low, lhs_low);
2997 __ Or(TMP, TMP, AT);
2998 __ Beqz(TMP, label);
2999 break;
3000 case kCondA:
3001 __ Bltu(rhs_high, lhs_high, label);
3002 __ Sltu(TMP, lhs_high, rhs_high);
3003 __ Sltu(AT, rhs_low, lhs_low);
3004 __ Blt(TMP, AT, label);
3005 break;
3006 }
3007 }
3008}
3009
3010void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3011 bool gt_bias,
3012 Primitive::Type type,
3013 LocationSummary* locations,
3014 MipsLabel* label) {
3015 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3016 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3017 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3018 if (type == Primitive::kPrimFloat) {
3019 if (isR6) {
3020 switch (cond) {
3021 case kCondEQ:
3022 __ CmpEqS(FTMP, lhs, rhs);
3023 __ Bc1nez(FTMP, label);
3024 break;
3025 case kCondNE:
3026 __ CmpEqS(FTMP, lhs, rhs);
3027 __ Bc1eqz(FTMP, label);
3028 break;
3029 case kCondLT:
3030 if (gt_bias) {
3031 __ CmpLtS(FTMP, lhs, rhs);
3032 } else {
3033 __ CmpUltS(FTMP, lhs, rhs);
3034 }
3035 __ Bc1nez(FTMP, label);
3036 break;
3037 case kCondLE:
3038 if (gt_bias) {
3039 __ CmpLeS(FTMP, lhs, rhs);
3040 } else {
3041 __ CmpUleS(FTMP, lhs, rhs);
3042 }
3043 __ Bc1nez(FTMP, label);
3044 break;
3045 case kCondGT:
3046 if (gt_bias) {
3047 __ CmpUltS(FTMP, rhs, lhs);
3048 } else {
3049 __ CmpLtS(FTMP, rhs, lhs);
3050 }
3051 __ Bc1nez(FTMP, label);
3052 break;
3053 case kCondGE:
3054 if (gt_bias) {
3055 __ CmpUleS(FTMP, rhs, lhs);
3056 } else {
3057 __ CmpLeS(FTMP, rhs, lhs);
3058 }
3059 __ Bc1nez(FTMP, label);
3060 break;
3061 default:
3062 LOG(FATAL) << "Unexpected non-floating-point condition";
3063 }
3064 } else {
3065 switch (cond) {
3066 case kCondEQ:
3067 __ CeqS(0, lhs, rhs);
3068 __ Bc1t(0, label);
3069 break;
3070 case kCondNE:
3071 __ CeqS(0, lhs, rhs);
3072 __ Bc1f(0, label);
3073 break;
3074 case kCondLT:
3075 if (gt_bias) {
3076 __ ColtS(0, lhs, rhs);
3077 } else {
3078 __ CultS(0, lhs, rhs);
3079 }
3080 __ Bc1t(0, label);
3081 break;
3082 case kCondLE:
3083 if (gt_bias) {
3084 __ ColeS(0, lhs, rhs);
3085 } else {
3086 __ CuleS(0, lhs, rhs);
3087 }
3088 __ Bc1t(0, label);
3089 break;
3090 case kCondGT:
3091 if (gt_bias) {
3092 __ CultS(0, rhs, lhs);
3093 } else {
3094 __ ColtS(0, rhs, lhs);
3095 }
3096 __ Bc1t(0, label);
3097 break;
3098 case kCondGE:
3099 if (gt_bias) {
3100 __ CuleS(0, rhs, lhs);
3101 } else {
3102 __ ColeS(0, rhs, lhs);
3103 }
3104 __ Bc1t(0, label);
3105 break;
3106 default:
3107 LOG(FATAL) << "Unexpected non-floating-point condition";
3108 }
3109 }
3110 } else {
3111 DCHECK_EQ(type, Primitive::kPrimDouble);
3112 if (isR6) {
3113 switch (cond) {
3114 case kCondEQ:
3115 __ CmpEqD(FTMP, lhs, rhs);
3116 __ Bc1nez(FTMP, label);
3117 break;
3118 case kCondNE:
3119 __ CmpEqD(FTMP, lhs, rhs);
3120 __ Bc1eqz(FTMP, label);
3121 break;
3122 case kCondLT:
3123 if (gt_bias) {
3124 __ CmpLtD(FTMP, lhs, rhs);
3125 } else {
3126 __ CmpUltD(FTMP, lhs, rhs);
3127 }
3128 __ Bc1nez(FTMP, label);
3129 break;
3130 case kCondLE:
3131 if (gt_bias) {
3132 __ CmpLeD(FTMP, lhs, rhs);
3133 } else {
3134 __ CmpUleD(FTMP, lhs, rhs);
3135 }
3136 __ Bc1nez(FTMP, label);
3137 break;
3138 case kCondGT:
3139 if (gt_bias) {
3140 __ CmpUltD(FTMP, rhs, lhs);
3141 } else {
3142 __ CmpLtD(FTMP, rhs, lhs);
3143 }
3144 __ Bc1nez(FTMP, label);
3145 break;
3146 case kCondGE:
3147 if (gt_bias) {
3148 __ CmpUleD(FTMP, rhs, lhs);
3149 } else {
3150 __ CmpLeD(FTMP, rhs, lhs);
3151 }
3152 __ Bc1nez(FTMP, label);
3153 break;
3154 default:
3155 LOG(FATAL) << "Unexpected non-floating-point condition";
3156 }
3157 } else {
3158 switch (cond) {
3159 case kCondEQ:
3160 __ CeqD(0, lhs, rhs);
3161 __ Bc1t(0, label);
3162 break;
3163 case kCondNE:
3164 __ CeqD(0, lhs, rhs);
3165 __ Bc1f(0, label);
3166 break;
3167 case kCondLT:
3168 if (gt_bias) {
3169 __ ColtD(0, lhs, rhs);
3170 } else {
3171 __ CultD(0, lhs, rhs);
3172 }
3173 __ Bc1t(0, label);
3174 break;
3175 case kCondLE:
3176 if (gt_bias) {
3177 __ ColeD(0, lhs, rhs);
3178 } else {
3179 __ CuleD(0, lhs, rhs);
3180 }
3181 __ Bc1t(0, label);
3182 break;
3183 case kCondGT:
3184 if (gt_bias) {
3185 __ CultD(0, rhs, lhs);
3186 } else {
3187 __ ColtD(0, rhs, lhs);
3188 }
3189 __ Bc1t(0, label);
3190 break;
3191 case kCondGE:
3192 if (gt_bias) {
3193 __ CuleD(0, rhs, lhs);
3194 } else {
3195 __ ColeD(0, rhs, lhs);
3196 }
3197 __ Bc1t(0, label);
3198 break;
3199 default:
3200 LOG(FATAL) << "Unexpected non-floating-point condition";
3201 }
3202 }
3203 }
3204}
3205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003206void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003207 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003209 MipsLabel* false_target) {
3210 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003211
David Brazdil0debae72015-11-12 18:37:00 +00003212 if (true_target == nullptr && false_target == nullptr) {
3213 // Nothing to do. The code always falls through.
3214 return;
3215 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003216 // Constant condition, statically compared against "true" (integer value 1).
3217 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003218 if (true_target != nullptr) {
3219 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003220 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003221 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003222 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003223 if (false_target != nullptr) {
3224 __ B(false_target);
3225 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226 }
David Brazdil0debae72015-11-12 18:37:00 +00003227 return;
3228 }
3229
3230 // The following code generates these patterns:
3231 // (1) true_target == nullptr && false_target != nullptr
3232 // - opposite condition true => branch to false_target
3233 // (2) true_target != nullptr && false_target == nullptr
3234 // - condition true => branch to true_target
3235 // (3) true_target != nullptr && false_target != nullptr
3236 // - condition true => branch to true_target
3237 // - branch to false_target
3238 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003239 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003240 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003241 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003242 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003243 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3244 } else {
3245 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3246 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 } else {
3248 // The condition instruction has not been materialized, use its inputs as
3249 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003250 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003251 Primitive::Type type = condition->InputAt(0)->GetType();
3252 LocationSummary* locations = cond->GetLocations();
3253 IfCondition if_cond = condition->GetCondition();
3254 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003255
David Brazdil0debae72015-11-12 18:37:00 +00003256 if (true_target == nullptr) {
3257 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003258 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003259 }
3260
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003261 switch (type) {
3262 default:
3263 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3264 break;
3265 case Primitive::kPrimLong:
3266 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3267 break;
3268 case Primitive::kPrimFloat:
3269 case Primitive::kPrimDouble:
3270 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3271 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272 }
3273 }
David Brazdil0debae72015-11-12 18:37:00 +00003274
3275 // If neither branch falls through (case 3), the conditional branch to `true_target`
3276 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3277 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003278 __ B(false_target);
3279 }
3280}
3281
3282void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003284 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003285 locations->SetInAt(0, Location::RequiresRegister());
3286 }
3287}
3288
3289void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003290 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3291 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3292 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3293 nullptr : codegen_->GetLabelOf(true_successor);
3294 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3295 nullptr : codegen_->GetLabelOf(false_successor);
3296 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003297}
3298
3299void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3300 LocationSummary* locations = new (GetGraph()->GetArena())
3301 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003302 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303 locations->SetInAt(0, Location::RequiresRegister());
3304 }
3305}
3306
3307void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003308 SlowPathCodeMIPS* slow_path =
3309 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003310 GenerateTestAndBranch(deoptimize,
3311 /* condition_input_index */ 0,
3312 slow_path->GetEntryLabel(),
3313 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314}
3315
David Brazdil74eb1b22015-12-14 11:44:01 +00003316void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3317 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3318 if (Primitive::IsFloatingPointType(select->GetType())) {
3319 locations->SetInAt(0, Location::RequiresFpuRegister());
3320 locations->SetInAt(1, Location::RequiresFpuRegister());
3321 } else {
3322 locations->SetInAt(0, Location::RequiresRegister());
3323 locations->SetInAt(1, Location::RequiresRegister());
3324 }
3325 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3326 locations->SetInAt(2, Location::RequiresRegister());
3327 }
3328 locations->SetOut(Location::SameAsFirstInput());
3329}
3330
3331void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3332 LocationSummary* locations = select->GetLocations();
3333 MipsLabel false_target;
3334 GenerateTestAndBranch(select,
3335 /* condition_input_index */ 2,
3336 /* true_target */ nullptr,
3337 &false_target);
3338 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3339 __ Bind(&false_target);
3340}
3341
David Srbecky0cf44932015-12-09 14:09:59 +00003342void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3343 new (GetGraph()->GetArena()) LocationSummary(info);
3344}
3345
David Srbeckyd28f4a02016-03-14 17:14:24 +00003346void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3347 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003348}
3349
3350void CodeGeneratorMIPS::GenerateNop() {
3351 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003352}
3353
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003354void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3355 Primitive::Type field_type = field_info.GetFieldType();
3356 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3357 bool generate_volatile = field_info.IsVolatile() && is_wide;
3358 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3359 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3360
3361 locations->SetInAt(0, Location::RequiresRegister());
3362 if (generate_volatile) {
3363 InvokeRuntimeCallingConvention calling_convention;
3364 // need A0 to hold base + offset
3365 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3366 if (field_type == Primitive::kPrimLong) {
3367 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3368 } else {
3369 locations->SetOut(Location::RequiresFpuRegister());
3370 // Need some temp core regs since FP results are returned in core registers
3371 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3372 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3373 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3374 }
3375 } else {
3376 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3377 locations->SetOut(Location::RequiresFpuRegister());
3378 } else {
3379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3380 }
3381 }
3382}
3383
3384void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3385 const FieldInfo& field_info,
3386 uint32_t dex_pc) {
3387 Primitive::Type type = field_info.GetFieldType();
3388 LocationSummary* locations = instruction->GetLocations();
3389 Register obj = locations->InAt(0).AsRegister<Register>();
3390 LoadOperandType load_type = kLoadUnsignedByte;
3391 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003392 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003393
3394 switch (type) {
3395 case Primitive::kPrimBoolean:
3396 load_type = kLoadUnsignedByte;
3397 break;
3398 case Primitive::kPrimByte:
3399 load_type = kLoadSignedByte;
3400 break;
3401 case Primitive::kPrimShort:
3402 load_type = kLoadSignedHalfword;
3403 break;
3404 case Primitive::kPrimChar:
3405 load_type = kLoadUnsignedHalfword;
3406 break;
3407 case Primitive::kPrimInt:
3408 case Primitive::kPrimFloat:
3409 case Primitive::kPrimNot:
3410 load_type = kLoadWord;
3411 break;
3412 case Primitive::kPrimLong:
3413 case Primitive::kPrimDouble:
3414 load_type = kLoadDoubleword;
3415 break;
3416 case Primitive::kPrimVoid:
3417 LOG(FATAL) << "Unreachable type " << type;
3418 UNREACHABLE();
3419 }
3420
3421 if (is_volatile && load_type == kLoadDoubleword) {
3422 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003423 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003424 // Do implicit Null check
3425 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3426 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3427 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3428 instruction,
3429 dex_pc,
3430 nullptr,
3431 IsDirectEntrypoint(kQuickA64Load));
3432 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3433 if (type == Primitive::kPrimDouble) {
3434 // Need to move to FP regs since FP results are returned in core registers.
3435 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3436 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003437 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3438 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003439 }
3440 } else {
3441 if (!Primitive::IsFloatingPointType(type)) {
3442 Register dst;
3443 if (type == Primitive::kPrimLong) {
3444 DCHECK(locations->Out().IsRegisterPair());
3445 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003446 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3447 if (obj == dst) {
3448 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3449 codegen_->MaybeRecordImplicitNullCheck(instruction);
3450 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3451 } else {
3452 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3453 codegen_->MaybeRecordImplicitNullCheck(instruction);
3454 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3455 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003456 } else {
3457 DCHECK(locations->Out().IsRegister());
3458 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003459 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003460 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003461 } else {
3462 DCHECK(locations->Out().IsFpuRegister());
3463 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3464 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003465 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003466 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003467 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003468 }
3469 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003470 // Longs are handled earlier.
3471 if (type != Primitive::kPrimLong) {
3472 codegen_->MaybeRecordImplicitNullCheck(instruction);
3473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003474 }
3475
3476 if (is_volatile) {
3477 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3478 }
3479}
3480
3481void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3482 Primitive::Type field_type = field_info.GetFieldType();
3483 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3484 bool generate_volatile = field_info.IsVolatile() && is_wide;
3485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3486 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
3487
3488 locations->SetInAt(0, Location::RequiresRegister());
3489 if (generate_volatile) {
3490 InvokeRuntimeCallingConvention calling_convention;
3491 // need A0 to hold base + offset
3492 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3493 if (field_type == Primitive::kPrimLong) {
3494 locations->SetInAt(1, Location::RegisterPairLocation(
3495 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3496 } else {
3497 locations->SetInAt(1, Location::RequiresFpuRegister());
3498 // Pass FP parameters in core registers.
3499 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3501 }
3502 } else {
3503 if (Primitive::IsFloatingPointType(field_type)) {
3504 locations->SetInAt(1, Location::RequiresFpuRegister());
3505 } else {
3506 locations->SetInAt(1, Location::RequiresRegister());
3507 }
3508 }
3509}
3510
3511void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3512 const FieldInfo& field_info,
3513 uint32_t dex_pc) {
3514 Primitive::Type type = field_info.GetFieldType();
3515 LocationSummary* locations = instruction->GetLocations();
3516 Register obj = locations->InAt(0).AsRegister<Register>();
3517 StoreOperandType store_type = kStoreByte;
3518 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003519 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003520
3521 switch (type) {
3522 case Primitive::kPrimBoolean:
3523 case Primitive::kPrimByte:
3524 store_type = kStoreByte;
3525 break;
3526 case Primitive::kPrimShort:
3527 case Primitive::kPrimChar:
3528 store_type = kStoreHalfword;
3529 break;
3530 case Primitive::kPrimInt:
3531 case Primitive::kPrimFloat:
3532 case Primitive::kPrimNot:
3533 store_type = kStoreWord;
3534 break;
3535 case Primitive::kPrimLong:
3536 case Primitive::kPrimDouble:
3537 store_type = kStoreDoubleword;
3538 break;
3539 case Primitive::kPrimVoid:
3540 LOG(FATAL) << "Unreachable type " << type;
3541 UNREACHABLE();
3542 }
3543
3544 if (is_volatile) {
3545 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3546 }
3547
3548 if (is_volatile && store_type == kStoreDoubleword) {
3549 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003550 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003551 // Do implicit Null check.
3552 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3553 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3554 if (type == Primitive::kPrimDouble) {
3555 // Pass FP parameters in core registers.
3556 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3557 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003558 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3559 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003560 }
3561 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3562 instruction,
3563 dex_pc,
3564 nullptr,
3565 IsDirectEntrypoint(kQuickA64Store));
3566 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3567 } else {
3568 if (!Primitive::IsFloatingPointType(type)) {
3569 Register src;
3570 if (type == Primitive::kPrimLong) {
3571 DCHECK(locations->InAt(1).IsRegisterPair());
3572 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003573 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3574 __ StoreToOffset(kStoreWord, src, obj, offset);
3575 codegen_->MaybeRecordImplicitNullCheck(instruction);
3576 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003577 } else {
3578 DCHECK(locations->InAt(1).IsRegister());
3579 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003580 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003581 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003582 } else {
3583 DCHECK(locations->InAt(1).IsFpuRegister());
3584 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3585 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003586 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003587 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003588 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003589 }
3590 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003591 // Longs are handled earlier.
3592 if (type != Primitive::kPrimLong) {
3593 codegen_->MaybeRecordImplicitNullCheck(instruction);
3594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 }
3596
3597 // TODO: memory barriers?
3598 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3599 DCHECK(locations->InAt(1).IsRegister());
3600 Register src = locations->InAt(1).AsRegister<Register>();
3601 codegen_->MarkGCCard(obj, src);
3602 }
3603
3604 if (is_volatile) {
3605 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3606 }
3607}
3608
3609void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3610 HandleFieldGet(instruction, instruction->GetFieldInfo());
3611}
3612
3613void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3614 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3615}
3616
3617void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3618 HandleFieldSet(instruction, instruction->GetFieldInfo());
3619}
3620
3621void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3622 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3623}
3624
3625void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3626 LocationSummary::CallKind call_kind =
3627 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3628 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3629 locations->SetInAt(0, Location::RequiresRegister());
3630 locations->SetInAt(1, Location::RequiresRegister());
3631 // The output does overlap inputs.
3632 // Note that TypeCheckSlowPathMIPS uses this register too.
3633 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3634}
3635
3636void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3637 LocationSummary* locations = instruction->GetLocations();
3638 Register obj = locations->InAt(0).AsRegister<Register>();
3639 Register cls = locations->InAt(1).AsRegister<Register>();
3640 Register out = locations->Out().AsRegister<Register>();
3641
3642 MipsLabel done;
3643
3644 // Return 0 if `obj` is null.
3645 // TODO: Avoid this check if we know `obj` is not null.
3646 __ Move(out, ZERO);
3647 __ Beqz(obj, &done);
3648
3649 // Compare the class of `obj` with `cls`.
3650 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3651 if (instruction->IsExactCheck()) {
3652 // Classes must be equal for the instanceof to succeed.
3653 __ Xor(out, out, cls);
3654 __ Sltiu(out, out, 1);
3655 } else {
3656 // If the classes are not equal, we go into a slow path.
3657 DCHECK(locations->OnlyCallsOnSlowPath());
3658 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3659 codegen_->AddSlowPath(slow_path);
3660 __ Bne(out, cls, slow_path->GetEntryLabel());
3661 __ LoadConst32(out, 1);
3662 __ Bind(slow_path->GetExitLabel());
3663 }
3664
3665 __ Bind(&done);
3666}
3667
3668void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3670 locations->SetOut(Location::ConstantLocation(constant));
3671}
3672
3673void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3674 // Will be generated at use site.
3675}
3676
3677void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3678 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3679 locations->SetOut(Location::ConstantLocation(constant));
3680}
3681
3682void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3683 // Will be generated at use site.
3684}
3685
3686void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3687 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3688 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3689}
3690
3691void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3692 HandleInvoke(invoke);
3693 // The register T0 is required to be used for the hidden argument in
3694 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3695 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3696}
3697
3698void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3699 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3700 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003701 Location receiver = invoke->GetLocations()->InAt(0);
3702 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3703 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3704
3705 // Set the hidden argument.
3706 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3707 invoke->GetDexMethodIndex());
3708
3709 // temp = object->GetClass();
3710 if (receiver.IsStackSlot()) {
3711 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3712 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3713 } else {
3714 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3715 }
3716 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003717 __ LoadFromOffset(kLoadWord, temp, temp,
3718 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3719 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
3720 invoke->GetImtIndex() % ImTable::kSize, kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003721 // temp = temp->GetImtEntryAt(method_offset);
3722 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3723 // T9 = temp->GetEntryPoint();
3724 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3725 // T9();
3726 __ Jalr(T9);
3727 __ Nop();
3728 DCHECK(!codegen_->IsLeafMethod());
3729 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3730}
3731
3732void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003733 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3734 if (intrinsic.TryDispatch(invoke)) {
3735 return;
3736 }
3737
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003738 HandleInvoke(invoke);
3739}
3740
3741void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003742 // Explicit clinit checks triggered by static invokes must have been pruned by
3743 // art::PrepareForRegisterAllocation.
3744 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003745
Chris Larsen701566a2015-10-27 15:29:13 -07003746 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3747 if (intrinsic.TryDispatch(invoke)) {
3748 return;
3749 }
3750
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003751 HandleInvoke(invoke);
3752}
3753
Chris Larsen701566a2015-10-27 15:29:13 -07003754static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003756 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3757 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003758 return true;
3759 }
3760 return false;
3761}
3762
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003763HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3764 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3765 // TODO: Implement other kinds.
3766 return HLoadString::LoadKind::kDexCacheViaMethod;
3767}
3768
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003769HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
3770 HLoadClass::LoadKind desired_class_load_kind) {
3771 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3772 // TODO: Implement other kinds.
3773 return HLoadClass::LoadKind::kDexCacheViaMethod;
3774}
3775
Vladimir Markodc151b22015-10-15 18:02:30 +01003776HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3777 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3778 MethodReference target_method ATTRIBUTE_UNUSED) {
3779 switch (desired_dispatch_info.method_load_kind) {
3780 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3781 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3782 // TODO: Implement these types. For the moment, we fall back to kDexCacheViaMethod.
3783 return HInvokeStaticOrDirect::DispatchInfo {
3784 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod,
3785 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3786 0u,
3787 0u
3788 };
3789 default:
3790 break;
3791 }
3792 switch (desired_dispatch_info.code_ptr_location) {
3793 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3794 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3795 // TODO: Implement these types. For the moment, we fall back to kCallArtMethod.
3796 return HInvokeStaticOrDirect::DispatchInfo {
3797 desired_dispatch_info.method_load_kind,
3798 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3799 desired_dispatch_info.method_load_data,
3800 0u
3801 };
3802 default:
3803 return desired_dispatch_info;
3804 }
3805}
3806
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003807void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3808 // All registers are assumed to be correctly set up per the calling convention.
3809
3810 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3811 switch (invoke->GetMethodLoadKind()) {
3812 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3813 // temp = thread->string_init_entrypoint
3814 __ LoadFromOffset(kLoadWord,
3815 temp.AsRegister<Register>(),
3816 TR,
3817 invoke->GetStringInitOffset());
3818 break;
3819 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003820 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821 break;
3822 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3823 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3824 break;
3825 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01003827 // TODO: Implement these types.
3828 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3829 LOG(FATAL) << "Unsupported";
3830 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003831 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003832 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833 Register reg = temp.AsRegister<Register>();
3834 Register method_reg;
3835 if (current_method.IsRegister()) {
3836 method_reg = current_method.AsRegister<Register>();
3837 } else {
3838 // TODO: use the appropriate DCHECK() here if possible.
3839 // DCHECK(invoke->GetLocations()->Intrinsified());
3840 DCHECK(!current_method.IsValid());
3841 method_reg = reg;
3842 __ Lw(reg, SP, kCurrentMethodStackOffset);
3843 }
3844
3845 // temp = temp->dex_cache_resolved_methods_;
3846 __ LoadFromOffset(kLoadWord,
3847 reg,
3848 method_reg,
3849 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01003850 // temp = temp[index_in_cache];
3851 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3852 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 __ LoadFromOffset(kLoadWord,
3854 reg,
3855 reg,
3856 CodeGenerator::GetCachePointerOffset(index_in_cache));
3857 break;
3858 }
3859 }
3860
3861 switch (invoke->GetCodePtrLocation()) {
3862 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3863 __ Jalr(&frame_entry_label_, T9);
3864 break;
3865 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3866 // LR = invoke->GetDirectCodePtr();
3867 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3868 // LR()
3869 __ Jalr(T9);
3870 __ Nop();
3871 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003872 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Vladimir Markodc151b22015-10-15 18:02:30 +01003873 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3874 // TODO: Implement these types.
3875 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3876 LOG(FATAL) << "Unsupported";
3877 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003878 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3879 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01003880 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003881 T9,
3882 callee_method.AsRegister<Register>(),
3883 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3884 kMipsWordSize).Int32Value());
3885 // T9()
3886 __ Jalr(T9);
3887 __ Nop();
3888 break;
3889 }
3890 DCHECK(!IsLeafMethod());
3891}
3892
3893void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003894 // Explicit clinit checks triggered by static invokes must have been pruned by
3895 // art::PrepareForRegisterAllocation.
3896 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003897
3898 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3899 return;
3900 }
3901
3902 LocationSummary* locations = invoke->GetLocations();
3903 codegen_->GenerateStaticOrDirectCall(invoke,
3904 locations->HasTemps()
3905 ? locations->GetTemp(0)
3906 : Location::NoLocation());
3907 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3908}
3909
Chris Larsen3acee732015-11-18 13:31:08 -08003910void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003911 LocationSummary* locations = invoke->GetLocations();
3912 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08003913 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3915 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3916 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3917 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3918
3919 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08003920 DCHECK(receiver.IsRegister());
3921 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3922 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003923 // temp = temp->GetMethodAt(method_offset);
3924 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3925 // T9 = temp->GetEntryPoint();
3926 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3927 // T9();
3928 __ Jalr(T9);
3929 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08003930}
3931
3932void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3933 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3934 return;
3935 }
3936
3937 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003938 DCHECK(!codegen_->IsLeafMethod());
3939 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3940}
3941
3942void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01003943 InvokeRuntimeCallingConvention calling_convention;
3944 CodeGenerator::CreateLoadClassLocationSummary(
3945 cls,
3946 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
3947 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948}
3949
3950void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3951 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01003952 if (cls->NeedsAccessCheck()) {
3953 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
3954 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3955 cls,
3956 cls->GetDexPc(),
3957 nullptr,
3958 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00003959 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01003960 return;
3961 }
3962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003963 Register out = locations->Out().AsRegister<Register>();
3964 Register current_method = locations->InAt(0).AsRegister<Register>();
3965 if (cls->IsReferrersClass()) {
3966 DCHECK(!cls->CanCallRuntime());
3967 DCHECK(!cls->MustGenerateClinitCheck());
3968 __ LoadFromOffset(kLoadWord, out, current_method,
3969 ArtMethod::DeclaringClassOffset().Int32Value());
3970 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 __ LoadFromOffset(kLoadWord, out, current_method,
3972 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3973 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003974
3975 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3976 DCHECK(cls->CanCallRuntime());
3977 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3978 cls,
3979 cls,
3980 cls->GetDexPc(),
3981 cls->MustGenerateClinitCheck());
3982 codegen_->AddSlowPath(slow_path);
3983 if (!cls->IsInDexCache()) {
3984 __ Beqz(out, slow_path->GetEntryLabel());
3985 }
3986 if (cls->MustGenerateClinitCheck()) {
3987 GenerateClassInitializationCheck(slow_path, out);
3988 } else {
3989 __ Bind(slow_path->GetExitLabel());
3990 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003991 }
3992 }
3993}
3994
3995static int32_t GetExceptionTlsOffset() {
3996 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3997}
3998
3999void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4000 LocationSummary* locations =
4001 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4002 locations->SetOut(Location::RequiresRegister());
4003}
4004
4005void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4006 Register out = load->GetLocations()->Out().AsRegister<Register>();
4007 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4008}
4009
4010void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4011 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4012}
4013
4014void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4015 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4016}
4017
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004018void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004019 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4020 ? LocationSummary::kCallOnSlowPath
4021 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004022 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 locations->SetInAt(0, Location::RequiresRegister());
4024 locations->SetOut(Location::RequiresRegister());
4025}
4026
4027void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004028 LocationSummary* locations = load->GetLocations();
4029 Register out = locations->Out().AsRegister<Register>();
4030 Register current_method = locations->InAt(0).AsRegister<Register>();
4031 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4032 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4033 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004034
4035 if (!load->IsInDexCache()) {
4036 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4037 codegen_->AddSlowPath(slow_path);
4038 __ Beqz(out, slow_path->GetEntryLabel());
4039 __ Bind(slow_path->GetExitLabel());
4040 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041}
4042
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004043void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4045 locations->SetOut(Location::ConstantLocation(constant));
4046}
4047
4048void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4049 // Will be generated at use site.
4050}
4051
4052void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4055 InvokeRuntimeCallingConvention calling_convention;
4056 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4057}
4058
4059void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4060 if (instruction->IsEnter()) {
4061 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4062 instruction,
4063 instruction->GetDexPc(),
4064 nullptr,
4065 IsDirectEntrypoint(kQuickLockObject));
4066 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4067 } else {
4068 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4069 instruction,
4070 instruction->GetDexPc(),
4071 nullptr,
4072 IsDirectEntrypoint(kQuickUnlockObject));
4073 }
4074 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4075}
4076
4077void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4078 LocationSummary* locations =
4079 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4080 switch (mul->GetResultType()) {
4081 case Primitive::kPrimInt:
4082 case Primitive::kPrimLong:
4083 locations->SetInAt(0, Location::RequiresRegister());
4084 locations->SetInAt(1, Location::RequiresRegister());
4085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4086 break;
4087
4088 case Primitive::kPrimFloat:
4089 case Primitive::kPrimDouble:
4090 locations->SetInAt(0, Location::RequiresFpuRegister());
4091 locations->SetInAt(1, Location::RequiresFpuRegister());
4092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4093 break;
4094
4095 default:
4096 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4097 }
4098}
4099
4100void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4101 Primitive::Type type = instruction->GetType();
4102 LocationSummary* locations = instruction->GetLocations();
4103 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4104
4105 switch (type) {
4106 case Primitive::kPrimInt: {
4107 Register dst = locations->Out().AsRegister<Register>();
4108 Register lhs = locations->InAt(0).AsRegister<Register>();
4109 Register rhs = locations->InAt(1).AsRegister<Register>();
4110
4111 if (isR6) {
4112 __ MulR6(dst, lhs, rhs);
4113 } else {
4114 __ MulR2(dst, lhs, rhs);
4115 }
4116 break;
4117 }
4118 case Primitive::kPrimLong: {
4119 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4120 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4121 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4122 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4123 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4124 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4125
4126 // Extra checks to protect caused by the existance of A1_A2.
4127 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4128 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4129 DCHECK_NE(dst_high, lhs_low);
4130 DCHECK_NE(dst_high, rhs_low);
4131
4132 // A_B * C_D
4133 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4134 // dst_lo: [ low(B*D) ]
4135 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4136
4137 if (isR6) {
4138 __ MulR6(TMP, lhs_high, rhs_low);
4139 __ MulR6(dst_high, lhs_low, rhs_high);
4140 __ Addu(dst_high, dst_high, TMP);
4141 __ MuhuR6(TMP, lhs_low, rhs_low);
4142 __ Addu(dst_high, dst_high, TMP);
4143 __ MulR6(dst_low, lhs_low, rhs_low);
4144 } else {
4145 __ MulR2(TMP, lhs_high, rhs_low);
4146 __ MulR2(dst_high, lhs_low, rhs_high);
4147 __ Addu(dst_high, dst_high, TMP);
4148 __ MultuR2(lhs_low, rhs_low);
4149 __ Mfhi(TMP);
4150 __ Addu(dst_high, dst_high, TMP);
4151 __ Mflo(dst_low);
4152 }
4153 break;
4154 }
4155 case Primitive::kPrimFloat:
4156 case Primitive::kPrimDouble: {
4157 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4158 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4159 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4160 if (type == Primitive::kPrimFloat) {
4161 __ MulS(dst, lhs, rhs);
4162 } else {
4163 __ MulD(dst, lhs, rhs);
4164 }
4165 break;
4166 }
4167 default:
4168 LOG(FATAL) << "Unexpected mul type " << type;
4169 }
4170}
4171
4172void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4173 LocationSummary* locations =
4174 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4175 switch (neg->GetResultType()) {
4176 case Primitive::kPrimInt:
4177 case Primitive::kPrimLong:
4178 locations->SetInAt(0, Location::RequiresRegister());
4179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4180 break;
4181
4182 case Primitive::kPrimFloat:
4183 case Primitive::kPrimDouble:
4184 locations->SetInAt(0, Location::RequiresFpuRegister());
4185 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4186 break;
4187
4188 default:
4189 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4190 }
4191}
4192
4193void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4194 Primitive::Type type = instruction->GetType();
4195 LocationSummary* locations = instruction->GetLocations();
4196
4197 switch (type) {
4198 case Primitive::kPrimInt: {
4199 Register dst = locations->Out().AsRegister<Register>();
4200 Register src = locations->InAt(0).AsRegister<Register>();
4201 __ Subu(dst, ZERO, src);
4202 break;
4203 }
4204 case Primitive::kPrimLong: {
4205 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4206 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4207 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4208 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4209 __ Subu(dst_low, ZERO, src_low);
4210 __ Sltu(TMP, ZERO, dst_low);
4211 __ Subu(dst_high, ZERO, src_high);
4212 __ Subu(dst_high, dst_high, TMP);
4213 break;
4214 }
4215 case Primitive::kPrimFloat:
4216 case Primitive::kPrimDouble: {
4217 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4218 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4219 if (type == Primitive::kPrimFloat) {
4220 __ NegS(dst, src);
4221 } else {
4222 __ NegD(dst, src);
4223 }
4224 break;
4225 }
4226 default:
4227 LOG(FATAL) << "Unexpected neg type " << type;
4228 }
4229}
4230
4231void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4232 LocationSummary* locations =
4233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4234 InvokeRuntimeCallingConvention calling_convention;
4235 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4236 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4237 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4238 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4239}
4240
4241void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4242 InvokeRuntimeCallingConvention calling_convention;
4243 Register current_method_register = calling_convention.GetRegisterAt(2);
4244 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4245 // Move an uint16_t value to a register.
4246 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4247 codegen_->InvokeRuntime(
4248 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4249 instruction,
4250 instruction->GetDexPc(),
4251 nullptr,
4252 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4253 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4254 void*, uint32_t, int32_t, ArtMethod*>();
4255}
4256
4257void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4258 LocationSummary* locations =
4259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4260 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004261 if (instruction->IsStringAlloc()) {
4262 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4263 } else {
4264 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4265 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4266 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004267 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4268}
4269
4270void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004271 if (instruction->IsStringAlloc()) {
4272 // String is allocated through StringFactory. Call NewEmptyString entry point.
4273 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4274 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4275 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4276 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4277 __ Jalr(T9);
4278 __ Nop();
4279 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4280 } else {
4281 codegen_->InvokeRuntime(
4282 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4283 instruction,
4284 instruction->GetDexPc(),
4285 nullptr,
4286 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4287 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4288 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004289}
4290
4291void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4292 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4293 locations->SetInAt(0, Location::RequiresRegister());
4294 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4295}
4296
4297void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4298 Primitive::Type type = instruction->GetType();
4299 LocationSummary* locations = instruction->GetLocations();
4300
4301 switch (type) {
4302 case Primitive::kPrimInt: {
4303 Register dst = locations->Out().AsRegister<Register>();
4304 Register src = locations->InAt(0).AsRegister<Register>();
4305 __ Nor(dst, src, ZERO);
4306 break;
4307 }
4308
4309 case Primitive::kPrimLong: {
4310 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4311 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4312 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4313 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4314 __ Nor(dst_high, src_high, ZERO);
4315 __ Nor(dst_low, src_low, ZERO);
4316 break;
4317 }
4318
4319 default:
4320 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4321 }
4322}
4323
4324void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4326 locations->SetInAt(0, Location::RequiresRegister());
4327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4328}
4329
4330void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4331 LocationSummary* locations = instruction->GetLocations();
4332 __ Xori(locations->Out().AsRegister<Register>(),
4333 locations->InAt(0).AsRegister<Register>(),
4334 1);
4335}
4336
4337void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4338 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4339 ? LocationSummary::kCallOnSlowPath
4340 : LocationSummary::kNoCall;
4341 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4342 locations->SetInAt(0, Location::RequiresRegister());
4343 if (instruction->HasUses()) {
4344 locations->SetOut(Location::SameAsFirstInput());
4345 }
4346}
4347
Calin Juravle2ae48182016-03-16 14:05:09 +00004348void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4349 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004350 return;
4351 }
4352 Location obj = instruction->GetLocations()->InAt(0);
4353
4354 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004355 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004356}
4357
Calin Juravle2ae48182016-03-16 14:05:09 +00004358void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004359 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004360 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004361
4362 Location obj = instruction->GetLocations()->InAt(0);
4363
4364 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4365}
4366
4367void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004368 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004369}
4370
4371void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4372 HandleBinaryOp(instruction);
4373}
4374
4375void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4376 HandleBinaryOp(instruction);
4377}
4378
4379void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4380 LOG(FATAL) << "Unreachable";
4381}
4382
4383void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4384 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4385}
4386
4387void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4389 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4390 if (location.IsStackSlot()) {
4391 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4392 } else if (location.IsDoubleStackSlot()) {
4393 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4394 }
4395 locations->SetOut(location);
4396}
4397
4398void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4399 ATTRIBUTE_UNUSED) {
4400 // Nothing to do, the parameter is already at its location.
4401}
4402
4403void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4404 LocationSummary* locations =
4405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4406 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4407}
4408
4409void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4410 ATTRIBUTE_UNUSED) {
4411 // Nothing to do, the method is already at its location.
4412}
4413
4414void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004416 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004417 locations->SetInAt(i, Location::Any());
4418 }
4419 locations->SetOut(Location::Any());
4420}
4421
4422void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4423 LOG(FATAL) << "Unreachable";
4424}
4425
4426void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4427 Primitive::Type type = rem->GetResultType();
4428 LocationSummary::CallKind call_kind =
4429 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
4430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4431
4432 switch (type) {
4433 case Primitive::kPrimInt:
4434 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004435 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4437 break;
4438
4439 case Primitive::kPrimLong: {
4440 InvokeRuntimeCallingConvention calling_convention;
4441 locations->SetInAt(0, Location::RegisterPairLocation(
4442 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4443 locations->SetInAt(1, Location::RegisterPairLocation(
4444 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4445 locations->SetOut(calling_convention.GetReturnLocation(type));
4446 break;
4447 }
4448
4449 case Primitive::kPrimFloat:
4450 case Primitive::kPrimDouble: {
4451 InvokeRuntimeCallingConvention calling_convention;
4452 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4453 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4454 locations->SetOut(calling_convention.GetReturnLocation(type));
4455 break;
4456 }
4457
4458 default:
4459 LOG(FATAL) << "Unexpected rem type " << type;
4460 }
4461}
4462
4463void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4464 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004465
4466 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004467 case Primitive::kPrimInt:
4468 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004469 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004470 case Primitive::kPrimLong: {
4471 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4472 instruction,
4473 instruction->GetDexPc(),
4474 nullptr,
4475 IsDirectEntrypoint(kQuickLmod));
4476 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4477 break;
4478 }
4479 case Primitive::kPrimFloat: {
4480 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4481 instruction, instruction->GetDexPc(),
4482 nullptr,
4483 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004484 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004485 break;
4486 }
4487 case Primitive::kPrimDouble: {
4488 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4489 instruction, instruction->GetDexPc(),
4490 nullptr,
4491 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004492 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004493 break;
4494 }
4495 default:
4496 LOG(FATAL) << "Unexpected rem type " << type;
4497 }
4498}
4499
4500void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4501 memory_barrier->SetLocations(nullptr);
4502}
4503
4504void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4505 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4506}
4507
4508void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4510 Primitive::Type return_type = ret->InputAt(0)->GetType();
4511 locations->SetInAt(0, MipsReturnLocation(return_type));
4512}
4513
4514void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4515 codegen_->GenerateFrameExit();
4516}
4517
4518void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4519 ret->SetLocations(nullptr);
4520}
4521
4522void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4523 codegen_->GenerateFrameExit();
4524}
4525
Alexey Frunze92d90602015-12-18 18:16:36 -08004526void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4527 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004528}
4529
Alexey Frunze92d90602015-12-18 18:16:36 -08004530void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4531 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004532}
4533
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004534void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4535 HandleShift(shl);
4536}
4537
4538void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4539 HandleShift(shl);
4540}
4541
4542void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4543 HandleShift(shr);
4544}
4545
4546void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4547 HandleShift(shr);
4548}
4549
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4551 HandleBinaryOp(instruction);
4552}
4553
4554void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4555 HandleBinaryOp(instruction);
4556}
4557
4558void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4559 HandleFieldGet(instruction, instruction->GetFieldInfo());
4560}
4561
4562void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4563 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4564}
4565
4566void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4567 HandleFieldSet(instruction, instruction->GetFieldInfo());
4568}
4569
4570void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4571 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4572}
4573
4574void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4575 HUnresolvedInstanceFieldGet* instruction) {
4576 FieldAccessCallingConventionMIPS calling_convention;
4577 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4578 instruction->GetFieldType(),
4579 calling_convention);
4580}
4581
4582void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4583 HUnresolvedInstanceFieldGet* instruction) {
4584 FieldAccessCallingConventionMIPS calling_convention;
4585 codegen_->GenerateUnresolvedFieldAccess(instruction,
4586 instruction->GetFieldType(),
4587 instruction->GetFieldIndex(),
4588 instruction->GetDexPc(),
4589 calling_convention);
4590}
4591
4592void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4593 HUnresolvedInstanceFieldSet* instruction) {
4594 FieldAccessCallingConventionMIPS calling_convention;
4595 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4596 instruction->GetFieldType(),
4597 calling_convention);
4598}
4599
4600void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4601 HUnresolvedInstanceFieldSet* instruction) {
4602 FieldAccessCallingConventionMIPS calling_convention;
4603 codegen_->GenerateUnresolvedFieldAccess(instruction,
4604 instruction->GetFieldType(),
4605 instruction->GetFieldIndex(),
4606 instruction->GetDexPc(),
4607 calling_convention);
4608}
4609
4610void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4611 HUnresolvedStaticFieldGet* instruction) {
4612 FieldAccessCallingConventionMIPS calling_convention;
4613 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4614 instruction->GetFieldType(),
4615 calling_convention);
4616}
4617
4618void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4619 HUnresolvedStaticFieldGet* instruction) {
4620 FieldAccessCallingConventionMIPS calling_convention;
4621 codegen_->GenerateUnresolvedFieldAccess(instruction,
4622 instruction->GetFieldType(),
4623 instruction->GetFieldIndex(),
4624 instruction->GetDexPc(),
4625 calling_convention);
4626}
4627
4628void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4629 HUnresolvedStaticFieldSet* instruction) {
4630 FieldAccessCallingConventionMIPS calling_convention;
4631 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4632 instruction->GetFieldType(),
4633 calling_convention);
4634}
4635
4636void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4637 HUnresolvedStaticFieldSet* instruction) {
4638 FieldAccessCallingConventionMIPS calling_convention;
4639 codegen_->GenerateUnresolvedFieldAccess(instruction,
4640 instruction->GetFieldType(),
4641 instruction->GetFieldIndex(),
4642 instruction->GetDexPc(),
4643 calling_convention);
4644}
4645
4646void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4648}
4649
4650void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4651 HBasicBlock* block = instruction->GetBlock();
4652 if (block->GetLoopInformation() != nullptr) {
4653 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4654 // The back edge will generate the suspend check.
4655 return;
4656 }
4657 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4658 // The goto will generate the suspend check.
4659 return;
4660 }
4661 GenerateSuspendCheck(instruction, nullptr);
4662}
4663
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004664void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4665 LocationSummary* locations =
4666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4667 InvokeRuntimeCallingConvention calling_convention;
4668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4669}
4670
4671void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4672 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4673 instruction,
4674 instruction->GetDexPc(),
4675 nullptr,
4676 IsDirectEntrypoint(kQuickDeliverException));
4677 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4678}
4679
4680void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4681 Primitive::Type input_type = conversion->GetInputType();
4682 Primitive::Type result_type = conversion->GetResultType();
4683 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004684 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004685
4686 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4687 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4688 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4689 }
4690
4691 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004692 if (!isR6 &&
4693 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4694 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004695 call_kind = LocationSummary::kCall;
4696 }
4697
4698 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4699
4700 if (call_kind == LocationSummary::kNoCall) {
4701 if (Primitive::IsFloatingPointType(input_type)) {
4702 locations->SetInAt(0, Location::RequiresFpuRegister());
4703 } else {
4704 locations->SetInAt(0, Location::RequiresRegister());
4705 }
4706
4707 if (Primitive::IsFloatingPointType(result_type)) {
4708 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4709 } else {
4710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4711 }
4712 } else {
4713 InvokeRuntimeCallingConvention calling_convention;
4714
4715 if (Primitive::IsFloatingPointType(input_type)) {
4716 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4717 } else {
4718 DCHECK_EQ(input_type, Primitive::kPrimLong);
4719 locations->SetInAt(0, Location::RegisterPairLocation(
4720 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4721 }
4722
4723 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4724 }
4725}
4726
4727void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4728 LocationSummary* locations = conversion->GetLocations();
4729 Primitive::Type result_type = conversion->GetResultType();
4730 Primitive::Type input_type = conversion->GetInputType();
4731 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004732 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004733
4734 DCHECK_NE(input_type, result_type);
4735
4736 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4737 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4738 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4739 Register src = locations->InAt(0).AsRegister<Register>();
4740
Alexey Frunzea871ef12016-06-27 15:20:11 -07004741 if (dst_low != src) {
4742 __ Move(dst_low, src);
4743 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004744 __ Sra(dst_high, src, 31);
4745 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4746 Register dst = locations->Out().AsRegister<Register>();
4747 Register src = (input_type == Primitive::kPrimLong)
4748 ? locations->InAt(0).AsRegisterPairLow<Register>()
4749 : locations->InAt(0).AsRegister<Register>();
4750
4751 switch (result_type) {
4752 case Primitive::kPrimChar:
4753 __ Andi(dst, src, 0xFFFF);
4754 break;
4755 case Primitive::kPrimByte:
4756 if (has_sign_extension) {
4757 __ Seb(dst, src);
4758 } else {
4759 __ Sll(dst, src, 24);
4760 __ Sra(dst, dst, 24);
4761 }
4762 break;
4763 case Primitive::kPrimShort:
4764 if (has_sign_extension) {
4765 __ Seh(dst, src);
4766 } else {
4767 __ Sll(dst, src, 16);
4768 __ Sra(dst, dst, 16);
4769 }
4770 break;
4771 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07004772 if (dst != src) {
4773 __ Move(dst, src);
4774 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004775 break;
4776
4777 default:
4778 LOG(FATAL) << "Unexpected type conversion from " << input_type
4779 << " to " << result_type;
4780 }
4781 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004782 if (input_type == Primitive::kPrimLong) {
4783 if (isR6) {
4784 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4785 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4786 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4787 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4788 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4789 __ Mtc1(src_low, FTMP);
4790 __ Mthc1(src_high, FTMP);
4791 if (result_type == Primitive::kPrimFloat) {
4792 __ Cvtsl(dst, FTMP);
4793 } else {
4794 __ Cvtdl(dst, FTMP);
4795 }
4796 } else {
4797 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4798 : QUICK_ENTRY_POINT(pL2d);
4799 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4800 : IsDirectEntrypoint(kQuickL2d);
4801 codegen_->InvokeRuntime(entry_offset,
4802 conversion,
4803 conversion->GetDexPc(),
4804 nullptr,
4805 direct);
4806 if (result_type == Primitive::kPrimFloat) {
4807 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4808 } else {
4809 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4810 }
4811 }
4812 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004813 Register src = locations->InAt(0).AsRegister<Register>();
4814 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4815 __ Mtc1(src, FTMP);
4816 if (result_type == Primitive::kPrimFloat) {
4817 __ Cvtsw(dst, FTMP);
4818 } else {
4819 __ Cvtdw(dst, FTMP);
4820 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004821 }
4822 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4823 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004824 if (result_type == Primitive::kPrimLong) {
4825 if (isR6) {
4826 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4827 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4828 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4829 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4830 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4831 MipsLabel truncate;
4832 MipsLabel done;
4833
4834 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
4835 // value when the input is either a NaN or is outside of the range of the output type
4836 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
4837 // the same result.
4838 //
4839 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
4840 // value of the output type if the input is outside of the range after the truncation or
4841 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
4842 // results. This matches the desired float/double-to-int/long conversion exactly.
4843 //
4844 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
4845 //
4846 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4847 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4848 // even though it must be NAN2008=1 on R6.
4849 //
4850 // The code takes care of the different behaviors by first comparing the input to the
4851 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
4852 // If the input is greater than or equal to the minimum, it procedes to the truncate
4853 // instruction, which will handle such an input the same way irrespective of NAN2008.
4854 // Otherwise the input is compared to itself to determine whether it is a NaN or not
4855 // in order to return either zero or the minimum value.
4856 //
4857 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4858 // truncate instruction for MIPS64R6.
4859 if (input_type == Primitive::kPrimFloat) {
4860 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
4861 __ LoadConst32(TMP, min_val);
4862 __ Mtc1(TMP, FTMP);
4863 __ CmpLeS(FTMP, FTMP, src);
4864 } else {
4865 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
4866 __ LoadConst32(TMP, High32Bits(min_val));
4867 __ Mtc1(ZERO, FTMP);
4868 __ Mthc1(TMP, FTMP);
4869 __ CmpLeD(FTMP, FTMP, src);
4870 }
4871
4872 __ Bc1nez(FTMP, &truncate);
4873
4874 if (input_type == Primitive::kPrimFloat) {
4875 __ CmpEqS(FTMP, src, src);
4876 } else {
4877 __ CmpEqD(FTMP, src, src);
4878 }
4879 __ Move(dst_low, ZERO);
4880 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
4881 __ Mfc1(TMP, FTMP);
4882 __ And(dst_high, dst_high, TMP);
4883
4884 __ B(&done);
4885
4886 __ Bind(&truncate);
4887
4888 if (input_type == Primitive::kPrimFloat) {
4889 __ TruncLS(FTMP, src);
4890 } else {
4891 __ TruncLD(FTMP, src);
4892 }
4893 __ Mfc1(dst_low, FTMP);
4894 __ Mfhc1(dst_high, FTMP);
4895
4896 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004897 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004898 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
4899 : QUICK_ENTRY_POINT(pD2l);
4900 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
4901 : IsDirectEntrypoint(kQuickD2l);
4902 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
4903 if (input_type == Primitive::kPrimFloat) {
4904 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4905 } else {
4906 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4907 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004908 }
4909 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004910 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4911 Register dst = locations->Out().AsRegister<Register>();
4912 MipsLabel truncate;
4913 MipsLabel done;
4914
4915 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
4916 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
4917 // even though it must be NAN2008=1 on R6.
4918 //
4919 // For details see the large comment above for the truncation of float/double to long on R6.
4920 //
4921 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
4922 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004923 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004924 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
4925 __ LoadConst32(TMP, min_val);
4926 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004927 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004928 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
4929 __ LoadConst32(TMP, High32Bits(min_val));
4930 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07004931 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004932 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004933
4934 if (isR6) {
4935 if (input_type == Primitive::kPrimFloat) {
4936 __ CmpLeS(FTMP, FTMP, src);
4937 } else {
4938 __ CmpLeD(FTMP, FTMP, src);
4939 }
4940 __ Bc1nez(FTMP, &truncate);
4941
4942 if (input_type == Primitive::kPrimFloat) {
4943 __ CmpEqS(FTMP, src, src);
4944 } else {
4945 __ CmpEqD(FTMP, src, src);
4946 }
4947 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4948 __ Mfc1(TMP, FTMP);
4949 __ And(dst, dst, TMP);
4950 } else {
4951 if (input_type == Primitive::kPrimFloat) {
4952 __ ColeS(0, FTMP, src);
4953 } else {
4954 __ ColeD(0, FTMP, src);
4955 }
4956 __ Bc1t(0, &truncate);
4957
4958 if (input_type == Primitive::kPrimFloat) {
4959 __ CeqS(0, src, src);
4960 } else {
4961 __ CeqD(0, src, src);
4962 }
4963 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
4964 __ Movf(dst, ZERO, 0);
4965 }
4966
4967 __ B(&done);
4968
4969 __ Bind(&truncate);
4970
4971 if (input_type == Primitive::kPrimFloat) {
4972 __ TruncWS(FTMP, src);
4973 } else {
4974 __ TruncWD(FTMP, src);
4975 }
4976 __ Mfc1(dst, FTMP);
4977
4978 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004979 }
4980 } else if (Primitive::IsFloatingPointType(result_type) &&
4981 Primitive::IsFloatingPointType(input_type)) {
4982 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4983 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4984 if (result_type == Primitive::kPrimFloat) {
4985 __ Cvtsd(dst, src);
4986 } else {
4987 __ Cvtds(dst, src);
4988 }
4989 } else {
4990 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4991 << " to " << result_type;
4992 }
4993}
4994
4995void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4996 HandleShift(ushr);
4997}
4998
4999void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5000 HandleShift(ushr);
5001}
5002
5003void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5004 HandleBinaryOp(instruction);
5005}
5006
5007void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5008 HandleBinaryOp(instruction);
5009}
5010
5011void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5012 // Nothing to do, this should be removed during prepare for register allocator.
5013 LOG(FATAL) << "Unreachable";
5014}
5015
5016void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5017 // Nothing to do, this should be removed during prepare for register allocator.
5018 LOG(FATAL) << "Unreachable";
5019}
5020
5021void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005022 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005023}
5024
5025void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005026 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005027}
5028
5029void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005031}
5032
5033void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005035}
5036
5037void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005039}
5040
5041void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005043}
5044
5045void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005047}
5048
5049void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005050 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005051}
5052
5053void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005054 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005055}
5056
5057void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005058 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005059}
5060
5061void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005062 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005063}
5064
5065void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005066 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005067}
5068
5069void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005070 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005071}
5072
5073void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005074 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005075}
5076
5077void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005078 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005079}
5080
5081void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005082 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005083}
5084
5085void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005086 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005087}
5088
5089void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005090 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005091}
5092
5093void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005094 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005095}
5096
5097void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005098 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005099}
5100
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005101void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5102 LocationSummary* locations =
5103 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5104 locations->SetInAt(0, Location::RequiresRegister());
5105}
5106
5107void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5108 int32_t lower_bound = switch_instr->GetStartValue();
5109 int32_t num_entries = switch_instr->GetNumEntries();
5110 LocationSummary* locations = switch_instr->GetLocations();
5111 Register value_reg = locations->InAt(0).AsRegister<Register>();
5112 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5113
5114 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005115 Register temp_reg = TMP;
5116 __ Addiu32(temp_reg, value_reg, -lower_bound);
5117 // Jump to default if index is negative
5118 // Note: We don't check the case that index is positive while value < lower_bound, because in
5119 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5120 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5121
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005122 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005123 // Jump to successors[0] if value == lower_bound.
5124 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5125 int32_t last_index = 0;
5126 for (; num_entries - last_index > 2; last_index += 2) {
5127 __ Addiu(temp_reg, temp_reg, -2);
5128 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5129 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5130 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5131 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5132 }
5133 if (num_entries - last_index == 2) {
5134 // The last missing case_value.
5135 __ Addiu(temp_reg, temp_reg, -1);
5136 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005137 }
5138
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005139 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5141 __ B(codegen_->GetLabelOf(default_block));
5142 }
5143}
5144
5145void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5146 // The trampoline uses the same calling convention as dex calling conventions,
5147 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5148 // the method_idx.
5149 HandleInvoke(invoke);
5150}
5151
5152void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5153 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5154}
5155
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005156void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5157 LocationSummary* locations =
5158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5159 locations->SetInAt(0, Location::RequiresRegister());
5160 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005161}
5162
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005163void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5164 LocationSummary* locations = instruction->GetLocations();
5165 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005166 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005167 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5168 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5169 } else {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005170 __ LoadFromOffset(kLoadWord,
5171 locations->Out().AsRegister<Register>(),
5172 locations->InAt(0).AsRegister<Register>(),
5173 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5174 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
5175 instruction->GetIndex() % ImTable::kSize, kMipsPointerSize));
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005176 }
5177 __ LoadFromOffset(kLoadWord,
5178 locations->Out().AsRegister<Register>(),
5179 locations->InAt(0).AsRegister<Register>(),
5180 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005181}
5182
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005183#undef __
5184#undef QUICK_ENTRY_POINT
5185
5186} // namespace mips
5187} // namespace art