blob: 573bb507f2c9c21acba0be6bf94181ee9fae2821 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Alexey Frunzee3fb2452016-05-10 16:08:05 -070043// We'll maximize the range of a single load instruction for dex cache array accesses
44// by aligning offset -32768 with the offset of the first used element.
45static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
46
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020047Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100149// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
150#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700151#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152
153class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
154 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000155 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200156
157 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
158 LocationSummary* locations = instruction_->GetLocations();
159 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
160 __ Bind(GetEntryLabel());
161 if (instruction_->CanThrowIntoCatchBlock()) {
162 // Live registers will be restored in the catch block if caught.
163 SaveLiveRegisters(codegen, instruction_->GetLocations());
164 }
165 // We're moving two locations to locations that could overlap, so we need a parallel
166 // move resolver.
167 InvokeRuntimeCallingConvention calling_convention;
168 codegen->EmitParallelMoves(locations->InAt(0),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
170 Primitive::kPrimInt,
171 locations->InAt(1),
172 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
173 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100174 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
175 ? kQuickThrowStringBounds
176 : kQuickThrowArrayBounds;
177 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100178 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200179 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
180 }
181
182 bool IsFatal() const OVERRIDE { return true; }
183
184 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
185
186 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200187 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
188};
189
190class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
191 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000192 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193
194 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
196 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100197 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
199 }
200
201 bool IsFatal() const OVERRIDE { return true; }
202
203 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
204
205 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
207};
208
209class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
210 public:
211 LoadClassSlowPathMIPS(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000215 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = at_->GetLocations();
221 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
222
223 __ Bind(GetEntryLabel());
224 SaveLiveRegisters(codegen, locations);
225
226 InvokeRuntimeCallingConvention calling_convention;
227 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
228
Serban Constantinescufca16662016-07-14 09:21:59 +0100229 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
230 : kQuickInitializeType;
231 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 if (do_clinit_) {
233 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
234 } else {
235 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
236 }
237
238 // Move the class to the desired location.
239 Location out = locations->Out();
240 if (out.IsValid()) {
241 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
242 Primitive::Type type = at_->GetType();
243 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
244 }
245
246 RestoreLiveRegisters(codegen, locations);
247 __ B(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
251
252 private:
253 // The class this slow path will load.
254 HLoadClass* const cls_;
255
256 // The instruction where this slow path is happening.
257 // (Might be the load class or an initialization check).
258 HInstruction* const at_;
259
260 // The dex PC of `at_`.
261 const uint32_t dex_pc_;
262
263 // Whether to initialize the class.
264 const bool do_clinit_;
265
266 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
267};
268
269class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
270 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000271 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272
273 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
274 LocationSummary* locations = instruction_->GetLocations();
275 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
276 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
277
278 __ Bind(GetEntryLabel());
279 SaveLiveRegisters(codegen, locations);
280
281 InvokeRuntimeCallingConvention calling_convention;
Vladimir 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,
1059 target_type.type_index));
1060 }
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(
1076 const DexFile& dex_file, uint32_t type_index) {
1077 return NewPcRelativePatch(dex_file, type_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,
1120 uint32_t type_index) {
1121 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;
5210 }
5211 if (fallback_load) {
5212 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5213 }
5214 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005215}
5216
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005217HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5218 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005219 if (kEmitCompilerReadBarrier) {
5220 UNIMPLEMENTED(FATAL) << "for read barrier";
5221 }
5222 // We disable pc-relative load when there is an irreducible loop, as the optimization
5223 // is incompatible with it.
5224 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5225 bool fallback_load = has_irreducible_loops;
5226 switch (desired_class_load_kind) {
5227 case HLoadClass::LoadKind::kReferrersClass:
5228 fallback_load = false;
5229 break;
5230 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5231 DCHECK(!GetCompilerOptions().GetCompilePic());
5232 break;
5233 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5234 DCHECK(GetCompilerOptions().GetCompilePic());
5235 break;
5236 case HLoadClass::LoadKind::kBootImageAddress:
5237 break;
5238 case HLoadClass::LoadKind::kDexCacheAddress:
5239 DCHECK(Runtime::Current()->UseJitCompilation());
5240 fallback_load = false;
5241 break;
5242 case HLoadClass::LoadKind::kDexCachePcRelative:
5243 DCHECK(!Runtime::Current()->UseJitCompilation());
5244 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5245 // with irreducible loops.
5246 break;
5247 case HLoadClass::LoadKind::kDexCacheViaMethod:
5248 fallback_load = false;
5249 break;
5250 }
5251 if (fallback_load) {
5252 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5253 }
5254 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005255}
5256
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005257Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5258 Register temp) {
5259 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5260 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5261 if (!invoke->GetLocations()->Intrinsified()) {
5262 return location.AsRegister<Register>();
5263 }
5264 // For intrinsics we allow any location, so it may be on the stack.
5265 if (!location.IsRegister()) {
5266 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5267 return temp;
5268 }
5269 // For register locations, check if the register was saved. If so, get it from the stack.
5270 // Note: There is a chance that the register was saved but not overwritten, so we could
5271 // save one load. However, since this is just an intrinsic slow path we prefer this
5272 // simple and more robust approach rather that trying to determine if that's the case.
5273 SlowPathCode* slow_path = GetCurrentSlowPath();
5274 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5275 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5276 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5277 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5278 return temp;
5279 }
5280 return location.AsRegister<Register>();
5281}
5282
Vladimir Markodc151b22015-10-15 18:02:30 +01005283HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5284 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005285 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005286 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5287 // We disable PC-relative load when there is an irreducible loop, as the optimization
5288 // is incompatible with it.
5289 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5290 bool fallback_load = true;
5291 bool fallback_call = true;
5292 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005293 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5294 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005295 fallback_load = has_irreducible_loops;
5296 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005297 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005298 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005299 break;
5300 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005301 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005302 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005303 fallback_call = has_irreducible_loops;
5304 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005305 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005306 // TODO: Implement this type.
5307 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005308 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 fallback_call = false;
5310 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005311 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005312 if (fallback_load) {
5313 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5314 dispatch_info.method_load_data = 0;
5315 }
5316 if (fallback_call) {
5317 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5318 dispatch_info.direct_code_ptr = 0;
5319 }
5320 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005321}
5322
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005323void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5324 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005325 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005326 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5327 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5328 bool isR6 = isa_features_.IsR6();
5329 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5330 // R6 has PC-relative addressing.
5331 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5332 (!isR6 &&
5333 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5334 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5335 Register base_reg = has_extra_input
5336 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5337 : ZERO;
5338
5339 // For better instruction scheduling we load the direct code pointer before the method pointer.
5340 switch (code_ptr_location) {
5341 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5342 // T9 = invoke->GetDirectCodePtr();
5343 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5344 break;
5345 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5346 // T9 = code address from literal pool with link-time patch.
5347 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5348 break;
5349 default:
5350 break;
5351 }
5352
5353 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005354 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005355 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005356 uint32_t offset =
5357 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005358 __ LoadFromOffset(kLoadWord,
5359 temp.AsRegister<Register>(),
5360 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005361 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005362 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005364 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005365 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005366 break;
5367 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5368 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5369 break;
5370 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005371 __ LoadLiteral(temp.AsRegister<Register>(),
5372 base_reg,
5373 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5374 break;
5375 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5376 HMipsDexCacheArraysBase* base =
5377 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5378 int32_t offset =
5379 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5380 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5381 break;
5382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005383 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005384 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005385 Register reg = temp.AsRegister<Register>();
5386 Register method_reg;
5387 if (current_method.IsRegister()) {
5388 method_reg = current_method.AsRegister<Register>();
5389 } else {
5390 // TODO: use the appropriate DCHECK() here if possible.
5391 // DCHECK(invoke->GetLocations()->Intrinsified());
5392 DCHECK(!current_method.IsValid());
5393 method_reg = reg;
5394 __ Lw(reg, SP, kCurrentMethodStackOffset);
5395 }
5396
5397 // temp = temp->dex_cache_resolved_methods_;
5398 __ LoadFromOffset(kLoadWord,
5399 reg,
5400 method_reg,
5401 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005402 // temp = temp[index_in_cache];
5403 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5404 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005405 __ LoadFromOffset(kLoadWord,
5406 reg,
5407 reg,
5408 CodeGenerator::GetCachePointerOffset(index_in_cache));
5409 break;
5410 }
5411 }
5412
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005413 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005414 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005415 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005416 break;
5417 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005418 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5419 // T9 prepared above for better instruction scheduling.
5420 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005421 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005422 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005423 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005424 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005425 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005426 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5427 LOG(FATAL) << "Unsupported";
5428 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5430 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005431 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005432 T9,
5433 callee_method.AsRegister<Register>(),
5434 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005435 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005436 // T9()
5437 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005438 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005439 break;
5440 }
5441 DCHECK(!IsLeafMethod());
5442}
5443
5444void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005445 // Explicit clinit checks triggered by static invokes must have been pruned by
5446 // art::PrepareForRegisterAllocation.
5447 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005448
5449 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5450 return;
5451 }
5452
5453 LocationSummary* locations = invoke->GetLocations();
5454 codegen_->GenerateStaticOrDirectCall(invoke,
5455 locations->HasTemps()
5456 ? locations->GetTemp(0)
5457 : Location::NoLocation());
5458 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5459}
5460
Chris Larsen3acee732015-11-18 13:31:08 -08005461void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005462 // Use the calling convention instead of the location of the receiver, as
5463 // intrinsics may have put the receiver in a different register. In the intrinsics
5464 // slow path, the arguments have been moved to the right place, so here we are
5465 // guaranteed that the receiver is the first register of the calling convention.
5466 InvokeDexCallingConvention calling_convention;
5467 Register receiver = calling_convention.GetRegisterAt(0);
5468
Chris Larsen3acee732015-11-18 13:31:08 -08005469 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005470 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5471 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5472 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005473 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005474
5475 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005476 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005477 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005478 // temp = temp->GetMethodAt(method_offset);
5479 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5480 // T9 = temp->GetEntryPoint();
5481 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5482 // T9();
5483 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005484 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005485}
5486
5487void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5488 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5489 return;
5490 }
5491
5492 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005493 DCHECK(!codegen_->IsLeafMethod());
5494 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5495}
5496
5497void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005498 if (cls->NeedsAccessCheck()) {
5499 InvokeRuntimeCallingConvention calling_convention;
5500 CodeGenerator::CreateLoadClassLocationSummary(
5501 cls,
5502 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5503 Location::RegisterLocation(V0),
5504 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5505 return;
5506 }
5507
5508 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5509 ? LocationSummary::kCallOnSlowPath
5510 : LocationSummary::kNoCall;
5511 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5512 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5513 switch (load_kind) {
5514 // We need an extra register for PC-relative literals on R2.
5515 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5516 case HLoadClass::LoadKind::kBootImageAddress:
5517 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5518 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5519 break;
5520 }
5521 FALLTHROUGH_INTENDED;
5522 // We need an extra register for PC-relative dex cache accesses.
5523 case HLoadClass::LoadKind::kDexCachePcRelative:
5524 case HLoadClass::LoadKind::kReferrersClass:
5525 case HLoadClass::LoadKind::kDexCacheViaMethod:
5526 locations->SetInAt(0, Location::RequiresRegister());
5527 break;
5528 default:
5529 break;
5530 }
5531 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005532}
5533
5534void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5535 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005536 if (cls->NeedsAccessCheck()) {
5537 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005538 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005539 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005540 return;
5541 }
5542
Alexey Frunze06a46c42016-07-19 15:00:40 -07005543 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5544 Location out_loc = locations->Out();
5545 Register out = out_loc.AsRegister<Register>();
5546 Register base_or_current_method_reg;
5547 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5548 switch (load_kind) {
5549 // We need an extra register for PC-relative literals on R2.
5550 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5551 case HLoadClass::LoadKind::kBootImageAddress:
5552 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5553 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5554 break;
5555 // We need an extra register for PC-relative dex cache accesses.
5556 case HLoadClass::LoadKind::kDexCachePcRelative:
5557 case HLoadClass::LoadKind::kReferrersClass:
5558 case HLoadClass::LoadKind::kDexCacheViaMethod:
5559 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5560 break;
5561 default:
5562 base_or_current_method_reg = ZERO;
5563 break;
5564 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005565
Alexey Frunze06a46c42016-07-19 15:00:40 -07005566 bool generate_null_check = false;
5567 switch (load_kind) {
5568 case HLoadClass::LoadKind::kReferrersClass: {
5569 DCHECK(!cls->CanCallRuntime());
5570 DCHECK(!cls->MustGenerateClinitCheck());
5571 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5572 GenerateGcRootFieldLoad(cls,
5573 out_loc,
5574 base_or_current_method_reg,
5575 ArtMethod::DeclaringClassOffset().Int32Value());
5576 break;
5577 }
5578 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5579 DCHECK(!kEmitCompilerReadBarrier);
5580 __ LoadLiteral(out,
5581 base_or_current_method_reg,
5582 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5583 cls->GetTypeIndex()));
5584 break;
5585 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5586 DCHECK(!kEmitCompilerReadBarrier);
5587 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5588 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005589 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005590 break;
5591 }
5592 case HLoadClass::LoadKind::kBootImageAddress: {
5593 DCHECK(!kEmitCompilerReadBarrier);
5594 DCHECK_NE(cls->GetAddress(), 0u);
5595 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5596 __ LoadLiteral(out,
5597 base_or_current_method_reg,
5598 codegen_->DeduplicateBootImageAddressLiteral(address));
5599 break;
5600 }
5601 case HLoadClass::LoadKind::kDexCacheAddress: {
5602 DCHECK_NE(cls->GetAddress(), 0u);
5603 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5604 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5605 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5606 int16_t offset = Low16Bits(address);
5607 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5608 __ Lui(out, High16Bits(base_address));
5609 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5610 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5611 generate_null_check = !cls->IsInDexCache();
5612 break;
5613 }
5614 case HLoadClass::LoadKind::kDexCachePcRelative: {
5615 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5616 int32_t offset =
5617 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5618 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5619 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5620 generate_null_check = !cls->IsInDexCache();
5621 break;
5622 }
5623 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5624 // /* GcRoot<mirror::Class>[] */ out =
5625 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5626 __ LoadFromOffset(kLoadWord,
5627 out,
5628 base_or_current_method_reg,
5629 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5630 // /* GcRoot<mirror::Class> */ out = out[type_index]
5631 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5632 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5633 generate_null_check = !cls->IsInDexCache();
5634 }
5635 }
5636
5637 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5638 DCHECK(cls->CanCallRuntime());
5639 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5640 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5641 codegen_->AddSlowPath(slow_path);
5642 if (generate_null_check) {
5643 __ Beqz(out, slow_path->GetEntryLabel());
5644 }
5645 if (cls->MustGenerateClinitCheck()) {
5646 GenerateClassInitializationCheck(slow_path, out);
5647 } else {
5648 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005649 }
5650 }
5651}
5652
5653static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005654 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005655}
5656
5657void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5658 LocationSummary* locations =
5659 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5660 locations->SetOut(Location::RequiresRegister());
5661}
5662
5663void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5664 Register out = load->GetLocations()->Out().AsRegister<Register>();
5665 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5666}
5667
5668void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5669 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5670}
5671
5672void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5673 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5674}
5675
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005676void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005677 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005678 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5679 ? LocationSummary::kCallOnMainOnly
5680 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005681 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005683 HLoadString::LoadKind load_kind = load->GetLoadKind();
5684 switch (load_kind) {
5685 // We need an extra register for PC-relative literals on R2.
5686 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5687 case HLoadString::LoadKind::kBootImageAddress:
5688 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005689 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005690 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5691 break;
5692 }
5693 FALLTHROUGH_INTENDED;
5694 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005695 case HLoadString::LoadKind::kDexCacheViaMethod:
5696 locations->SetInAt(0, Location::RequiresRegister());
5697 break;
5698 default:
5699 break;
5700 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005701 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5702 InvokeRuntimeCallingConvention calling_convention;
5703 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5704 } else {
5705 locations->SetOut(Location::RequiresRegister());
5706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005707}
5708
5709void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005710 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005711 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005712 Location out_loc = locations->Out();
5713 Register out = out_loc.AsRegister<Register>();
5714 Register base_or_current_method_reg;
5715 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5716 switch (load_kind) {
5717 // We need an extra register for PC-relative literals on R2.
5718 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5719 case HLoadString::LoadKind::kBootImageAddress:
5720 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005721 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005722 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5723 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005724 default:
5725 base_or_current_method_reg = ZERO;
5726 break;
5727 }
5728
5729 switch (load_kind) {
5730 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5731 DCHECK(!kEmitCompilerReadBarrier);
5732 __ LoadLiteral(out,
5733 base_or_current_method_reg,
5734 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5735 load->GetStringIndex()));
5736 return; // No dex cache slow path.
5737 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5738 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005739 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005740 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5741 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005742 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005743 return; // No dex cache slow path.
5744 }
5745 case HLoadString::LoadKind::kBootImageAddress: {
5746 DCHECK(!kEmitCompilerReadBarrier);
5747 DCHECK_NE(load->GetAddress(), 0u);
5748 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5749 __ LoadLiteral(out,
5750 base_or_current_method_reg,
5751 codegen_->DeduplicateBootImageAddressLiteral(address));
5752 return; // No dex cache slow path.
5753 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005754 case HLoadString::LoadKind::kBssEntry: {
5755 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5756 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5757 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5758 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5759 __ LoadFromOffset(kLoadWord, out, out, 0);
5760 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5761 codegen_->AddSlowPath(slow_path);
5762 __ Beqz(out, slow_path->GetEntryLabel());
5763 __ Bind(slow_path->GetExitLabel());
5764 return;
5765 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005766 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005767 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005768 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005769
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005770 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005771 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5772 InvokeRuntimeCallingConvention calling_convention;
5773 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5774 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5775 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005776}
5777
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005778void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5780 locations->SetOut(Location::ConstantLocation(constant));
5781}
5782
5783void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5784 // Will be generated at use site.
5785}
5786
5787void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5788 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005790 InvokeRuntimeCallingConvention calling_convention;
5791 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5792}
5793
5794void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5795 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005796 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005797 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5798 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005799 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800 }
5801 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5802}
5803
5804void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5805 LocationSummary* locations =
5806 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5807 switch (mul->GetResultType()) {
5808 case Primitive::kPrimInt:
5809 case Primitive::kPrimLong:
5810 locations->SetInAt(0, Location::RequiresRegister());
5811 locations->SetInAt(1, Location::RequiresRegister());
5812 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5813 break;
5814
5815 case Primitive::kPrimFloat:
5816 case Primitive::kPrimDouble:
5817 locations->SetInAt(0, Location::RequiresFpuRegister());
5818 locations->SetInAt(1, Location::RequiresFpuRegister());
5819 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5820 break;
5821
5822 default:
5823 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5824 }
5825}
5826
5827void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5828 Primitive::Type type = instruction->GetType();
5829 LocationSummary* locations = instruction->GetLocations();
5830 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5831
5832 switch (type) {
5833 case Primitive::kPrimInt: {
5834 Register dst = locations->Out().AsRegister<Register>();
5835 Register lhs = locations->InAt(0).AsRegister<Register>();
5836 Register rhs = locations->InAt(1).AsRegister<Register>();
5837
5838 if (isR6) {
5839 __ MulR6(dst, lhs, rhs);
5840 } else {
5841 __ MulR2(dst, lhs, rhs);
5842 }
5843 break;
5844 }
5845 case Primitive::kPrimLong: {
5846 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5847 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5848 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5849 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5850 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5851 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5852
5853 // Extra checks to protect caused by the existance of A1_A2.
5854 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5855 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5856 DCHECK_NE(dst_high, lhs_low);
5857 DCHECK_NE(dst_high, rhs_low);
5858
5859 // A_B * C_D
5860 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5861 // dst_lo: [ low(B*D) ]
5862 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5863
5864 if (isR6) {
5865 __ MulR6(TMP, lhs_high, rhs_low);
5866 __ MulR6(dst_high, lhs_low, rhs_high);
5867 __ Addu(dst_high, dst_high, TMP);
5868 __ MuhuR6(TMP, lhs_low, rhs_low);
5869 __ Addu(dst_high, dst_high, TMP);
5870 __ MulR6(dst_low, lhs_low, rhs_low);
5871 } else {
5872 __ MulR2(TMP, lhs_high, rhs_low);
5873 __ MulR2(dst_high, lhs_low, rhs_high);
5874 __ Addu(dst_high, dst_high, TMP);
5875 __ MultuR2(lhs_low, rhs_low);
5876 __ Mfhi(TMP);
5877 __ Addu(dst_high, dst_high, TMP);
5878 __ Mflo(dst_low);
5879 }
5880 break;
5881 }
5882 case Primitive::kPrimFloat:
5883 case Primitive::kPrimDouble: {
5884 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5885 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5886 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5887 if (type == Primitive::kPrimFloat) {
5888 __ MulS(dst, lhs, rhs);
5889 } else {
5890 __ MulD(dst, lhs, rhs);
5891 }
5892 break;
5893 }
5894 default:
5895 LOG(FATAL) << "Unexpected mul type " << type;
5896 }
5897}
5898
5899void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5900 LocationSummary* locations =
5901 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5902 switch (neg->GetResultType()) {
5903 case Primitive::kPrimInt:
5904 case Primitive::kPrimLong:
5905 locations->SetInAt(0, Location::RequiresRegister());
5906 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5907 break;
5908
5909 case Primitive::kPrimFloat:
5910 case Primitive::kPrimDouble:
5911 locations->SetInAt(0, Location::RequiresFpuRegister());
5912 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5913 break;
5914
5915 default:
5916 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5917 }
5918}
5919
5920void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5921 Primitive::Type type = instruction->GetType();
5922 LocationSummary* locations = instruction->GetLocations();
5923
5924 switch (type) {
5925 case Primitive::kPrimInt: {
5926 Register dst = locations->Out().AsRegister<Register>();
5927 Register src = locations->InAt(0).AsRegister<Register>();
5928 __ Subu(dst, ZERO, src);
5929 break;
5930 }
5931 case Primitive::kPrimLong: {
5932 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5933 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5934 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5935 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5936 __ Subu(dst_low, ZERO, src_low);
5937 __ Sltu(TMP, ZERO, dst_low);
5938 __ Subu(dst_high, ZERO, src_high);
5939 __ Subu(dst_high, dst_high, TMP);
5940 break;
5941 }
5942 case Primitive::kPrimFloat:
5943 case Primitive::kPrimDouble: {
5944 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5945 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5946 if (type == Primitive::kPrimFloat) {
5947 __ NegS(dst, src);
5948 } else {
5949 __ NegD(dst, src);
5950 }
5951 break;
5952 }
5953 default:
5954 LOG(FATAL) << "Unexpected neg type " << type;
5955 }
5956}
5957
5958void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5959 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005960 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005961 InvokeRuntimeCallingConvention calling_convention;
5962 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5963 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5964 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5965 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5966}
5967
5968void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5969 InvokeRuntimeCallingConvention calling_convention;
5970 Register current_method_register = calling_convention.GetRegisterAt(2);
5971 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5972 // Move an uint16_t value to a register.
5973 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Serban Constantinescufca16662016-07-14 09:21:59 +01005974 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005975 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5976 void*, uint32_t, int32_t, ArtMethod*>();
5977}
5978
5979void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5980 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005982 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005983 if (instruction->IsStringAlloc()) {
5984 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5985 } else {
5986 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5987 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005989 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5990}
5991
5992void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005993 if (instruction->IsStringAlloc()) {
5994 // String is allocated through StringFactory. Call NewEmptyString entry point.
5995 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005996 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005997 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5998 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5999 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006000 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006001 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6002 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006003 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006004 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6005 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006006}
6007
6008void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6009 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6010 locations->SetInAt(0, Location::RequiresRegister());
6011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6012}
6013
6014void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6015 Primitive::Type type = instruction->GetType();
6016 LocationSummary* locations = instruction->GetLocations();
6017
6018 switch (type) {
6019 case Primitive::kPrimInt: {
6020 Register dst = locations->Out().AsRegister<Register>();
6021 Register src = locations->InAt(0).AsRegister<Register>();
6022 __ Nor(dst, src, ZERO);
6023 break;
6024 }
6025
6026 case Primitive::kPrimLong: {
6027 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6028 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6029 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6030 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6031 __ Nor(dst_high, src_high, ZERO);
6032 __ Nor(dst_low, src_low, ZERO);
6033 break;
6034 }
6035
6036 default:
6037 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6038 }
6039}
6040
6041void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6042 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6043 locations->SetInAt(0, Location::RequiresRegister());
6044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6045}
6046
6047void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6048 LocationSummary* locations = instruction->GetLocations();
6049 __ Xori(locations->Out().AsRegister<Register>(),
6050 locations->InAt(0).AsRegister<Register>(),
6051 1);
6052}
6053
6054void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006055 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6056 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006057}
6058
Calin Juravle2ae48182016-03-16 14:05:09 +00006059void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6060 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006061 return;
6062 }
6063 Location obj = instruction->GetLocations()->InAt(0);
6064
6065 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006066 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067}
6068
Calin Juravle2ae48182016-03-16 14:05:09 +00006069void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006070 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006071 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006072
6073 Location obj = instruction->GetLocations()->InAt(0);
6074
6075 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6076}
6077
6078void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006079 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006080}
6081
6082void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6083 HandleBinaryOp(instruction);
6084}
6085
6086void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6087 HandleBinaryOp(instruction);
6088}
6089
6090void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6091 LOG(FATAL) << "Unreachable";
6092}
6093
6094void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6095 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6096}
6097
6098void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6100 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6101 if (location.IsStackSlot()) {
6102 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6103 } else if (location.IsDoubleStackSlot()) {
6104 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6105 }
6106 locations->SetOut(location);
6107}
6108
6109void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6110 ATTRIBUTE_UNUSED) {
6111 // Nothing to do, the parameter is already at its location.
6112}
6113
6114void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6115 LocationSummary* locations =
6116 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6117 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6118}
6119
6120void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6121 ATTRIBUTE_UNUSED) {
6122 // Nothing to do, the method is already at its location.
6123}
6124
6125void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006127 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006128 locations->SetInAt(i, Location::Any());
6129 }
6130 locations->SetOut(Location::Any());
6131}
6132
6133void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6134 LOG(FATAL) << "Unreachable";
6135}
6136
6137void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6138 Primitive::Type type = rem->GetResultType();
6139 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006140 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6142
6143 switch (type) {
6144 case Primitive::kPrimInt:
6145 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006146 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006147 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6148 break;
6149
6150 case Primitive::kPrimLong: {
6151 InvokeRuntimeCallingConvention calling_convention;
6152 locations->SetInAt(0, Location::RegisterPairLocation(
6153 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6154 locations->SetInAt(1, Location::RegisterPairLocation(
6155 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6156 locations->SetOut(calling_convention.GetReturnLocation(type));
6157 break;
6158 }
6159
6160 case Primitive::kPrimFloat:
6161 case Primitive::kPrimDouble: {
6162 InvokeRuntimeCallingConvention calling_convention;
6163 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6164 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6165 locations->SetOut(calling_convention.GetReturnLocation(type));
6166 break;
6167 }
6168
6169 default:
6170 LOG(FATAL) << "Unexpected rem type " << type;
6171 }
6172}
6173
6174void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6175 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176
6177 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006178 case Primitive::kPrimInt:
6179 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006180 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006181 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006182 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006183 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6184 break;
6185 }
6186 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006187 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006188 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 break;
6190 }
6191 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006192 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006193 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006194 break;
6195 }
6196 default:
6197 LOG(FATAL) << "Unexpected rem type " << type;
6198 }
6199}
6200
6201void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6202 memory_barrier->SetLocations(nullptr);
6203}
6204
6205void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6206 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6207}
6208
6209void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6211 Primitive::Type return_type = ret->InputAt(0)->GetType();
6212 locations->SetInAt(0, MipsReturnLocation(return_type));
6213}
6214
6215void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6216 codegen_->GenerateFrameExit();
6217}
6218
6219void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6220 ret->SetLocations(nullptr);
6221}
6222
6223void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6224 codegen_->GenerateFrameExit();
6225}
6226
Alexey Frunze92d90602015-12-18 18:16:36 -08006227void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6228 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006229}
6230
Alexey Frunze92d90602015-12-18 18:16:36 -08006231void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6232 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006233}
6234
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6236 HandleShift(shl);
6237}
6238
6239void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6240 HandleShift(shl);
6241}
6242
6243void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6244 HandleShift(shr);
6245}
6246
6247void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6248 HandleShift(shr);
6249}
6250
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006251void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6252 HandleBinaryOp(instruction);
6253}
6254
6255void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6256 HandleBinaryOp(instruction);
6257}
6258
6259void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6260 HandleFieldGet(instruction, instruction->GetFieldInfo());
6261}
6262
6263void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6264 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6265}
6266
6267void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6268 HandleFieldSet(instruction, instruction->GetFieldInfo());
6269}
6270
6271void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6272 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6273}
6274
6275void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6276 HUnresolvedInstanceFieldGet* instruction) {
6277 FieldAccessCallingConventionMIPS calling_convention;
6278 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6279 instruction->GetFieldType(),
6280 calling_convention);
6281}
6282
6283void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6284 HUnresolvedInstanceFieldGet* instruction) {
6285 FieldAccessCallingConventionMIPS calling_convention;
6286 codegen_->GenerateUnresolvedFieldAccess(instruction,
6287 instruction->GetFieldType(),
6288 instruction->GetFieldIndex(),
6289 instruction->GetDexPc(),
6290 calling_convention);
6291}
6292
6293void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6294 HUnresolvedInstanceFieldSet* instruction) {
6295 FieldAccessCallingConventionMIPS calling_convention;
6296 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6297 instruction->GetFieldType(),
6298 calling_convention);
6299}
6300
6301void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6302 HUnresolvedInstanceFieldSet* instruction) {
6303 FieldAccessCallingConventionMIPS calling_convention;
6304 codegen_->GenerateUnresolvedFieldAccess(instruction,
6305 instruction->GetFieldType(),
6306 instruction->GetFieldIndex(),
6307 instruction->GetDexPc(),
6308 calling_convention);
6309}
6310
6311void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6312 HUnresolvedStaticFieldGet* instruction) {
6313 FieldAccessCallingConventionMIPS calling_convention;
6314 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6315 instruction->GetFieldType(),
6316 calling_convention);
6317}
6318
6319void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6320 HUnresolvedStaticFieldGet* instruction) {
6321 FieldAccessCallingConventionMIPS calling_convention;
6322 codegen_->GenerateUnresolvedFieldAccess(instruction,
6323 instruction->GetFieldType(),
6324 instruction->GetFieldIndex(),
6325 instruction->GetDexPc(),
6326 calling_convention);
6327}
6328
6329void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6330 HUnresolvedStaticFieldSet* instruction) {
6331 FieldAccessCallingConventionMIPS calling_convention;
6332 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6333 instruction->GetFieldType(),
6334 calling_convention);
6335}
6336
6337void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6338 HUnresolvedStaticFieldSet* instruction) {
6339 FieldAccessCallingConventionMIPS calling_convention;
6340 codegen_->GenerateUnresolvedFieldAccess(instruction,
6341 instruction->GetFieldType(),
6342 instruction->GetFieldIndex(),
6343 instruction->GetDexPc(),
6344 calling_convention);
6345}
6346
6347void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006348 LocationSummary* locations =
6349 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006350 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006351}
6352
6353void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6354 HBasicBlock* block = instruction->GetBlock();
6355 if (block->GetLoopInformation() != nullptr) {
6356 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6357 // The back edge will generate the suspend check.
6358 return;
6359 }
6360 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6361 // The goto will generate the suspend check.
6362 return;
6363 }
6364 GenerateSuspendCheck(instruction, nullptr);
6365}
6366
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6368 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 InvokeRuntimeCallingConvention calling_convention;
6371 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6372}
6373
6374void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006375 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6377}
6378
6379void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6380 Primitive::Type input_type = conversion->GetInputType();
6381 Primitive::Type result_type = conversion->GetResultType();
6382 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006383 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006384
6385 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6386 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6387 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6388 }
6389
6390 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006391 if (!isR6 &&
6392 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6393 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006394 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006395 }
6396
6397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6398
6399 if (call_kind == LocationSummary::kNoCall) {
6400 if (Primitive::IsFloatingPointType(input_type)) {
6401 locations->SetInAt(0, Location::RequiresFpuRegister());
6402 } else {
6403 locations->SetInAt(0, Location::RequiresRegister());
6404 }
6405
6406 if (Primitive::IsFloatingPointType(result_type)) {
6407 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6408 } else {
6409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6410 }
6411 } else {
6412 InvokeRuntimeCallingConvention calling_convention;
6413
6414 if (Primitive::IsFloatingPointType(input_type)) {
6415 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6416 } else {
6417 DCHECK_EQ(input_type, Primitive::kPrimLong);
6418 locations->SetInAt(0, Location::RegisterPairLocation(
6419 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6420 }
6421
6422 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6423 }
6424}
6425
6426void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6427 LocationSummary* locations = conversion->GetLocations();
6428 Primitive::Type result_type = conversion->GetResultType();
6429 Primitive::Type input_type = conversion->GetInputType();
6430 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006431 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006432
6433 DCHECK_NE(input_type, result_type);
6434
6435 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6436 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6437 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6438 Register src = locations->InAt(0).AsRegister<Register>();
6439
Alexey Frunzea871ef12016-06-27 15:20:11 -07006440 if (dst_low != src) {
6441 __ Move(dst_low, src);
6442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 __ Sra(dst_high, src, 31);
6444 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6445 Register dst = locations->Out().AsRegister<Register>();
6446 Register src = (input_type == Primitive::kPrimLong)
6447 ? locations->InAt(0).AsRegisterPairLow<Register>()
6448 : locations->InAt(0).AsRegister<Register>();
6449
6450 switch (result_type) {
6451 case Primitive::kPrimChar:
6452 __ Andi(dst, src, 0xFFFF);
6453 break;
6454 case Primitive::kPrimByte:
6455 if (has_sign_extension) {
6456 __ Seb(dst, src);
6457 } else {
6458 __ Sll(dst, src, 24);
6459 __ Sra(dst, dst, 24);
6460 }
6461 break;
6462 case Primitive::kPrimShort:
6463 if (has_sign_extension) {
6464 __ Seh(dst, src);
6465 } else {
6466 __ Sll(dst, src, 16);
6467 __ Sra(dst, dst, 16);
6468 }
6469 break;
6470 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006471 if (dst != src) {
6472 __ Move(dst, src);
6473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006474 break;
6475
6476 default:
6477 LOG(FATAL) << "Unexpected type conversion from " << input_type
6478 << " to " << result_type;
6479 }
6480 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006481 if (input_type == Primitive::kPrimLong) {
6482 if (isR6) {
6483 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6484 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6485 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6486 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6487 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6488 __ Mtc1(src_low, FTMP);
6489 __ Mthc1(src_high, FTMP);
6490 if (result_type == Primitive::kPrimFloat) {
6491 __ Cvtsl(dst, FTMP);
6492 } else {
6493 __ Cvtdl(dst, FTMP);
6494 }
6495 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006496 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6497 : kQuickL2d;
6498 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006499 if (result_type == Primitive::kPrimFloat) {
6500 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6501 } else {
6502 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6503 }
6504 }
6505 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 Register src = locations->InAt(0).AsRegister<Register>();
6507 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6508 __ Mtc1(src, FTMP);
6509 if (result_type == Primitive::kPrimFloat) {
6510 __ Cvtsw(dst, FTMP);
6511 } else {
6512 __ Cvtdw(dst, FTMP);
6513 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006514 }
6515 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6516 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006517 if (result_type == Primitive::kPrimLong) {
6518 if (isR6) {
6519 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6520 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6521 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6522 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6523 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6524 MipsLabel truncate;
6525 MipsLabel done;
6526
6527 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6528 // value when the input is either a NaN or is outside of the range of the output type
6529 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6530 // the same result.
6531 //
6532 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6533 // value of the output type if the input is outside of the range after the truncation or
6534 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6535 // results. This matches the desired float/double-to-int/long conversion exactly.
6536 //
6537 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6538 //
6539 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6540 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6541 // even though it must be NAN2008=1 on R6.
6542 //
6543 // The code takes care of the different behaviors by first comparing the input to the
6544 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6545 // If the input is greater than or equal to the minimum, it procedes to the truncate
6546 // instruction, which will handle such an input the same way irrespective of NAN2008.
6547 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6548 // in order to return either zero or the minimum value.
6549 //
6550 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6551 // truncate instruction for MIPS64R6.
6552 if (input_type == Primitive::kPrimFloat) {
6553 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6554 __ LoadConst32(TMP, min_val);
6555 __ Mtc1(TMP, FTMP);
6556 __ CmpLeS(FTMP, FTMP, src);
6557 } else {
6558 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6559 __ LoadConst32(TMP, High32Bits(min_val));
6560 __ Mtc1(ZERO, FTMP);
6561 __ Mthc1(TMP, FTMP);
6562 __ CmpLeD(FTMP, FTMP, src);
6563 }
6564
6565 __ Bc1nez(FTMP, &truncate);
6566
6567 if (input_type == Primitive::kPrimFloat) {
6568 __ CmpEqS(FTMP, src, src);
6569 } else {
6570 __ CmpEqD(FTMP, src, src);
6571 }
6572 __ Move(dst_low, ZERO);
6573 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6574 __ Mfc1(TMP, FTMP);
6575 __ And(dst_high, dst_high, TMP);
6576
6577 __ B(&done);
6578
6579 __ Bind(&truncate);
6580
6581 if (input_type == Primitive::kPrimFloat) {
6582 __ TruncLS(FTMP, src);
6583 } else {
6584 __ TruncLD(FTMP, src);
6585 }
6586 __ Mfc1(dst_low, FTMP);
6587 __ Mfhc1(dst_high, FTMP);
6588
6589 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006590 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006591 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6592 : kQuickD2l;
6593 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006594 if (input_type == Primitive::kPrimFloat) {
6595 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6596 } else {
6597 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6598 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006599 }
6600 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006601 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6602 Register dst = locations->Out().AsRegister<Register>();
6603 MipsLabel truncate;
6604 MipsLabel done;
6605
6606 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6607 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6608 // even though it must be NAN2008=1 on R6.
6609 //
6610 // For details see the large comment above for the truncation of float/double to long on R6.
6611 //
6612 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6613 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006614 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006615 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6616 __ LoadConst32(TMP, min_val);
6617 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006618 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006619 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6620 __ LoadConst32(TMP, High32Bits(min_val));
6621 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006622 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006623 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006624
6625 if (isR6) {
6626 if (input_type == Primitive::kPrimFloat) {
6627 __ CmpLeS(FTMP, FTMP, src);
6628 } else {
6629 __ CmpLeD(FTMP, FTMP, src);
6630 }
6631 __ Bc1nez(FTMP, &truncate);
6632
6633 if (input_type == Primitive::kPrimFloat) {
6634 __ CmpEqS(FTMP, src, src);
6635 } else {
6636 __ CmpEqD(FTMP, src, src);
6637 }
6638 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6639 __ Mfc1(TMP, FTMP);
6640 __ And(dst, dst, TMP);
6641 } else {
6642 if (input_type == Primitive::kPrimFloat) {
6643 __ ColeS(0, FTMP, src);
6644 } else {
6645 __ ColeD(0, FTMP, src);
6646 }
6647 __ Bc1t(0, &truncate);
6648
6649 if (input_type == Primitive::kPrimFloat) {
6650 __ CeqS(0, src, src);
6651 } else {
6652 __ CeqD(0, src, src);
6653 }
6654 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6655 __ Movf(dst, ZERO, 0);
6656 }
6657
6658 __ B(&done);
6659
6660 __ Bind(&truncate);
6661
6662 if (input_type == Primitive::kPrimFloat) {
6663 __ TruncWS(FTMP, src);
6664 } else {
6665 __ TruncWD(FTMP, src);
6666 }
6667 __ Mfc1(dst, FTMP);
6668
6669 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006670 }
6671 } else if (Primitive::IsFloatingPointType(result_type) &&
6672 Primitive::IsFloatingPointType(input_type)) {
6673 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6674 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6675 if (result_type == Primitive::kPrimFloat) {
6676 __ Cvtsd(dst, src);
6677 } else {
6678 __ Cvtds(dst, src);
6679 }
6680 } else {
6681 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6682 << " to " << result_type;
6683 }
6684}
6685
6686void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6687 HandleShift(ushr);
6688}
6689
6690void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6691 HandleShift(ushr);
6692}
6693
6694void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6695 HandleBinaryOp(instruction);
6696}
6697
6698void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6699 HandleBinaryOp(instruction);
6700}
6701
6702void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6703 // Nothing to do, this should be removed during prepare for register allocator.
6704 LOG(FATAL) << "Unreachable";
6705}
6706
6707void InstructionCodeGeneratorMIPS::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 LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006713 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006714}
6715
6716void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006717 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006718}
6719
6720void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006721 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722}
6723
6724void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006725 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726}
6727
6728void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006729 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006730}
6731
6732void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006733 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734}
6735
6736void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006737 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006738}
6739
6740void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006741 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742}
6743
6744void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006745 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006746}
6747
6748void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006749 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006750}
6751
6752void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006753 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006754}
6755
6756void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006757 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758}
6759
6760void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006761 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006762}
6763
6764void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006765 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006766}
6767
6768void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006769 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006770}
6771
6772void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006773 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006774}
6775
6776void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006777 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778}
6779
6780void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006781 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006782}
6783
6784void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006785 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006786}
6787
6788void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006789 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006790}
6791
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006792void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6793 LocationSummary* locations =
6794 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6795 locations->SetInAt(0, Location::RequiresRegister());
6796}
6797
Alexey Frunze96b66822016-09-10 02:32:44 -07006798void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6799 int32_t lower_bound,
6800 uint32_t num_entries,
6801 HBasicBlock* switch_block,
6802 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006803 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006804 Register temp_reg = TMP;
6805 __ Addiu32(temp_reg, value_reg, -lower_bound);
6806 // Jump to default if index is negative
6807 // Note: We don't check the case that index is positive while value < lower_bound, because in
6808 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6809 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6810
Alexey Frunze96b66822016-09-10 02:32:44 -07006811 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006812 // Jump to successors[0] if value == lower_bound.
6813 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6814 int32_t last_index = 0;
6815 for (; num_entries - last_index > 2; last_index += 2) {
6816 __ Addiu(temp_reg, temp_reg, -2);
6817 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6818 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6819 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6820 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6821 }
6822 if (num_entries - last_index == 2) {
6823 // The last missing case_value.
6824 __ Addiu(temp_reg, temp_reg, -1);
6825 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006826 }
6827
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006828 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006829 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006830 __ B(codegen_->GetLabelOf(default_block));
6831 }
6832}
6833
Alexey Frunze96b66822016-09-10 02:32:44 -07006834void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6835 Register constant_area,
6836 int32_t lower_bound,
6837 uint32_t num_entries,
6838 HBasicBlock* switch_block,
6839 HBasicBlock* default_block) {
6840 // Create a jump table.
6841 std::vector<MipsLabel*> labels(num_entries);
6842 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6843 for (uint32_t i = 0; i < num_entries; i++) {
6844 labels[i] = codegen_->GetLabelOf(successors[i]);
6845 }
6846 JumpTable* table = __ CreateJumpTable(std::move(labels));
6847
6848 // Is the value in range?
6849 __ Addiu32(TMP, value_reg, -lower_bound);
6850 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6851 __ Sltiu(AT, TMP, num_entries);
6852 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6853 } else {
6854 __ LoadConst32(AT, num_entries);
6855 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6856 }
6857
6858 // We are in the range of the table.
6859 // Load the target address from the jump table, indexing by the value.
6860 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6861 __ Sll(TMP, TMP, 2);
6862 __ Addu(TMP, TMP, AT);
6863 __ Lw(TMP, TMP, 0);
6864 // Compute the absolute target address by adding the table start address
6865 // (the table contains offsets to targets relative to its start).
6866 __ Addu(TMP, TMP, AT);
6867 // And jump.
6868 __ Jr(TMP);
6869 __ NopIfNoReordering();
6870}
6871
6872void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6873 int32_t lower_bound = switch_instr->GetStartValue();
6874 uint32_t num_entries = switch_instr->GetNumEntries();
6875 LocationSummary* locations = switch_instr->GetLocations();
6876 Register value_reg = locations->InAt(0).AsRegister<Register>();
6877 HBasicBlock* switch_block = switch_instr->GetBlock();
6878 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6879
6880 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6881 num_entries > kPackedSwitchJumpTableThreshold) {
6882 // R6 uses PC-relative addressing to access the jump table.
6883 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6884 // the jump table and it is implemented by changing HPackedSwitch to
6885 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6886 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6887 GenTableBasedPackedSwitch(value_reg,
6888 ZERO,
6889 lower_bound,
6890 num_entries,
6891 switch_block,
6892 default_block);
6893 } else {
6894 GenPackedSwitchWithCompares(value_reg,
6895 lower_bound,
6896 num_entries,
6897 switch_block,
6898 default_block);
6899 }
6900}
6901
6902void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6903 LocationSummary* locations =
6904 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6905 locations->SetInAt(0, Location::RequiresRegister());
6906 // Constant area pointer (HMipsComputeBaseMethodAddress).
6907 locations->SetInAt(1, Location::RequiresRegister());
6908}
6909
6910void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6911 int32_t lower_bound = switch_instr->GetStartValue();
6912 uint32_t num_entries = switch_instr->GetNumEntries();
6913 LocationSummary* locations = switch_instr->GetLocations();
6914 Register value_reg = locations->InAt(0).AsRegister<Register>();
6915 Register constant_area = locations->InAt(1).AsRegister<Register>();
6916 HBasicBlock* switch_block = switch_instr->GetBlock();
6917 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6918
6919 // This is an R2-only path. HPackedSwitch has been changed to
6920 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6921 // required to address the jump table relative to PC.
6922 GenTableBasedPackedSwitch(value_reg,
6923 constant_area,
6924 lower_bound,
6925 num_entries,
6926 switch_block,
6927 default_block);
6928}
6929
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006930void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6931 HMipsComputeBaseMethodAddress* insn) {
6932 LocationSummary* locations =
6933 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6934 locations->SetOut(Location::RequiresRegister());
6935}
6936
6937void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6938 HMipsComputeBaseMethodAddress* insn) {
6939 LocationSummary* locations = insn->GetLocations();
6940 Register reg = locations->Out().AsRegister<Register>();
6941
6942 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6943
6944 // Generate a dummy PC-relative call to obtain PC.
6945 __ Nal();
6946 // Grab the return address off RA.
6947 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006948 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006949
6950 // Remember this offset (the obtained PC value) for later use with constant area.
6951 __ BindPcRelBaseLabel();
6952}
6953
6954void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6955 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6956 locations->SetOut(Location::RequiresRegister());
6957}
6958
6959void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6960 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6961 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6962 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006963 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6964 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006965}
6966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006967void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6968 // The trampoline uses the same calling convention as dex calling conventions,
6969 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6970 // the method_idx.
6971 HandleInvoke(invoke);
6972}
6973
6974void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6975 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6976}
6977
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006978void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6979 LocationSummary* locations =
6980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6981 locations->SetInAt(0, Location::RequiresRegister());
6982 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006983}
6984
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006985void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6986 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006987 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006988 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006989 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006990 __ LoadFromOffset(kLoadWord,
6991 locations->Out().AsRegister<Register>(),
6992 locations->InAt(0).AsRegister<Register>(),
6993 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006994 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006995 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006996 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006997 __ LoadFromOffset(kLoadWord,
6998 locations->Out().AsRegister<Register>(),
6999 locations->InAt(0).AsRegister<Register>(),
7000 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007001 __ LoadFromOffset(kLoadWord,
7002 locations->Out().AsRegister<Register>(),
7003 locations->Out().AsRegister<Register>(),
7004 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007005 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007006}
7007
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007008#undef __
7009#undef QUICK_ENTRY_POINT
7010
7011} // namespace mips
7012} // namespace art