blob: f50d0167d383f40d08f9230193d63537510c4e0e [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>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003775 Location receiver = invoke->GetLocations()->InAt(0);
3776 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3777 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3778
3779 // Set the hidden argument.
3780 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
3781 invoke->GetDexMethodIndex());
3782
3783 // temp = object->GetClass();
3784 if (receiver.IsStackSlot()) {
3785 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3786 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3787 } else {
3788 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3789 }
3790 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003791 __ LoadFromOffset(kLoadWord, temp, temp,
3792 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
3793 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003794 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003795 // temp = temp->GetImtEntryAt(method_offset);
3796 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3797 // T9 = temp->GetEntryPoint();
3798 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3799 // T9();
3800 __ Jalr(T9);
3801 __ Nop();
3802 DCHECK(!codegen_->IsLeafMethod());
3803 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3804}
3805
3806void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07003807 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3808 if (intrinsic.TryDispatch(invoke)) {
3809 return;
3810 }
3811
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812 HandleInvoke(invoke);
3813}
3814
3815void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003816 // Explicit clinit checks triggered by static invokes must have been pruned by
3817 // art::PrepareForRegisterAllocation.
3818 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003820 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3821 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3822 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3823
3824 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
3825 // R6 has PC-relative addressing.
3826 bool has_extra_input = !isR6 &&
3827 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3828 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
3829
3830 if (invoke->HasPcRelativeDexCache()) {
3831 // kDexCachePcRelative is mutually exclusive with
3832 // kDirectAddressWithFixup/kCallDirectWithFixup.
3833 CHECK(!has_extra_input);
3834 has_extra_input = true;
3835 }
3836
Chris Larsen701566a2015-10-27 15:29:13 -07003837 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
3838 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003839 if (invoke->GetLocations()->CanCall() && has_extra_input) {
3840 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3841 }
Chris Larsen701566a2015-10-27 15:29:13 -07003842 return;
3843 }
3844
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003845 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003846
3847 // Add the extra input register if either the dex cache array base register
3848 // or the PC-relative base register for accessing literals is needed.
3849 if (has_extra_input) {
3850 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003852}
3853
Chris Larsen701566a2015-10-27 15:29:13 -07003854static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003855 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07003856 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
3857 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003858 return true;
3859 }
3860 return false;
3861}
3862
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003863HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
3864 HLoadString::LoadKind desired_string_load_kind ATTRIBUTE_UNUSED) {
3865 // TODO: Implement other kinds.
3866 return HLoadString::LoadKind::kDexCacheViaMethod;
3867}
3868
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003869HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
3870 HLoadClass::LoadKind desired_class_load_kind) {
3871 DCHECK_NE(desired_class_load_kind, HLoadClass::LoadKind::kReferrersClass);
3872 // TODO: Implement other kinds.
3873 return HLoadClass::LoadKind::kDexCacheViaMethod;
3874}
3875
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003876Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
3877 Register temp) {
3878 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
3879 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
3880 if (!invoke->GetLocations()->Intrinsified()) {
3881 return location.AsRegister<Register>();
3882 }
3883 // For intrinsics we allow any location, so it may be on the stack.
3884 if (!location.IsRegister()) {
3885 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
3886 return temp;
3887 }
3888 // For register locations, check if the register was saved. If so, get it from the stack.
3889 // Note: There is a chance that the register was saved but not overwritten, so we could
3890 // save one load. However, since this is just an intrinsic slow path we prefer this
3891 // simple and more robust approach rather that trying to determine if that's the case.
3892 SlowPathCode* slow_path = GetCurrentSlowPath();
3893 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
3894 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
3895 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
3896 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
3897 return temp;
3898 }
3899 return location.AsRegister<Register>();
3900}
3901
Vladimir Markodc151b22015-10-15 18:02:30 +01003902HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
3903 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3904 MethodReference target_method ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003905 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
3906 // We disable PC-relative load when there is an irreducible loop, as the optimization
3907 // is incompatible with it.
3908 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
3909 bool fallback_load = true;
3910 bool fallback_call = true;
3911 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003912 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3913 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003914 fallback_load = has_irreducible_loops;
3915 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003916 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003917 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 break;
3919 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003920 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003921 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003922 fallback_call = has_irreducible_loops;
3923 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003924 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003925 // TODO: Implement this type.
3926 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003927 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003928 fallback_call = false;
3929 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01003930 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003931 if (fallback_load) {
3932 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
3933 dispatch_info.method_load_data = 0;
3934 }
3935 if (fallback_call) {
3936 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
3937 dispatch_info.direct_code_ptr = 0;
3938 }
3939 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01003940}
3941
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003942void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3943 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003944 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003945 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
3946 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
3947 bool isR6 = isa_features_.IsR6();
3948 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
3949 // R6 has PC-relative addressing.
3950 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
3951 (!isR6 &&
3952 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
3953 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
3954 Register base_reg = has_extra_input
3955 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
3956 : ZERO;
3957
3958 // For better instruction scheduling we load the direct code pointer before the method pointer.
3959 switch (code_ptr_location) {
3960 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3961 // T9 = invoke->GetDirectCodePtr();
3962 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3963 break;
3964 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3965 // T9 = code address from literal pool with link-time patch.
3966 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3967 break;
3968 default:
3969 break;
3970 }
3971
3972 switch (method_load_kind) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003973 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3974 // temp = thread->string_init_entrypoint
3975 __ LoadFromOffset(kLoadWord,
3976 temp.AsRegister<Register>(),
3977 TR,
3978 invoke->GetStringInitOffset());
3979 break;
3980 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003981 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003982 break;
3983 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3984 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
3985 break;
3986 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07003987 __ LoadLiteral(temp.AsRegister<Register>(),
3988 base_reg,
3989 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3990 break;
3991 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3992 HMipsDexCacheArraysBase* base =
3993 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
3994 int32_t offset =
3995 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
3996 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
3997 break;
3998 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003999 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004000 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004001 Register reg = temp.AsRegister<Register>();
4002 Register method_reg;
4003 if (current_method.IsRegister()) {
4004 method_reg = current_method.AsRegister<Register>();
4005 } else {
4006 // TODO: use the appropriate DCHECK() here if possible.
4007 // DCHECK(invoke->GetLocations()->Intrinsified());
4008 DCHECK(!current_method.IsValid());
4009 method_reg = reg;
4010 __ Lw(reg, SP, kCurrentMethodStackOffset);
4011 }
4012
4013 // temp = temp->dex_cache_resolved_methods_;
4014 __ LoadFromOffset(kLoadWord,
4015 reg,
4016 method_reg,
4017 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004018 // temp = temp[index_in_cache];
4019 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4020 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004021 __ LoadFromOffset(kLoadWord,
4022 reg,
4023 reg,
4024 CodeGenerator::GetCachePointerOffset(index_in_cache));
4025 break;
4026 }
4027 }
4028
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004029 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004030 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004031 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004032 break;
4033 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004034 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4035 // T9 prepared above for better instruction scheduling.
4036 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004037 __ Jalr(T9);
4038 __ Nop();
4039 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004040 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004041 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004042 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4043 LOG(FATAL) << "Unsupported";
4044 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4046 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004047 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004048 T9,
4049 callee_method.AsRegister<Register>(),
4050 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4051 kMipsWordSize).Int32Value());
4052 // T9()
4053 __ Jalr(T9);
4054 __ Nop();
4055 break;
4056 }
4057 DCHECK(!IsLeafMethod());
4058}
4059
4060void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004061 // Explicit clinit checks triggered by static invokes must have been pruned by
4062 // art::PrepareForRegisterAllocation.
4063 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004064
4065 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4066 return;
4067 }
4068
4069 LocationSummary* locations = invoke->GetLocations();
4070 codegen_->GenerateStaticOrDirectCall(invoke,
4071 locations->HasTemps()
4072 ? locations->GetTemp(0)
4073 : Location::NoLocation());
4074 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4075}
4076
Chris Larsen3acee732015-11-18 13:31:08 -08004077void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004078 LocationSummary* locations = invoke->GetLocations();
4079 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004080 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004081 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4082 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4083 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4084 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4085
4086 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004087 DCHECK(receiver.IsRegister());
4088 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4089 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004090 // temp = temp->GetMethodAt(method_offset);
4091 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4092 // T9 = temp->GetEntryPoint();
4093 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4094 // T9();
4095 __ Jalr(T9);
4096 __ Nop();
Chris Larsen3acee732015-11-18 13:31:08 -08004097}
4098
4099void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4100 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4101 return;
4102 }
4103
4104 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004105 DCHECK(!codegen_->IsLeafMethod());
4106 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4107}
4108
4109void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Pavle Batutae87a7182015-10-28 13:10:42 +01004110 InvokeRuntimeCallingConvention calling_convention;
4111 CodeGenerator::CreateLoadClassLocationSummary(
4112 cls,
4113 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4114 Location::RegisterLocation(V0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004115}
4116
4117void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4118 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004119 if (cls->NeedsAccessCheck()) {
4120 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
4121 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4122 cls,
4123 cls->GetDexPc(),
4124 nullptr,
4125 IsDirectEntrypoint(kQuickInitializeTypeAndVerifyAccess));
Roland Levillain888d0672015-11-23 18:53:50 +00004126 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004127 return;
4128 }
4129
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004130 Register out = locations->Out().AsRegister<Register>();
4131 Register current_method = locations->InAt(0).AsRegister<Register>();
4132 if (cls->IsReferrersClass()) {
4133 DCHECK(!cls->CanCallRuntime());
4134 DCHECK(!cls->MustGenerateClinitCheck());
4135 __ LoadFromOffset(kLoadWord, out, current_method,
4136 ArtMethod::DeclaringClassOffset().Int32Value());
4137 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004138 __ LoadFromOffset(kLoadWord, out, current_method,
4139 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
4140 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004141
4142 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
4143 DCHECK(cls->CanCallRuntime());
4144 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4145 cls,
4146 cls,
4147 cls->GetDexPc(),
4148 cls->MustGenerateClinitCheck());
4149 codegen_->AddSlowPath(slow_path);
4150 if (!cls->IsInDexCache()) {
4151 __ Beqz(out, slow_path->GetEntryLabel());
4152 }
4153 if (cls->MustGenerateClinitCheck()) {
4154 GenerateClassInitializationCheck(slow_path, out);
4155 } else {
4156 __ Bind(slow_path->GetExitLabel());
4157 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 }
4159 }
4160}
4161
4162static int32_t GetExceptionTlsOffset() {
4163 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
4164}
4165
4166void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4167 LocationSummary* locations =
4168 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4169 locations->SetOut(Location::RequiresRegister());
4170}
4171
4172void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4173 Register out = load->GetLocations()->Out().AsRegister<Register>();
4174 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4175}
4176
4177void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4178 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4179}
4180
4181void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4182 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4183}
4184
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004185void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004186 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
4187 ? LocationSummary::kCallOnSlowPath
4188 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004190 locations->SetInAt(0, Location::RequiresRegister());
4191 locations->SetOut(Location::RequiresRegister());
4192}
4193
4194void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004195 LocationSummary* locations = load->GetLocations();
4196 Register out = locations->Out().AsRegister<Register>();
4197 Register current_method = locations->InAt(0).AsRegister<Register>();
4198 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4199 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
4200 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004201
4202 if (!load->IsInDexCache()) {
4203 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4204 codegen_->AddSlowPath(slow_path);
4205 __ Beqz(out, slow_path->GetEntryLabel());
4206 __ Bind(slow_path->GetExitLabel());
4207 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208}
4209
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004210void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4211 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4212 locations->SetOut(Location::ConstantLocation(constant));
4213}
4214
4215void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4216 // Will be generated at use site.
4217}
4218
4219void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4220 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004222 InvokeRuntimeCallingConvention calling_convention;
4223 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4224}
4225
4226void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4227 if (instruction->IsEnter()) {
4228 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
4229 instruction,
4230 instruction->GetDexPc(),
4231 nullptr,
4232 IsDirectEntrypoint(kQuickLockObject));
4233 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4234 } else {
4235 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
4236 instruction,
4237 instruction->GetDexPc(),
4238 nullptr,
4239 IsDirectEntrypoint(kQuickUnlockObject));
4240 }
4241 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4242}
4243
4244void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4245 LocationSummary* locations =
4246 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4247 switch (mul->GetResultType()) {
4248 case Primitive::kPrimInt:
4249 case Primitive::kPrimLong:
4250 locations->SetInAt(0, Location::RequiresRegister());
4251 locations->SetInAt(1, Location::RequiresRegister());
4252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4253 break;
4254
4255 case Primitive::kPrimFloat:
4256 case Primitive::kPrimDouble:
4257 locations->SetInAt(0, Location::RequiresFpuRegister());
4258 locations->SetInAt(1, Location::RequiresFpuRegister());
4259 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4260 break;
4261
4262 default:
4263 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4264 }
4265}
4266
4267void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4268 Primitive::Type type = instruction->GetType();
4269 LocationSummary* locations = instruction->GetLocations();
4270 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4271
4272 switch (type) {
4273 case Primitive::kPrimInt: {
4274 Register dst = locations->Out().AsRegister<Register>();
4275 Register lhs = locations->InAt(0).AsRegister<Register>();
4276 Register rhs = locations->InAt(1).AsRegister<Register>();
4277
4278 if (isR6) {
4279 __ MulR6(dst, lhs, rhs);
4280 } else {
4281 __ MulR2(dst, lhs, rhs);
4282 }
4283 break;
4284 }
4285 case Primitive::kPrimLong: {
4286 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4287 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4288 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4289 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4290 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4291 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4292
4293 // Extra checks to protect caused by the existance of A1_A2.
4294 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4295 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4296 DCHECK_NE(dst_high, lhs_low);
4297 DCHECK_NE(dst_high, rhs_low);
4298
4299 // A_B * C_D
4300 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4301 // dst_lo: [ low(B*D) ]
4302 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4303
4304 if (isR6) {
4305 __ MulR6(TMP, lhs_high, rhs_low);
4306 __ MulR6(dst_high, lhs_low, rhs_high);
4307 __ Addu(dst_high, dst_high, TMP);
4308 __ MuhuR6(TMP, lhs_low, rhs_low);
4309 __ Addu(dst_high, dst_high, TMP);
4310 __ MulR6(dst_low, lhs_low, rhs_low);
4311 } else {
4312 __ MulR2(TMP, lhs_high, rhs_low);
4313 __ MulR2(dst_high, lhs_low, rhs_high);
4314 __ Addu(dst_high, dst_high, TMP);
4315 __ MultuR2(lhs_low, rhs_low);
4316 __ Mfhi(TMP);
4317 __ Addu(dst_high, dst_high, TMP);
4318 __ Mflo(dst_low);
4319 }
4320 break;
4321 }
4322 case Primitive::kPrimFloat:
4323 case Primitive::kPrimDouble: {
4324 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4325 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4326 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4327 if (type == Primitive::kPrimFloat) {
4328 __ MulS(dst, lhs, rhs);
4329 } else {
4330 __ MulD(dst, lhs, rhs);
4331 }
4332 break;
4333 }
4334 default:
4335 LOG(FATAL) << "Unexpected mul type " << type;
4336 }
4337}
4338
4339void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4340 LocationSummary* locations =
4341 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4342 switch (neg->GetResultType()) {
4343 case Primitive::kPrimInt:
4344 case Primitive::kPrimLong:
4345 locations->SetInAt(0, Location::RequiresRegister());
4346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4347 break;
4348
4349 case Primitive::kPrimFloat:
4350 case Primitive::kPrimDouble:
4351 locations->SetInAt(0, Location::RequiresFpuRegister());
4352 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4353 break;
4354
4355 default:
4356 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4357 }
4358}
4359
4360void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4361 Primitive::Type type = instruction->GetType();
4362 LocationSummary* locations = instruction->GetLocations();
4363
4364 switch (type) {
4365 case Primitive::kPrimInt: {
4366 Register dst = locations->Out().AsRegister<Register>();
4367 Register src = locations->InAt(0).AsRegister<Register>();
4368 __ Subu(dst, ZERO, src);
4369 break;
4370 }
4371 case Primitive::kPrimLong: {
4372 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4373 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4374 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4375 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4376 __ Subu(dst_low, ZERO, src_low);
4377 __ Sltu(TMP, ZERO, dst_low);
4378 __ Subu(dst_high, ZERO, src_high);
4379 __ Subu(dst_high, dst_high, TMP);
4380 break;
4381 }
4382 case Primitive::kPrimFloat:
4383 case Primitive::kPrimDouble: {
4384 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4385 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4386 if (type == Primitive::kPrimFloat) {
4387 __ NegS(dst, src);
4388 } else {
4389 __ NegD(dst, src);
4390 }
4391 break;
4392 }
4393 default:
4394 LOG(FATAL) << "Unexpected neg type " << type;
4395 }
4396}
4397
4398void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
4399 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004401 InvokeRuntimeCallingConvention calling_convention;
4402 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4403 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4404 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4405 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4406}
4407
4408void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
4409 InvokeRuntimeCallingConvention calling_convention;
4410 Register current_method_register = calling_convention.GetRegisterAt(2);
4411 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
4412 // Move an uint16_t value to a register.
4413 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
4414 codegen_->InvokeRuntime(
4415 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4416 instruction,
4417 instruction->GetDexPc(),
4418 nullptr,
4419 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
4420 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
4421 void*, uint32_t, int32_t, ArtMethod*>();
4422}
4423
4424void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
4425 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004427 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004428 if (instruction->IsStringAlloc()) {
4429 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4430 } else {
4431 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4432 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004434 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4435}
4436
4437void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00004438 if (instruction->IsStringAlloc()) {
4439 // String is allocated through StringFactory. Call NewEmptyString entry point.
4440 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4441 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
4442 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4443 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
4444 __ Jalr(T9);
4445 __ Nop();
4446 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4447 } else {
4448 codegen_->InvokeRuntime(
4449 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
4450 instruction,
4451 instruction->GetDexPc(),
4452 nullptr,
4453 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
4454 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4455 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004456}
4457
4458void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
4459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4460 locations->SetInAt(0, Location::RequiresRegister());
4461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4462}
4463
4464void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
4465 Primitive::Type type = instruction->GetType();
4466 LocationSummary* locations = instruction->GetLocations();
4467
4468 switch (type) {
4469 case Primitive::kPrimInt: {
4470 Register dst = locations->Out().AsRegister<Register>();
4471 Register src = locations->InAt(0).AsRegister<Register>();
4472 __ Nor(dst, src, ZERO);
4473 break;
4474 }
4475
4476 case Primitive::kPrimLong: {
4477 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4478 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4479 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4480 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4481 __ Nor(dst_high, src_high, ZERO);
4482 __ Nor(dst_low, src_low, ZERO);
4483 break;
4484 }
4485
4486 default:
4487 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4488 }
4489}
4490
4491void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4493 locations->SetInAt(0, Location::RequiresRegister());
4494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4495}
4496
4497void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
4498 LocationSummary* locations = instruction->GetLocations();
4499 __ Xori(locations->Out().AsRegister<Register>(),
4500 locations->InAt(0).AsRegister<Register>(),
4501 1);
4502}
4503
4504void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
4505 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4506 ? LocationSummary::kCallOnSlowPath
4507 : LocationSummary::kNoCall;
4508 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4509 locations->SetInAt(0, Location::RequiresRegister());
4510 if (instruction->HasUses()) {
4511 locations->SetOut(Location::SameAsFirstInput());
4512 }
4513}
4514
Calin Juravle2ae48182016-03-16 14:05:09 +00004515void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
4516 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004517 return;
4518 }
4519 Location obj = instruction->GetLocations()->InAt(0);
4520
4521 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004522 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004523}
4524
Calin Juravle2ae48182016-03-16 14:05:09 +00004525void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004526 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004527 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004528
4529 Location obj = instruction->GetLocations()->InAt(0);
4530
4531 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
4532}
4533
4534void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004535 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004536}
4537
4538void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
4539 HandleBinaryOp(instruction);
4540}
4541
4542void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
4543 HandleBinaryOp(instruction);
4544}
4545
4546void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4547 LOG(FATAL) << "Unreachable";
4548}
4549
4550void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
4551 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4552}
4553
4554void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
4555 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4556 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4557 if (location.IsStackSlot()) {
4558 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4559 } else if (location.IsDoubleStackSlot()) {
4560 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4561 }
4562 locations->SetOut(location);
4563}
4564
4565void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
4566 ATTRIBUTE_UNUSED) {
4567 // Nothing to do, the parameter is already at its location.
4568}
4569
4570void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
4571 LocationSummary* locations =
4572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4573 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4574}
4575
4576void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
4577 ATTRIBUTE_UNUSED) {
4578 // Nothing to do, the method is already at its location.
4579}
4580
4581void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
4582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004583 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004584 locations->SetInAt(i, Location::Any());
4585 }
4586 locations->SetOut(Location::Any());
4587}
4588
4589void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
4590 LOG(FATAL) << "Unreachable";
4591}
4592
4593void LocationsBuilderMIPS::VisitRem(HRem* rem) {
4594 Primitive::Type type = rem->GetResultType();
4595 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004596 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004597 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4598
4599 switch (type) {
4600 case Primitive::kPrimInt:
4601 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004602 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4604 break;
4605
4606 case Primitive::kPrimLong: {
4607 InvokeRuntimeCallingConvention calling_convention;
4608 locations->SetInAt(0, Location::RegisterPairLocation(
4609 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4610 locations->SetInAt(1, Location::RegisterPairLocation(
4611 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4612 locations->SetOut(calling_convention.GetReturnLocation(type));
4613 break;
4614 }
4615
4616 case Primitive::kPrimFloat:
4617 case Primitive::kPrimDouble: {
4618 InvokeRuntimeCallingConvention calling_convention;
4619 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4620 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4621 locations->SetOut(calling_convention.GetReturnLocation(type));
4622 break;
4623 }
4624
4625 default:
4626 LOG(FATAL) << "Unexpected rem type " << type;
4627 }
4628}
4629
4630void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
4631 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004632
4633 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004634 case Primitive::kPrimInt:
4635 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004636 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004637 case Primitive::kPrimLong: {
4638 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
4639 instruction,
4640 instruction->GetDexPc(),
4641 nullptr,
4642 IsDirectEntrypoint(kQuickLmod));
4643 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
4644 break;
4645 }
4646 case Primitive::kPrimFloat: {
4647 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
4648 instruction, instruction->GetDexPc(),
4649 nullptr,
4650 IsDirectEntrypoint(kQuickFmodf));
Roland Levillain888d0672015-11-23 18:53:50 +00004651 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004652 break;
4653 }
4654 case Primitive::kPrimDouble: {
4655 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
4656 instruction, instruction->GetDexPc(),
4657 nullptr,
4658 IsDirectEntrypoint(kQuickFmod));
Roland Levillain888d0672015-11-23 18:53:50 +00004659 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004660 break;
4661 }
4662 default:
4663 LOG(FATAL) << "Unexpected rem type " << type;
4664 }
4665}
4666
4667void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4668 memory_barrier->SetLocations(nullptr);
4669}
4670
4671void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4672 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
4673}
4674
4675void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
4676 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
4677 Primitive::Type return_type = ret->InputAt(0)->GetType();
4678 locations->SetInAt(0, MipsReturnLocation(return_type));
4679}
4680
4681void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
4682 codegen_->GenerateFrameExit();
4683}
4684
4685void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
4686 ret->SetLocations(nullptr);
4687}
4688
4689void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
4690 codegen_->GenerateFrameExit();
4691}
4692
Alexey Frunze92d90602015-12-18 18:16:36 -08004693void LocationsBuilderMIPS::VisitRor(HRor* ror) {
4694 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004695}
4696
Alexey Frunze92d90602015-12-18 18:16:36 -08004697void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
4698 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004699}
4700
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004701void LocationsBuilderMIPS::VisitShl(HShl* shl) {
4702 HandleShift(shl);
4703}
4704
4705void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
4706 HandleShift(shl);
4707}
4708
4709void LocationsBuilderMIPS::VisitShr(HShr* shr) {
4710 HandleShift(shr);
4711}
4712
4713void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
4714 HandleShift(shr);
4715}
4716
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004717void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
4718 HandleBinaryOp(instruction);
4719}
4720
4721void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
4722 HandleBinaryOp(instruction);
4723}
4724
4725void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4726 HandleFieldGet(instruction, instruction->GetFieldInfo());
4727}
4728
4729void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4730 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4731}
4732
4733void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4734 HandleFieldSet(instruction, instruction->GetFieldInfo());
4735}
4736
4737void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4738 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4739}
4740
4741void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
4742 HUnresolvedInstanceFieldGet* instruction) {
4743 FieldAccessCallingConventionMIPS calling_convention;
4744 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4745 instruction->GetFieldType(),
4746 calling_convention);
4747}
4748
4749void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
4750 HUnresolvedInstanceFieldGet* instruction) {
4751 FieldAccessCallingConventionMIPS calling_convention;
4752 codegen_->GenerateUnresolvedFieldAccess(instruction,
4753 instruction->GetFieldType(),
4754 instruction->GetFieldIndex(),
4755 instruction->GetDexPc(),
4756 calling_convention);
4757}
4758
4759void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
4760 HUnresolvedInstanceFieldSet* instruction) {
4761 FieldAccessCallingConventionMIPS calling_convention;
4762 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4763 instruction->GetFieldType(),
4764 calling_convention);
4765}
4766
4767void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
4768 HUnresolvedInstanceFieldSet* instruction) {
4769 FieldAccessCallingConventionMIPS calling_convention;
4770 codegen_->GenerateUnresolvedFieldAccess(instruction,
4771 instruction->GetFieldType(),
4772 instruction->GetFieldIndex(),
4773 instruction->GetDexPc(),
4774 calling_convention);
4775}
4776
4777void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
4778 HUnresolvedStaticFieldGet* instruction) {
4779 FieldAccessCallingConventionMIPS calling_convention;
4780 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4781 instruction->GetFieldType(),
4782 calling_convention);
4783}
4784
4785void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
4786 HUnresolvedStaticFieldGet* instruction) {
4787 FieldAccessCallingConventionMIPS calling_convention;
4788 codegen_->GenerateUnresolvedFieldAccess(instruction,
4789 instruction->GetFieldType(),
4790 instruction->GetFieldIndex(),
4791 instruction->GetDexPc(),
4792 calling_convention);
4793}
4794
4795void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
4796 HUnresolvedStaticFieldSet* instruction) {
4797 FieldAccessCallingConventionMIPS calling_convention;
4798 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
4799 instruction->GetFieldType(),
4800 calling_convention);
4801}
4802
4803void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
4804 HUnresolvedStaticFieldSet* instruction) {
4805 FieldAccessCallingConventionMIPS calling_convention;
4806 codegen_->GenerateUnresolvedFieldAccess(instruction,
4807 instruction->GetFieldType(),
4808 instruction->GetFieldIndex(),
4809 instruction->GetDexPc(),
4810 calling_convention);
4811}
4812
4813void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4815}
4816
4817void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
4818 HBasicBlock* block = instruction->GetBlock();
4819 if (block->GetLoopInformation() != nullptr) {
4820 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4821 // The back edge will generate the suspend check.
4822 return;
4823 }
4824 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4825 // The goto will generate the suspend check.
4826 return;
4827 }
4828 GenerateSuspendCheck(instruction, nullptr);
4829}
4830
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004831void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
4832 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004834 InvokeRuntimeCallingConvention calling_convention;
4835 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4836}
4837
4838void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
4839 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
4840 instruction,
4841 instruction->GetDexPc(),
4842 nullptr,
4843 IsDirectEntrypoint(kQuickDeliverException));
4844 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
4845}
4846
4847void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4848 Primitive::Type input_type = conversion->GetInputType();
4849 Primitive::Type result_type = conversion->GetResultType();
4850 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004851 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004852
4853 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4854 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4855 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4856 }
4857
4858 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004859 if (!isR6 &&
4860 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
4861 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004862 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004863 }
4864
4865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
4866
4867 if (call_kind == LocationSummary::kNoCall) {
4868 if (Primitive::IsFloatingPointType(input_type)) {
4869 locations->SetInAt(0, Location::RequiresFpuRegister());
4870 } else {
4871 locations->SetInAt(0, Location::RequiresRegister());
4872 }
4873
4874 if (Primitive::IsFloatingPointType(result_type)) {
4875 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4876 } else {
4877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4878 }
4879 } else {
4880 InvokeRuntimeCallingConvention calling_convention;
4881
4882 if (Primitive::IsFloatingPointType(input_type)) {
4883 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4884 } else {
4885 DCHECK_EQ(input_type, Primitive::kPrimLong);
4886 locations->SetInAt(0, Location::RegisterPairLocation(
4887 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4888 }
4889
4890 locations->SetOut(calling_convention.GetReturnLocation(result_type));
4891 }
4892}
4893
4894void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
4895 LocationSummary* locations = conversion->GetLocations();
4896 Primitive::Type result_type = conversion->GetResultType();
4897 Primitive::Type input_type = conversion->GetInputType();
4898 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004899 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004900
4901 DCHECK_NE(input_type, result_type);
4902
4903 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
4904 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4905 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4906 Register src = locations->InAt(0).AsRegister<Register>();
4907
Alexey Frunzea871ef12016-06-27 15:20:11 -07004908 if (dst_low != src) {
4909 __ Move(dst_low, src);
4910 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004911 __ Sra(dst_high, src, 31);
4912 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
4913 Register dst = locations->Out().AsRegister<Register>();
4914 Register src = (input_type == Primitive::kPrimLong)
4915 ? locations->InAt(0).AsRegisterPairLow<Register>()
4916 : locations->InAt(0).AsRegister<Register>();
4917
4918 switch (result_type) {
4919 case Primitive::kPrimChar:
4920 __ Andi(dst, src, 0xFFFF);
4921 break;
4922 case Primitive::kPrimByte:
4923 if (has_sign_extension) {
4924 __ Seb(dst, src);
4925 } else {
4926 __ Sll(dst, src, 24);
4927 __ Sra(dst, dst, 24);
4928 }
4929 break;
4930 case Primitive::kPrimShort:
4931 if (has_sign_extension) {
4932 __ Seh(dst, src);
4933 } else {
4934 __ Sll(dst, src, 16);
4935 __ Sra(dst, dst, 16);
4936 }
4937 break;
4938 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07004939 if (dst != src) {
4940 __ Move(dst, src);
4941 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004942 break;
4943
4944 default:
4945 LOG(FATAL) << "Unexpected type conversion from " << input_type
4946 << " to " << result_type;
4947 }
4948 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004949 if (input_type == Primitive::kPrimLong) {
4950 if (isR6) {
4951 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4952 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4953 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4954 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
4955 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4956 __ Mtc1(src_low, FTMP);
4957 __ Mthc1(src_high, FTMP);
4958 if (result_type == Primitive::kPrimFloat) {
4959 __ Cvtsl(dst, FTMP);
4960 } else {
4961 __ Cvtdl(dst, FTMP);
4962 }
4963 } else {
4964 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
4965 : QUICK_ENTRY_POINT(pL2d);
4966 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
4967 : IsDirectEntrypoint(kQuickL2d);
4968 codegen_->InvokeRuntime(entry_offset,
4969 conversion,
4970 conversion->GetDexPc(),
4971 nullptr,
4972 direct);
4973 if (result_type == Primitive::kPrimFloat) {
4974 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
4975 } else {
4976 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
4977 }
4978 }
4979 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 Register src = locations->InAt(0).AsRegister<Register>();
4981 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4982 __ Mtc1(src, FTMP);
4983 if (result_type == Primitive::kPrimFloat) {
4984 __ Cvtsw(dst, FTMP);
4985 } else {
4986 __ Cvtdw(dst, FTMP);
4987 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004988 }
4989 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
4990 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08004991 if (result_type == Primitive::kPrimLong) {
4992 if (isR6) {
4993 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
4994 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
4995 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4996 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4997 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4998 MipsLabel truncate;
4999 MipsLabel done;
5000
5001 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5002 // value when the input is either a NaN or is outside of the range of the output type
5003 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5004 // the same result.
5005 //
5006 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5007 // value of the output type if the input is outside of the range after the truncation or
5008 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5009 // results. This matches the desired float/double-to-int/long conversion exactly.
5010 //
5011 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5012 //
5013 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5014 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5015 // even though it must be NAN2008=1 on R6.
5016 //
5017 // The code takes care of the different behaviors by first comparing the input to the
5018 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5019 // If the input is greater than or equal to the minimum, it procedes to the truncate
5020 // instruction, which will handle such an input the same way irrespective of NAN2008.
5021 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5022 // in order to return either zero or the minimum value.
5023 //
5024 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5025 // truncate instruction for MIPS64R6.
5026 if (input_type == Primitive::kPrimFloat) {
5027 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5028 __ LoadConst32(TMP, min_val);
5029 __ Mtc1(TMP, FTMP);
5030 __ CmpLeS(FTMP, FTMP, src);
5031 } else {
5032 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5033 __ LoadConst32(TMP, High32Bits(min_val));
5034 __ Mtc1(ZERO, FTMP);
5035 __ Mthc1(TMP, FTMP);
5036 __ CmpLeD(FTMP, FTMP, src);
5037 }
5038
5039 __ Bc1nez(FTMP, &truncate);
5040
5041 if (input_type == Primitive::kPrimFloat) {
5042 __ CmpEqS(FTMP, src, src);
5043 } else {
5044 __ CmpEqD(FTMP, src, src);
5045 }
5046 __ Move(dst_low, ZERO);
5047 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5048 __ Mfc1(TMP, FTMP);
5049 __ And(dst_high, dst_high, TMP);
5050
5051 __ B(&done);
5052
5053 __ Bind(&truncate);
5054
5055 if (input_type == Primitive::kPrimFloat) {
5056 __ TruncLS(FTMP, src);
5057 } else {
5058 __ TruncLD(FTMP, src);
5059 }
5060 __ Mfc1(dst_low, FTMP);
5061 __ Mfhc1(dst_high, FTMP);
5062
5063 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005064 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005065 int32_t entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
5066 : QUICK_ENTRY_POINT(pD2l);
5067 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
5068 : IsDirectEntrypoint(kQuickD2l);
5069 codegen_->InvokeRuntime(entry_offset, conversion, conversion->GetDexPc(), nullptr, direct);
5070 if (input_type == Primitive::kPrimFloat) {
5071 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5072 } else {
5073 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5074 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005075 }
5076 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005077 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5078 Register dst = locations->Out().AsRegister<Register>();
5079 MipsLabel truncate;
5080 MipsLabel done;
5081
5082 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5083 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5084 // even though it must be NAN2008=1 on R6.
5085 //
5086 // For details see the large comment above for the truncation of float/double to long on R6.
5087 //
5088 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5089 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005090 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005091 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5092 __ LoadConst32(TMP, min_val);
5093 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005094 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005095 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5096 __ LoadConst32(TMP, High32Bits(min_val));
5097 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005098 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005099 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005100
5101 if (isR6) {
5102 if (input_type == Primitive::kPrimFloat) {
5103 __ CmpLeS(FTMP, FTMP, src);
5104 } else {
5105 __ CmpLeD(FTMP, FTMP, src);
5106 }
5107 __ Bc1nez(FTMP, &truncate);
5108
5109 if (input_type == Primitive::kPrimFloat) {
5110 __ CmpEqS(FTMP, src, src);
5111 } else {
5112 __ CmpEqD(FTMP, src, src);
5113 }
5114 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5115 __ Mfc1(TMP, FTMP);
5116 __ And(dst, dst, TMP);
5117 } else {
5118 if (input_type == Primitive::kPrimFloat) {
5119 __ ColeS(0, FTMP, src);
5120 } else {
5121 __ ColeD(0, FTMP, src);
5122 }
5123 __ Bc1t(0, &truncate);
5124
5125 if (input_type == Primitive::kPrimFloat) {
5126 __ CeqS(0, src, src);
5127 } else {
5128 __ CeqD(0, src, src);
5129 }
5130 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5131 __ Movf(dst, ZERO, 0);
5132 }
5133
5134 __ B(&done);
5135
5136 __ Bind(&truncate);
5137
5138 if (input_type == Primitive::kPrimFloat) {
5139 __ TruncWS(FTMP, src);
5140 } else {
5141 __ TruncWD(FTMP, src);
5142 }
5143 __ Mfc1(dst, FTMP);
5144
5145 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146 }
5147 } else if (Primitive::IsFloatingPointType(result_type) &&
5148 Primitive::IsFloatingPointType(input_type)) {
5149 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5150 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5151 if (result_type == Primitive::kPrimFloat) {
5152 __ Cvtsd(dst, src);
5153 } else {
5154 __ Cvtds(dst, src);
5155 }
5156 } else {
5157 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5158 << " to " << result_type;
5159 }
5160}
5161
5162void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5163 HandleShift(ushr);
5164}
5165
5166void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5167 HandleShift(ushr);
5168}
5169
5170void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5171 HandleBinaryOp(instruction);
5172}
5173
5174void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5175 HandleBinaryOp(instruction);
5176}
5177
5178void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5179 // Nothing to do, this should be removed during prepare for register allocator.
5180 LOG(FATAL) << "Unreachable";
5181}
5182
5183void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5184 // Nothing to do, this should be removed during prepare for register allocator.
5185 LOG(FATAL) << "Unreachable";
5186}
5187
5188void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005189 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005190}
5191
5192void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005193 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005194}
5195
5196void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005197 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005198}
5199
5200void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005201 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005202}
5203
5204void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005205 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005206}
5207
5208void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005209 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005210}
5211
5212void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005213 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005214}
5215
5216void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005217 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005218}
5219
5220void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005221 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005222}
5223
5224void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005225 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005226}
5227
5228void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005229 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005230}
5231
5232void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005233 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005234}
5235
5236void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005237 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005238}
5239
5240void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005241 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005242}
5243
5244void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005245 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005246}
5247
5248void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005249 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005250}
5251
5252void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005253 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005254}
5255
5256void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005257 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005258}
5259
5260void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005261 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005262}
5263
5264void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005265 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005266}
5267
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005268void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5269 LocationSummary* locations =
5270 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5271 locations->SetInAt(0, Location::RequiresRegister());
5272}
5273
5274void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5275 int32_t lower_bound = switch_instr->GetStartValue();
5276 int32_t num_entries = switch_instr->GetNumEntries();
5277 LocationSummary* locations = switch_instr->GetLocations();
5278 Register value_reg = locations->InAt(0).AsRegister<Register>();
5279 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5280
5281 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005282 Register temp_reg = TMP;
5283 __ Addiu32(temp_reg, value_reg, -lower_bound);
5284 // Jump to default if index is negative
5285 // Note: We don't check the case that index is positive while value < lower_bound, because in
5286 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5287 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5288
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005289 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005290 // Jump to successors[0] if value == lower_bound.
5291 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5292 int32_t last_index = 0;
5293 for (; num_entries - last_index > 2; last_index += 2) {
5294 __ Addiu(temp_reg, temp_reg, -2);
5295 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5296 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5297 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5298 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5299 }
5300 if (num_entries - last_index == 2) {
5301 // The last missing case_value.
5302 __ Addiu(temp_reg, temp_reg, -1);
5303 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005304 }
5305
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005306 // And the default for any other value.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005307 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5308 __ B(codegen_->GetLabelOf(default_block));
5309 }
5310}
5311
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005312void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5313 HMipsComputeBaseMethodAddress* insn) {
5314 LocationSummary* locations =
5315 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5316 locations->SetOut(Location::RequiresRegister());
5317}
5318
5319void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
5320 HMipsComputeBaseMethodAddress* insn) {
5321 LocationSummary* locations = insn->GetLocations();
5322 Register reg = locations->Out().AsRegister<Register>();
5323
5324 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5325
5326 // Generate a dummy PC-relative call to obtain PC.
5327 __ Nal();
5328 // Grab the return address off RA.
5329 __ Move(reg, RA);
5330
5331 // Remember this offset (the obtained PC value) for later use with constant area.
5332 __ BindPcRelBaseLabel();
5333}
5334
5335void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
5337 locations->SetOut(Location::RequiresRegister());
5338}
5339
5340void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
5341 Register reg = base->GetLocations()->Out().AsRegister<Register>();
5342 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5343 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
5344
5345 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5346 __ Bind(&info->high_label);
5347 __ Bind(&info->pc_rel_label);
5348 // Add a 32-bit offset to PC.
5349 __ Auipc(reg, /* placeholder */ 0x1234);
5350 __ Addiu(reg, reg, /* placeholder */ 0x5678);
5351 } else {
5352 // Generate a dummy PC-relative call to obtain PC.
5353 __ Nal();
5354 __ Bind(&info->high_label);
5355 __ Lui(reg, /* placeholder */ 0x1234);
5356 __ Bind(&info->pc_rel_label);
5357 __ Ori(reg, reg, /* placeholder */ 0x5678);
5358 // Add a 32-bit offset to PC.
5359 __ Addu(reg, reg, RA);
5360 }
5361}
5362
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005363void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5364 // The trampoline uses the same calling convention as dex calling conventions,
5365 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
5366 // the method_idx.
5367 HandleInvoke(invoke);
5368}
5369
5370void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
5371 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
5372}
5373
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005374void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5375 LocationSummary* locations =
5376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5377 locations->SetInAt(0, Location::RequiresRegister());
5378 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005379}
5380
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005381void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
5382 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005383 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005384 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005385 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005386 __ LoadFromOffset(kLoadWord,
5387 locations->Out().AsRegister<Register>(),
5388 locations->InAt(0).AsRegister<Register>(),
5389 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005390 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005391 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005392 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005393 __ LoadFromOffset(kLoadWord,
5394 locations->Out().AsRegister<Register>(),
5395 locations->InAt(0).AsRegister<Register>(),
5396 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005397 __ LoadFromOffset(kLoadWord,
5398 locations->Out().AsRegister<Register>(),
5399 locations->Out().AsRegister<Register>(),
5400 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00005401 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005402}
5403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404#undef __
5405#undef QUICK_ENTRY_POINT
5406
5407} // namespace mips
5408} // namespace art