blob: e49d6f2742bd780053a4e07197e644ec4f54354b [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
Alexey Frunzee3fb2452016-05-10 16:08:05 -070042// We'll maximize the range of a single load instruction for dex cache array accesses
43// by aligning offset -32768 with the offset of the first used element.
44static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
45
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020046Location MipsReturnLocation(Primitive::Type return_type) {
47 switch (return_type) {
48 case Primitive::kPrimBoolean:
49 case Primitive::kPrimByte:
50 case Primitive::kPrimChar:
51 case Primitive::kPrimShort:
52 case Primitive::kPrimInt:
53 case Primitive::kPrimNot:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimLong:
57 return Location::RegisterPairLocation(V0, V1);
58
59 case Primitive::kPrimFloat:
60 case Primitive::kPrimDouble:
61 return Location::FpuRegisterLocation(F0);
62
63 case Primitive::kPrimVoid:
64 return Location();
65 }
66 UNREACHABLE();
67}
68
69Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
70 return MipsReturnLocation(type);
71}
72
73Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
74 return Location::RegisterLocation(kMethodRegisterArgument);
75}
76
77Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
78 Location next_location;
79
80 switch (type) {
81 case Primitive::kPrimBoolean:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimChar:
84 case Primitive::kPrimShort:
85 case Primitive::kPrimInt:
86 case Primitive::kPrimNot: {
87 uint32_t gp_index = gp_index_++;
88 if (gp_index < calling_convention.GetNumberOfRegisters()) {
89 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
90 } else {
91 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
92 next_location = Location::StackSlot(stack_offset);
93 }
94 break;
95 }
96
97 case Primitive::kPrimLong: {
98 uint32_t gp_index = gp_index_;
99 gp_index_ += 2;
100 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
101 if (calling_convention.GetRegisterAt(gp_index) == A1) {
102 gp_index_++; // Skip A1, and use A2_A3 instead.
103 gp_index++;
104 }
105 Register low_even = calling_convention.GetRegisterAt(gp_index);
106 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
107 DCHECK_EQ(low_even + 1, high_odd);
108 next_location = Location::RegisterPairLocation(low_even, high_odd);
109 } else {
110 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
111 next_location = Location::DoubleStackSlot(stack_offset);
112 }
113 break;
114 }
115
116 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
117 // will take up the even/odd pair, while floats are stored in even regs only.
118 // On 64 bit FPU, both double and float are stored in even registers only.
119 case Primitive::kPrimFloat:
120 case Primitive::kPrimDouble: {
121 uint32_t float_index = float_index_++;
122 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
123 next_location = Location::FpuRegisterLocation(
124 calling_convention.GetFpuRegisterAt(float_index));
125 } else {
126 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
127 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
128 : Location::StackSlot(stack_offset);
129 }
130 break;
131 }
132
133 case Primitive::kPrimVoid:
134 LOG(FATAL) << "Unexpected parameter type " << type;
135 break;
136 }
137
138 // Space on the stack is reserved for all arguments.
139 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
140
141 return next_location;
142}
143
144Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
145 return MipsReturnLocation(type);
146}
147
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700148// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
174 ? QUICK_ENTRY_POINT(pThrowStringBounds)
175 : QUICK_ENTRY_POINT(pThrowArrayBounds);
176 mips_codegen->InvokeRuntime(entry_point_offset,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 instruction_,
178 instruction_->GetDexPc(),
179 this,
180 IsDirectEntrypoint(kQuickThrowArrayBounds));
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100181 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200182 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
183 }
184
185 bool IsFatal() const OVERRIDE { return true; }
186
187 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
188
189 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
191};
192
193class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
194 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000195 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196
197 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
198 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
199 __ Bind(GetEntryLabel());
200 if (instruction_->CanThrowIntoCatchBlock()) {
201 // Live registers will be restored in the catch block if caught.
202 SaveLiveRegisters(codegen, instruction_->GetLocations());
203 }
204 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
205 instruction_,
206 instruction_->GetDexPc(),
207 this,
208 IsDirectEntrypoint(kQuickThrowDivZero));
209 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
210 }
211
212 bool IsFatal() const OVERRIDE { return true; }
213
214 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
215
216 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
218};
219
220class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
221 public:
222 LoadClassSlowPathMIPS(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000226 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
233
234 __ Bind(GetEntryLabel());
235 SaveLiveRegisters(codegen, locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
239
240 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
241 : QUICK_ENTRY_POINT(pInitializeType);
242 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
243 : IsDirectEntrypoint(kQuickInitializeType);
244
245 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
246 if (do_clinit_) {
247 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
248 } else {
249 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
250 }
251
252 // Move the class to the desired location.
253 Location out = locations->Out();
254 if (out.IsValid()) {
255 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
256 Primitive::Type type = at_->GetType();
257 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
258 }
259
260 RestoreLiveRegisters(codegen, locations);
261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
270 // The instruction where this slow path is happening.
271 // (Might be the load class or an initialization check).
272 HInstruction* const at_;
273
274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
289 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
291
292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
295 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000296 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
297 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200298 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
299 instruction_,
300 instruction_->GetDexPc(),
301 this,
302 IsDirectEntrypoint(kQuickResolveString));
303 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
304 Primitive::Type type = instruction_->GetType();
305 mips_codegen->MoveLocation(locations->Out(),
306 calling_convention.GetReturnLocation(type),
307 type);
308
309 RestoreLiveRegisters(codegen, locations);
310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
344};
345
346class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
347 public:
348 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350
351 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
354 SaveLiveRegisters(codegen, instruction_->GetLocations());
355 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
356 instruction_,
357 instruction_->GetDexPc(),
358 this,
359 IsDirectEntrypoint(kQuickTestSuspend));
360 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
361 RestoreLiveRegisters(codegen, instruction_->GetLocations());
362 if (successor_ == nullptr) {
363 __ B(GetReturnLabel());
364 } else {
365 __ B(mips_codegen->GetLabelOf(successor_));
366 }
367 }
368
369 MipsLabel* GetReturnLabel() {
370 DCHECK(successor_ == nullptr);
371 return &return_label_;
372 }
373
374 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
375
376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
399 SaveLiveRegisters(codegen, locations);
400
401 // We're moving two locations to locations that could overlap, so we need a parallel
402 // move resolver.
403 InvokeRuntimeCallingConvention calling_convention;
404 codegen->EmitParallelMoves(locations->InAt(1),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
406 Primitive::kPrimNot,
407 object_class,
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimNot);
410
411 if (instruction_->IsInstanceOf()) {
412 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
413 instruction_,
414 dex_pc,
415 this,
416 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
Roland Levillain888d0672015-11-23 18:53:50 +0000417 CheckEntrypointTypes<
418 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 Primitive::Type ret_type = instruction_->GetType();
420 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
421 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 } else {
423 DCHECK(instruction_->IsCheckCast());
424 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
425 instruction_,
426 dex_pc,
427 this,
428 IsDirectEntrypoint(kQuickCheckCast));
429 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
430 }
431
432 RestoreLiveRegisters(codegen, locations);
433 __ B(GetExitLabel());
434 }
435
436 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
437
438 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
440};
441
442class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
443 public:
Aart Bik42249c32016-01-07 15:33:50 -0800444 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000445 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800448 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200449 __ Bind(GetEntryLabel());
450 SaveLiveRegisters(codegen, instruction_->GetLocations());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
452 instruction_,
Aart Bik42249c32016-01-07 15:33:50 -0800453 instruction_->GetDexPc(),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 this,
455 IsDirectEntrypoint(kQuickDeoptimize));
Roland Levillain888d0672015-11-23 18:53:50 +0000456 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 }
458
459 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
460
461 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
463};
464
465CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
466 const MipsInstructionSetFeatures& isa_features,
467 const CompilerOptions& compiler_options,
468 OptimizingCompilerStats* stats)
469 : CodeGenerator(graph,
470 kNumberOfCoreRegisters,
471 kNumberOfFRegisters,
472 kNumberOfRegisterPairs,
473 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
474 arraysize(kCoreCalleeSaves)),
475 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
476 arraysize(kFpuCalleeSaves)),
477 compiler_options,
478 stats),
479 block_labels_(nullptr),
480 location_builder_(graph, this),
481 instruction_visitor_(graph, this),
482 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100483 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700484 isa_features_(isa_features),
485 method_patches_(MethodReferenceComparator(),
486 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
487 call_patches_(MethodReferenceComparator(),
488 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
489 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 // Save RA (containing the return address) to mimic Quick.
491 AddAllocatedRegister(Location::RegisterLocation(RA));
492}
493
494#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700495// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
496#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
498
499void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
500 // Ensure that we fix up branches.
501 __ FinalizeCode();
502
503 // Adjust native pc offsets in stack maps.
504 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
505 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
506 uint32_t new_position = __ GetAdjustedPosition(old_position);
507 DCHECK_GE(new_position, old_position);
508 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
509 }
510
511 // Adjust pc offsets for the disassembly information.
512 if (disasm_info_ != nullptr) {
513 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
514 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
515 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
516 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
517 it.second.start = __ GetAdjustedPosition(it.second.start);
518 it.second.end = __ GetAdjustedPosition(it.second.end);
519 }
520 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
521 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
522 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
523 }
524 }
525
526 CodeGenerator::Finalize(allocator);
527}
528
529MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
530 return codegen_->GetAssembler();
531}
532
533void ParallelMoveResolverMIPS::EmitMove(size_t index) {
534 DCHECK_LT(index, moves_.size());
535 MoveOperands* move = moves_[index];
536 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
537}
538
539void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
540 DCHECK_LT(index, moves_.size());
541 MoveOperands* move = moves_[index];
542 Primitive::Type type = move->GetType();
543 Location loc1 = move->GetDestination();
544 Location loc2 = move->GetSource();
545
546 DCHECK(!loc1.IsConstant());
547 DCHECK(!loc2.IsConstant());
548
549 if (loc1.Equals(loc2)) {
550 return;
551 }
552
553 if (loc1.IsRegister() && loc2.IsRegister()) {
554 // Swap 2 GPRs.
555 Register r1 = loc1.AsRegister<Register>();
556 Register r2 = loc2.AsRegister<Register>();
557 __ Move(TMP, r2);
558 __ Move(r2, r1);
559 __ Move(r1, TMP);
560 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
561 FRegister f1 = loc1.AsFpuRegister<FRegister>();
562 FRegister f2 = loc2.AsFpuRegister<FRegister>();
563 if (type == Primitive::kPrimFloat) {
564 __ MovS(FTMP, f2);
565 __ MovS(f2, f1);
566 __ MovS(f1, FTMP);
567 } else {
568 DCHECK_EQ(type, Primitive::kPrimDouble);
569 __ MovD(FTMP, f2);
570 __ MovD(f2, f1);
571 __ MovD(f1, FTMP);
572 }
573 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
574 (loc1.IsFpuRegister() && loc2.IsRegister())) {
575 // Swap FPR and GPR.
576 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
577 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
578 : loc2.AsFpuRegister<FRegister>();
579 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
580 : loc2.AsRegister<Register>();
581 __ Move(TMP, r2);
582 __ Mfc1(r2, f1);
583 __ Mtc1(TMP, f1);
584 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
585 // Swap 2 GPR register pairs.
586 Register r1 = loc1.AsRegisterPairLow<Register>();
587 Register r2 = loc2.AsRegisterPairLow<Register>();
588 __ Move(TMP, r2);
589 __ Move(r2, r1);
590 __ Move(r1, TMP);
591 r1 = loc1.AsRegisterPairHigh<Register>();
592 r2 = loc2.AsRegisterPairHigh<Register>();
593 __ Move(TMP, r2);
594 __ Move(r2, r1);
595 __ Move(r1, TMP);
596 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
597 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
598 // Swap FPR and GPR register pair.
599 DCHECK_EQ(type, Primitive::kPrimDouble);
600 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
601 : loc2.AsFpuRegister<FRegister>();
602 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
603 : loc2.AsRegisterPairLow<Register>();
604 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
605 : loc2.AsRegisterPairHigh<Register>();
606 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
607 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
608 // unpredictable and the following mfch1 will fail.
609 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800610 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200611 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800612 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200613 __ Move(r2_l, TMP);
614 __ Move(r2_h, AT);
615 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
616 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
617 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
618 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000619 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
620 (loc1.IsStackSlot() && loc2.IsRegister())) {
621 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
622 : loc2.AsRegister<Register>();
623 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
624 : loc2.GetStackIndex();
625 __ Move(TMP, reg);
626 __ LoadFromOffset(kLoadWord, reg, SP, offset);
627 __ StoreToOffset(kStoreWord, TMP, SP, offset);
628 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
629 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
630 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
631 : loc2.AsRegisterPairLow<Register>();
632 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
633 : loc2.AsRegisterPairHigh<Register>();
634 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
635 : loc2.GetStackIndex();
636 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
637 : loc2.GetHighStackIndex(kMipsWordSize);
638 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000639 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000640 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000641 __ Move(TMP, reg_h);
642 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
643 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200644 } else {
645 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
646 }
647}
648
649void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
650 __ Pop(static_cast<Register>(reg));
651}
652
653void ParallelMoveResolverMIPS::SpillScratch(int reg) {
654 __ Push(static_cast<Register>(reg));
655}
656
657void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
658 // Allocate a scratch register other than TMP, if available.
659 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
660 // automatically unspilled when the scratch scope object is destroyed).
661 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
662 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
663 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
664 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
665 __ LoadFromOffset(kLoadWord,
666 Register(ensure_scratch.GetRegister()),
667 SP,
668 index1 + stack_offset);
669 __ LoadFromOffset(kLoadWord,
670 TMP,
671 SP,
672 index2 + stack_offset);
673 __ StoreToOffset(kStoreWord,
674 Register(ensure_scratch.GetRegister()),
675 SP,
676 index2 + stack_offset);
677 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
678 }
679}
680
Alexey Frunze73296a72016-06-03 22:51:46 -0700681void CodeGeneratorMIPS::ComputeSpillMask() {
682 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
683 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
684 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
685 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
686 // registers, include the ZERO register to force alignment of FPU callee-saved registers
687 // within the stack frame.
688 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
689 core_spill_mask_ |= (1 << ZERO);
690 }
691}
692
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200693static dwarf::Reg DWARFReg(Register reg) {
694 return dwarf::Reg::MipsCore(static_cast<int>(reg));
695}
696
697// TODO: mapping of floating-point registers to DWARF.
698
699void CodeGeneratorMIPS::GenerateFrameEntry() {
700 __ Bind(&frame_entry_label_);
701
702 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
703
704 if (do_overflow_check) {
705 __ LoadFromOffset(kLoadWord,
706 ZERO,
707 SP,
708 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
709 RecordPcInfo(nullptr, 0);
710 }
711
712 if (HasEmptyFrame()) {
713 return;
714 }
715
716 // Make sure the frame size isn't unreasonably large.
717 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
718 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
719 }
720
721 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200722
Alexey Frunze73296a72016-06-03 22:51:46 -0700723 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200724 __ IncreaseFrameSize(ofs);
725
Alexey Frunze73296a72016-06-03 22:51:46 -0700726 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
727 Register reg = static_cast<Register>(MostSignificantBit(mask));
728 mask ^= 1u << reg;
729 ofs -= kMipsWordSize;
730 // The ZERO register is only included for alignment.
731 if (reg != ZERO) {
732 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200733 __ cfi().RelOffset(DWARFReg(reg), ofs);
734 }
735 }
736
Alexey Frunze73296a72016-06-03 22:51:46 -0700737 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
738 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
739 mask ^= 1u << reg;
740 ofs -= kMipsDoublewordSize;
741 __ StoreDToOffset(reg, SP, ofs);
742 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200743 }
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 // Store the current method pointer.
746 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200747}
748
749void CodeGeneratorMIPS::GenerateFrameExit() {
750 __ cfi().RememberState();
751
752 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200753 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200754
Alexey Frunze73296a72016-06-03 22:51:46 -0700755 // For better instruction scheduling restore RA before other registers.
756 uint32_t ofs = GetFrameSize();
757 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
758 Register reg = static_cast<Register>(MostSignificantBit(mask));
759 mask ^= 1u << reg;
760 ofs -= kMipsWordSize;
761 // The ZERO register is only included for alignment.
762 if (reg != ZERO) {
763 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200764 __ cfi().Restore(DWARFReg(reg));
765 }
766 }
767
Alexey Frunze73296a72016-06-03 22:51:46 -0700768 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
769 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
770 mask ^= 1u << reg;
771 ofs -= kMipsDoublewordSize;
772 __ LoadDFromOffset(reg, SP, ofs);
773 // TODO: __ cfi().Restore(DWARFReg(reg));
774 }
775
776 __ DecreaseFrameSize(GetFrameSize());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200777 }
778
779 __ Jr(RA);
780 __ Nop();
781
782 __ cfi().RestoreState();
783 __ cfi().DefCFAOffset(GetFrameSize());
784}
785
786void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
787 __ Bind(GetLabelOf(block));
788}
789
790void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
791 if (src.Equals(dst)) {
792 return;
793 }
794
795 if (src.IsConstant()) {
796 MoveConstant(dst, src.GetConstant());
797 } else {
798 if (Primitive::Is64BitType(dst_type)) {
799 Move64(dst, src);
800 } else {
801 Move32(dst, src);
802 }
803 }
804}
805
806void CodeGeneratorMIPS::Move32(Location destination, Location source) {
807 if (source.Equals(destination)) {
808 return;
809 }
810
811 if (destination.IsRegister()) {
812 if (source.IsRegister()) {
813 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
814 } else if (source.IsFpuRegister()) {
815 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
816 } else {
817 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
818 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
819 }
820 } else if (destination.IsFpuRegister()) {
821 if (source.IsRegister()) {
822 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
823 } else if (source.IsFpuRegister()) {
824 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
825 } else {
826 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
827 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
828 }
829 } else {
830 DCHECK(destination.IsStackSlot()) << destination;
831 if (source.IsRegister()) {
832 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
833 } else if (source.IsFpuRegister()) {
834 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
835 } else {
836 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
837 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
838 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
839 }
840 }
841}
842
843void CodeGeneratorMIPS::Move64(Location destination, Location source) {
844 if (source.Equals(destination)) {
845 return;
846 }
847
848 if (destination.IsRegisterPair()) {
849 if (source.IsRegisterPair()) {
850 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
851 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
852 } else if (source.IsFpuRegister()) {
853 Register dst_high = destination.AsRegisterPairHigh<Register>();
854 Register dst_low = destination.AsRegisterPairLow<Register>();
855 FRegister src = source.AsFpuRegister<FRegister>();
856 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800857 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200858 } else {
859 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
860 int32_t off = source.GetStackIndex();
861 Register r = destination.AsRegisterPairLow<Register>();
862 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
863 }
864 } else if (destination.IsFpuRegister()) {
865 if (source.IsRegisterPair()) {
866 FRegister dst = destination.AsFpuRegister<FRegister>();
867 Register src_high = source.AsRegisterPairHigh<Register>();
868 Register src_low = source.AsRegisterPairLow<Register>();
869 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800870 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200871 } else if (source.IsFpuRegister()) {
872 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
873 } else {
874 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
875 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
876 }
877 } else {
878 DCHECK(destination.IsDoubleStackSlot()) << destination;
879 int32_t off = destination.GetStackIndex();
880 if (source.IsRegisterPair()) {
881 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
882 } else if (source.IsFpuRegister()) {
883 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
884 } else {
885 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
886 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
887 __ StoreToOffset(kStoreWord, TMP, SP, off);
888 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
889 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
890 }
891 }
892}
893
894void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
895 if (c->IsIntConstant() || c->IsNullConstant()) {
896 // Move 32 bit constant.
897 int32_t value = GetInt32ValueOf(c);
898 if (destination.IsRegister()) {
899 Register dst = destination.AsRegister<Register>();
900 __ LoadConst32(dst, value);
901 } else {
902 DCHECK(destination.IsStackSlot())
903 << "Cannot move " << c->DebugName() << " to " << destination;
904 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
905 }
906 } else if (c->IsLongConstant()) {
907 // Move 64 bit constant.
908 int64_t value = GetInt64ValueOf(c);
909 if (destination.IsRegisterPair()) {
910 Register r_h = destination.AsRegisterPairHigh<Register>();
911 Register r_l = destination.AsRegisterPairLow<Register>();
912 __ LoadConst64(r_h, r_l, value);
913 } else {
914 DCHECK(destination.IsDoubleStackSlot())
915 << "Cannot move " << c->DebugName() << " to " << destination;
916 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
917 }
918 } else if (c->IsFloatConstant()) {
919 // Move 32 bit float constant.
920 int32_t value = GetInt32ValueOf(c);
921 if (destination.IsFpuRegister()) {
922 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
923 } else {
924 DCHECK(destination.IsStackSlot())
925 << "Cannot move " << c->DebugName() << " to " << destination;
926 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
927 }
928 } else {
929 // Move 64 bit double constant.
930 DCHECK(c->IsDoubleConstant()) << c->DebugName();
931 int64_t value = GetInt64ValueOf(c);
932 if (destination.IsFpuRegister()) {
933 FRegister fd = destination.AsFpuRegister<FRegister>();
934 __ LoadDConst64(fd, value, TMP);
935 } else {
936 DCHECK(destination.IsDoubleStackSlot())
937 << "Cannot move " << c->DebugName() << " to " << destination;
938 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
939 }
940 }
941}
942
943void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
944 DCHECK(destination.IsRegister());
945 Register dst = destination.AsRegister<Register>();
946 __ LoadConst32(dst, value);
947}
948
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200949void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
950 if (location.IsRegister()) {
951 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700952 } else if (location.IsRegisterPair()) {
953 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
954 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200955 } else {
956 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
957 }
958}
959
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700960void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
961 DCHECK(linker_patches->empty());
962 size_t size =
963 method_patches_.size() +
964 call_patches_.size() +
965 pc_relative_dex_cache_patches_.size();
966 linker_patches->reserve(size);
967 for (const auto& entry : method_patches_) {
968 const MethodReference& target_method = entry.first;
969 Literal* literal = entry.second;
970 DCHECK(literal->GetLabel()->IsBound());
971 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
972 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
973 target_method.dex_file,
974 target_method.dex_method_index));
975 }
976 for (const auto& entry : call_patches_) {
977 const MethodReference& target_method = entry.first;
978 Literal* literal = entry.second;
979 DCHECK(literal->GetLabel()->IsBound());
980 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
981 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
982 target_method.dex_file,
983 target_method.dex_method_index));
984 }
985 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
986 const DexFile& dex_file = info.target_dex_file;
987 size_t base_element_offset = info.offset_or_index;
988 DCHECK(info.high_label.IsBound());
989 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
990 DCHECK(info.pc_rel_label.IsBound());
991 uint32_t pc_rel_offset = __ GetLabelLocation(&info.pc_rel_label);
992 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(high_offset,
993 &dex_file,
994 pc_rel_offset,
995 base_element_offset));
996 }
997}
998
999CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1000 const DexFile& dex_file, uint32_t element_offset) {
1001 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1002}
1003
1004CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1005 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1006 patches->emplace_back(dex_file, offset_or_index);
1007 return &patches->back();
1008}
1009
1010Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1011 MethodToLiteralMap* map) {
1012 return map->GetOrCreate(
1013 target_method,
1014 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1015}
1016
1017Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1018 return DeduplicateMethodLiteral(target_method, &method_patches_);
1019}
1020
1021Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1022 return DeduplicateMethodLiteral(target_method, &call_patches_);
1023}
1024
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1026 MipsLabel done;
1027 Register card = AT;
1028 Register temp = TMP;
1029 __ Beqz(value, &done);
1030 __ LoadFromOffset(kLoadWord,
1031 card,
1032 TR,
1033 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1034 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1035 __ Addu(temp, card, temp);
1036 __ Sb(card, temp, 0);
1037 __ Bind(&done);
1038}
1039
David Brazdil58282f42016-01-14 12:45:10 +00001040void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001041 // Don't allocate the dalvik style register pair passing.
1042 blocked_register_pairs_[A1_A2] = true;
1043
1044 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1045 blocked_core_registers_[ZERO] = true;
1046 blocked_core_registers_[K0] = true;
1047 blocked_core_registers_[K1] = true;
1048 blocked_core_registers_[GP] = true;
1049 blocked_core_registers_[SP] = true;
1050 blocked_core_registers_[RA] = true;
1051
1052 // AT and TMP(T8) are used as temporary/scratch registers
1053 // (similar to how AT is used by MIPS assemblers).
1054 blocked_core_registers_[AT] = true;
1055 blocked_core_registers_[TMP] = true;
1056 blocked_fpu_registers_[FTMP] = true;
1057
1058 // Reserve suspend and thread registers.
1059 blocked_core_registers_[S0] = true;
1060 blocked_core_registers_[TR] = true;
1061
1062 // Reserve T9 for function calls
1063 blocked_core_registers_[T9] = true;
1064
1065 // Reserve odd-numbered FPU registers.
1066 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1067 blocked_fpu_registers_[i] = true;
1068 }
1069
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001070 UpdateBlockedPairRegisters();
1071}
1072
1073void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1074 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1075 MipsManagedRegister current =
1076 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1077 if (blocked_core_registers_[current.AsRegisterPairLow()]
1078 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1079 blocked_register_pairs_[i] = true;
1080 }
1081 }
1082}
1083
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1085 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1086 return kMipsWordSize;
1087}
1088
1089size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1090 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1091 return kMipsWordSize;
1092}
1093
1094size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1095 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1096 return kMipsDoublewordSize;
1097}
1098
1099size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1100 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1101 return kMipsDoublewordSize;
1102}
1103
1104void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001105 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106}
1107
1108void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001109 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001110}
1111
1112void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1113 HInstruction* instruction,
1114 uint32_t dex_pc,
1115 SlowPathCode* slow_path) {
1116 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1117 instruction,
1118 dex_pc,
1119 slow_path,
1120 IsDirectEntrypoint(entrypoint));
1121}
1122
1123constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1124
1125void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1126 HInstruction* instruction,
1127 uint32_t dex_pc,
1128 SlowPathCode* slow_path,
1129 bool is_direct_entrypoint) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001130 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1131 __ Jalr(T9);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001132 if (is_direct_entrypoint) {
1133 // Reserve argument space on stack (for $a0-$a3) for
1134 // entrypoints that directly reference native implementations.
1135 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001136 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001137 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001138 } else {
1139 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001140 }
1141 RecordPcInfo(instruction, dex_pc, slow_path);
1142}
1143
1144void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1145 Register class_reg) {
1146 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1147 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1148 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1149 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1150 __ Sync(0);
1151 __ Bind(slow_path->GetExitLabel());
1152}
1153
1154void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1155 __ Sync(0); // Only stype 0 is supported.
1156}
1157
1158void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1159 HBasicBlock* successor) {
1160 SuspendCheckSlowPathMIPS* slow_path =
1161 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1162 codegen_->AddSlowPath(slow_path);
1163
1164 __ LoadFromOffset(kLoadUnsignedHalfword,
1165 TMP,
1166 TR,
1167 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1168 if (successor == nullptr) {
1169 __ Bnez(TMP, slow_path->GetEntryLabel());
1170 __ Bind(slow_path->GetReturnLabel());
1171 } else {
1172 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1173 __ B(slow_path->GetEntryLabel());
1174 // slow_path will return to GetLabelOf(successor).
1175 }
1176}
1177
1178InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1179 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001180 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001181 assembler_(codegen->GetAssembler()),
1182 codegen_(codegen) {}
1183
1184void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1185 DCHECK_EQ(instruction->InputCount(), 2U);
1186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1187 Primitive::Type type = instruction->GetResultType();
1188 switch (type) {
1189 case Primitive::kPrimInt: {
1190 locations->SetInAt(0, Location::RequiresRegister());
1191 HInstruction* right = instruction->InputAt(1);
1192 bool can_use_imm = false;
1193 if (right->IsConstant()) {
1194 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1195 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1196 can_use_imm = IsUint<16>(imm);
1197 } else if (instruction->IsAdd()) {
1198 can_use_imm = IsInt<16>(imm);
1199 } else {
1200 DCHECK(instruction->IsSub());
1201 can_use_imm = IsInt<16>(-imm);
1202 }
1203 }
1204 if (can_use_imm)
1205 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1206 else
1207 locations->SetInAt(1, Location::RequiresRegister());
1208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1209 break;
1210 }
1211
1212 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001213 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001214 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001216 break;
1217 }
1218
1219 case Primitive::kPrimFloat:
1220 case Primitive::kPrimDouble:
1221 DCHECK(instruction->IsAdd() || instruction->IsSub());
1222 locations->SetInAt(0, Location::RequiresFpuRegister());
1223 locations->SetInAt(1, Location::RequiresFpuRegister());
1224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1225 break;
1226
1227 default:
1228 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1229 }
1230}
1231
1232void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1233 Primitive::Type type = instruction->GetType();
1234 LocationSummary* locations = instruction->GetLocations();
1235
1236 switch (type) {
1237 case Primitive::kPrimInt: {
1238 Register dst = locations->Out().AsRegister<Register>();
1239 Register lhs = locations->InAt(0).AsRegister<Register>();
1240 Location rhs_location = locations->InAt(1);
1241
1242 Register rhs_reg = ZERO;
1243 int32_t rhs_imm = 0;
1244 bool use_imm = rhs_location.IsConstant();
1245 if (use_imm) {
1246 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1247 } else {
1248 rhs_reg = rhs_location.AsRegister<Register>();
1249 }
1250
1251 if (instruction->IsAnd()) {
1252 if (use_imm)
1253 __ Andi(dst, lhs, rhs_imm);
1254 else
1255 __ And(dst, lhs, rhs_reg);
1256 } else if (instruction->IsOr()) {
1257 if (use_imm)
1258 __ Ori(dst, lhs, rhs_imm);
1259 else
1260 __ Or(dst, lhs, rhs_reg);
1261 } else if (instruction->IsXor()) {
1262 if (use_imm)
1263 __ Xori(dst, lhs, rhs_imm);
1264 else
1265 __ Xor(dst, lhs, rhs_reg);
1266 } else if (instruction->IsAdd()) {
1267 if (use_imm)
1268 __ Addiu(dst, lhs, rhs_imm);
1269 else
1270 __ Addu(dst, lhs, rhs_reg);
1271 } else {
1272 DCHECK(instruction->IsSub());
1273 if (use_imm)
1274 __ Addiu(dst, lhs, -rhs_imm);
1275 else
1276 __ Subu(dst, lhs, rhs_reg);
1277 }
1278 break;
1279 }
1280
1281 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1283 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1284 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1285 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001286 Location rhs_location = locations->InAt(1);
1287 bool use_imm = rhs_location.IsConstant();
1288 if (!use_imm) {
1289 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1290 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1291 if (instruction->IsAnd()) {
1292 __ And(dst_low, lhs_low, rhs_low);
1293 __ And(dst_high, lhs_high, rhs_high);
1294 } else if (instruction->IsOr()) {
1295 __ Or(dst_low, lhs_low, rhs_low);
1296 __ Or(dst_high, lhs_high, rhs_high);
1297 } else if (instruction->IsXor()) {
1298 __ Xor(dst_low, lhs_low, rhs_low);
1299 __ Xor(dst_high, lhs_high, rhs_high);
1300 } else if (instruction->IsAdd()) {
1301 if (lhs_low == rhs_low) {
1302 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1303 __ Slt(TMP, lhs_low, ZERO);
1304 __ Addu(dst_low, lhs_low, rhs_low);
1305 } else {
1306 __ Addu(dst_low, lhs_low, rhs_low);
1307 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1308 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1309 }
1310 __ Addu(dst_high, lhs_high, rhs_high);
1311 __ Addu(dst_high, dst_high, TMP);
1312 } else {
1313 DCHECK(instruction->IsSub());
1314 __ Sltu(TMP, lhs_low, rhs_low);
1315 __ Subu(dst_low, lhs_low, rhs_low);
1316 __ Subu(dst_high, lhs_high, rhs_high);
1317 __ Subu(dst_high, dst_high, TMP);
1318 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001320 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1321 if (instruction->IsOr()) {
1322 uint32_t low = Low32Bits(value);
1323 uint32_t high = High32Bits(value);
1324 if (IsUint<16>(low)) {
1325 if (dst_low != lhs_low || low != 0) {
1326 __ Ori(dst_low, lhs_low, low);
1327 }
1328 } else {
1329 __ LoadConst32(TMP, low);
1330 __ Or(dst_low, lhs_low, TMP);
1331 }
1332 if (IsUint<16>(high)) {
1333 if (dst_high != lhs_high || high != 0) {
1334 __ Ori(dst_high, lhs_high, high);
1335 }
1336 } else {
1337 if (high != low) {
1338 __ LoadConst32(TMP, high);
1339 }
1340 __ Or(dst_high, lhs_high, TMP);
1341 }
1342 } else if (instruction->IsXor()) {
1343 uint32_t low = Low32Bits(value);
1344 uint32_t high = High32Bits(value);
1345 if (IsUint<16>(low)) {
1346 if (dst_low != lhs_low || low != 0) {
1347 __ Xori(dst_low, lhs_low, low);
1348 }
1349 } else {
1350 __ LoadConst32(TMP, low);
1351 __ Xor(dst_low, lhs_low, TMP);
1352 }
1353 if (IsUint<16>(high)) {
1354 if (dst_high != lhs_high || high != 0) {
1355 __ Xori(dst_high, lhs_high, high);
1356 }
1357 } else {
1358 if (high != low) {
1359 __ LoadConst32(TMP, high);
1360 }
1361 __ Xor(dst_high, lhs_high, TMP);
1362 }
1363 } else if (instruction->IsAnd()) {
1364 uint32_t low = Low32Bits(value);
1365 uint32_t high = High32Bits(value);
1366 if (IsUint<16>(low)) {
1367 __ Andi(dst_low, lhs_low, low);
1368 } else if (low != 0xFFFFFFFF) {
1369 __ LoadConst32(TMP, low);
1370 __ And(dst_low, lhs_low, TMP);
1371 } else if (dst_low != lhs_low) {
1372 __ Move(dst_low, lhs_low);
1373 }
1374 if (IsUint<16>(high)) {
1375 __ Andi(dst_high, lhs_high, high);
1376 } else if (high != 0xFFFFFFFF) {
1377 if (high != low) {
1378 __ LoadConst32(TMP, high);
1379 }
1380 __ And(dst_high, lhs_high, TMP);
1381 } else if (dst_high != lhs_high) {
1382 __ Move(dst_high, lhs_high);
1383 }
1384 } else {
1385 if (instruction->IsSub()) {
1386 value = -value;
1387 } else {
1388 DCHECK(instruction->IsAdd());
1389 }
1390 int32_t low = Low32Bits(value);
1391 int32_t high = High32Bits(value);
1392 if (IsInt<16>(low)) {
1393 if (dst_low != lhs_low || low != 0) {
1394 __ Addiu(dst_low, lhs_low, low);
1395 }
1396 if (low != 0) {
1397 __ Sltiu(AT, dst_low, low);
1398 }
1399 } else {
1400 __ LoadConst32(TMP, low);
1401 __ Addu(dst_low, lhs_low, TMP);
1402 __ Sltu(AT, dst_low, TMP);
1403 }
1404 if (IsInt<16>(high)) {
1405 if (dst_high != lhs_high || high != 0) {
1406 __ Addiu(dst_high, lhs_high, high);
1407 }
1408 } else {
1409 if (high != low) {
1410 __ LoadConst32(TMP, high);
1411 }
1412 __ Addu(dst_high, lhs_high, TMP);
1413 }
1414 if (low != 0) {
1415 __ Addu(dst_high, dst_high, AT);
1416 }
1417 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001418 }
1419 break;
1420 }
1421
1422 case Primitive::kPrimFloat:
1423 case Primitive::kPrimDouble: {
1424 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1425 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1426 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1427 if (instruction->IsAdd()) {
1428 if (type == Primitive::kPrimFloat) {
1429 __ AddS(dst, lhs, rhs);
1430 } else {
1431 __ AddD(dst, lhs, rhs);
1432 }
1433 } else {
1434 DCHECK(instruction->IsSub());
1435 if (type == Primitive::kPrimFloat) {
1436 __ SubS(dst, lhs, rhs);
1437 } else {
1438 __ SubD(dst, lhs, rhs);
1439 }
1440 }
1441 break;
1442 }
1443
1444 default:
1445 LOG(FATAL) << "Unexpected binary operation type " << type;
1446 }
1447}
1448
1449void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001450 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001451
1452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1453 Primitive::Type type = instr->GetResultType();
1454 switch (type) {
1455 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001456 locations->SetInAt(0, Location::RequiresRegister());
1457 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1459 break;
1460 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001461 locations->SetInAt(0, Location::RequiresRegister());
1462 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1463 locations->SetOut(Location::RequiresRegister());
1464 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001465 default:
1466 LOG(FATAL) << "Unexpected shift type " << type;
1467 }
1468}
1469
1470static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1471
1472void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001473 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001474 LocationSummary* locations = instr->GetLocations();
1475 Primitive::Type type = instr->GetType();
1476
1477 Location rhs_location = locations->InAt(1);
1478 bool use_imm = rhs_location.IsConstant();
1479 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1480 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001481 const uint32_t shift_mask =
1482 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001483 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001484 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1485 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001486
1487 switch (type) {
1488 case Primitive::kPrimInt: {
1489 Register dst = locations->Out().AsRegister<Register>();
1490 Register lhs = locations->InAt(0).AsRegister<Register>();
1491 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001492 if (shift_value == 0) {
1493 if (dst != lhs) {
1494 __ Move(dst, lhs);
1495 }
1496 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001497 __ Sll(dst, lhs, shift_value);
1498 } else if (instr->IsShr()) {
1499 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001500 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001502 } else {
1503 if (has_ins_rotr) {
1504 __ Rotr(dst, lhs, shift_value);
1505 } else {
1506 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1507 __ Srl(dst, lhs, shift_value);
1508 __ Or(dst, dst, TMP);
1509 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001510 }
1511 } else {
1512 if (instr->IsShl()) {
1513 __ Sllv(dst, lhs, rhs_reg);
1514 } else if (instr->IsShr()) {
1515 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001516 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001517 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001518 } else {
1519 if (has_ins_rotr) {
1520 __ Rotrv(dst, lhs, rhs_reg);
1521 } else {
1522 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001523 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1524 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1525 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1526 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1527 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001528 __ Sllv(TMP, lhs, TMP);
1529 __ Srlv(dst, lhs, rhs_reg);
1530 __ Or(dst, dst, TMP);
1531 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001532 }
1533 }
1534 break;
1535 }
1536
1537 case Primitive::kPrimLong: {
1538 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1539 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1540 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1541 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1542 if (use_imm) {
1543 if (shift_value == 0) {
1544 codegen_->Move64(locations->Out(), locations->InAt(0));
1545 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001546 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001547 if (instr->IsShl()) {
1548 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1549 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1550 __ Sll(dst_low, lhs_low, shift_value);
1551 } else if (instr->IsShr()) {
1552 __ Srl(dst_low, lhs_low, shift_value);
1553 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1554 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001555 } else if (instr->IsUShr()) {
1556 __ Srl(dst_low, lhs_low, shift_value);
1557 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1558 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001559 } else {
1560 __ Srl(dst_low, lhs_low, shift_value);
1561 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1562 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001563 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001564 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001565 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001566 if (instr->IsShl()) {
1567 __ Sll(dst_low, lhs_low, shift_value);
1568 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1569 __ Sll(dst_high, lhs_high, shift_value);
1570 __ Or(dst_high, dst_high, TMP);
1571 } else if (instr->IsShr()) {
1572 __ Sra(dst_high, lhs_high, shift_value);
1573 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1574 __ Srl(dst_low, lhs_low, shift_value);
1575 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001576 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001577 __ Srl(dst_high, lhs_high, shift_value);
1578 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1579 __ Srl(dst_low, lhs_low, shift_value);
1580 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001581 } else {
1582 __ Srl(TMP, lhs_low, shift_value);
1583 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1584 __ Or(dst_low, dst_low, TMP);
1585 __ Srl(TMP, lhs_high, shift_value);
1586 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1587 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001588 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589 }
1590 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001591 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001592 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001593 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 __ Move(dst_low, ZERO);
1595 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001596 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001597 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001598 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001599 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001600 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001601 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001602 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001603 // 64-bit rotation by 32 is just a swap.
1604 __ Move(dst_low, lhs_high);
1605 __ Move(dst_high, lhs_low);
1606 } else {
1607 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001608 __ Srl(dst_low, lhs_high, shift_value_high);
1609 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1610 __ Srl(dst_high, lhs_low, shift_value_high);
1611 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001612 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001613 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1614 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001615 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001616 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1617 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001618 __ Or(dst_high, dst_high, TMP);
1619 }
1620 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001621 }
1622 }
1623 } else {
1624 MipsLabel done;
1625 if (instr->IsShl()) {
1626 __ Sllv(dst_low, lhs_low, rhs_reg);
1627 __ Nor(AT, ZERO, rhs_reg);
1628 __ Srl(TMP, lhs_low, 1);
1629 __ Srlv(TMP, TMP, AT);
1630 __ Sllv(dst_high, lhs_high, rhs_reg);
1631 __ Or(dst_high, dst_high, TMP);
1632 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1633 __ Beqz(TMP, &done);
1634 __ Move(dst_high, dst_low);
1635 __ Move(dst_low, ZERO);
1636 } else if (instr->IsShr()) {
1637 __ Srav(dst_high, lhs_high, rhs_reg);
1638 __ Nor(AT, ZERO, rhs_reg);
1639 __ Sll(TMP, lhs_high, 1);
1640 __ Sllv(TMP, TMP, AT);
1641 __ Srlv(dst_low, lhs_low, rhs_reg);
1642 __ Or(dst_low, dst_low, TMP);
1643 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1644 __ Beqz(TMP, &done);
1645 __ Move(dst_low, dst_high);
1646 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001648 __ Srlv(dst_high, lhs_high, rhs_reg);
1649 __ Nor(AT, ZERO, rhs_reg);
1650 __ Sll(TMP, lhs_high, 1);
1651 __ Sllv(TMP, TMP, AT);
1652 __ Srlv(dst_low, lhs_low, rhs_reg);
1653 __ Or(dst_low, dst_low, TMP);
1654 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1655 __ Beqz(TMP, &done);
1656 __ Move(dst_low, dst_high);
1657 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001658 } else {
1659 __ Nor(AT, ZERO, rhs_reg);
1660 __ Srlv(TMP, lhs_low, rhs_reg);
1661 __ Sll(dst_low, lhs_high, 1);
1662 __ Sllv(dst_low, dst_low, AT);
1663 __ Or(dst_low, dst_low, TMP);
1664 __ Srlv(TMP, lhs_high, rhs_reg);
1665 __ Sll(dst_high, lhs_low, 1);
1666 __ Sllv(dst_high, dst_high, AT);
1667 __ Or(dst_high, dst_high, TMP);
1668 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1669 __ Beqz(TMP, &done);
1670 __ Move(TMP, dst_high);
1671 __ Move(dst_high, dst_low);
1672 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001673 }
1674 __ Bind(&done);
1675 }
1676 break;
1677 }
1678
1679 default:
1680 LOG(FATAL) << "Unexpected shift operation type " << type;
1681 }
1682}
1683
1684void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1685 HandleBinaryOp(instruction);
1686}
1687
1688void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1689 HandleBinaryOp(instruction);
1690}
1691
1692void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1693 HandleBinaryOp(instruction);
1694}
1695
1696void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1697 HandleBinaryOp(instruction);
1698}
1699
1700void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1701 LocationSummary* locations =
1702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1703 locations->SetInAt(0, Location::RequiresRegister());
1704 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1705 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1706 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1707 } else {
1708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1709 }
1710}
1711
1712void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1713 LocationSummary* locations = instruction->GetLocations();
1714 Register obj = locations->InAt(0).AsRegister<Register>();
1715 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001716 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001717
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001718 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001719 switch (type) {
1720 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 Register out = locations->Out().AsRegister<Register>();
1722 if (index.IsConstant()) {
1723 size_t offset =
1724 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1725 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1726 } else {
1727 __ Addu(TMP, obj, index.AsRegister<Register>());
1728 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1729 }
1730 break;
1731 }
1732
1733 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001734 Register out = locations->Out().AsRegister<Register>();
1735 if (index.IsConstant()) {
1736 size_t offset =
1737 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1738 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1739 } else {
1740 __ Addu(TMP, obj, index.AsRegister<Register>());
1741 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1742 }
1743 break;
1744 }
1745
1746 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001747 Register out = locations->Out().AsRegister<Register>();
1748 if (index.IsConstant()) {
1749 size_t offset =
1750 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1751 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1752 } else {
1753 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1754 __ Addu(TMP, obj, TMP);
1755 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1756 }
1757 break;
1758 }
1759
1760 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001761 Register out = locations->Out().AsRegister<Register>();
1762 if (index.IsConstant()) {
1763 size_t offset =
1764 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1765 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1766 } else {
1767 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1768 __ Addu(TMP, obj, TMP);
1769 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1770 }
1771 break;
1772 }
1773
1774 case Primitive::kPrimInt:
1775 case Primitive::kPrimNot: {
1776 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 Register out = locations->Out().AsRegister<Register>();
1778 if (index.IsConstant()) {
1779 size_t offset =
1780 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1781 __ LoadFromOffset(kLoadWord, out, obj, offset);
1782 } else {
1783 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1784 __ Addu(TMP, obj, TMP);
1785 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1786 }
1787 break;
1788 }
1789
1790 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001791 Register out = locations->Out().AsRegisterPairLow<Register>();
1792 if (index.IsConstant()) {
1793 size_t offset =
1794 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1795 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1796 } else {
1797 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1798 __ Addu(TMP, obj, TMP);
1799 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1800 }
1801 break;
1802 }
1803
1804 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1806 if (index.IsConstant()) {
1807 size_t offset =
1808 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1809 __ LoadSFromOffset(out, obj, offset);
1810 } else {
1811 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1812 __ Addu(TMP, obj, TMP);
1813 __ LoadSFromOffset(out, TMP, data_offset);
1814 }
1815 break;
1816 }
1817
1818 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001819 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1820 if (index.IsConstant()) {
1821 size_t offset =
1822 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1823 __ LoadDFromOffset(out, obj, offset);
1824 } else {
1825 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1826 __ Addu(TMP, obj, TMP);
1827 __ LoadDFromOffset(out, TMP, data_offset);
1828 }
1829 break;
1830 }
1831
1832 case Primitive::kPrimVoid:
1833 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1834 UNREACHABLE();
1835 }
1836 codegen_->MaybeRecordImplicitNullCheck(instruction);
1837}
1838
1839void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1840 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1841 locations->SetInAt(0, Location::RequiresRegister());
1842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1843}
1844
1845void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1846 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001847 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848 Register obj = locations->InAt(0).AsRegister<Register>();
1849 Register out = locations->Out().AsRegister<Register>();
1850 __ LoadFromOffset(kLoadWord, out, obj, offset);
1851 codegen_->MaybeRecordImplicitNullCheck(instruction);
1852}
1853
1854void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001855 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1857 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001858 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001859 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001860 InvokeRuntimeCallingConvention calling_convention;
1861 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1862 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1863 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1864 } else {
1865 locations->SetInAt(0, Location::RequiresRegister());
1866 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1867 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1868 locations->SetInAt(2, Location::RequiresFpuRegister());
1869 } else {
1870 locations->SetInAt(2, Location::RequiresRegister());
1871 }
1872 }
1873}
1874
1875void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1876 LocationSummary* locations = instruction->GetLocations();
1877 Register obj = locations->InAt(0).AsRegister<Register>();
1878 Location index = locations->InAt(1);
1879 Primitive::Type value_type = instruction->GetComponentType();
1880 bool needs_runtime_call = locations->WillCall();
1881 bool needs_write_barrier =
1882 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1883
1884 switch (value_type) {
1885 case Primitive::kPrimBoolean:
1886 case Primitive::kPrimByte: {
1887 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1888 Register value = locations->InAt(2).AsRegister<Register>();
1889 if (index.IsConstant()) {
1890 size_t offset =
1891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1892 __ StoreToOffset(kStoreByte, value, obj, offset);
1893 } else {
1894 __ Addu(TMP, obj, index.AsRegister<Register>());
1895 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1896 }
1897 break;
1898 }
1899
1900 case Primitive::kPrimShort:
1901 case Primitive::kPrimChar: {
1902 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1903 Register value = locations->InAt(2).AsRegister<Register>();
1904 if (index.IsConstant()) {
1905 size_t offset =
1906 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1907 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1908 } else {
1909 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1910 __ Addu(TMP, obj, TMP);
1911 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1912 }
1913 break;
1914 }
1915
1916 case Primitive::kPrimInt:
1917 case Primitive::kPrimNot: {
1918 if (!needs_runtime_call) {
1919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1920 Register value = locations->InAt(2).AsRegister<Register>();
1921 if (index.IsConstant()) {
1922 size_t offset =
1923 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1924 __ StoreToOffset(kStoreWord, value, obj, offset);
1925 } else {
1926 DCHECK(index.IsRegister()) << index;
1927 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1928 __ Addu(TMP, obj, TMP);
1929 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1930 }
1931 codegen_->MaybeRecordImplicitNullCheck(instruction);
1932 if (needs_write_barrier) {
1933 DCHECK_EQ(value_type, Primitive::kPrimNot);
1934 codegen_->MarkGCCard(obj, value);
1935 }
1936 } else {
1937 DCHECK_EQ(value_type, Primitive::kPrimNot);
1938 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1939 instruction,
1940 instruction->GetDexPc(),
1941 nullptr,
1942 IsDirectEntrypoint(kQuickAputObject));
1943 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1944 }
1945 break;
1946 }
1947
1948 case Primitive::kPrimLong: {
1949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1950 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1951 if (index.IsConstant()) {
1952 size_t offset =
1953 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1954 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1955 } else {
1956 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1957 __ Addu(TMP, obj, TMP);
1958 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1959 }
1960 break;
1961 }
1962
1963 case Primitive::kPrimFloat: {
1964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1965 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1966 DCHECK(locations->InAt(2).IsFpuRegister());
1967 if (index.IsConstant()) {
1968 size_t offset =
1969 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1970 __ StoreSToOffset(value, obj, offset);
1971 } else {
1972 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1973 __ Addu(TMP, obj, TMP);
1974 __ StoreSToOffset(value, TMP, data_offset);
1975 }
1976 break;
1977 }
1978
1979 case Primitive::kPrimDouble: {
1980 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1981 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1982 DCHECK(locations->InAt(2).IsFpuRegister());
1983 if (index.IsConstant()) {
1984 size_t offset =
1985 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1986 __ StoreDToOffset(value, obj, offset);
1987 } else {
1988 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1989 __ Addu(TMP, obj, TMP);
1990 __ StoreDToOffset(value, TMP, data_offset);
1991 }
1992 break;
1993 }
1994
1995 case Primitive::kPrimVoid:
1996 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1997 UNREACHABLE();
1998 }
1999
2000 // Ints and objects are handled in the switch.
2001 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
2002 codegen_->MaybeRecordImplicitNullCheck(instruction);
2003 }
2004}
2005
2006void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2007 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2008 ? LocationSummary::kCallOnSlowPath
2009 : LocationSummary::kNoCall;
2010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2011 locations->SetInAt(0, Location::RequiresRegister());
2012 locations->SetInAt(1, Location::RequiresRegister());
2013 if (instruction->HasUses()) {
2014 locations->SetOut(Location::SameAsFirstInput());
2015 }
2016}
2017
2018void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2019 LocationSummary* locations = instruction->GetLocations();
2020 BoundsCheckSlowPathMIPS* slow_path =
2021 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2022 codegen_->AddSlowPath(slow_path);
2023
2024 Register index = locations->InAt(0).AsRegister<Register>();
2025 Register length = locations->InAt(1).AsRegister<Register>();
2026
2027 // length is limited by the maximum positive signed 32-bit integer.
2028 // Unsigned comparison of length and index checks for index < 0
2029 // and for length <= index simultaneously.
2030 __ Bgeu(index, length, slow_path->GetEntryLabel());
2031}
2032
2033void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2034 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2035 instruction,
2036 LocationSummary::kCallOnSlowPath);
2037 locations->SetInAt(0, Location::RequiresRegister());
2038 locations->SetInAt(1, Location::RequiresRegister());
2039 // Note that TypeCheckSlowPathMIPS uses this register too.
2040 locations->AddTemp(Location::RequiresRegister());
2041}
2042
2043void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2044 LocationSummary* locations = instruction->GetLocations();
2045 Register obj = locations->InAt(0).AsRegister<Register>();
2046 Register cls = locations->InAt(1).AsRegister<Register>();
2047 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2048
2049 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2050 codegen_->AddSlowPath(slow_path);
2051
2052 // TODO: avoid this check if we know obj is not null.
2053 __ Beqz(obj, slow_path->GetExitLabel());
2054 // Compare the class of `obj` with `cls`.
2055 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2056 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2057 __ Bind(slow_path->GetExitLabel());
2058}
2059
2060void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 if (check->HasUses()) {
2065 locations->SetOut(Location::SameAsFirstInput());
2066 }
2067}
2068
2069void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2070 // We assume the class is not null.
2071 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2072 check->GetLoadClass(),
2073 check,
2074 check->GetDexPc(),
2075 true);
2076 codegen_->AddSlowPath(slow_path);
2077 GenerateClassInitializationCheck(slow_path,
2078 check->GetLocations()->InAt(0).AsRegister<Register>());
2079}
2080
2081void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2082 Primitive::Type in_type = compare->InputAt(0)->GetType();
2083
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002084 LocationSummary* locations =
2085 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086
2087 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002088 case Primitive::kPrimBoolean:
2089 case Primitive::kPrimByte:
2090 case Primitive::kPrimShort:
2091 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002092 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 case Primitive::kPrimLong:
2094 locations->SetInAt(0, Location::RequiresRegister());
2095 locations->SetInAt(1, Location::RequiresRegister());
2096 // Output overlaps because it is written before doing the low comparison.
2097 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2098 break;
2099
2100 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002101 case Primitive::kPrimDouble:
2102 locations->SetInAt(0, Location::RequiresFpuRegister());
2103 locations->SetInAt(1, Location::RequiresFpuRegister());
2104 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106
2107 default:
2108 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2109 }
2110}
2111
2112void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2113 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002114 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002116 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002117
2118 // 0 if: left == right
2119 // 1 if: left > right
2120 // -1 if: left < right
2121 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002122 case Primitive::kPrimBoolean:
2123 case Primitive::kPrimByte:
2124 case Primitive::kPrimShort:
2125 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002126 case Primitive::kPrimInt: {
2127 Register lhs = locations->InAt(0).AsRegister<Register>();
2128 Register rhs = locations->InAt(1).AsRegister<Register>();
2129 __ Slt(TMP, lhs, rhs);
2130 __ Slt(res, rhs, lhs);
2131 __ Subu(res, res, TMP);
2132 break;
2133 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002134 case Primitive::kPrimLong: {
2135 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2137 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2138 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2139 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2140 // TODO: more efficient (direct) comparison with a constant.
2141 __ Slt(TMP, lhs_high, rhs_high);
2142 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2143 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2144 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2145 __ Sltu(TMP, lhs_low, rhs_low);
2146 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2147 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2148 __ Bind(&done);
2149 break;
2150 }
2151
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002152 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002153 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002154 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2155 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2156 MipsLabel done;
2157 if (isR6) {
2158 __ CmpEqS(FTMP, lhs, rhs);
2159 __ LoadConst32(res, 0);
2160 __ Bc1nez(FTMP, &done);
2161 if (gt_bias) {
2162 __ CmpLtS(FTMP, lhs, rhs);
2163 __ LoadConst32(res, -1);
2164 __ Bc1nez(FTMP, &done);
2165 __ LoadConst32(res, 1);
2166 } else {
2167 __ CmpLtS(FTMP, rhs, lhs);
2168 __ LoadConst32(res, 1);
2169 __ Bc1nez(FTMP, &done);
2170 __ LoadConst32(res, -1);
2171 }
2172 } else {
2173 if (gt_bias) {
2174 __ ColtS(0, lhs, rhs);
2175 __ LoadConst32(res, -1);
2176 __ Bc1t(0, &done);
2177 __ CeqS(0, lhs, rhs);
2178 __ LoadConst32(res, 1);
2179 __ Movt(res, ZERO, 0);
2180 } else {
2181 __ ColtS(0, rhs, lhs);
2182 __ LoadConst32(res, 1);
2183 __ Bc1t(0, &done);
2184 __ CeqS(0, lhs, rhs);
2185 __ LoadConst32(res, -1);
2186 __ Movt(res, ZERO, 0);
2187 }
2188 }
2189 __ Bind(&done);
2190 break;
2191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002192 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002193 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002194 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2195 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2196 MipsLabel done;
2197 if (isR6) {
2198 __ CmpEqD(FTMP, lhs, rhs);
2199 __ LoadConst32(res, 0);
2200 __ Bc1nez(FTMP, &done);
2201 if (gt_bias) {
2202 __ CmpLtD(FTMP, lhs, rhs);
2203 __ LoadConst32(res, -1);
2204 __ Bc1nez(FTMP, &done);
2205 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002206 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002207 __ CmpLtD(FTMP, rhs, lhs);
2208 __ LoadConst32(res, 1);
2209 __ Bc1nez(FTMP, &done);
2210 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002211 }
2212 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002213 if (gt_bias) {
2214 __ ColtD(0, lhs, rhs);
2215 __ LoadConst32(res, -1);
2216 __ Bc1t(0, &done);
2217 __ CeqD(0, lhs, rhs);
2218 __ LoadConst32(res, 1);
2219 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002220 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002221 __ ColtD(0, rhs, lhs);
2222 __ LoadConst32(res, 1);
2223 __ Bc1t(0, &done);
2224 __ CeqD(0, lhs, rhs);
2225 __ LoadConst32(res, -1);
2226 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002227 }
2228 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002229 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230 break;
2231 }
2232
2233 default:
2234 LOG(FATAL) << "Unimplemented compare type " << in_type;
2235 }
2236}
2237
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002238void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002239 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 switch (instruction->InputAt(0)->GetType()) {
2241 default:
2242 case Primitive::kPrimLong:
2243 locations->SetInAt(0, Location::RequiresRegister());
2244 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2245 break;
2246
2247 case Primitive::kPrimFloat:
2248 case Primitive::kPrimDouble:
2249 locations->SetInAt(0, Location::RequiresFpuRegister());
2250 locations->SetInAt(1, Location::RequiresFpuRegister());
2251 break;
2252 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002253 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2255 }
2256}
2257
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002258void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002259 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 return;
2261 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002263 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 LocationSummary* locations = instruction->GetLocations();
2265 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002266 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002268 switch (type) {
2269 default:
2270 // Integer case.
2271 GenerateIntCompare(instruction->GetCondition(), locations);
2272 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002273
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002274 case Primitive::kPrimLong:
2275 // TODO: don't use branches.
2276 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 break;
2278
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002279 case Primitive::kPrimFloat:
2280 case Primitive::kPrimDouble:
2281 // TODO: don't use branches.
2282 GenerateFpCompareAndBranch(instruction->GetCondition(),
2283 instruction->IsGtBias(),
2284 type,
2285 locations,
2286 &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287 break;
2288 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002289
2290 // Convert the branches into the result.
2291 MipsLabel done;
2292
2293 // False case: result = 0.
2294 __ LoadConst32(dst, 0);
2295 __ B(&done);
2296
2297 // True case: result = 1.
2298 __ Bind(&true_label);
2299 __ LoadConst32(dst, 1);
2300 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301}
2302
Alexey Frunze7e99e052015-11-24 19:28:01 -08002303void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2304 DCHECK(instruction->IsDiv() || instruction->IsRem());
2305 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2306
2307 LocationSummary* locations = instruction->GetLocations();
2308 Location second = locations->InAt(1);
2309 DCHECK(second.IsConstant());
2310
2311 Register out = locations->Out().AsRegister<Register>();
2312 Register dividend = locations->InAt(0).AsRegister<Register>();
2313 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2314 DCHECK(imm == 1 || imm == -1);
2315
2316 if (instruction->IsRem()) {
2317 __ Move(out, ZERO);
2318 } else {
2319 if (imm == -1) {
2320 __ Subu(out, ZERO, dividend);
2321 } else if (out != dividend) {
2322 __ Move(out, dividend);
2323 }
2324 }
2325}
2326
2327void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2328 DCHECK(instruction->IsDiv() || instruction->IsRem());
2329 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2330
2331 LocationSummary* locations = instruction->GetLocations();
2332 Location second = locations->InAt(1);
2333 DCHECK(second.IsConstant());
2334
2335 Register out = locations->Out().AsRegister<Register>();
2336 Register dividend = locations->InAt(0).AsRegister<Register>();
2337 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002338 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002339 int ctz_imm = CTZ(abs_imm);
2340
2341 if (instruction->IsDiv()) {
2342 if (ctz_imm == 1) {
2343 // Fast path for division by +/-2, which is very common.
2344 __ Srl(TMP, dividend, 31);
2345 } else {
2346 __ Sra(TMP, dividend, 31);
2347 __ Srl(TMP, TMP, 32 - ctz_imm);
2348 }
2349 __ Addu(out, dividend, TMP);
2350 __ Sra(out, out, ctz_imm);
2351 if (imm < 0) {
2352 __ Subu(out, ZERO, out);
2353 }
2354 } else {
2355 if (ctz_imm == 1) {
2356 // Fast path for modulo +/-2, which is very common.
2357 __ Sra(TMP, dividend, 31);
2358 __ Subu(out, dividend, TMP);
2359 __ Andi(out, out, 1);
2360 __ Addu(out, out, TMP);
2361 } else {
2362 __ Sra(TMP, dividend, 31);
2363 __ Srl(TMP, TMP, 32 - ctz_imm);
2364 __ Addu(out, dividend, TMP);
2365 if (IsUint<16>(abs_imm - 1)) {
2366 __ Andi(out, out, abs_imm - 1);
2367 } else {
2368 __ Sll(out, out, 32 - ctz_imm);
2369 __ Srl(out, out, 32 - ctz_imm);
2370 }
2371 __ Subu(out, out, TMP);
2372 }
2373 }
2374}
2375
2376void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2377 DCHECK(instruction->IsDiv() || instruction->IsRem());
2378 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2379
2380 LocationSummary* locations = instruction->GetLocations();
2381 Location second = locations->InAt(1);
2382 DCHECK(second.IsConstant());
2383
2384 Register out = locations->Out().AsRegister<Register>();
2385 Register dividend = locations->InAt(0).AsRegister<Register>();
2386 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2387
2388 int64_t magic;
2389 int shift;
2390 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2391
2392 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2393
2394 __ LoadConst32(TMP, magic);
2395 if (isR6) {
2396 __ MuhR6(TMP, dividend, TMP);
2397 } else {
2398 __ MultR2(dividend, TMP);
2399 __ Mfhi(TMP);
2400 }
2401 if (imm > 0 && magic < 0) {
2402 __ Addu(TMP, TMP, dividend);
2403 } else if (imm < 0 && magic > 0) {
2404 __ Subu(TMP, TMP, dividend);
2405 }
2406
2407 if (shift != 0) {
2408 __ Sra(TMP, TMP, shift);
2409 }
2410
2411 if (instruction->IsDiv()) {
2412 __ Sra(out, TMP, 31);
2413 __ Subu(out, TMP, out);
2414 } else {
2415 __ Sra(AT, TMP, 31);
2416 __ Subu(AT, TMP, AT);
2417 __ LoadConst32(TMP, imm);
2418 if (isR6) {
2419 __ MulR6(TMP, AT, TMP);
2420 } else {
2421 __ MulR2(TMP, AT, TMP);
2422 }
2423 __ Subu(out, dividend, TMP);
2424 }
2425}
2426
2427void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2428 DCHECK(instruction->IsDiv() || instruction->IsRem());
2429 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2430
2431 LocationSummary* locations = instruction->GetLocations();
2432 Register out = locations->Out().AsRegister<Register>();
2433 Location second = locations->InAt(1);
2434
2435 if (second.IsConstant()) {
2436 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2437 if (imm == 0) {
2438 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2439 } else if (imm == 1 || imm == -1) {
2440 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002441 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002442 DivRemByPowerOfTwo(instruction);
2443 } else {
2444 DCHECK(imm <= -2 || imm >= 2);
2445 GenerateDivRemWithAnyConstant(instruction);
2446 }
2447 } else {
2448 Register dividend = locations->InAt(0).AsRegister<Register>();
2449 Register divisor = second.AsRegister<Register>();
2450 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2451 if (instruction->IsDiv()) {
2452 if (isR6) {
2453 __ DivR6(out, dividend, divisor);
2454 } else {
2455 __ DivR2(out, dividend, divisor);
2456 }
2457 } else {
2458 if (isR6) {
2459 __ ModR6(out, dividend, divisor);
2460 } else {
2461 __ ModR2(out, dividend, divisor);
2462 }
2463 }
2464 }
2465}
2466
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002467void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2468 Primitive::Type type = div->GetResultType();
2469 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002470 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002471 : LocationSummary::kNoCall;
2472
2473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2474
2475 switch (type) {
2476 case Primitive::kPrimInt:
2477 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002478 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2480 break;
2481
2482 case Primitive::kPrimLong: {
2483 InvokeRuntimeCallingConvention calling_convention;
2484 locations->SetInAt(0, Location::RegisterPairLocation(
2485 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2486 locations->SetInAt(1, Location::RegisterPairLocation(
2487 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2488 locations->SetOut(calling_convention.GetReturnLocation(type));
2489 break;
2490 }
2491
2492 case Primitive::kPrimFloat:
2493 case Primitive::kPrimDouble:
2494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetInAt(1, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected div type " << type;
2501 }
2502}
2503
2504void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2505 Primitive::Type type = instruction->GetType();
2506 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507
2508 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002509 case Primitive::kPrimInt:
2510 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002512 case Primitive::kPrimLong: {
2513 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2514 instruction,
2515 instruction->GetDexPc(),
2516 nullptr,
2517 IsDirectEntrypoint(kQuickLdiv));
2518 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2519 break;
2520 }
2521 case Primitive::kPrimFloat:
2522 case Primitive::kPrimDouble: {
2523 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2524 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2525 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2526 if (type == Primitive::kPrimFloat) {
2527 __ DivS(dst, lhs, rhs);
2528 } else {
2529 __ DivD(dst, lhs, rhs);
2530 }
2531 break;
2532 }
2533 default:
2534 LOG(FATAL) << "Unexpected div type " << type;
2535 }
2536}
2537
2538void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2539 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2540 ? LocationSummary::kCallOnSlowPath
2541 : LocationSummary::kNoCall;
2542 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2543 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2544 if (instruction->HasUses()) {
2545 locations->SetOut(Location::SameAsFirstInput());
2546 }
2547}
2548
2549void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2550 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2551 codegen_->AddSlowPath(slow_path);
2552 Location value = instruction->GetLocations()->InAt(0);
2553 Primitive::Type type = instruction->GetType();
2554
2555 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002556 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002557 case Primitive::kPrimByte:
2558 case Primitive::kPrimChar:
2559 case Primitive::kPrimShort:
2560 case Primitive::kPrimInt: {
2561 if (value.IsConstant()) {
2562 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2563 __ B(slow_path->GetEntryLabel());
2564 } else {
2565 // A division by a non-null constant is valid. We don't need to perform
2566 // any check, so simply fall through.
2567 }
2568 } else {
2569 DCHECK(value.IsRegister()) << value;
2570 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2571 }
2572 break;
2573 }
2574 case Primitive::kPrimLong: {
2575 if (value.IsConstant()) {
2576 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2577 __ B(slow_path->GetEntryLabel());
2578 } else {
2579 // A division by a non-null constant is valid. We don't need to perform
2580 // any check, so simply fall through.
2581 }
2582 } else {
2583 DCHECK(value.IsRegisterPair()) << value;
2584 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2585 __ Beqz(TMP, slow_path->GetEntryLabel());
2586 }
2587 break;
2588 }
2589 default:
2590 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2591 }
2592}
2593
2594void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2595 LocationSummary* locations =
2596 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2597 locations->SetOut(Location::ConstantLocation(constant));
2598}
2599
2600void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2601 // Will be generated at use site.
2602}
2603
2604void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2605 exit->SetLocations(nullptr);
2606}
2607
2608void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2609}
2610
2611void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2612 LocationSummary* locations =
2613 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2614 locations->SetOut(Location::ConstantLocation(constant));
2615}
2616
2617void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2618 // Will be generated at use site.
2619}
2620
2621void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2622 got->SetLocations(nullptr);
2623}
2624
2625void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2626 DCHECK(!successor->IsExitBlock());
2627 HBasicBlock* block = got->GetBlock();
2628 HInstruction* previous = got->GetPrevious();
2629 HLoopInformation* info = block->GetLoopInformation();
2630
2631 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2632 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2633 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2634 return;
2635 }
2636 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2637 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2638 }
2639 if (!codegen_->GoesToNextBlock(block, successor)) {
2640 __ B(codegen_->GetLabelOf(successor));
2641 }
2642}
2643
2644void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2645 HandleGoto(got, got->GetSuccessor());
2646}
2647
2648void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2649 try_boundary->SetLocations(nullptr);
2650}
2651
2652void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2653 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2654 if (!successor->IsExitBlock()) {
2655 HandleGoto(try_boundary, successor);
2656 }
2657}
2658
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002659void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2660 LocationSummary* locations) {
2661 Register dst = locations->Out().AsRegister<Register>();
2662 Register lhs = locations->InAt(0).AsRegister<Register>();
2663 Location rhs_location = locations->InAt(1);
2664 Register rhs_reg = ZERO;
2665 int64_t rhs_imm = 0;
2666 bool use_imm = rhs_location.IsConstant();
2667 if (use_imm) {
2668 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2669 } else {
2670 rhs_reg = rhs_location.AsRegister<Register>();
2671 }
2672
2673 switch (cond) {
2674 case kCondEQ:
2675 case kCondNE:
2676 if (use_imm && IsUint<16>(rhs_imm)) {
2677 __ Xori(dst, lhs, rhs_imm);
2678 } else {
2679 if (use_imm) {
2680 rhs_reg = TMP;
2681 __ LoadConst32(rhs_reg, rhs_imm);
2682 }
2683 __ Xor(dst, lhs, rhs_reg);
2684 }
2685 if (cond == kCondEQ) {
2686 __ Sltiu(dst, dst, 1);
2687 } else {
2688 __ Sltu(dst, ZERO, dst);
2689 }
2690 break;
2691
2692 case kCondLT:
2693 case kCondGE:
2694 if (use_imm && IsInt<16>(rhs_imm)) {
2695 __ Slti(dst, lhs, rhs_imm);
2696 } else {
2697 if (use_imm) {
2698 rhs_reg = TMP;
2699 __ LoadConst32(rhs_reg, rhs_imm);
2700 }
2701 __ Slt(dst, lhs, rhs_reg);
2702 }
2703 if (cond == kCondGE) {
2704 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2705 // only the slt instruction but no sge.
2706 __ Xori(dst, dst, 1);
2707 }
2708 break;
2709
2710 case kCondLE:
2711 case kCondGT:
2712 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2713 // Simulate lhs <= rhs via lhs < rhs + 1.
2714 __ Slti(dst, lhs, rhs_imm + 1);
2715 if (cond == kCondGT) {
2716 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2717 // only the slti instruction but no sgti.
2718 __ Xori(dst, dst, 1);
2719 }
2720 } else {
2721 if (use_imm) {
2722 rhs_reg = TMP;
2723 __ LoadConst32(rhs_reg, rhs_imm);
2724 }
2725 __ Slt(dst, rhs_reg, lhs);
2726 if (cond == kCondLE) {
2727 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2728 // only the slt instruction but no sle.
2729 __ Xori(dst, dst, 1);
2730 }
2731 }
2732 break;
2733
2734 case kCondB:
2735 case kCondAE:
2736 if (use_imm && IsInt<16>(rhs_imm)) {
2737 // Sltiu sign-extends its 16-bit immediate operand before
2738 // the comparison and thus lets us compare directly with
2739 // unsigned values in the ranges [0, 0x7fff] and
2740 // [0xffff8000, 0xffffffff].
2741 __ Sltiu(dst, lhs, rhs_imm);
2742 } else {
2743 if (use_imm) {
2744 rhs_reg = TMP;
2745 __ LoadConst32(rhs_reg, rhs_imm);
2746 }
2747 __ Sltu(dst, lhs, rhs_reg);
2748 }
2749 if (cond == kCondAE) {
2750 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2751 // only the sltu instruction but no sgeu.
2752 __ Xori(dst, dst, 1);
2753 }
2754 break;
2755
2756 case kCondBE:
2757 case kCondA:
2758 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2759 // Simulate lhs <= rhs via lhs < rhs + 1.
2760 // Note that this only works if rhs + 1 does not overflow
2761 // to 0, hence the check above.
2762 // Sltiu sign-extends its 16-bit immediate operand before
2763 // the comparison and thus lets us compare directly with
2764 // unsigned values in the ranges [0, 0x7fff] and
2765 // [0xffff8000, 0xffffffff].
2766 __ Sltiu(dst, lhs, rhs_imm + 1);
2767 if (cond == kCondA) {
2768 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2769 // only the sltiu instruction but no sgtiu.
2770 __ Xori(dst, dst, 1);
2771 }
2772 } else {
2773 if (use_imm) {
2774 rhs_reg = TMP;
2775 __ LoadConst32(rhs_reg, rhs_imm);
2776 }
2777 __ Sltu(dst, rhs_reg, lhs);
2778 if (cond == kCondBE) {
2779 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2780 // only the sltu instruction but no sleu.
2781 __ Xori(dst, dst, 1);
2782 }
2783 }
2784 break;
2785 }
2786}
2787
2788void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2789 LocationSummary* locations,
2790 MipsLabel* label) {
2791 Register lhs = locations->InAt(0).AsRegister<Register>();
2792 Location rhs_location = locations->InAt(1);
2793 Register rhs_reg = ZERO;
2794 int32_t rhs_imm = 0;
2795 bool use_imm = rhs_location.IsConstant();
2796 if (use_imm) {
2797 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2798 } else {
2799 rhs_reg = rhs_location.AsRegister<Register>();
2800 }
2801
2802 if (use_imm && rhs_imm == 0) {
2803 switch (cond) {
2804 case kCondEQ:
2805 case kCondBE: // <= 0 if zero
2806 __ Beqz(lhs, label);
2807 break;
2808 case kCondNE:
2809 case kCondA: // > 0 if non-zero
2810 __ Bnez(lhs, label);
2811 break;
2812 case kCondLT:
2813 __ Bltz(lhs, label);
2814 break;
2815 case kCondGE:
2816 __ Bgez(lhs, label);
2817 break;
2818 case kCondLE:
2819 __ Blez(lhs, label);
2820 break;
2821 case kCondGT:
2822 __ Bgtz(lhs, label);
2823 break;
2824 case kCondB: // always false
2825 break;
2826 case kCondAE: // always true
2827 __ B(label);
2828 break;
2829 }
2830 } else {
2831 if (use_imm) {
2832 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2833 rhs_reg = TMP;
2834 __ LoadConst32(rhs_reg, rhs_imm);
2835 }
2836 switch (cond) {
2837 case kCondEQ:
2838 __ Beq(lhs, rhs_reg, label);
2839 break;
2840 case kCondNE:
2841 __ Bne(lhs, rhs_reg, label);
2842 break;
2843 case kCondLT:
2844 __ Blt(lhs, rhs_reg, label);
2845 break;
2846 case kCondGE:
2847 __ Bge(lhs, rhs_reg, label);
2848 break;
2849 case kCondLE:
2850 __ Bge(rhs_reg, lhs, label);
2851 break;
2852 case kCondGT:
2853 __ Blt(rhs_reg, lhs, label);
2854 break;
2855 case kCondB:
2856 __ Bltu(lhs, rhs_reg, label);
2857 break;
2858 case kCondAE:
2859 __ Bgeu(lhs, rhs_reg, label);
2860 break;
2861 case kCondBE:
2862 __ Bgeu(rhs_reg, lhs, label);
2863 break;
2864 case kCondA:
2865 __ Bltu(rhs_reg, lhs, label);
2866 break;
2867 }
2868 }
2869}
2870
2871void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
2872 LocationSummary* locations,
2873 MipsLabel* label) {
2874 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2875 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2876 Location rhs_location = locations->InAt(1);
2877 Register rhs_high = ZERO;
2878 Register rhs_low = ZERO;
2879 int64_t imm = 0;
2880 uint32_t imm_high = 0;
2881 uint32_t imm_low = 0;
2882 bool use_imm = rhs_location.IsConstant();
2883 if (use_imm) {
2884 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
2885 imm_high = High32Bits(imm);
2886 imm_low = Low32Bits(imm);
2887 } else {
2888 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2889 rhs_low = rhs_location.AsRegisterPairLow<Register>();
2890 }
2891
2892 if (use_imm && imm == 0) {
2893 switch (cond) {
2894 case kCondEQ:
2895 case kCondBE: // <= 0 if zero
2896 __ Or(TMP, lhs_high, lhs_low);
2897 __ Beqz(TMP, label);
2898 break;
2899 case kCondNE:
2900 case kCondA: // > 0 if non-zero
2901 __ Or(TMP, lhs_high, lhs_low);
2902 __ Bnez(TMP, label);
2903 break;
2904 case kCondLT:
2905 __ Bltz(lhs_high, label);
2906 break;
2907 case kCondGE:
2908 __ Bgez(lhs_high, label);
2909 break;
2910 case kCondLE:
2911 __ Or(TMP, lhs_high, lhs_low);
2912 __ Sra(AT, lhs_high, 31);
2913 __ Bgeu(AT, TMP, label);
2914 break;
2915 case kCondGT:
2916 __ Or(TMP, lhs_high, lhs_low);
2917 __ Sra(AT, lhs_high, 31);
2918 __ Bltu(AT, TMP, label);
2919 break;
2920 case kCondB: // always false
2921 break;
2922 case kCondAE: // always true
2923 __ B(label);
2924 break;
2925 }
2926 } else if (use_imm) {
2927 // TODO: more efficient comparison with constants without loading them into TMP/AT.
2928 switch (cond) {
2929 case kCondEQ:
2930 __ LoadConst32(TMP, imm_high);
2931 __ Xor(TMP, TMP, lhs_high);
2932 __ LoadConst32(AT, imm_low);
2933 __ Xor(AT, AT, lhs_low);
2934 __ Or(TMP, TMP, AT);
2935 __ Beqz(TMP, label);
2936 break;
2937 case kCondNE:
2938 __ LoadConst32(TMP, imm_high);
2939 __ Xor(TMP, TMP, lhs_high);
2940 __ LoadConst32(AT, imm_low);
2941 __ Xor(AT, AT, lhs_low);
2942 __ Or(TMP, TMP, AT);
2943 __ Bnez(TMP, label);
2944 break;
2945 case kCondLT:
2946 __ LoadConst32(TMP, imm_high);
2947 __ Blt(lhs_high, TMP, label);
2948 __ Slt(TMP, TMP, lhs_high);
2949 __ LoadConst32(AT, imm_low);
2950 __ Sltu(AT, lhs_low, AT);
2951 __ Blt(TMP, AT, label);
2952 break;
2953 case kCondGE:
2954 __ LoadConst32(TMP, imm_high);
2955 __ Blt(TMP, lhs_high, label);
2956 __ Slt(TMP, lhs_high, TMP);
2957 __ LoadConst32(AT, imm_low);
2958 __ Sltu(AT, lhs_low, AT);
2959 __ Or(TMP, TMP, AT);
2960 __ Beqz(TMP, label);
2961 break;
2962 case kCondLE:
2963 __ LoadConst32(TMP, imm_high);
2964 __ Blt(lhs_high, TMP, label);
2965 __ Slt(TMP, TMP, lhs_high);
2966 __ LoadConst32(AT, imm_low);
2967 __ Sltu(AT, AT, lhs_low);
2968 __ Or(TMP, TMP, AT);
2969 __ Beqz(TMP, label);
2970 break;
2971 case kCondGT:
2972 __ LoadConst32(TMP, imm_high);
2973 __ Blt(TMP, lhs_high, label);
2974 __ Slt(TMP, lhs_high, TMP);
2975 __ LoadConst32(AT, imm_low);
2976 __ Sltu(AT, AT, lhs_low);
2977 __ Blt(TMP, AT, label);
2978 break;
2979 case kCondB:
2980 __ LoadConst32(TMP, imm_high);
2981 __ Bltu(lhs_high, TMP, label);
2982 __ Sltu(TMP, TMP, lhs_high);
2983 __ LoadConst32(AT, imm_low);
2984 __ Sltu(AT, lhs_low, AT);
2985 __ Blt(TMP, AT, label);
2986 break;
2987 case kCondAE:
2988 __ LoadConst32(TMP, imm_high);
2989 __ Bltu(TMP, lhs_high, label);
2990 __ Sltu(TMP, lhs_high, TMP);
2991 __ LoadConst32(AT, imm_low);
2992 __ Sltu(AT, lhs_low, AT);
2993 __ Or(TMP, TMP, AT);
2994 __ Beqz(TMP, label);
2995 break;
2996 case kCondBE:
2997 __ LoadConst32(TMP, imm_high);
2998 __ Bltu(lhs_high, TMP, label);
2999 __ Sltu(TMP, TMP, lhs_high);
3000 __ LoadConst32(AT, imm_low);
3001 __ Sltu(AT, AT, lhs_low);
3002 __ Or(TMP, TMP, AT);
3003 __ Beqz(TMP, label);
3004 break;
3005 case kCondA:
3006 __ LoadConst32(TMP, imm_high);
3007 __ Bltu(TMP, lhs_high, label);
3008 __ Sltu(TMP, lhs_high, TMP);
3009 __ LoadConst32(AT, imm_low);
3010 __ Sltu(AT, AT, lhs_low);
3011 __ Blt(TMP, AT, label);
3012 break;
3013 }
3014 } else {
3015 switch (cond) {
3016 case kCondEQ:
3017 __ Xor(TMP, lhs_high, rhs_high);
3018 __ Xor(AT, lhs_low, rhs_low);
3019 __ Or(TMP, TMP, AT);
3020 __ Beqz(TMP, label);
3021 break;
3022 case kCondNE:
3023 __ Xor(TMP, lhs_high, rhs_high);
3024 __ Xor(AT, lhs_low, rhs_low);
3025 __ Or(TMP, TMP, AT);
3026 __ Bnez(TMP, label);
3027 break;
3028 case kCondLT:
3029 __ Blt(lhs_high, rhs_high, label);
3030 __ Slt(TMP, rhs_high, lhs_high);
3031 __ Sltu(AT, lhs_low, rhs_low);
3032 __ Blt(TMP, AT, label);
3033 break;
3034 case kCondGE:
3035 __ Blt(rhs_high, lhs_high, label);
3036 __ Slt(TMP, lhs_high, rhs_high);
3037 __ Sltu(AT, lhs_low, rhs_low);
3038 __ Or(TMP, TMP, AT);
3039 __ Beqz(TMP, label);
3040 break;
3041 case kCondLE:
3042 __ Blt(lhs_high, rhs_high, label);
3043 __ Slt(TMP, rhs_high, lhs_high);
3044 __ Sltu(AT, rhs_low, lhs_low);
3045 __ Or(TMP, TMP, AT);
3046 __ Beqz(TMP, label);
3047 break;
3048 case kCondGT:
3049 __ Blt(rhs_high, lhs_high, label);
3050 __ Slt(TMP, lhs_high, rhs_high);
3051 __ Sltu(AT, rhs_low, lhs_low);
3052 __ Blt(TMP, AT, label);
3053 break;
3054 case kCondB:
3055 __ Bltu(lhs_high, rhs_high, label);
3056 __ Sltu(TMP, rhs_high, lhs_high);
3057 __ Sltu(AT, lhs_low, rhs_low);
3058 __ Blt(TMP, AT, label);
3059 break;
3060 case kCondAE:
3061 __ Bltu(rhs_high, lhs_high, label);
3062 __ Sltu(TMP, lhs_high, rhs_high);
3063 __ Sltu(AT, lhs_low, rhs_low);
3064 __ Or(TMP, TMP, AT);
3065 __ Beqz(TMP, label);
3066 break;
3067 case kCondBE:
3068 __ Bltu(lhs_high, rhs_high, label);
3069 __ Sltu(TMP, rhs_high, lhs_high);
3070 __ Sltu(AT, rhs_low, lhs_low);
3071 __ Or(TMP, TMP, AT);
3072 __ Beqz(TMP, label);
3073 break;
3074 case kCondA:
3075 __ Bltu(rhs_high, lhs_high, label);
3076 __ Sltu(TMP, lhs_high, rhs_high);
3077 __ Sltu(AT, rhs_low, lhs_low);
3078 __ Blt(TMP, AT, label);
3079 break;
3080 }
3081 }
3082}
3083
3084void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3085 bool gt_bias,
3086 Primitive::Type type,
3087 LocationSummary* locations,
3088 MipsLabel* label) {
3089 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3090 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3091 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3092 if (type == Primitive::kPrimFloat) {
3093 if (isR6) {
3094 switch (cond) {
3095 case kCondEQ:
3096 __ CmpEqS(FTMP, lhs, rhs);
3097 __ Bc1nez(FTMP, label);
3098 break;
3099 case kCondNE:
3100 __ CmpEqS(FTMP, lhs, rhs);
3101 __ Bc1eqz(FTMP, label);
3102 break;
3103 case kCondLT:
3104 if (gt_bias) {
3105 __ CmpLtS(FTMP, lhs, rhs);
3106 } else {
3107 __ CmpUltS(FTMP, lhs, rhs);
3108 }
3109 __ Bc1nez(FTMP, label);
3110 break;
3111 case kCondLE:
3112 if (gt_bias) {
3113 __ CmpLeS(FTMP, lhs, rhs);
3114 } else {
3115 __ CmpUleS(FTMP, lhs, rhs);
3116 }
3117 __ Bc1nez(FTMP, label);
3118 break;
3119 case kCondGT:
3120 if (gt_bias) {
3121 __ CmpUltS(FTMP, rhs, lhs);
3122 } else {
3123 __ CmpLtS(FTMP, rhs, lhs);
3124 }
3125 __ Bc1nez(FTMP, label);
3126 break;
3127 case kCondGE:
3128 if (gt_bias) {
3129 __ CmpUleS(FTMP, rhs, lhs);
3130 } else {
3131 __ CmpLeS(FTMP, rhs, lhs);
3132 }
3133 __ Bc1nez(FTMP, label);
3134 break;
3135 default:
3136 LOG(FATAL) << "Unexpected non-floating-point condition";
3137 }
3138 } else {
3139 switch (cond) {
3140 case kCondEQ:
3141 __ CeqS(0, lhs, rhs);
3142 __ Bc1t(0, label);
3143 break;
3144 case kCondNE:
3145 __ CeqS(0, lhs, rhs);
3146 __ Bc1f(0, label);
3147 break;
3148 case kCondLT:
3149 if (gt_bias) {
3150 __ ColtS(0, lhs, rhs);
3151 } else {
3152 __ CultS(0, lhs, rhs);
3153 }
3154 __ Bc1t(0, label);
3155 break;
3156 case kCondLE:
3157 if (gt_bias) {
3158 __ ColeS(0, lhs, rhs);
3159 } else {
3160 __ CuleS(0, lhs, rhs);
3161 }
3162 __ Bc1t(0, label);
3163 break;
3164 case kCondGT:
3165 if (gt_bias) {
3166 __ CultS(0, rhs, lhs);
3167 } else {
3168 __ ColtS(0, rhs, lhs);
3169 }
3170 __ Bc1t(0, label);
3171 break;
3172 case kCondGE:
3173 if (gt_bias) {
3174 __ CuleS(0, rhs, lhs);
3175 } else {
3176 __ ColeS(0, rhs, lhs);
3177 }
3178 __ Bc1t(0, label);
3179 break;
3180 default:
3181 LOG(FATAL) << "Unexpected non-floating-point condition";
3182 }
3183 }
3184 } else {
3185 DCHECK_EQ(type, Primitive::kPrimDouble);
3186 if (isR6) {
3187 switch (cond) {
3188 case kCondEQ:
3189 __ CmpEqD(FTMP, lhs, rhs);
3190 __ Bc1nez(FTMP, label);
3191 break;
3192 case kCondNE:
3193 __ CmpEqD(FTMP, lhs, rhs);
3194 __ Bc1eqz(FTMP, label);
3195 break;
3196 case kCondLT:
3197 if (gt_bias) {
3198 __ CmpLtD(FTMP, lhs, rhs);
3199 } else {
3200 __ CmpUltD(FTMP, lhs, rhs);
3201 }
3202 __ Bc1nez(FTMP, label);
3203 break;
3204 case kCondLE:
3205 if (gt_bias) {
3206 __ CmpLeD(FTMP, lhs, rhs);
3207 } else {
3208 __ CmpUleD(FTMP, lhs, rhs);
3209 }
3210 __ Bc1nez(FTMP, label);
3211 break;
3212 case kCondGT:
3213 if (gt_bias) {
3214 __ CmpUltD(FTMP, rhs, lhs);
3215 } else {
3216 __ CmpLtD(FTMP, rhs, lhs);
3217 }
3218 __ Bc1nez(FTMP, label);
3219 break;
3220 case kCondGE:
3221 if (gt_bias) {
3222 __ CmpUleD(FTMP, rhs, lhs);
3223 } else {
3224 __ CmpLeD(FTMP, rhs, lhs);
3225 }
3226 __ Bc1nez(FTMP, label);
3227 break;
3228 default:
3229 LOG(FATAL) << "Unexpected non-floating-point condition";
3230 }
3231 } else {
3232 switch (cond) {
3233 case kCondEQ:
3234 __ CeqD(0, lhs, rhs);
3235 __ Bc1t(0, label);
3236 break;
3237 case kCondNE:
3238 __ CeqD(0, lhs, rhs);
3239 __ Bc1f(0, label);
3240 break;
3241 case kCondLT:
3242 if (gt_bias) {
3243 __ ColtD(0, lhs, rhs);
3244 } else {
3245 __ CultD(0, lhs, rhs);
3246 }
3247 __ Bc1t(0, label);
3248 break;
3249 case kCondLE:
3250 if (gt_bias) {
3251 __ ColeD(0, lhs, rhs);
3252 } else {
3253 __ CuleD(0, lhs, rhs);
3254 }
3255 __ Bc1t(0, label);
3256 break;
3257 case kCondGT:
3258 if (gt_bias) {
3259 __ CultD(0, rhs, lhs);
3260 } else {
3261 __ ColtD(0, rhs, lhs);
3262 }
3263 __ Bc1t(0, label);
3264 break;
3265 case kCondGE:
3266 if (gt_bias) {
3267 __ CuleD(0, rhs, lhs);
3268 } else {
3269 __ ColeD(0, rhs, lhs);
3270 }
3271 __ Bc1t(0, label);
3272 break;
3273 default:
3274 LOG(FATAL) << "Unexpected non-floating-point condition";
3275 }
3276 }
3277 }
3278}
3279
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003280void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003281 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003282 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003283 MipsLabel* false_target) {
3284 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003285
David Brazdil0debae72015-11-12 18:37:00 +00003286 if (true_target == nullptr && false_target == nullptr) {
3287 // Nothing to do. The code always falls through.
3288 return;
3289 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003290 // Constant condition, statically compared against "true" (integer value 1).
3291 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003292 if (true_target != nullptr) {
3293 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003294 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003295 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003296 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003297 if (false_target != nullptr) {
3298 __ B(false_target);
3299 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 }
David Brazdil0debae72015-11-12 18:37:00 +00003301 return;
3302 }
3303
3304 // The following code generates these patterns:
3305 // (1) true_target == nullptr && false_target != nullptr
3306 // - opposite condition true => branch to false_target
3307 // (2) true_target != nullptr && false_target == nullptr
3308 // - condition true => branch to true_target
3309 // (3) true_target != nullptr && false_target != nullptr
3310 // - condition true => branch to true_target
3311 // - branch to false_target
3312 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003313 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003314 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003315 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003316 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003317 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3318 } else {
3319 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3320 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003321 } else {
3322 // The condition instruction has not been materialized, use its inputs as
3323 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003324 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003325 Primitive::Type type = condition->InputAt(0)->GetType();
3326 LocationSummary* locations = cond->GetLocations();
3327 IfCondition if_cond = condition->GetCondition();
3328 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003329
David Brazdil0debae72015-11-12 18:37:00 +00003330 if (true_target == nullptr) {
3331 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003332 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003333 }
3334
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003335 switch (type) {
3336 default:
3337 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3338 break;
3339 case Primitive::kPrimLong:
3340 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3341 break;
3342 case Primitive::kPrimFloat:
3343 case Primitive::kPrimDouble:
3344 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3345 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003346 }
3347 }
David Brazdil0debae72015-11-12 18:37:00 +00003348
3349 // If neither branch falls through (case 3), the conditional branch to `true_target`
3350 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3351 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003352 __ B(false_target);
3353 }
3354}
3355
3356void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003358 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003359 locations->SetInAt(0, Location::RequiresRegister());
3360 }
3361}
3362
3363void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003364 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3365 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3366 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3367 nullptr : codegen_->GetLabelOf(true_successor);
3368 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3369 nullptr : codegen_->GetLabelOf(false_successor);
3370 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003371}
3372
3373void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3374 LocationSummary* locations = new (GetGraph()->GetArena())
3375 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00003376 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003377 locations->SetInAt(0, Location::RequiresRegister());
3378 }
3379}
3380
3381void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003382 SlowPathCodeMIPS* slow_path =
3383 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003384 GenerateTestAndBranch(deoptimize,
3385 /* condition_input_index */ 0,
3386 slow_path->GetEntryLabel(),
3387 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003388}
3389
David Brazdil74eb1b22015-12-14 11:44:01 +00003390void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3391 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3392 if (Primitive::IsFloatingPointType(select->GetType())) {
3393 locations->SetInAt(0, Location::RequiresFpuRegister());
3394 locations->SetInAt(1, Location::RequiresFpuRegister());
3395 } else {
3396 locations->SetInAt(0, Location::RequiresRegister());
3397 locations->SetInAt(1, Location::RequiresRegister());
3398 }
3399 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3400 locations->SetInAt(2, Location::RequiresRegister());
3401 }
3402 locations->SetOut(Location::SameAsFirstInput());
3403}
3404
3405void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3406 LocationSummary* locations = select->GetLocations();
3407 MipsLabel false_target;
3408 GenerateTestAndBranch(select,
3409 /* condition_input_index */ 2,
3410 /* true_target */ nullptr,
3411 &false_target);
3412 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3413 __ Bind(&false_target);
3414}
3415
David Srbecky0cf44932015-12-09 14:09:59 +00003416void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3417 new (GetGraph()->GetArena()) LocationSummary(info);
3418}
3419
David Srbeckyd28f4a02016-03-14 17:14:24 +00003420void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3421 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003422}
3423
3424void CodeGeneratorMIPS::GenerateNop() {
3425 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003426}
3427
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003428void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3429 Primitive::Type field_type = field_info.GetFieldType();
3430 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3431 bool generate_volatile = field_info.IsVolatile() && is_wide;
3432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003433 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003434
3435 locations->SetInAt(0, Location::RequiresRegister());
3436 if (generate_volatile) {
3437 InvokeRuntimeCallingConvention calling_convention;
3438 // need A0 to hold base + offset
3439 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3440 if (field_type == Primitive::kPrimLong) {
3441 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3442 } else {
3443 locations->SetOut(Location::RequiresFpuRegister());
3444 // Need some temp core regs since FP results are returned in core registers
3445 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3446 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3447 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3448 }
3449 } else {
3450 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3451 locations->SetOut(Location::RequiresFpuRegister());
3452 } else {
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454 }
3455 }
3456}
3457
3458void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3459 const FieldInfo& field_info,
3460 uint32_t dex_pc) {
3461 Primitive::Type type = field_info.GetFieldType();
3462 LocationSummary* locations = instruction->GetLocations();
3463 Register obj = locations->InAt(0).AsRegister<Register>();
3464 LoadOperandType load_type = kLoadUnsignedByte;
3465 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003466 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467
3468 switch (type) {
3469 case Primitive::kPrimBoolean:
3470 load_type = kLoadUnsignedByte;
3471 break;
3472 case Primitive::kPrimByte:
3473 load_type = kLoadSignedByte;
3474 break;
3475 case Primitive::kPrimShort:
3476 load_type = kLoadSignedHalfword;
3477 break;
3478 case Primitive::kPrimChar:
3479 load_type = kLoadUnsignedHalfword;
3480 break;
3481 case Primitive::kPrimInt:
3482 case Primitive::kPrimFloat:
3483 case Primitive::kPrimNot:
3484 load_type = kLoadWord;
3485 break;
3486 case Primitive::kPrimLong:
3487 case Primitive::kPrimDouble:
3488 load_type = kLoadDoubleword;
3489 break;
3490 case Primitive::kPrimVoid:
3491 LOG(FATAL) << "Unreachable type " << type;
3492 UNREACHABLE();
3493 }
3494
3495 if (is_volatile && load_type == kLoadDoubleword) {
3496 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003497 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498 // Do implicit Null check
3499 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3500 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3501 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
3502 instruction,
3503 dex_pc,
3504 nullptr,
3505 IsDirectEntrypoint(kQuickA64Load));
3506 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3507 if (type == Primitive::kPrimDouble) {
3508 // Need to move to FP regs since FP results are returned in core registers.
3509 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
3510 locations->Out().AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003511 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3512 locations->Out().AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003513 }
3514 } else {
3515 if (!Primitive::IsFloatingPointType(type)) {
3516 Register dst;
3517 if (type == Primitive::kPrimLong) {
3518 DCHECK(locations->Out().IsRegisterPair());
3519 dst = locations->Out().AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003520 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3521 if (obj == dst) {
3522 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3523 codegen_->MaybeRecordImplicitNullCheck(instruction);
3524 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3525 } else {
3526 __ LoadFromOffset(kLoadWord, dst, obj, offset);
3527 codegen_->MaybeRecordImplicitNullCheck(instruction);
3528 __ LoadFromOffset(kLoadWord, dst_high, obj, offset + kMipsWordSize);
3529 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003530 } else {
3531 DCHECK(locations->Out().IsRegister());
3532 dst = locations->Out().AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003533 __ LoadFromOffset(load_type, dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003534 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 } else {
3536 DCHECK(locations->Out().IsFpuRegister());
3537 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3538 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003539 __ LoadSFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003540 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003541 __ LoadDFromOffset(dst, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003542 }
3543 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003544 // Longs are handled earlier.
3545 if (type != Primitive::kPrimLong) {
3546 codegen_->MaybeRecordImplicitNullCheck(instruction);
3547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003548 }
3549
3550 if (is_volatile) {
3551 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3552 }
3553}
3554
3555void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3556 Primitive::Type field_type = field_info.GetFieldType();
3557 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3558 bool generate_volatile = field_info.IsVolatile() && is_wide;
3559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003560 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003561
3562 locations->SetInAt(0, Location::RequiresRegister());
3563 if (generate_volatile) {
3564 InvokeRuntimeCallingConvention calling_convention;
3565 // need A0 to hold base + offset
3566 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3567 if (field_type == Primitive::kPrimLong) {
3568 locations->SetInAt(1, Location::RegisterPairLocation(
3569 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3570 } else {
3571 locations->SetInAt(1, Location::RequiresFpuRegister());
3572 // Pass FP parameters in core registers.
3573 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3574 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3575 }
3576 } else {
3577 if (Primitive::IsFloatingPointType(field_type)) {
3578 locations->SetInAt(1, Location::RequiresFpuRegister());
3579 } else {
3580 locations->SetInAt(1, Location::RequiresRegister());
3581 }
3582 }
3583}
3584
3585void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3586 const FieldInfo& field_info,
3587 uint32_t dex_pc) {
3588 Primitive::Type type = field_info.GetFieldType();
3589 LocationSummary* locations = instruction->GetLocations();
3590 Register obj = locations->InAt(0).AsRegister<Register>();
3591 StoreOperandType store_type = kStoreByte;
3592 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003593 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003594
3595 switch (type) {
3596 case Primitive::kPrimBoolean:
3597 case Primitive::kPrimByte:
3598 store_type = kStoreByte;
3599 break;
3600 case Primitive::kPrimShort:
3601 case Primitive::kPrimChar:
3602 store_type = kStoreHalfword;
3603 break;
3604 case Primitive::kPrimInt:
3605 case Primitive::kPrimFloat:
3606 case Primitive::kPrimNot:
3607 store_type = kStoreWord;
3608 break;
3609 case Primitive::kPrimLong:
3610 case Primitive::kPrimDouble:
3611 store_type = kStoreDoubleword;
3612 break;
3613 case Primitive::kPrimVoid:
3614 LOG(FATAL) << "Unreachable type " << type;
3615 UNREACHABLE();
3616 }
3617
3618 if (is_volatile) {
3619 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3620 }
3621
3622 if (is_volatile && store_type == kStoreDoubleword) {
3623 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003624 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003625 // Do implicit Null check.
3626 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3627 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3628 if (type == Primitive::kPrimDouble) {
3629 // Pass FP parameters in core registers.
3630 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
3631 locations->InAt(1).AsFpuRegister<FRegister>());
Alexey Frunzebb9863a2016-01-11 15:51:16 -08003632 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3633 locations->InAt(1).AsFpuRegister<FRegister>());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003634 }
3635 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
3636 instruction,
3637 dex_pc,
3638 nullptr,
3639 IsDirectEntrypoint(kQuickA64Store));
3640 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
3641 } else {
3642 if (!Primitive::IsFloatingPointType(type)) {
3643 Register src;
3644 if (type == Primitive::kPrimLong) {
3645 DCHECK(locations->InAt(1).IsRegisterPair());
3646 src = locations->InAt(1).AsRegisterPairLow<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003647 Register src_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3648 __ StoreToOffset(kStoreWord, src, obj, offset);
3649 codegen_->MaybeRecordImplicitNullCheck(instruction);
3650 __ StoreToOffset(kStoreWord, src_high, obj, offset + kMipsWordSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003651 } else {
3652 DCHECK(locations->InAt(1).IsRegister());
3653 src = locations->InAt(1).AsRegister<Register>();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003654 __ StoreToOffset(store_type, src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 } else {
3657 DCHECK(locations->InAt(1).IsFpuRegister());
3658 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
3659 if (type == Primitive::kPrimFloat) {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003660 __ StoreSToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003661 } else {
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003662 __ StoreDToOffset(src, obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003663 }
3664 }
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003665 // Longs are handled earlier.
3666 if (type != Primitive::kPrimLong) {
3667 codegen_->MaybeRecordImplicitNullCheck(instruction);
3668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003669 }
3670
3671 // TODO: memory barriers?
3672 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
3673 DCHECK(locations->InAt(1).IsRegister());
3674 Register src = locations->InAt(1).AsRegister<Register>();
3675 codegen_->MarkGCCard(obj, src);
3676 }
3677
3678 if (is_volatile) {
3679 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3680 }
3681}
3682
3683void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3684 HandleFieldGet(instruction, instruction->GetFieldInfo());
3685}
3686
3687void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3688 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3689}
3690
3691void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3692 HandleFieldSet(instruction, instruction->GetFieldInfo());
3693}
3694
3695void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3696 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3697}
3698
3699void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3700 LocationSummary::CallKind call_kind =
3701 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
3702 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3703 locations->SetInAt(0, Location::RequiresRegister());
3704 locations->SetInAt(1, Location::RequiresRegister());
3705 // The output does overlap inputs.
3706 // Note that TypeCheckSlowPathMIPS uses this register too.
3707 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3708}
3709
3710void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
3711 LocationSummary* locations = instruction->GetLocations();
3712 Register obj = locations->InAt(0).AsRegister<Register>();
3713 Register cls = locations->InAt(1).AsRegister<Register>();
3714 Register out = locations->Out().AsRegister<Register>();
3715
3716 MipsLabel done;
3717
3718 // Return 0 if `obj` is null.
3719 // TODO: Avoid this check if we know `obj` is not null.
3720 __ Move(out, ZERO);
3721 __ Beqz(obj, &done);
3722
3723 // Compare the class of `obj` with `cls`.
3724 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
3725 if (instruction->IsExactCheck()) {
3726 // Classes must be equal for the instanceof to succeed.
3727 __ Xor(out, out, cls);
3728 __ Sltiu(out, out, 1);
3729 } else {
3730 // If the classes are not equal, we go into a slow path.
3731 DCHECK(locations->OnlyCallsOnSlowPath());
3732 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
3733 codegen_->AddSlowPath(slow_path);
3734 __ Bne(out, cls, slow_path->GetEntryLabel());
3735 __ LoadConst32(out, 1);
3736 __ Bind(slow_path->GetExitLabel());
3737 }
3738
3739 __ Bind(&done);
3740}
3741
3742void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
3743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3744 locations->SetOut(Location::ConstantLocation(constant));
3745}
3746
3747void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
3748 // Will be generated at use site.
3749}
3750
3751void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
3752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3753 locations->SetOut(Location::ConstantLocation(constant));
3754}
3755
3756void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
3757 // Will be generated at use site.
3758}
3759
3760void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
3761 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
3762 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
3763}
3764
3765void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3766 HandleInvoke(invoke);
3767 // The register T0 is required to be used for the hidden argument in
3768 // art_quick_imt_conflict_trampoline, so add the hidden argument.
3769 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
3770}
3771
3772void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
3773 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
3774 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00003775 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3776 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003777 Location receiver = invoke->GetLocations()->InAt(0);
3778 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3779 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3780
3781 // Set the hidden argument.
3782 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3783 invoke->GetDexMethodIndex());
3784
3785 // temp = object->GetClass();
3786 if (receiver.IsStackSlot()) {
3787 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3788 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3789 } else {
3790 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3791 }
3792 codegen_->MaybeRecordImplicitNullCheck(invoke);
3793 // temp = temp->GetImtEntryAt(method_offset);
3794 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3795 // T9 = temp->GetEntryPoint();
3796 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3797 // T9();
3798 __ Jalr(T9);
3799 __ Nop();
3800 DCHECK(!codegen_->IsLeafMethod());
3801 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3802}
3803
3804void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003805 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3806 if (intrinsic.TryDispatch(invoke)) {
3807 return;
3808 }
3809
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003810 HandleInvoke(invoke);
3811}
3812
3813void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003814 // Explicit clinit checks triggered by static invokes must have been pruned by
3815 // art::PrepareForRegisterAllocation.
3816 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003818 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3819 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3820 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3821
3822 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3823 // R6 has PC-relative addressing.
3824 bool has_extra_input = !isR6 &&
3825 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3826 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3827
3828 if (invoke->HasPcRelativeDexCache()) {
3829 // kDexCachePcRelative is mutually exclusive with
3830 // kDirectAddressWithFixup/kCallDirectWithFixup.
3831 CHECK(!has_extra_input);
3832 has_extra_input = true;
3833 }
3834
Chris Larsen701566a2015-10-27 15:29:13 -07003835 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3836 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003837 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3838 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3839 }
Chris Larsen701566a2015-10-27 15:29:13 -07003840 return;
3841 }
3842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003843 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003844
3845 // Add the extra input register if either the dex cache array base register
3846 // or the PC-relative base register for accessing literals is needed.
3847 if (has_extra_input) {
3848 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3849 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003850}
3851
Chris Larsen701566a2015-10-27 15:29:13 -07003852static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003853 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003854 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3855 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003856 return true;
3857 }
3858 return false;
3859}
3860
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003861HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3862 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3863 // TODO: Implement other kinds.
3864 return HLoadString::LoadKind::kDexCacheViaMethod;
3865}
3866
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003867HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
3868 HLoadClass::LoadKind desired_class_load_kind) {
3869 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3870 // TODO: Implement other kinds.
3871 return HLoadClass::LoadKind::kDexCacheViaMethod;
3872}
3873
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003874Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
3875 Register temp) {
3876 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
3877 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
3878 if (!invoke->GetLocations()->Intrinsified()) {
3879 return location.AsRegister<Register>();
3880 }
3881 // For intrinsics we allow any location, so it may be on the stack.
3882 if (!location.IsRegister()) {
3883 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
3884 return temp;
3885 }
3886 // For register locations, check if the register was saved. If so, get it from the stack.
3887 // Note: There is a chance that the register was saved but not overwritten, so we could
3888 // save one load. However, since this is just an intrinsic slow path we prefer this
3889 // simple and more robust approach rather that trying to determine if that's the case.
3890 SlowPathCode* slow_path = GetCurrentSlowPath();
3891 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
3892 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
3893 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
3894 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
3895 return temp;
3896 }
3897 return location.AsRegister<Register>();
3898}
3899
Vladimir Markodc151b22015-10-15 18:02:30 +01003900HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3901 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3902 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003903 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
3904 // We disable PC-relative load when there is an irreducible loop, as the optimization
3905 // is incompatible with it.
3906 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
3907 bool fallback_load = true;
3908 bool fallback_call = true;
3909 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003910 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3911 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003912 fallback_load = has_irreducible_loops;
3913 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003914 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003915 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01003916 break;
3917 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003918 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003919 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003920 fallback_call = has_irreducible_loops;
3921 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003922 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003923 // TODO: Implement this type.
3924 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003926 fallback_call = false;
3927 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003928 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003929 if (fallback_load) {
3930 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
3931 dispatch_info.method_load_data = 0;
3932 }
3933 if (fallback_call) {
3934 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
3935 dispatch_info.direct_code_ptr = 0;
3936 }
3937 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003938}
3939
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003940void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3941 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003942 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003943 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3944 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3945 bool isR6 = isa_features_.IsR6();
3946 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
3947 // R6 has PC-relative addressing.
3948 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
3949 (!isR6 &&
3950 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3951 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
3952 Register base_reg = has_extra_input
3953 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
3954 : ZERO;
3955
3956 // For better instruction scheduling we load the direct code pointer before the method pointer.
3957 switch (code_ptr_location) {
3958 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3959 // T9 = invoke->GetDirectCodePtr();
3960 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3961 break;
3962 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3963 // T9 = code address from literal pool with link-time patch.
3964 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3965 break;
3966 default:
3967 break;
3968 }
3969
3970 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3972 // temp = thread->string_init_entrypoint
3973 __ LoadFromOffset(kLoadWord,
3974 temp.AsRegister<Register>(),
3975 TR,
3976 invoke->GetStringInitOffset());
3977 break;
3978 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003979 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003980 break;
3981 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3982 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3983 break;
3984 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003985 __ LoadLiteral(temp.AsRegister<Register>(),
3986 base_reg,
3987 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3988 break;
3989 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3990 HMipsDexCacheArraysBase* base =
3991 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
3992 int32_t offset =
3993 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
3994 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
3995 break;
3996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003997 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003998 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003999 Register reg = temp.AsRegister<Register>();
4000 Register method_reg;
4001 if (current_method.IsRegister()) {
4002 method_reg = current_method.AsRegister<Register>();
4003 } else {
4004 // TODO: use the appropriate DCHECK() here if possible.
4005 // DCHECK(invoke->GetLocations()->Intrinsified());
4006 DCHECK(!current_method.IsValid());
4007 method_reg = reg;
4008 __ Lw(reg, SP, kCurrentMethodStackOffset);
4009 }
4010
4011 // temp = temp->dex_cache_resolved_methods_;
4012 __ LoadFromOffset(kLoadWord,
4013 reg,
4014 method_reg,
4015 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004016 // temp = temp[index_in_cache];
4017 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4018 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004019 __ LoadFromOffset(kLoadWord,
4020 reg,
4021 reg,
4022 CodeGenerator::GetCachePointerOffset(index_in_cache));
4023 break;
4024 }
4025 }
4026
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004027 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004028 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004029 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004030 break;
4031 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004032 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4033 // T9 prepared above for better instruction scheduling.
4034 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004035 __ Jalr(T9);
4036 __ Nop();
4037 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004038 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004039 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004040 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4041 LOG(FATAL) << "Unsupported";
4042 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004043 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4044 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004045 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046 T9,
4047 callee_method.AsRegister<Register>(),
4048 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4049 kMipsWordSize).Int32Value());
4050 // T9()
4051 __ Jalr(T9);
4052 __ Nop();
4053 break;
4054 }
4055 DCHECK(!IsLeafMethod());
4056}
4057
4058void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004059 // Explicit clinit checks triggered by static invokes must have been pruned by
4060 // art::PrepareForRegisterAllocation.
4061 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062
4063 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4064 return;
4065 }
4066
4067 LocationSummary* locations = invoke->GetLocations();
4068 codegen_->GenerateStaticOrDirectCall(invoke,
4069 locations->HasTemps()
4070 ? locations->GetTemp(0)
4071 : Location::NoLocation());
4072 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4073}
4074
Chris Larsen3acee732015-11-18 13:31:08 -08004075void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004076 LocationSummary* locations = invoke->GetLocations();
4077 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004078 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004079 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4080 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4081 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4082 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4083
4084 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004085 DCHECK(receiver.IsRegister());
4086 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4087 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004088 // temp = temp->GetMethodAt(method_offset);
4089 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4090 // T9 = temp->GetEntryPoint();
4091 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4092 // T9();
4093 __ Jalr(T9);
4094 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004095}
4096
4097void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4098 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4099 return;
4100 }
4101
4102 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004103 DCHECK(!codegen_->IsLeafMethod());
4104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4105}
4106
4107void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004108 InvokeRuntimeCallingConvention calling_convention;
4109 CodeGenerator::CreateLoadClassLocationSummary(
4110 cls,
4111 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4112 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004113}
4114
4115void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4116 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004117 if (cls->NeedsAccessCheck()) {
4118 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4119 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4120 cls,
4121 cls->GetDexPc(),
4122 nullptr,
4123 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004124 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004125 return;
4126 }
4127
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004128 Register out = locations->Out().AsRegister<Register>();
4129 Register current_method = locations->InAt(0).AsRegister<Register>();
4130 if (cls->IsReferrersClass()) {
4131 DCHECK(!cls->CanCallRuntime());
4132 DCHECK(!cls->MustGenerateClinitCheck());
4133 __ LoadFromOffset(kLoadWord, out, current_method,
4134 ArtMethod::DeclaringClassOffset().Int32Value());
4135 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004136 __ LoadFromOffset(kLoadWord, out, current_method,
4137 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4138 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004139
4140 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4141 DCHECK(cls->CanCallRuntime());
4142 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4143 cls,
4144 cls,
4145 cls->GetDexPc(),
4146 cls->MustGenerateClinitCheck());
4147 codegen_->AddSlowPath(slow_path);
4148 if (!cls->IsInDexCache()) {
4149 __ Beqz(out, slow_path->GetEntryLabel());
4150 }
4151 if (cls->MustGenerateClinitCheck()) {
4152 GenerateClassInitializationCheck(slow_path, out);
4153 } else {
4154 __ Bind(slow_path->GetExitLabel());
4155 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004156 }
4157 }
4158}
4159
4160static int32_t GetExceptionTlsOffset() {
4161 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4162}
4163
4164void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4165 LocationSummary* locations =
4166 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4167 locations->SetOut(Location::RequiresRegister());
4168}
4169
4170void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4171 Register out = load->GetLocations()->Out().AsRegister<Register>();
4172 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4173}
4174
4175void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4176 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4177}
4178
4179void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4180 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4181}
4182
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004183void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004184 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4185 ? LocationSummary::kCallOnSlowPath
4186 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004188 locations->SetInAt(0, Location::RequiresRegister());
4189 locations->SetOut(Location::RequiresRegister());
4190}
4191
4192void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004193 LocationSummary* locations = load->GetLocations();
4194 Register out = locations->Out().AsRegister<Register>();
4195 Register current_method = locations->InAt(0).AsRegister<Register>();
4196 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4197 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4198 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004199
4200 if (!load->IsInDexCache()) {
4201 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4202 codegen_->AddSlowPath(slow_path);
4203 __ Beqz(out, slow_path->GetEntryLabel());
4204 __ Bind(slow_path->GetExitLabel());
4205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206}
4207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4210 locations->SetOut(Location::ConstantLocation(constant));
4211}
4212
4213void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4214 // Will be generated at use site.
4215}
4216
4217void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4218 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004220 InvokeRuntimeCallingConvention calling_convention;
4221 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4222}
4223
4224void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4225 if (instruction->IsEnter()) {
4226 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4227 instruction,
4228 instruction->GetDexPc(),
4229 nullptr,
4230 IsDirectEntrypoint(kQuickLockObject));
4231 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4232 } else {
4233 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4234 instruction,
4235 instruction->GetDexPc(),
4236 nullptr,
4237 IsDirectEntrypoint(kQuickUnlockObject));
4238 }
4239 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4240}
4241
4242void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4243 LocationSummary* locations =
4244 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4245 switch (mul->GetResultType()) {
4246 case Primitive::kPrimInt:
4247 case Primitive::kPrimLong:
4248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetInAt(1, Location::RequiresRegister());
4250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4251 break;
4252
4253 case Primitive::kPrimFloat:
4254 case Primitive::kPrimDouble:
4255 locations->SetInAt(0, Location::RequiresFpuRegister());
4256 locations->SetInAt(1, Location::RequiresFpuRegister());
4257 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4258 break;
4259
4260 default:
4261 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4262 }
4263}
4264
4265void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4266 Primitive::Type type = instruction->GetType();
4267 LocationSummary* locations = instruction->GetLocations();
4268 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4269
4270 switch (type) {
4271 case Primitive::kPrimInt: {
4272 Register dst = locations->Out().AsRegister<Register>();
4273 Register lhs = locations->InAt(0).AsRegister<Register>();
4274 Register rhs = locations->InAt(1).AsRegister<Register>();
4275
4276 if (isR6) {
4277 __ MulR6(dst, lhs, rhs);
4278 } else {
4279 __ MulR2(dst, lhs, rhs);
4280 }
4281 break;
4282 }
4283 case Primitive::kPrimLong: {
4284 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4285 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4286 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4287 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4288 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4289 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4290
4291 // Extra checks to protect caused by the existance of A1_A2.
4292 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4293 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4294 DCHECK_NE(dst_high, lhs_low);
4295 DCHECK_NE(dst_high, rhs_low);
4296
4297 // A_B * C_D
4298 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4299 // dst_lo: [ low(B*D) ]
4300 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4301
4302 if (isR6) {
4303 __ MulR6(TMP, lhs_high, rhs_low);
4304 __ MulR6(dst_high, lhs_low, rhs_high);
4305 __ Addu(dst_high, dst_high, TMP);
4306 __ MuhuR6(TMP, lhs_low, rhs_low);
4307 __ Addu(dst_high, dst_high, TMP);
4308 __ MulR6(dst_low, lhs_low, rhs_low);
4309 } else {
4310 __ MulR2(TMP, lhs_high, rhs_low);
4311 __ MulR2(dst_high, lhs_low, rhs_high);
4312 __ Addu(dst_high, dst_high, TMP);
4313 __ MultuR2(lhs_low, rhs_low);
4314 __ Mfhi(TMP);
4315 __ Addu(dst_high, dst_high, TMP);
4316 __ Mflo(dst_low);
4317 }
4318 break;
4319 }
4320 case Primitive::kPrimFloat:
4321 case Primitive::kPrimDouble: {
4322 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4323 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4324 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4325 if (type == Primitive::kPrimFloat) {
4326 __ MulS(dst, lhs, rhs);
4327 } else {
4328 __ MulD(dst, lhs, rhs);
4329 }
4330 break;
4331 }
4332 default:
4333 LOG(FATAL) << "Unexpected mul type " << type;
4334 }
4335}
4336
4337void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4338 LocationSummary* locations =
4339 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4340 switch (neg->GetResultType()) {
4341 case Primitive::kPrimInt:
4342 case Primitive::kPrimLong:
4343 locations->SetInAt(0, Location::RequiresRegister());
4344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4345 break;
4346
4347 case Primitive::kPrimFloat:
4348 case Primitive::kPrimDouble:
4349 locations->SetInAt(0, Location::RequiresFpuRegister());
4350 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4351 break;
4352
4353 default:
4354 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4355 }
4356}
4357
4358void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4359 Primitive::Type type = instruction->GetType();
4360 LocationSummary* locations = instruction->GetLocations();
4361
4362 switch (type) {
4363 case Primitive::kPrimInt: {
4364 Register dst = locations->Out().AsRegister<Register>();
4365 Register src = locations->InAt(0).AsRegister<Register>();
4366 __ Subu(dst, ZERO, src);
4367 break;
4368 }
4369 case Primitive::kPrimLong: {
4370 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4371 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4372 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4373 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4374 __ Subu(dst_low, ZERO, src_low);
4375 __ Sltu(TMP, ZERO, dst_low);
4376 __ Subu(dst_high, ZERO, src_high);
4377 __ Subu(dst_high, dst_high, TMP);
4378 break;
4379 }
4380 case Primitive::kPrimFloat:
4381 case Primitive::kPrimDouble: {
4382 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4383 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4384 if (type == Primitive::kPrimFloat) {
4385 __ NegS(dst, src);
4386 } else {
4387 __ NegD(dst, src);
4388 }
4389 break;
4390 }
4391 default:
4392 LOG(FATAL) << "Unexpected neg type " << type;
4393 }
4394}
4395
4396void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4397 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004399 InvokeRuntimeCallingConvention calling_convention;
4400 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4401 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4402 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4403 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4404}
4405
4406void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4407 InvokeRuntimeCallingConvention calling_convention;
4408 Register current_method_register = calling_convention.GetRegisterAt(2);
4409 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4410 // Move an uint16_t value to a register.
4411 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4412 codegen_->InvokeRuntime(
4413 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4414 instruction,
4415 instruction->GetDexPc(),
4416 nullptr,
4417 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4418 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4419 void*, uint32_t, int32_t, ArtMethod*>();
4420}
4421
4422void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4423 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004425 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004426 if (instruction->IsStringAlloc()) {
4427 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4428 } else {
4429 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4430 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4431 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004432 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4433}
4434
4435void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004436 if (instruction->IsStringAlloc()) {
4437 // String is allocated through StringFactory. Call NewEmptyString entry point.
4438 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4439 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4440 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4441 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4442 __ Jalr(T9);
4443 __ Nop();
4444 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4445 } else {
4446 codegen_->InvokeRuntime(
4447 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4448 instruction,
4449 instruction->GetDexPc(),
4450 nullptr,
4451 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4452 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4453 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004454}
4455
4456void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4458 locations->SetInAt(0, Location::RequiresRegister());
4459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4460}
4461
4462void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4463 Primitive::Type type = instruction->GetType();
4464 LocationSummary* locations = instruction->GetLocations();
4465
4466 switch (type) {
4467 case Primitive::kPrimInt: {
4468 Register dst = locations->Out().AsRegister<Register>();
4469 Register src = locations->InAt(0).AsRegister<Register>();
4470 __ Nor(dst, src, ZERO);
4471 break;
4472 }
4473
4474 case Primitive::kPrimLong: {
4475 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4476 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4477 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4478 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4479 __ Nor(dst_high, src_high, ZERO);
4480 __ Nor(dst_low, src_low, ZERO);
4481 break;
4482 }
4483
4484 default:
4485 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4486 }
4487}
4488
4489void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4491 locations->SetInAt(0, Location::RequiresRegister());
4492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4493}
4494
4495void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4496 LocationSummary* locations = instruction->GetLocations();
4497 __ Xori(locations->Out().AsRegister<Register>(),
4498 locations->InAt(0).AsRegister<Register>(),
4499 1);
4500}
4501
4502void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4503 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4504 ? LocationSummary::kCallOnSlowPath
4505 : LocationSummary::kNoCall;
4506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4507 locations->SetInAt(0, Location::RequiresRegister());
4508 if (instruction->HasUses()) {
4509 locations->SetOut(Location::SameAsFirstInput());
4510 }
4511}
4512
Calin Juravle2ae48182016-03-16 14:05:09 +00004513void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4514 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004515 return;
4516 }
4517 Location obj = instruction->GetLocations()->InAt(0);
4518
4519 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004520 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004521}
4522
Calin Juravle2ae48182016-03-16 14:05:09 +00004523void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004524 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004525 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004526
4527 Location obj = instruction->GetLocations()->InAt(0);
4528
4529 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4530}
4531
4532void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004533 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004534}
4535
4536void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4537 HandleBinaryOp(instruction);
4538}
4539
4540void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4541 HandleBinaryOp(instruction);
4542}
4543
4544void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4545 LOG(FATAL) << "Unreachable";
4546}
4547
4548void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4549 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4550}
4551
4552void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4554 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4555 if (location.IsStackSlot()) {
4556 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4557 } else if (location.IsDoubleStackSlot()) {
4558 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4559 }
4560 locations->SetOut(location);
4561}
4562
4563void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4564 ATTRIBUTE_UNUSED) {
4565 // Nothing to do, the parameter is already at its location.
4566}
4567
4568void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4569 LocationSummary* locations =
4570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4571 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4572}
4573
4574void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4575 ATTRIBUTE_UNUSED) {
4576 // Nothing to do, the method is already at its location.
4577}
4578
4579void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004581 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004582 locations->SetInAt(i, Location::Any());
4583 }
4584 locations->SetOut(Location::Any());
4585}
4586
4587void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4588 LOG(FATAL) << "Unreachable";
4589}
4590
4591void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4592 Primitive::Type type = rem->GetResultType();
4593 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004594 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4596
4597 switch (type) {
4598 case Primitive::kPrimInt:
4599 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004600 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4602 break;
4603
4604 case Primitive::kPrimLong: {
4605 InvokeRuntimeCallingConvention calling_convention;
4606 locations->SetInAt(0, Location::RegisterPairLocation(
4607 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4608 locations->SetInAt(1, Location::RegisterPairLocation(
4609 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4610 locations->SetOut(calling_convention.GetReturnLocation(type));
4611 break;
4612 }
4613
4614 case Primitive::kPrimFloat:
4615 case Primitive::kPrimDouble: {
4616 InvokeRuntimeCallingConvention calling_convention;
4617 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4618 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4619 locations->SetOut(calling_convention.GetReturnLocation(type));
4620 break;
4621 }
4622
4623 default:
4624 LOG(FATAL) << "Unexpected rem type " << type;
4625 }
4626}
4627
4628void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4629 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004630
4631 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004632 case Primitive::kPrimInt:
4633 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004634 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004635 case Primitive::kPrimLong: {
4636 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4637 instruction,
4638 instruction->GetDexPc(),
4639 nullptr,
4640 IsDirectEntrypoint(kQuickLmod));
4641 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4642 break;
4643 }
4644 case Primitive::kPrimFloat: {
4645 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4646 instruction, instruction->GetDexPc(),
4647 nullptr,
4648 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004649 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004650 break;
4651 }
4652 case Primitive::kPrimDouble: {
4653 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4654 instruction, instruction->GetDexPc(),
4655 nullptr,
4656 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004657 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004658 break;
4659 }
4660 default:
4661 LOG(FATAL) << "Unexpected rem type " << type;
4662 }
4663}
4664
4665void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4666 memory_barrier->SetLocations(nullptr);
4667}
4668
4669void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4670 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4671}
4672
4673void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4675 Primitive::Type return_type = ret->InputAt(0)->GetType();
4676 locations->SetInAt(0, MipsReturnLocation(return_type));
4677}
4678
4679void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4680 codegen_->GenerateFrameExit();
4681}
4682
4683void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4684 ret->SetLocations(nullptr);
4685}
4686
4687void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4688 codegen_->GenerateFrameExit();
4689}
4690
Alexey Frunze92d90602015-12-18 18:16:36 -08004691void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4692 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004693}
4694
Alexey Frunze92d90602015-12-18 18:16:36 -08004695void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4696 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004697}
4698
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004699void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4700 HandleShift(shl);
4701}
4702
4703void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4704 HandleShift(shl);
4705}
4706
4707void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4708 HandleShift(shr);
4709}
4710
4711void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4712 HandleShift(shr);
4713}
4714
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004715void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4716 HandleBinaryOp(instruction);
4717}
4718
4719void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4720 HandleBinaryOp(instruction);
4721}
4722
4723void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4724 HandleFieldGet(instruction, instruction->GetFieldInfo());
4725}
4726
4727void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4728 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4729}
4730
4731void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4732 HandleFieldSet(instruction, instruction->GetFieldInfo());
4733}
4734
4735void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4736 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4737}
4738
4739void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4740 HUnresolvedInstanceFieldGet* instruction) {
4741 FieldAccessCallingConventionMIPS calling_convention;
4742 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4743 instruction->GetFieldType(),
4744 calling_convention);
4745}
4746
4747void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4748 HUnresolvedInstanceFieldGet* instruction) {
4749 FieldAccessCallingConventionMIPS calling_convention;
4750 codegen_->GenerateUnresolvedFieldAccess(instruction,
4751 instruction->GetFieldType(),
4752 instruction->GetFieldIndex(),
4753 instruction->GetDexPc(),
4754 calling_convention);
4755}
4756
4757void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4758 HUnresolvedInstanceFieldSet* instruction) {
4759 FieldAccessCallingConventionMIPS calling_convention;
4760 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4761 instruction->GetFieldType(),
4762 calling_convention);
4763}
4764
4765void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4766 HUnresolvedInstanceFieldSet* instruction) {
4767 FieldAccessCallingConventionMIPS calling_convention;
4768 codegen_->GenerateUnresolvedFieldAccess(instruction,
4769 instruction->GetFieldType(),
4770 instruction->GetFieldIndex(),
4771 instruction->GetDexPc(),
4772 calling_convention);
4773}
4774
4775void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4776 HUnresolvedStaticFieldGet* instruction) {
4777 FieldAccessCallingConventionMIPS calling_convention;
4778 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4779 instruction->GetFieldType(),
4780 calling_convention);
4781}
4782
4783void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4784 HUnresolvedStaticFieldGet* instruction) {
4785 FieldAccessCallingConventionMIPS calling_convention;
4786 codegen_->GenerateUnresolvedFieldAccess(instruction,
4787 instruction->GetFieldType(),
4788 instruction->GetFieldIndex(),
4789 instruction->GetDexPc(),
4790 calling_convention);
4791}
4792
4793void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4794 HUnresolvedStaticFieldSet* instruction) {
4795 FieldAccessCallingConventionMIPS calling_convention;
4796 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4797 instruction->GetFieldType(),
4798 calling_convention);
4799}
4800
4801void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4802 HUnresolvedStaticFieldSet* instruction) {
4803 FieldAccessCallingConventionMIPS calling_convention;
4804 codegen_->GenerateUnresolvedFieldAccess(instruction,
4805 instruction->GetFieldType(),
4806 instruction->GetFieldIndex(),
4807 instruction->GetDexPc(),
4808 calling_convention);
4809}
4810
4811void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4813}
4814
4815void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4816 HBasicBlock* block = instruction->GetBlock();
4817 if (block->GetLoopInformation() != nullptr) {
4818 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4819 // The back edge will generate the suspend check.
4820 return;
4821 }
4822 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4823 // The goto will generate the suspend check.
4824 return;
4825 }
4826 GenerateSuspendCheck(instruction, nullptr);
4827}
4828
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004829void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4830 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004832 InvokeRuntimeCallingConvention calling_convention;
4833 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4834}
4835
4836void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4837 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4838 instruction,
4839 instruction->GetDexPc(),
4840 nullptr,
4841 IsDirectEntrypoint(kQuickDeliverException));
4842 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4843}
4844
4845void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4846 Primitive::Type input_type = conversion->GetInputType();
4847 Primitive::Type result_type = conversion->GetResultType();
4848 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004849 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004850
4851 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4852 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4853 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4854 }
4855
4856 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004857 if (!isR6 &&
4858 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4859 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004860 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004861 }
4862
4863 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4864
4865 if (call_kind == LocationSummary::kNoCall) {
4866 if (Primitive::IsFloatingPointType(input_type)) {
4867 locations->SetInAt(0, Location::RequiresFpuRegister());
4868 } else {
4869 locations->SetInAt(0, Location::RequiresRegister());
4870 }
4871
4872 if (Primitive::IsFloatingPointType(result_type)) {
4873 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4874 } else {
4875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4876 }
4877 } else {
4878 InvokeRuntimeCallingConvention calling_convention;
4879
4880 if (Primitive::IsFloatingPointType(input_type)) {
4881 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4882 } else {
4883 DCHECK_EQ(input_type, Primitive::kPrimLong);
4884 locations->SetInAt(0, Location::RegisterPairLocation(
4885 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4886 }
4887
4888 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4889 }
4890}
4891
4892void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4893 LocationSummary* locations = conversion->GetLocations();
4894 Primitive::Type result_type = conversion->GetResultType();
4895 Primitive::Type input_type = conversion->GetInputType();
4896 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004897 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004898
4899 DCHECK_NE(input_type, result_type);
4900
4901 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4902 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4903 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4904 Register src = locations->InAt(0).AsRegister<Register>();
4905
Alexey Frunzea871ef12016-06-27 15:20:11 -07004906 if (dst_low != src) {
4907 __ Move(dst_low, src);
4908 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004909 __ Sra(dst_high, src, 31);
4910 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4911 Register dst = locations->Out().AsRegister<Register>();
4912 Register src = (input_type == Primitive::kPrimLong)
4913 ? locations->InAt(0).AsRegisterPairLow<Register>()
4914 : locations->InAt(0).AsRegister<Register>();
4915
4916 switch (result_type) {
4917 case Primitive::kPrimChar:
4918 __ Andi(dst, src, 0xFFFF);
4919 break;
4920 case Primitive::kPrimByte:
4921 if (has_sign_extension) {
4922 __ Seb(dst, src);
4923 } else {
4924 __ Sll(dst, src, 24);
4925 __ Sra(dst, dst, 24);
4926 }
4927 break;
4928 case Primitive::kPrimShort:
4929 if (has_sign_extension) {
4930 __ Seh(dst, src);
4931 } else {
4932 __ Sll(dst, src, 16);
4933 __ Sra(dst, dst, 16);
4934 }
4935 break;
4936 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07004937 if (dst != src) {
4938 __ Move(dst, src);
4939 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004940 break;
4941
4942 default:
4943 LOG(FATAL) << "Unexpected type conversion from " << input_type
4944 << " to " << result_type;
4945 }
4946 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004947 if (input_type == Primitive::kPrimLong) {
4948 if (isR6) {
4949 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4950 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4951 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4952 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4953 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4954 __ Mtc1(src_low, FTMP);
4955 __ Mthc1(src_high, FTMP);
4956 if (result_type == Primitive::kPrimFloat) {
4957 __ Cvtsl(dst, FTMP);
4958 } else {
4959 __ Cvtdl(dst, FTMP);
4960 }
4961 } else {
4962 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4963 : QUICK_ENTRY_POINT(pL2d);
4964 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4965 : IsDirectEntrypoint(kQuickL2d);
4966 codegen_->InvokeRuntime(entry_offset,
4967 conversion,
4968 conversion->GetDexPc(),
4969 nullptr,
4970 direct);
4971 if (result_type == Primitive::kPrimFloat) {
4972 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4973 } else {
4974 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4975 }
4976 }
4977 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004978 Register src = locations->InAt(0).AsRegister<Register>();
4979 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4980 __ Mtc1(src, FTMP);
4981 if (result_type == Primitive::kPrimFloat) {
4982 __ Cvtsw(dst, FTMP);
4983 } else {
4984 __ Cvtdw(dst, FTMP);
4985 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004986 }
4987 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4988 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004989 if (result_type == Primitive::kPrimLong) {
4990 if (isR6) {
4991 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4992 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4993 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4994 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4995 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4996 MipsLabel truncate;
4997 MipsLabel done;
4998
4999 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5000 // value when the input is either a NaN or is outside of the range of the output type
5001 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5002 // the same result.
5003 //
5004 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5005 // value of the output type if the input is outside of the range after the truncation or
5006 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5007 // results. This matches the desired float/double-to-int/long conversion exactly.
5008 //
5009 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5010 //
5011 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5012 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5013 // even though it must be NAN2008=1 on R6.
5014 //
5015 // The code takes care of the different behaviors by first comparing the input to the
5016 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5017 // If the input is greater than or equal to the minimum, it procedes to the truncate
5018 // instruction, which will handle such an input the same way irrespective of NAN2008.
5019 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5020 // in order to return either zero or the minimum value.
5021 //
5022 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5023 // truncate instruction for MIPS64R6.
5024 if (input_type == Primitive::kPrimFloat) {
5025 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5026 __ LoadConst32(TMP, min_val);
5027 __ Mtc1(TMP, FTMP);
5028 __ CmpLeS(FTMP, FTMP, src);
5029 } else {
5030 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5031 __ LoadConst32(TMP, High32Bits(min_val));
5032 __ Mtc1(ZERO, FTMP);
5033 __ Mthc1(TMP, FTMP);
5034 __ CmpLeD(FTMP, FTMP, src);
5035 }
5036
5037 __ Bc1nez(FTMP, &truncate);
5038
5039 if (input_type == Primitive::kPrimFloat) {
5040 __ CmpEqS(FTMP, src, src);
5041 } else {
5042 __ CmpEqD(FTMP, src, src);
5043 }
5044 __ Move(dst_low, ZERO);
5045 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5046 __ Mfc1(TMP, FTMP);
5047 __ And(dst_high, dst_high, TMP);
5048
5049 __ B(&done);
5050
5051 __ Bind(&truncate);
5052
5053 if (input_type == Primitive::kPrimFloat) {
5054 __ TruncLS(FTMP, src);
5055 } else {
5056 __ TruncLD(FTMP, src);
5057 }
5058 __ Mfc1(dst_low, FTMP);
5059 __ Mfhc1(dst_high, FTMP);
5060
5061 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005062 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005063 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5064 : QUICK_ENTRY_POINT(pD2l);
5065 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5066 : IsDirectEntrypoint(kQuickD2l);
5067 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5068 if (input_type == Primitive::kPrimFloat) {
5069 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5070 } else {
5071 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5072 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005073 }
5074 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005075 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5076 Register dst = locations->Out().AsRegister<Register>();
5077 MipsLabel truncate;
5078 MipsLabel done;
5079
5080 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5081 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5082 // even though it must be NAN2008=1 on R6.
5083 //
5084 // For details see the large comment above for the truncation of float/double to long on R6.
5085 //
5086 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5087 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005088 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005089 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5090 __ LoadConst32(TMP, min_val);
5091 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005093 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5094 __ LoadConst32(TMP, High32Bits(min_val));
5095 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005096 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005097 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005098
5099 if (isR6) {
5100 if (input_type == Primitive::kPrimFloat) {
5101 __ CmpLeS(FTMP, FTMP, src);
5102 } else {
5103 __ CmpLeD(FTMP, FTMP, src);
5104 }
5105 __ Bc1nez(FTMP, &truncate);
5106
5107 if (input_type == Primitive::kPrimFloat) {
5108 __ CmpEqS(FTMP, src, src);
5109 } else {
5110 __ CmpEqD(FTMP, src, src);
5111 }
5112 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5113 __ Mfc1(TMP, FTMP);
5114 __ And(dst, dst, TMP);
5115 } else {
5116 if (input_type == Primitive::kPrimFloat) {
5117 __ ColeS(0, FTMP, src);
5118 } else {
5119 __ ColeD(0, FTMP, src);
5120 }
5121 __ Bc1t(0, &truncate);
5122
5123 if (input_type == Primitive::kPrimFloat) {
5124 __ CeqS(0, src, src);
5125 } else {
5126 __ CeqD(0, src, src);
5127 }
5128 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5129 __ Movf(dst, ZERO, 0);
5130 }
5131
5132 __ B(&done);
5133
5134 __ Bind(&truncate);
5135
5136 if (input_type == Primitive::kPrimFloat) {
5137 __ TruncWS(FTMP, src);
5138 } else {
5139 __ TruncWD(FTMP, src);
5140 }
5141 __ Mfc1(dst, FTMP);
5142
5143 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005144 }
5145 } else if (Primitive::IsFloatingPointType(result_type) &&
5146 Primitive::IsFloatingPointType(input_type)) {
5147 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5148 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5149 if (result_type == Primitive::kPrimFloat) {
5150 __ Cvtsd(dst, src);
5151 } else {
5152 __ Cvtds(dst, src);
5153 }
5154 } else {
5155 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5156 << " to " << result_type;
5157 }
5158}
5159
5160void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5161 HandleShift(ushr);
5162}
5163
5164void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5165 HandleShift(ushr);
5166}
5167
5168void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5169 HandleBinaryOp(instruction);
5170}
5171
5172void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5173 HandleBinaryOp(instruction);
5174}
5175
5176void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5177 // Nothing to do, this should be removed during prepare for register allocator.
5178 LOG(FATAL) << "Unreachable";
5179}
5180
5181void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5182 // Nothing to do, this should be removed during prepare for register allocator.
5183 LOG(FATAL) << "Unreachable";
5184}
5185
5186void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005187 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005188}
5189
5190void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005191 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005192}
5193
5194void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005195 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005196}
5197
5198void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005199 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005200}
5201
5202void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005203 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005204}
5205
5206void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005207 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005208}
5209
5210void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005211 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005212}
5213
5214void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005215 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005216}
5217
5218void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005219 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005220}
5221
5222void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005223 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005224}
5225
5226void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005227 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005228}
5229
5230void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005231 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005232}
5233
5234void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005235 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005236}
5237
5238void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005239 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005240}
5241
5242void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005243 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005244}
5245
5246void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005247 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005248}
5249
5250void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005251 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005252}
5253
5254void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005255 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005256}
5257
5258void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005259 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005260}
5261
5262void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005263 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005264}
5265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005266void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5267 LocationSummary* locations =
5268 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5269 locations->SetInAt(0, Location::RequiresRegister());
5270}
5271
5272void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5273 int32_t lower_bound = switch_instr->GetStartValue();
5274 int32_t num_entries = switch_instr->GetNumEntries();
5275 LocationSummary* locations = switch_instr->GetLocations();
5276 Register value_reg = locations->InAt(0).AsRegister<Register>();
5277 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5278
5279 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005280 Register temp_reg = TMP;
5281 __ Addiu32(temp_reg, value_reg, -lower_bound);
5282 // Jump to default if index is negative
5283 // Note: We don't check the case that index is positive while value < lower_bound, because in
5284 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5285 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5286
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005287 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005288 // Jump to successors[0] if value == lower_bound.
5289 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5290 int32_t last_index = 0;
5291 for (; num_entries - last_index > 2; last_index += 2) {
5292 __ Addiu(temp_reg, temp_reg, -2);
5293 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5294 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5295 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5296 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5297 }
5298 if (num_entries - last_index == 2) {
5299 // The last missing case_value.
5300 __ Addiu(temp_reg, temp_reg, -1);
5301 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005302 }
5303
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005304 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005305 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5306 __ B(codegen_->GetLabelOf(default_block));
5307 }
5308}
5309
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005310void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5311 HMipsComputeBaseMethodAddress* insn) {
5312 LocationSummary* locations =
5313 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5314 locations->SetOut(Location::RequiresRegister());
5315}
5316
5317void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5318 HMipsComputeBaseMethodAddress* insn) {
5319 LocationSummary* locations = insn->GetLocations();
5320 Register reg = locations->Out().AsRegister<Register>();
5321
5322 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5323
5324 // Generate a dummy PC-relative call to obtain PC.
5325 __ Nal();
5326 // Grab the return address off RA.
5327 __ Move(reg, RA);
5328
5329 // Remember this offset (the obtained PC value) for later use with constant area.
5330 __ BindPcRelBaseLabel();
5331}
5332
5333void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5335 locations->SetOut(Location::RequiresRegister());
5336}
5337
5338void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5339 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5340 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5341 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5342
5343 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5344 __ Bind(&info->high_label);
5345 __ Bind(&info->pc_rel_label);
5346 // Add a 32-bit offset to PC.
5347 __ Auipc(reg, /* placeholder */ 0x1234);
5348 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5349 } else {
5350 // Generate a dummy PC-relative call to obtain PC.
5351 __ Nal();
5352 __ Bind(&info->high_label);
5353 __ Lui(reg, /* placeholder */ 0x1234);
5354 __ Bind(&info->pc_rel_label);
5355 __ Ori(reg, reg, /* placeholder */ 0x5678);
5356 // Add a 32-bit offset to PC.
5357 __ Addu(reg, reg, RA);
5358 }
5359}
5360
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5362 // The trampoline uses the same calling convention as dex calling conventions,
5363 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5364 // the method_idx.
5365 HandleInvoke(invoke);
5366}
5367
5368void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5369 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5370}
5371
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005372void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5375 locations->SetInAt(0, Location::RequiresRegister());
5376 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005377}
5378
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005379void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5380 LocationSummary* locations = instruction->GetLocations();
5381 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005382 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005383 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5384 instruction->GetIndex(), kMipsPointerSize).SizeValue();
5385 } else {
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00005386 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5387 instruction->GetIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005388 }
5389 __ LoadFromOffset(kLoadWord,
5390 locations->Out().AsRegister<Register>(),
5391 locations->InAt(0).AsRegister<Register>(),
5392 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005393}
5394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005395#undef __
5396#undef QUICK_ENTRY_POINT
5397
5398} // namespace mips
5399} // namespace art