blob: f0118a465b6527f7c512c6663eb2d9033848d351 [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"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100282 HLoadString* load = instruction_->AsLoadString();
283 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100285 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
287 Primitive::Type type = instruction_->GetType();
288 mips_codegen->MoveLocation(locations->Out(),
289 calling_convention.GetReturnLocation(type),
290 type);
291
292 RestoreLiveRegisters(codegen, locations);
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100293
294 // Store the resolved String to the BSS entry.
295 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
296 // .bss entry address in the fast path, so that we can avoid another calculation here.
297 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
298 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
299 Register out = locations->Out().AsRegister<Register>();
300 DCHECK_NE(out, AT);
301 CodeGeneratorMIPS::PcRelativePatchInfo* info =
302 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
303 mips_codegen->EmitPcRelativeAddressPlaceholder(info, AT, base);
304 __ StoreToOffset(kStoreWord, out, AT, 0);
305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 __ B(GetExitLabel());
307 }
308
309 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
310
311 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
313};
314
315class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
316 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000317 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318
319 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
320 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
321 __ Bind(GetEntryLabel());
322 if (instruction_->CanThrowIntoCatchBlock()) {
323 // Live registers will be restored in the catch block if caught.
324 SaveLiveRegisters(codegen, instruction_->GetLocations());
325 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100326 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327 instruction_,
328 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
331 }
332
333 bool IsFatal() const OVERRIDE { return true; }
334
335 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
336
337 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
339};
340
341class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
342 public:
343 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000344 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200345
346 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100349 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 if (successor_ == nullptr) {
352 __ B(GetReturnLabel());
353 } else {
354 __ B(mips_codegen->GetLabelOf(successor_));
355 }
356 }
357
358 MipsLabel* GetReturnLabel() {
359 DCHECK(successor_ == nullptr);
360 return &return_label_;
361 }
362
363 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
364
365 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 // If not null, the block to branch to after the suspend check.
367 HBasicBlock* const successor_;
368
369 // If `successor_` is null, the label to branch to after the suspend check.
370 MipsLabel return_label_;
371
372 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
373};
374
375class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
376 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
380 LocationSummary* locations = instruction_->GetLocations();
381 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
382 uint32_t dex_pc = instruction_->GetDexPc();
383 DCHECK(instruction_->IsCheckCast()
384 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
385 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
386
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 // We're moving two locations to locations that could overlap, so we need a parallel
391 // move resolver.
392 InvokeRuntimeCallingConvention calling_convention;
393 codegen->EmitParallelMoves(locations->InAt(1),
394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
396 object_class,
397 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
398 Primitive::kPrimNot);
399
400 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100401 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000402 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700403 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 Primitive::Type ret_type = instruction_->GetType();
405 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
406 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 } else {
408 DCHECK(instruction_->IsCheckCast());
Serban Constantinescufca16662016-07-14 09:21:59 +0100409 mips_codegen->InvokeRuntime(kQuickCheckCast, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
411 }
412
413 RestoreLiveRegisters(codegen, locations);
414 __ B(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
418
419 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
421};
422
423class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
424 public:
Aart Bik42249c32016-01-07 15:33:50 -0800425 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000426 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427
428 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800429 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100431 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000432 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 }
434
435 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
436
437 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
439};
440
441CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
442 const MipsInstructionSetFeatures& isa_features,
443 const CompilerOptions& compiler_options,
444 OptimizingCompilerStats* stats)
445 : CodeGenerator(graph,
446 kNumberOfCoreRegisters,
447 kNumberOfFRegisters,
448 kNumberOfRegisterPairs,
449 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
450 arraysize(kCoreCalleeSaves)),
451 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
452 arraysize(kFpuCalleeSaves)),
453 compiler_options,
454 stats),
455 block_labels_(nullptr),
456 location_builder_(graph, this),
457 instruction_visitor_(graph, this),
458 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100459 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700460 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700461 uint32_literals_(std::less<uint32_t>(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700463 method_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 call_patches_(MethodReferenceComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700467 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_string_patches_(StringReferenceValueComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 boot_image_type_patches_(TypeReferenceValueComparator(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 boot_image_address_patches_(std::less<uint32_t>(),
475 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
476 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477 // Save RA (containing the return address) to mimic Quick.
478 AddAllocatedRegister(Location::RegisterLocation(RA));
479}
480
481#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100482// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
483#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700484#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200485
486void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
487 // Ensure that we fix up branches.
488 __ FinalizeCode();
489
490 // Adjust native pc offsets in stack maps.
491 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
492 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
493 uint32_t new_position = __ GetAdjustedPosition(old_position);
494 DCHECK_GE(new_position, old_position);
495 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
496 }
497
498 // Adjust pc offsets for the disassembly information.
499 if (disasm_info_ != nullptr) {
500 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
501 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
502 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
503 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
504 it.second.start = __ GetAdjustedPosition(it.second.start);
505 it.second.end = __ GetAdjustedPosition(it.second.end);
506 }
507 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
508 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
509 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
510 }
511 }
512
513 CodeGenerator::Finalize(allocator);
514}
515
516MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
517 return codegen_->GetAssembler();
518}
519
520void ParallelMoveResolverMIPS::EmitMove(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
524}
525
526void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
527 DCHECK_LT(index, moves_.size());
528 MoveOperands* move = moves_[index];
529 Primitive::Type type = move->GetType();
530 Location loc1 = move->GetDestination();
531 Location loc2 = move->GetSource();
532
533 DCHECK(!loc1.IsConstant());
534 DCHECK(!loc2.IsConstant());
535
536 if (loc1.Equals(loc2)) {
537 return;
538 }
539
540 if (loc1.IsRegister() && loc2.IsRegister()) {
541 // Swap 2 GPRs.
542 Register r1 = loc1.AsRegister<Register>();
543 Register r2 = loc2.AsRegister<Register>();
544 __ Move(TMP, r2);
545 __ Move(r2, r1);
546 __ Move(r1, TMP);
547 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
548 FRegister f1 = loc1.AsFpuRegister<FRegister>();
549 FRegister f2 = loc2.AsFpuRegister<FRegister>();
550 if (type == Primitive::kPrimFloat) {
551 __ MovS(FTMP, f2);
552 __ MovS(f2, f1);
553 __ MovS(f1, FTMP);
554 } else {
555 DCHECK_EQ(type, Primitive::kPrimDouble);
556 __ MovD(FTMP, f2);
557 __ MovD(f2, f1);
558 __ MovD(f1, FTMP);
559 }
560 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
561 (loc1.IsFpuRegister() && loc2.IsRegister())) {
562 // Swap FPR and GPR.
563 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
564 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
565 : loc2.AsFpuRegister<FRegister>();
566 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
567 : loc2.AsRegister<Register>();
568 __ Move(TMP, r2);
569 __ Mfc1(r2, f1);
570 __ Mtc1(TMP, f1);
571 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
572 // Swap 2 GPR register pairs.
573 Register r1 = loc1.AsRegisterPairLow<Register>();
574 Register r2 = loc2.AsRegisterPairLow<Register>();
575 __ Move(TMP, r2);
576 __ Move(r2, r1);
577 __ Move(r1, TMP);
578 r1 = loc1.AsRegisterPairHigh<Register>();
579 r2 = loc2.AsRegisterPairHigh<Register>();
580 __ Move(TMP, r2);
581 __ Move(r2, r1);
582 __ Move(r1, TMP);
583 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
584 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
585 // Swap FPR and GPR register pair.
586 DCHECK_EQ(type, Primitive::kPrimDouble);
587 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
588 : loc2.AsFpuRegister<FRegister>();
589 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
590 : loc2.AsRegisterPairLow<Register>();
591 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
592 : loc2.AsRegisterPairHigh<Register>();
593 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
594 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
595 // unpredictable and the following mfch1 will fail.
596 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800597 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200598 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800599 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200600 __ Move(r2_l, TMP);
601 __ Move(r2_h, AT);
602 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
603 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
604 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
605 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
607 (loc1.IsStackSlot() && loc2.IsRegister())) {
608 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>()
609 : loc2.AsRegister<Register>();
610 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex()
611 : loc2.GetStackIndex();
612 __ Move(TMP, reg);
613 __ LoadFromOffset(kLoadWord, reg, SP, offset);
614 __ StoreToOffset(kStoreWord, TMP, SP, offset);
615 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
616 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
617 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
618 : loc2.AsRegisterPairLow<Register>();
619 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
620 : loc2.AsRegisterPairHigh<Register>();
621 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex()
622 : loc2.GetStackIndex();
623 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
624 : loc2.GetHighStackIndex(kMipsWordSize);
625 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000626 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000627 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000628 __ Move(TMP, reg_h);
629 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
630 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200631 } else {
632 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
633 }
634}
635
636void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
637 __ Pop(static_cast<Register>(reg));
638}
639
640void ParallelMoveResolverMIPS::SpillScratch(int reg) {
641 __ Push(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
645 // Allocate a scratch register other than TMP, if available.
646 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
647 // automatically unspilled when the scratch scope object is destroyed).
648 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
649 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
650 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
651 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
652 __ LoadFromOffset(kLoadWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index1 + stack_offset);
656 __ LoadFromOffset(kLoadWord,
657 TMP,
658 SP,
659 index2 + stack_offset);
660 __ StoreToOffset(kStoreWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
665 }
666}
667
Alexey Frunze73296a72016-06-03 22:51:46 -0700668void CodeGeneratorMIPS::ComputeSpillMask() {
669 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
670 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
671 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
672 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
673 // registers, include the ZERO register to force alignment of FPU callee-saved registers
674 // within the stack frame.
675 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
676 core_spill_mask_ |= (1 << ZERO);
677 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700678}
679
680bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700681 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700682 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
683 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
684 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700685 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
686 // saved in an unused temporary register) and saving of RA and the current method pointer
687 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700688 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700689}
690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200691static dwarf::Reg DWARFReg(Register reg) {
692 return dwarf::Reg::MipsCore(static_cast<int>(reg));
693}
694
695// TODO: mapping of floating-point registers to DWARF.
696
697void CodeGeneratorMIPS::GenerateFrameEntry() {
698 __ Bind(&frame_entry_label_);
699
700 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
701
702 if (do_overflow_check) {
703 __ LoadFromOffset(kLoadWord,
704 ZERO,
705 SP,
706 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
707 RecordPcInfo(nullptr, 0);
708 }
709
710 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700711 CHECK_EQ(fpu_spill_mask_, 0u);
712 CHECK_EQ(core_spill_mask_, 1u << RA);
713 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200714 return;
715 }
716
717 // Make sure the frame size isn't unreasonably large.
718 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
719 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
720 }
721
722 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200723
Alexey Frunze73296a72016-06-03 22:51:46 -0700724 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200725 __ IncreaseFrameSize(ofs);
726
Alexey Frunze73296a72016-06-03 22:51:46 -0700727 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
728 Register reg = static_cast<Register>(MostSignificantBit(mask));
729 mask ^= 1u << reg;
730 ofs -= kMipsWordSize;
731 // The ZERO register is only included for alignment.
732 if (reg != ZERO) {
733 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200734 __ cfi().RelOffset(DWARFReg(reg), ofs);
735 }
736 }
737
Alexey Frunze73296a72016-06-03 22:51:46 -0700738 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
739 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
740 mask ^= 1u << reg;
741 ofs -= kMipsDoublewordSize;
742 __ StoreDToOffset(reg, SP, ofs);
743 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200744 }
745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 // Store the current method pointer.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700747 // TODO: can we not do this if RequiresCurrentMethod() returns false?
Alexey Frunze73296a72016-06-03 22:51:46 -0700748 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749}
750
751void CodeGeneratorMIPS::GenerateFrameExit() {
752 __ cfi().RememberState();
753
754 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200755 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756
Alexey Frunze73296a72016-06-03 22:51:46 -0700757 // For better instruction scheduling restore RA before other registers.
758 uint32_t ofs = GetFrameSize();
759 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
760 Register reg = static_cast<Register>(MostSignificantBit(mask));
761 mask ^= 1u << reg;
762 ofs -= kMipsWordSize;
763 // The ZERO register is only included for alignment.
764 if (reg != ZERO) {
765 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 __ cfi().Restore(DWARFReg(reg));
767 }
768 }
769
Alexey Frunze73296a72016-06-03 22:51:46 -0700770 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
771 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
772 mask ^= 1u << reg;
773 ofs -= kMipsDoublewordSize;
774 __ LoadDFromOffset(reg, SP, ofs);
775 // TODO: __ cfi().Restore(DWARFReg(reg));
776 }
777
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700778 size_t frame_size = GetFrameSize();
779 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
780 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
781 bool reordering = __ SetReorder(false);
782 if (exchange) {
783 __ Jr(RA);
784 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
785 } else {
786 __ DecreaseFrameSize(frame_size);
787 __ Jr(RA);
788 __ Nop(); // In delay slot.
789 }
790 __ SetReorder(reordering);
791 } else {
792 __ Jr(RA);
793 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200794 }
795
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200796 __ cfi().RestoreState();
797 __ cfi().DefCFAOffset(GetFrameSize());
798}
799
800void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
801 __ Bind(GetLabelOf(block));
802}
803
804void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
805 if (src.Equals(dst)) {
806 return;
807 }
808
809 if (src.IsConstant()) {
810 MoveConstant(dst, src.GetConstant());
811 } else {
812 if (Primitive::Is64BitType(dst_type)) {
813 Move64(dst, src);
814 } else {
815 Move32(dst, src);
816 }
817 }
818}
819
820void CodeGeneratorMIPS::Move32(Location destination, Location source) {
821 if (source.Equals(destination)) {
822 return;
823 }
824
825 if (destination.IsRegister()) {
826 if (source.IsRegister()) {
827 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
828 } else if (source.IsFpuRegister()) {
829 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
830 } else {
831 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
832 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
833 }
834 } else if (destination.IsFpuRegister()) {
835 if (source.IsRegister()) {
836 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
837 } else if (source.IsFpuRegister()) {
838 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
839 } else {
840 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
841 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
842 }
843 } else {
844 DCHECK(destination.IsStackSlot()) << destination;
845 if (source.IsRegister()) {
846 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
847 } else if (source.IsFpuRegister()) {
848 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
849 } else {
850 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
851 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
852 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
853 }
854 }
855}
856
857void CodeGeneratorMIPS::Move64(Location destination, Location source) {
858 if (source.Equals(destination)) {
859 return;
860 }
861
862 if (destination.IsRegisterPair()) {
863 if (source.IsRegisterPair()) {
864 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
865 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
866 } else if (source.IsFpuRegister()) {
867 Register dst_high = destination.AsRegisterPairHigh<Register>();
868 Register dst_low = destination.AsRegisterPairLow<Register>();
869 FRegister src = source.AsFpuRegister<FRegister>();
870 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800871 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200872 } else {
873 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
874 int32_t off = source.GetStackIndex();
875 Register r = destination.AsRegisterPairLow<Register>();
876 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
877 }
878 } else if (destination.IsFpuRegister()) {
879 if (source.IsRegisterPair()) {
880 FRegister dst = destination.AsFpuRegister<FRegister>();
881 Register src_high = source.AsRegisterPairHigh<Register>();
882 Register src_low = source.AsRegisterPairLow<Register>();
883 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800884 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200885 } else if (source.IsFpuRegister()) {
886 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
887 } else {
888 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
889 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
890 }
891 } else {
892 DCHECK(destination.IsDoubleStackSlot()) << destination;
893 int32_t off = destination.GetStackIndex();
894 if (source.IsRegisterPair()) {
895 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
896 } else if (source.IsFpuRegister()) {
897 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
898 } else {
899 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
900 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
901 __ StoreToOffset(kStoreWord, TMP, SP, off);
902 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
903 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
904 }
905 }
906}
907
908void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
909 if (c->IsIntConstant() || c->IsNullConstant()) {
910 // Move 32 bit constant.
911 int32_t value = GetInt32ValueOf(c);
912 if (destination.IsRegister()) {
913 Register dst = destination.AsRegister<Register>();
914 __ LoadConst32(dst, value);
915 } else {
916 DCHECK(destination.IsStackSlot())
917 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700918 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200919 }
920 } else if (c->IsLongConstant()) {
921 // Move 64 bit constant.
922 int64_t value = GetInt64ValueOf(c);
923 if (destination.IsRegisterPair()) {
924 Register r_h = destination.AsRegisterPairHigh<Register>();
925 Register r_l = destination.AsRegisterPairLow<Register>();
926 __ LoadConst64(r_h, r_l, value);
927 } else {
928 DCHECK(destination.IsDoubleStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700930 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200931 }
932 } else if (c->IsFloatConstant()) {
933 // Move 32 bit float constant.
934 int32_t value = GetInt32ValueOf(c);
935 if (destination.IsFpuRegister()) {
936 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
937 } else {
938 DCHECK(destination.IsStackSlot())
939 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700940 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200941 }
942 } else {
943 // Move 64 bit double constant.
944 DCHECK(c->IsDoubleConstant()) << c->DebugName();
945 int64_t value = GetInt64ValueOf(c);
946 if (destination.IsFpuRegister()) {
947 FRegister fd = destination.AsFpuRegister<FRegister>();
948 __ LoadDConst64(fd, value, TMP);
949 } else {
950 DCHECK(destination.IsDoubleStackSlot())
951 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700952 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953 }
954 }
955}
956
957void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
958 DCHECK(destination.IsRegister());
959 Register dst = destination.AsRegister<Register>();
960 __ LoadConst32(dst, value);
961}
962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200963void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
964 if (location.IsRegister()) {
965 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700966 } else if (location.IsRegisterPair()) {
967 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
968 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200969 } else {
970 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
971 }
972}
973
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100974template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
975inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
976 const ArenaDeque<PcRelativePatchInfo>& infos,
977 ArenaVector<LinkerPatch>* linker_patches) {
978 for (const PcRelativePatchInfo& info : infos) {
979 const DexFile& dex_file = info.target_dex_file;
980 size_t offset_or_index = info.offset_or_index;
981 DCHECK(info.high_label.IsBound());
982 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
983 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
984 // the assembler's base label used for PC-relative addressing.
985 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
986 ? __ GetLabelLocation(&info.pc_rel_label)
987 : __ GetPcRelBaseLabelLocation();
988 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
989 }
990}
991
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700992void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
993 DCHECK(linker_patches->empty());
994 size_t size =
995 method_patches_.size() +
996 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -0700997 pc_relative_dex_cache_patches_.size() +
998 pc_relative_string_patches_.size() +
999 pc_relative_type_patches_.size() +
1000 boot_image_string_patches_.size() +
1001 boot_image_type_patches_.size() +
1002 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001003 linker_patches->reserve(size);
1004 for (const auto& entry : method_patches_) {
1005 const MethodReference& target_method = entry.first;
1006 Literal* literal = entry.second;
1007 DCHECK(literal->GetLabel()->IsBound());
1008 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1009 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1010 target_method.dex_file,
1011 target_method.dex_method_index));
1012 }
1013 for (const auto& entry : call_patches_) {
1014 const MethodReference& target_method = entry.first;
1015 Literal* literal = entry.second;
1016 DCHECK(literal->GetLabel()->IsBound());
1017 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1018 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1019 target_method.dex_file,
1020 target_method.dex_method_index));
1021 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01001022 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1023 linker_patches);
1024 if (!GetCompilerOptions().IsBootImage()) {
1025 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1026 linker_patches);
1027 } else {
1028 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1029 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001030 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01001031 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1032 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001033 for (const auto& entry : boot_image_string_patches_) {
1034 const StringReference& target_string = entry.first;
1035 Literal* literal = entry.second;
1036 DCHECK(literal->GetLabel()->IsBound());
1037 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1038 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1039 target_string.dex_file,
1040 target_string.string_index));
1041 }
1042 for (const auto& entry : boot_image_type_patches_) {
1043 const TypeReference& target_type = entry.first;
1044 Literal* literal = entry.second;
1045 DCHECK(literal->GetLabel()->IsBound());
1046 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1047 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1048 target_type.dex_file,
1049 target_type.type_index));
1050 }
1051 for (const auto& entry : boot_image_address_patches_) {
1052 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1053 Literal* literal = entry.second;
1054 DCHECK(literal->GetLabel()->IsBound());
1055 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1056 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1057 }
1058}
1059
1060CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1061 const DexFile& dex_file, uint32_t string_index) {
1062 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1063}
1064
1065CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
1066 const DexFile& dex_file, uint32_t type_index) {
1067 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001068}
1069
1070CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1071 const DexFile& dex_file, uint32_t element_offset) {
1072 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1073}
1074
1075CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1076 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1077 patches->emplace_back(dex_file, offset_or_index);
1078 return &patches->back();
1079}
1080
Alexey Frunze06a46c42016-07-19 15:00:40 -07001081Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1082 return map->GetOrCreate(
1083 value,
1084 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1085}
1086
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001087Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1088 MethodToLiteralMap* map) {
1089 return map->GetOrCreate(
1090 target_method,
1091 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1092}
1093
1094Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1095 return DeduplicateMethodLiteral(target_method, &method_patches_);
1096}
1097
1098Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1099 return DeduplicateMethodLiteral(target_method, &call_patches_);
1100}
1101
Alexey Frunze06a46c42016-07-19 15:00:40 -07001102Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1103 uint32_t string_index) {
1104 return boot_image_string_patches_.GetOrCreate(
1105 StringReference(&dex_file, string_index),
1106 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1107}
1108
1109Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
1110 uint32_t type_index) {
1111 return boot_image_type_patches_.GetOrCreate(
1112 TypeReference(&dex_file, type_index),
1113 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1114}
1115
1116Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1117 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1118 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1119 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1120}
1121
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01001122void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1123 PcRelativePatchInfo* info, Register out, Register base) {
1124 bool reordering = __ SetReorder(false);
1125 if (GetInstructionSetFeatures().IsR6()) {
1126 DCHECK_EQ(base, ZERO);
1127 __ Bind(&info->high_label);
1128 __ Bind(&info->pc_rel_label);
1129 // Add a 32-bit offset to PC.
1130 __ Auipc(out, /* placeholder */ 0x1234);
1131 __ Addiu(out, out, /* placeholder */ 0x5678);
1132 } else {
1133 // If base is ZERO, emit NAL to obtain the actual base.
1134 if (base == ZERO) {
1135 // Generate a dummy PC-relative call to obtain PC.
1136 __ Nal();
1137 }
1138 __ Bind(&info->high_label);
1139 __ Lui(out, /* placeholder */ 0x1234);
1140 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1141 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1142 if (base == ZERO) {
1143 __ Bind(&info->pc_rel_label);
1144 }
1145 __ Ori(out, out, /* placeholder */ 0x5678);
1146 // Add a 32-bit offset to PC.
1147 __ Addu(out, out, (base == ZERO) ? RA : base);
1148 }
1149 __ SetReorder(reordering);
1150}
1151
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001152void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1153 MipsLabel done;
1154 Register card = AT;
1155 Register temp = TMP;
1156 __ Beqz(value, &done);
1157 __ LoadFromOffset(kLoadWord,
1158 card,
1159 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001160 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001161 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1162 __ Addu(temp, card, temp);
1163 __ Sb(card, temp, 0);
1164 __ Bind(&done);
1165}
1166
David Brazdil58282f42016-01-14 12:45:10 +00001167void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001168 // Don't allocate the dalvik style register pair passing.
1169 blocked_register_pairs_[A1_A2] = true;
1170
1171 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1172 blocked_core_registers_[ZERO] = true;
1173 blocked_core_registers_[K0] = true;
1174 blocked_core_registers_[K1] = true;
1175 blocked_core_registers_[GP] = true;
1176 blocked_core_registers_[SP] = true;
1177 blocked_core_registers_[RA] = true;
1178
1179 // AT and TMP(T8) are used as temporary/scratch registers
1180 // (similar to how AT is used by MIPS assemblers).
1181 blocked_core_registers_[AT] = true;
1182 blocked_core_registers_[TMP] = true;
1183 blocked_fpu_registers_[FTMP] = true;
1184
1185 // Reserve suspend and thread registers.
1186 blocked_core_registers_[S0] = true;
1187 blocked_core_registers_[TR] = true;
1188
1189 // Reserve T9 for function calls
1190 blocked_core_registers_[T9] = true;
1191
1192 // Reserve odd-numbered FPU registers.
1193 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1194 blocked_fpu_registers_[i] = true;
1195 }
1196
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001197 if (GetGraph()->IsDebuggable()) {
1198 // Stubs do not save callee-save floating point registers. If the graph
1199 // is debuggable, we need to deal with these registers differently. For
1200 // now, just block them.
1201 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1202 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1203 }
1204 }
1205
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 UpdateBlockedPairRegisters();
1207}
1208
1209void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1210 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1211 MipsManagedRegister current =
1212 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1213 if (blocked_core_registers_[current.AsRegisterPairLow()]
1214 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1215 blocked_register_pairs_[i] = true;
1216 }
1217 }
1218}
1219
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001220size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1221 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1222 return kMipsWordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1226 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1227 return kMipsWordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1231 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1232 return kMipsDoublewordSize;
1233}
1234
1235size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1236 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1237 return kMipsDoublewordSize;
1238}
1239
1240void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001241 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001242}
1243
1244void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001245 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001246}
1247
Serban Constantinescufca16662016-07-14 09:21:59 +01001248constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1249
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001250void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1251 HInstruction* instruction,
1252 uint32_t dex_pc,
1253 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001254 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001255 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001256 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001257 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001258 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001259 // Reserve argument space on stack (for $a0-$a3) for
1260 // entrypoints that directly reference native implementations.
1261 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001262 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001263 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001264 } else {
1265 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001267 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001268 if (EntrypointRequiresStackMap(entrypoint)) {
1269 RecordPcInfo(instruction, dex_pc, slow_path);
1270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271}
1272
1273void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1274 Register class_reg) {
1275 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1276 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1277 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1278 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1279 __ Sync(0);
1280 __ Bind(slow_path->GetExitLabel());
1281}
1282
1283void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1284 __ Sync(0); // Only stype 0 is supported.
1285}
1286
1287void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1288 HBasicBlock* successor) {
1289 SuspendCheckSlowPathMIPS* slow_path =
1290 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1291 codegen_->AddSlowPath(slow_path);
1292
1293 __ LoadFromOffset(kLoadUnsignedHalfword,
1294 TMP,
1295 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001296 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 if (successor == nullptr) {
1298 __ Bnez(TMP, slow_path->GetEntryLabel());
1299 __ Bind(slow_path->GetReturnLabel());
1300 } else {
1301 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1302 __ B(slow_path->GetEntryLabel());
1303 // slow_path will return to GetLabelOf(successor).
1304 }
1305}
1306
1307InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1308 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001309 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 assembler_(codegen->GetAssembler()),
1311 codegen_(codegen) {}
1312
1313void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1314 DCHECK_EQ(instruction->InputCount(), 2U);
1315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1316 Primitive::Type type = instruction->GetResultType();
1317 switch (type) {
1318 case Primitive::kPrimInt: {
1319 locations->SetInAt(0, Location::RequiresRegister());
1320 HInstruction* right = instruction->InputAt(1);
1321 bool can_use_imm = false;
1322 if (right->IsConstant()) {
1323 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1324 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1325 can_use_imm = IsUint<16>(imm);
1326 } else if (instruction->IsAdd()) {
1327 can_use_imm = IsInt<16>(imm);
1328 } else {
1329 DCHECK(instruction->IsSub());
1330 can_use_imm = IsInt<16>(-imm);
1331 }
1332 }
1333 if (can_use_imm)
1334 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1335 else
1336 locations->SetInAt(1, Location::RequiresRegister());
1337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1338 break;
1339 }
1340
1341 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345 break;
1346 }
1347
1348 case Primitive::kPrimFloat:
1349 case Primitive::kPrimDouble:
1350 DCHECK(instruction->IsAdd() || instruction->IsSub());
1351 locations->SetInAt(0, Location::RequiresFpuRegister());
1352 locations->SetInAt(1, Location::RequiresFpuRegister());
1353 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1358 }
1359}
1360
1361void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1362 Primitive::Type type = instruction->GetType();
1363 LocationSummary* locations = instruction->GetLocations();
1364
1365 switch (type) {
1366 case Primitive::kPrimInt: {
1367 Register dst = locations->Out().AsRegister<Register>();
1368 Register lhs = locations->InAt(0).AsRegister<Register>();
1369 Location rhs_location = locations->InAt(1);
1370
1371 Register rhs_reg = ZERO;
1372 int32_t rhs_imm = 0;
1373 bool use_imm = rhs_location.IsConstant();
1374 if (use_imm) {
1375 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1376 } else {
1377 rhs_reg = rhs_location.AsRegister<Register>();
1378 }
1379
1380 if (instruction->IsAnd()) {
1381 if (use_imm)
1382 __ Andi(dst, lhs, rhs_imm);
1383 else
1384 __ And(dst, lhs, rhs_reg);
1385 } else if (instruction->IsOr()) {
1386 if (use_imm)
1387 __ Ori(dst, lhs, rhs_imm);
1388 else
1389 __ Or(dst, lhs, rhs_reg);
1390 } else if (instruction->IsXor()) {
1391 if (use_imm)
1392 __ Xori(dst, lhs, rhs_imm);
1393 else
1394 __ Xor(dst, lhs, rhs_reg);
1395 } else if (instruction->IsAdd()) {
1396 if (use_imm)
1397 __ Addiu(dst, lhs, rhs_imm);
1398 else
1399 __ Addu(dst, lhs, rhs_reg);
1400 } else {
1401 DCHECK(instruction->IsSub());
1402 if (use_imm)
1403 __ Addiu(dst, lhs, -rhs_imm);
1404 else
1405 __ Subu(dst, lhs, rhs_reg);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1412 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1413 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1414 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001415 Location rhs_location = locations->InAt(1);
1416 bool use_imm = rhs_location.IsConstant();
1417 if (!use_imm) {
1418 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1419 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1420 if (instruction->IsAnd()) {
1421 __ And(dst_low, lhs_low, rhs_low);
1422 __ And(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsOr()) {
1424 __ Or(dst_low, lhs_low, rhs_low);
1425 __ Or(dst_high, lhs_high, rhs_high);
1426 } else if (instruction->IsXor()) {
1427 __ Xor(dst_low, lhs_low, rhs_low);
1428 __ Xor(dst_high, lhs_high, rhs_high);
1429 } else if (instruction->IsAdd()) {
1430 if (lhs_low == rhs_low) {
1431 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1432 __ Slt(TMP, lhs_low, ZERO);
1433 __ Addu(dst_low, lhs_low, rhs_low);
1434 } else {
1435 __ Addu(dst_low, lhs_low, rhs_low);
1436 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1437 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1438 }
1439 __ Addu(dst_high, lhs_high, rhs_high);
1440 __ Addu(dst_high, dst_high, TMP);
1441 } else {
1442 DCHECK(instruction->IsSub());
1443 __ Sltu(TMP, lhs_low, rhs_low);
1444 __ Subu(dst_low, lhs_low, rhs_low);
1445 __ Subu(dst_high, lhs_high, rhs_high);
1446 __ Subu(dst_high, dst_high, TMP);
1447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001449 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1450 if (instruction->IsOr()) {
1451 uint32_t low = Low32Bits(value);
1452 uint32_t high = High32Bits(value);
1453 if (IsUint<16>(low)) {
1454 if (dst_low != lhs_low || low != 0) {
1455 __ Ori(dst_low, lhs_low, low);
1456 }
1457 } else {
1458 __ LoadConst32(TMP, low);
1459 __ Or(dst_low, lhs_low, TMP);
1460 }
1461 if (IsUint<16>(high)) {
1462 if (dst_high != lhs_high || high != 0) {
1463 __ Ori(dst_high, lhs_high, high);
1464 }
1465 } else {
1466 if (high != low) {
1467 __ LoadConst32(TMP, high);
1468 }
1469 __ Or(dst_high, lhs_high, TMP);
1470 }
1471 } else if (instruction->IsXor()) {
1472 uint32_t low = Low32Bits(value);
1473 uint32_t high = High32Bits(value);
1474 if (IsUint<16>(low)) {
1475 if (dst_low != lhs_low || low != 0) {
1476 __ Xori(dst_low, lhs_low, low);
1477 }
1478 } else {
1479 __ LoadConst32(TMP, low);
1480 __ Xor(dst_low, lhs_low, TMP);
1481 }
1482 if (IsUint<16>(high)) {
1483 if (dst_high != lhs_high || high != 0) {
1484 __ Xori(dst_high, lhs_high, high);
1485 }
1486 } else {
1487 if (high != low) {
1488 __ LoadConst32(TMP, high);
1489 }
1490 __ Xor(dst_high, lhs_high, TMP);
1491 }
1492 } else if (instruction->IsAnd()) {
1493 uint32_t low = Low32Bits(value);
1494 uint32_t high = High32Bits(value);
1495 if (IsUint<16>(low)) {
1496 __ Andi(dst_low, lhs_low, low);
1497 } else if (low != 0xFFFFFFFF) {
1498 __ LoadConst32(TMP, low);
1499 __ And(dst_low, lhs_low, TMP);
1500 } else if (dst_low != lhs_low) {
1501 __ Move(dst_low, lhs_low);
1502 }
1503 if (IsUint<16>(high)) {
1504 __ Andi(dst_high, lhs_high, high);
1505 } else if (high != 0xFFFFFFFF) {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ And(dst_high, lhs_high, TMP);
1510 } else if (dst_high != lhs_high) {
1511 __ Move(dst_high, lhs_high);
1512 }
1513 } else {
1514 if (instruction->IsSub()) {
1515 value = -value;
1516 } else {
1517 DCHECK(instruction->IsAdd());
1518 }
1519 int32_t low = Low32Bits(value);
1520 int32_t high = High32Bits(value);
1521 if (IsInt<16>(low)) {
1522 if (dst_low != lhs_low || low != 0) {
1523 __ Addiu(dst_low, lhs_low, low);
1524 }
1525 if (low != 0) {
1526 __ Sltiu(AT, dst_low, low);
1527 }
1528 } else {
1529 __ LoadConst32(TMP, low);
1530 __ Addu(dst_low, lhs_low, TMP);
1531 __ Sltu(AT, dst_low, TMP);
1532 }
1533 if (IsInt<16>(high)) {
1534 if (dst_high != lhs_high || high != 0) {
1535 __ Addiu(dst_high, lhs_high, high);
1536 }
1537 } else {
1538 if (high != low) {
1539 __ LoadConst32(TMP, high);
1540 }
1541 __ Addu(dst_high, lhs_high, TMP);
1542 }
1543 if (low != 0) {
1544 __ Addu(dst_high, dst_high, AT);
1545 }
1546 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 break;
1549 }
1550
1551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble: {
1553 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1554 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1555 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1556 if (instruction->IsAdd()) {
1557 if (type == Primitive::kPrimFloat) {
1558 __ AddS(dst, lhs, rhs);
1559 } else {
1560 __ AddD(dst, lhs, rhs);
1561 }
1562 } else {
1563 DCHECK(instruction->IsSub());
1564 if (type == Primitive::kPrimFloat) {
1565 __ SubS(dst, lhs, rhs);
1566 } else {
1567 __ SubD(dst, lhs, rhs);
1568 }
1569 }
1570 break;
1571 }
1572
1573 default:
1574 LOG(FATAL) << "Unexpected binary operation type " << type;
1575 }
1576}
1577
1578void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001579 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001580
1581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1582 Primitive::Type type = instr->GetResultType();
1583 switch (type) {
1584 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1588 break;
1589 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001590 locations->SetInAt(0, Location::RequiresRegister());
1591 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1592 locations->SetOut(Location::RequiresRegister());
1593 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 default:
1595 LOG(FATAL) << "Unexpected shift type " << type;
1596 }
1597}
1598
1599static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1600
1601void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001602 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 LocationSummary* locations = instr->GetLocations();
1604 Primitive::Type type = instr->GetType();
1605
1606 Location rhs_location = locations->InAt(1);
1607 bool use_imm = rhs_location.IsConstant();
1608 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1609 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001610 const uint32_t shift_mask =
1611 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001612 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1614 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615
1616 switch (type) {
1617 case Primitive::kPrimInt: {
1618 Register dst = locations->Out().AsRegister<Register>();
1619 Register lhs = locations->InAt(0).AsRegister<Register>();
1620 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001621 if (shift_value == 0) {
1622 if (dst != lhs) {
1623 __ Move(dst, lhs);
1624 }
1625 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 __ Sll(dst, lhs, shift_value);
1627 } else if (instr->IsShr()) {
1628 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001629 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001631 } else {
1632 if (has_ins_rotr) {
1633 __ Rotr(dst, lhs, shift_value);
1634 } else {
1635 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1636 __ Srl(dst, lhs, shift_value);
1637 __ Or(dst, dst, TMP);
1638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001639 }
1640 } else {
1641 if (instr->IsShl()) {
1642 __ Sllv(dst, lhs, rhs_reg);
1643 } else if (instr->IsShr()) {
1644 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001645 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001646 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001647 } else {
1648 if (has_ins_rotr) {
1649 __ Rotrv(dst, lhs, rhs_reg);
1650 } else {
1651 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001652 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1653 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1654 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1655 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1656 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001657 __ Sllv(TMP, lhs, TMP);
1658 __ Srlv(dst, lhs, rhs_reg);
1659 __ Or(dst, dst, TMP);
1660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001661 }
1662 }
1663 break;
1664 }
1665
1666 case Primitive::kPrimLong: {
1667 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1668 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1669 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1670 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1671 if (use_imm) {
1672 if (shift_value == 0) {
1673 codegen_->Move64(locations->Out(), locations->InAt(0));
1674 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001675 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001676 if (instr->IsShl()) {
1677 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1678 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1679 __ Sll(dst_low, lhs_low, shift_value);
1680 } else if (instr->IsShr()) {
1681 __ Srl(dst_low, lhs_low, shift_value);
1682 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1683 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001684 } else if (instr->IsUShr()) {
1685 __ Srl(dst_low, lhs_low, shift_value);
1686 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1687 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 } else {
1689 __ Srl(dst_low, lhs_low, shift_value);
1690 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1691 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001692 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001695 if (instr->IsShl()) {
1696 __ Sll(dst_low, lhs_low, shift_value);
1697 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1698 __ Sll(dst_high, lhs_high, shift_value);
1699 __ Or(dst_high, dst_high, TMP);
1700 } else if (instr->IsShr()) {
1701 __ Sra(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001706 __ Srl(dst_high, lhs_high, shift_value);
1707 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Srl(dst_low, lhs_low, shift_value);
1709 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001710 } else {
1711 __ Srl(TMP, lhs_low, shift_value);
1712 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1713 __ Or(dst_low, dst_low, TMP);
1714 __ Srl(TMP, lhs_high, shift_value);
1715 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1716 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Move(dst_low, ZERO);
1724 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001726 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001728 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001729 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001730 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001732 // 64-bit rotation by 32 is just a swap.
1733 __ Move(dst_low, lhs_high);
1734 __ Move(dst_high, lhs_low);
1735 } else {
1736 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Srl(dst_low, lhs_high, shift_value_high);
1738 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1739 __ Srl(dst_high, lhs_low, shift_value_high);
1740 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001742 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1743 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001745 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1746 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001747 __ Or(dst_high, dst_high, TMP);
1748 }
1749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001750 }
1751 }
1752 } else {
1753 MipsLabel done;
1754 if (instr->IsShl()) {
1755 __ Sllv(dst_low, lhs_low, rhs_reg);
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srl(TMP, lhs_low, 1);
1758 __ Srlv(TMP, TMP, AT);
1759 __ Sllv(dst_high, lhs_high, rhs_reg);
1760 __ Or(dst_high, dst_high, TMP);
1761 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1762 __ Beqz(TMP, &done);
1763 __ Move(dst_high, dst_low);
1764 __ Move(dst_low, ZERO);
1765 } else if (instr->IsShr()) {
1766 __ Srav(dst_high, lhs_high, rhs_reg);
1767 __ Nor(AT, ZERO, rhs_reg);
1768 __ Sll(TMP, lhs_high, 1);
1769 __ Sllv(TMP, TMP, AT);
1770 __ Srlv(dst_low, lhs_low, rhs_reg);
1771 __ Or(dst_low, dst_low, TMP);
1772 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1773 __ Beqz(TMP, &done);
1774 __ Move(dst_low, dst_high);
1775 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001776 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 __ Srlv(dst_high, lhs_high, rhs_reg);
1778 __ Nor(AT, ZERO, rhs_reg);
1779 __ Sll(TMP, lhs_high, 1);
1780 __ Sllv(TMP, TMP, AT);
1781 __ Srlv(dst_low, lhs_low, rhs_reg);
1782 __ Or(dst_low, dst_low, TMP);
1783 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1784 __ Beqz(TMP, &done);
1785 __ Move(dst_low, dst_high);
1786 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001787 } else {
1788 __ Nor(AT, ZERO, rhs_reg);
1789 __ Srlv(TMP, lhs_low, rhs_reg);
1790 __ Sll(dst_low, lhs_high, 1);
1791 __ Sllv(dst_low, dst_low, AT);
1792 __ Or(dst_low, dst_low, TMP);
1793 __ Srlv(TMP, lhs_high, rhs_reg);
1794 __ Sll(dst_high, lhs_low, 1);
1795 __ Sllv(dst_high, dst_high, AT);
1796 __ Or(dst_high, dst_high, TMP);
1797 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1798 __ Beqz(TMP, &done);
1799 __ Move(TMP, dst_high);
1800 __ Move(dst_high, dst_low);
1801 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802 }
1803 __ Bind(&done);
1804 }
1805 break;
1806 }
1807
1808 default:
1809 LOG(FATAL) << "Unexpected shift operation type " << type;
1810 }
1811}
1812
1813void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1814 HandleBinaryOp(instruction);
1815}
1816
1817void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1818 HandleBinaryOp(instruction);
1819}
1820
1821void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1822 HandleBinaryOp(instruction);
1823}
1824
1825void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1826 HandleBinaryOp(instruction);
1827}
1828
1829void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1834 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1835 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1836 } else {
1837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1838 }
1839}
1840
Alexey Frunze2923db72016-08-20 01:55:47 -07001841auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1842 auto null_checker = [this, instruction]() {
1843 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1844 };
1845 return null_checker;
1846}
1847
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1849 LocationSummary* locations = instruction->GetLocations();
1850 Register obj = locations->InAt(0).AsRegister<Register>();
1851 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001852 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001853 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001855 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 switch (type) {
1857 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 Register out = locations->Out().AsRegister<Register>();
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001862 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 } else {
1864 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001865 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 }
1867 break;
1868 }
1869
1870 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 Register out = locations->Out().AsRegister<Register>();
1872 if (index.IsConstant()) {
1873 size_t offset =
1874 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001875 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 } else {
1877 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001878 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 }
1880 break;
1881 }
1882
1883 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 Register out = locations->Out().AsRegister<Register>();
1885 if (index.IsConstant()) {
1886 size_t offset =
1887 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001888 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889 } else {
1890 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1891 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001892 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 Register out = locations->Out().AsRegister<Register>();
1899 if (index.IsConstant()) {
1900 size_t offset =
1901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001902 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 } else {
1904 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1905 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001906 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimInt:
1912 case Primitive::kPrimNot: {
1913 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 Register out = locations->Out().AsRegister<Register>();
1915 if (index.IsConstant()) {
1916 size_t offset =
1917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 } else {
1920 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1921 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001922 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 }
1924 break;
1925 }
1926
1927 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 Register out = locations->Out().AsRegisterPairLow<Register>();
1929 if (index.IsConstant()) {
1930 size_t offset =
1931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 } else {
1934 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1935 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001936 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 }
1938 break;
1939 }
1940
1941 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1943 if (index.IsConstant()) {
1944 size_t offset =
1945 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001946 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947 } else {
1948 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1949 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001950 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 }
1952 break;
1953 }
1954
1955 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1957 if (index.IsConstant()) {
1958 size_t offset =
1959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001960 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001961 } else {
1962 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1963 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001964 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001965 }
1966 break;
1967 }
1968
1969 case Primitive::kPrimVoid:
1970 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1971 UNREACHABLE();
1972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973}
1974
1975void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1976 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1979}
1980
1981void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1982 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001983 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 Register obj = locations->InAt(0).AsRegister<Register>();
1985 Register out = locations->Out().AsRegister<Register>();
1986 __ LoadFromOffset(kLoadWord, out, obj, offset);
1987 codegen_->MaybeRecordImplicitNullCheck(instruction);
1988}
1989
Alexey Frunzef58b2482016-09-02 22:14:06 -07001990Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1991 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1992 ? Location::ConstantLocation(instruction->AsConstant())
1993 : Location::RequiresRegister();
1994}
1995
1996Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1997 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1998 // We can store a non-zero float or double constant without first loading it into the FPU,
1999 // but we should only prefer this if the constant has a single use.
2000 if (instruction->IsConstant() &&
2001 (instruction->AsConstant()->IsZeroBitPattern() ||
2002 instruction->GetUses().HasExactlyOneElement())) {
2003 return Location::ConstantLocation(instruction->AsConstant());
2004 // Otherwise fall through and require an FPU register for the constant.
2005 }
2006 return Location::RequiresFpuRegister();
2007}
2008
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002010 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2012 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002013 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002014 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002015 InvokeRuntimeCallingConvention calling_convention;
2016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2017 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2018 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2019 } else {
2020 locations->SetInAt(0, Location::RequiresRegister());
2021 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2022 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002023 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002025 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 }
2028}
2029
2030void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2031 LocationSummary* locations = instruction->GetLocations();
2032 Register obj = locations->InAt(0).AsRegister<Register>();
2033 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 Primitive::Type value_type = instruction->GetComponentType();
2036 bool needs_runtime_call = locations->WillCall();
2037 bool needs_write_barrier =
2038 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002039 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002040 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041
2042 switch (value_type) {
2043 case Primitive::kPrimBoolean:
2044 case Primitive::kPrimByte: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002047 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002049 __ Addu(base_reg, obj, index.AsRegister<Register>());
2050 }
2051 if (value_location.IsConstant()) {
2052 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2053 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2054 } else {
2055 Register value = value_location.AsRegister<Register>();
2056 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimShort:
2062 case Primitive::kPrimChar: {
2063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002067 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2068 __ Addu(base_reg, obj, base_reg);
2069 }
2070 if (value_location.IsConstant()) {
2071 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2072 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2073 } else {
2074 Register value = value_location.AsRegister<Register>();
2075 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 }
2077 break;
2078 }
2079
2080 case Primitive::kPrimInt:
2081 case Primitive::kPrimNot: {
2082 if (!needs_runtime_call) {
2083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002087 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2088 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002090 if (value_location.IsConstant()) {
2091 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2092 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2093 DCHECK(!needs_write_barrier);
2094 } else {
2095 Register value = value_location.AsRegister<Register>();
2096 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2097 if (needs_write_barrier) {
2098 DCHECK_EQ(value_type, Primitive::kPrimNot);
2099 codegen_->MarkGCCard(obj, value);
2100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 } else {
2103 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002104 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimLong: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002114 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002115 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2116 __ Addu(base_reg, obj, base_reg);
2117 }
2118 if (value_location.IsConstant()) {
2119 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2120 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2121 } else {
2122 Register value = value_location.AsRegisterPairLow<Register>();
2123 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 }
2125 break;
2126 }
2127
2128 case Primitive::kPrimFloat: {
2129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002133 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2134 __ Addu(base_reg, obj, base_reg);
2135 }
2136 if (value_location.IsConstant()) {
2137 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2138 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2139 } else {
2140 FRegister value = value_location.AsFpuRegister<FRegister>();
2141 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 }
2143 break;
2144 }
2145
2146 case Primitive::kPrimDouble: {
2147 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002149 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002151 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2152 __ Addu(base_reg, obj, base_reg);
2153 }
2154 if (value_location.IsConstant()) {
2155 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2156 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2157 } else {
2158 FRegister value = value_location.AsFpuRegister<FRegister>();
2159 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimVoid:
2165 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2166 UNREACHABLE();
2167 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168}
2169
2170void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002171 RegisterSet caller_saves = RegisterSet::Empty();
2172 InvokeRuntimeCallingConvention calling_convention;
2173 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2174 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2175 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002178}
2179
2180void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2181 LocationSummary* locations = instruction->GetLocations();
2182 BoundsCheckSlowPathMIPS* slow_path =
2183 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2184 codegen_->AddSlowPath(slow_path);
2185
2186 Register index = locations->InAt(0).AsRegister<Register>();
2187 Register length = locations->InAt(1).AsRegister<Register>();
2188
2189 // length is limited by the maximum positive signed 32-bit integer.
2190 // Unsigned comparison of length and index checks for index < 0
2191 // and for length <= index simultaneously.
2192 __ Bgeu(index, length, slow_path->GetEntryLabel());
2193}
2194
2195void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2197 instruction,
2198 LocationSummary::kCallOnSlowPath);
2199 locations->SetInAt(0, Location::RequiresRegister());
2200 locations->SetInAt(1, Location::RequiresRegister());
2201 // Note that TypeCheckSlowPathMIPS uses this register too.
2202 locations->AddTemp(Location::RequiresRegister());
2203}
2204
2205void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2206 LocationSummary* locations = instruction->GetLocations();
2207 Register obj = locations->InAt(0).AsRegister<Register>();
2208 Register cls = locations->InAt(1).AsRegister<Register>();
2209 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2210
2211 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2212 codegen_->AddSlowPath(slow_path);
2213
2214 // TODO: avoid this check if we know obj is not null.
2215 __ Beqz(obj, slow_path->GetExitLabel());
2216 // Compare the class of `obj` with `cls`.
2217 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2218 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2219 __ Bind(slow_path->GetExitLabel());
2220}
2221
2222void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2223 LocationSummary* locations =
2224 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2225 locations->SetInAt(0, Location::RequiresRegister());
2226 if (check->HasUses()) {
2227 locations->SetOut(Location::SameAsFirstInput());
2228 }
2229}
2230
2231void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2232 // We assume the class is not null.
2233 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2234 check->GetLoadClass(),
2235 check,
2236 check->GetDexPc(),
2237 true);
2238 codegen_->AddSlowPath(slow_path);
2239 GenerateClassInitializationCheck(slow_path,
2240 check->GetLocations()->InAt(0).AsRegister<Register>());
2241}
2242
2243void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2244 Primitive::Type in_type = compare->InputAt(0)->GetType();
2245
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248
2249 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002250 case Primitive::kPrimBoolean:
2251 case Primitive::kPrimByte:
2252 case Primitive::kPrimShort:
2253 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002254 case Primitive::kPrimInt:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 case Primitive::kPrimLong:
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, Location::RequiresRegister());
2258 // Output overlaps because it is written before doing the low comparison.
2259 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2260 break;
2261
2262 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002263 case Primitive::kPrimDouble:
2264 locations->SetInAt(0, Location::RequiresFpuRegister());
2265 locations->SetInAt(1, Location::RequiresFpuRegister());
2266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268
2269 default:
2270 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2271 }
2272}
2273
2274void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2275 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002276 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002278 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002279
2280 // 0 if: left == right
2281 // 1 if: left > right
2282 // -1 if: left < right
2283 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002284 case Primitive::kPrimBoolean:
2285 case Primitive::kPrimByte:
2286 case Primitive::kPrimShort:
2287 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002288 case Primitive::kPrimInt: {
2289 Register lhs = locations->InAt(0).AsRegister<Register>();
2290 Register rhs = locations->InAt(1).AsRegister<Register>();
2291 __ Slt(TMP, lhs, rhs);
2292 __ Slt(res, rhs, lhs);
2293 __ Subu(res, res, TMP);
2294 break;
2295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296 case Primitive::kPrimLong: {
2297 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2299 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2300 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2301 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2302 // TODO: more efficient (direct) comparison with a constant.
2303 __ Slt(TMP, lhs_high, rhs_high);
2304 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2305 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2306 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2307 __ Sltu(TMP, lhs_low, rhs_low);
2308 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2309 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2310 __ Bind(&done);
2311 break;
2312 }
2313
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002314 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002315 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002316 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2317 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2318 MipsLabel done;
2319 if (isR6) {
2320 __ CmpEqS(FTMP, lhs, rhs);
2321 __ LoadConst32(res, 0);
2322 __ Bc1nez(FTMP, &done);
2323 if (gt_bias) {
2324 __ CmpLtS(FTMP, lhs, rhs);
2325 __ LoadConst32(res, -1);
2326 __ Bc1nez(FTMP, &done);
2327 __ LoadConst32(res, 1);
2328 } else {
2329 __ CmpLtS(FTMP, rhs, lhs);
2330 __ LoadConst32(res, 1);
2331 __ Bc1nez(FTMP, &done);
2332 __ LoadConst32(res, -1);
2333 }
2334 } else {
2335 if (gt_bias) {
2336 __ ColtS(0, lhs, rhs);
2337 __ LoadConst32(res, -1);
2338 __ Bc1t(0, &done);
2339 __ CeqS(0, lhs, rhs);
2340 __ LoadConst32(res, 1);
2341 __ Movt(res, ZERO, 0);
2342 } else {
2343 __ ColtS(0, rhs, lhs);
2344 __ LoadConst32(res, 1);
2345 __ Bc1t(0, &done);
2346 __ CeqS(0, lhs, rhs);
2347 __ LoadConst32(res, -1);
2348 __ Movt(res, ZERO, 0);
2349 }
2350 }
2351 __ Bind(&done);
2352 break;
2353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002355 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002356 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2357 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2358 MipsLabel done;
2359 if (isR6) {
2360 __ CmpEqD(FTMP, lhs, rhs);
2361 __ LoadConst32(res, 0);
2362 __ Bc1nez(FTMP, &done);
2363 if (gt_bias) {
2364 __ CmpLtD(FTMP, lhs, rhs);
2365 __ LoadConst32(res, -1);
2366 __ Bc1nez(FTMP, &done);
2367 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002369 __ CmpLtD(FTMP, rhs, lhs);
2370 __ LoadConst32(res, 1);
2371 __ Bc1nez(FTMP, &done);
2372 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 }
2374 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 if (gt_bias) {
2376 __ ColtD(0, lhs, rhs);
2377 __ LoadConst32(res, -1);
2378 __ Bc1t(0, &done);
2379 __ CeqD(0, lhs, rhs);
2380 __ LoadConst32(res, 1);
2381 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 __ ColtD(0, rhs, lhs);
2384 __ LoadConst32(res, 1);
2385 __ Bc1t(0, &done);
2386 __ CeqD(0, lhs, rhs);
2387 __ LoadConst32(res, -1);
2388 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 }
2390 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002391 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 break;
2393 }
2394
2395 default:
2396 LOG(FATAL) << "Unimplemented compare type " << in_type;
2397 }
2398}
2399
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002400void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002402 switch (instruction->InputAt(0)->GetType()) {
2403 default:
2404 case Primitive::kPrimLong:
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2407 break;
2408
2409 case Primitive::kPrimFloat:
2410 case Primitive::kPrimDouble:
2411 locations->SetInAt(0, Location::RequiresFpuRegister());
2412 locations->SetInAt(1, Location::RequiresFpuRegister());
2413 break;
2414 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002415 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2417 }
2418}
2419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002420void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002421 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 return;
2423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002425 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 LocationSummary* locations = instruction->GetLocations();
2427 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002430 switch (type) {
2431 default:
2432 // Integer case.
2433 GenerateIntCompare(instruction->GetCondition(), locations);
2434 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002436 case Primitive::kPrimLong:
2437 // TODO: don't use branches.
2438 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 break;
2440
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002441 case Primitive::kPrimFloat:
2442 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002443 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2444 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002446
2447 // Convert the branches into the result.
2448 MipsLabel done;
2449
2450 // False case: result = 0.
2451 __ LoadConst32(dst, 0);
2452 __ B(&done);
2453
2454 // True case: result = 1.
2455 __ Bind(&true_label);
2456 __ LoadConst32(dst, 1);
2457 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458}
2459
Alexey Frunze7e99e052015-11-24 19:28:01 -08002460void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2461 DCHECK(instruction->IsDiv() || instruction->IsRem());
2462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location second = locations->InAt(1);
2466 DCHECK(second.IsConstant());
2467
2468 Register out = locations->Out().AsRegister<Register>();
2469 Register dividend = locations->InAt(0).AsRegister<Register>();
2470 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2471 DCHECK(imm == 1 || imm == -1);
2472
2473 if (instruction->IsRem()) {
2474 __ Move(out, ZERO);
2475 } else {
2476 if (imm == -1) {
2477 __ Subu(out, ZERO, dividend);
2478 } else if (out != dividend) {
2479 __ Move(out, dividend);
2480 }
2481 }
2482}
2483
2484void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2485 DCHECK(instruction->IsDiv() || instruction->IsRem());
2486 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2487
2488 LocationSummary* locations = instruction->GetLocations();
2489 Location second = locations->InAt(1);
2490 DCHECK(second.IsConstant());
2491
2492 Register out = locations->Out().AsRegister<Register>();
2493 Register dividend = locations->InAt(0).AsRegister<Register>();
2494 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002495 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002496 int ctz_imm = CTZ(abs_imm);
2497
2498 if (instruction->IsDiv()) {
2499 if (ctz_imm == 1) {
2500 // Fast path for division by +/-2, which is very common.
2501 __ Srl(TMP, dividend, 31);
2502 } else {
2503 __ Sra(TMP, dividend, 31);
2504 __ Srl(TMP, TMP, 32 - ctz_imm);
2505 }
2506 __ Addu(out, dividend, TMP);
2507 __ Sra(out, out, ctz_imm);
2508 if (imm < 0) {
2509 __ Subu(out, ZERO, out);
2510 }
2511 } else {
2512 if (ctz_imm == 1) {
2513 // Fast path for modulo +/-2, which is very common.
2514 __ Sra(TMP, dividend, 31);
2515 __ Subu(out, dividend, TMP);
2516 __ Andi(out, out, 1);
2517 __ Addu(out, out, TMP);
2518 } else {
2519 __ Sra(TMP, dividend, 31);
2520 __ Srl(TMP, TMP, 32 - ctz_imm);
2521 __ Addu(out, dividend, TMP);
2522 if (IsUint<16>(abs_imm - 1)) {
2523 __ Andi(out, out, abs_imm - 1);
2524 } else {
2525 __ Sll(out, out, 32 - ctz_imm);
2526 __ Srl(out, out, 32 - ctz_imm);
2527 }
2528 __ Subu(out, out, TMP);
2529 }
2530 }
2531}
2532
2533void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2534 DCHECK(instruction->IsDiv() || instruction->IsRem());
2535 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2536
2537 LocationSummary* locations = instruction->GetLocations();
2538 Location second = locations->InAt(1);
2539 DCHECK(second.IsConstant());
2540
2541 Register out = locations->Out().AsRegister<Register>();
2542 Register dividend = locations->InAt(0).AsRegister<Register>();
2543 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2544
2545 int64_t magic;
2546 int shift;
2547 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2548
2549 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2550
2551 __ LoadConst32(TMP, magic);
2552 if (isR6) {
2553 __ MuhR6(TMP, dividend, TMP);
2554 } else {
2555 __ MultR2(dividend, TMP);
2556 __ Mfhi(TMP);
2557 }
2558 if (imm > 0 && magic < 0) {
2559 __ Addu(TMP, TMP, dividend);
2560 } else if (imm < 0 && magic > 0) {
2561 __ Subu(TMP, TMP, dividend);
2562 }
2563
2564 if (shift != 0) {
2565 __ Sra(TMP, TMP, shift);
2566 }
2567
2568 if (instruction->IsDiv()) {
2569 __ Sra(out, TMP, 31);
2570 __ Subu(out, TMP, out);
2571 } else {
2572 __ Sra(AT, TMP, 31);
2573 __ Subu(AT, TMP, AT);
2574 __ LoadConst32(TMP, imm);
2575 if (isR6) {
2576 __ MulR6(TMP, AT, TMP);
2577 } else {
2578 __ MulR2(TMP, AT, TMP);
2579 }
2580 __ Subu(out, dividend, TMP);
2581 }
2582}
2583
2584void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2585 DCHECK(instruction->IsDiv() || instruction->IsRem());
2586 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2587
2588 LocationSummary* locations = instruction->GetLocations();
2589 Register out = locations->Out().AsRegister<Register>();
2590 Location second = locations->InAt(1);
2591
2592 if (second.IsConstant()) {
2593 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2594 if (imm == 0) {
2595 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2596 } else if (imm == 1 || imm == -1) {
2597 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002598 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002599 DivRemByPowerOfTwo(instruction);
2600 } else {
2601 DCHECK(imm <= -2 || imm >= 2);
2602 GenerateDivRemWithAnyConstant(instruction);
2603 }
2604 } else {
2605 Register dividend = locations->InAt(0).AsRegister<Register>();
2606 Register divisor = second.AsRegister<Register>();
2607 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2608 if (instruction->IsDiv()) {
2609 if (isR6) {
2610 __ DivR6(out, dividend, divisor);
2611 } else {
2612 __ DivR2(out, dividend, divisor);
2613 }
2614 } else {
2615 if (isR6) {
2616 __ ModR6(out, dividend, divisor);
2617 } else {
2618 __ ModR2(out, dividend, divisor);
2619 }
2620 }
2621 }
2622}
2623
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2625 Primitive::Type type = div->GetResultType();
2626 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002627 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 : LocationSummary::kNoCall;
2629
2630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2631
2632 switch (type) {
2633 case Primitive::kPrimInt:
2634 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002635 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2637 break;
2638
2639 case Primitive::kPrimLong: {
2640 InvokeRuntimeCallingConvention calling_convention;
2641 locations->SetInAt(0, Location::RegisterPairLocation(
2642 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2643 locations->SetInAt(1, Location::RegisterPairLocation(
2644 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2645 locations->SetOut(calling_convention.GetReturnLocation(type));
2646 break;
2647 }
2648
2649 case Primitive::kPrimFloat:
2650 case Primitive::kPrimDouble:
2651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetInAt(1, Location::RequiresFpuRegister());
2653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2654 break;
2655
2656 default:
2657 LOG(FATAL) << "Unexpected div type " << type;
2658 }
2659}
2660
2661void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2662 Primitive::Type type = instruction->GetType();
2663 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664
2665 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002666 case Primitive::kPrimInt:
2667 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002670 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2672 break;
2673 }
2674 case Primitive::kPrimFloat:
2675 case Primitive::kPrimDouble: {
2676 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2677 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2678 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2679 if (type == Primitive::kPrimFloat) {
2680 __ DivS(dst, lhs, rhs);
2681 } else {
2682 __ DivD(dst, lhs, rhs);
2683 }
2684 break;
2685 }
2686 default:
2687 LOG(FATAL) << "Unexpected div type " << type;
2688 }
2689}
2690
2691void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002692 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002693 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694}
2695
2696void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2697 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2698 codegen_->AddSlowPath(slow_path);
2699 Location value = instruction->GetLocations()->InAt(0);
2700 Primitive::Type type = instruction->GetType();
2701
2702 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002703 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 case Primitive::kPrimByte:
2705 case Primitive::kPrimChar:
2706 case Primitive::kPrimShort:
2707 case Primitive::kPrimInt: {
2708 if (value.IsConstant()) {
2709 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2710 __ B(slow_path->GetEntryLabel());
2711 } else {
2712 // A division by a non-null constant is valid. We don't need to perform
2713 // any check, so simply fall through.
2714 }
2715 } else {
2716 DCHECK(value.IsRegister()) << value;
2717 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2718 }
2719 break;
2720 }
2721 case Primitive::kPrimLong: {
2722 if (value.IsConstant()) {
2723 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2724 __ B(slow_path->GetEntryLabel());
2725 } else {
2726 // A division by a non-null constant is valid. We don't need to perform
2727 // any check, so simply fall through.
2728 }
2729 } else {
2730 DCHECK(value.IsRegisterPair()) << value;
2731 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2732 __ Beqz(TMP, slow_path->GetEntryLabel());
2733 }
2734 break;
2735 }
2736 default:
2737 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2738 }
2739}
2740
2741void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2744 locations->SetOut(Location::ConstantLocation(constant));
2745}
2746
2747void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2748 // Will be generated at use site.
2749}
2750
2751void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2752 exit->SetLocations(nullptr);
2753}
2754
2755void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2756}
2757
2758void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2761 locations->SetOut(Location::ConstantLocation(constant));
2762}
2763
2764void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2765 // Will be generated at use site.
2766}
2767
2768void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2769 got->SetLocations(nullptr);
2770}
2771
2772void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2773 DCHECK(!successor->IsExitBlock());
2774 HBasicBlock* block = got->GetBlock();
2775 HInstruction* previous = got->GetPrevious();
2776 HLoopInformation* info = block->GetLoopInformation();
2777
2778 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2779 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2781 return;
2782 }
2783 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2784 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2785 }
2786 if (!codegen_->GoesToNextBlock(block, successor)) {
2787 __ B(codegen_->GetLabelOf(successor));
2788 }
2789}
2790
2791void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2792 HandleGoto(got, got->GetSuccessor());
2793}
2794
2795void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2796 try_boundary->SetLocations(nullptr);
2797}
2798
2799void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2800 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2801 if (!successor->IsExitBlock()) {
2802 HandleGoto(try_boundary, successor);
2803 }
2804}
2805
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002806void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2807 LocationSummary* locations) {
2808 Register dst = locations->Out().AsRegister<Register>();
2809 Register lhs = locations->InAt(0).AsRegister<Register>();
2810 Location rhs_location = locations->InAt(1);
2811 Register rhs_reg = ZERO;
2812 int64_t rhs_imm = 0;
2813 bool use_imm = rhs_location.IsConstant();
2814 if (use_imm) {
2815 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2816 } else {
2817 rhs_reg = rhs_location.AsRegister<Register>();
2818 }
2819
2820 switch (cond) {
2821 case kCondEQ:
2822 case kCondNE:
2823 if (use_imm && IsUint<16>(rhs_imm)) {
2824 __ Xori(dst, lhs, rhs_imm);
2825 } else {
2826 if (use_imm) {
2827 rhs_reg = TMP;
2828 __ LoadConst32(rhs_reg, rhs_imm);
2829 }
2830 __ Xor(dst, lhs, rhs_reg);
2831 }
2832 if (cond == kCondEQ) {
2833 __ Sltiu(dst, dst, 1);
2834 } else {
2835 __ Sltu(dst, ZERO, dst);
2836 }
2837 break;
2838
2839 case kCondLT:
2840 case kCondGE:
2841 if (use_imm && IsInt<16>(rhs_imm)) {
2842 __ Slti(dst, lhs, rhs_imm);
2843 } else {
2844 if (use_imm) {
2845 rhs_reg = TMP;
2846 __ LoadConst32(rhs_reg, rhs_imm);
2847 }
2848 __ Slt(dst, lhs, rhs_reg);
2849 }
2850 if (cond == kCondGE) {
2851 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2852 // only the slt instruction but no sge.
2853 __ Xori(dst, dst, 1);
2854 }
2855 break;
2856
2857 case kCondLE:
2858 case kCondGT:
2859 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2860 // Simulate lhs <= rhs via lhs < rhs + 1.
2861 __ Slti(dst, lhs, rhs_imm + 1);
2862 if (cond == kCondGT) {
2863 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2864 // only the slti instruction but no sgti.
2865 __ Xori(dst, dst, 1);
2866 }
2867 } else {
2868 if (use_imm) {
2869 rhs_reg = TMP;
2870 __ LoadConst32(rhs_reg, rhs_imm);
2871 }
2872 __ Slt(dst, rhs_reg, lhs);
2873 if (cond == kCondLE) {
2874 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2875 // only the slt instruction but no sle.
2876 __ Xori(dst, dst, 1);
2877 }
2878 }
2879 break;
2880
2881 case kCondB:
2882 case kCondAE:
2883 if (use_imm && IsInt<16>(rhs_imm)) {
2884 // Sltiu sign-extends its 16-bit immediate operand before
2885 // the comparison and thus lets us compare directly with
2886 // unsigned values in the ranges [0, 0x7fff] and
2887 // [0xffff8000, 0xffffffff].
2888 __ Sltiu(dst, lhs, rhs_imm);
2889 } else {
2890 if (use_imm) {
2891 rhs_reg = TMP;
2892 __ LoadConst32(rhs_reg, rhs_imm);
2893 }
2894 __ Sltu(dst, lhs, rhs_reg);
2895 }
2896 if (cond == kCondAE) {
2897 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2898 // only the sltu instruction but no sgeu.
2899 __ Xori(dst, dst, 1);
2900 }
2901 break;
2902
2903 case kCondBE:
2904 case kCondA:
2905 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2906 // Simulate lhs <= rhs via lhs < rhs + 1.
2907 // Note that this only works if rhs + 1 does not overflow
2908 // to 0, hence the check above.
2909 // Sltiu sign-extends its 16-bit immediate operand before
2910 // the comparison and thus lets us compare directly with
2911 // unsigned values in the ranges [0, 0x7fff] and
2912 // [0xffff8000, 0xffffffff].
2913 __ Sltiu(dst, lhs, rhs_imm + 1);
2914 if (cond == kCondA) {
2915 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2916 // only the sltiu instruction but no sgtiu.
2917 __ Xori(dst, dst, 1);
2918 }
2919 } else {
2920 if (use_imm) {
2921 rhs_reg = TMP;
2922 __ LoadConst32(rhs_reg, rhs_imm);
2923 }
2924 __ Sltu(dst, rhs_reg, lhs);
2925 if (cond == kCondBE) {
2926 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2927 // only the sltu instruction but no sleu.
2928 __ Xori(dst, dst, 1);
2929 }
2930 }
2931 break;
2932 }
2933}
2934
2935void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
2936 LocationSummary* locations,
2937 MipsLabel* label) {
2938 Register lhs = locations->InAt(0).AsRegister<Register>();
2939 Location rhs_location = locations->InAt(1);
2940 Register rhs_reg = ZERO;
2941 int32_t rhs_imm = 0;
2942 bool use_imm = rhs_location.IsConstant();
2943 if (use_imm) {
2944 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2945 } else {
2946 rhs_reg = rhs_location.AsRegister<Register>();
2947 }
2948
2949 if (use_imm && rhs_imm == 0) {
2950 switch (cond) {
2951 case kCondEQ:
2952 case kCondBE: // <= 0 if zero
2953 __ Beqz(lhs, label);
2954 break;
2955 case kCondNE:
2956 case kCondA: // > 0 if non-zero
2957 __ Bnez(lhs, label);
2958 break;
2959 case kCondLT:
2960 __ Bltz(lhs, label);
2961 break;
2962 case kCondGE:
2963 __ Bgez(lhs, label);
2964 break;
2965 case kCondLE:
2966 __ Blez(lhs, label);
2967 break;
2968 case kCondGT:
2969 __ Bgtz(lhs, label);
2970 break;
2971 case kCondB: // always false
2972 break;
2973 case kCondAE: // always true
2974 __ B(label);
2975 break;
2976 }
2977 } else {
2978 if (use_imm) {
2979 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2980 rhs_reg = TMP;
2981 __ LoadConst32(rhs_reg, rhs_imm);
2982 }
2983 switch (cond) {
2984 case kCondEQ:
2985 __ Beq(lhs, rhs_reg, label);
2986 break;
2987 case kCondNE:
2988 __ Bne(lhs, rhs_reg, label);
2989 break;
2990 case kCondLT:
2991 __ Blt(lhs, rhs_reg, label);
2992 break;
2993 case kCondGE:
2994 __ Bge(lhs, rhs_reg, label);
2995 break;
2996 case kCondLE:
2997 __ Bge(rhs_reg, lhs, label);
2998 break;
2999 case kCondGT:
3000 __ Blt(rhs_reg, lhs, label);
3001 break;
3002 case kCondB:
3003 __ Bltu(lhs, rhs_reg, label);
3004 break;
3005 case kCondAE:
3006 __ Bgeu(lhs, rhs_reg, label);
3007 break;
3008 case kCondBE:
3009 __ Bgeu(rhs_reg, lhs, label);
3010 break;
3011 case kCondA:
3012 __ Bltu(rhs_reg, lhs, label);
3013 break;
3014 }
3015 }
3016}
3017
3018void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3019 LocationSummary* locations,
3020 MipsLabel* label) {
3021 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3022 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3023 Location rhs_location = locations->InAt(1);
3024 Register rhs_high = ZERO;
3025 Register rhs_low = ZERO;
3026 int64_t imm = 0;
3027 uint32_t imm_high = 0;
3028 uint32_t imm_low = 0;
3029 bool use_imm = rhs_location.IsConstant();
3030 if (use_imm) {
3031 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3032 imm_high = High32Bits(imm);
3033 imm_low = Low32Bits(imm);
3034 } else {
3035 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3036 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3037 }
3038
3039 if (use_imm && imm == 0) {
3040 switch (cond) {
3041 case kCondEQ:
3042 case kCondBE: // <= 0 if zero
3043 __ Or(TMP, lhs_high, lhs_low);
3044 __ Beqz(TMP, label);
3045 break;
3046 case kCondNE:
3047 case kCondA: // > 0 if non-zero
3048 __ Or(TMP, lhs_high, lhs_low);
3049 __ Bnez(TMP, label);
3050 break;
3051 case kCondLT:
3052 __ Bltz(lhs_high, label);
3053 break;
3054 case kCondGE:
3055 __ Bgez(lhs_high, label);
3056 break;
3057 case kCondLE:
3058 __ Or(TMP, lhs_high, lhs_low);
3059 __ Sra(AT, lhs_high, 31);
3060 __ Bgeu(AT, TMP, label);
3061 break;
3062 case kCondGT:
3063 __ Or(TMP, lhs_high, lhs_low);
3064 __ Sra(AT, lhs_high, 31);
3065 __ Bltu(AT, TMP, label);
3066 break;
3067 case kCondB: // always false
3068 break;
3069 case kCondAE: // always true
3070 __ B(label);
3071 break;
3072 }
3073 } else if (use_imm) {
3074 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3075 switch (cond) {
3076 case kCondEQ:
3077 __ LoadConst32(TMP, imm_high);
3078 __ Xor(TMP, TMP, lhs_high);
3079 __ LoadConst32(AT, imm_low);
3080 __ Xor(AT, AT, lhs_low);
3081 __ Or(TMP, TMP, AT);
3082 __ Beqz(TMP, label);
3083 break;
3084 case kCondNE:
3085 __ LoadConst32(TMP, imm_high);
3086 __ Xor(TMP, TMP, lhs_high);
3087 __ LoadConst32(AT, imm_low);
3088 __ Xor(AT, AT, lhs_low);
3089 __ Or(TMP, TMP, AT);
3090 __ Bnez(TMP, label);
3091 break;
3092 case kCondLT:
3093 __ LoadConst32(TMP, imm_high);
3094 __ Blt(lhs_high, TMP, label);
3095 __ Slt(TMP, TMP, lhs_high);
3096 __ LoadConst32(AT, imm_low);
3097 __ Sltu(AT, lhs_low, AT);
3098 __ Blt(TMP, AT, label);
3099 break;
3100 case kCondGE:
3101 __ LoadConst32(TMP, imm_high);
3102 __ Blt(TMP, lhs_high, label);
3103 __ Slt(TMP, lhs_high, TMP);
3104 __ LoadConst32(AT, imm_low);
3105 __ Sltu(AT, lhs_low, AT);
3106 __ Or(TMP, TMP, AT);
3107 __ Beqz(TMP, label);
3108 break;
3109 case kCondLE:
3110 __ LoadConst32(TMP, imm_high);
3111 __ Blt(lhs_high, TMP, label);
3112 __ Slt(TMP, TMP, lhs_high);
3113 __ LoadConst32(AT, imm_low);
3114 __ Sltu(AT, AT, lhs_low);
3115 __ Or(TMP, TMP, AT);
3116 __ Beqz(TMP, label);
3117 break;
3118 case kCondGT:
3119 __ LoadConst32(TMP, imm_high);
3120 __ Blt(TMP, lhs_high, label);
3121 __ Slt(TMP, lhs_high, TMP);
3122 __ LoadConst32(AT, imm_low);
3123 __ Sltu(AT, AT, lhs_low);
3124 __ Blt(TMP, AT, label);
3125 break;
3126 case kCondB:
3127 __ LoadConst32(TMP, imm_high);
3128 __ Bltu(lhs_high, TMP, label);
3129 __ Sltu(TMP, TMP, lhs_high);
3130 __ LoadConst32(AT, imm_low);
3131 __ Sltu(AT, lhs_low, AT);
3132 __ Blt(TMP, AT, label);
3133 break;
3134 case kCondAE:
3135 __ LoadConst32(TMP, imm_high);
3136 __ Bltu(TMP, lhs_high, label);
3137 __ Sltu(TMP, lhs_high, TMP);
3138 __ LoadConst32(AT, imm_low);
3139 __ Sltu(AT, lhs_low, AT);
3140 __ Or(TMP, TMP, AT);
3141 __ Beqz(TMP, label);
3142 break;
3143 case kCondBE:
3144 __ LoadConst32(TMP, imm_high);
3145 __ Bltu(lhs_high, TMP, label);
3146 __ Sltu(TMP, TMP, lhs_high);
3147 __ LoadConst32(AT, imm_low);
3148 __ Sltu(AT, AT, lhs_low);
3149 __ Or(TMP, TMP, AT);
3150 __ Beqz(TMP, label);
3151 break;
3152 case kCondA:
3153 __ LoadConst32(TMP, imm_high);
3154 __ Bltu(TMP, lhs_high, label);
3155 __ Sltu(TMP, lhs_high, TMP);
3156 __ LoadConst32(AT, imm_low);
3157 __ Sltu(AT, AT, lhs_low);
3158 __ Blt(TMP, AT, label);
3159 break;
3160 }
3161 } else {
3162 switch (cond) {
3163 case kCondEQ:
3164 __ Xor(TMP, lhs_high, rhs_high);
3165 __ Xor(AT, lhs_low, rhs_low);
3166 __ Or(TMP, TMP, AT);
3167 __ Beqz(TMP, label);
3168 break;
3169 case kCondNE:
3170 __ Xor(TMP, lhs_high, rhs_high);
3171 __ Xor(AT, lhs_low, rhs_low);
3172 __ Or(TMP, TMP, AT);
3173 __ Bnez(TMP, label);
3174 break;
3175 case kCondLT:
3176 __ Blt(lhs_high, rhs_high, label);
3177 __ Slt(TMP, rhs_high, lhs_high);
3178 __ Sltu(AT, lhs_low, rhs_low);
3179 __ Blt(TMP, AT, label);
3180 break;
3181 case kCondGE:
3182 __ Blt(rhs_high, lhs_high, label);
3183 __ Slt(TMP, lhs_high, rhs_high);
3184 __ Sltu(AT, lhs_low, rhs_low);
3185 __ Or(TMP, TMP, AT);
3186 __ Beqz(TMP, label);
3187 break;
3188 case kCondLE:
3189 __ Blt(lhs_high, rhs_high, label);
3190 __ Slt(TMP, rhs_high, lhs_high);
3191 __ Sltu(AT, rhs_low, lhs_low);
3192 __ Or(TMP, TMP, AT);
3193 __ Beqz(TMP, label);
3194 break;
3195 case kCondGT:
3196 __ Blt(rhs_high, lhs_high, label);
3197 __ Slt(TMP, lhs_high, rhs_high);
3198 __ Sltu(AT, rhs_low, lhs_low);
3199 __ Blt(TMP, AT, label);
3200 break;
3201 case kCondB:
3202 __ Bltu(lhs_high, rhs_high, label);
3203 __ Sltu(TMP, rhs_high, lhs_high);
3204 __ Sltu(AT, lhs_low, rhs_low);
3205 __ Blt(TMP, AT, label);
3206 break;
3207 case kCondAE:
3208 __ Bltu(rhs_high, lhs_high, label);
3209 __ Sltu(TMP, lhs_high, rhs_high);
3210 __ Sltu(AT, lhs_low, rhs_low);
3211 __ Or(TMP, TMP, AT);
3212 __ Beqz(TMP, label);
3213 break;
3214 case kCondBE:
3215 __ Bltu(lhs_high, rhs_high, label);
3216 __ Sltu(TMP, rhs_high, lhs_high);
3217 __ Sltu(AT, rhs_low, lhs_low);
3218 __ Or(TMP, TMP, AT);
3219 __ Beqz(TMP, label);
3220 break;
3221 case kCondA:
3222 __ Bltu(rhs_high, lhs_high, label);
3223 __ Sltu(TMP, lhs_high, rhs_high);
3224 __ Sltu(AT, rhs_low, lhs_low);
3225 __ Blt(TMP, AT, label);
3226 break;
3227 }
3228 }
3229}
3230
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003231void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3232 bool gt_bias,
3233 Primitive::Type type,
3234 LocationSummary* locations) {
3235 Register dst = locations->Out().AsRegister<Register>();
3236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3238 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3239 if (type == Primitive::kPrimFloat) {
3240 if (isR6) {
3241 switch (cond) {
3242 case kCondEQ:
3243 __ CmpEqS(FTMP, lhs, rhs);
3244 __ Mfc1(dst, FTMP);
3245 __ Andi(dst, dst, 1);
3246 break;
3247 case kCondNE:
3248 __ CmpEqS(FTMP, lhs, rhs);
3249 __ Mfc1(dst, FTMP);
3250 __ Addiu(dst, dst, 1);
3251 break;
3252 case kCondLT:
3253 if (gt_bias) {
3254 __ CmpLtS(FTMP, lhs, rhs);
3255 } else {
3256 __ CmpUltS(FTMP, lhs, rhs);
3257 }
3258 __ Mfc1(dst, FTMP);
3259 __ Andi(dst, dst, 1);
3260 break;
3261 case kCondLE:
3262 if (gt_bias) {
3263 __ CmpLeS(FTMP, lhs, rhs);
3264 } else {
3265 __ CmpUleS(FTMP, lhs, rhs);
3266 }
3267 __ Mfc1(dst, FTMP);
3268 __ Andi(dst, dst, 1);
3269 break;
3270 case kCondGT:
3271 if (gt_bias) {
3272 __ CmpUltS(FTMP, rhs, lhs);
3273 } else {
3274 __ CmpLtS(FTMP, rhs, lhs);
3275 }
3276 __ Mfc1(dst, FTMP);
3277 __ Andi(dst, dst, 1);
3278 break;
3279 case kCondGE:
3280 if (gt_bias) {
3281 __ CmpUleS(FTMP, rhs, lhs);
3282 } else {
3283 __ CmpLeS(FTMP, rhs, lhs);
3284 }
3285 __ Mfc1(dst, FTMP);
3286 __ Andi(dst, dst, 1);
3287 break;
3288 default:
3289 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3290 UNREACHABLE();
3291 }
3292 } else {
3293 switch (cond) {
3294 case kCondEQ:
3295 __ CeqS(0, lhs, rhs);
3296 __ LoadConst32(dst, 1);
3297 __ Movf(dst, ZERO, 0);
3298 break;
3299 case kCondNE:
3300 __ CeqS(0, lhs, rhs);
3301 __ LoadConst32(dst, 1);
3302 __ Movt(dst, ZERO, 0);
3303 break;
3304 case kCondLT:
3305 if (gt_bias) {
3306 __ ColtS(0, lhs, rhs);
3307 } else {
3308 __ CultS(0, lhs, rhs);
3309 }
3310 __ LoadConst32(dst, 1);
3311 __ Movf(dst, ZERO, 0);
3312 break;
3313 case kCondLE:
3314 if (gt_bias) {
3315 __ ColeS(0, lhs, rhs);
3316 } else {
3317 __ CuleS(0, lhs, rhs);
3318 }
3319 __ LoadConst32(dst, 1);
3320 __ Movf(dst, ZERO, 0);
3321 break;
3322 case kCondGT:
3323 if (gt_bias) {
3324 __ CultS(0, rhs, lhs);
3325 } else {
3326 __ ColtS(0, rhs, lhs);
3327 }
3328 __ LoadConst32(dst, 1);
3329 __ Movf(dst, ZERO, 0);
3330 break;
3331 case kCondGE:
3332 if (gt_bias) {
3333 __ CuleS(0, rhs, lhs);
3334 } else {
3335 __ ColeS(0, rhs, lhs);
3336 }
3337 __ LoadConst32(dst, 1);
3338 __ Movf(dst, ZERO, 0);
3339 break;
3340 default:
3341 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3342 UNREACHABLE();
3343 }
3344 }
3345 } else {
3346 DCHECK_EQ(type, Primitive::kPrimDouble);
3347 if (isR6) {
3348 switch (cond) {
3349 case kCondEQ:
3350 __ CmpEqD(FTMP, lhs, rhs);
3351 __ Mfc1(dst, FTMP);
3352 __ Andi(dst, dst, 1);
3353 break;
3354 case kCondNE:
3355 __ CmpEqD(FTMP, lhs, rhs);
3356 __ Mfc1(dst, FTMP);
3357 __ Addiu(dst, dst, 1);
3358 break;
3359 case kCondLT:
3360 if (gt_bias) {
3361 __ CmpLtD(FTMP, lhs, rhs);
3362 } else {
3363 __ CmpUltD(FTMP, lhs, rhs);
3364 }
3365 __ Mfc1(dst, FTMP);
3366 __ Andi(dst, dst, 1);
3367 break;
3368 case kCondLE:
3369 if (gt_bias) {
3370 __ CmpLeD(FTMP, lhs, rhs);
3371 } else {
3372 __ CmpUleD(FTMP, lhs, rhs);
3373 }
3374 __ Mfc1(dst, FTMP);
3375 __ Andi(dst, dst, 1);
3376 break;
3377 case kCondGT:
3378 if (gt_bias) {
3379 __ CmpUltD(FTMP, rhs, lhs);
3380 } else {
3381 __ CmpLtD(FTMP, rhs, lhs);
3382 }
3383 __ Mfc1(dst, FTMP);
3384 __ Andi(dst, dst, 1);
3385 break;
3386 case kCondGE:
3387 if (gt_bias) {
3388 __ CmpUleD(FTMP, rhs, lhs);
3389 } else {
3390 __ CmpLeD(FTMP, rhs, lhs);
3391 }
3392 __ Mfc1(dst, FTMP);
3393 __ Andi(dst, dst, 1);
3394 break;
3395 default:
3396 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3397 UNREACHABLE();
3398 }
3399 } else {
3400 switch (cond) {
3401 case kCondEQ:
3402 __ CeqD(0, lhs, rhs);
3403 __ LoadConst32(dst, 1);
3404 __ Movf(dst, ZERO, 0);
3405 break;
3406 case kCondNE:
3407 __ CeqD(0, lhs, rhs);
3408 __ LoadConst32(dst, 1);
3409 __ Movt(dst, ZERO, 0);
3410 break;
3411 case kCondLT:
3412 if (gt_bias) {
3413 __ ColtD(0, lhs, rhs);
3414 } else {
3415 __ CultD(0, lhs, rhs);
3416 }
3417 __ LoadConst32(dst, 1);
3418 __ Movf(dst, ZERO, 0);
3419 break;
3420 case kCondLE:
3421 if (gt_bias) {
3422 __ ColeD(0, lhs, rhs);
3423 } else {
3424 __ CuleD(0, lhs, rhs);
3425 }
3426 __ LoadConst32(dst, 1);
3427 __ Movf(dst, ZERO, 0);
3428 break;
3429 case kCondGT:
3430 if (gt_bias) {
3431 __ CultD(0, rhs, lhs);
3432 } else {
3433 __ ColtD(0, rhs, lhs);
3434 }
3435 __ LoadConst32(dst, 1);
3436 __ Movf(dst, ZERO, 0);
3437 break;
3438 case kCondGE:
3439 if (gt_bias) {
3440 __ CuleD(0, rhs, lhs);
3441 } else {
3442 __ ColeD(0, rhs, lhs);
3443 }
3444 __ LoadConst32(dst, 1);
3445 __ Movf(dst, ZERO, 0);
3446 break;
3447 default:
3448 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3449 UNREACHABLE();
3450 }
3451 }
3452 }
3453}
3454
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003455void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3456 bool gt_bias,
3457 Primitive::Type type,
3458 LocationSummary* locations,
3459 MipsLabel* label) {
3460 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3461 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3462 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3463 if (type == Primitive::kPrimFloat) {
3464 if (isR6) {
3465 switch (cond) {
3466 case kCondEQ:
3467 __ CmpEqS(FTMP, lhs, rhs);
3468 __ Bc1nez(FTMP, label);
3469 break;
3470 case kCondNE:
3471 __ CmpEqS(FTMP, lhs, rhs);
3472 __ Bc1eqz(FTMP, label);
3473 break;
3474 case kCondLT:
3475 if (gt_bias) {
3476 __ CmpLtS(FTMP, lhs, rhs);
3477 } else {
3478 __ CmpUltS(FTMP, lhs, rhs);
3479 }
3480 __ Bc1nez(FTMP, label);
3481 break;
3482 case kCondLE:
3483 if (gt_bias) {
3484 __ CmpLeS(FTMP, lhs, rhs);
3485 } else {
3486 __ CmpUleS(FTMP, lhs, rhs);
3487 }
3488 __ Bc1nez(FTMP, label);
3489 break;
3490 case kCondGT:
3491 if (gt_bias) {
3492 __ CmpUltS(FTMP, rhs, lhs);
3493 } else {
3494 __ CmpLtS(FTMP, rhs, lhs);
3495 }
3496 __ Bc1nez(FTMP, label);
3497 break;
3498 case kCondGE:
3499 if (gt_bias) {
3500 __ CmpUleS(FTMP, rhs, lhs);
3501 } else {
3502 __ CmpLeS(FTMP, rhs, lhs);
3503 }
3504 __ Bc1nez(FTMP, label);
3505 break;
3506 default:
3507 LOG(FATAL) << "Unexpected non-floating-point condition";
3508 }
3509 } else {
3510 switch (cond) {
3511 case kCondEQ:
3512 __ CeqS(0, lhs, rhs);
3513 __ Bc1t(0, label);
3514 break;
3515 case kCondNE:
3516 __ CeqS(0, lhs, rhs);
3517 __ Bc1f(0, label);
3518 break;
3519 case kCondLT:
3520 if (gt_bias) {
3521 __ ColtS(0, lhs, rhs);
3522 } else {
3523 __ CultS(0, lhs, rhs);
3524 }
3525 __ Bc1t(0, label);
3526 break;
3527 case kCondLE:
3528 if (gt_bias) {
3529 __ ColeS(0, lhs, rhs);
3530 } else {
3531 __ CuleS(0, lhs, rhs);
3532 }
3533 __ Bc1t(0, label);
3534 break;
3535 case kCondGT:
3536 if (gt_bias) {
3537 __ CultS(0, rhs, lhs);
3538 } else {
3539 __ ColtS(0, rhs, lhs);
3540 }
3541 __ Bc1t(0, label);
3542 break;
3543 case kCondGE:
3544 if (gt_bias) {
3545 __ CuleS(0, rhs, lhs);
3546 } else {
3547 __ ColeS(0, rhs, lhs);
3548 }
3549 __ Bc1t(0, label);
3550 break;
3551 default:
3552 LOG(FATAL) << "Unexpected non-floating-point condition";
3553 }
3554 }
3555 } else {
3556 DCHECK_EQ(type, Primitive::kPrimDouble);
3557 if (isR6) {
3558 switch (cond) {
3559 case kCondEQ:
3560 __ CmpEqD(FTMP, lhs, rhs);
3561 __ Bc1nez(FTMP, label);
3562 break;
3563 case kCondNE:
3564 __ CmpEqD(FTMP, lhs, rhs);
3565 __ Bc1eqz(FTMP, label);
3566 break;
3567 case kCondLT:
3568 if (gt_bias) {
3569 __ CmpLtD(FTMP, lhs, rhs);
3570 } else {
3571 __ CmpUltD(FTMP, lhs, rhs);
3572 }
3573 __ Bc1nez(FTMP, label);
3574 break;
3575 case kCondLE:
3576 if (gt_bias) {
3577 __ CmpLeD(FTMP, lhs, rhs);
3578 } else {
3579 __ CmpUleD(FTMP, lhs, rhs);
3580 }
3581 __ Bc1nez(FTMP, label);
3582 break;
3583 case kCondGT:
3584 if (gt_bias) {
3585 __ CmpUltD(FTMP, rhs, lhs);
3586 } else {
3587 __ CmpLtD(FTMP, rhs, lhs);
3588 }
3589 __ Bc1nez(FTMP, label);
3590 break;
3591 case kCondGE:
3592 if (gt_bias) {
3593 __ CmpUleD(FTMP, rhs, lhs);
3594 } else {
3595 __ CmpLeD(FTMP, rhs, lhs);
3596 }
3597 __ Bc1nez(FTMP, label);
3598 break;
3599 default:
3600 LOG(FATAL) << "Unexpected non-floating-point condition";
3601 }
3602 } else {
3603 switch (cond) {
3604 case kCondEQ:
3605 __ CeqD(0, lhs, rhs);
3606 __ Bc1t(0, label);
3607 break;
3608 case kCondNE:
3609 __ CeqD(0, lhs, rhs);
3610 __ Bc1f(0, label);
3611 break;
3612 case kCondLT:
3613 if (gt_bias) {
3614 __ ColtD(0, lhs, rhs);
3615 } else {
3616 __ CultD(0, lhs, rhs);
3617 }
3618 __ Bc1t(0, label);
3619 break;
3620 case kCondLE:
3621 if (gt_bias) {
3622 __ ColeD(0, lhs, rhs);
3623 } else {
3624 __ CuleD(0, lhs, rhs);
3625 }
3626 __ Bc1t(0, label);
3627 break;
3628 case kCondGT:
3629 if (gt_bias) {
3630 __ CultD(0, rhs, lhs);
3631 } else {
3632 __ ColtD(0, rhs, lhs);
3633 }
3634 __ Bc1t(0, label);
3635 break;
3636 case kCondGE:
3637 if (gt_bias) {
3638 __ CuleD(0, rhs, lhs);
3639 } else {
3640 __ ColeD(0, rhs, lhs);
3641 }
3642 __ Bc1t(0, label);
3643 break;
3644 default:
3645 LOG(FATAL) << "Unexpected non-floating-point condition";
3646 }
3647 }
3648 }
3649}
3650
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003651void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003652 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00003654 MipsLabel* false_target) {
3655 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656
David Brazdil0debae72015-11-12 18:37:00 +00003657 if (true_target == nullptr && false_target == nullptr) {
3658 // Nothing to do. The code always falls through.
3659 return;
3660 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003661 // Constant condition, statically compared against "true" (integer value 1).
3662 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003663 if (true_target != nullptr) {
3664 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003665 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003667 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003668 if (false_target != nullptr) {
3669 __ B(false_target);
3670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671 }
David Brazdil0debae72015-11-12 18:37:00 +00003672 return;
3673 }
3674
3675 // The following code generates these patterns:
3676 // (1) true_target == nullptr && false_target != nullptr
3677 // - opposite condition true => branch to false_target
3678 // (2) true_target != nullptr && false_target == nullptr
3679 // - condition true => branch to true_target
3680 // (3) true_target != nullptr && false_target != nullptr
3681 // - condition true => branch to true_target
3682 // - branch to false_target
3683 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003685 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003686 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003687 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ Beqz(cond_val.AsRegister<Register>(), false_target);
3689 } else {
3690 __ Bnez(cond_val.AsRegister<Register>(), true_target);
3691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003692 } else {
3693 // The condition instruction has not been materialized, use its inputs as
3694 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003695 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003696 Primitive::Type type = condition->InputAt(0)->GetType();
3697 LocationSummary* locations = cond->GetLocations();
3698 IfCondition if_cond = condition->GetCondition();
3699 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00003700
David Brazdil0debae72015-11-12 18:37:00 +00003701 if (true_target == nullptr) {
3702 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003703 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00003704 }
3705
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003706 switch (type) {
3707 default:
3708 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
3709 break;
3710 case Primitive::kPrimLong:
3711 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
3712 break;
3713 case Primitive::kPrimFloat:
3714 case Primitive::kPrimDouble:
3715 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
3716 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003717 }
3718 }
David Brazdil0debae72015-11-12 18:37:00 +00003719
3720 // If neither branch falls through (case 3), the conditional branch to `true_target`
3721 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3722 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003723 __ B(false_target);
3724 }
3725}
3726
3727void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
3728 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003735 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3736 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3737 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3738 nullptr : codegen_->GetLabelOf(true_successor);
3739 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3740 nullptr : codegen_->GetLabelOf(false_successor);
3741 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003742}
3743
3744void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
3745 LocationSummary* locations = new (GetGraph()->GetArena())
3746 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003747 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003748 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003749 locations->SetInAt(0, Location::RequiresRegister());
3750 }
3751}
3752
3753void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003754 SlowPathCodeMIPS* slow_path =
3755 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003756 GenerateTestAndBranch(deoptimize,
3757 /* condition_input_index */ 0,
3758 slow_path->GetEntryLabel(),
3759 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760}
3761
David Brazdil74eb1b22015-12-14 11:44:01 +00003762void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
3763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
3764 if (Primitive::IsFloatingPointType(select->GetType())) {
3765 locations->SetInAt(0, Location::RequiresFpuRegister());
3766 locations->SetInAt(1, Location::RequiresFpuRegister());
3767 } else {
3768 locations->SetInAt(0, Location::RequiresRegister());
3769 locations->SetInAt(1, Location::RequiresRegister());
3770 }
3771 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3772 locations->SetInAt(2, Location::RequiresRegister());
3773 }
3774 locations->SetOut(Location::SameAsFirstInput());
3775}
3776
3777void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
3778 LocationSummary* locations = select->GetLocations();
3779 MipsLabel false_target;
3780 GenerateTestAndBranch(select,
3781 /* condition_input_index */ 2,
3782 /* true_target */ nullptr,
3783 &false_target);
3784 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
3785 __ Bind(&false_target);
3786}
3787
David Srbecky0cf44932015-12-09 14:09:59 +00003788void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3789 new (GetGraph()->GetArena()) LocationSummary(info);
3790}
3791
David Srbeckyd28f4a02016-03-14 17:14:24 +00003792void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
3793 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003794}
3795
3796void CodeGeneratorMIPS::GenerateNop() {
3797 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003798}
3799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3801 Primitive::Type field_type = field_info.GetFieldType();
3802 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3803 bool generate_volatile = field_info.IsVolatile() && is_wide;
3804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003805 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003806
3807 locations->SetInAt(0, Location::RequiresRegister());
3808 if (generate_volatile) {
3809 InvokeRuntimeCallingConvention calling_convention;
3810 // need A0 to hold base + offset
3811 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3812 if (field_type == Primitive::kPrimLong) {
3813 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
3814 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003815 // Use Location::Any() to prevent situations when running out of available fp registers.
3816 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 // Need some temp core regs since FP results are returned in core registers
3818 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
3819 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
3820 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
3821 }
3822 } else {
3823 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3824 locations->SetOut(Location::RequiresFpuRegister());
3825 } else {
3826 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3827 }
3828 }
3829}
3830
3831void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
3832 const FieldInfo& field_info,
3833 uint32_t dex_pc) {
3834 Primitive::Type type = field_info.GetFieldType();
3835 LocationSummary* locations = instruction->GetLocations();
3836 Register obj = locations->InAt(0).AsRegister<Register>();
3837 LoadOperandType load_type = kLoadUnsignedByte;
3838 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003839 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003840 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003841
3842 switch (type) {
3843 case Primitive::kPrimBoolean:
3844 load_type = kLoadUnsignedByte;
3845 break;
3846 case Primitive::kPrimByte:
3847 load_type = kLoadSignedByte;
3848 break;
3849 case Primitive::kPrimShort:
3850 load_type = kLoadSignedHalfword;
3851 break;
3852 case Primitive::kPrimChar:
3853 load_type = kLoadUnsignedHalfword;
3854 break;
3855 case Primitive::kPrimInt:
3856 case Primitive::kPrimFloat:
3857 case Primitive::kPrimNot:
3858 load_type = kLoadWord;
3859 break;
3860 case Primitive::kPrimLong:
3861 case Primitive::kPrimDouble:
3862 load_type = kLoadDoubleword;
3863 break;
3864 case Primitive::kPrimVoid:
3865 LOG(FATAL) << "Unreachable type " << type;
3866 UNREACHABLE();
3867 }
3868
3869 if (is_volatile && load_type == kLoadDoubleword) {
3870 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003871 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003872 // Do implicit Null check
3873 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3874 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01003875 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
3877 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003878 // FP results are returned in core registers. Need to move them.
3879 Location out = locations->Out();
3880 if (out.IsFpuRegister()) {
3881 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
3882 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
3883 out.AsFpuRegister<FRegister>());
3884 } else {
3885 DCHECK(out.IsDoubleStackSlot());
3886 __ StoreToOffset(kStoreWord,
3887 locations->GetTemp(1).AsRegister<Register>(),
3888 SP,
3889 out.GetStackIndex());
3890 __ StoreToOffset(kStoreWord,
3891 locations->GetTemp(2).AsRegister<Register>(),
3892 SP,
3893 out.GetStackIndex() + 4);
3894 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003895 }
3896 } else {
3897 if (!Primitive::IsFloatingPointType(type)) {
3898 Register dst;
3899 if (type == Primitive::kPrimLong) {
3900 DCHECK(locations->Out().IsRegisterPair());
3901 dst = locations->Out().AsRegisterPairLow<Register>();
3902 } else {
3903 DCHECK(locations->Out().IsRegister());
3904 dst = locations->Out().AsRegister<Register>();
3905 }
Alexey Frunze2923db72016-08-20 01:55:47 -07003906 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003907 } else {
3908 DCHECK(locations->Out().IsFpuRegister());
3909 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3910 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07003911 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003912 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07003913 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003914 }
3915 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916 }
3917
3918 if (is_volatile) {
3919 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3920 }
3921}
3922
3923void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3924 Primitive::Type field_type = field_info.GetFieldType();
3925 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
3926 bool generate_volatile = field_info.IsVolatile() && is_wide;
3927 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003928 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003929
3930 locations->SetInAt(0, Location::RequiresRegister());
3931 if (generate_volatile) {
3932 InvokeRuntimeCallingConvention calling_convention;
3933 // need A0 to hold base + offset
3934 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3935 if (field_type == Primitive::kPrimLong) {
3936 locations->SetInAt(1, Location::RegisterPairLocation(
3937 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3938 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02003939 // Use Location::Any() to prevent situations when running out of available fp registers.
3940 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003941 // Pass FP parameters in core registers.
3942 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3943 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
3944 }
3945 } else {
3946 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003947 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003948 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003949 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003950 }
3951 }
3952}
3953
3954void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
3955 const FieldInfo& field_info,
3956 uint32_t dex_pc) {
3957 Primitive::Type type = field_info.GetFieldType();
3958 LocationSummary* locations = instruction->GetLocations();
3959 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07003960 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003961 StoreOperandType store_type = kStoreByte;
3962 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003963 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07003964 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003965
3966 switch (type) {
3967 case Primitive::kPrimBoolean:
3968 case Primitive::kPrimByte:
3969 store_type = kStoreByte;
3970 break;
3971 case Primitive::kPrimShort:
3972 case Primitive::kPrimChar:
3973 store_type = kStoreHalfword;
3974 break;
3975 case Primitive::kPrimInt:
3976 case Primitive::kPrimFloat:
3977 case Primitive::kPrimNot:
3978 store_type = kStoreWord;
3979 break;
3980 case Primitive::kPrimLong:
3981 case Primitive::kPrimDouble:
3982 store_type = kStoreDoubleword;
3983 break;
3984 case Primitive::kPrimVoid:
3985 LOG(FATAL) << "Unreachable type " << type;
3986 UNREACHABLE();
3987 }
3988
3989 if (is_volatile) {
3990 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3991 }
3992
3993 if (is_volatile && store_type == kStoreDoubleword) {
3994 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01003995 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003996 // Do implicit Null check.
3997 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
3998 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3999 if (type == Primitive::kPrimDouble) {
4000 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004001 if (value_location.IsFpuRegister()) {
4002 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4003 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004004 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004005 value_location.AsFpuRegister<FRegister>());
4006 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004007 __ LoadFromOffset(kLoadWord,
4008 locations->GetTemp(1).AsRegister<Register>(),
4009 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004010 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004011 __ LoadFromOffset(kLoadWord,
4012 locations->GetTemp(2).AsRegister<Register>(),
4013 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004014 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004015 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004016 DCHECK(value_location.IsConstant());
4017 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4018 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004019 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4020 locations->GetTemp(1).AsRegister<Register>(),
4021 value);
4022 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004024 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004025 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4026 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004027 if (value_location.IsConstant()) {
4028 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4029 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4030 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004031 Register src;
4032 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004033 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004034 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004035 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004036 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004037 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004038 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004039 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004040 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004041 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004043 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004044 }
4045 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004046 }
4047
4048 // TODO: memory barriers?
4049 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004050 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004051 codegen_->MarkGCCard(obj, src);
4052 }
4053
4054 if (is_volatile) {
4055 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4056 }
4057}
4058
4059void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4060 HandleFieldGet(instruction, instruction->GetFieldInfo());
4061}
4062
4063void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4064 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4065}
4066
4067void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4068 HandleFieldSet(instruction, instruction->GetFieldInfo());
4069}
4070
4071void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4072 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4073}
4074
Alexey Frunze06a46c42016-07-19 15:00:40 -07004075void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4076 HInstruction* instruction ATTRIBUTE_UNUSED,
4077 Location root,
4078 Register obj,
4079 uint32_t offset) {
4080 Register root_reg = root.AsRegister<Register>();
4081 if (kEmitCompilerReadBarrier) {
4082 UNIMPLEMENTED(FATAL) << "for read barrier";
4083 } else {
4084 // Plain GC root load with no read barrier.
4085 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
4086 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
4087 // Note that GC roots are not affected by heap poisoning, thus we
4088 // do not have to unpoison `root_reg` here.
4089 }
4090}
4091
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004092void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4093 LocationSummary::CallKind call_kind =
4094 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
4095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4096 locations->SetInAt(0, Location::RequiresRegister());
4097 locations->SetInAt(1, Location::RequiresRegister());
4098 // The output does overlap inputs.
4099 // Note that TypeCheckSlowPathMIPS uses this register too.
4100 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4101}
4102
4103void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
4104 LocationSummary* locations = instruction->GetLocations();
4105 Register obj = locations->InAt(0).AsRegister<Register>();
4106 Register cls = locations->InAt(1).AsRegister<Register>();
4107 Register out = locations->Out().AsRegister<Register>();
4108
4109 MipsLabel done;
4110
4111 // Return 0 if `obj` is null.
4112 // TODO: Avoid this check if we know `obj` is not null.
4113 __ Move(out, ZERO);
4114 __ Beqz(obj, &done);
4115
4116 // Compare the class of `obj` with `cls`.
4117 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
4118 if (instruction->IsExactCheck()) {
4119 // Classes must be equal for the instanceof to succeed.
4120 __ Xor(out, out, cls);
4121 __ Sltiu(out, out, 1);
4122 } else {
4123 // If the classes are not equal, we go into a slow path.
4124 DCHECK(locations->OnlyCallsOnSlowPath());
4125 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
4126 codegen_->AddSlowPath(slow_path);
4127 __ Bne(out, cls, slow_path->GetEntryLabel());
4128 __ LoadConst32(out, 1);
4129 __ Bind(slow_path->GetExitLabel());
4130 }
4131
4132 __ Bind(&done);
4133}
4134
4135void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
4136 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4137 locations->SetOut(Location::ConstantLocation(constant));
4138}
4139
4140void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
4141 // Will be generated at use site.
4142}
4143
4144void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
4145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4146 locations->SetOut(Location::ConstantLocation(constant));
4147}
4148
4149void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
4150 // Will be generated at use site.
4151}
4152
4153void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
4154 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
4155 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
4156}
4157
4158void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4159 HandleInvoke(invoke);
4160 // The register T0 is required to be used for the hidden argument in
4161 // art_quick_imt_conflict_trampoline, so add the hidden argument.
4162 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
4163}
4164
4165void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
4166 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
4167 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004168 Location receiver = invoke->GetLocations()->InAt(0);
4169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004170 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171
4172 // Set the hidden argument.
4173 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
4174 invoke->GetDexMethodIndex());
4175
4176 // temp = object->GetClass();
4177 if (receiver.IsStackSlot()) {
4178 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
4179 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
4180 } else {
4181 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4182 }
4183 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004184 __ LoadFromOffset(kLoadWord, temp, temp,
4185 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
4186 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004187 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004188 // temp = temp->GetImtEntryAt(method_offset);
4189 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4190 // T9 = temp->GetEntryPoint();
4191 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4192 // T9();
4193 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004194 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004195 DCHECK(!codegen_->IsLeafMethod());
4196 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4197}
4198
4199void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07004200 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4201 if (intrinsic.TryDispatch(invoke)) {
4202 return;
4203 }
4204
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004205 HandleInvoke(invoke);
4206}
4207
4208void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004209 // Explicit clinit checks triggered by static invokes must have been pruned by
4210 // art::PrepareForRegisterAllocation.
4211 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004212
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004213 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4214 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4215 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4216
4217 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
4218 // R6 has PC-relative addressing.
4219 bool has_extra_input = !isR6 &&
4220 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4221 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
4222
4223 if (invoke->HasPcRelativeDexCache()) {
4224 // kDexCachePcRelative is mutually exclusive with
4225 // kDirectAddressWithFixup/kCallDirectWithFixup.
4226 CHECK(!has_extra_input);
4227 has_extra_input = true;
4228 }
4229
Chris Larsen701566a2015-10-27 15:29:13 -07004230 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
4231 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004232 if (invoke->GetLocations()->CanCall() && has_extra_input) {
4233 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
4234 }
Chris Larsen701566a2015-10-27 15:29:13 -07004235 return;
4236 }
4237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004239
4240 // Add the extra input register if either the dex cache array base register
4241 // or the PC-relative base register for accessing literals is needed.
4242 if (has_extra_input) {
4243 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
4244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004245}
4246
Chris Larsen701566a2015-10-27 15:29:13 -07004247static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004248 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07004249 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
4250 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004251 return true;
4252 }
4253 return false;
4254}
4255
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004256HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07004257 HLoadString::LoadKind desired_string_load_kind) {
4258 if (kEmitCompilerReadBarrier) {
4259 UNIMPLEMENTED(FATAL) << "for read barrier";
4260 }
4261 // We disable PC-relative load when there is an irreducible loop, as the optimization
4262 // is incompatible with it.
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004263 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4264 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07004265 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4266 bool fallback_load = has_irreducible_loops;
4267 switch (desired_string_load_kind) {
4268 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4269 DCHECK(!GetCompilerOptions().GetCompilePic());
4270 break;
4271 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4272 DCHECK(GetCompilerOptions().GetCompilePic());
4273 break;
4274 case HLoadString::LoadKind::kBootImageAddress:
4275 break;
4276 case HLoadString::LoadKind::kDexCacheAddress:
4277 DCHECK(Runtime::Current()->UseJitCompilation());
4278 fallback_load = false;
4279 break;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004280 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004281 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004282 break;
4283 case HLoadString::LoadKind::kDexCacheViaMethod:
4284 fallback_load = false;
4285 break;
4286 }
4287 if (fallback_load) {
4288 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
4289 }
4290 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004291}
4292
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004293HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
4294 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004295 if (kEmitCompilerReadBarrier) {
4296 UNIMPLEMENTED(FATAL) << "for read barrier";
4297 }
4298 // We disable pc-relative load when there is an irreducible loop, as the optimization
4299 // is incompatible with it.
4300 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4301 bool fallback_load = has_irreducible_loops;
4302 switch (desired_class_load_kind) {
4303 case HLoadClass::LoadKind::kReferrersClass:
4304 fallback_load = false;
4305 break;
4306 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4307 DCHECK(!GetCompilerOptions().GetCompilePic());
4308 break;
4309 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4310 DCHECK(GetCompilerOptions().GetCompilePic());
4311 break;
4312 case HLoadClass::LoadKind::kBootImageAddress:
4313 break;
4314 case HLoadClass::LoadKind::kDexCacheAddress:
4315 DCHECK(Runtime::Current()->UseJitCompilation());
4316 fallback_load = false;
4317 break;
4318 case HLoadClass::LoadKind::kDexCachePcRelative:
4319 DCHECK(!Runtime::Current()->UseJitCompilation());
4320 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
4321 // with irreducible loops.
4322 break;
4323 case HLoadClass::LoadKind::kDexCacheViaMethod:
4324 fallback_load = false;
4325 break;
4326 }
4327 if (fallback_load) {
4328 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
4329 }
4330 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004331}
4332
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004333Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4334 Register temp) {
4335 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4336 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
4337 if (!invoke->GetLocations()->Intrinsified()) {
4338 return location.AsRegister<Register>();
4339 }
4340 // For intrinsics we allow any location, so it may be on the stack.
4341 if (!location.IsRegister()) {
4342 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
4343 return temp;
4344 }
4345 // For register locations, check if the register was saved. If so, get it from the stack.
4346 // Note: There is a chance that the register was saved but not overwritten, so we could
4347 // save one load. However, since this is just an intrinsic slow path we prefer this
4348 // simple and more robust approach rather that trying to determine if that's the case.
4349 SlowPathCode* slow_path = GetCurrentSlowPath();
4350 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4351 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4352 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4353 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
4354 return temp;
4355 }
4356 return location.AsRegister<Register>();
4357}
4358
Vladimir Markodc151b22015-10-15 18:02:30 +01004359HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
4360 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004361 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004362 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4363 // We disable PC-relative load when there is an irreducible loop, as the optimization
4364 // is incompatible with it.
4365 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
4366 bool fallback_load = true;
4367 bool fallback_call = true;
4368 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004369 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4370 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004371 fallback_load = has_irreducible_loops;
4372 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004373 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004374 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01004375 break;
4376 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004377 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004378 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004379 fallback_call = has_irreducible_loops;
4380 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004381 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004382 // TODO: Implement this type.
4383 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004384 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004385 fallback_call = false;
4386 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004387 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004388 if (fallback_load) {
4389 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4390 dispatch_info.method_load_data = 0;
4391 }
4392 if (fallback_call) {
4393 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
4394 dispatch_info.direct_code_ptr = 0;
4395 }
4396 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004397}
4398
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004399void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4400 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004401 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004402 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
4403 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
4404 bool isR6 = isa_features_.IsR6();
4405 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
4406 // R6 has PC-relative addressing.
4407 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
4408 (!isR6 &&
4409 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
4410 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
4411 Register base_reg = has_extra_input
4412 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
4413 : ZERO;
4414
4415 // For better instruction scheduling we load the direct code pointer before the method pointer.
4416 switch (code_ptr_location) {
4417 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4418 // T9 = invoke->GetDirectCodePtr();
4419 __ LoadConst32(T9, invoke->GetDirectCodePtr());
4420 break;
4421 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4422 // T9 = code address from literal pool with link-time patch.
4423 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
4424 break;
4425 default:
4426 break;
4427 }
4428
4429 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004430 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004431 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004432 uint32_t offset =
4433 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004434 __ LoadFromOffset(kLoadWord,
4435 temp.AsRegister<Register>(),
4436 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004437 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004438 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004439 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004440 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004441 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004442 break;
4443 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4444 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
4445 break;
4446 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004447 __ LoadLiteral(temp.AsRegister<Register>(),
4448 base_reg,
4449 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
4450 break;
4451 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4452 HMipsDexCacheArraysBase* base =
4453 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
4454 int32_t offset =
4455 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4456 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
4457 break;
4458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004459 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004460 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004461 Register reg = temp.AsRegister<Register>();
4462 Register method_reg;
4463 if (current_method.IsRegister()) {
4464 method_reg = current_method.AsRegister<Register>();
4465 } else {
4466 // TODO: use the appropriate DCHECK() here if possible.
4467 // DCHECK(invoke->GetLocations()->Intrinsified());
4468 DCHECK(!current_method.IsValid());
4469 method_reg = reg;
4470 __ Lw(reg, SP, kCurrentMethodStackOffset);
4471 }
4472
4473 // temp = temp->dex_cache_resolved_methods_;
4474 __ LoadFromOffset(kLoadWord,
4475 reg,
4476 method_reg,
4477 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01004478 // temp = temp[index_in_cache];
4479 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4480 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004481 __ LoadFromOffset(kLoadWord,
4482 reg,
4483 reg,
4484 CodeGenerator::GetCachePointerOffset(index_in_cache));
4485 break;
4486 }
4487 }
4488
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004489 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004490 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004491 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004492 break;
4493 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004494 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4495 // T9 prepared above for better instruction scheduling.
4496 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004497 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004498 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004499 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01004500 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07004501 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01004502 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4503 LOG(FATAL) << "Unsupported";
4504 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004505 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4506 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01004507 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004508 T9,
4509 callee_method.AsRegister<Register>(),
4510 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004511 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004512 // T9()
4513 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004514 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004515 break;
4516 }
4517 DCHECK(!IsLeafMethod());
4518}
4519
4520void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004521 // Explicit clinit checks triggered by static invokes must have been pruned by
4522 // art::PrepareForRegisterAllocation.
4523 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004524
4525 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4526 return;
4527 }
4528
4529 LocationSummary* locations = invoke->GetLocations();
4530 codegen_->GenerateStaticOrDirectCall(invoke,
4531 locations->HasTemps()
4532 ? locations->GetTemp(0)
4533 : Location::NoLocation());
4534 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4535}
4536
Chris Larsen3acee732015-11-18 13:31:08 -08004537void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004538 LocationSummary* locations = invoke->GetLocations();
4539 Location receiver = locations->InAt(0);
Chris Larsen3acee732015-11-18 13:31:08 -08004540 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004541 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4542 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
4543 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07004544 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004545
4546 // temp = object->GetClass();
Chris Larsen3acee732015-11-18 13:31:08 -08004547 DCHECK(receiver.IsRegister());
4548 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
4549 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004550 // temp = temp->GetMethodAt(method_offset);
4551 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
4552 // T9 = temp->GetEntryPoint();
4553 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
4554 // T9();
4555 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07004556 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08004557}
4558
4559void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
4560 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4561 return;
4562 }
4563
4564 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004565 DCHECK(!codegen_->IsLeafMethod());
4566 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4567}
4568
4569void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004570 if (cls->NeedsAccessCheck()) {
4571 InvokeRuntimeCallingConvention calling_convention;
4572 CodeGenerator::CreateLoadClassLocationSummary(
4573 cls,
4574 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
4575 Location::RegisterLocation(V0),
4576 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
4577 return;
4578 }
4579
4580 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
4581 ? LocationSummary::kCallOnSlowPath
4582 : LocationSummary::kNoCall;
4583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4584 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4585 switch (load_kind) {
4586 // We need an extra register for PC-relative literals on R2.
4587 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4588 case HLoadClass::LoadKind::kBootImageAddress:
4589 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4590 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4591 break;
4592 }
4593 FALLTHROUGH_INTENDED;
4594 // We need an extra register for PC-relative dex cache accesses.
4595 case HLoadClass::LoadKind::kDexCachePcRelative:
4596 case HLoadClass::LoadKind::kReferrersClass:
4597 case HLoadClass::LoadKind::kDexCacheViaMethod:
4598 locations->SetInAt(0, Location::RequiresRegister());
4599 break;
4600 default:
4601 break;
4602 }
4603 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004604}
4605
4606void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
4607 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01004608 if (cls->NeedsAccessCheck()) {
4609 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01004610 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004611 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01004612 return;
4613 }
4614
Alexey Frunze06a46c42016-07-19 15:00:40 -07004615 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4616 Location out_loc = locations->Out();
4617 Register out = out_loc.AsRegister<Register>();
4618 Register base_or_current_method_reg;
4619 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4620 switch (load_kind) {
4621 // We need an extra register for PC-relative literals on R2.
4622 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4623 case HLoadClass::LoadKind::kBootImageAddress:
4624 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4625 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4626 break;
4627 // We need an extra register for PC-relative dex cache accesses.
4628 case HLoadClass::LoadKind::kDexCachePcRelative:
4629 case HLoadClass::LoadKind::kReferrersClass:
4630 case HLoadClass::LoadKind::kDexCacheViaMethod:
4631 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
4632 break;
4633 default:
4634 base_or_current_method_reg = ZERO;
4635 break;
4636 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004637
Alexey Frunze06a46c42016-07-19 15:00:40 -07004638 bool generate_null_check = false;
4639 switch (load_kind) {
4640 case HLoadClass::LoadKind::kReferrersClass: {
4641 DCHECK(!cls->CanCallRuntime());
4642 DCHECK(!cls->MustGenerateClinitCheck());
4643 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4644 GenerateGcRootFieldLoad(cls,
4645 out_loc,
4646 base_or_current_method_reg,
4647 ArtMethod::DeclaringClassOffset().Int32Value());
4648 break;
4649 }
4650 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4651 DCHECK(!kEmitCompilerReadBarrier);
4652 __ LoadLiteral(out,
4653 base_or_current_method_reg,
4654 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4655 cls->GetTypeIndex()));
4656 break;
4657 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4658 DCHECK(!kEmitCompilerReadBarrier);
4659 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4660 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004661 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004662 break;
4663 }
4664 case HLoadClass::LoadKind::kBootImageAddress: {
4665 DCHECK(!kEmitCompilerReadBarrier);
4666 DCHECK_NE(cls->GetAddress(), 0u);
4667 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4668 __ LoadLiteral(out,
4669 base_or_current_method_reg,
4670 codegen_->DeduplicateBootImageAddressLiteral(address));
4671 break;
4672 }
4673 case HLoadClass::LoadKind::kDexCacheAddress: {
4674 DCHECK_NE(cls->GetAddress(), 0u);
4675 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
4676 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4677 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4678 int16_t offset = Low16Bits(address);
4679 uint32_t base_address = address - offset; // This accounts for offset sign extension.
4680 __ Lui(out, High16Bits(base_address));
4681 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4682 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4683 generate_null_check = !cls->IsInDexCache();
4684 break;
4685 }
4686 case HLoadClass::LoadKind::kDexCachePcRelative: {
4687 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
4688 int32_t offset =
4689 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
4690 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
4691 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
4692 generate_null_check = !cls->IsInDexCache();
4693 break;
4694 }
4695 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4696 // /* GcRoot<mirror::Class>[] */ out =
4697 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4698 __ LoadFromOffset(kLoadWord,
4699 out,
4700 base_or_current_method_reg,
4701 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
4702 // /* GcRoot<mirror::Class> */ out = out[type_index]
4703 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
4704 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
4705 generate_null_check = !cls->IsInDexCache();
4706 }
4707 }
4708
4709 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4710 DCHECK(cls->CanCallRuntime());
4711 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
4712 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4713 codegen_->AddSlowPath(slow_path);
4714 if (generate_null_check) {
4715 __ Beqz(out, slow_path->GetEntryLabel());
4716 }
4717 if (cls->MustGenerateClinitCheck()) {
4718 GenerateClassInitializationCheck(slow_path, out);
4719 } else {
4720 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721 }
4722 }
4723}
4724
4725static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004726 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004727}
4728
4729void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
4730 LocationSummary* locations =
4731 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4732 locations->SetOut(Location::RequiresRegister());
4733}
4734
4735void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
4736 Register out = load->GetLocations()->Out().AsRegister<Register>();
4737 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
4738}
4739
4740void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
4741 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4742}
4743
4744void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4745 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
4746}
4747
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004748void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004749 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004750 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
4751 ? LocationSummary::kCallOnMainOnly
4752 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004753 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004754 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004755 HLoadString::LoadKind load_kind = load->GetLoadKind();
4756 switch (load_kind) {
4757 // We need an extra register for PC-relative literals on R2.
4758 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4759 case HLoadString::LoadKind::kBootImageAddress:
4760 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4761 if (codegen_->GetInstructionSetFeatures().IsR6()) {
4762 break;
4763 }
4764 FALLTHROUGH_INTENDED;
4765 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004766 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004767 case HLoadString::LoadKind::kDexCacheViaMethod:
4768 locations->SetInAt(0, Location::RequiresRegister());
4769 break;
4770 default:
4771 break;
4772 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004773 locations->SetOut(Location::RequiresRegister());
4774}
4775
4776void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07004777 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004778 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07004779 Location out_loc = locations->Out();
4780 Register out = out_loc.AsRegister<Register>();
4781 Register base_or_current_method_reg;
4782 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4783 switch (load_kind) {
4784 // We need an extra register for PC-relative literals on R2.
4785 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4786 case HLoadString::LoadKind::kBootImageAddress:
4787 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004788 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07004789 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
4790 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004791 default:
4792 base_or_current_method_reg = ZERO;
4793 break;
4794 }
4795
4796 switch (load_kind) {
4797 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4798 DCHECK(!kEmitCompilerReadBarrier);
4799 __ LoadLiteral(out,
4800 base_or_current_method_reg,
4801 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4802 load->GetStringIndex()));
4803 return; // No dex cache slow path.
4804 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4805 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004806 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07004807 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4808 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004809 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07004810 return; // No dex cache slow path.
4811 }
4812 case HLoadString::LoadKind::kBootImageAddress: {
4813 DCHECK(!kEmitCompilerReadBarrier);
4814 DCHECK_NE(load->GetAddress(), 0u);
4815 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
4816 __ LoadLiteral(out,
4817 base_or_current_method_reg,
4818 codegen_->DeduplicateBootImageAddressLiteral(address));
4819 return; // No dex cache slow path.
4820 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01004821 case HLoadString::LoadKind::kBssEntry: {
4822 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
4823 CodeGeneratorMIPS::PcRelativePatchInfo* info =
4824 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
4825 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
4826 __ LoadFromOffset(kLoadWord, out, out, 0);
4827 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4828 codegen_->AddSlowPath(slow_path);
4829 __ Beqz(out, slow_path->GetEntryLabel());
4830 __ Bind(slow_path->GetExitLabel());
4831 return;
4832 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07004833 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004834 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07004835 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004836
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07004837 // TODO: Re-add the compiler code to do string dex cache lookup again.
4838 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
4839 codegen_->AddSlowPath(slow_path);
4840 __ B(slow_path->GetEntryLabel());
4841 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842}
4843
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004844void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
4845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4846 locations->SetOut(Location::ConstantLocation(constant));
4847}
4848
4849void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
4850 // Will be generated at use site.
4851}
4852
4853void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4854 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004856 InvokeRuntimeCallingConvention calling_convention;
4857 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4858}
4859
4860void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
4861 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01004862 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004863 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4864 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01004865 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004866 }
4867 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4868}
4869
4870void LocationsBuilderMIPS::VisitMul(HMul* mul) {
4871 LocationSummary* locations =
4872 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4873 switch (mul->GetResultType()) {
4874 case Primitive::kPrimInt:
4875 case Primitive::kPrimLong:
4876 locations->SetInAt(0, Location::RequiresRegister());
4877 locations->SetInAt(1, Location::RequiresRegister());
4878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4879 break;
4880
4881 case Primitive::kPrimFloat:
4882 case Primitive::kPrimDouble:
4883 locations->SetInAt(0, Location::RequiresFpuRegister());
4884 locations->SetInAt(1, Location::RequiresFpuRegister());
4885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4886 break;
4887
4888 default:
4889 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4890 }
4891}
4892
4893void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
4894 Primitive::Type type = instruction->GetType();
4895 LocationSummary* locations = instruction->GetLocations();
4896 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4897
4898 switch (type) {
4899 case Primitive::kPrimInt: {
4900 Register dst = locations->Out().AsRegister<Register>();
4901 Register lhs = locations->InAt(0).AsRegister<Register>();
4902 Register rhs = locations->InAt(1).AsRegister<Register>();
4903
4904 if (isR6) {
4905 __ MulR6(dst, lhs, rhs);
4906 } else {
4907 __ MulR2(dst, lhs, rhs);
4908 }
4909 break;
4910 }
4911 case Primitive::kPrimLong: {
4912 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4913 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
4914 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4915 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4916 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
4917 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
4918
4919 // Extra checks to protect caused by the existance of A1_A2.
4920 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
4921 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
4922 DCHECK_NE(dst_high, lhs_low);
4923 DCHECK_NE(dst_high, rhs_low);
4924
4925 // A_B * C_D
4926 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
4927 // dst_lo: [ low(B*D) ]
4928 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
4929
4930 if (isR6) {
4931 __ MulR6(TMP, lhs_high, rhs_low);
4932 __ MulR6(dst_high, lhs_low, rhs_high);
4933 __ Addu(dst_high, dst_high, TMP);
4934 __ MuhuR6(TMP, lhs_low, rhs_low);
4935 __ Addu(dst_high, dst_high, TMP);
4936 __ MulR6(dst_low, lhs_low, rhs_low);
4937 } else {
4938 __ MulR2(TMP, lhs_high, rhs_low);
4939 __ MulR2(dst_high, lhs_low, rhs_high);
4940 __ Addu(dst_high, dst_high, TMP);
4941 __ MultuR2(lhs_low, rhs_low);
4942 __ Mfhi(TMP);
4943 __ Addu(dst_high, dst_high, TMP);
4944 __ Mflo(dst_low);
4945 }
4946 break;
4947 }
4948 case Primitive::kPrimFloat:
4949 case Primitive::kPrimDouble: {
4950 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4951 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4952 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4953 if (type == Primitive::kPrimFloat) {
4954 __ MulS(dst, lhs, rhs);
4955 } else {
4956 __ MulD(dst, lhs, rhs);
4957 }
4958 break;
4959 }
4960 default:
4961 LOG(FATAL) << "Unexpected mul type " << type;
4962 }
4963}
4964
4965void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
4966 LocationSummary* locations =
4967 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4968 switch (neg->GetResultType()) {
4969 case Primitive::kPrimInt:
4970 case Primitive::kPrimLong:
4971 locations->SetInAt(0, Location::RequiresRegister());
4972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4973 break;
4974
4975 case Primitive::kPrimFloat:
4976 case Primitive::kPrimDouble:
4977 locations->SetInAt(0, Location::RequiresFpuRegister());
4978 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4979 break;
4980
4981 default:
4982 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4983 }
4984}
4985
4986void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
4987 Primitive::Type type = instruction->GetType();
4988 LocationSummary* locations = instruction->GetLocations();
4989
4990 switch (type) {
4991 case Primitive::kPrimInt: {
4992 Register dst = locations->Out().AsRegister<Register>();
4993 Register src = locations->InAt(0).AsRegister<Register>();
4994 __ Subu(dst, ZERO, src);
4995 break;
4996 }
4997 case Primitive::kPrimLong: {
4998 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
4999 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5000 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5001 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5002 __ Subu(dst_low, ZERO, src_low);
5003 __ Sltu(TMP, ZERO, dst_low);
5004 __ Subu(dst_high, ZERO, src_high);
5005 __ Subu(dst_high, dst_high, TMP);
5006 break;
5007 }
5008 case Primitive::kPrimFloat:
5009 case Primitive::kPrimDouble: {
5010 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5011 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5012 if (type == Primitive::kPrimFloat) {
5013 __ NegS(dst, src);
5014 } else {
5015 __ NegD(dst, src);
5016 }
5017 break;
5018 }
5019 default:
5020 LOG(FATAL) << "Unexpected neg type " << type;
5021 }
5022}
5023
5024void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5025 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005027 InvokeRuntimeCallingConvention calling_convention;
5028 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5029 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5030 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5031 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5032}
5033
5034void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5035 InvokeRuntimeCallingConvention calling_convention;
5036 Register current_method_register = calling_convention.GetRegisterAt(2);
5037 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5038 // Move an uint16_t value to a register.
5039 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005040 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005041 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5042 void*, uint32_t, int32_t, ArtMethod*>();
5043}
5044
5045void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5046 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005047 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005048 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005049 if (instruction->IsStringAlloc()) {
5050 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5051 } else {
5052 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5053 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5054 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005055 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5056}
5057
5058void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005059 if (instruction->IsStringAlloc()) {
5060 // String is allocated through StringFactory. Call NewEmptyString entry point.
5061 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005062 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005063 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5064 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5065 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005066 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005067 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5068 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005069 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005070 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5071 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005072}
5073
5074void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5076 locations->SetInAt(0, Location::RequiresRegister());
5077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5078}
5079
5080void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5081 Primitive::Type type = instruction->GetType();
5082 LocationSummary* locations = instruction->GetLocations();
5083
5084 switch (type) {
5085 case Primitive::kPrimInt: {
5086 Register dst = locations->Out().AsRegister<Register>();
5087 Register src = locations->InAt(0).AsRegister<Register>();
5088 __ Nor(dst, src, ZERO);
5089 break;
5090 }
5091
5092 case Primitive::kPrimLong: {
5093 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5094 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5095 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5096 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5097 __ Nor(dst_high, src_high, ZERO);
5098 __ Nor(dst_low, src_low, ZERO);
5099 break;
5100 }
5101
5102 default:
5103 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5104 }
5105}
5106
5107void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5109 locations->SetInAt(0, Location::RequiresRegister());
5110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5111}
5112
5113void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5114 LocationSummary* locations = instruction->GetLocations();
5115 __ Xori(locations->Out().AsRegister<Register>(),
5116 locations->InAt(0).AsRegister<Register>(),
5117 1);
5118}
5119
5120void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005121 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5122 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005123}
5124
Calin Juravle2ae48182016-03-16 14:05:09 +00005125void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5126 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005127 return;
5128 }
5129 Location obj = instruction->GetLocations()->InAt(0);
5130
5131 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005132 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005133}
5134
Calin Juravle2ae48182016-03-16 14:05:09 +00005135void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005136 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005137 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138
5139 Location obj = instruction->GetLocations()->InAt(0);
5140
5141 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5142}
5143
5144void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005145 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005146}
5147
5148void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5149 HandleBinaryOp(instruction);
5150}
5151
5152void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5153 HandleBinaryOp(instruction);
5154}
5155
5156void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5157 LOG(FATAL) << "Unreachable";
5158}
5159
5160void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
5161 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5162}
5163
5164void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
5165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5166 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5167 if (location.IsStackSlot()) {
5168 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5169 } else if (location.IsDoubleStackSlot()) {
5170 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5171 }
5172 locations->SetOut(location);
5173}
5174
5175void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
5176 ATTRIBUTE_UNUSED) {
5177 // Nothing to do, the parameter is already at its location.
5178}
5179
5180void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
5181 LocationSummary* locations =
5182 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5183 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5184}
5185
5186void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
5187 ATTRIBUTE_UNUSED) {
5188 // Nothing to do, the method is already at its location.
5189}
5190
5191void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
5192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005193 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005194 locations->SetInAt(i, Location::Any());
5195 }
5196 locations->SetOut(Location::Any());
5197}
5198
5199void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
5200 LOG(FATAL) << "Unreachable";
5201}
5202
5203void LocationsBuilderMIPS::VisitRem(HRem* rem) {
5204 Primitive::Type type = rem->GetResultType();
5205 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005206 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5208
5209 switch (type) {
5210 case Primitive::kPrimInt:
5211 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08005212 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5214 break;
5215
5216 case Primitive::kPrimLong: {
5217 InvokeRuntimeCallingConvention calling_convention;
5218 locations->SetInAt(0, Location::RegisterPairLocation(
5219 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5220 locations->SetInAt(1, Location::RegisterPairLocation(
5221 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
5222 locations->SetOut(calling_convention.GetReturnLocation(type));
5223 break;
5224 }
5225
5226 case Primitive::kPrimFloat:
5227 case Primitive::kPrimDouble: {
5228 InvokeRuntimeCallingConvention calling_convention;
5229 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5230 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
5231 locations->SetOut(calling_convention.GetReturnLocation(type));
5232 break;
5233 }
5234
5235 default:
5236 LOG(FATAL) << "Unexpected rem type " << type;
5237 }
5238}
5239
5240void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
5241 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005242
5243 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08005244 case Primitive::kPrimInt:
5245 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005246 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005247 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005248 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005249 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
5250 break;
5251 }
5252 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005253 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005254 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005255 break;
5256 }
5257 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01005258 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005259 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005260 break;
5261 }
5262 default:
5263 LOG(FATAL) << "Unexpected rem type " << type;
5264 }
5265}
5266
5267void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5268 memory_barrier->SetLocations(nullptr);
5269}
5270
5271void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5272 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
5273}
5274
5275void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
5276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
5277 Primitive::Type return_type = ret->InputAt(0)->GetType();
5278 locations->SetInAt(0, MipsReturnLocation(return_type));
5279}
5280
5281void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
5282 codegen_->GenerateFrameExit();
5283}
5284
5285void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
5286 ret->SetLocations(nullptr);
5287}
5288
5289void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
5290 codegen_->GenerateFrameExit();
5291}
5292
Alexey Frunze92d90602015-12-18 18:16:36 -08005293void LocationsBuilderMIPS::VisitRor(HRor* ror) {
5294 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005295}
5296
Alexey Frunze92d90602015-12-18 18:16:36 -08005297void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
5298 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005299}
5300
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005301void LocationsBuilderMIPS::VisitShl(HShl* shl) {
5302 HandleShift(shl);
5303}
5304
5305void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
5306 HandleShift(shl);
5307}
5308
5309void LocationsBuilderMIPS::VisitShr(HShr* shr) {
5310 HandleShift(shr);
5311}
5312
5313void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
5314 HandleShift(shr);
5315}
5316
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005317void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
5318 HandleBinaryOp(instruction);
5319}
5320
5321void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
5322 HandleBinaryOp(instruction);
5323}
5324
5325void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5326 HandleFieldGet(instruction, instruction->GetFieldInfo());
5327}
5328
5329void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5330 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5331}
5332
5333void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5334 HandleFieldSet(instruction, instruction->GetFieldInfo());
5335}
5336
5337void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5338 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5339}
5340
5341void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
5342 HUnresolvedInstanceFieldGet* instruction) {
5343 FieldAccessCallingConventionMIPS calling_convention;
5344 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5345 instruction->GetFieldType(),
5346 calling_convention);
5347}
5348
5349void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
5350 HUnresolvedInstanceFieldGet* instruction) {
5351 FieldAccessCallingConventionMIPS calling_convention;
5352 codegen_->GenerateUnresolvedFieldAccess(instruction,
5353 instruction->GetFieldType(),
5354 instruction->GetFieldIndex(),
5355 instruction->GetDexPc(),
5356 calling_convention);
5357}
5358
5359void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
5360 HUnresolvedInstanceFieldSet* instruction) {
5361 FieldAccessCallingConventionMIPS calling_convention;
5362 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5363 instruction->GetFieldType(),
5364 calling_convention);
5365}
5366
5367void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
5368 HUnresolvedInstanceFieldSet* instruction) {
5369 FieldAccessCallingConventionMIPS calling_convention;
5370 codegen_->GenerateUnresolvedFieldAccess(instruction,
5371 instruction->GetFieldType(),
5372 instruction->GetFieldIndex(),
5373 instruction->GetDexPc(),
5374 calling_convention);
5375}
5376
5377void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
5378 HUnresolvedStaticFieldGet* instruction) {
5379 FieldAccessCallingConventionMIPS calling_convention;
5380 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5381 instruction->GetFieldType(),
5382 calling_convention);
5383}
5384
5385void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
5386 HUnresolvedStaticFieldGet* instruction) {
5387 FieldAccessCallingConventionMIPS calling_convention;
5388 codegen_->GenerateUnresolvedFieldAccess(instruction,
5389 instruction->GetFieldType(),
5390 instruction->GetFieldIndex(),
5391 instruction->GetDexPc(),
5392 calling_convention);
5393}
5394
5395void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
5396 HUnresolvedStaticFieldSet* instruction) {
5397 FieldAccessCallingConventionMIPS calling_convention;
5398 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
5399 instruction->GetFieldType(),
5400 calling_convention);
5401}
5402
5403void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
5404 HUnresolvedStaticFieldSet* instruction) {
5405 FieldAccessCallingConventionMIPS calling_convention;
5406 codegen_->GenerateUnresolvedFieldAccess(instruction,
5407 instruction->GetFieldType(),
5408 instruction->GetFieldIndex(),
5409 instruction->GetDexPc(),
5410 calling_convention);
5411}
5412
5413void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005414 LocationSummary* locations =
5415 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005416 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005417}
5418
5419void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
5420 HBasicBlock* block = instruction->GetBlock();
5421 if (block->GetLoopInformation() != nullptr) {
5422 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5423 // The back edge will generate the suspend check.
5424 return;
5425 }
5426 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5427 // The goto will generate the suspend check.
5428 return;
5429 }
5430 GenerateSuspendCheck(instruction, nullptr);
5431}
5432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005433void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
5434 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005436 InvokeRuntimeCallingConvention calling_convention;
5437 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5438}
5439
5440void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005441 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005442 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
5443}
5444
5445void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5446 Primitive::Type input_type = conversion->GetInputType();
5447 Primitive::Type result_type = conversion->GetResultType();
5448 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005449 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005450
5451 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5452 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5453 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5454 }
5455
5456 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005457 if (!isR6 &&
5458 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
5459 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005460 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005461 }
5462
5463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
5464
5465 if (call_kind == LocationSummary::kNoCall) {
5466 if (Primitive::IsFloatingPointType(input_type)) {
5467 locations->SetInAt(0, Location::RequiresFpuRegister());
5468 } else {
5469 locations->SetInAt(0, Location::RequiresRegister());
5470 }
5471
5472 if (Primitive::IsFloatingPointType(result_type)) {
5473 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5474 } else {
5475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5476 }
5477 } else {
5478 InvokeRuntimeCallingConvention calling_convention;
5479
5480 if (Primitive::IsFloatingPointType(input_type)) {
5481 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
5482 } else {
5483 DCHECK_EQ(input_type, Primitive::kPrimLong);
5484 locations->SetInAt(0, Location::RegisterPairLocation(
5485 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
5486 }
5487
5488 locations->SetOut(calling_convention.GetReturnLocation(result_type));
5489 }
5490}
5491
5492void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
5493 LocationSummary* locations = conversion->GetLocations();
5494 Primitive::Type result_type = conversion->GetResultType();
5495 Primitive::Type input_type = conversion->GetInputType();
5496 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005497 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005498
5499 DCHECK_NE(input_type, result_type);
5500
5501 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
5502 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5503 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5504 Register src = locations->InAt(0).AsRegister<Register>();
5505
Alexey Frunzea871ef12016-06-27 15:20:11 -07005506 if (dst_low != src) {
5507 __ Move(dst_low, src);
5508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005509 __ Sra(dst_high, src, 31);
5510 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
5511 Register dst = locations->Out().AsRegister<Register>();
5512 Register src = (input_type == Primitive::kPrimLong)
5513 ? locations->InAt(0).AsRegisterPairLow<Register>()
5514 : locations->InAt(0).AsRegister<Register>();
5515
5516 switch (result_type) {
5517 case Primitive::kPrimChar:
5518 __ Andi(dst, src, 0xFFFF);
5519 break;
5520 case Primitive::kPrimByte:
5521 if (has_sign_extension) {
5522 __ Seb(dst, src);
5523 } else {
5524 __ Sll(dst, src, 24);
5525 __ Sra(dst, dst, 24);
5526 }
5527 break;
5528 case Primitive::kPrimShort:
5529 if (has_sign_extension) {
5530 __ Seh(dst, src);
5531 } else {
5532 __ Sll(dst, src, 16);
5533 __ Sra(dst, dst, 16);
5534 }
5535 break;
5536 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07005537 if (dst != src) {
5538 __ Move(dst, src);
5539 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005540 break;
5541
5542 default:
5543 LOG(FATAL) << "Unexpected type conversion from " << input_type
5544 << " to " << result_type;
5545 }
5546 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005547 if (input_type == Primitive::kPrimLong) {
5548 if (isR6) {
5549 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5550 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5551 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5552 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5553 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5554 __ Mtc1(src_low, FTMP);
5555 __ Mthc1(src_high, FTMP);
5556 if (result_type == Primitive::kPrimFloat) {
5557 __ Cvtsl(dst, FTMP);
5558 } else {
5559 __ Cvtdl(dst, FTMP);
5560 }
5561 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005562 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
5563 : kQuickL2d;
5564 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005565 if (result_type == Primitive::kPrimFloat) {
5566 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
5567 } else {
5568 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
5569 }
5570 }
5571 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005572 Register src = locations->InAt(0).AsRegister<Register>();
5573 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5574 __ Mtc1(src, FTMP);
5575 if (result_type == Primitive::kPrimFloat) {
5576 __ Cvtsw(dst, FTMP);
5577 } else {
5578 __ Cvtdw(dst, FTMP);
5579 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005580 }
5581 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
5582 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005583 if (result_type == Primitive::kPrimLong) {
5584 if (isR6) {
5585 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
5586 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
5587 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5588 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5589 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5590 MipsLabel truncate;
5591 MipsLabel done;
5592
5593 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
5594 // value when the input is either a NaN or is outside of the range of the output type
5595 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
5596 // the same result.
5597 //
5598 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
5599 // value of the output type if the input is outside of the range after the truncation or
5600 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
5601 // results. This matches the desired float/double-to-int/long conversion exactly.
5602 //
5603 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
5604 //
5605 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5606 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5607 // even though it must be NAN2008=1 on R6.
5608 //
5609 // The code takes care of the different behaviors by first comparing the input to the
5610 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
5611 // If the input is greater than or equal to the minimum, it procedes to the truncate
5612 // instruction, which will handle such an input the same way irrespective of NAN2008.
5613 // Otherwise the input is compared to itself to determine whether it is a NaN or not
5614 // in order to return either zero or the minimum value.
5615 //
5616 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5617 // truncate instruction for MIPS64R6.
5618 if (input_type == Primitive::kPrimFloat) {
5619 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
5620 __ LoadConst32(TMP, min_val);
5621 __ Mtc1(TMP, FTMP);
5622 __ CmpLeS(FTMP, FTMP, src);
5623 } else {
5624 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
5625 __ LoadConst32(TMP, High32Bits(min_val));
5626 __ Mtc1(ZERO, FTMP);
5627 __ Mthc1(TMP, FTMP);
5628 __ CmpLeD(FTMP, FTMP, src);
5629 }
5630
5631 __ Bc1nez(FTMP, &truncate);
5632
5633 if (input_type == Primitive::kPrimFloat) {
5634 __ CmpEqS(FTMP, src, src);
5635 } else {
5636 __ CmpEqD(FTMP, src, src);
5637 }
5638 __ Move(dst_low, ZERO);
5639 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
5640 __ Mfc1(TMP, FTMP);
5641 __ And(dst_high, dst_high, TMP);
5642
5643 __ B(&done);
5644
5645 __ Bind(&truncate);
5646
5647 if (input_type == Primitive::kPrimFloat) {
5648 __ TruncLS(FTMP, src);
5649 } else {
5650 __ TruncLD(FTMP, src);
5651 }
5652 __ Mfc1(dst_low, FTMP);
5653 __ Mfhc1(dst_high, FTMP);
5654
5655 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005656 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005657 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
5658 : kQuickD2l;
5659 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005660 if (input_type == Primitive::kPrimFloat) {
5661 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
5662 } else {
5663 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
5664 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005665 }
5666 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005667 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5668 Register dst = locations->Out().AsRegister<Register>();
5669 MipsLabel truncate;
5670 MipsLabel done;
5671
5672 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
5673 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
5674 // even though it must be NAN2008=1 on R6.
5675 //
5676 // For details see the large comment above for the truncation of float/double to long on R6.
5677 //
5678 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
5679 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005680 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005681 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
5682 __ LoadConst32(TMP, min_val);
5683 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005684 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005685 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
5686 __ LoadConst32(TMP, High32Bits(min_val));
5687 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07005688 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005689 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08005690
5691 if (isR6) {
5692 if (input_type == Primitive::kPrimFloat) {
5693 __ CmpLeS(FTMP, FTMP, src);
5694 } else {
5695 __ CmpLeD(FTMP, FTMP, src);
5696 }
5697 __ Bc1nez(FTMP, &truncate);
5698
5699 if (input_type == Primitive::kPrimFloat) {
5700 __ CmpEqS(FTMP, src, src);
5701 } else {
5702 __ CmpEqD(FTMP, src, src);
5703 }
5704 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5705 __ Mfc1(TMP, FTMP);
5706 __ And(dst, dst, TMP);
5707 } else {
5708 if (input_type == Primitive::kPrimFloat) {
5709 __ ColeS(0, FTMP, src);
5710 } else {
5711 __ ColeD(0, FTMP, src);
5712 }
5713 __ Bc1t(0, &truncate);
5714
5715 if (input_type == Primitive::kPrimFloat) {
5716 __ CeqS(0, src, src);
5717 } else {
5718 __ CeqD(0, src, src);
5719 }
5720 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
5721 __ Movf(dst, ZERO, 0);
5722 }
5723
5724 __ B(&done);
5725
5726 __ Bind(&truncate);
5727
5728 if (input_type == Primitive::kPrimFloat) {
5729 __ TruncWS(FTMP, src);
5730 } else {
5731 __ TruncWD(FTMP, src);
5732 }
5733 __ Mfc1(dst, FTMP);
5734
5735 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736 }
5737 } else if (Primitive::IsFloatingPointType(result_type) &&
5738 Primitive::IsFloatingPointType(input_type)) {
5739 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5740 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5741 if (result_type == Primitive::kPrimFloat) {
5742 __ Cvtsd(dst, src);
5743 } else {
5744 __ Cvtds(dst, src);
5745 }
5746 } else {
5747 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5748 << " to " << result_type;
5749 }
5750}
5751
5752void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
5753 HandleShift(ushr);
5754}
5755
5756void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
5757 HandleShift(ushr);
5758}
5759
5760void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
5761 HandleBinaryOp(instruction);
5762}
5763
5764void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
5765 HandleBinaryOp(instruction);
5766}
5767
5768void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5769 // Nothing to do, this should be removed during prepare for register allocator.
5770 LOG(FATAL) << "Unreachable";
5771}
5772
5773void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
5774 // Nothing to do, this should be removed during prepare for register allocator.
5775 LOG(FATAL) << "Unreachable";
5776}
5777
5778void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005779 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005780}
5781
5782void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005783 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005784}
5785
5786void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005788}
5789
5790void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005792}
5793
5794void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796}
5797
5798void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005799 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800}
5801
5802void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005803 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005804}
5805
5806void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005807 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005808}
5809
5810void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005811 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005812}
5813
5814void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005815 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005816}
5817
5818void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005819 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005820}
5821
5822void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005823 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005824}
5825
5826void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005827 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005828}
5829
5830void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005831 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005832}
5833
5834void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005835 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836}
5837
5838void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005839 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005840}
5841
5842void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005843 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005844}
5845
5846void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005847 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848}
5849
5850void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005851 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005852}
5853
5854void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005855 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005856}
5857
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005858void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5859 LocationSummary* locations =
5860 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5861 locations->SetInAt(0, Location::RequiresRegister());
5862}
5863
Alexey Frunze96b66822016-09-10 02:32:44 -07005864void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
5865 int32_t lower_bound,
5866 uint32_t num_entries,
5867 HBasicBlock* switch_block,
5868 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005869 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005870 Register temp_reg = TMP;
5871 __ Addiu32(temp_reg, value_reg, -lower_bound);
5872 // Jump to default if index is negative
5873 // Note: We don't check the case that index is positive while value < lower_bound, because in
5874 // this case, index >= num_entries must be true. So that we can save one branch instruction.
5875 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
5876
Alexey Frunze96b66822016-09-10 02:32:44 -07005877 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005878 // Jump to successors[0] if value == lower_bound.
5879 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
5880 int32_t last_index = 0;
5881 for (; num_entries - last_index > 2; last_index += 2) {
5882 __ Addiu(temp_reg, temp_reg, -2);
5883 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5884 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
5885 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5886 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
5887 }
5888 if (num_entries - last_index == 2) {
5889 // The last missing case_value.
5890 __ Addiu(temp_reg, temp_reg, -1);
5891 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005892 }
5893
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005894 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07005895 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005896 __ B(codegen_->GetLabelOf(default_block));
5897 }
5898}
5899
Alexey Frunze96b66822016-09-10 02:32:44 -07005900void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
5901 Register constant_area,
5902 int32_t lower_bound,
5903 uint32_t num_entries,
5904 HBasicBlock* switch_block,
5905 HBasicBlock* default_block) {
5906 // Create a jump table.
5907 std::vector<MipsLabel*> labels(num_entries);
5908 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
5909 for (uint32_t i = 0; i < num_entries; i++) {
5910 labels[i] = codegen_->GetLabelOf(successors[i]);
5911 }
5912 JumpTable* table = __ CreateJumpTable(std::move(labels));
5913
5914 // Is the value in range?
5915 __ Addiu32(TMP, value_reg, -lower_bound);
5916 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
5917 __ Sltiu(AT, TMP, num_entries);
5918 __ Beqz(AT, codegen_->GetLabelOf(default_block));
5919 } else {
5920 __ LoadConst32(AT, num_entries);
5921 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
5922 }
5923
5924 // We are in the range of the table.
5925 // Load the target address from the jump table, indexing by the value.
5926 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
5927 __ Sll(TMP, TMP, 2);
5928 __ Addu(TMP, TMP, AT);
5929 __ Lw(TMP, TMP, 0);
5930 // Compute the absolute target address by adding the table start address
5931 // (the table contains offsets to targets relative to its start).
5932 __ Addu(TMP, TMP, AT);
5933 // And jump.
5934 __ Jr(TMP);
5935 __ NopIfNoReordering();
5936}
5937
5938void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5939 int32_t lower_bound = switch_instr->GetStartValue();
5940 uint32_t num_entries = switch_instr->GetNumEntries();
5941 LocationSummary* locations = switch_instr->GetLocations();
5942 Register value_reg = locations->InAt(0).AsRegister<Register>();
5943 HBasicBlock* switch_block = switch_instr->GetBlock();
5944 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5945
5946 if (codegen_->GetInstructionSetFeatures().IsR6() &&
5947 num_entries > kPackedSwitchJumpTableThreshold) {
5948 // R6 uses PC-relative addressing to access the jump table.
5949 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
5950 // the jump table and it is implemented by changing HPackedSwitch to
5951 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
5952 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
5953 GenTableBasedPackedSwitch(value_reg,
5954 ZERO,
5955 lower_bound,
5956 num_entries,
5957 switch_block,
5958 default_block);
5959 } else {
5960 GenPackedSwitchWithCompares(value_reg,
5961 lower_bound,
5962 num_entries,
5963 switch_block,
5964 default_block);
5965 }
5966}
5967
5968void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5969 LocationSummary* locations =
5970 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5971 locations->SetInAt(0, Location::RequiresRegister());
5972 // Constant area pointer (HMipsComputeBaseMethodAddress).
5973 locations->SetInAt(1, Location::RequiresRegister());
5974}
5975
5976void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
5977 int32_t lower_bound = switch_instr->GetStartValue();
5978 uint32_t num_entries = switch_instr->GetNumEntries();
5979 LocationSummary* locations = switch_instr->GetLocations();
5980 Register value_reg = locations->InAt(0).AsRegister<Register>();
5981 Register constant_area = locations->InAt(1).AsRegister<Register>();
5982 HBasicBlock* switch_block = switch_instr->GetBlock();
5983 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5984
5985 // This is an R2-only path. HPackedSwitch has been changed to
5986 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
5987 // required to address the jump table relative to PC.
5988 GenTableBasedPackedSwitch(value_reg,
5989 constant_area,
5990 lower_bound,
5991 num_entries,
5992 switch_block,
5993 default_block);
5994}
5995
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005996void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
5997 HMipsComputeBaseMethodAddress* insn) {
5998 LocationSummary* locations =
5999 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6000 locations->SetOut(Location::RequiresRegister());
6001}
6002
6003void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6004 HMipsComputeBaseMethodAddress* insn) {
6005 LocationSummary* locations = insn->GetLocations();
6006 Register reg = locations->Out().AsRegister<Register>();
6007
6008 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6009
6010 // Generate a dummy PC-relative call to obtain PC.
6011 __ Nal();
6012 // Grab the return address off RA.
6013 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006014 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006015
6016 // Remember this offset (the obtained PC value) for later use with constant area.
6017 __ BindPcRelBaseLabel();
6018}
6019
6020void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6021 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6022 locations->SetOut(Location::RequiresRegister());
6023}
6024
6025void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6026 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6027 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6028 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006029 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6030 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006031}
6032
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006033void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6034 // The trampoline uses the same calling convention as dex calling conventions,
6035 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6036 // the method_idx.
6037 HandleInvoke(invoke);
6038}
6039
6040void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6041 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6042}
6043
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006044void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6045 LocationSummary* locations =
6046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6047 locations->SetInAt(0, Location::RequiresRegister());
6048 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006049}
6050
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006051void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6052 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006053 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006054 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006055 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006056 __ LoadFromOffset(kLoadWord,
6057 locations->Out().AsRegister<Register>(),
6058 locations->InAt(0).AsRegister<Register>(),
6059 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006060 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006061 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006062 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006063 __ LoadFromOffset(kLoadWord,
6064 locations->Out().AsRegister<Register>(),
6065 locations->InAt(0).AsRegister<Register>(),
6066 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006067 __ LoadFromOffset(kLoadWord,
6068 locations->Out().AsRegister<Register>(),
6069 locations->Out().AsRegister<Register>(),
6070 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006071 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006072}
6073
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006074#undef __
6075#undef QUICK_ENTRY_POINT
6076
6077} // namespace mips
6078} // namespace art