blob: 8f94834333909036a6b5971fc0160ab96d62a413 [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;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200228
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 Markoaad75c62016-10-03 08:46:48 +0000282 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 Markoaad75c62016-10-03 08:46:48 +0000293
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, TMP, base);
304 __ StoreToOffset(kStoreWord, out, TMP, 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();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200381 uint32_t dex_pc = instruction_->GetDexPc();
382 DCHECK(instruction_->IsCheckCast()
383 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
384 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
385
386 __ Bind(GetEntryLabel());
387 SaveLiveRegisters(codegen, locations);
388
389 // We're moving two locations to locations that could overlap, so we need a parallel
390 // move resolver.
391 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800392 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
394 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800395 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200396 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
397 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100399 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800400 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401 Primitive::Type ret_type = instruction_->GetType();
402 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
403 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 } else {
405 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800406 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
407 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200408 }
409
410 RestoreLiveRegisters(codegen, locations);
411 __ B(GetExitLabel());
412 }
413
414 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
415
416 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
418};
419
420class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
421 public:
Aart Bik42249c32016-01-07 15:33:50 -0800422 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000423 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424
425 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800426 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100428 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000429 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 }
431
432 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
433
434 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200435 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
436};
437
438CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
439 const MipsInstructionSetFeatures& isa_features,
440 const CompilerOptions& compiler_options,
441 OptimizingCompilerStats* stats)
442 : CodeGenerator(graph,
443 kNumberOfCoreRegisters,
444 kNumberOfFRegisters,
445 kNumberOfRegisterPairs,
446 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
447 arraysize(kCoreCalleeSaves)),
448 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
449 arraysize(kFpuCalleeSaves)),
450 compiler_options,
451 stats),
452 block_labels_(nullptr),
453 location_builder_(graph, this),
454 instruction_visitor_(graph, this),
455 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100456 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700457 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700458 uint32_literals_(std::less<uint32_t>(),
459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700460 method_patches_(MethodReferenceComparator(),
461 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
462 call_patches_(MethodReferenceComparator(),
463 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700464 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 boot_image_string_patches_(StringReferenceValueComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
467 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_type_patches_(TypeReferenceValueComparator(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 boot_image_address_patches_(std::less<uint32_t>(),
472 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
473 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200474 // Save RA (containing the return address) to mimic Quick.
475 AddAllocatedRegister(Location::RegisterLocation(RA));
476}
477
478#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100479// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
480#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700481#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482
483void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
484 // Ensure that we fix up branches.
485 __ FinalizeCode();
486
487 // Adjust native pc offsets in stack maps.
488 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
489 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
490 uint32_t new_position = __ GetAdjustedPosition(old_position);
491 DCHECK_GE(new_position, old_position);
492 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
493 }
494
495 // Adjust pc offsets for the disassembly information.
496 if (disasm_info_ != nullptr) {
497 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
498 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
499 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
500 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
501 it.second.start = __ GetAdjustedPosition(it.second.start);
502 it.second.end = __ GetAdjustedPosition(it.second.end);
503 }
504 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
505 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
506 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
507 }
508 }
509
510 CodeGenerator::Finalize(allocator);
511}
512
513MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
514 return codegen_->GetAssembler();
515}
516
517void ParallelMoveResolverMIPS::EmitMove(size_t index) {
518 DCHECK_LT(index, moves_.size());
519 MoveOperands* move = moves_[index];
520 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
521}
522
523void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
524 DCHECK_LT(index, moves_.size());
525 MoveOperands* move = moves_[index];
526 Primitive::Type type = move->GetType();
527 Location loc1 = move->GetDestination();
528 Location loc2 = move->GetSource();
529
530 DCHECK(!loc1.IsConstant());
531 DCHECK(!loc2.IsConstant());
532
533 if (loc1.Equals(loc2)) {
534 return;
535 }
536
537 if (loc1.IsRegister() && loc2.IsRegister()) {
538 // Swap 2 GPRs.
539 Register r1 = loc1.AsRegister<Register>();
540 Register r2 = loc2.AsRegister<Register>();
541 __ Move(TMP, r2);
542 __ Move(r2, r1);
543 __ Move(r1, TMP);
544 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
545 FRegister f1 = loc1.AsFpuRegister<FRegister>();
546 FRegister f2 = loc2.AsFpuRegister<FRegister>();
547 if (type == Primitive::kPrimFloat) {
548 __ MovS(FTMP, f2);
549 __ MovS(f2, f1);
550 __ MovS(f1, FTMP);
551 } else {
552 DCHECK_EQ(type, Primitive::kPrimDouble);
553 __ MovD(FTMP, f2);
554 __ MovD(f2, f1);
555 __ MovD(f1, FTMP);
556 }
557 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
558 (loc1.IsFpuRegister() && loc2.IsRegister())) {
559 // Swap FPR and GPR.
560 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
561 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
562 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200563 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200564 __ Move(TMP, r2);
565 __ Mfc1(r2, f1);
566 __ Mtc1(TMP, f1);
567 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
568 // Swap 2 GPR register pairs.
569 Register r1 = loc1.AsRegisterPairLow<Register>();
570 Register r2 = loc2.AsRegisterPairLow<Register>();
571 __ Move(TMP, r2);
572 __ Move(r2, r1);
573 __ Move(r1, TMP);
574 r1 = loc1.AsRegisterPairHigh<Register>();
575 r2 = loc2.AsRegisterPairHigh<Register>();
576 __ Move(TMP, r2);
577 __ Move(r2, r1);
578 __ Move(r1, TMP);
579 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
580 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
581 // Swap FPR and GPR register pair.
582 DCHECK_EQ(type, Primitive::kPrimDouble);
583 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
584 : loc2.AsFpuRegister<FRegister>();
585 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
586 : loc2.AsRegisterPairLow<Register>();
587 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
588 : loc2.AsRegisterPairHigh<Register>();
589 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
590 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
591 // unpredictable and the following mfch1 will fail.
592 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800593 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200594 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800595 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200596 __ Move(r2_l, TMP);
597 __ Move(r2_h, AT);
598 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
599 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
600 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
601 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000602 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
603 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200604 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
605 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000606 __ Move(TMP, reg);
607 __ LoadFromOffset(kLoadWord, reg, SP, offset);
608 __ StoreToOffset(kStoreWord, TMP, SP, offset);
609 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
610 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
611 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
612 : loc2.AsRegisterPairLow<Register>();
613 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
614 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200615 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000616 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
617 : loc2.GetHighStackIndex(kMipsWordSize);
618 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000619 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000620 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000621 __ Move(TMP, reg_h);
622 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
623 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200624 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
625 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
626 : loc2.AsFpuRegister<FRegister>();
627 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
628 if (type == Primitive::kPrimFloat) {
629 __ MovS(FTMP, reg);
630 __ LoadSFromOffset(reg, SP, offset);
631 __ StoreSToOffset(FTMP, SP, offset);
632 } else {
633 DCHECK_EQ(type, Primitive::kPrimDouble);
634 __ MovD(FTMP, reg);
635 __ LoadDFromOffset(reg, SP, offset);
636 __ StoreDToOffset(FTMP, SP, offset);
637 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200638 } else {
639 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
640 }
641}
642
643void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
644 __ Pop(static_cast<Register>(reg));
645}
646
647void ParallelMoveResolverMIPS::SpillScratch(int reg) {
648 __ Push(static_cast<Register>(reg));
649}
650
651void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
652 // Allocate a scratch register other than TMP, if available.
653 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
654 // automatically unspilled when the scratch scope object is destroyed).
655 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
656 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
657 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
658 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
659 __ LoadFromOffset(kLoadWord,
660 Register(ensure_scratch.GetRegister()),
661 SP,
662 index1 + stack_offset);
663 __ LoadFromOffset(kLoadWord,
664 TMP,
665 SP,
666 index2 + stack_offset);
667 __ StoreToOffset(kStoreWord,
668 Register(ensure_scratch.GetRegister()),
669 SP,
670 index2 + stack_offset);
671 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
672 }
673}
674
Alexey Frunze73296a72016-06-03 22:51:46 -0700675void CodeGeneratorMIPS::ComputeSpillMask() {
676 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
677 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
678 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
679 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
680 // registers, include the ZERO register to force alignment of FPU callee-saved registers
681 // within the stack frame.
682 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
683 core_spill_mask_ |= (1 << ZERO);
684 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700685}
686
687bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700688 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700689 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
690 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
691 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700692 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
693 // saved in an unused temporary register) and saving of RA and the current method pointer
694 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700695 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700696}
697
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200698static dwarf::Reg DWARFReg(Register reg) {
699 return dwarf::Reg::MipsCore(static_cast<int>(reg));
700}
701
702// TODO: mapping of floating-point registers to DWARF.
703
704void CodeGeneratorMIPS::GenerateFrameEntry() {
705 __ Bind(&frame_entry_label_);
706
707 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
708
709 if (do_overflow_check) {
710 __ LoadFromOffset(kLoadWord,
711 ZERO,
712 SP,
713 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
714 RecordPcInfo(nullptr, 0);
715 }
716
717 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700718 CHECK_EQ(fpu_spill_mask_, 0u);
719 CHECK_EQ(core_spill_mask_, 1u << RA);
720 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200721 return;
722 }
723
724 // Make sure the frame size isn't unreasonably large.
725 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
726 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
727 }
728
729 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200730
Alexey Frunze73296a72016-06-03 22:51:46 -0700731 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200732 __ IncreaseFrameSize(ofs);
733
Alexey Frunze73296a72016-06-03 22:51:46 -0700734 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
735 Register reg = static_cast<Register>(MostSignificantBit(mask));
736 mask ^= 1u << reg;
737 ofs -= kMipsWordSize;
738 // The ZERO register is only included for alignment.
739 if (reg != ZERO) {
740 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200741 __ cfi().RelOffset(DWARFReg(reg), ofs);
742 }
743 }
744
Alexey Frunze73296a72016-06-03 22:51:46 -0700745 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
746 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
747 mask ^= 1u << reg;
748 ofs -= kMipsDoublewordSize;
749 __ StoreDToOffset(reg, SP, ofs);
750 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200751 }
752
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100753 // Save the current method if we need it. Note that we do not
754 // do this in HCurrentMethod, as the instruction might have been removed
755 // in the SSA graph.
756 if (RequiresCurrentMethod()) {
757 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
758 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200759}
760
761void CodeGeneratorMIPS::GenerateFrameExit() {
762 __ cfi().RememberState();
763
764 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200765 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766
Alexey Frunze73296a72016-06-03 22:51:46 -0700767 // For better instruction scheduling restore RA before other registers.
768 uint32_t ofs = GetFrameSize();
769 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
770 Register reg = static_cast<Register>(MostSignificantBit(mask));
771 mask ^= 1u << reg;
772 ofs -= kMipsWordSize;
773 // The ZERO register is only included for alignment.
774 if (reg != ZERO) {
775 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200776 __ cfi().Restore(DWARFReg(reg));
777 }
778 }
779
Alexey Frunze73296a72016-06-03 22:51:46 -0700780 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
781 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
782 mask ^= 1u << reg;
783 ofs -= kMipsDoublewordSize;
784 __ LoadDFromOffset(reg, SP, ofs);
785 // TODO: __ cfi().Restore(DWARFReg(reg));
786 }
787
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700788 size_t frame_size = GetFrameSize();
789 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
790 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
791 bool reordering = __ SetReorder(false);
792 if (exchange) {
793 __ Jr(RA);
794 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
795 } else {
796 __ DecreaseFrameSize(frame_size);
797 __ Jr(RA);
798 __ Nop(); // In delay slot.
799 }
800 __ SetReorder(reordering);
801 } else {
802 __ Jr(RA);
803 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200804 }
805
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200806 __ cfi().RestoreState();
807 __ cfi().DefCFAOffset(GetFrameSize());
808}
809
810void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
811 __ Bind(GetLabelOf(block));
812}
813
814void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
815 if (src.Equals(dst)) {
816 return;
817 }
818
819 if (src.IsConstant()) {
820 MoveConstant(dst, src.GetConstant());
821 } else {
822 if (Primitive::Is64BitType(dst_type)) {
823 Move64(dst, src);
824 } else {
825 Move32(dst, src);
826 }
827 }
828}
829
830void CodeGeneratorMIPS::Move32(Location destination, Location source) {
831 if (source.Equals(destination)) {
832 return;
833 }
834
835 if (destination.IsRegister()) {
836 if (source.IsRegister()) {
837 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
838 } else if (source.IsFpuRegister()) {
839 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
840 } else {
841 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
842 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
843 }
844 } else if (destination.IsFpuRegister()) {
845 if (source.IsRegister()) {
846 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
847 } else if (source.IsFpuRegister()) {
848 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
849 } else {
850 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
851 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
852 }
853 } else {
854 DCHECK(destination.IsStackSlot()) << destination;
855 if (source.IsRegister()) {
856 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
857 } else if (source.IsFpuRegister()) {
858 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
859 } else {
860 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
861 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
862 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
863 }
864 }
865}
866
867void CodeGeneratorMIPS::Move64(Location destination, Location source) {
868 if (source.Equals(destination)) {
869 return;
870 }
871
872 if (destination.IsRegisterPair()) {
873 if (source.IsRegisterPair()) {
874 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
875 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
876 } else if (source.IsFpuRegister()) {
877 Register dst_high = destination.AsRegisterPairHigh<Register>();
878 Register dst_low = destination.AsRegisterPairLow<Register>();
879 FRegister src = source.AsFpuRegister<FRegister>();
880 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800881 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200882 } else {
883 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
884 int32_t off = source.GetStackIndex();
885 Register r = destination.AsRegisterPairLow<Register>();
886 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
887 }
888 } else if (destination.IsFpuRegister()) {
889 if (source.IsRegisterPair()) {
890 FRegister dst = destination.AsFpuRegister<FRegister>();
891 Register src_high = source.AsRegisterPairHigh<Register>();
892 Register src_low = source.AsRegisterPairLow<Register>();
893 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800894 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200895 } else if (source.IsFpuRegister()) {
896 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
897 } else {
898 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
899 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
900 }
901 } else {
902 DCHECK(destination.IsDoubleStackSlot()) << destination;
903 int32_t off = destination.GetStackIndex();
904 if (source.IsRegisterPair()) {
905 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
906 } else if (source.IsFpuRegister()) {
907 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
908 } else {
909 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
910 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
911 __ StoreToOffset(kStoreWord, TMP, SP, off);
912 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
913 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
914 }
915 }
916}
917
918void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
919 if (c->IsIntConstant() || c->IsNullConstant()) {
920 // Move 32 bit constant.
921 int32_t value = GetInt32ValueOf(c);
922 if (destination.IsRegister()) {
923 Register dst = destination.AsRegister<Register>();
924 __ LoadConst32(dst, value);
925 } else {
926 DCHECK(destination.IsStackSlot())
927 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700928 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200929 }
930 } else if (c->IsLongConstant()) {
931 // Move 64 bit constant.
932 int64_t value = GetInt64ValueOf(c);
933 if (destination.IsRegisterPair()) {
934 Register r_h = destination.AsRegisterPairHigh<Register>();
935 Register r_l = destination.AsRegisterPairLow<Register>();
936 __ LoadConst64(r_h, r_l, value);
937 } else {
938 DCHECK(destination.IsDoubleStackSlot())
939 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700940 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200941 }
942 } else if (c->IsFloatConstant()) {
943 // Move 32 bit float constant.
944 int32_t value = GetInt32ValueOf(c);
945 if (destination.IsFpuRegister()) {
946 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
947 } else {
948 DCHECK(destination.IsStackSlot())
949 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700950 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200951 }
952 } else {
953 // Move 64 bit double constant.
954 DCHECK(c->IsDoubleConstant()) << c->DebugName();
955 int64_t value = GetInt64ValueOf(c);
956 if (destination.IsFpuRegister()) {
957 FRegister fd = destination.AsFpuRegister<FRegister>();
958 __ LoadDConst64(fd, value, TMP);
959 } else {
960 DCHECK(destination.IsDoubleStackSlot())
961 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700962 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200963 }
964 }
965}
966
967void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
968 DCHECK(destination.IsRegister());
969 Register dst = destination.AsRegister<Register>();
970 __ LoadConst32(dst, value);
971}
972
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200973void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
974 if (location.IsRegister()) {
975 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700976 } else if (location.IsRegisterPair()) {
977 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
978 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200979 } else {
980 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
981 }
982}
983
Vladimir Markoaad75c62016-10-03 08:46:48 +0000984template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
985inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
986 const ArenaDeque<PcRelativePatchInfo>& infos,
987 ArenaVector<LinkerPatch>* linker_patches) {
988 for (const PcRelativePatchInfo& info : infos) {
989 const DexFile& dex_file = info.target_dex_file;
990 size_t offset_or_index = info.offset_or_index;
991 DCHECK(info.high_label.IsBound());
992 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
993 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
994 // the assembler's base label used for PC-relative addressing.
995 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
996 ? __ GetLabelLocation(&info.pc_rel_label)
997 : __ GetPcRelBaseLabelLocation();
998 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
999 }
1000}
1001
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001002void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1003 DCHECK(linker_patches->empty());
1004 size_t size =
1005 method_patches_.size() +
1006 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001007 pc_relative_dex_cache_patches_.size() +
1008 pc_relative_string_patches_.size() +
1009 pc_relative_type_patches_.size() +
1010 boot_image_string_patches_.size() +
1011 boot_image_type_patches_.size() +
1012 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 linker_patches->reserve(size);
1014 for (const auto& entry : method_patches_) {
1015 const MethodReference& target_method = entry.first;
1016 Literal* literal = entry.second;
1017 DCHECK(literal->GetLabel()->IsBound());
1018 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1019 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1020 target_method.dex_file,
1021 target_method.dex_method_index));
1022 }
1023 for (const auto& entry : call_patches_) {
1024 const MethodReference& target_method = entry.first;
1025 Literal* literal = entry.second;
1026 DCHECK(literal->GetLabel()->IsBound());
1027 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1028 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1029 target_method.dex_file,
1030 target_method.dex_method_index));
1031 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001032 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1033 linker_patches);
1034 if (!GetCompilerOptions().IsBootImage()) {
1035 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1036 linker_patches);
1037 } else {
1038 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1039 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001040 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001041 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1042 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001043 for (const auto& entry : boot_image_string_patches_) {
1044 const StringReference& target_string = entry.first;
1045 Literal* literal = entry.second;
1046 DCHECK(literal->GetLabel()->IsBound());
1047 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1048 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1049 target_string.dex_file,
1050 target_string.string_index));
1051 }
1052 for (const auto& entry : boot_image_type_patches_) {
1053 const TypeReference& target_type = entry.first;
1054 Literal* literal = entry.second;
1055 DCHECK(literal->GetLabel()->IsBound());
1056 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1057 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1058 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001059 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001060 }
1061 for (const auto& entry : boot_image_address_patches_) {
1062 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1063 Literal* literal = entry.second;
1064 DCHECK(literal->GetLabel()->IsBound());
1065 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1066 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1067 }
1068}
1069
1070CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1071 const DexFile& dex_file, uint32_t string_index) {
1072 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1073}
1074
1075CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001076 const DexFile& dex_file, dex::TypeIndex type_index) {
1077 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001078}
1079
1080CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1081 const DexFile& dex_file, uint32_t element_offset) {
1082 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1083}
1084
1085CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1086 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1087 patches->emplace_back(dex_file, offset_or_index);
1088 return &patches->back();
1089}
1090
Alexey Frunze06a46c42016-07-19 15:00:40 -07001091Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1092 return map->GetOrCreate(
1093 value,
1094 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1095}
1096
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001097Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1098 MethodToLiteralMap* map) {
1099 return map->GetOrCreate(
1100 target_method,
1101 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1102}
1103
1104Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1105 return DeduplicateMethodLiteral(target_method, &method_patches_);
1106}
1107
1108Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1109 return DeduplicateMethodLiteral(target_method, &call_patches_);
1110}
1111
Alexey Frunze06a46c42016-07-19 15:00:40 -07001112Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1113 uint32_t string_index) {
1114 return boot_image_string_patches_.GetOrCreate(
1115 StringReference(&dex_file, string_index),
1116 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1117}
1118
1119Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001120 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001121 return boot_image_type_patches_.GetOrCreate(
1122 TypeReference(&dex_file, type_index),
1123 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1124}
1125
1126Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1127 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1128 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1129 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1130}
1131
Vladimir Markoaad75c62016-10-03 08:46:48 +00001132void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1133 PcRelativePatchInfo* info, Register out, Register base) {
1134 bool reordering = __ SetReorder(false);
1135 if (GetInstructionSetFeatures().IsR6()) {
1136 DCHECK_EQ(base, ZERO);
1137 __ Bind(&info->high_label);
1138 __ Bind(&info->pc_rel_label);
1139 // Add a 32-bit offset to PC.
1140 __ Auipc(out, /* placeholder */ 0x1234);
1141 __ Addiu(out, out, /* placeholder */ 0x5678);
1142 } else {
1143 // If base is ZERO, emit NAL to obtain the actual base.
1144 if (base == ZERO) {
1145 // Generate a dummy PC-relative call to obtain PC.
1146 __ Nal();
1147 }
1148 __ Bind(&info->high_label);
1149 __ Lui(out, /* placeholder */ 0x1234);
1150 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1151 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1152 if (base == ZERO) {
1153 __ Bind(&info->pc_rel_label);
1154 }
1155 __ Ori(out, out, /* placeholder */ 0x5678);
1156 // Add a 32-bit offset to PC.
1157 __ Addu(out, out, (base == ZERO) ? RA : base);
1158 }
1159 __ SetReorder(reordering);
1160}
1161
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001162void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1163 MipsLabel done;
1164 Register card = AT;
1165 Register temp = TMP;
1166 __ Beqz(value, &done);
1167 __ LoadFromOffset(kLoadWord,
1168 card,
1169 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001170 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1172 __ Addu(temp, card, temp);
1173 __ Sb(card, temp, 0);
1174 __ Bind(&done);
1175}
1176
David Brazdil58282f42016-01-14 12:45:10 +00001177void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001178 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1179 blocked_core_registers_[ZERO] = true;
1180 blocked_core_registers_[K0] = true;
1181 blocked_core_registers_[K1] = true;
1182 blocked_core_registers_[GP] = true;
1183 blocked_core_registers_[SP] = true;
1184 blocked_core_registers_[RA] = true;
1185
1186 // AT and TMP(T8) are used as temporary/scratch registers
1187 // (similar to how AT is used by MIPS assemblers).
1188 blocked_core_registers_[AT] = true;
1189 blocked_core_registers_[TMP] = true;
1190 blocked_fpu_registers_[FTMP] = true;
1191
1192 // Reserve suspend and thread registers.
1193 blocked_core_registers_[S0] = true;
1194 blocked_core_registers_[TR] = true;
1195
1196 // Reserve T9 for function calls
1197 blocked_core_registers_[T9] = true;
1198
1199 // Reserve odd-numbered FPU registers.
1200 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1201 blocked_fpu_registers_[i] = true;
1202 }
1203
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001204 if (GetGraph()->IsDebuggable()) {
1205 // Stubs do not save callee-save floating point registers. If the graph
1206 // is debuggable, we need to deal with these registers differently. For
1207 // now, just block them.
1208 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1209 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1210 }
1211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001212}
1213
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001214size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1215 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1216 return kMipsWordSize;
1217}
1218
1219size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1220 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1221 return kMipsWordSize;
1222}
1223
1224size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1225 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1226 return kMipsDoublewordSize;
1227}
1228
1229size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1230 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1231 return kMipsDoublewordSize;
1232}
1233
1234void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001235 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236}
1237
1238void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001239 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001240}
1241
Serban Constantinescufca16662016-07-14 09:21:59 +01001242constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1243
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001244void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1245 HInstruction* instruction,
1246 uint32_t dex_pc,
1247 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001248 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001249 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001250 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001251 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001252 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001253 // Reserve argument space on stack (for $a0-$a3) for
1254 // entrypoints that directly reference native implementations.
1255 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001256 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001257 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001258 } else {
1259 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001260 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001261 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001262 if (EntrypointRequiresStackMap(entrypoint)) {
1263 RecordPcInfo(instruction, dex_pc, slow_path);
1264 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001265}
1266
1267void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1268 Register class_reg) {
1269 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1270 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1271 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1272 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1273 __ Sync(0);
1274 __ Bind(slow_path->GetExitLabel());
1275}
1276
1277void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1278 __ Sync(0); // Only stype 0 is supported.
1279}
1280
1281void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1282 HBasicBlock* successor) {
1283 SuspendCheckSlowPathMIPS* slow_path =
1284 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1285 codegen_->AddSlowPath(slow_path);
1286
1287 __ LoadFromOffset(kLoadUnsignedHalfword,
1288 TMP,
1289 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001290 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291 if (successor == nullptr) {
1292 __ Bnez(TMP, slow_path->GetEntryLabel());
1293 __ Bind(slow_path->GetReturnLabel());
1294 } else {
1295 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1296 __ B(slow_path->GetEntryLabel());
1297 // slow_path will return to GetLabelOf(successor).
1298 }
1299}
1300
1301InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1302 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001303 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 assembler_(codegen->GetAssembler()),
1305 codegen_(codegen) {}
1306
1307void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1308 DCHECK_EQ(instruction->InputCount(), 2U);
1309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1310 Primitive::Type type = instruction->GetResultType();
1311 switch (type) {
1312 case Primitive::kPrimInt: {
1313 locations->SetInAt(0, Location::RequiresRegister());
1314 HInstruction* right = instruction->InputAt(1);
1315 bool can_use_imm = false;
1316 if (right->IsConstant()) {
1317 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1318 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1319 can_use_imm = IsUint<16>(imm);
1320 } else if (instruction->IsAdd()) {
1321 can_use_imm = IsInt<16>(imm);
1322 } else {
1323 DCHECK(instruction->IsSub());
1324 can_use_imm = IsInt<16>(-imm);
1325 }
1326 }
1327 if (can_use_imm)
1328 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1329 else
1330 locations->SetInAt(1, Location::RequiresRegister());
1331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1332 break;
1333 }
1334
1335 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001337 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001339 break;
1340 }
1341
1342 case Primitive::kPrimFloat:
1343 case Primitive::kPrimDouble:
1344 DCHECK(instruction->IsAdd() || instruction->IsSub());
1345 locations->SetInAt(0, Location::RequiresFpuRegister());
1346 locations->SetInAt(1, Location::RequiresFpuRegister());
1347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1348 break;
1349
1350 default:
1351 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1352 }
1353}
1354
1355void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1356 Primitive::Type type = instruction->GetType();
1357 LocationSummary* locations = instruction->GetLocations();
1358
1359 switch (type) {
1360 case Primitive::kPrimInt: {
1361 Register dst = locations->Out().AsRegister<Register>();
1362 Register lhs = locations->InAt(0).AsRegister<Register>();
1363 Location rhs_location = locations->InAt(1);
1364
1365 Register rhs_reg = ZERO;
1366 int32_t rhs_imm = 0;
1367 bool use_imm = rhs_location.IsConstant();
1368 if (use_imm) {
1369 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1370 } else {
1371 rhs_reg = rhs_location.AsRegister<Register>();
1372 }
1373
1374 if (instruction->IsAnd()) {
1375 if (use_imm)
1376 __ Andi(dst, lhs, rhs_imm);
1377 else
1378 __ And(dst, lhs, rhs_reg);
1379 } else if (instruction->IsOr()) {
1380 if (use_imm)
1381 __ Ori(dst, lhs, rhs_imm);
1382 else
1383 __ Or(dst, lhs, rhs_reg);
1384 } else if (instruction->IsXor()) {
1385 if (use_imm)
1386 __ Xori(dst, lhs, rhs_imm);
1387 else
1388 __ Xor(dst, lhs, rhs_reg);
1389 } else if (instruction->IsAdd()) {
1390 if (use_imm)
1391 __ Addiu(dst, lhs, rhs_imm);
1392 else
1393 __ Addu(dst, lhs, rhs_reg);
1394 } else {
1395 DCHECK(instruction->IsSub());
1396 if (use_imm)
1397 __ Addiu(dst, lhs, -rhs_imm);
1398 else
1399 __ Subu(dst, lhs, rhs_reg);
1400 }
1401 break;
1402 }
1403
1404 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1406 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1407 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1408 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001409 Location rhs_location = locations->InAt(1);
1410 bool use_imm = rhs_location.IsConstant();
1411 if (!use_imm) {
1412 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1413 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1414 if (instruction->IsAnd()) {
1415 __ And(dst_low, lhs_low, rhs_low);
1416 __ And(dst_high, lhs_high, rhs_high);
1417 } else if (instruction->IsOr()) {
1418 __ Or(dst_low, lhs_low, rhs_low);
1419 __ Or(dst_high, lhs_high, rhs_high);
1420 } else if (instruction->IsXor()) {
1421 __ Xor(dst_low, lhs_low, rhs_low);
1422 __ Xor(dst_high, lhs_high, rhs_high);
1423 } else if (instruction->IsAdd()) {
1424 if (lhs_low == rhs_low) {
1425 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1426 __ Slt(TMP, lhs_low, ZERO);
1427 __ Addu(dst_low, lhs_low, rhs_low);
1428 } else {
1429 __ Addu(dst_low, lhs_low, rhs_low);
1430 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1431 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1432 }
1433 __ Addu(dst_high, lhs_high, rhs_high);
1434 __ Addu(dst_high, dst_high, TMP);
1435 } else {
1436 DCHECK(instruction->IsSub());
1437 __ Sltu(TMP, lhs_low, rhs_low);
1438 __ Subu(dst_low, lhs_low, rhs_low);
1439 __ Subu(dst_high, lhs_high, rhs_high);
1440 __ Subu(dst_high, dst_high, TMP);
1441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001442 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001443 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1444 if (instruction->IsOr()) {
1445 uint32_t low = Low32Bits(value);
1446 uint32_t high = High32Bits(value);
1447 if (IsUint<16>(low)) {
1448 if (dst_low != lhs_low || low != 0) {
1449 __ Ori(dst_low, lhs_low, low);
1450 }
1451 } else {
1452 __ LoadConst32(TMP, low);
1453 __ Or(dst_low, lhs_low, TMP);
1454 }
1455 if (IsUint<16>(high)) {
1456 if (dst_high != lhs_high || high != 0) {
1457 __ Ori(dst_high, lhs_high, high);
1458 }
1459 } else {
1460 if (high != low) {
1461 __ LoadConst32(TMP, high);
1462 }
1463 __ Or(dst_high, lhs_high, TMP);
1464 }
1465 } else if (instruction->IsXor()) {
1466 uint32_t low = Low32Bits(value);
1467 uint32_t high = High32Bits(value);
1468 if (IsUint<16>(low)) {
1469 if (dst_low != lhs_low || low != 0) {
1470 __ Xori(dst_low, lhs_low, low);
1471 }
1472 } else {
1473 __ LoadConst32(TMP, low);
1474 __ Xor(dst_low, lhs_low, TMP);
1475 }
1476 if (IsUint<16>(high)) {
1477 if (dst_high != lhs_high || high != 0) {
1478 __ Xori(dst_high, lhs_high, high);
1479 }
1480 } else {
1481 if (high != low) {
1482 __ LoadConst32(TMP, high);
1483 }
1484 __ Xor(dst_high, lhs_high, TMP);
1485 }
1486 } else if (instruction->IsAnd()) {
1487 uint32_t low = Low32Bits(value);
1488 uint32_t high = High32Bits(value);
1489 if (IsUint<16>(low)) {
1490 __ Andi(dst_low, lhs_low, low);
1491 } else if (low != 0xFFFFFFFF) {
1492 __ LoadConst32(TMP, low);
1493 __ And(dst_low, lhs_low, TMP);
1494 } else if (dst_low != lhs_low) {
1495 __ Move(dst_low, lhs_low);
1496 }
1497 if (IsUint<16>(high)) {
1498 __ Andi(dst_high, lhs_high, high);
1499 } else if (high != 0xFFFFFFFF) {
1500 if (high != low) {
1501 __ LoadConst32(TMP, high);
1502 }
1503 __ And(dst_high, lhs_high, TMP);
1504 } else if (dst_high != lhs_high) {
1505 __ Move(dst_high, lhs_high);
1506 }
1507 } else {
1508 if (instruction->IsSub()) {
1509 value = -value;
1510 } else {
1511 DCHECK(instruction->IsAdd());
1512 }
1513 int32_t low = Low32Bits(value);
1514 int32_t high = High32Bits(value);
1515 if (IsInt<16>(low)) {
1516 if (dst_low != lhs_low || low != 0) {
1517 __ Addiu(dst_low, lhs_low, low);
1518 }
1519 if (low != 0) {
1520 __ Sltiu(AT, dst_low, low);
1521 }
1522 } else {
1523 __ LoadConst32(TMP, low);
1524 __ Addu(dst_low, lhs_low, TMP);
1525 __ Sltu(AT, dst_low, TMP);
1526 }
1527 if (IsInt<16>(high)) {
1528 if (dst_high != lhs_high || high != 0) {
1529 __ Addiu(dst_high, lhs_high, high);
1530 }
1531 } else {
1532 if (high != low) {
1533 __ LoadConst32(TMP, high);
1534 }
1535 __ Addu(dst_high, lhs_high, TMP);
1536 }
1537 if (low != 0) {
1538 __ Addu(dst_high, dst_high, AT);
1539 }
1540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001541 }
1542 break;
1543 }
1544
1545 case Primitive::kPrimFloat:
1546 case Primitive::kPrimDouble: {
1547 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1548 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1549 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1550 if (instruction->IsAdd()) {
1551 if (type == Primitive::kPrimFloat) {
1552 __ AddS(dst, lhs, rhs);
1553 } else {
1554 __ AddD(dst, lhs, rhs);
1555 }
1556 } else {
1557 DCHECK(instruction->IsSub());
1558 if (type == Primitive::kPrimFloat) {
1559 __ SubS(dst, lhs, rhs);
1560 } else {
1561 __ SubD(dst, lhs, rhs);
1562 }
1563 }
1564 break;
1565 }
1566
1567 default:
1568 LOG(FATAL) << "Unexpected binary operation type " << type;
1569 }
1570}
1571
1572void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001573 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001574
1575 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1576 Primitive::Type type = instr->GetResultType();
1577 switch (type) {
1578 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001579 locations->SetInAt(0, Location::RequiresRegister());
1580 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1581 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1582 break;
1583 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1586 locations->SetOut(Location::RequiresRegister());
1587 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001588 default:
1589 LOG(FATAL) << "Unexpected shift type " << type;
1590 }
1591}
1592
1593static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1594
1595void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001596 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001597 LocationSummary* locations = instr->GetLocations();
1598 Primitive::Type type = instr->GetType();
1599
1600 Location rhs_location = locations->InAt(1);
1601 bool use_imm = rhs_location.IsConstant();
1602 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1603 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001604 const uint32_t shift_mask =
1605 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001606 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001607 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1608 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001609
1610 switch (type) {
1611 case Primitive::kPrimInt: {
1612 Register dst = locations->Out().AsRegister<Register>();
1613 Register lhs = locations->InAt(0).AsRegister<Register>();
1614 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001615 if (shift_value == 0) {
1616 if (dst != lhs) {
1617 __ Move(dst, lhs);
1618 }
1619 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001620 __ Sll(dst, lhs, shift_value);
1621 } else if (instr->IsShr()) {
1622 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001623 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001624 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001625 } else {
1626 if (has_ins_rotr) {
1627 __ Rotr(dst, lhs, shift_value);
1628 } else {
1629 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1630 __ Srl(dst, lhs, shift_value);
1631 __ Or(dst, dst, TMP);
1632 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001633 }
1634 } else {
1635 if (instr->IsShl()) {
1636 __ Sllv(dst, lhs, rhs_reg);
1637 } else if (instr->IsShr()) {
1638 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001639 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001640 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001641 } else {
1642 if (has_ins_rotr) {
1643 __ Rotrv(dst, lhs, rhs_reg);
1644 } else {
1645 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001646 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1647 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1648 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1649 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1650 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001651 __ Sllv(TMP, lhs, TMP);
1652 __ Srlv(dst, lhs, rhs_reg);
1653 __ Or(dst, dst, TMP);
1654 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001655 }
1656 }
1657 break;
1658 }
1659
1660 case Primitive::kPrimLong: {
1661 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1662 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1663 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1664 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1665 if (use_imm) {
1666 if (shift_value == 0) {
1667 codegen_->Move64(locations->Out(), locations->InAt(0));
1668 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001669 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001670 if (instr->IsShl()) {
1671 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1672 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1673 __ Sll(dst_low, lhs_low, shift_value);
1674 } else if (instr->IsShr()) {
1675 __ Srl(dst_low, lhs_low, shift_value);
1676 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1677 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 } else if (instr->IsUShr()) {
1679 __ Srl(dst_low, lhs_low, shift_value);
1680 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1681 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001682 } else {
1683 __ Srl(dst_low, lhs_low, shift_value);
1684 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1685 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001686 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001688 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001689 if (instr->IsShl()) {
1690 __ Sll(dst_low, lhs_low, shift_value);
1691 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1692 __ Sll(dst_high, lhs_high, shift_value);
1693 __ Or(dst_high, dst_high, TMP);
1694 } else if (instr->IsShr()) {
1695 __ Sra(dst_high, lhs_high, shift_value);
1696 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1697 __ Srl(dst_low, lhs_low, shift_value);
1698 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001699 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001700 __ Srl(dst_high, lhs_high, shift_value);
1701 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1702 __ Srl(dst_low, lhs_low, shift_value);
1703 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001704 } else {
1705 __ Srl(TMP, lhs_low, shift_value);
1706 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1707 __ Or(dst_low, dst_low, TMP);
1708 __ Srl(TMP, lhs_high, shift_value);
1709 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1710 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001711 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001712 }
1713 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001714 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001715 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001716 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001717 __ Move(dst_low, ZERO);
1718 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001719 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001720 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001721 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001722 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001723 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001724 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001725 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001726 // 64-bit rotation by 32 is just a swap.
1727 __ Move(dst_low, lhs_high);
1728 __ Move(dst_high, lhs_low);
1729 } else {
1730 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001731 __ Srl(dst_low, lhs_high, shift_value_high);
1732 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1733 __ Srl(dst_high, lhs_low, shift_value_high);
1734 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001735 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001736 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1737 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001738 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001739 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1740 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001741 __ Or(dst_high, dst_high, TMP);
1742 }
1743 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001744 }
1745 }
1746 } else {
1747 MipsLabel done;
1748 if (instr->IsShl()) {
1749 __ Sllv(dst_low, lhs_low, rhs_reg);
1750 __ Nor(AT, ZERO, rhs_reg);
1751 __ Srl(TMP, lhs_low, 1);
1752 __ Srlv(TMP, TMP, AT);
1753 __ Sllv(dst_high, lhs_high, rhs_reg);
1754 __ Or(dst_high, dst_high, TMP);
1755 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1756 __ Beqz(TMP, &done);
1757 __ Move(dst_high, dst_low);
1758 __ Move(dst_low, ZERO);
1759 } else if (instr->IsShr()) {
1760 __ Srav(dst_high, lhs_high, rhs_reg);
1761 __ Nor(AT, ZERO, rhs_reg);
1762 __ Sll(TMP, lhs_high, 1);
1763 __ Sllv(TMP, TMP, AT);
1764 __ Srlv(dst_low, lhs_low, rhs_reg);
1765 __ Or(dst_low, dst_low, TMP);
1766 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1767 __ Beqz(TMP, &done);
1768 __ Move(dst_low, dst_high);
1769 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001770 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001771 __ Srlv(dst_high, lhs_high, rhs_reg);
1772 __ Nor(AT, ZERO, rhs_reg);
1773 __ Sll(TMP, lhs_high, 1);
1774 __ Sllv(TMP, TMP, AT);
1775 __ Srlv(dst_low, lhs_low, rhs_reg);
1776 __ Or(dst_low, dst_low, TMP);
1777 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1778 __ Beqz(TMP, &done);
1779 __ Move(dst_low, dst_high);
1780 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001781 } else {
1782 __ Nor(AT, ZERO, rhs_reg);
1783 __ Srlv(TMP, lhs_low, rhs_reg);
1784 __ Sll(dst_low, lhs_high, 1);
1785 __ Sllv(dst_low, dst_low, AT);
1786 __ Or(dst_low, dst_low, TMP);
1787 __ Srlv(TMP, lhs_high, rhs_reg);
1788 __ Sll(dst_high, lhs_low, 1);
1789 __ Sllv(dst_high, dst_high, AT);
1790 __ Or(dst_high, dst_high, TMP);
1791 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1792 __ Beqz(TMP, &done);
1793 __ Move(TMP, dst_high);
1794 __ Move(dst_high, dst_low);
1795 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001796 }
1797 __ Bind(&done);
1798 }
1799 break;
1800 }
1801
1802 default:
1803 LOG(FATAL) << "Unexpected shift operation type " << type;
1804 }
1805}
1806
1807void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1808 HandleBinaryOp(instruction);
1809}
1810
1811void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1812 HandleBinaryOp(instruction);
1813}
1814
1815void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1816 HandleBinaryOp(instruction);
1817}
1818
1819void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1820 HandleBinaryOp(instruction);
1821}
1822
1823void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1828 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1829 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1830 } else {
1831 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1832 }
1833}
1834
Alexey Frunze2923db72016-08-20 01:55:47 -07001835auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1836 auto null_checker = [this, instruction]() {
1837 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1838 };
1839 return null_checker;
1840}
1841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001842void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1843 LocationSummary* locations = instruction->GetLocations();
1844 Register obj = locations->InAt(0).AsRegister<Register>();
1845 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001846 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001847 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001849 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850 switch (type) {
1851 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 Register out = locations->Out().AsRegister<Register>();
1853 if (index.IsConstant()) {
1854 size_t offset =
1855 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001856 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 } else {
1858 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001859 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001860 }
1861 break;
1862 }
1863
1864 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 Register out = locations->Out().AsRegister<Register>();
1866 if (index.IsConstant()) {
1867 size_t offset =
1868 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001869 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001870 } else {
1871 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001872 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873 }
1874 break;
1875 }
1876
1877 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878 Register out = locations->Out().AsRegister<Register>();
1879 if (index.IsConstant()) {
1880 size_t offset =
1881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001882 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001883 } else {
1884 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1885 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001886 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 }
1888 break;
1889 }
1890
1891 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001892 Register out = locations->Out().AsRegister<Register>();
1893 if (index.IsConstant()) {
1894 size_t offset =
1895 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001896 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001897 } else {
1898 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1899 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001900 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 }
1902 break;
1903 }
1904
1905 case Primitive::kPrimInt:
1906 case Primitive::kPrimNot: {
1907 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908 Register out = locations->Out().AsRegister<Register>();
1909 if (index.IsConstant()) {
1910 size_t offset =
1911 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001912 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001913 } else {
1914 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1915 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001916 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917 }
1918 break;
1919 }
1920
1921 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001922 Register out = locations->Out().AsRegisterPairLow<Register>();
1923 if (index.IsConstant()) {
1924 size_t offset =
1925 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001926 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001927 } else {
1928 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1929 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001930 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001931 }
1932 break;
1933 }
1934
1935 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001936 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1937 if (index.IsConstant()) {
1938 size_t offset =
1939 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001940 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941 } else {
1942 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1943 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001944 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 }
1946 break;
1947 }
1948
1949 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001950 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1951 if (index.IsConstant()) {
1952 size_t offset =
1953 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001954 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001955 } else {
1956 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1957 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001958 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959 }
1960 break;
1961 }
1962
1963 case Primitive::kPrimVoid:
1964 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1965 UNREACHABLE();
1966 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001967}
1968
1969void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1973}
1974
1975void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1976 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001977 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978 Register obj = locations->InAt(0).AsRegister<Register>();
1979 Register out = locations->Out().AsRegister<Register>();
1980 __ LoadFromOffset(kLoadWord, out, obj, offset);
1981 codegen_->MaybeRecordImplicitNullCheck(instruction);
1982}
1983
Alexey Frunzef58b2482016-09-02 22:14:06 -07001984Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1985 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1986 ? Location::ConstantLocation(instruction->AsConstant())
1987 : Location::RequiresRegister();
1988}
1989
1990Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1991 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1992 // We can store a non-zero float or double constant without first loading it into the FPU,
1993 // but we should only prefer this if the constant has a single use.
1994 if (instruction->IsConstant() &&
1995 (instruction->AsConstant()->IsZeroBitPattern() ||
1996 instruction->GetUses().HasExactlyOneElement())) {
1997 return Location::ConstantLocation(instruction->AsConstant());
1998 // Otherwise fall through and require an FPU register for the constant.
1999 }
2000 return Location::RequiresFpuRegister();
2001}
2002
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002004 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2006 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002007 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002008 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009 InvokeRuntimeCallingConvention calling_convention;
2010 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2011 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2012 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2013 } else {
2014 locations->SetInAt(0, Location::RequiresRegister());
2015 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2016 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002017 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002019 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002020 }
2021 }
2022}
2023
2024void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2025 LocationSummary* locations = instruction->GetLocations();
2026 Register obj = locations->InAt(0).AsRegister<Register>();
2027 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002028 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 Primitive::Type value_type = instruction->GetComponentType();
2030 bool needs_runtime_call = locations->WillCall();
2031 bool needs_write_barrier =
2032 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002033 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002034 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035
2036 switch (value_type) {
2037 case Primitive::kPrimBoolean:
2038 case Primitive::kPrimByte: {
2039 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002041 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002043 __ Addu(base_reg, obj, index.AsRegister<Register>());
2044 }
2045 if (value_location.IsConstant()) {
2046 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2047 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2048 } else {
2049 Register value = value_location.AsRegister<Register>();
2050 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002051 }
2052 break;
2053 }
2054
2055 case Primitive::kPrimShort:
2056 case Primitive::kPrimChar: {
2057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002059 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002061 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2062 __ Addu(base_reg, obj, base_reg);
2063 }
2064 if (value_location.IsConstant()) {
2065 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2066 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2067 } else {
2068 Register value = value_location.AsRegister<Register>();
2069 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002070 }
2071 break;
2072 }
2073
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimNot: {
2076 if (!needs_runtime_call) {
2077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002079 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002080 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002081 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2082 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002083 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002084 if (value_location.IsConstant()) {
2085 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2086 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2087 DCHECK(!needs_write_barrier);
2088 } else {
2089 Register value = value_location.AsRegister<Register>();
2090 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2091 if (needs_write_barrier) {
2092 DCHECK_EQ(value_type, Primitive::kPrimNot);
2093 codegen_->MarkGCCard(obj, value);
2094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095 }
2096 } else {
2097 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002098 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2100 }
2101 break;
2102 }
2103
2104 case Primitive::kPrimLong: {
2105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002107 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002108 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002109 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2110 __ Addu(base_reg, obj, base_reg);
2111 }
2112 if (value_location.IsConstant()) {
2113 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2114 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2115 } else {
2116 Register value = value_location.AsRegisterPairLow<Register>();
2117 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 }
2119 break;
2120 }
2121
2122 case Primitive::kPrimFloat: {
2123 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002125 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002126 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002127 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2128 __ Addu(base_reg, obj, base_reg);
2129 }
2130 if (value_location.IsConstant()) {
2131 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2132 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2133 } else {
2134 FRegister value = value_location.AsFpuRegister<FRegister>();
2135 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 }
2137 break;
2138 }
2139
2140 case Primitive::kPrimDouble: {
2141 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002143 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002145 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2146 __ Addu(base_reg, obj, base_reg);
2147 }
2148 if (value_location.IsConstant()) {
2149 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2150 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2151 } else {
2152 FRegister value = value_location.AsFpuRegister<FRegister>();
2153 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002154 }
2155 break;
2156 }
2157
2158 case Primitive::kPrimVoid:
2159 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2160 UNREACHABLE();
2161 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002162}
2163
2164void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002165 RegisterSet caller_saves = RegisterSet::Empty();
2166 InvokeRuntimeCallingConvention calling_convention;
2167 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2168 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2169 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170 locations->SetInAt(0, Location::RequiresRegister());
2171 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002172}
2173
2174void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2175 LocationSummary* locations = instruction->GetLocations();
2176 BoundsCheckSlowPathMIPS* slow_path =
2177 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2178 codegen_->AddSlowPath(slow_path);
2179
2180 Register index = locations->InAt(0).AsRegister<Register>();
2181 Register length = locations->InAt(1).AsRegister<Register>();
2182
2183 // length is limited by the maximum positive signed 32-bit integer.
2184 // Unsigned comparison of length and index checks for index < 0
2185 // and for length <= index simultaneously.
2186 __ Bgeu(index, length, slow_path->GetEntryLabel());
2187}
2188
2189void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2190 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2191 instruction,
2192 LocationSummary::kCallOnSlowPath);
2193 locations->SetInAt(0, Location::RequiresRegister());
2194 locations->SetInAt(1, Location::RequiresRegister());
2195 // Note that TypeCheckSlowPathMIPS uses this register too.
2196 locations->AddTemp(Location::RequiresRegister());
2197}
2198
2199void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2200 LocationSummary* locations = instruction->GetLocations();
2201 Register obj = locations->InAt(0).AsRegister<Register>();
2202 Register cls = locations->InAt(1).AsRegister<Register>();
2203 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2204
2205 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2206 codegen_->AddSlowPath(slow_path);
2207
2208 // TODO: avoid this check if we know obj is not null.
2209 __ Beqz(obj, slow_path->GetExitLabel());
2210 // Compare the class of `obj` with `cls`.
2211 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2212 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2213 __ Bind(slow_path->GetExitLabel());
2214}
2215
2216void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2217 LocationSummary* locations =
2218 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2219 locations->SetInAt(0, Location::RequiresRegister());
2220 if (check->HasUses()) {
2221 locations->SetOut(Location::SameAsFirstInput());
2222 }
2223}
2224
2225void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2226 // We assume the class is not null.
2227 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2228 check->GetLoadClass(),
2229 check,
2230 check->GetDexPc(),
2231 true);
2232 codegen_->AddSlowPath(slow_path);
2233 GenerateClassInitializationCheck(slow_path,
2234 check->GetLocations()->InAt(0).AsRegister<Register>());
2235}
2236
2237void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2238 Primitive::Type in_type = compare->InputAt(0)->GetType();
2239
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002240 LocationSummary* locations =
2241 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002242
2243 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002244 case Primitive::kPrimBoolean:
2245 case Primitive::kPrimByte:
2246 case Primitive::kPrimShort:
2247 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002248 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetInAt(1, Location::RequiresRegister());
2251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252 break;
2253
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 case Primitive::kPrimLong:
2255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, Location::RequiresRegister());
2257 // Output overlaps because it is written before doing the low comparison.
2258 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2259 break;
2260
2261 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002262 case Primitive::kPrimDouble:
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
2264 locations->SetInAt(1, Location::RequiresFpuRegister());
2265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002266 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267
2268 default:
2269 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2270 }
2271}
2272
2273void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2274 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002275 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002277 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278
2279 // 0 if: left == right
2280 // 1 if: left > right
2281 // -1 if: left < right
2282 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002283 case Primitive::kPrimBoolean:
2284 case Primitive::kPrimByte:
2285 case Primitive::kPrimShort:
2286 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002287 case Primitive::kPrimInt: {
2288 Register lhs = locations->InAt(0).AsRegister<Register>();
2289 Register rhs = locations->InAt(1).AsRegister<Register>();
2290 __ Slt(TMP, lhs, rhs);
2291 __ Slt(res, rhs, lhs);
2292 __ Subu(res, res, TMP);
2293 break;
2294 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 case Primitive::kPrimLong: {
2296 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2298 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2299 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2300 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2301 // TODO: more efficient (direct) comparison with a constant.
2302 __ Slt(TMP, lhs_high, rhs_high);
2303 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2304 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2305 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2306 __ Sltu(TMP, lhs_low, rhs_low);
2307 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2308 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2309 __ Bind(&done);
2310 break;
2311 }
2312
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002313 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002314 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002315 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2316 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2317 MipsLabel done;
2318 if (isR6) {
2319 __ CmpEqS(FTMP, lhs, rhs);
2320 __ LoadConst32(res, 0);
2321 __ Bc1nez(FTMP, &done);
2322 if (gt_bias) {
2323 __ CmpLtS(FTMP, lhs, rhs);
2324 __ LoadConst32(res, -1);
2325 __ Bc1nez(FTMP, &done);
2326 __ LoadConst32(res, 1);
2327 } else {
2328 __ CmpLtS(FTMP, rhs, lhs);
2329 __ LoadConst32(res, 1);
2330 __ Bc1nez(FTMP, &done);
2331 __ LoadConst32(res, -1);
2332 }
2333 } else {
2334 if (gt_bias) {
2335 __ ColtS(0, lhs, rhs);
2336 __ LoadConst32(res, -1);
2337 __ Bc1t(0, &done);
2338 __ CeqS(0, lhs, rhs);
2339 __ LoadConst32(res, 1);
2340 __ Movt(res, ZERO, 0);
2341 } else {
2342 __ ColtS(0, rhs, lhs);
2343 __ LoadConst32(res, 1);
2344 __ Bc1t(0, &done);
2345 __ CeqS(0, lhs, rhs);
2346 __ LoadConst32(res, -1);
2347 __ Movt(res, ZERO, 0);
2348 }
2349 }
2350 __ Bind(&done);
2351 break;
2352 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002354 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002355 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2356 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2357 MipsLabel done;
2358 if (isR6) {
2359 __ CmpEqD(FTMP, lhs, rhs);
2360 __ LoadConst32(res, 0);
2361 __ Bc1nez(FTMP, &done);
2362 if (gt_bias) {
2363 __ CmpLtD(FTMP, lhs, rhs);
2364 __ LoadConst32(res, -1);
2365 __ Bc1nez(FTMP, &done);
2366 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002368 __ CmpLtD(FTMP, rhs, lhs);
2369 __ LoadConst32(res, 1);
2370 __ Bc1nez(FTMP, &done);
2371 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 }
2373 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002374 if (gt_bias) {
2375 __ ColtD(0, lhs, rhs);
2376 __ LoadConst32(res, -1);
2377 __ Bc1t(0, &done);
2378 __ CeqD(0, lhs, rhs);
2379 __ LoadConst32(res, 1);
2380 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002381 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002382 __ ColtD(0, rhs, lhs);
2383 __ LoadConst32(res, 1);
2384 __ Bc1t(0, &done);
2385 __ CeqD(0, lhs, rhs);
2386 __ LoadConst32(res, -1);
2387 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002388 }
2389 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002390 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002391 break;
2392 }
2393
2394 default:
2395 LOG(FATAL) << "Unimplemented compare type " << in_type;
2396 }
2397}
2398
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002399void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002401 switch (instruction->InputAt(0)->GetType()) {
2402 default:
2403 case Primitive::kPrimLong:
2404 locations->SetInAt(0, Location::RequiresRegister());
2405 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2406 break;
2407
2408 case Primitive::kPrimFloat:
2409 case Primitive::kPrimDouble:
2410 locations->SetInAt(0, Location::RequiresFpuRegister());
2411 locations->SetInAt(1, Location::RequiresFpuRegister());
2412 break;
2413 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002414 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2416 }
2417}
2418
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002419void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002420 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 return;
2422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002424 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 LocationSummary* locations = instruction->GetLocations();
2426 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002427 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002428
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002429 switch (type) {
2430 default:
2431 // Integer case.
2432 GenerateIntCompare(instruction->GetCondition(), locations);
2433 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002435 case Primitive::kPrimLong:
2436 // TODO: don't use branches.
2437 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 break;
2439
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002440 case Primitive::kPrimFloat:
2441 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002442 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2443 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002444 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002445
2446 // Convert the branches into the result.
2447 MipsLabel done;
2448
2449 // False case: result = 0.
2450 __ LoadConst32(dst, 0);
2451 __ B(&done);
2452
2453 // True case: result = 1.
2454 __ Bind(&true_label);
2455 __ LoadConst32(dst, 1);
2456 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002457}
2458
Alexey Frunze7e99e052015-11-24 19:28:01 -08002459void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2460 DCHECK(instruction->IsDiv() || instruction->IsRem());
2461 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2462
2463 LocationSummary* locations = instruction->GetLocations();
2464 Location second = locations->InAt(1);
2465 DCHECK(second.IsConstant());
2466
2467 Register out = locations->Out().AsRegister<Register>();
2468 Register dividend = locations->InAt(0).AsRegister<Register>();
2469 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2470 DCHECK(imm == 1 || imm == -1);
2471
2472 if (instruction->IsRem()) {
2473 __ Move(out, ZERO);
2474 } else {
2475 if (imm == -1) {
2476 __ Subu(out, ZERO, dividend);
2477 } else if (out != dividend) {
2478 __ Move(out, dividend);
2479 }
2480 }
2481}
2482
2483void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2484 DCHECK(instruction->IsDiv() || instruction->IsRem());
2485 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2486
2487 LocationSummary* locations = instruction->GetLocations();
2488 Location second = locations->InAt(1);
2489 DCHECK(second.IsConstant());
2490
2491 Register out = locations->Out().AsRegister<Register>();
2492 Register dividend = locations->InAt(0).AsRegister<Register>();
2493 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002494 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002495 int ctz_imm = CTZ(abs_imm);
2496
2497 if (instruction->IsDiv()) {
2498 if (ctz_imm == 1) {
2499 // Fast path for division by +/-2, which is very common.
2500 __ Srl(TMP, dividend, 31);
2501 } else {
2502 __ Sra(TMP, dividend, 31);
2503 __ Srl(TMP, TMP, 32 - ctz_imm);
2504 }
2505 __ Addu(out, dividend, TMP);
2506 __ Sra(out, out, ctz_imm);
2507 if (imm < 0) {
2508 __ Subu(out, ZERO, out);
2509 }
2510 } else {
2511 if (ctz_imm == 1) {
2512 // Fast path for modulo +/-2, which is very common.
2513 __ Sra(TMP, dividend, 31);
2514 __ Subu(out, dividend, TMP);
2515 __ Andi(out, out, 1);
2516 __ Addu(out, out, TMP);
2517 } else {
2518 __ Sra(TMP, dividend, 31);
2519 __ Srl(TMP, TMP, 32 - ctz_imm);
2520 __ Addu(out, dividend, TMP);
2521 if (IsUint<16>(abs_imm - 1)) {
2522 __ Andi(out, out, abs_imm - 1);
2523 } else {
2524 __ Sll(out, out, 32 - ctz_imm);
2525 __ Srl(out, out, 32 - ctz_imm);
2526 }
2527 __ Subu(out, out, TMP);
2528 }
2529 }
2530}
2531
2532void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2533 DCHECK(instruction->IsDiv() || instruction->IsRem());
2534 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2535
2536 LocationSummary* locations = instruction->GetLocations();
2537 Location second = locations->InAt(1);
2538 DCHECK(second.IsConstant());
2539
2540 Register out = locations->Out().AsRegister<Register>();
2541 Register dividend = locations->InAt(0).AsRegister<Register>();
2542 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2543
2544 int64_t magic;
2545 int shift;
2546 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2547
2548 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2549
2550 __ LoadConst32(TMP, magic);
2551 if (isR6) {
2552 __ MuhR6(TMP, dividend, TMP);
2553 } else {
2554 __ MultR2(dividend, TMP);
2555 __ Mfhi(TMP);
2556 }
2557 if (imm > 0 && magic < 0) {
2558 __ Addu(TMP, TMP, dividend);
2559 } else if (imm < 0 && magic > 0) {
2560 __ Subu(TMP, TMP, dividend);
2561 }
2562
2563 if (shift != 0) {
2564 __ Sra(TMP, TMP, shift);
2565 }
2566
2567 if (instruction->IsDiv()) {
2568 __ Sra(out, TMP, 31);
2569 __ Subu(out, TMP, out);
2570 } else {
2571 __ Sra(AT, TMP, 31);
2572 __ Subu(AT, TMP, AT);
2573 __ LoadConst32(TMP, imm);
2574 if (isR6) {
2575 __ MulR6(TMP, AT, TMP);
2576 } else {
2577 __ MulR2(TMP, AT, TMP);
2578 }
2579 __ Subu(out, dividend, TMP);
2580 }
2581}
2582
2583void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2584 DCHECK(instruction->IsDiv() || instruction->IsRem());
2585 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2586
2587 LocationSummary* locations = instruction->GetLocations();
2588 Register out = locations->Out().AsRegister<Register>();
2589 Location second = locations->InAt(1);
2590
2591 if (second.IsConstant()) {
2592 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2593 if (imm == 0) {
2594 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2595 } else if (imm == 1 || imm == -1) {
2596 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002597 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002598 DivRemByPowerOfTwo(instruction);
2599 } else {
2600 DCHECK(imm <= -2 || imm >= 2);
2601 GenerateDivRemWithAnyConstant(instruction);
2602 }
2603 } else {
2604 Register dividend = locations->InAt(0).AsRegister<Register>();
2605 Register divisor = second.AsRegister<Register>();
2606 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2607 if (instruction->IsDiv()) {
2608 if (isR6) {
2609 __ DivR6(out, dividend, divisor);
2610 } else {
2611 __ DivR2(out, dividend, divisor);
2612 }
2613 } else {
2614 if (isR6) {
2615 __ ModR6(out, dividend, divisor);
2616 } else {
2617 __ ModR2(out, dividend, divisor);
2618 }
2619 }
2620 }
2621}
2622
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2624 Primitive::Type type = div->GetResultType();
2625 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002626 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 : LocationSummary::kNoCall;
2628
2629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2630
2631 switch (type) {
2632 case Primitive::kPrimInt:
2633 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002634 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002635 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2636 break;
2637
2638 case Primitive::kPrimLong: {
2639 InvokeRuntimeCallingConvention calling_convention;
2640 locations->SetInAt(0, Location::RegisterPairLocation(
2641 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2642 locations->SetInAt(1, Location::RegisterPairLocation(
2643 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2644 locations->SetOut(calling_convention.GetReturnLocation(type));
2645 break;
2646 }
2647
2648 case Primitive::kPrimFloat:
2649 case Primitive::kPrimDouble:
2650 locations->SetInAt(0, Location::RequiresFpuRegister());
2651 locations->SetInAt(1, Location::RequiresFpuRegister());
2652 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2653 break;
2654
2655 default:
2656 LOG(FATAL) << "Unexpected div type " << type;
2657 }
2658}
2659
2660void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2661 Primitive::Type type = instruction->GetType();
2662 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663
2664 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002665 case Primitive::kPrimInt:
2666 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002669 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002670 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2671 break;
2672 }
2673 case Primitive::kPrimFloat:
2674 case Primitive::kPrimDouble: {
2675 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2676 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2677 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2678 if (type == Primitive::kPrimFloat) {
2679 __ DivS(dst, lhs, rhs);
2680 } else {
2681 __ DivD(dst, lhs, rhs);
2682 }
2683 break;
2684 }
2685 default:
2686 LOG(FATAL) << "Unexpected div type " << type;
2687 }
2688}
2689
2690void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002691 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002693}
2694
2695void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2696 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2697 codegen_->AddSlowPath(slow_path);
2698 Location value = instruction->GetLocations()->InAt(0);
2699 Primitive::Type type = instruction->GetType();
2700
2701 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002702 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 case Primitive::kPrimByte:
2704 case Primitive::kPrimChar:
2705 case Primitive::kPrimShort:
2706 case Primitive::kPrimInt: {
2707 if (value.IsConstant()) {
2708 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2709 __ B(slow_path->GetEntryLabel());
2710 } else {
2711 // A division by a non-null constant is valid. We don't need to perform
2712 // any check, so simply fall through.
2713 }
2714 } else {
2715 DCHECK(value.IsRegister()) << value;
2716 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2717 }
2718 break;
2719 }
2720 case Primitive::kPrimLong: {
2721 if (value.IsConstant()) {
2722 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2723 __ B(slow_path->GetEntryLabel());
2724 } else {
2725 // A division by a non-null constant is valid. We don't need to perform
2726 // any check, so simply fall through.
2727 }
2728 } else {
2729 DCHECK(value.IsRegisterPair()) << value;
2730 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2731 __ Beqz(TMP, slow_path->GetEntryLabel());
2732 }
2733 break;
2734 }
2735 default:
2736 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2737 }
2738}
2739
2740void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2741 LocationSummary* locations =
2742 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2743 locations->SetOut(Location::ConstantLocation(constant));
2744}
2745
2746void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2747 // Will be generated at use site.
2748}
2749
2750void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2751 exit->SetLocations(nullptr);
2752}
2753
2754void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2755}
2756
2757void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2758 LocationSummary* locations =
2759 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2760 locations->SetOut(Location::ConstantLocation(constant));
2761}
2762
2763void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2764 // Will be generated at use site.
2765}
2766
2767void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2768 got->SetLocations(nullptr);
2769}
2770
2771void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2772 DCHECK(!successor->IsExitBlock());
2773 HBasicBlock* block = got->GetBlock();
2774 HInstruction* previous = got->GetPrevious();
2775 HLoopInformation* info = block->GetLoopInformation();
2776
2777 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2778 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2779 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2780 return;
2781 }
2782 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2783 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2784 }
2785 if (!codegen_->GoesToNextBlock(block, successor)) {
2786 __ B(codegen_->GetLabelOf(successor));
2787 }
2788}
2789
2790void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2791 HandleGoto(got, got->GetSuccessor());
2792}
2793
2794void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2795 try_boundary->SetLocations(nullptr);
2796}
2797
2798void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2799 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2800 if (!successor->IsExitBlock()) {
2801 HandleGoto(try_boundary, successor);
2802 }
2803}
2804
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002805void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2806 LocationSummary* locations) {
2807 Register dst = locations->Out().AsRegister<Register>();
2808 Register lhs = locations->InAt(0).AsRegister<Register>();
2809 Location rhs_location = locations->InAt(1);
2810 Register rhs_reg = ZERO;
2811 int64_t rhs_imm = 0;
2812 bool use_imm = rhs_location.IsConstant();
2813 if (use_imm) {
2814 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2815 } else {
2816 rhs_reg = rhs_location.AsRegister<Register>();
2817 }
2818
2819 switch (cond) {
2820 case kCondEQ:
2821 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002822 if (use_imm && IsInt<16>(-rhs_imm)) {
2823 if (rhs_imm == 0) {
2824 if (cond == kCondEQ) {
2825 __ Sltiu(dst, lhs, 1);
2826 } else {
2827 __ Sltu(dst, ZERO, lhs);
2828 }
2829 } else {
2830 __ Addiu(dst, lhs, -rhs_imm);
2831 if (cond == kCondEQ) {
2832 __ Sltiu(dst, dst, 1);
2833 } else {
2834 __ Sltu(dst, ZERO, dst);
2835 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002836 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002837 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002838 if (use_imm && IsUint<16>(rhs_imm)) {
2839 __ Xori(dst, lhs, rhs_imm);
2840 } else {
2841 if (use_imm) {
2842 rhs_reg = TMP;
2843 __ LoadConst32(rhs_reg, rhs_imm);
2844 }
2845 __ Xor(dst, lhs, rhs_reg);
2846 }
2847 if (cond == kCondEQ) {
2848 __ Sltiu(dst, dst, 1);
2849 } else {
2850 __ Sltu(dst, ZERO, dst);
2851 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002852 }
2853 break;
2854
2855 case kCondLT:
2856 case kCondGE:
2857 if (use_imm && IsInt<16>(rhs_imm)) {
2858 __ Slti(dst, lhs, rhs_imm);
2859 } else {
2860 if (use_imm) {
2861 rhs_reg = TMP;
2862 __ LoadConst32(rhs_reg, rhs_imm);
2863 }
2864 __ Slt(dst, lhs, rhs_reg);
2865 }
2866 if (cond == kCondGE) {
2867 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2868 // only the slt instruction but no sge.
2869 __ Xori(dst, dst, 1);
2870 }
2871 break;
2872
2873 case kCondLE:
2874 case kCondGT:
2875 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2876 // Simulate lhs <= rhs via lhs < rhs + 1.
2877 __ Slti(dst, lhs, rhs_imm + 1);
2878 if (cond == kCondGT) {
2879 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2880 // only the slti instruction but no sgti.
2881 __ Xori(dst, dst, 1);
2882 }
2883 } else {
2884 if (use_imm) {
2885 rhs_reg = TMP;
2886 __ LoadConst32(rhs_reg, rhs_imm);
2887 }
2888 __ Slt(dst, rhs_reg, lhs);
2889 if (cond == kCondLE) {
2890 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2891 // only the slt instruction but no sle.
2892 __ Xori(dst, dst, 1);
2893 }
2894 }
2895 break;
2896
2897 case kCondB:
2898 case kCondAE:
2899 if (use_imm && IsInt<16>(rhs_imm)) {
2900 // Sltiu sign-extends its 16-bit immediate operand before
2901 // the comparison and thus lets us compare directly with
2902 // unsigned values in the ranges [0, 0x7fff] and
2903 // [0xffff8000, 0xffffffff].
2904 __ Sltiu(dst, lhs, rhs_imm);
2905 } else {
2906 if (use_imm) {
2907 rhs_reg = TMP;
2908 __ LoadConst32(rhs_reg, rhs_imm);
2909 }
2910 __ Sltu(dst, lhs, rhs_reg);
2911 }
2912 if (cond == kCondAE) {
2913 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2914 // only the sltu instruction but no sgeu.
2915 __ Xori(dst, dst, 1);
2916 }
2917 break;
2918
2919 case kCondBE:
2920 case kCondA:
2921 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2922 // Simulate lhs <= rhs via lhs < rhs + 1.
2923 // Note that this only works if rhs + 1 does not overflow
2924 // to 0, hence the check above.
2925 // Sltiu sign-extends its 16-bit immediate operand before
2926 // the comparison and thus lets us compare directly with
2927 // unsigned values in the ranges [0, 0x7fff] and
2928 // [0xffff8000, 0xffffffff].
2929 __ Sltiu(dst, lhs, rhs_imm + 1);
2930 if (cond == kCondA) {
2931 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2932 // only the sltiu instruction but no sgtiu.
2933 __ Xori(dst, dst, 1);
2934 }
2935 } else {
2936 if (use_imm) {
2937 rhs_reg = TMP;
2938 __ LoadConst32(rhs_reg, rhs_imm);
2939 }
2940 __ Sltu(dst, rhs_reg, lhs);
2941 if (cond == kCondBE) {
2942 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2943 // only the sltu instruction but no sleu.
2944 __ Xori(dst, dst, 1);
2945 }
2946 }
2947 break;
2948 }
2949}
2950
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002951bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2952 LocationSummary* input_locations,
2953 Register dst) {
2954 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2955 Location rhs_location = input_locations->InAt(1);
2956 Register rhs_reg = ZERO;
2957 int64_t rhs_imm = 0;
2958 bool use_imm = rhs_location.IsConstant();
2959 if (use_imm) {
2960 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2961 } else {
2962 rhs_reg = rhs_location.AsRegister<Register>();
2963 }
2964
2965 switch (cond) {
2966 case kCondEQ:
2967 case kCondNE:
2968 if (use_imm && IsInt<16>(-rhs_imm)) {
2969 __ Addiu(dst, lhs, -rhs_imm);
2970 } else if (use_imm && IsUint<16>(rhs_imm)) {
2971 __ Xori(dst, lhs, rhs_imm);
2972 } else {
2973 if (use_imm) {
2974 rhs_reg = TMP;
2975 __ LoadConst32(rhs_reg, rhs_imm);
2976 }
2977 __ Xor(dst, lhs, rhs_reg);
2978 }
2979 return (cond == kCondEQ);
2980
2981 case kCondLT:
2982 case kCondGE:
2983 if (use_imm && IsInt<16>(rhs_imm)) {
2984 __ Slti(dst, lhs, rhs_imm);
2985 } else {
2986 if (use_imm) {
2987 rhs_reg = TMP;
2988 __ LoadConst32(rhs_reg, rhs_imm);
2989 }
2990 __ Slt(dst, lhs, rhs_reg);
2991 }
2992 return (cond == kCondGE);
2993
2994 case kCondLE:
2995 case kCondGT:
2996 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2997 // Simulate lhs <= rhs via lhs < rhs + 1.
2998 __ Slti(dst, lhs, rhs_imm + 1);
2999 return (cond == kCondGT);
3000 } else {
3001 if (use_imm) {
3002 rhs_reg = TMP;
3003 __ LoadConst32(rhs_reg, rhs_imm);
3004 }
3005 __ Slt(dst, rhs_reg, lhs);
3006 return (cond == kCondLE);
3007 }
3008
3009 case kCondB:
3010 case kCondAE:
3011 if (use_imm && IsInt<16>(rhs_imm)) {
3012 // Sltiu sign-extends its 16-bit immediate operand before
3013 // the comparison and thus lets us compare directly with
3014 // unsigned values in the ranges [0, 0x7fff] and
3015 // [0xffff8000, 0xffffffff].
3016 __ Sltiu(dst, lhs, rhs_imm);
3017 } else {
3018 if (use_imm) {
3019 rhs_reg = TMP;
3020 __ LoadConst32(rhs_reg, rhs_imm);
3021 }
3022 __ Sltu(dst, lhs, rhs_reg);
3023 }
3024 return (cond == kCondAE);
3025
3026 case kCondBE:
3027 case kCondA:
3028 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3029 // Simulate lhs <= rhs via lhs < rhs + 1.
3030 // Note that this only works if rhs + 1 does not overflow
3031 // to 0, hence the check above.
3032 // Sltiu sign-extends its 16-bit immediate operand before
3033 // the comparison and thus lets us compare directly with
3034 // unsigned values in the ranges [0, 0x7fff] and
3035 // [0xffff8000, 0xffffffff].
3036 __ Sltiu(dst, lhs, rhs_imm + 1);
3037 return (cond == kCondA);
3038 } else {
3039 if (use_imm) {
3040 rhs_reg = TMP;
3041 __ LoadConst32(rhs_reg, rhs_imm);
3042 }
3043 __ Sltu(dst, rhs_reg, lhs);
3044 return (cond == kCondBE);
3045 }
3046 }
3047}
3048
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003049void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3050 LocationSummary* locations,
3051 MipsLabel* label) {
3052 Register lhs = locations->InAt(0).AsRegister<Register>();
3053 Location rhs_location = locations->InAt(1);
3054 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003055 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003056 bool use_imm = rhs_location.IsConstant();
3057 if (use_imm) {
3058 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3059 } else {
3060 rhs_reg = rhs_location.AsRegister<Register>();
3061 }
3062
3063 if (use_imm && rhs_imm == 0) {
3064 switch (cond) {
3065 case kCondEQ:
3066 case kCondBE: // <= 0 if zero
3067 __ Beqz(lhs, label);
3068 break;
3069 case kCondNE:
3070 case kCondA: // > 0 if non-zero
3071 __ Bnez(lhs, label);
3072 break;
3073 case kCondLT:
3074 __ Bltz(lhs, label);
3075 break;
3076 case kCondGE:
3077 __ Bgez(lhs, label);
3078 break;
3079 case kCondLE:
3080 __ Blez(lhs, label);
3081 break;
3082 case kCondGT:
3083 __ Bgtz(lhs, label);
3084 break;
3085 case kCondB: // always false
3086 break;
3087 case kCondAE: // always true
3088 __ B(label);
3089 break;
3090 }
3091 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003092 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3093 if (isR6 || !use_imm) {
3094 if (use_imm) {
3095 rhs_reg = TMP;
3096 __ LoadConst32(rhs_reg, rhs_imm);
3097 }
3098 switch (cond) {
3099 case kCondEQ:
3100 __ Beq(lhs, rhs_reg, label);
3101 break;
3102 case kCondNE:
3103 __ Bne(lhs, rhs_reg, label);
3104 break;
3105 case kCondLT:
3106 __ Blt(lhs, rhs_reg, label);
3107 break;
3108 case kCondGE:
3109 __ Bge(lhs, rhs_reg, label);
3110 break;
3111 case kCondLE:
3112 __ Bge(rhs_reg, lhs, label);
3113 break;
3114 case kCondGT:
3115 __ Blt(rhs_reg, lhs, label);
3116 break;
3117 case kCondB:
3118 __ Bltu(lhs, rhs_reg, label);
3119 break;
3120 case kCondAE:
3121 __ Bgeu(lhs, rhs_reg, label);
3122 break;
3123 case kCondBE:
3124 __ Bgeu(rhs_reg, lhs, label);
3125 break;
3126 case kCondA:
3127 __ Bltu(rhs_reg, lhs, label);
3128 break;
3129 }
3130 } else {
3131 // Special cases for more efficient comparison with constants on R2.
3132 switch (cond) {
3133 case kCondEQ:
3134 __ LoadConst32(TMP, rhs_imm);
3135 __ Beq(lhs, TMP, label);
3136 break;
3137 case kCondNE:
3138 __ LoadConst32(TMP, rhs_imm);
3139 __ Bne(lhs, TMP, label);
3140 break;
3141 case kCondLT:
3142 if (IsInt<16>(rhs_imm)) {
3143 __ Slti(TMP, lhs, rhs_imm);
3144 __ Bnez(TMP, label);
3145 } else {
3146 __ LoadConst32(TMP, rhs_imm);
3147 __ Blt(lhs, TMP, label);
3148 }
3149 break;
3150 case kCondGE:
3151 if (IsInt<16>(rhs_imm)) {
3152 __ Slti(TMP, lhs, rhs_imm);
3153 __ Beqz(TMP, label);
3154 } else {
3155 __ LoadConst32(TMP, rhs_imm);
3156 __ Bge(lhs, TMP, label);
3157 }
3158 break;
3159 case kCondLE:
3160 if (IsInt<16>(rhs_imm + 1)) {
3161 // Simulate lhs <= rhs via lhs < rhs + 1.
3162 __ Slti(TMP, lhs, rhs_imm + 1);
3163 __ Bnez(TMP, label);
3164 } else {
3165 __ LoadConst32(TMP, rhs_imm);
3166 __ Bge(TMP, lhs, label);
3167 }
3168 break;
3169 case kCondGT:
3170 if (IsInt<16>(rhs_imm + 1)) {
3171 // Simulate lhs > rhs via !(lhs < rhs + 1).
3172 __ Slti(TMP, lhs, rhs_imm + 1);
3173 __ Beqz(TMP, label);
3174 } else {
3175 __ LoadConst32(TMP, rhs_imm);
3176 __ Blt(TMP, lhs, label);
3177 }
3178 break;
3179 case kCondB:
3180 if (IsInt<16>(rhs_imm)) {
3181 __ Sltiu(TMP, lhs, rhs_imm);
3182 __ Bnez(TMP, label);
3183 } else {
3184 __ LoadConst32(TMP, rhs_imm);
3185 __ Bltu(lhs, TMP, label);
3186 }
3187 break;
3188 case kCondAE:
3189 if (IsInt<16>(rhs_imm)) {
3190 __ Sltiu(TMP, lhs, rhs_imm);
3191 __ Beqz(TMP, label);
3192 } else {
3193 __ LoadConst32(TMP, rhs_imm);
3194 __ Bgeu(lhs, TMP, label);
3195 }
3196 break;
3197 case kCondBE:
3198 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3199 // Simulate lhs <= rhs via lhs < rhs + 1.
3200 // Note that this only works if rhs + 1 does not overflow
3201 // to 0, hence the check above.
3202 __ Sltiu(TMP, lhs, rhs_imm + 1);
3203 __ Bnez(TMP, label);
3204 } else {
3205 __ LoadConst32(TMP, rhs_imm);
3206 __ Bgeu(TMP, lhs, label);
3207 }
3208 break;
3209 case kCondA:
3210 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3211 // Simulate lhs > rhs via !(lhs < rhs + 1).
3212 // Note that this only works if rhs + 1 does not overflow
3213 // to 0, hence the check above.
3214 __ Sltiu(TMP, lhs, rhs_imm + 1);
3215 __ Beqz(TMP, label);
3216 } else {
3217 __ LoadConst32(TMP, rhs_imm);
3218 __ Bltu(TMP, lhs, label);
3219 }
3220 break;
3221 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003222 }
3223 }
3224}
3225
3226void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3227 LocationSummary* locations,
3228 MipsLabel* label) {
3229 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3230 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3231 Location rhs_location = locations->InAt(1);
3232 Register rhs_high = ZERO;
3233 Register rhs_low = ZERO;
3234 int64_t imm = 0;
3235 uint32_t imm_high = 0;
3236 uint32_t imm_low = 0;
3237 bool use_imm = rhs_location.IsConstant();
3238 if (use_imm) {
3239 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3240 imm_high = High32Bits(imm);
3241 imm_low = Low32Bits(imm);
3242 } else {
3243 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3244 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3245 }
3246
3247 if (use_imm && imm == 0) {
3248 switch (cond) {
3249 case kCondEQ:
3250 case kCondBE: // <= 0 if zero
3251 __ Or(TMP, lhs_high, lhs_low);
3252 __ Beqz(TMP, label);
3253 break;
3254 case kCondNE:
3255 case kCondA: // > 0 if non-zero
3256 __ Or(TMP, lhs_high, lhs_low);
3257 __ Bnez(TMP, label);
3258 break;
3259 case kCondLT:
3260 __ Bltz(lhs_high, label);
3261 break;
3262 case kCondGE:
3263 __ Bgez(lhs_high, label);
3264 break;
3265 case kCondLE:
3266 __ Or(TMP, lhs_high, lhs_low);
3267 __ Sra(AT, lhs_high, 31);
3268 __ Bgeu(AT, TMP, label);
3269 break;
3270 case kCondGT:
3271 __ Or(TMP, lhs_high, lhs_low);
3272 __ Sra(AT, lhs_high, 31);
3273 __ Bltu(AT, TMP, label);
3274 break;
3275 case kCondB: // always false
3276 break;
3277 case kCondAE: // always true
3278 __ B(label);
3279 break;
3280 }
3281 } else if (use_imm) {
3282 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3283 switch (cond) {
3284 case kCondEQ:
3285 __ LoadConst32(TMP, imm_high);
3286 __ Xor(TMP, TMP, lhs_high);
3287 __ LoadConst32(AT, imm_low);
3288 __ Xor(AT, AT, lhs_low);
3289 __ Or(TMP, TMP, AT);
3290 __ Beqz(TMP, label);
3291 break;
3292 case kCondNE:
3293 __ LoadConst32(TMP, imm_high);
3294 __ Xor(TMP, TMP, lhs_high);
3295 __ LoadConst32(AT, imm_low);
3296 __ Xor(AT, AT, lhs_low);
3297 __ Or(TMP, TMP, AT);
3298 __ Bnez(TMP, label);
3299 break;
3300 case kCondLT:
3301 __ LoadConst32(TMP, imm_high);
3302 __ Blt(lhs_high, TMP, label);
3303 __ Slt(TMP, TMP, lhs_high);
3304 __ LoadConst32(AT, imm_low);
3305 __ Sltu(AT, lhs_low, AT);
3306 __ Blt(TMP, AT, label);
3307 break;
3308 case kCondGE:
3309 __ LoadConst32(TMP, imm_high);
3310 __ Blt(TMP, lhs_high, label);
3311 __ Slt(TMP, lhs_high, TMP);
3312 __ LoadConst32(AT, imm_low);
3313 __ Sltu(AT, lhs_low, AT);
3314 __ Or(TMP, TMP, AT);
3315 __ Beqz(TMP, label);
3316 break;
3317 case kCondLE:
3318 __ LoadConst32(TMP, imm_high);
3319 __ Blt(lhs_high, TMP, label);
3320 __ Slt(TMP, TMP, lhs_high);
3321 __ LoadConst32(AT, imm_low);
3322 __ Sltu(AT, AT, lhs_low);
3323 __ Or(TMP, TMP, AT);
3324 __ Beqz(TMP, label);
3325 break;
3326 case kCondGT:
3327 __ LoadConst32(TMP, imm_high);
3328 __ Blt(TMP, lhs_high, label);
3329 __ Slt(TMP, lhs_high, TMP);
3330 __ LoadConst32(AT, imm_low);
3331 __ Sltu(AT, AT, lhs_low);
3332 __ Blt(TMP, AT, label);
3333 break;
3334 case kCondB:
3335 __ LoadConst32(TMP, imm_high);
3336 __ Bltu(lhs_high, TMP, label);
3337 __ Sltu(TMP, TMP, lhs_high);
3338 __ LoadConst32(AT, imm_low);
3339 __ Sltu(AT, lhs_low, AT);
3340 __ Blt(TMP, AT, label);
3341 break;
3342 case kCondAE:
3343 __ LoadConst32(TMP, imm_high);
3344 __ Bltu(TMP, lhs_high, label);
3345 __ Sltu(TMP, lhs_high, TMP);
3346 __ LoadConst32(AT, imm_low);
3347 __ Sltu(AT, lhs_low, AT);
3348 __ Or(TMP, TMP, AT);
3349 __ Beqz(TMP, label);
3350 break;
3351 case kCondBE:
3352 __ LoadConst32(TMP, imm_high);
3353 __ Bltu(lhs_high, TMP, label);
3354 __ Sltu(TMP, TMP, lhs_high);
3355 __ LoadConst32(AT, imm_low);
3356 __ Sltu(AT, AT, lhs_low);
3357 __ Or(TMP, TMP, AT);
3358 __ Beqz(TMP, label);
3359 break;
3360 case kCondA:
3361 __ LoadConst32(TMP, imm_high);
3362 __ Bltu(TMP, lhs_high, label);
3363 __ Sltu(TMP, lhs_high, TMP);
3364 __ LoadConst32(AT, imm_low);
3365 __ Sltu(AT, AT, lhs_low);
3366 __ Blt(TMP, AT, label);
3367 break;
3368 }
3369 } else {
3370 switch (cond) {
3371 case kCondEQ:
3372 __ Xor(TMP, lhs_high, rhs_high);
3373 __ Xor(AT, lhs_low, rhs_low);
3374 __ Or(TMP, TMP, AT);
3375 __ Beqz(TMP, label);
3376 break;
3377 case kCondNE:
3378 __ Xor(TMP, lhs_high, rhs_high);
3379 __ Xor(AT, lhs_low, rhs_low);
3380 __ Or(TMP, TMP, AT);
3381 __ Bnez(TMP, label);
3382 break;
3383 case kCondLT:
3384 __ Blt(lhs_high, rhs_high, label);
3385 __ Slt(TMP, rhs_high, lhs_high);
3386 __ Sltu(AT, lhs_low, rhs_low);
3387 __ Blt(TMP, AT, label);
3388 break;
3389 case kCondGE:
3390 __ Blt(rhs_high, lhs_high, label);
3391 __ Slt(TMP, lhs_high, rhs_high);
3392 __ Sltu(AT, lhs_low, rhs_low);
3393 __ Or(TMP, TMP, AT);
3394 __ Beqz(TMP, label);
3395 break;
3396 case kCondLE:
3397 __ Blt(lhs_high, rhs_high, label);
3398 __ Slt(TMP, rhs_high, lhs_high);
3399 __ Sltu(AT, rhs_low, lhs_low);
3400 __ Or(TMP, TMP, AT);
3401 __ Beqz(TMP, label);
3402 break;
3403 case kCondGT:
3404 __ Blt(rhs_high, lhs_high, label);
3405 __ Slt(TMP, lhs_high, rhs_high);
3406 __ Sltu(AT, rhs_low, lhs_low);
3407 __ Blt(TMP, AT, label);
3408 break;
3409 case kCondB:
3410 __ Bltu(lhs_high, rhs_high, label);
3411 __ Sltu(TMP, rhs_high, lhs_high);
3412 __ Sltu(AT, lhs_low, rhs_low);
3413 __ Blt(TMP, AT, label);
3414 break;
3415 case kCondAE:
3416 __ Bltu(rhs_high, lhs_high, label);
3417 __ Sltu(TMP, lhs_high, rhs_high);
3418 __ Sltu(AT, lhs_low, rhs_low);
3419 __ Or(TMP, TMP, AT);
3420 __ Beqz(TMP, label);
3421 break;
3422 case kCondBE:
3423 __ Bltu(lhs_high, rhs_high, label);
3424 __ Sltu(TMP, rhs_high, lhs_high);
3425 __ Sltu(AT, rhs_low, lhs_low);
3426 __ Or(TMP, TMP, AT);
3427 __ Beqz(TMP, label);
3428 break;
3429 case kCondA:
3430 __ Bltu(rhs_high, lhs_high, label);
3431 __ Sltu(TMP, lhs_high, rhs_high);
3432 __ Sltu(AT, rhs_low, lhs_low);
3433 __ Blt(TMP, AT, label);
3434 break;
3435 }
3436 }
3437}
3438
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003439void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3440 bool gt_bias,
3441 Primitive::Type type,
3442 LocationSummary* locations) {
3443 Register dst = locations->Out().AsRegister<Register>();
3444 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3445 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3446 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3447 if (type == Primitive::kPrimFloat) {
3448 if (isR6) {
3449 switch (cond) {
3450 case kCondEQ:
3451 __ CmpEqS(FTMP, lhs, rhs);
3452 __ Mfc1(dst, FTMP);
3453 __ Andi(dst, dst, 1);
3454 break;
3455 case kCondNE:
3456 __ CmpEqS(FTMP, lhs, rhs);
3457 __ Mfc1(dst, FTMP);
3458 __ Addiu(dst, dst, 1);
3459 break;
3460 case kCondLT:
3461 if (gt_bias) {
3462 __ CmpLtS(FTMP, lhs, rhs);
3463 } else {
3464 __ CmpUltS(FTMP, lhs, rhs);
3465 }
3466 __ Mfc1(dst, FTMP);
3467 __ Andi(dst, dst, 1);
3468 break;
3469 case kCondLE:
3470 if (gt_bias) {
3471 __ CmpLeS(FTMP, lhs, rhs);
3472 } else {
3473 __ CmpUleS(FTMP, lhs, rhs);
3474 }
3475 __ Mfc1(dst, FTMP);
3476 __ Andi(dst, dst, 1);
3477 break;
3478 case kCondGT:
3479 if (gt_bias) {
3480 __ CmpUltS(FTMP, rhs, lhs);
3481 } else {
3482 __ CmpLtS(FTMP, rhs, lhs);
3483 }
3484 __ Mfc1(dst, FTMP);
3485 __ Andi(dst, dst, 1);
3486 break;
3487 case kCondGE:
3488 if (gt_bias) {
3489 __ CmpUleS(FTMP, rhs, lhs);
3490 } else {
3491 __ CmpLeS(FTMP, rhs, lhs);
3492 }
3493 __ Mfc1(dst, FTMP);
3494 __ Andi(dst, dst, 1);
3495 break;
3496 default:
3497 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3498 UNREACHABLE();
3499 }
3500 } else {
3501 switch (cond) {
3502 case kCondEQ:
3503 __ CeqS(0, lhs, rhs);
3504 __ LoadConst32(dst, 1);
3505 __ Movf(dst, ZERO, 0);
3506 break;
3507 case kCondNE:
3508 __ CeqS(0, lhs, rhs);
3509 __ LoadConst32(dst, 1);
3510 __ Movt(dst, ZERO, 0);
3511 break;
3512 case kCondLT:
3513 if (gt_bias) {
3514 __ ColtS(0, lhs, rhs);
3515 } else {
3516 __ CultS(0, lhs, rhs);
3517 }
3518 __ LoadConst32(dst, 1);
3519 __ Movf(dst, ZERO, 0);
3520 break;
3521 case kCondLE:
3522 if (gt_bias) {
3523 __ ColeS(0, lhs, rhs);
3524 } else {
3525 __ CuleS(0, lhs, rhs);
3526 }
3527 __ LoadConst32(dst, 1);
3528 __ Movf(dst, ZERO, 0);
3529 break;
3530 case kCondGT:
3531 if (gt_bias) {
3532 __ CultS(0, rhs, lhs);
3533 } else {
3534 __ ColtS(0, rhs, lhs);
3535 }
3536 __ LoadConst32(dst, 1);
3537 __ Movf(dst, ZERO, 0);
3538 break;
3539 case kCondGE:
3540 if (gt_bias) {
3541 __ CuleS(0, rhs, lhs);
3542 } else {
3543 __ ColeS(0, rhs, lhs);
3544 }
3545 __ LoadConst32(dst, 1);
3546 __ Movf(dst, ZERO, 0);
3547 break;
3548 default:
3549 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3550 UNREACHABLE();
3551 }
3552 }
3553 } else {
3554 DCHECK_EQ(type, Primitive::kPrimDouble);
3555 if (isR6) {
3556 switch (cond) {
3557 case kCondEQ:
3558 __ CmpEqD(FTMP, lhs, rhs);
3559 __ Mfc1(dst, FTMP);
3560 __ Andi(dst, dst, 1);
3561 break;
3562 case kCondNE:
3563 __ CmpEqD(FTMP, lhs, rhs);
3564 __ Mfc1(dst, FTMP);
3565 __ Addiu(dst, dst, 1);
3566 break;
3567 case kCondLT:
3568 if (gt_bias) {
3569 __ CmpLtD(FTMP, lhs, rhs);
3570 } else {
3571 __ CmpUltD(FTMP, lhs, rhs);
3572 }
3573 __ Mfc1(dst, FTMP);
3574 __ Andi(dst, dst, 1);
3575 break;
3576 case kCondLE:
3577 if (gt_bias) {
3578 __ CmpLeD(FTMP, lhs, rhs);
3579 } else {
3580 __ CmpUleD(FTMP, lhs, rhs);
3581 }
3582 __ Mfc1(dst, FTMP);
3583 __ Andi(dst, dst, 1);
3584 break;
3585 case kCondGT:
3586 if (gt_bias) {
3587 __ CmpUltD(FTMP, rhs, lhs);
3588 } else {
3589 __ CmpLtD(FTMP, rhs, lhs);
3590 }
3591 __ Mfc1(dst, FTMP);
3592 __ Andi(dst, dst, 1);
3593 break;
3594 case kCondGE:
3595 if (gt_bias) {
3596 __ CmpUleD(FTMP, rhs, lhs);
3597 } else {
3598 __ CmpLeD(FTMP, rhs, lhs);
3599 }
3600 __ Mfc1(dst, FTMP);
3601 __ Andi(dst, dst, 1);
3602 break;
3603 default:
3604 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3605 UNREACHABLE();
3606 }
3607 } else {
3608 switch (cond) {
3609 case kCondEQ:
3610 __ CeqD(0, lhs, rhs);
3611 __ LoadConst32(dst, 1);
3612 __ Movf(dst, ZERO, 0);
3613 break;
3614 case kCondNE:
3615 __ CeqD(0, lhs, rhs);
3616 __ LoadConst32(dst, 1);
3617 __ Movt(dst, ZERO, 0);
3618 break;
3619 case kCondLT:
3620 if (gt_bias) {
3621 __ ColtD(0, lhs, rhs);
3622 } else {
3623 __ CultD(0, lhs, rhs);
3624 }
3625 __ LoadConst32(dst, 1);
3626 __ Movf(dst, ZERO, 0);
3627 break;
3628 case kCondLE:
3629 if (gt_bias) {
3630 __ ColeD(0, lhs, rhs);
3631 } else {
3632 __ CuleD(0, lhs, rhs);
3633 }
3634 __ LoadConst32(dst, 1);
3635 __ Movf(dst, ZERO, 0);
3636 break;
3637 case kCondGT:
3638 if (gt_bias) {
3639 __ CultD(0, rhs, lhs);
3640 } else {
3641 __ ColtD(0, rhs, lhs);
3642 }
3643 __ LoadConst32(dst, 1);
3644 __ Movf(dst, ZERO, 0);
3645 break;
3646 case kCondGE:
3647 if (gt_bias) {
3648 __ CuleD(0, rhs, lhs);
3649 } else {
3650 __ ColeD(0, rhs, lhs);
3651 }
3652 __ LoadConst32(dst, 1);
3653 __ Movf(dst, ZERO, 0);
3654 break;
3655 default:
3656 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3657 UNREACHABLE();
3658 }
3659 }
3660 }
3661}
3662
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003663bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3664 bool gt_bias,
3665 Primitive::Type type,
3666 LocationSummary* input_locations,
3667 int cc) {
3668 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3669 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3670 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3671 if (type == Primitive::kPrimFloat) {
3672 switch (cond) {
3673 case kCondEQ:
3674 __ CeqS(cc, lhs, rhs);
3675 return false;
3676 case kCondNE:
3677 __ CeqS(cc, lhs, rhs);
3678 return true;
3679 case kCondLT:
3680 if (gt_bias) {
3681 __ ColtS(cc, lhs, rhs);
3682 } else {
3683 __ CultS(cc, lhs, rhs);
3684 }
3685 return false;
3686 case kCondLE:
3687 if (gt_bias) {
3688 __ ColeS(cc, lhs, rhs);
3689 } else {
3690 __ CuleS(cc, lhs, rhs);
3691 }
3692 return false;
3693 case kCondGT:
3694 if (gt_bias) {
3695 __ CultS(cc, rhs, lhs);
3696 } else {
3697 __ ColtS(cc, rhs, lhs);
3698 }
3699 return false;
3700 case kCondGE:
3701 if (gt_bias) {
3702 __ CuleS(cc, rhs, lhs);
3703 } else {
3704 __ ColeS(cc, rhs, lhs);
3705 }
3706 return false;
3707 default:
3708 LOG(FATAL) << "Unexpected non-floating-point condition";
3709 UNREACHABLE();
3710 }
3711 } else {
3712 DCHECK_EQ(type, Primitive::kPrimDouble);
3713 switch (cond) {
3714 case kCondEQ:
3715 __ CeqD(cc, lhs, rhs);
3716 return false;
3717 case kCondNE:
3718 __ CeqD(cc, lhs, rhs);
3719 return true;
3720 case kCondLT:
3721 if (gt_bias) {
3722 __ ColtD(cc, lhs, rhs);
3723 } else {
3724 __ CultD(cc, lhs, rhs);
3725 }
3726 return false;
3727 case kCondLE:
3728 if (gt_bias) {
3729 __ ColeD(cc, lhs, rhs);
3730 } else {
3731 __ CuleD(cc, lhs, rhs);
3732 }
3733 return false;
3734 case kCondGT:
3735 if (gt_bias) {
3736 __ CultD(cc, rhs, lhs);
3737 } else {
3738 __ ColtD(cc, rhs, lhs);
3739 }
3740 return false;
3741 case kCondGE:
3742 if (gt_bias) {
3743 __ CuleD(cc, rhs, lhs);
3744 } else {
3745 __ ColeD(cc, rhs, lhs);
3746 }
3747 return false;
3748 default:
3749 LOG(FATAL) << "Unexpected non-floating-point condition";
3750 UNREACHABLE();
3751 }
3752 }
3753}
3754
3755bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3756 bool gt_bias,
3757 Primitive::Type type,
3758 LocationSummary* input_locations,
3759 FRegister dst) {
3760 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3761 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3762 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3763 if (type == Primitive::kPrimFloat) {
3764 switch (cond) {
3765 case kCondEQ:
3766 __ CmpEqS(dst, lhs, rhs);
3767 return false;
3768 case kCondNE:
3769 __ CmpEqS(dst, lhs, rhs);
3770 return true;
3771 case kCondLT:
3772 if (gt_bias) {
3773 __ CmpLtS(dst, lhs, rhs);
3774 } else {
3775 __ CmpUltS(dst, lhs, rhs);
3776 }
3777 return false;
3778 case kCondLE:
3779 if (gt_bias) {
3780 __ CmpLeS(dst, lhs, rhs);
3781 } else {
3782 __ CmpUleS(dst, lhs, rhs);
3783 }
3784 return false;
3785 case kCondGT:
3786 if (gt_bias) {
3787 __ CmpUltS(dst, rhs, lhs);
3788 } else {
3789 __ CmpLtS(dst, rhs, lhs);
3790 }
3791 return false;
3792 case kCondGE:
3793 if (gt_bias) {
3794 __ CmpUleS(dst, rhs, lhs);
3795 } else {
3796 __ CmpLeS(dst, rhs, lhs);
3797 }
3798 return false;
3799 default:
3800 LOG(FATAL) << "Unexpected non-floating-point condition";
3801 UNREACHABLE();
3802 }
3803 } else {
3804 DCHECK_EQ(type, Primitive::kPrimDouble);
3805 switch (cond) {
3806 case kCondEQ:
3807 __ CmpEqD(dst, lhs, rhs);
3808 return false;
3809 case kCondNE:
3810 __ CmpEqD(dst, lhs, rhs);
3811 return true;
3812 case kCondLT:
3813 if (gt_bias) {
3814 __ CmpLtD(dst, lhs, rhs);
3815 } else {
3816 __ CmpUltD(dst, lhs, rhs);
3817 }
3818 return false;
3819 case kCondLE:
3820 if (gt_bias) {
3821 __ CmpLeD(dst, lhs, rhs);
3822 } else {
3823 __ CmpUleD(dst, lhs, rhs);
3824 }
3825 return false;
3826 case kCondGT:
3827 if (gt_bias) {
3828 __ CmpUltD(dst, rhs, lhs);
3829 } else {
3830 __ CmpLtD(dst, rhs, lhs);
3831 }
3832 return false;
3833 case kCondGE:
3834 if (gt_bias) {
3835 __ CmpUleD(dst, rhs, lhs);
3836 } else {
3837 __ CmpLeD(dst, rhs, lhs);
3838 }
3839 return false;
3840 default:
3841 LOG(FATAL) << "Unexpected non-floating-point condition";
3842 UNREACHABLE();
3843 }
3844 }
3845}
3846
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003847void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3848 bool gt_bias,
3849 Primitive::Type type,
3850 LocationSummary* locations,
3851 MipsLabel* label) {
3852 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3853 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3854 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3855 if (type == Primitive::kPrimFloat) {
3856 if (isR6) {
3857 switch (cond) {
3858 case kCondEQ:
3859 __ CmpEqS(FTMP, lhs, rhs);
3860 __ Bc1nez(FTMP, label);
3861 break;
3862 case kCondNE:
3863 __ CmpEqS(FTMP, lhs, rhs);
3864 __ Bc1eqz(FTMP, label);
3865 break;
3866 case kCondLT:
3867 if (gt_bias) {
3868 __ CmpLtS(FTMP, lhs, rhs);
3869 } else {
3870 __ CmpUltS(FTMP, lhs, rhs);
3871 }
3872 __ Bc1nez(FTMP, label);
3873 break;
3874 case kCondLE:
3875 if (gt_bias) {
3876 __ CmpLeS(FTMP, lhs, rhs);
3877 } else {
3878 __ CmpUleS(FTMP, lhs, rhs);
3879 }
3880 __ Bc1nez(FTMP, label);
3881 break;
3882 case kCondGT:
3883 if (gt_bias) {
3884 __ CmpUltS(FTMP, rhs, lhs);
3885 } else {
3886 __ CmpLtS(FTMP, rhs, lhs);
3887 }
3888 __ Bc1nez(FTMP, label);
3889 break;
3890 case kCondGE:
3891 if (gt_bias) {
3892 __ CmpUleS(FTMP, rhs, lhs);
3893 } else {
3894 __ CmpLeS(FTMP, rhs, lhs);
3895 }
3896 __ Bc1nez(FTMP, label);
3897 break;
3898 default:
3899 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003900 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003901 }
3902 } else {
3903 switch (cond) {
3904 case kCondEQ:
3905 __ CeqS(0, lhs, rhs);
3906 __ Bc1t(0, label);
3907 break;
3908 case kCondNE:
3909 __ CeqS(0, lhs, rhs);
3910 __ Bc1f(0, label);
3911 break;
3912 case kCondLT:
3913 if (gt_bias) {
3914 __ ColtS(0, lhs, rhs);
3915 } else {
3916 __ CultS(0, lhs, rhs);
3917 }
3918 __ Bc1t(0, label);
3919 break;
3920 case kCondLE:
3921 if (gt_bias) {
3922 __ ColeS(0, lhs, rhs);
3923 } else {
3924 __ CuleS(0, lhs, rhs);
3925 }
3926 __ Bc1t(0, label);
3927 break;
3928 case kCondGT:
3929 if (gt_bias) {
3930 __ CultS(0, rhs, lhs);
3931 } else {
3932 __ ColtS(0, rhs, lhs);
3933 }
3934 __ Bc1t(0, label);
3935 break;
3936 case kCondGE:
3937 if (gt_bias) {
3938 __ CuleS(0, rhs, lhs);
3939 } else {
3940 __ ColeS(0, rhs, lhs);
3941 }
3942 __ Bc1t(0, label);
3943 break;
3944 default:
3945 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003946 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003947 }
3948 }
3949 } else {
3950 DCHECK_EQ(type, Primitive::kPrimDouble);
3951 if (isR6) {
3952 switch (cond) {
3953 case kCondEQ:
3954 __ CmpEqD(FTMP, lhs, rhs);
3955 __ Bc1nez(FTMP, label);
3956 break;
3957 case kCondNE:
3958 __ CmpEqD(FTMP, lhs, rhs);
3959 __ Bc1eqz(FTMP, label);
3960 break;
3961 case kCondLT:
3962 if (gt_bias) {
3963 __ CmpLtD(FTMP, lhs, rhs);
3964 } else {
3965 __ CmpUltD(FTMP, lhs, rhs);
3966 }
3967 __ Bc1nez(FTMP, label);
3968 break;
3969 case kCondLE:
3970 if (gt_bias) {
3971 __ CmpLeD(FTMP, lhs, rhs);
3972 } else {
3973 __ CmpUleD(FTMP, lhs, rhs);
3974 }
3975 __ Bc1nez(FTMP, label);
3976 break;
3977 case kCondGT:
3978 if (gt_bias) {
3979 __ CmpUltD(FTMP, rhs, lhs);
3980 } else {
3981 __ CmpLtD(FTMP, rhs, lhs);
3982 }
3983 __ Bc1nez(FTMP, label);
3984 break;
3985 case kCondGE:
3986 if (gt_bias) {
3987 __ CmpUleD(FTMP, rhs, lhs);
3988 } else {
3989 __ CmpLeD(FTMP, rhs, lhs);
3990 }
3991 __ Bc1nez(FTMP, label);
3992 break;
3993 default:
3994 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003995 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003996 }
3997 } else {
3998 switch (cond) {
3999 case kCondEQ:
4000 __ CeqD(0, lhs, rhs);
4001 __ Bc1t(0, label);
4002 break;
4003 case kCondNE:
4004 __ CeqD(0, lhs, rhs);
4005 __ Bc1f(0, label);
4006 break;
4007 case kCondLT:
4008 if (gt_bias) {
4009 __ ColtD(0, lhs, rhs);
4010 } else {
4011 __ CultD(0, lhs, rhs);
4012 }
4013 __ Bc1t(0, label);
4014 break;
4015 case kCondLE:
4016 if (gt_bias) {
4017 __ ColeD(0, lhs, rhs);
4018 } else {
4019 __ CuleD(0, lhs, rhs);
4020 }
4021 __ Bc1t(0, label);
4022 break;
4023 case kCondGT:
4024 if (gt_bias) {
4025 __ CultD(0, rhs, lhs);
4026 } else {
4027 __ ColtD(0, rhs, lhs);
4028 }
4029 __ Bc1t(0, label);
4030 break;
4031 case kCondGE:
4032 if (gt_bias) {
4033 __ CuleD(0, rhs, lhs);
4034 } else {
4035 __ ColeD(0, rhs, lhs);
4036 }
4037 __ Bc1t(0, label);
4038 break;
4039 default:
4040 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004041 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004042 }
4043 }
4044 }
4045}
4046
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004047void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004048 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004049 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004050 MipsLabel* false_target) {
4051 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004052
David Brazdil0debae72015-11-12 18:37:00 +00004053 if (true_target == nullptr && false_target == nullptr) {
4054 // Nothing to do. The code always falls through.
4055 return;
4056 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004057 // Constant condition, statically compared against "true" (integer value 1).
4058 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004059 if (true_target != nullptr) {
4060 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004061 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004063 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004064 if (false_target != nullptr) {
4065 __ B(false_target);
4066 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004067 }
David Brazdil0debae72015-11-12 18:37:00 +00004068 return;
4069 }
4070
4071 // The following code generates these patterns:
4072 // (1) true_target == nullptr && false_target != nullptr
4073 // - opposite condition true => branch to false_target
4074 // (2) true_target != nullptr && false_target == nullptr
4075 // - condition true => branch to true_target
4076 // (3) true_target != nullptr && false_target != nullptr
4077 // - condition true => branch to true_target
4078 // - branch to false_target
4079 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004080 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004081 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004082 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004083 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004084 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4085 } else {
4086 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4087 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004088 } else {
4089 // The condition instruction has not been materialized, use its inputs as
4090 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004091 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004092 Primitive::Type type = condition->InputAt(0)->GetType();
4093 LocationSummary* locations = cond->GetLocations();
4094 IfCondition if_cond = condition->GetCondition();
4095 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004096
David Brazdil0debae72015-11-12 18:37:00 +00004097 if (true_target == nullptr) {
4098 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004099 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004100 }
4101
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004102 switch (type) {
4103 default:
4104 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4105 break;
4106 case Primitive::kPrimLong:
4107 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4108 break;
4109 case Primitive::kPrimFloat:
4110 case Primitive::kPrimDouble:
4111 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4112 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004113 }
4114 }
David Brazdil0debae72015-11-12 18:37:00 +00004115
4116 // If neither branch falls through (case 3), the conditional branch to `true_target`
4117 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4118 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 __ B(false_target);
4120 }
4121}
4122
4123void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4124 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004125 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004126 locations->SetInAt(0, Location::RequiresRegister());
4127 }
4128}
4129
4130void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004131 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4132 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4133 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4134 nullptr : codegen_->GetLabelOf(true_successor);
4135 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4136 nullptr : codegen_->GetLabelOf(false_successor);
4137 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004138}
4139
4140void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4141 LocationSummary* locations = new (GetGraph()->GetArena())
4142 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004143 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004144 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004145 locations->SetInAt(0, Location::RequiresRegister());
4146 }
4147}
4148
4149void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004150 SlowPathCodeMIPS* slow_path =
4151 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004152 GenerateTestAndBranch(deoptimize,
4153 /* condition_input_index */ 0,
4154 slow_path->GetEntryLabel(),
4155 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004156}
4157
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004158// This function returns true if a conditional move can be generated for HSelect.
4159// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4160// branches and regular moves.
4161//
4162// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4163//
4164// While determining feasibility of a conditional move and setting inputs/outputs
4165// are two distinct tasks, this function does both because they share quite a bit
4166// of common logic.
4167static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4168 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4169 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4170 HCondition* condition = cond->AsCondition();
4171
4172 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4173 Primitive::Type dst_type = select->GetType();
4174
4175 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4176 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4177 bool is_true_value_zero_constant =
4178 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4179 bool is_false_value_zero_constant =
4180 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4181
4182 bool can_move_conditionally = false;
4183 bool use_const_for_false_in = false;
4184 bool use_const_for_true_in = false;
4185
4186 if (!cond->IsConstant()) {
4187 switch (cond_type) {
4188 default:
4189 switch (dst_type) {
4190 default:
4191 // Moving int on int condition.
4192 if (is_r6) {
4193 if (is_true_value_zero_constant) {
4194 // seleqz out_reg, false_reg, cond_reg
4195 can_move_conditionally = true;
4196 use_const_for_true_in = true;
4197 } else if (is_false_value_zero_constant) {
4198 // selnez out_reg, true_reg, cond_reg
4199 can_move_conditionally = true;
4200 use_const_for_false_in = true;
4201 } else if (materialized) {
4202 // Not materializing unmaterialized int conditions
4203 // to keep the instruction count low.
4204 // selnez AT, true_reg, cond_reg
4205 // seleqz TMP, false_reg, cond_reg
4206 // or out_reg, AT, TMP
4207 can_move_conditionally = true;
4208 }
4209 } else {
4210 // movn out_reg, true_reg/ZERO, cond_reg
4211 can_move_conditionally = true;
4212 use_const_for_true_in = is_true_value_zero_constant;
4213 }
4214 break;
4215 case Primitive::kPrimLong:
4216 // Moving long on int condition.
4217 if (is_r6) {
4218 if (is_true_value_zero_constant) {
4219 // seleqz out_reg_lo, false_reg_lo, cond_reg
4220 // seleqz out_reg_hi, false_reg_hi, cond_reg
4221 can_move_conditionally = true;
4222 use_const_for_true_in = true;
4223 } else if (is_false_value_zero_constant) {
4224 // selnez out_reg_lo, true_reg_lo, cond_reg
4225 // selnez out_reg_hi, true_reg_hi, cond_reg
4226 can_move_conditionally = true;
4227 use_const_for_false_in = true;
4228 }
4229 // Other long conditional moves would generate 6+ instructions,
4230 // which is too many.
4231 } else {
4232 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4233 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4234 can_move_conditionally = true;
4235 use_const_for_true_in = is_true_value_zero_constant;
4236 }
4237 break;
4238 case Primitive::kPrimFloat:
4239 case Primitive::kPrimDouble:
4240 // Moving float/double on int condition.
4241 if (is_r6) {
4242 if (materialized) {
4243 // Not materializing unmaterialized int conditions
4244 // to keep the instruction count low.
4245 can_move_conditionally = true;
4246 if (is_true_value_zero_constant) {
4247 // sltu TMP, ZERO, cond_reg
4248 // mtc1 TMP, temp_cond_reg
4249 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4250 use_const_for_true_in = true;
4251 } else if (is_false_value_zero_constant) {
4252 // sltu TMP, ZERO, cond_reg
4253 // mtc1 TMP, temp_cond_reg
4254 // selnez.fmt out_reg, true_reg, temp_cond_reg
4255 use_const_for_false_in = true;
4256 } else {
4257 // sltu TMP, ZERO, cond_reg
4258 // mtc1 TMP, temp_cond_reg
4259 // sel.fmt temp_cond_reg, false_reg, true_reg
4260 // mov.fmt out_reg, temp_cond_reg
4261 }
4262 }
4263 } else {
4264 // movn.fmt out_reg, true_reg, cond_reg
4265 can_move_conditionally = true;
4266 }
4267 break;
4268 }
4269 break;
4270 case Primitive::kPrimLong:
4271 // We don't materialize long comparison now
4272 // and use conditional branches instead.
4273 break;
4274 case Primitive::kPrimFloat:
4275 case Primitive::kPrimDouble:
4276 switch (dst_type) {
4277 default:
4278 // Moving int on float/double condition.
4279 if (is_r6) {
4280 if (is_true_value_zero_constant) {
4281 // mfc1 TMP, temp_cond_reg
4282 // seleqz out_reg, false_reg, TMP
4283 can_move_conditionally = true;
4284 use_const_for_true_in = true;
4285 } else if (is_false_value_zero_constant) {
4286 // mfc1 TMP, temp_cond_reg
4287 // selnez out_reg, true_reg, TMP
4288 can_move_conditionally = true;
4289 use_const_for_false_in = true;
4290 } else {
4291 // mfc1 TMP, temp_cond_reg
4292 // selnez AT, true_reg, TMP
4293 // seleqz TMP, false_reg, TMP
4294 // or out_reg, AT, TMP
4295 can_move_conditionally = true;
4296 }
4297 } else {
4298 // movt out_reg, true_reg/ZERO, cc
4299 can_move_conditionally = true;
4300 use_const_for_true_in = is_true_value_zero_constant;
4301 }
4302 break;
4303 case Primitive::kPrimLong:
4304 // Moving long on float/double condition.
4305 if (is_r6) {
4306 if (is_true_value_zero_constant) {
4307 // mfc1 TMP, temp_cond_reg
4308 // seleqz out_reg_lo, false_reg_lo, TMP
4309 // seleqz out_reg_hi, false_reg_hi, TMP
4310 can_move_conditionally = true;
4311 use_const_for_true_in = true;
4312 } else if (is_false_value_zero_constant) {
4313 // mfc1 TMP, temp_cond_reg
4314 // selnez out_reg_lo, true_reg_lo, TMP
4315 // selnez out_reg_hi, true_reg_hi, TMP
4316 can_move_conditionally = true;
4317 use_const_for_false_in = true;
4318 }
4319 // Other long conditional moves would generate 6+ instructions,
4320 // which is too many.
4321 } else {
4322 // movt out_reg_lo, true_reg_lo/ZERO, cc
4323 // movt out_reg_hi, true_reg_hi/ZERO, cc
4324 can_move_conditionally = true;
4325 use_const_for_true_in = is_true_value_zero_constant;
4326 }
4327 break;
4328 case Primitive::kPrimFloat:
4329 case Primitive::kPrimDouble:
4330 // Moving float/double on float/double condition.
4331 if (is_r6) {
4332 can_move_conditionally = true;
4333 if (is_true_value_zero_constant) {
4334 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4335 use_const_for_true_in = true;
4336 } else if (is_false_value_zero_constant) {
4337 // selnez.fmt out_reg, true_reg, temp_cond_reg
4338 use_const_for_false_in = true;
4339 } else {
4340 // sel.fmt temp_cond_reg, false_reg, true_reg
4341 // mov.fmt out_reg, temp_cond_reg
4342 }
4343 } else {
4344 // movt.fmt out_reg, true_reg, cc
4345 can_move_conditionally = true;
4346 }
4347 break;
4348 }
4349 break;
4350 }
4351 }
4352
4353 if (can_move_conditionally) {
4354 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4355 } else {
4356 DCHECK(!use_const_for_false_in);
4357 DCHECK(!use_const_for_true_in);
4358 }
4359
4360 if (locations_to_set != nullptr) {
4361 if (use_const_for_false_in) {
4362 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4363 } else {
4364 locations_to_set->SetInAt(0,
4365 Primitive::IsFloatingPointType(dst_type)
4366 ? Location::RequiresFpuRegister()
4367 : Location::RequiresRegister());
4368 }
4369 if (use_const_for_true_in) {
4370 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4371 } else {
4372 locations_to_set->SetInAt(1,
4373 Primitive::IsFloatingPointType(dst_type)
4374 ? Location::RequiresFpuRegister()
4375 : Location::RequiresRegister());
4376 }
4377 if (materialized) {
4378 locations_to_set->SetInAt(2, Location::RequiresRegister());
4379 }
4380 // On R6 we don't require the output to be the same as the
4381 // first input for conditional moves unlike on R2.
4382 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4383 if (is_out_same_as_first_in) {
4384 locations_to_set->SetOut(Location::SameAsFirstInput());
4385 } else {
4386 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4387 ? Location::RequiresFpuRegister()
4388 : Location::RequiresRegister());
4389 }
4390 }
4391
4392 return can_move_conditionally;
4393}
4394
4395void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4396 LocationSummary* locations = select->GetLocations();
4397 Location dst = locations->Out();
4398 Location src = locations->InAt(1);
4399 Register src_reg = ZERO;
4400 Register src_reg_high = ZERO;
4401 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4402 Register cond_reg = TMP;
4403 int cond_cc = 0;
4404 Primitive::Type cond_type = Primitive::kPrimInt;
4405 bool cond_inverted = false;
4406 Primitive::Type dst_type = select->GetType();
4407
4408 if (IsBooleanValueOrMaterializedCondition(cond)) {
4409 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4410 } else {
4411 HCondition* condition = cond->AsCondition();
4412 LocationSummary* cond_locations = cond->GetLocations();
4413 IfCondition if_cond = condition->GetCondition();
4414 cond_type = condition->InputAt(0)->GetType();
4415 switch (cond_type) {
4416 default:
4417 DCHECK_NE(cond_type, Primitive::kPrimLong);
4418 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4419 break;
4420 case Primitive::kPrimFloat:
4421 case Primitive::kPrimDouble:
4422 cond_inverted = MaterializeFpCompareR2(if_cond,
4423 condition->IsGtBias(),
4424 cond_type,
4425 cond_locations,
4426 cond_cc);
4427 break;
4428 }
4429 }
4430
4431 DCHECK(dst.Equals(locations->InAt(0)));
4432 if (src.IsRegister()) {
4433 src_reg = src.AsRegister<Register>();
4434 } else if (src.IsRegisterPair()) {
4435 src_reg = src.AsRegisterPairLow<Register>();
4436 src_reg_high = src.AsRegisterPairHigh<Register>();
4437 } else if (src.IsConstant()) {
4438 DCHECK(src.GetConstant()->IsZeroBitPattern());
4439 }
4440
4441 switch (cond_type) {
4442 default:
4443 switch (dst_type) {
4444 default:
4445 if (cond_inverted) {
4446 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4447 } else {
4448 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4449 }
4450 break;
4451 case Primitive::kPrimLong:
4452 if (cond_inverted) {
4453 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4454 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4455 } else {
4456 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4457 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4458 }
4459 break;
4460 case Primitive::kPrimFloat:
4461 if (cond_inverted) {
4462 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4463 } else {
4464 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4465 }
4466 break;
4467 case Primitive::kPrimDouble:
4468 if (cond_inverted) {
4469 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4470 } else {
4471 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4472 }
4473 break;
4474 }
4475 break;
4476 case Primitive::kPrimLong:
4477 LOG(FATAL) << "Unreachable";
4478 UNREACHABLE();
4479 case Primitive::kPrimFloat:
4480 case Primitive::kPrimDouble:
4481 switch (dst_type) {
4482 default:
4483 if (cond_inverted) {
4484 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4485 } else {
4486 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4487 }
4488 break;
4489 case Primitive::kPrimLong:
4490 if (cond_inverted) {
4491 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4492 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4493 } else {
4494 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4495 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4496 }
4497 break;
4498 case Primitive::kPrimFloat:
4499 if (cond_inverted) {
4500 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4501 } else {
4502 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4503 }
4504 break;
4505 case Primitive::kPrimDouble:
4506 if (cond_inverted) {
4507 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4508 } else {
4509 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4510 }
4511 break;
4512 }
4513 break;
4514 }
4515}
4516
4517void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4518 LocationSummary* locations = select->GetLocations();
4519 Location dst = locations->Out();
4520 Location false_src = locations->InAt(0);
4521 Location true_src = locations->InAt(1);
4522 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4523 Register cond_reg = TMP;
4524 FRegister fcond_reg = FTMP;
4525 Primitive::Type cond_type = Primitive::kPrimInt;
4526 bool cond_inverted = false;
4527 Primitive::Type dst_type = select->GetType();
4528
4529 if (IsBooleanValueOrMaterializedCondition(cond)) {
4530 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4531 } else {
4532 HCondition* condition = cond->AsCondition();
4533 LocationSummary* cond_locations = cond->GetLocations();
4534 IfCondition if_cond = condition->GetCondition();
4535 cond_type = condition->InputAt(0)->GetType();
4536 switch (cond_type) {
4537 default:
4538 DCHECK_NE(cond_type, Primitive::kPrimLong);
4539 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4540 break;
4541 case Primitive::kPrimFloat:
4542 case Primitive::kPrimDouble:
4543 cond_inverted = MaterializeFpCompareR6(if_cond,
4544 condition->IsGtBias(),
4545 cond_type,
4546 cond_locations,
4547 fcond_reg);
4548 break;
4549 }
4550 }
4551
4552 if (true_src.IsConstant()) {
4553 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4554 }
4555 if (false_src.IsConstant()) {
4556 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4557 }
4558
4559 switch (dst_type) {
4560 default:
4561 if (Primitive::IsFloatingPointType(cond_type)) {
4562 __ Mfc1(cond_reg, fcond_reg);
4563 }
4564 if (true_src.IsConstant()) {
4565 if (cond_inverted) {
4566 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4567 } else {
4568 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4569 }
4570 } else if (false_src.IsConstant()) {
4571 if (cond_inverted) {
4572 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4573 } else {
4574 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4575 }
4576 } else {
4577 DCHECK_NE(cond_reg, AT);
4578 if (cond_inverted) {
4579 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4580 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4581 } else {
4582 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4583 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4584 }
4585 __ Or(dst.AsRegister<Register>(), AT, TMP);
4586 }
4587 break;
4588 case Primitive::kPrimLong: {
4589 if (Primitive::IsFloatingPointType(cond_type)) {
4590 __ Mfc1(cond_reg, fcond_reg);
4591 }
4592 Register dst_lo = dst.AsRegisterPairLow<Register>();
4593 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4594 if (true_src.IsConstant()) {
4595 Register src_lo = false_src.AsRegisterPairLow<Register>();
4596 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4597 if (cond_inverted) {
4598 __ Selnez(dst_lo, src_lo, cond_reg);
4599 __ Selnez(dst_hi, src_hi, cond_reg);
4600 } else {
4601 __ Seleqz(dst_lo, src_lo, cond_reg);
4602 __ Seleqz(dst_hi, src_hi, cond_reg);
4603 }
4604 } else {
4605 DCHECK(false_src.IsConstant());
4606 Register src_lo = true_src.AsRegisterPairLow<Register>();
4607 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4608 if (cond_inverted) {
4609 __ Seleqz(dst_lo, src_lo, cond_reg);
4610 __ Seleqz(dst_hi, src_hi, cond_reg);
4611 } else {
4612 __ Selnez(dst_lo, src_lo, cond_reg);
4613 __ Selnez(dst_hi, src_hi, cond_reg);
4614 }
4615 }
4616 break;
4617 }
4618 case Primitive::kPrimFloat: {
4619 if (!Primitive::IsFloatingPointType(cond_type)) {
4620 // sel*.fmt tests bit 0 of the condition register, account for that.
4621 __ Sltu(TMP, ZERO, cond_reg);
4622 __ Mtc1(TMP, fcond_reg);
4623 }
4624 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4625 if (true_src.IsConstant()) {
4626 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4627 if (cond_inverted) {
4628 __ SelnezS(dst_reg, src_reg, fcond_reg);
4629 } else {
4630 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4631 }
4632 } else if (false_src.IsConstant()) {
4633 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4634 if (cond_inverted) {
4635 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4636 } else {
4637 __ SelnezS(dst_reg, src_reg, fcond_reg);
4638 }
4639 } else {
4640 if (cond_inverted) {
4641 __ SelS(fcond_reg,
4642 true_src.AsFpuRegister<FRegister>(),
4643 false_src.AsFpuRegister<FRegister>());
4644 } else {
4645 __ SelS(fcond_reg,
4646 false_src.AsFpuRegister<FRegister>(),
4647 true_src.AsFpuRegister<FRegister>());
4648 }
4649 __ MovS(dst_reg, fcond_reg);
4650 }
4651 break;
4652 }
4653 case Primitive::kPrimDouble: {
4654 if (!Primitive::IsFloatingPointType(cond_type)) {
4655 // sel*.fmt tests bit 0 of the condition register, account for that.
4656 __ Sltu(TMP, ZERO, cond_reg);
4657 __ Mtc1(TMP, fcond_reg);
4658 }
4659 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4660 if (true_src.IsConstant()) {
4661 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4662 if (cond_inverted) {
4663 __ SelnezD(dst_reg, src_reg, fcond_reg);
4664 } else {
4665 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4666 }
4667 } else if (false_src.IsConstant()) {
4668 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4669 if (cond_inverted) {
4670 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4671 } else {
4672 __ SelnezD(dst_reg, src_reg, fcond_reg);
4673 }
4674 } else {
4675 if (cond_inverted) {
4676 __ SelD(fcond_reg,
4677 true_src.AsFpuRegister<FRegister>(),
4678 false_src.AsFpuRegister<FRegister>());
4679 } else {
4680 __ SelD(fcond_reg,
4681 false_src.AsFpuRegister<FRegister>(),
4682 true_src.AsFpuRegister<FRegister>());
4683 }
4684 __ MovD(dst_reg, fcond_reg);
4685 }
4686 break;
4687 }
4688 }
4689}
4690
David Brazdil74eb1b22015-12-14 11:44:01 +00004691void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004693 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004694}
4695
4696void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004697 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4698 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4699 if (is_r6) {
4700 GenConditionalMoveR6(select);
4701 } else {
4702 GenConditionalMoveR2(select);
4703 }
4704 } else {
4705 LocationSummary* locations = select->GetLocations();
4706 MipsLabel false_target;
4707 GenerateTestAndBranch(select,
4708 /* condition_input_index */ 2,
4709 /* true_target */ nullptr,
4710 &false_target);
4711 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4712 __ Bind(&false_target);
4713 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004714}
4715
David Srbecky0cf44932015-12-09 14:09:59 +00004716void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4717 new (GetGraph()->GetArena()) LocationSummary(info);
4718}
4719
David Srbeckyd28f4a02016-03-14 17:14:24 +00004720void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4721 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004722}
4723
4724void CodeGeneratorMIPS::GenerateNop() {
4725 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004726}
4727
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004728void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4729 Primitive::Type field_type = field_info.GetFieldType();
4730 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4731 bool generate_volatile = field_info.IsVolatile() && is_wide;
4732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004733 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004734
4735 locations->SetInAt(0, Location::RequiresRegister());
4736 if (generate_volatile) {
4737 InvokeRuntimeCallingConvention calling_convention;
4738 // need A0 to hold base + offset
4739 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4740 if (field_type == Primitive::kPrimLong) {
4741 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4742 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004743 // Use Location::Any() to prevent situations when running out of available fp registers.
4744 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004745 // Need some temp core regs since FP results are returned in core registers
4746 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4747 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4748 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4749 }
4750 } else {
4751 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4752 locations->SetOut(Location::RequiresFpuRegister());
4753 } else {
4754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4755 }
4756 }
4757}
4758
4759void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4760 const FieldInfo& field_info,
4761 uint32_t dex_pc) {
4762 Primitive::Type type = field_info.GetFieldType();
4763 LocationSummary* locations = instruction->GetLocations();
4764 Register obj = locations->InAt(0).AsRegister<Register>();
4765 LoadOperandType load_type = kLoadUnsignedByte;
4766 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004767 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004768 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004769
4770 switch (type) {
4771 case Primitive::kPrimBoolean:
4772 load_type = kLoadUnsignedByte;
4773 break;
4774 case Primitive::kPrimByte:
4775 load_type = kLoadSignedByte;
4776 break;
4777 case Primitive::kPrimShort:
4778 load_type = kLoadSignedHalfword;
4779 break;
4780 case Primitive::kPrimChar:
4781 load_type = kLoadUnsignedHalfword;
4782 break;
4783 case Primitive::kPrimInt:
4784 case Primitive::kPrimFloat:
4785 case Primitive::kPrimNot:
4786 load_type = kLoadWord;
4787 break;
4788 case Primitive::kPrimLong:
4789 case Primitive::kPrimDouble:
4790 load_type = kLoadDoubleword;
4791 break;
4792 case Primitive::kPrimVoid:
4793 LOG(FATAL) << "Unreachable type " << type;
4794 UNREACHABLE();
4795 }
4796
4797 if (is_volatile && load_type == kLoadDoubleword) {
4798 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004799 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004800 // Do implicit Null check
4801 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4802 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004803 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004804 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4805 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004806 // FP results are returned in core registers. Need to move them.
4807 Location out = locations->Out();
4808 if (out.IsFpuRegister()) {
4809 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4810 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4811 out.AsFpuRegister<FRegister>());
4812 } else {
4813 DCHECK(out.IsDoubleStackSlot());
4814 __ StoreToOffset(kStoreWord,
4815 locations->GetTemp(1).AsRegister<Register>(),
4816 SP,
4817 out.GetStackIndex());
4818 __ StoreToOffset(kStoreWord,
4819 locations->GetTemp(2).AsRegister<Register>(),
4820 SP,
4821 out.GetStackIndex() + 4);
4822 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004823 }
4824 } else {
4825 if (!Primitive::IsFloatingPointType(type)) {
4826 Register dst;
4827 if (type == Primitive::kPrimLong) {
4828 DCHECK(locations->Out().IsRegisterPair());
4829 dst = locations->Out().AsRegisterPairLow<Register>();
4830 } else {
4831 DCHECK(locations->Out().IsRegister());
4832 dst = locations->Out().AsRegister<Register>();
4833 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004834 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004835 } else {
4836 DCHECK(locations->Out().IsFpuRegister());
4837 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4838 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004839 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004840 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004841 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004842 }
4843 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004844 }
4845
4846 if (is_volatile) {
4847 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4848 }
4849}
4850
4851void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4852 Primitive::Type field_type = field_info.GetFieldType();
4853 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4854 bool generate_volatile = field_info.IsVolatile() && is_wide;
4855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004856 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004857
4858 locations->SetInAt(0, Location::RequiresRegister());
4859 if (generate_volatile) {
4860 InvokeRuntimeCallingConvention calling_convention;
4861 // need A0 to hold base + offset
4862 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4863 if (field_type == Primitive::kPrimLong) {
4864 locations->SetInAt(1, Location::RegisterPairLocation(
4865 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4866 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004867 // Use Location::Any() to prevent situations when running out of available fp registers.
4868 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004869 // Pass FP parameters in core registers.
4870 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4871 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4872 }
4873 } else {
4874 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004875 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004877 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004878 }
4879 }
4880}
4881
4882void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4883 const FieldInfo& field_info,
4884 uint32_t dex_pc) {
4885 Primitive::Type type = field_info.GetFieldType();
4886 LocationSummary* locations = instruction->GetLocations();
4887 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004888 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004889 StoreOperandType store_type = kStoreByte;
4890 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004891 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004892 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004893
4894 switch (type) {
4895 case Primitive::kPrimBoolean:
4896 case Primitive::kPrimByte:
4897 store_type = kStoreByte;
4898 break;
4899 case Primitive::kPrimShort:
4900 case Primitive::kPrimChar:
4901 store_type = kStoreHalfword;
4902 break;
4903 case Primitive::kPrimInt:
4904 case Primitive::kPrimFloat:
4905 case Primitive::kPrimNot:
4906 store_type = kStoreWord;
4907 break;
4908 case Primitive::kPrimLong:
4909 case Primitive::kPrimDouble:
4910 store_type = kStoreDoubleword;
4911 break;
4912 case Primitive::kPrimVoid:
4913 LOG(FATAL) << "Unreachable type " << type;
4914 UNREACHABLE();
4915 }
4916
4917 if (is_volatile) {
4918 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4919 }
4920
4921 if (is_volatile && store_type == kStoreDoubleword) {
4922 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004923 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004924 // Do implicit Null check.
4925 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4926 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4927 if (type == Primitive::kPrimDouble) {
4928 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004929 if (value_location.IsFpuRegister()) {
4930 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4931 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004932 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004933 value_location.AsFpuRegister<FRegister>());
4934 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004935 __ LoadFromOffset(kLoadWord,
4936 locations->GetTemp(1).AsRegister<Register>(),
4937 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004938 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004939 __ LoadFromOffset(kLoadWord,
4940 locations->GetTemp(2).AsRegister<Register>(),
4941 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004942 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004943 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004944 DCHECK(value_location.IsConstant());
4945 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4946 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004947 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4948 locations->GetTemp(1).AsRegister<Register>(),
4949 value);
4950 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004951 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004952 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004953 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4954 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004955 if (value_location.IsConstant()) {
4956 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4957 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4958 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 Register src;
4960 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004961 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004962 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004963 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004964 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004965 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004967 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004968 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004969 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004970 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004971 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004972 }
4973 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004974 }
4975
4976 // TODO: memory barriers?
4977 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004978 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004979 codegen_->MarkGCCard(obj, src);
4980 }
4981
4982 if (is_volatile) {
4983 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4984 }
4985}
4986
4987void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4988 HandleFieldGet(instruction, instruction->GetFieldInfo());
4989}
4990
4991void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4992 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4993}
4994
4995void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4996 HandleFieldSet(instruction, instruction->GetFieldInfo());
4997}
4998
4999void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5000 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5001}
5002
Alexey Frunze06a46c42016-07-19 15:00:40 -07005003void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5004 HInstruction* instruction ATTRIBUTE_UNUSED,
5005 Location root,
5006 Register obj,
5007 uint32_t offset) {
5008 Register root_reg = root.AsRegister<Register>();
5009 if (kEmitCompilerReadBarrier) {
5010 UNIMPLEMENTED(FATAL) << "for read barrier";
5011 } else {
5012 // Plain GC root load with no read barrier.
5013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5014 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5015 // Note that GC roots are not affected by heap poisoning, thus we
5016 // do not have to unpoison `root_reg` here.
5017 }
5018}
5019
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005020void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5021 LocationSummary::CallKind call_kind =
5022 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5024 locations->SetInAt(0, Location::RequiresRegister());
5025 locations->SetInAt(1, Location::RequiresRegister());
5026 // The output does overlap inputs.
5027 // Note that TypeCheckSlowPathMIPS uses this register too.
5028 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5029}
5030
5031void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5032 LocationSummary* locations = instruction->GetLocations();
5033 Register obj = locations->InAt(0).AsRegister<Register>();
5034 Register cls = locations->InAt(1).AsRegister<Register>();
5035 Register out = locations->Out().AsRegister<Register>();
5036
5037 MipsLabel done;
5038
5039 // Return 0 if `obj` is null.
5040 // TODO: Avoid this check if we know `obj` is not null.
5041 __ Move(out, ZERO);
5042 __ Beqz(obj, &done);
5043
5044 // Compare the class of `obj` with `cls`.
5045 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5046 if (instruction->IsExactCheck()) {
5047 // Classes must be equal for the instanceof to succeed.
5048 __ Xor(out, out, cls);
5049 __ Sltiu(out, out, 1);
5050 } else {
5051 // If the classes are not equal, we go into a slow path.
5052 DCHECK(locations->OnlyCallsOnSlowPath());
5053 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5054 codegen_->AddSlowPath(slow_path);
5055 __ Bne(out, cls, slow_path->GetEntryLabel());
5056 __ LoadConst32(out, 1);
5057 __ Bind(slow_path->GetExitLabel());
5058 }
5059
5060 __ Bind(&done);
5061}
5062
5063void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5065 locations->SetOut(Location::ConstantLocation(constant));
5066}
5067
5068void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5069 // Will be generated at use site.
5070}
5071
5072void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5074 locations->SetOut(Location::ConstantLocation(constant));
5075}
5076
5077void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5078 // Will be generated at use site.
5079}
5080
5081void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5082 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5083 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5084}
5085
5086void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5087 HandleInvoke(invoke);
5088 // The register T0 is required to be used for the hidden argument in
5089 // art_quick_imt_conflict_trampoline, so add the hidden argument.
5090 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
5091}
5092
5093void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5094 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5095 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005096 Location receiver = invoke->GetLocations()->InAt(0);
5097 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005098 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005099
5100 // Set the hidden argument.
5101 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5102 invoke->GetDexMethodIndex());
5103
5104 // temp = object->GetClass();
5105 if (receiver.IsStackSlot()) {
5106 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5107 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5108 } else {
5109 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5110 }
5111 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005112 __ LoadFromOffset(kLoadWord, temp, temp,
5113 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5114 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005115 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005116 // temp = temp->GetImtEntryAt(method_offset);
5117 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5118 // T9 = temp->GetEntryPoint();
5119 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5120 // T9();
5121 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005122 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005123 DCHECK(!codegen_->IsLeafMethod());
5124 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5125}
5126
5127void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005128 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5129 if (intrinsic.TryDispatch(invoke)) {
5130 return;
5131 }
5132
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005133 HandleInvoke(invoke);
5134}
5135
5136void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005137 // Explicit clinit checks triggered by static invokes must have been pruned by
5138 // art::PrepareForRegisterAllocation.
5139 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005140
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005141 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5142 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5143 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5144
5145 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5146 // R6 has PC-relative addressing.
5147 bool has_extra_input = !isR6 &&
5148 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5149 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5150
5151 if (invoke->HasPcRelativeDexCache()) {
5152 // kDexCachePcRelative is mutually exclusive with
5153 // kDirectAddressWithFixup/kCallDirectWithFixup.
5154 CHECK(!has_extra_input);
5155 has_extra_input = true;
5156 }
5157
Chris Larsen701566a2015-10-27 15:29:13 -07005158 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5159 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005160 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5161 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5162 }
Chris Larsen701566a2015-10-27 15:29:13 -07005163 return;
5164 }
5165
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005166 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005167
5168 // Add the extra input register if either the dex cache array base register
5169 // or the PC-relative base register for accessing literals is needed.
5170 if (has_extra_input) {
5171 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5172 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005173}
5174
Chris Larsen701566a2015-10-27 15:29:13 -07005175static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005176 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005177 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5178 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005179 return true;
5180 }
5181 return false;
5182}
5183
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005184HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005185 HLoadString::LoadKind desired_string_load_kind) {
5186 if (kEmitCompilerReadBarrier) {
5187 UNIMPLEMENTED(FATAL) << "for read barrier";
5188 }
5189 // We disable PC-relative load when there is an irreducible loop, as the optimization
5190 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005191 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5192 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005193 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5194 bool fallback_load = has_irreducible_loops;
5195 switch (desired_string_load_kind) {
5196 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5197 DCHECK(!GetCompilerOptions().GetCompilePic());
5198 break;
5199 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5200 DCHECK(GetCompilerOptions().GetCompilePic());
5201 break;
5202 case HLoadString::LoadKind::kBootImageAddress:
5203 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005204 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005205 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005206 break;
5207 case HLoadString::LoadKind::kDexCacheViaMethod:
5208 fallback_load = false;
5209 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005210 case HLoadString::LoadKind::kJitTableAddress:
5211 DCHECK(Runtime::Current()->UseJitCompilation());
5212 // TODO: implement.
5213 fallback_load = true;
5214 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005215 }
5216 if (fallback_load) {
5217 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5218 }
5219 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005220}
5221
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005222HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5223 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005224 if (kEmitCompilerReadBarrier) {
5225 UNIMPLEMENTED(FATAL) << "for read barrier";
5226 }
5227 // We disable pc-relative load when there is an irreducible loop, as the optimization
5228 // is incompatible with it.
5229 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5230 bool fallback_load = has_irreducible_loops;
5231 switch (desired_class_load_kind) {
5232 case HLoadClass::LoadKind::kReferrersClass:
5233 fallback_load = false;
5234 break;
5235 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5236 DCHECK(!GetCompilerOptions().GetCompilePic());
5237 break;
5238 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5239 DCHECK(GetCompilerOptions().GetCompilePic());
5240 break;
5241 case HLoadClass::LoadKind::kBootImageAddress:
5242 break;
5243 case HLoadClass::LoadKind::kDexCacheAddress:
5244 DCHECK(Runtime::Current()->UseJitCompilation());
5245 fallback_load = false;
5246 break;
5247 case HLoadClass::LoadKind::kDexCachePcRelative:
5248 DCHECK(!Runtime::Current()->UseJitCompilation());
5249 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5250 // with irreducible loops.
5251 break;
5252 case HLoadClass::LoadKind::kDexCacheViaMethod:
5253 fallback_load = false;
5254 break;
5255 }
5256 if (fallback_load) {
5257 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5258 }
5259 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005260}
5261
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005262Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5263 Register temp) {
5264 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5265 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5266 if (!invoke->GetLocations()->Intrinsified()) {
5267 return location.AsRegister<Register>();
5268 }
5269 // For intrinsics we allow any location, so it may be on the stack.
5270 if (!location.IsRegister()) {
5271 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5272 return temp;
5273 }
5274 // For register locations, check if the register was saved. If so, get it from the stack.
5275 // Note: There is a chance that the register was saved but not overwritten, so we could
5276 // save one load. However, since this is just an intrinsic slow path we prefer this
5277 // simple and more robust approach rather that trying to determine if that's the case.
5278 SlowPathCode* slow_path = GetCurrentSlowPath();
5279 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5280 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5281 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5282 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5283 return temp;
5284 }
5285 return location.AsRegister<Register>();
5286}
5287
Vladimir Markodc151b22015-10-15 18:02:30 +01005288HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5289 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005290 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005291 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5292 // We disable PC-relative load when there is an irreducible loop, as the optimization
5293 // is incompatible with it.
5294 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5295 bool fallback_load = true;
5296 bool fallback_call = true;
5297 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005298 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5299 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005300 fallback_load = has_irreducible_loops;
5301 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005302 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005304 break;
5305 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005307 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005308 fallback_call = has_irreducible_loops;
5309 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005310 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005311 // TODO: Implement this type.
5312 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005313 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005314 fallback_call = false;
5315 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005316 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005317 if (fallback_load) {
5318 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5319 dispatch_info.method_load_data = 0;
5320 }
5321 if (fallback_call) {
5322 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5323 dispatch_info.direct_code_ptr = 0;
5324 }
5325 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005326}
5327
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005328void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5329 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005330 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005331 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5332 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5333 bool isR6 = isa_features_.IsR6();
5334 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5335 // R6 has PC-relative addressing.
5336 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5337 (!isR6 &&
5338 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5339 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5340 Register base_reg = has_extra_input
5341 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5342 : ZERO;
5343
5344 // For better instruction scheduling we load the direct code pointer before the method pointer.
5345 switch (code_ptr_location) {
5346 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5347 // T9 = invoke->GetDirectCodePtr();
5348 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5349 break;
5350 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5351 // T9 = code address from literal pool with link-time patch.
5352 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5353 break;
5354 default:
5355 break;
5356 }
5357
5358 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005359 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005360 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005361 uint32_t offset =
5362 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005363 __ LoadFromOffset(kLoadWord,
5364 temp.AsRegister<Register>(),
5365 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005366 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005367 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005368 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005369 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005370 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005371 break;
5372 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5373 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5374 break;
5375 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005376 __ LoadLiteral(temp.AsRegister<Register>(),
5377 base_reg,
5378 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5379 break;
5380 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5381 HMipsDexCacheArraysBase* base =
5382 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5383 int32_t offset =
5384 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5385 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5386 break;
5387 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005388 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005389 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390 Register reg = temp.AsRegister<Register>();
5391 Register method_reg;
5392 if (current_method.IsRegister()) {
5393 method_reg = current_method.AsRegister<Register>();
5394 } else {
5395 // TODO: use the appropriate DCHECK() here if possible.
5396 // DCHECK(invoke->GetLocations()->Intrinsified());
5397 DCHECK(!current_method.IsValid());
5398 method_reg = reg;
5399 __ Lw(reg, SP, kCurrentMethodStackOffset);
5400 }
5401
5402 // temp = temp->dex_cache_resolved_methods_;
5403 __ LoadFromOffset(kLoadWord,
5404 reg,
5405 method_reg,
5406 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005407 // temp = temp[index_in_cache];
5408 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5409 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410 __ LoadFromOffset(kLoadWord,
5411 reg,
5412 reg,
5413 CodeGenerator::GetCachePointerOffset(index_in_cache));
5414 break;
5415 }
5416 }
5417
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005418 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005419 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005420 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005421 break;
5422 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005423 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5424 // T9 prepared above for better instruction scheduling.
5425 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005426 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005427 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005428 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005429 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005430 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005431 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5432 LOG(FATAL) << "Unsupported";
5433 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005434 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5435 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005436 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005437 T9,
5438 callee_method.AsRegister<Register>(),
5439 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005440 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005441 // T9()
5442 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005443 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005444 break;
5445 }
5446 DCHECK(!IsLeafMethod());
5447}
5448
5449void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005450 // Explicit clinit checks triggered by static invokes must have been pruned by
5451 // art::PrepareForRegisterAllocation.
5452 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005453
5454 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5455 return;
5456 }
5457
5458 LocationSummary* locations = invoke->GetLocations();
5459 codegen_->GenerateStaticOrDirectCall(invoke,
5460 locations->HasTemps()
5461 ? locations->GetTemp(0)
5462 : Location::NoLocation());
5463 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5464}
5465
Chris Larsen3acee732015-11-18 13:31:08 -08005466void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005467 // Use the calling convention instead of the location of the receiver, as
5468 // intrinsics may have put the receiver in a different register. In the intrinsics
5469 // slow path, the arguments have been moved to the right place, so here we are
5470 // guaranteed that the receiver is the first register of the calling convention.
5471 InvokeDexCallingConvention calling_convention;
5472 Register receiver = calling_convention.GetRegisterAt(0);
5473
Chris Larsen3acee732015-11-18 13:31:08 -08005474 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005475 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5476 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5477 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005478 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005479
5480 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005481 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005482 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005483 // temp = temp->GetMethodAt(method_offset);
5484 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5485 // T9 = temp->GetEntryPoint();
5486 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5487 // T9();
5488 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005489 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005490}
5491
5492void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5493 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5494 return;
5495 }
5496
5497 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005498 DCHECK(!codegen_->IsLeafMethod());
5499 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5500}
5501
5502void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005503 if (cls->NeedsAccessCheck()) {
5504 InvokeRuntimeCallingConvention calling_convention;
5505 CodeGenerator::CreateLoadClassLocationSummary(
5506 cls,
5507 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5508 Location::RegisterLocation(V0),
5509 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5510 return;
5511 }
5512
5513 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5514 ? LocationSummary::kCallOnSlowPath
5515 : LocationSummary::kNoCall;
5516 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5517 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5518 switch (load_kind) {
5519 // We need an extra register for PC-relative literals on R2.
5520 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5521 case HLoadClass::LoadKind::kBootImageAddress:
5522 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5523 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5524 break;
5525 }
5526 FALLTHROUGH_INTENDED;
5527 // We need an extra register for PC-relative dex cache accesses.
5528 case HLoadClass::LoadKind::kDexCachePcRelative:
5529 case HLoadClass::LoadKind::kReferrersClass:
5530 case HLoadClass::LoadKind::kDexCacheViaMethod:
5531 locations->SetInAt(0, Location::RequiresRegister());
5532 break;
5533 default:
5534 break;
5535 }
5536 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005537}
5538
5539void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5540 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005541 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005542 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005543 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005544 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005545 return;
5546 }
5547
Alexey Frunze06a46c42016-07-19 15:00:40 -07005548 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5549 Location out_loc = locations->Out();
5550 Register out = out_loc.AsRegister<Register>();
5551 Register base_or_current_method_reg;
5552 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5553 switch (load_kind) {
5554 // We need an extra register for PC-relative literals on R2.
5555 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5556 case HLoadClass::LoadKind::kBootImageAddress:
5557 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5558 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5559 break;
5560 // We need an extra register for PC-relative dex cache accesses.
5561 case HLoadClass::LoadKind::kDexCachePcRelative:
5562 case HLoadClass::LoadKind::kReferrersClass:
5563 case HLoadClass::LoadKind::kDexCacheViaMethod:
5564 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5565 break;
5566 default:
5567 base_or_current_method_reg = ZERO;
5568 break;
5569 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005570
Alexey Frunze06a46c42016-07-19 15:00:40 -07005571 bool generate_null_check = false;
5572 switch (load_kind) {
5573 case HLoadClass::LoadKind::kReferrersClass: {
5574 DCHECK(!cls->CanCallRuntime());
5575 DCHECK(!cls->MustGenerateClinitCheck());
5576 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5577 GenerateGcRootFieldLoad(cls,
5578 out_loc,
5579 base_or_current_method_reg,
5580 ArtMethod::DeclaringClassOffset().Int32Value());
5581 break;
5582 }
5583 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5584 DCHECK(!kEmitCompilerReadBarrier);
5585 __ LoadLiteral(out,
5586 base_or_current_method_reg,
5587 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5588 cls->GetTypeIndex()));
5589 break;
5590 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5591 DCHECK(!kEmitCompilerReadBarrier);
5592 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5593 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005594 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005595 break;
5596 }
5597 case HLoadClass::LoadKind::kBootImageAddress: {
5598 DCHECK(!kEmitCompilerReadBarrier);
5599 DCHECK_NE(cls->GetAddress(), 0u);
5600 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5601 __ LoadLiteral(out,
5602 base_or_current_method_reg,
5603 codegen_->DeduplicateBootImageAddressLiteral(address));
5604 break;
5605 }
5606 case HLoadClass::LoadKind::kDexCacheAddress: {
5607 DCHECK_NE(cls->GetAddress(), 0u);
5608 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5609 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5610 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5611 int16_t offset = Low16Bits(address);
5612 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5613 __ Lui(out, High16Bits(base_address));
5614 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5615 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5616 generate_null_check = !cls->IsInDexCache();
5617 break;
5618 }
5619 case HLoadClass::LoadKind::kDexCachePcRelative: {
5620 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5621 int32_t offset =
5622 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5623 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5624 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5625 generate_null_check = !cls->IsInDexCache();
5626 break;
5627 }
5628 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5629 // /* GcRoot<mirror::Class>[] */ out =
5630 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5631 __ LoadFromOffset(kLoadWord,
5632 out,
5633 base_or_current_method_reg,
5634 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5635 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005636 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005637 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5638 generate_null_check = !cls->IsInDexCache();
5639 }
5640 }
5641
5642 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5643 DCHECK(cls->CanCallRuntime());
5644 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5645 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5646 codegen_->AddSlowPath(slow_path);
5647 if (generate_null_check) {
5648 __ Beqz(out, slow_path->GetEntryLabel());
5649 }
5650 if (cls->MustGenerateClinitCheck()) {
5651 GenerateClassInitializationCheck(slow_path, out);
5652 } else {
5653 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005654 }
5655 }
5656}
5657
5658static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005659 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005660}
5661
5662void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5663 LocationSummary* locations =
5664 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5665 locations->SetOut(Location::RequiresRegister());
5666}
5667
5668void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5669 Register out = load->GetLocations()->Out().AsRegister<Register>();
5670 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5671}
5672
5673void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5674 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5675}
5676
5677void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5678 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5679}
5680
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005681void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005682 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005683 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5684 ? LocationSummary::kCallOnMainOnly
5685 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005686 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005687 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005688 HLoadString::LoadKind load_kind = load->GetLoadKind();
5689 switch (load_kind) {
5690 // We need an extra register for PC-relative literals on R2.
5691 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5692 case HLoadString::LoadKind::kBootImageAddress:
5693 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005694 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005695 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5696 break;
5697 }
5698 FALLTHROUGH_INTENDED;
5699 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005700 case HLoadString::LoadKind::kDexCacheViaMethod:
5701 locations->SetInAt(0, Location::RequiresRegister());
5702 break;
5703 default:
5704 break;
5705 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005706 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5707 InvokeRuntimeCallingConvention calling_convention;
5708 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5709 } else {
5710 locations->SetOut(Location::RequiresRegister());
5711 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005712}
5713
5714void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005715 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005716 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005717 Location out_loc = locations->Out();
5718 Register out = out_loc.AsRegister<Register>();
5719 Register base_or_current_method_reg;
5720 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5721 switch (load_kind) {
5722 // We need an extra register for PC-relative literals on R2.
5723 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5724 case HLoadString::LoadKind::kBootImageAddress:
5725 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005726 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005727 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5728 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005729 default:
5730 base_or_current_method_reg = ZERO;
5731 break;
5732 }
5733
5734 switch (load_kind) {
5735 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5736 DCHECK(!kEmitCompilerReadBarrier);
5737 __ LoadLiteral(out,
5738 base_or_current_method_reg,
5739 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5740 load->GetStringIndex()));
5741 return; // No dex cache slow path.
5742 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5743 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005744 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005745 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5746 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005747 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005748 return; // No dex cache slow path.
5749 }
5750 case HLoadString::LoadKind::kBootImageAddress: {
5751 DCHECK(!kEmitCompilerReadBarrier);
5752 DCHECK_NE(load->GetAddress(), 0u);
5753 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5754 __ LoadLiteral(out,
5755 base_or_current_method_reg,
5756 codegen_->DeduplicateBootImageAddressLiteral(address));
5757 return; // No dex cache slow path.
5758 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005759 case HLoadString::LoadKind::kBssEntry: {
5760 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5761 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5762 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5763 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5764 __ LoadFromOffset(kLoadWord, out, out, 0);
5765 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5766 codegen_->AddSlowPath(slow_path);
5767 __ Beqz(out, slow_path->GetEntryLabel());
5768 __ Bind(slow_path->GetExitLabel());
5769 return;
5770 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005771 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005772 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005773 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005774
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005775 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005776 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5777 InvokeRuntimeCallingConvention calling_convention;
5778 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5779 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5780 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781}
5782
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005783void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5785 locations->SetOut(Location::ConstantLocation(constant));
5786}
5787
5788void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5789 // Will be generated at use site.
5790}
5791
5792void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5793 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005794 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005795 InvokeRuntimeCallingConvention calling_convention;
5796 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5797}
5798
5799void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5800 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005801 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005802 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5803 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005804 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805 }
5806 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5807}
5808
5809void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5810 LocationSummary* locations =
5811 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5812 switch (mul->GetResultType()) {
5813 case Primitive::kPrimInt:
5814 case Primitive::kPrimLong:
5815 locations->SetInAt(0, Location::RequiresRegister());
5816 locations->SetInAt(1, Location::RequiresRegister());
5817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5818 break;
5819
5820 case Primitive::kPrimFloat:
5821 case Primitive::kPrimDouble:
5822 locations->SetInAt(0, Location::RequiresFpuRegister());
5823 locations->SetInAt(1, Location::RequiresFpuRegister());
5824 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5825 break;
5826
5827 default:
5828 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5829 }
5830}
5831
5832void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5833 Primitive::Type type = instruction->GetType();
5834 LocationSummary* locations = instruction->GetLocations();
5835 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5836
5837 switch (type) {
5838 case Primitive::kPrimInt: {
5839 Register dst = locations->Out().AsRegister<Register>();
5840 Register lhs = locations->InAt(0).AsRegister<Register>();
5841 Register rhs = locations->InAt(1).AsRegister<Register>();
5842
5843 if (isR6) {
5844 __ MulR6(dst, lhs, rhs);
5845 } else {
5846 __ MulR2(dst, lhs, rhs);
5847 }
5848 break;
5849 }
5850 case Primitive::kPrimLong: {
5851 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5852 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5853 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5854 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5855 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5856 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5857
5858 // Extra checks to protect caused by the existance of A1_A2.
5859 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5860 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5861 DCHECK_NE(dst_high, lhs_low);
5862 DCHECK_NE(dst_high, rhs_low);
5863
5864 // A_B * C_D
5865 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5866 // dst_lo: [ low(B*D) ]
5867 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5868
5869 if (isR6) {
5870 __ MulR6(TMP, lhs_high, rhs_low);
5871 __ MulR6(dst_high, lhs_low, rhs_high);
5872 __ Addu(dst_high, dst_high, TMP);
5873 __ MuhuR6(TMP, lhs_low, rhs_low);
5874 __ Addu(dst_high, dst_high, TMP);
5875 __ MulR6(dst_low, lhs_low, rhs_low);
5876 } else {
5877 __ MulR2(TMP, lhs_high, rhs_low);
5878 __ MulR2(dst_high, lhs_low, rhs_high);
5879 __ Addu(dst_high, dst_high, TMP);
5880 __ MultuR2(lhs_low, rhs_low);
5881 __ Mfhi(TMP);
5882 __ Addu(dst_high, dst_high, TMP);
5883 __ Mflo(dst_low);
5884 }
5885 break;
5886 }
5887 case Primitive::kPrimFloat:
5888 case Primitive::kPrimDouble: {
5889 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5890 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5891 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5892 if (type == Primitive::kPrimFloat) {
5893 __ MulS(dst, lhs, rhs);
5894 } else {
5895 __ MulD(dst, lhs, rhs);
5896 }
5897 break;
5898 }
5899 default:
5900 LOG(FATAL) << "Unexpected mul type " << type;
5901 }
5902}
5903
5904void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5905 LocationSummary* locations =
5906 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5907 switch (neg->GetResultType()) {
5908 case Primitive::kPrimInt:
5909 case Primitive::kPrimLong:
5910 locations->SetInAt(0, Location::RequiresRegister());
5911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5912 break;
5913
5914 case Primitive::kPrimFloat:
5915 case Primitive::kPrimDouble:
5916 locations->SetInAt(0, Location::RequiresFpuRegister());
5917 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5918 break;
5919
5920 default:
5921 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5922 }
5923}
5924
5925void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5926 Primitive::Type type = instruction->GetType();
5927 LocationSummary* locations = instruction->GetLocations();
5928
5929 switch (type) {
5930 case Primitive::kPrimInt: {
5931 Register dst = locations->Out().AsRegister<Register>();
5932 Register src = locations->InAt(0).AsRegister<Register>();
5933 __ Subu(dst, ZERO, src);
5934 break;
5935 }
5936 case Primitive::kPrimLong: {
5937 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5938 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5939 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5940 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5941 __ Subu(dst_low, ZERO, src_low);
5942 __ Sltu(TMP, ZERO, dst_low);
5943 __ Subu(dst_high, ZERO, src_high);
5944 __ Subu(dst_high, dst_high, TMP);
5945 break;
5946 }
5947 case Primitive::kPrimFloat:
5948 case Primitive::kPrimDouble: {
5949 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5950 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5951 if (type == Primitive::kPrimFloat) {
5952 __ NegS(dst, src);
5953 } else {
5954 __ NegD(dst, src);
5955 }
5956 break;
5957 }
5958 default:
5959 LOG(FATAL) << "Unexpected neg type " << type;
5960 }
5961}
5962
5963void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5964 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005965 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005966 InvokeRuntimeCallingConvention calling_convention;
5967 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5968 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5969 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5970 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5971}
5972
5973void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5974 InvokeRuntimeCallingConvention calling_convention;
5975 Register current_method_register = calling_convention.GetRegisterAt(2);
5976 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5977 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005978 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005979 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005980 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5981 void*, uint32_t, int32_t, ArtMethod*>();
5982}
5983
5984void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5985 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005987 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005988 if (instruction->IsStringAlloc()) {
5989 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5990 } else {
5991 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5992 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5993 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005994 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5995}
5996
5997void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005998 if (instruction->IsStringAlloc()) {
5999 // String is allocated through StringFactory. Call NewEmptyString entry point.
6000 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006001 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006002 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6003 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6004 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006005 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006006 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6007 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006008 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006009 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6010 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006011}
6012
6013void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6014 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6015 locations->SetInAt(0, Location::RequiresRegister());
6016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6017}
6018
6019void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6020 Primitive::Type type = instruction->GetType();
6021 LocationSummary* locations = instruction->GetLocations();
6022
6023 switch (type) {
6024 case Primitive::kPrimInt: {
6025 Register dst = locations->Out().AsRegister<Register>();
6026 Register src = locations->InAt(0).AsRegister<Register>();
6027 __ Nor(dst, src, ZERO);
6028 break;
6029 }
6030
6031 case Primitive::kPrimLong: {
6032 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6033 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6034 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6035 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6036 __ Nor(dst_high, src_high, ZERO);
6037 __ Nor(dst_low, src_low, ZERO);
6038 break;
6039 }
6040
6041 default:
6042 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6043 }
6044}
6045
6046void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6048 locations->SetInAt(0, Location::RequiresRegister());
6049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6050}
6051
6052void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6053 LocationSummary* locations = instruction->GetLocations();
6054 __ Xori(locations->Out().AsRegister<Register>(),
6055 locations->InAt(0).AsRegister<Register>(),
6056 1);
6057}
6058
6059void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006060 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6061 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006062}
6063
Calin Juravle2ae48182016-03-16 14:05:09 +00006064void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6065 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006066 return;
6067 }
6068 Location obj = instruction->GetLocations()->InAt(0);
6069
6070 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006071 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006072}
6073
Calin Juravle2ae48182016-03-16 14:05:09 +00006074void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006076 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006077
6078 Location obj = instruction->GetLocations()->InAt(0);
6079
6080 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6081}
6082
6083void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006084 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006085}
6086
6087void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6088 HandleBinaryOp(instruction);
6089}
6090
6091void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6092 HandleBinaryOp(instruction);
6093}
6094
6095void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6096 LOG(FATAL) << "Unreachable";
6097}
6098
6099void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6100 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6101}
6102
6103void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6104 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6105 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6106 if (location.IsStackSlot()) {
6107 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6108 } else if (location.IsDoubleStackSlot()) {
6109 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6110 }
6111 locations->SetOut(location);
6112}
6113
6114void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6115 ATTRIBUTE_UNUSED) {
6116 // Nothing to do, the parameter is already at its location.
6117}
6118
6119void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6120 LocationSummary* locations =
6121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6122 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6123}
6124
6125void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6126 ATTRIBUTE_UNUSED) {
6127 // Nothing to do, the method is already at its location.
6128}
6129
6130void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6131 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006132 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006133 locations->SetInAt(i, Location::Any());
6134 }
6135 locations->SetOut(Location::Any());
6136}
6137
6138void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6139 LOG(FATAL) << "Unreachable";
6140}
6141
6142void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6143 Primitive::Type type = rem->GetResultType();
6144 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006145 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6147
6148 switch (type) {
6149 case Primitive::kPrimInt:
6150 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006151 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6153 break;
6154
6155 case Primitive::kPrimLong: {
6156 InvokeRuntimeCallingConvention calling_convention;
6157 locations->SetInAt(0, Location::RegisterPairLocation(
6158 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6159 locations->SetInAt(1, Location::RegisterPairLocation(
6160 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6161 locations->SetOut(calling_convention.GetReturnLocation(type));
6162 break;
6163 }
6164
6165 case Primitive::kPrimFloat:
6166 case Primitive::kPrimDouble: {
6167 InvokeRuntimeCallingConvention calling_convention;
6168 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6169 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6170 locations->SetOut(calling_convention.GetReturnLocation(type));
6171 break;
6172 }
6173
6174 default:
6175 LOG(FATAL) << "Unexpected rem type " << type;
6176 }
6177}
6178
6179void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6180 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006181
6182 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006183 case Primitive::kPrimInt:
6184 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006185 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006187 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6189 break;
6190 }
6191 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006192 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006193 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006194 break;
6195 }
6196 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006197 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006198 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006199 break;
6200 }
6201 default:
6202 LOG(FATAL) << "Unexpected rem type " << type;
6203 }
6204}
6205
6206void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6207 memory_barrier->SetLocations(nullptr);
6208}
6209
6210void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6211 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6212}
6213
6214void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6215 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6216 Primitive::Type return_type = ret->InputAt(0)->GetType();
6217 locations->SetInAt(0, MipsReturnLocation(return_type));
6218}
6219
6220void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6221 codegen_->GenerateFrameExit();
6222}
6223
6224void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6225 ret->SetLocations(nullptr);
6226}
6227
6228void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6229 codegen_->GenerateFrameExit();
6230}
6231
Alexey Frunze92d90602015-12-18 18:16:36 -08006232void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6233 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006234}
6235
Alexey Frunze92d90602015-12-18 18:16:36 -08006236void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6237 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006238}
6239
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006240void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6241 HandleShift(shl);
6242}
6243
6244void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6245 HandleShift(shl);
6246}
6247
6248void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6249 HandleShift(shr);
6250}
6251
6252void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6253 HandleShift(shr);
6254}
6255
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006256void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6257 HandleBinaryOp(instruction);
6258}
6259
6260void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6261 HandleBinaryOp(instruction);
6262}
6263
6264void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6265 HandleFieldGet(instruction, instruction->GetFieldInfo());
6266}
6267
6268void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6269 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6270}
6271
6272void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6273 HandleFieldSet(instruction, instruction->GetFieldInfo());
6274}
6275
6276void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6277 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6278}
6279
6280void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6281 HUnresolvedInstanceFieldGet* instruction) {
6282 FieldAccessCallingConventionMIPS calling_convention;
6283 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6284 instruction->GetFieldType(),
6285 calling_convention);
6286}
6287
6288void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6289 HUnresolvedInstanceFieldGet* instruction) {
6290 FieldAccessCallingConventionMIPS calling_convention;
6291 codegen_->GenerateUnresolvedFieldAccess(instruction,
6292 instruction->GetFieldType(),
6293 instruction->GetFieldIndex(),
6294 instruction->GetDexPc(),
6295 calling_convention);
6296}
6297
6298void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6299 HUnresolvedInstanceFieldSet* instruction) {
6300 FieldAccessCallingConventionMIPS calling_convention;
6301 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6302 instruction->GetFieldType(),
6303 calling_convention);
6304}
6305
6306void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6307 HUnresolvedInstanceFieldSet* instruction) {
6308 FieldAccessCallingConventionMIPS calling_convention;
6309 codegen_->GenerateUnresolvedFieldAccess(instruction,
6310 instruction->GetFieldType(),
6311 instruction->GetFieldIndex(),
6312 instruction->GetDexPc(),
6313 calling_convention);
6314}
6315
6316void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6317 HUnresolvedStaticFieldGet* instruction) {
6318 FieldAccessCallingConventionMIPS calling_convention;
6319 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6320 instruction->GetFieldType(),
6321 calling_convention);
6322}
6323
6324void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6325 HUnresolvedStaticFieldGet* instruction) {
6326 FieldAccessCallingConventionMIPS calling_convention;
6327 codegen_->GenerateUnresolvedFieldAccess(instruction,
6328 instruction->GetFieldType(),
6329 instruction->GetFieldIndex(),
6330 instruction->GetDexPc(),
6331 calling_convention);
6332}
6333
6334void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6335 HUnresolvedStaticFieldSet* instruction) {
6336 FieldAccessCallingConventionMIPS calling_convention;
6337 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6338 instruction->GetFieldType(),
6339 calling_convention);
6340}
6341
6342void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6343 HUnresolvedStaticFieldSet* instruction) {
6344 FieldAccessCallingConventionMIPS calling_convention;
6345 codegen_->GenerateUnresolvedFieldAccess(instruction,
6346 instruction->GetFieldType(),
6347 instruction->GetFieldIndex(),
6348 instruction->GetDexPc(),
6349 calling_convention);
6350}
6351
6352void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006353 LocationSummary* locations =
6354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006355 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006356}
6357
6358void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6359 HBasicBlock* block = instruction->GetBlock();
6360 if (block->GetLoopInformation() != nullptr) {
6361 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6362 // The back edge will generate the suspend check.
6363 return;
6364 }
6365 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6366 // The goto will generate the suspend check.
6367 return;
6368 }
6369 GenerateSuspendCheck(instruction, nullptr);
6370}
6371
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006372void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6373 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375 InvokeRuntimeCallingConvention calling_convention;
6376 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6377}
6378
6379void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006380 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6382}
6383
6384void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6385 Primitive::Type input_type = conversion->GetInputType();
6386 Primitive::Type result_type = conversion->GetResultType();
6387 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006388 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006389
6390 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6391 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6392 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6393 }
6394
6395 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006396 if (!isR6 &&
6397 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6398 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006399 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400 }
6401
6402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6403
6404 if (call_kind == LocationSummary::kNoCall) {
6405 if (Primitive::IsFloatingPointType(input_type)) {
6406 locations->SetInAt(0, Location::RequiresFpuRegister());
6407 } else {
6408 locations->SetInAt(0, Location::RequiresRegister());
6409 }
6410
6411 if (Primitive::IsFloatingPointType(result_type)) {
6412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6413 } else {
6414 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6415 }
6416 } else {
6417 InvokeRuntimeCallingConvention calling_convention;
6418
6419 if (Primitive::IsFloatingPointType(input_type)) {
6420 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6421 } else {
6422 DCHECK_EQ(input_type, Primitive::kPrimLong);
6423 locations->SetInAt(0, Location::RegisterPairLocation(
6424 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6425 }
6426
6427 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6428 }
6429}
6430
6431void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6432 LocationSummary* locations = conversion->GetLocations();
6433 Primitive::Type result_type = conversion->GetResultType();
6434 Primitive::Type input_type = conversion->GetInputType();
6435 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006436 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437
6438 DCHECK_NE(input_type, result_type);
6439
6440 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6441 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6442 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6443 Register src = locations->InAt(0).AsRegister<Register>();
6444
Alexey Frunzea871ef12016-06-27 15:20:11 -07006445 if (dst_low != src) {
6446 __ Move(dst_low, src);
6447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006448 __ Sra(dst_high, src, 31);
6449 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6450 Register dst = locations->Out().AsRegister<Register>();
6451 Register src = (input_type == Primitive::kPrimLong)
6452 ? locations->InAt(0).AsRegisterPairLow<Register>()
6453 : locations->InAt(0).AsRegister<Register>();
6454
6455 switch (result_type) {
6456 case Primitive::kPrimChar:
6457 __ Andi(dst, src, 0xFFFF);
6458 break;
6459 case Primitive::kPrimByte:
6460 if (has_sign_extension) {
6461 __ Seb(dst, src);
6462 } else {
6463 __ Sll(dst, src, 24);
6464 __ Sra(dst, dst, 24);
6465 }
6466 break;
6467 case Primitive::kPrimShort:
6468 if (has_sign_extension) {
6469 __ Seh(dst, src);
6470 } else {
6471 __ Sll(dst, src, 16);
6472 __ Sra(dst, dst, 16);
6473 }
6474 break;
6475 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006476 if (dst != src) {
6477 __ Move(dst, src);
6478 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479 break;
6480
6481 default:
6482 LOG(FATAL) << "Unexpected type conversion from " << input_type
6483 << " to " << result_type;
6484 }
6485 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006486 if (input_type == Primitive::kPrimLong) {
6487 if (isR6) {
6488 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6489 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6490 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6491 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6492 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6493 __ Mtc1(src_low, FTMP);
6494 __ Mthc1(src_high, FTMP);
6495 if (result_type == Primitive::kPrimFloat) {
6496 __ Cvtsl(dst, FTMP);
6497 } else {
6498 __ Cvtdl(dst, FTMP);
6499 }
6500 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006501 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6502 : kQuickL2d;
6503 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006504 if (result_type == Primitive::kPrimFloat) {
6505 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6506 } else {
6507 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6508 }
6509 }
6510 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 Register src = locations->InAt(0).AsRegister<Register>();
6512 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6513 __ Mtc1(src, FTMP);
6514 if (result_type == Primitive::kPrimFloat) {
6515 __ Cvtsw(dst, FTMP);
6516 } else {
6517 __ Cvtdw(dst, FTMP);
6518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006519 }
6520 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6521 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006522 if (result_type == Primitive::kPrimLong) {
6523 if (isR6) {
6524 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6525 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6526 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6527 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6528 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6529 MipsLabel truncate;
6530 MipsLabel done;
6531
6532 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6533 // value when the input is either a NaN or is outside of the range of the output type
6534 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6535 // the same result.
6536 //
6537 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6538 // value of the output type if the input is outside of the range after the truncation or
6539 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6540 // results. This matches the desired float/double-to-int/long conversion exactly.
6541 //
6542 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6543 //
6544 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6545 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6546 // even though it must be NAN2008=1 on R6.
6547 //
6548 // The code takes care of the different behaviors by first comparing the input to the
6549 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6550 // If the input is greater than or equal to the minimum, it procedes to the truncate
6551 // instruction, which will handle such an input the same way irrespective of NAN2008.
6552 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6553 // in order to return either zero or the minimum value.
6554 //
6555 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6556 // truncate instruction for MIPS64R6.
6557 if (input_type == Primitive::kPrimFloat) {
6558 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6559 __ LoadConst32(TMP, min_val);
6560 __ Mtc1(TMP, FTMP);
6561 __ CmpLeS(FTMP, FTMP, src);
6562 } else {
6563 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6564 __ LoadConst32(TMP, High32Bits(min_val));
6565 __ Mtc1(ZERO, FTMP);
6566 __ Mthc1(TMP, FTMP);
6567 __ CmpLeD(FTMP, FTMP, src);
6568 }
6569
6570 __ Bc1nez(FTMP, &truncate);
6571
6572 if (input_type == Primitive::kPrimFloat) {
6573 __ CmpEqS(FTMP, src, src);
6574 } else {
6575 __ CmpEqD(FTMP, src, src);
6576 }
6577 __ Move(dst_low, ZERO);
6578 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6579 __ Mfc1(TMP, FTMP);
6580 __ And(dst_high, dst_high, TMP);
6581
6582 __ B(&done);
6583
6584 __ Bind(&truncate);
6585
6586 if (input_type == Primitive::kPrimFloat) {
6587 __ TruncLS(FTMP, src);
6588 } else {
6589 __ TruncLD(FTMP, src);
6590 }
6591 __ Mfc1(dst_low, FTMP);
6592 __ Mfhc1(dst_high, FTMP);
6593
6594 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006596 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6597 : kQuickD2l;
6598 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006599 if (input_type == Primitive::kPrimFloat) {
6600 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6601 } else {
6602 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6603 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006604 }
6605 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006606 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6607 Register dst = locations->Out().AsRegister<Register>();
6608 MipsLabel truncate;
6609 MipsLabel done;
6610
6611 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6612 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6613 // even though it must be NAN2008=1 on R6.
6614 //
6615 // For details see the large comment above for the truncation of float/double to long on R6.
6616 //
6617 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6618 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006619 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006620 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6621 __ LoadConst32(TMP, min_val);
6622 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006623 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006624 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6625 __ LoadConst32(TMP, High32Bits(min_val));
6626 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006627 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006628 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006629
6630 if (isR6) {
6631 if (input_type == Primitive::kPrimFloat) {
6632 __ CmpLeS(FTMP, FTMP, src);
6633 } else {
6634 __ CmpLeD(FTMP, FTMP, src);
6635 }
6636 __ Bc1nez(FTMP, &truncate);
6637
6638 if (input_type == Primitive::kPrimFloat) {
6639 __ CmpEqS(FTMP, src, src);
6640 } else {
6641 __ CmpEqD(FTMP, src, src);
6642 }
6643 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6644 __ Mfc1(TMP, FTMP);
6645 __ And(dst, dst, TMP);
6646 } else {
6647 if (input_type == Primitive::kPrimFloat) {
6648 __ ColeS(0, FTMP, src);
6649 } else {
6650 __ ColeD(0, FTMP, src);
6651 }
6652 __ Bc1t(0, &truncate);
6653
6654 if (input_type == Primitive::kPrimFloat) {
6655 __ CeqS(0, src, src);
6656 } else {
6657 __ CeqD(0, src, src);
6658 }
6659 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6660 __ Movf(dst, ZERO, 0);
6661 }
6662
6663 __ B(&done);
6664
6665 __ Bind(&truncate);
6666
6667 if (input_type == Primitive::kPrimFloat) {
6668 __ TruncWS(FTMP, src);
6669 } else {
6670 __ TruncWD(FTMP, src);
6671 }
6672 __ Mfc1(dst, FTMP);
6673
6674 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006675 }
6676 } else if (Primitive::IsFloatingPointType(result_type) &&
6677 Primitive::IsFloatingPointType(input_type)) {
6678 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6679 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6680 if (result_type == Primitive::kPrimFloat) {
6681 __ Cvtsd(dst, src);
6682 } else {
6683 __ Cvtds(dst, src);
6684 }
6685 } else {
6686 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6687 << " to " << result_type;
6688 }
6689}
6690
6691void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6692 HandleShift(ushr);
6693}
6694
6695void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6696 HandleShift(ushr);
6697}
6698
6699void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6700 HandleBinaryOp(instruction);
6701}
6702
6703void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6704 HandleBinaryOp(instruction);
6705}
6706
6707void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6708 // Nothing to do, this should be removed during prepare for register allocator.
6709 LOG(FATAL) << "Unreachable";
6710}
6711
6712void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6713 // Nothing to do, this should be removed during prepare for register allocator.
6714 LOG(FATAL) << "Unreachable";
6715}
6716
6717void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006718 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719}
6720
6721void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006722 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723}
6724
6725void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006726 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006727}
6728
6729void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006730 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731}
6732
6733void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006734 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735}
6736
6737void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006738 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739}
6740
6741void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006742 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006743}
6744
6745void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006746 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747}
6748
6749void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006750 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751}
6752
6753void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006754 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755}
6756
6757void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006758 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006759}
6760
6761void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006762 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006763}
6764
6765void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006766 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006767}
6768
6769void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006770 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006771}
6772
6773void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006774 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006775}
6776
6777void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006778 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006779}
6780
6781void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006782 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006783}
6784
6785void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006786 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006787}
6788
6789void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006790 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006791}
6792
6793void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006794 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006795}
6796
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6798 LocationSummary* locations =
6799 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6800 locations->SetInAt(0, Location::RequiresRegister());
6801}
6802
Alexey Frunze96b66822016-09-10 02:32:44 -07006803void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6804 int32_t lower_bound,
6805 uint32_t num_entries,
6806 HBasicBlock* switch_block,
6807 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006808 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006809 Register temp_reg = TMP;
6810 __ Addiu32(temp_reg, value_reg, -lower_bound);
6811 // Jump to default if index is negative
6812 // Note: We don't check the case that index is positive while value < lower_bound, because in
6813 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6814 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6815
Alexey Frunze96b66822016-09-10 02:32:44 -07006816 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006817 // Jump to successors[0] if value == lower_bound.
6818 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6819 int32_t last_index = 0;
6820 for (; num_entries - last_index > 2; last_index += 2) {
6821 __ Addiu(temp_reg, temp_reg, -2);
6822 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6823 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6824 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6825 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6826 }
6827 if (num_entries - last_index == 2) {
6828 // The last missing case_value.
6829 __ Addiu(temp_reg, temp_reg, -1);
6830 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006831 }
6832
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006833 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006834 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006835 __ B(codegen_->GetLabelOf(default_block));
6836 }
6837}
6838
Alexey Frunze96b66822016-09-10 02:32:44 -07006839void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6840 Register constant_area,
6841 int32_t lower_bound,
6842 uint32_t num_entries,
6843 HBasicBlock* switch_block,
6844 HBasicBlock* default_block) {
6845 // Create a jump table.
6846 std::vector<MipsLabel*> labels(num_entries);
6847 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6848 for (uint32_t i = 0; i < num_entries; i++) {
6849 labels[i] = codegen_->GetLabelOf(successors[i]);
6850 }
6851 JumpTable* table = __ CreateJumpTable(std::move(labels));
6852
6853 // Is the value in range?
6854 __ Addiu32(TMP, value_reg, -lower_bound);
6855 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6856 __ Sltiu(AT, TMP, num_entries);
6857 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6858 } else {
6859 __ LoadConst32(AT, num_entries);
6860 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6861 }
6862
6863 // We are in the range of the table.
6864 // Load the target address from the jump table, indexing by the value.
6865 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6866 __ Sll(TMP, TMP, 2);
6867 __ Addu(TMP, TMP, AT);
6868 __ Lw(TMP, TMP, 0);
6869 // Compute the absolute target address by adding the table start address
6870 // (the table contains offsets to targets relative to its start).
6871 __ Addu(TMP, TMP, AT);
6872 // And jump.
6873 __ Jr(TMP);
6874 __ NopIfNoReordering();
6875}
6876
6877void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6878 int32_t lower_bound = switch_instr->GetStartValue();
6879 uint32_t num_entries = switch_instr->GetNumEntries();
6880 LocationSummary* locations = switch_instr->GetLocations();
6881 Register value_reg = locations->InAt(0).AsRegister<Register>();
6882 HBasicBlock* switch_block = switch_instr->GetBlock();
6883 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6884
6885 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6886 num_entries > kPackedSwitchJumpTableThreshold) {
6887 // R6 uses PC-relative addressing to access the jump table.
6888 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6889 // the jump table and it is implemented by changing HPackedSwitch to
6890 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6891 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6892 GenTableBasedPackedSwitch(value_reg,
6893 ZERO,
6894 lower_bound,
6895 num_entries,
6896 switch_block,
6897 default_block);
6898 } else {
6899 GenPackedSwitchWithCompares(value_reg,
6900 lower_bound,
6901 num_entries,
6902 switch_block,
6903 default_block);
6904 }
6905}
6906
6907void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6908 LocationSummary* locations =
6909 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6910 locations->SetInAt(0, Location::RequiresRegister());
6911 // Constant area pointer (HMipsComputeBaseMethodAddress).
6912 locations->SetInAt(1, Location::RequiresRegister());
6913}
6914
6915void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6916 int32_t lower_bound = switch_instr->GetStartValue();
6917 uint32_t num_entries = switch_instr->GetNumEntries();
6918 LocationSummary* locations = switch_instr->GetLocations();
6919 Register value_reg = locations->InAt(0).AsRegister<Register>();
6920 Register constant_area = locations->InAt(1).AsRegister<Register>();
6921 HBasicBlock* switch_block = switch_instr->GetBlock();
6922 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6923
6924 // This is an R2-only path. HPackedSwitch has been changed to
6925 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6926 // required to address the jump table relative to PC.
6927 GenTableBasedPackedSwitch(value_reg,
6928 constant_area,
6929 lower_bound,
6930 num_entries,
6931 switch_block,
6932 default_block);
6933}
6934
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006935void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6936 HMipsComputeBaseMethodAddress* insn) {
6937 LocationSummary* locations =
6938 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6939 locations->SetOut(Location::RequiresRegister());
6940}
6941
6942void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6943 HMipsComputeBaseMethodAddress* insn) {
6944 LocationSummary* locations = insn->GetLocations();
6945 Register reg = locations->Out().AsRegister<Register>();
6946
6947 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6948
6949 // Generate a dummy PC-relative call to obtain PC.
6950 __ Nal();
6951 // Grab the return address off RA.
6952 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006953 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006954
6955 // Remember this offset (the obtained PC value) for later use with constant area.
6956 __ BindPcRelBaseLabel();
6957}
6958
6959void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6961 locations->SetOut(Location::RequiresRegister());
6962}
6963
6964void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6965 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6966 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6967 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006968 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6969 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006970}
6971
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006972void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6973 // The trampoline uses the same calling convention as dex calling conventions,
6974 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6975 // the method_idx.
6976 HandleInvoke(invoke);
6977}
6978
6979void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6980 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6981}
6982
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006983void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6984 LocationSummary* locations =
6985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6986 locations->SetInAt(0, Location::RequiresRegister());
6987 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006988}
6989
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006990void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6991 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006992 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006993 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006994 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006995 __ LoadFromOffset(kLoadWord,
6996 locations->Out().AsRegister<Register>(),
6997 locations->InAt(0).AsRegister<Register>(),
6998 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006999 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007000 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007001 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007002 __ LoadFromOffset(kLoadWord,
7003 locations->Out().AsRegister<Register>(),
7004 locations->InAt(0).AsRegister<Register>(),
7005 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007006 __ LoadFromOffset(kLoadWord,
7007 locations->Out().AsRegister<Register>(),
7008 locations->Out().AsRegister<Register>(),
7009 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007010 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007011}
7012
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007013#undef __
7014#undef QUICK_ENTRY_POINT
7015
7016} // namespace mips
7017} // namespace art