blob: f0d49106fcfc8c0a67694d8893a87f7ede3c03fd [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()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800102 Register reg = calling_convention.GetRegisterAt(gp_index);
103 if (reg == A1 || reg == A3) {
104 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200105 gp_index++;
106 }
107 Register low_even = calling_convention.GetRegisterAt(gp_index);
108 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
109 DCHECK_EQ(low_even + 1, high_odd);
110 next_location = Location::RegisterPairLocation(low_even, high_odd);
111 } else {
112 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
113 next_location = Location::DoubleStackSlot(stack_offset);
114 }
115 break;
116 }
117
118 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
119 // will take up the even/odd pair, while floats are stored in even regs only.
120 // On 64 bit FPU, both double and float are stored in even registers only.
121 case Primitive::kPrimFloat:
122 case Primitive::kPrimDouble: {
123 uint32_t float_index = float_index_++;
124 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
125 next_location = Location::FpuRegisterLocation(
126 calling_convention.GetFpuRegisterAt(float_index));
127 } else {
128 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
129 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
130 : Location::StackSlot(stack_offset);
131 }
132 break;
133 }
134
135 case Primitive::kPrimVoid:
136 LOG(FATAL) << "Unexpected parameter type " << type;
137 break;
138 }
139
140 // Space on the stack is reserved for all arguments.
141 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
142
143 return next_location;
144}
145
146Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
147 return MipsReturnLocation(type);
148}
149
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100150// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
151#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700152#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
155 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000156 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200157
158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
159 LocationSummary* locations = instruction_->GetLocations();
160 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
161 __ Bind(GetEntryLabel());
162 if (instruction_->CanThrowIntoCatchBlock()) {
163 // Live registers will be restored in the catch block if caught.
164 SaveLiveRegisters(codegen, instruction_->GetLocations());
165 }
166 // We're moving two locations to locations that could overlap, so we need a parallel
167 // move resolver.
168 InvokeRuntimeCallingConvention calling_convention;
169 codegen->EmitParallelMoves(locations->InAt(0),
170 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
171 Primitive::kPrimInt,
172 locations->InAt(1),
173 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
174 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100175 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
176 ? kQuickThrowStringBounds
177 : kQuickThrowArrayBounds;
178 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100179 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
181 }
182
183 bool IsFatal() const OVERRIDE { return true; }
184
185 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
186
187 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
189};
190
191class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
192 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000193 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200194
195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
197 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100198 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
200 }
201
202 bool IsFatal() const OVERRIDE { return true; }
203
204 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
205
206 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
208};
209
210class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
211 public:
212 LoadClassSlowPathMIPS(HLoadClass* cls,
213 HInstruction* at,
214 uint32_t dex_pc,
215 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000216 : SlowPathCodeMIPS(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
218 }
219
220 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
221 LocationSummary* locations = at_->GetLocations();
222 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
223
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800228 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex().index_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200229
Serban Constantinescufca16662016-07-14 09:21:59 +0100230 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
231 : kQuickInitializeType;
232 mips_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200233 if (do_clinit_) {
234 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
235 } else {
236 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
237 }
238
239 // Move the class to the desired location.
240 Location out = locations->Out();
241 if (out.IsValid()) {
242 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
243 Primitive::Type type = at_->GetType();
244 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
245 }
246
247 RestoreLiveRegisters(codegen, locations);
248 __ B(GetExitLabel());
249 }
250
251 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
252
253 private:
254 // The class this slow path will load.
255 HLoadClass* const cls_;
256
257 // The instruction where this slow path is happening.
258 // (Might be the load class or an initialization check).
259 HInstruction* const at_;
260
261 // The dex PC of `at_`.
262 const uint32_t dex_pc_;
263
264 // Whether to initialize the class.
265 const bool do_clinit_;
266
267 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
268};
269
270class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
271 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000272 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200273
274 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
275 LocationSummary* locations = instruction_->GetLocations();
276 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
277 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
278
279 __ Bind(GetEntryLabel());
280 SaveLiveRegisters(codegen, locations);
281
282 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000283 HLoadString* load = instruction_->AsLoadString();
284 const uint32_t string_index = load->GetStringIndex();
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index);
Serban Constantinescufca16662016-07-14 09:21:59 +0100286 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
288 Primitive::Type type = instruction_->GetType();
289 mips_codegen->MoveLocation(locations->Out(),
290 calling_convention.GetReturnLocation(type),
291 type);
292
293 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000294
295 // Store the resolved String to the BSS entry.
296 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
297 // .bss entry address in the fast path, so that we can avoid another calculation here.
298 bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
299 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
300 Register out = locations->Out().AsRegister<Register>();
301 DCHECK_NE(out, AT);
302 CodeGeneratorMIPS::PcRelativePatchInfo* info =
303 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
304 mips_codegen->EmitPcRelativeAddressPlaceholder(info, TMP, base);
305 __ StoreToOffset(kStoreWord, out, TMP, 0);
306
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 __ B(GetExitLabel());
308 }
309
310 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
311
312 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200313 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
314};
315
316class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
317 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000318 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200319
320 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
321 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
322 __ Bind(GetEntryLabel());
323 if (instruction_->CanThrowIntoCatchBlock()) {
324 // Live registers will be restored in the catch block if caught.
325 SaveLiveRegisters(codegen, instruction_->GetLocations());
326 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100327 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200328 instruction_,
329 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100330 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
332 }
333
334 bool IsFatal() const OVERRIDE { return true; }
335
336 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
337
338 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
340};
341
342class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
343 public:
344 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000345 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200346
347 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
348 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
349 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100350 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200352 if (successor_ == nullptr) {
353 __ B(GetReturnLabel());
354 } else {
355 __ B(mips_codegen->GetLabelOf(successor_));
356 }
357 }
358
359 MipsLabel* GetReturnLabel() {
360 DCHECK(successor_ == nullptr);
361 return &return_label_;
362 }
363
364 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
365
366 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367 // If not null, the block to branch to after the suspend check.
368 HBasicBlock* const successor_;
369
370 // If `successor_` is null, the label to branch to after the suspend check.
371 MipsLabel return_label_;
372
373 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
374};
375
376class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
377 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000378 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379
380 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
381 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 uint32_t dex_pc = instruction_->GetDexPc();
383 DCHECK(instruction_->IsCheckCast()
384 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
385 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
386
387 __ Bind(GetEntryLabel());
388 SaveLiveRegisters(codegen, locations);
389
390 // We're moving two locations to locations that could overlap, so we need a parallel
391 // move resolver.
392 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800393 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200394 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
395 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800396 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
398 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200399 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100400 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800401 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402 Primitive::Type ret_type = instruction_->GetType();
403 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
404 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 } else {
406 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800407 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
408 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 }
410
411 RestoreLiveRegisters(codegen, locations);
412 __ B(GetExitLabel());
413 }
414
415 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
416
417 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
419};
420
421class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
422 public:
Aart Bik42249c32016-01-07 15:33:50 -0800423 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000424 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200425
426 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800427 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100429 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000430 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200431 }
432
433 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
434
435 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
437};
438
439CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
440 const MipsInstructionSetFeatures& isa_features,
441 const CompilerOptions& compiler_options,
442 OptimizingCompilerStats* stats)
443 : CodeGenerator(graph,
444 kNumberOfCoreRegisters,
445 kNumberOfFRegisters,
446 kNumberOfRegisterPairs,
447 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
448 arraysize(kCoreCalleeSaves)),
449 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
450 arraysize(kFpuCalleeSaves)),
451 compiler_options,
452 stats),
453 block_labels_(nullptr),
454 location_builder_(graph, this),
455 instruction_visitor_(graph, this),
456 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100457 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700458 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700459 uint32_literals_(std::less<uint32_t>(),
460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700461 method_patches_(MethodReferenceComparator(),
462 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
463 call_patches_(MethodReferenceComparator(),
464 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -0700465 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
466 boot_image_string_patches_(StringReferenceValueComparator(),
467 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
469 boot_image_type_patches_(TypeReferenceValueComparator(),
470 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
471 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
472 boot_image_address_patches_(std::less<uint32_t>(),
473 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
474 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475 // Save RA (containing the return address) to mimic Quick.
476 AddAllocatedRegister(Location::RegisterLocation(RA));
477}
478
479#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100480// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
481#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700482#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200483
484void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
485 // Ensure that we fix up branches.
486 __ FinalizeCode();
487
488 // Adjust native pc offsets in stack maps.
489 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
490 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
491 uint32_t new_position = __ GetAdjustedPosition(old_position);
492 DCHECK_GE(new_position, old_position);
493 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
494 }
495
496 // Adjust pc offsets for the disassembly information.
497 if (disasm_info_ != nullptr) {
498 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
499 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
500 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
501 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
502 it.second.start = __ GetAdjustedPosition(it.second.start);
503 it.second.end = __ GetAdjustedPosition(it.second.end);
504 }
505 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
506 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
507 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
508 }
509 }
510
511 CodeGenerator::Finalize(allocator);
512}
513
514MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
515 return codegen_->GetAssembler();
516}
517
518void ParallelMoveResolverMIPS::EmitMove(size_t index) {
519 DCHECK_LT(index, moves_.size());
520 MoveOperands* move = moves_[index];
521 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
522}
523
524void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
525 DCHECK_LT(index, moves_.size());
526 MoveOperands* move = moves_[index];
527 Primitive::Type type = move->GetType();
528 Location loc1 = move->GetDestination();
529 Location loc2 = move->GetSource();
530
531 DCHECK(!loc1.IsConstant());
532 DCHECK(!loc2.IsConstant());
533
534 if (loc1.Equals(loc2)) {
535 return;
536 }
537
538 if (loc1.IsRegister() && loc2.IsRegister()) {
539 // Swap 2 GPRs.
540 Register r1 = loc1.AsRegister<Register>();
541 Register r2 = loc2.AsRegister<Register>();
542 __ Move(TMP, r2);
543 __ Move(r2, r1);
544 __ Move(r1, TMP);
545 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
546 FRegister f1 = loc1.AsFpuRegister<FRegister>();
547 FRegister f2 = loc2.AsFpuRegister<FRegister>();
548 if (type == Primitive::kPrimFloat) {
549 __ MovS(FTMP, f2);
550 __ MovS(f2, f1);
551 __ MovS(f1, FTMP);
552 } else {
553 DCHECK_EQ(type, Primitive::kPrimDouble);
554 __ MovD(FTMP, f2);
555 __ MovD(f2, f1);
556 __ MovD(f1, FTMP);
557 }
558 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
559 (loc1.IsFpuRegister() && loc2.IsRegister())) {
560 // Swap FPR and GPR.
561 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
562 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
563 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200564 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200565 __ Move(TMP, r2);
566 __ Mfc1(r2, f1);
567 __ Mtc1(TMP, f1);
568 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
569 // Swap 2 GPR register pairs.
570 Register r1 = loc1.AsRegisterPairLow<Register>();
571 Register r2 = loc2.AsRegisterPairLow<Register>();
572 __ Move(TMP, r2);
573 __ Move(r2, r1);
574 __ Move(r1, TMP);
575 r1 = loc1.AsRegisterPairHigh<Register>();
576 r2 = loc2.AsRegisterPairHigh<Register>();
577 __ Move(TMP, r2);
578 __ Move(r2, r1);
579 __ Move(r1, TMP);
580 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
581 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
582 // Swap FPR and GPR register pair.
583 DCHECK_EQ(type, Primitive::kPrimDouble);
584 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
585 : loc2.AsFpuRegister<FRegister>();
586 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
587 : loc2.AsRegisterPairLow<Register>();
588 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
589 : loc2.AsRegisterPairHigh<Register>();
590 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
591 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
592 // unpredictable and the following mfch1 will fail.
593 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800594 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200595 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800596 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200597 __ Move(r2_l, TMP);
598 __ Move(r2_h, AT);
599 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
600 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
601 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
602 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000603 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
604 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200605 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
606 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000607 __ Move(TMP, reg);
608 __ LoadFromOffset(kLoadWord, reg, SP, offset);
609 __ StoreToOffset(kStoreWord, TMP, SP, offset);
610 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
611 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
612 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
613 : loc2.AsRegisterPairLow<Register>();
614 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
615 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200616 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
618 : loc2.GetHighStackIndex(kMipsWordSize);
619 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000620 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000621 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000622 __ Move(TMP, reg_h);
623 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
624 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200625 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
626 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
627 : loc2.AsFpuRegister<FRegister>();
628 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
629 if (type == Primitive::kPrimFloat) {
630 __ MovS(FTMP, reg);
631 __ LoadSFromOffset(reg, SP, offset);
632 __ StoreSToOffset(FTMP, SP, offset);
633 } else {
634 DCHECK_EQ(type, Primitive::kPrimDouble);
635 __ MovD(FTMP, reg);
636 __ LoadDFromOffset(reg, SP, offset);
637 __ StoreDToOffset(FTMP, SP, offset);
638 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200639 } else {
640 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
641 }
642}
643
644void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
645 __ Pop(static_cast<Register>(reg));
646}
647
648void ParallelMoveResolverMIPS::SpillScratch(int reg) {
649 __ Push(static_cast<Register>(reg));
650}
651
652void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
653 // Allocate a scratch register other than TMP, if available.
654 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
655 // automatically unspilled when the scratch scope object is destroyed).
656 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
657 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
658 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
659 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
660 __ LoadFromOffset(kLoadWord,
661 Register(ensure_scratch.GetRegister()),
662 SP,
663 index1 + stack_offset);
664 __ LoadFromOffset(kLoadWord,
665 TMP,
666 SP,
667 index2 + stack_offset);
668 __ StoreToOffset(kStoreWord,
669 Register(ensure_scratch.GetRegister()),
670 SP,
671 index2 + stack_offset);
672 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
673 }
674}
675
Alexey Frunze73296a72016-06-03 22:51:46 -0700676void CodeGeneratorMIPS::ComputeSpillMask() {
677 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
678 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
679 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
680 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
681 // registers, include the ZERO register to force alignment of FPU callee-saved registers
682 // within the stack frame.
683 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
684 core_spill_mask_ |= (1 << ZERO);
685 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700686}
687
688bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700689 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700690 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
691 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
692 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700693 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
694 // saved in an unused temporary register) and saving of RA and the current method pointer
695 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700696 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700697}
698
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200699static dwarf::Reg DWARFReg(Register reg) {
700 return dwarf::Reg::MipsCore(static_cast<int>(reg));
701}
702
703// TODO: mapping of floating-point registers to DWARF.
704
705void CodeGeneratorMIPS::GenerateFrameEntry() {
706 __ Bind(&frame_entry_label_);
707
708 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
709
710 if (do_overflow_check) {
711 __ LoadFromOffset(kLoadWord,
712 ZERO,
713 SP,
714 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
715 RecordPcInfo(nullptr, 0);
716 }
717
718 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700719 CHECK_EQ(fpu_spill_mask_, 0u);
720 CHECK_EQ(core_spill_mask_, 1u << RA);
721 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200722 return;
723 }
724
725 // Make sure the frame size isn't unreasonably large.
726 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
727 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
728 }
729
730 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200731
Alexey Frunze73296a72016-06-03 22:51:46 -0700732 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200733 __ IncreaseFrameSize(ofs);
734
Alexey Frunze73296a72016-06-03 22:51:46 -0700735 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
736 Register reg = static_cast<Register>(MostSignificantBit(mask));
737 mask ^= 1u << reg;
738 ofs -= kMipsWordSize;
739 // The ZERO register is only included for alignment.
740 if (reg != ZERO) {
741 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200742 __ cfi().RelOffset(DWARFReg(reg), ofs);
743 }
744 }
745
Alexey Frunze73296a72016-06-03 22:51:46 -0700746 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
747 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
748 mask ^= 1u << reg;
749 ofs -= kMipsDoublewordSize;
750 __ StoreDToOffset(reg, SP, ofs);
751 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200752 }
753
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100754 // Save the current method if we need it. Note that we do not
755 // do this in HCurrentMethod, as the instruction might have been removed
756 // in the SSA graph.
757 if (RequiresCurrentMethod()) {
758 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200760}
761
762void CodeGeneratorMIPS::GenerateFrameExit() {
763 __ cfi().RememberState();
764
765 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200766 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200767
Alexey Frunze73296a72016-06-03 22:51:46 -0700768 // For better instruction scheduling restore RA before other registers.
769 uint32_t ofs = GetFrameSize();
770 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
771 Register reg = static_cast<Register>(MostSignificantBit(mask));
772 mask ^= 1u << reg;
773 ofs -= kMipsWordSize;
774 // The ZERO register is only included for alignment.
775 if (reg != ZERO) {
776 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200777 __ cfi().Restore(DWARFReg(reg));
778 }
779 }
780
Alexey Frunze73296a72016-06-03 22:51:46 -0700781 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
782 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
783 mask ^= 1u << reg;
784 ofs -= kMipsDoublewordSize;
785 __ LoadDFromOffset(reg, SP, ofs);
786 // TODO: __ cfi().Restore(DWARFReg(reg));
787 }
788
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700789 size_t frame_size = GetFrameSize();
790 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
791 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
792 bool reordering = __ SetReorder(false);
793 if (exchange) {
794 __ Jr(RA);
795 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
796 } else {
797 __ DecreaseFrameSize(frame_size);
798 __ Jr(RA);
799 __ Nop(); // In delay slot.
800 }
801 __ SetReorder(reordering);
802 } else {
803 __ Jr(RA);
804 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200805 }
806
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200807 __ cfi().RestoreState();
808 __ cfi().DefCFAOffset(GetFrameSize());
809}
810
811void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
812 __ Bind(GetLabelOf(block));
813}
814
815void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
816 if (src.Equals(dst)) {
817 return;
818 }
819
820 if (src.IsConstant()) {
821 MoveConstant(dst, src.GetConstant());
822 } else {
823 if (Primitive::Is64BitType(dst_type)) {
824 Move64(dst, src);
825 } else {
826 Move32(dst, src);
827 }
828 }
829}
830
831void CodeGeneratorMIPS::Move32(Location destination, Location source) {
832 if (source.Equals(destination)) {
833 return;
834 }
835
836 if (destination.IsRegister()) {
837 if (source.IsRegister()) {
838 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
839 } else if (source.IsFpuRegister()) {
840 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
841 } else {
842 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
843 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
844 }
845 } else if (destination.IsFpuRegister()) {
846 if (source.IsRegister()) {
847 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
848 } else if (source.IsFpuRegister()) {
849 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
850 } else {
851 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
852 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
853 }
854 } else {
855 DCHECK(destination.IsStackSlot()) << destination;
856 if (source.IsRegister()) {
857 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
858 } else if (source.IsFpuRegister()) {
859 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
860 } else {
861 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
862 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
863 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
864 }
865 }
866}
867
868void CodeGeneratorMIPS::Move64(Location destination, Location source) {
869 if (source.Equals(destination)) {
870 return;
871 }
872
873 if (destination.IsRegisterPair()) {
874 if (source.IsRegisterPair()) {
875 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
876 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
877 } else if (source.IsFpuRegister()) {
878 Register dst_high = destination.AsRegisterPairHigh<Register>();
879 Register dst_low = destination.AsRegisterPairLow<Register>();
880 FRegister src = source.AsFpuRegister<FRegister>();
881 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800882 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200883 } else {
884 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
885 int32_t off = source.GetStackIndex();
886 Register r = destination.AsRegisterPairLow<Register>();
887 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
888 }
889 } else if (destination.IsFpuRegister()) {
890 if (source.IsRegisterPair()) {
891 FRegister dst = destination.AsFpuRegister<FRegister>();
892 Register src_high = source.AsRegisterPairHigh<Register>();
893 Register src_low = source.AsRegisterPairLow<Register>();
894 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800895 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200896 } else if (source.IsFpuRegister()) {
897 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
898 } else {
899 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
900 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
901 }
902 } else {
903 DCHECK(destination.IsDoubleStackSlot()) << destination;
904 int32_t off = destination.GetStackIndex();
905 if (source.IsRegisterPair()) {
906 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
907 } else if (source.IsFpuRegister()) {
908 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
909 } else {
910 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
911 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
912 __ StoreToOffset(kStoreWord, TMP, SP, off);
913 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
914 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
915 }
916 }
917}
918
919void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
920 if (c->IsIntConstant() || c->IsNullConstant()) {
921 // Move 32 bit constant.
922 int32_t value = GetInt32ValueOf(c);
923 if (destination.IsRegister()) {
924 Register dst = destination.AsRegister<Register>();
925 __ LoadConst32(dst, value);
926 } else {
927 DCHECK(destination.IsStackSlot())
928 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700929 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200930 }
931 } else if (c->IsLongConstant()) {
932 // Move 64 bit constant.
933 int64_t value = GetInt64ValueOf(c);
934 if (destination.IsRegisterPair()) {
935 Register r_h = destination.AsRegisterPairHigh<Register>();
936 Register r_l = destination.AsRegisterPairLow<Register>();
937 __ LoadConst64(r_h, r_l, value);
938 } else {
939 DCHECK(destination.IsDoubleStackSlot())
940 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700941 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200942 }
943 } else if (c->IsFloatConstant()) {
944 // Move 32 bit float constant.
945 int32_t value = GetInt32ValueOf(c);
946 if (destination.IsFpuRegister()) {
947 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
948 } else {
949 DCHECK(destination.IsStackSlot())
950 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700951 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200952 }
953 } else {
954 // Move 64 bit double constant.
955 DCHECK(c->IsDoubleConstant()) << c->DebugName();
956 int64_t value = GetInt64ValueOf(c);
957 if (destination.IsFpuRegister()) {
958 FRegister fd = destination.AsFpuRegister<FRegister>();
959 __ LoadDConst64(fd, value, TMP);
960 } else {
961 DCHECK(destination.IsDoubleStackSlot())
962 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700963 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200964 }
965 }
966}
967
968void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
969 DCHECK(destination.IsRegister());
970 Register dst = destination.AsRegister<Register>();
971 __ LoadConst32(dst, value);
972}
973
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200974void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
975 if (location.IsRegister()) {
976 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700977 } else if (location.IsRegisterPair()) {
978 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
979 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200980 } else {
981 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
982 }
983}
984
Vladimir Markoaad75c62016-10-03 08:46:48 +0000985template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
986inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
987 const ArenaDeque<PcRelativePatchInfo>& infos,
988 ArenaVector<LinkerPatch>* linker_patches) {
989 for (const PcRelativePatchInfo& info : infos) {
990 const DexFile& dex_file = info.target_dex_file;
991 size_t offset_or_index = info.offset_or_index;
992 DCHECK(info.high_label.IsBound());
993 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
994 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
995 // the assembler's base label used for PC-relative addressing.
996 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
997 ? __ GetLabelLocation(&info.pc_rel_label)
998 : __ GetPcRelBaseLabelLocation();
999 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1000 }
1001}
1002
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001003void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1004 DCHECK(linker_patches->empty());
1005 size_t size =
1006 method_patches_.size() +
1007 call_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001008 pc_relative_dex_cache_patches_.size() +
1009 pc_relative_string_patches_.size() +
1010 pc_relative_type_patches_.size() +
1011 boot_image_string_patches_.size() +
1012 boot_image_type_patches_.size() +
1013 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001014 linker_patches->reserve(size);
1015 for (const auto& entry : method_patches_) {
1016 const MethodReference& target_method = entry.first;
1017 Literal* literal = entry.second;
1018 DCHECK(literal->GetLabel()->IsBound());
1019 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1020 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
1021 target_method.dex_file,
1022 target_method.dex_method_index));
1023 }
1024 for (const auto& entry : call_patches_) {
1025 const MethodReference& target_method = entry.first;
1026 Literal* literal = entry.second;
1027 DCHECK(literal->GetLabel()->IsBound());
1028 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1029 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
1030 target_method.dex_file,
1031 target_method.dex_method_index));
1032 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001033 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1034 linker_patches);
1035 if (!GetCompilerOptions().IsBootImage()) {
1036 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1037 linker_patches);
1038 } else {
1039 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1040 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001041 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001042 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1043 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001044 for (const auto& entry : boot_image_string_patches_) {
1045 const StringReference& target_string = entry.first;
1046 Literal* literal = entry.second;
1047 DCHECK(literal->GetLabel()->IsBound());
1048 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1049 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1050 target_string.dex_file,
1051 target_string.string_index));
1052 }
1053 for (const auto& entry : boot_image_type_patches_) {
1054 const TypeReference& target_type = entry.first;
1055 Literal* literal = entry.second;
1056 DCHECK(literal->GetLabel()->IsBound());
1057 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1058 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1059 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001060 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001061 }
1062 for (const auto& entry : boot_image_address_patches_) {
1063 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1064 Literal* literal = entry.second;
1065 DCHECK(literal->GetLabel()->IsBound());
1066 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1067 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1068 }
1069}
1070
1071CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1072 const DexFile& dex_file, uint32_t string_index) {
1073 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1074}
1075
1076CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001077 const DexFile& dex_file, dex::TypeIndex type_index) {
1078 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001079}
1080
1081CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1082 const DexFile& dex_file, uint32_t element_offset) {
1083 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1084}
1085
1086CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1087 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1088 patches->emplace_back(dex_file, offset_or_index);
1089 return &patches->back();
1090}
1091
Alexey Frunze06a46c42016-07-19 15:00:40 -07001092Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1093 return map->GetOrCreate(
1094 value,
1095 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1096}
1097
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001098Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1099 MethodToLiteralMap* map) {
1100 return map->GetOrCreate(
1101 target_method,
1102 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1103}
1104
1105Literal* CodeGeneratorMIPS::DeduplicateMethodAddressLiteral(MethodReference target_method) {
1106 return DeduplicateMethodLiteral(target_method, &method_patches_);
1107}
1108
1109Literal* CodeGeneratorMIPS::DeduplicateMethodCodeLiteral(MethodReference target_method) {
1110 return DeduplicateMethodLiteral(target_method, &call_patches_);
1111}
1112
Alexey Frunze06a46c42016-07-19 15:00:40 -07001113Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
1114 uint32_t string_index) {
1115 return boot_image_string_patches_.GetOrCreate(
1116 StringReference(&dex_file, string_index),
1117 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1118}
1119
1120Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001121 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001122 return boot_image_type_patches_.GetOrCreate(
1123 TypeReference(&dex_file, type_index),
1124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1125}
1126
1127Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1128 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1129 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1130 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1131}
1132
Vladimir Markoaad75c62016-10-03 08:46:48 +00001133void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1134 PcRelativePatchInfo* info, Register out, Register base) {
1135 bool reordering = __ SetReorder(false);
1136 if (GetInstructionSetFeatures().IsR6()) {
1137 DCHECK_EQ(base, ZERO);
1138 __ Bind(&info->high_label);
1139 __ Bind(&info->pc_rel_label);
1140 // Add a 32-bit offset to PC.
1141 __ Auipc(out, /* placeholder */ 0x1234);
1142 __ Addiu(out, out, /* placeholder */ 0x5678);
1143 } else {
1144 // If base is ZERO, emit NAL to obtain the actual base.
1145 if (base == ZERO) {
1146 // Generate a dummy PC-relative call to obtain PC.
1147 __ Nal();
1148 }
1149 __ Bind(&info->high_label);
1150 __ Lui(out, /* placeholder */ 0x1234);
1151 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1152 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1153 if (base == ZERO) {
1154 __ Bind(&info->pc_rel_label);
1155 }
1156 __ Ori(out, out, /* placeholder */ 0x5678);
1157 // Add a 32-bit offset to PC.
1158 __ Addu(out, out, (base == ZERO) ? RA : base);
1159 }
1160 __ SetReorder(reordering);
1161}
1162
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001163void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1164 MipsLabel done;
1165 Register card = AT;
1166 Register temp = TMP;
1167 __ Beqz(value, &done);
1168 __ LoadFromOffset(kLoadWord,
1169 card,
1170 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001171 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001172 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1173 __ Addu(temp, card, temp);
1174 __ Sb(card, temp, 0);
1175 __ Bind(&done);
1176}
1177
David Brazdil58282f42016-01-14 12:45:10 +00001178void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001179 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1180 blocked_core_registers_[ZERO] = true;
1181 blocked_core_registers_[K0] = true;
1182 blocked_core_registers_[K1] = true;
1183 blocked_core_registers_[GP] = true;
1184 blocked_core_registers_[SP] = true;
1185 blocked_core_registers_[RA] = true;
1186
1187 // AT and TMP(T8) are used as temporary/scratch registers
1188 // (similar to how AT is used by MIPS assemblers).
1189 blocked_core_registers_[AT] = true;
1190 blocked_core_registers_[TMP] = true;
1191 blocked_fpu_registers_[FTMP] = true;
1192
1193 // Reserve suspend and thread registers.
1194 blocked_core_registers_[S0] = true;
1195 blocked_core_registers_[TR] = true;
1196
1197 // Reserve T9 for function calls
1198 blocked_core_registers_[T9] = true;
1199
1200 // Reserve odd-numbered FPU registers.
1201 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1202 blocked_fpu_registers_[i] = true;
1203 }
1204
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001205 if (GetGraph()->IsDebuggable()) {
1206 // Stubs do not save callee-save floating point registers. If the graph
1207 // is debuggable, we need to deal with these registers differently. For
1208 // now, just block them.
1209 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1210 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1211 }
1212 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001213}
1214
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001215size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1216 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1217 return kMipsWordSize;
1218}
1219
1220size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1221 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1222 return kMipsWordSize;
1223}
1224
1225size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1226 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1227 return kMipsDoublewordSize;
1228}
1229
1230size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1231 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1232 return kMipsDoublewordSize;
1233}
1234
1235void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001236 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001237}
1238
1239void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001240 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001241}
1242
Serban Constantinescufca16662016-07-14 09:21:59 +01001243constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1244
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1246 HInstruction* instruction,
1247 uint32_t dex_pc,
1248 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001249 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001250 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001251 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001252 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001253 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254 // Reserve argument space on stack (for $a0-$a3) for
1255 // entrypoints that directly reference native implementations.
1256 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001257 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001258 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001259 } else {
1260 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001261 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001262 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001263 if (EntrypointRequiresStackMap(entrypoint)) {
1264 RecordPcInfo(instruction, dex_pc, slow_path);
1265 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266}
1267
1268void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1269 Register class_reg) {
1270 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1271 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1272 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1273 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1274 __ Sync(0);
1275 __ Bind(slow_path->GetExitLabel());
1276}
1277
1278void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1279 __ Sync(0); // Only stype 0 is supported.
1280}
1281
1282void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1283 HBasicBlock* successor) {
1284 SuspendCheckSlowPathMIPS* slow_path =
1285 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1286 codegen_->AddSlowPath(slow_path);
1287
1288 __ LoadFromOffset(kLoadUnsignedHalfword,
1289 TMP,
1290 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001291 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001292 if (successor == nullptr) {
1293 __ Bnez(TMP, slow_path->GetEntryLabel());
1294 __ Bind(slow_path->GetReturnLabel());
1295 } else {
1296 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1297 __ B(slow_path->GetEntryLabel());
1298 // slow_path will return to GetLabelOf(successor).
1299 }
1300}
1301
1302InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1303 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001304 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 assembler_(codegen->GetAssembler()),
1306 codegen_(codegen) {}
1307
1308void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1309 DCHECK_EQ(instruction->InputCount(), 2U);
1310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1311 Primitive::Type type = instruction->GetResultType();
1312 switch (type) {
1313 case Primitive::kPrimInt: {
1314 locations->SetInAt(0, Location::RequiresRegister());
1315 HInstruction* right = instruction->InputAt(1);
1316 bool can_use_imm = false;
1317 if (right->IsConstant()) {
1318 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1319 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1320 can_use_imm = IsUint<16>(imm);
1321 } else if (instruction->IsAdd()) {
1322 can_use_imm = IsInt<16>(imm);
1323 } else {
1324 DCHECK(instruction->IsSub());
1325 can_use_imm = IsInt<16>(-imm);
1326 }
1327 }
1328 if (can_use_imm)
1329 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1330 else
1331 locations->SetInAt(1, Location::RequiresRegister());
1332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1333 break;
1334 }
1335
1336 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001338 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001340 break;
1341 }
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 DCHECK(instruction->IsAdd() || instruction->IsSub());
1346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetInAt(1, Location::RequiresFpuRegister());
1348 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1353 }
1354}
1355
1356void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1357 Primitive::Type type = instruction->GetType();
1358 LocationSummary* locations = instruction->GetLocations();
1359
1360 switch (type) {
1361 case Primitive::kPrimInt: {
1362 Register dst = locations->Out().AsRegister<Register>();
1363 Register lhs = locations->InAt(0).AsRegister<Register>();
1364 Location rhs_location = locations->InAt(1);
1365
1366 Register rhs_reg = ZERO;
1367 int32_t rhs_imm = 0;
1368 bool use_imm = rhs_location.IsConstant();
1369 if (use_imm) {
1370 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1371 } else {
1372 rhs_reg = rhs_location.AsRegister<Register>();
1373 }
1374
1375 if (instruction->IsAnd()) {
1376 if (use_imm)
1377 __ Andi(dst, lhs, rhs_imm);
1378 else
1379 __ And(dst, lhs, rhs_reg);
1380 } else if (instruction->IsOr()) {
1381 if (use_imm)
1382 __ Ori(dst, lhs, rhs_imm);
1383 else
1384 __ Or(dst, lhs, rhs_reg);
1385 } else if (instruction->IsXor()) {
1386 if (use_imm)
1387 __ Xori(dst, lhs, rhs_imm);
1388 else
1389 __ Xor(dst, lhs, rhs_reg);
1390 } else if (instruction->IsAdd()) {
1391 if (use_imm)
1392 __ Addiu(dst, lhs, rhs_imm);
1393 else
1394 __ Addu(dst, lhs, rhs_reg);
1395 } else {
1396 DCHECK(instruction->IsSub());
1397 if (use_imm)
1398 __ Addiu(dst, lhs, -rhs_imm);
1399 else
1400 __ Subu(dst, lhs, rhs_reg);
1401 }
1402 break;
1403 }
1404
1405 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001406 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1407 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1408 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1409 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001410 Location rhs_location = locations->InAt(1);
1411 bool use_imm = rhs_location.IsConstant();
1412 if (!use_imm) {
1413 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1414 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1415 if (instruction->IsAnd()) {
1416 __ And(dst_low, lhs_low, rhs_low);
1417 __ And(dst_high, lhs_high, rhs_high);
1418 } else if (instruction->IsOr()) {
1419 __ Or(dst_low, lhs_low, rhs_low);
1420 __ Or(dst_high, lhs_high, rhs_high);
1421 } else if (instruction->IsXor()) {
1422 __ Xor(dst_low, lhs_low, rhs_low);
1423 __ Xor(dst_high, lhs_high, rhs_high);
1424 } else if (instruction->IsAdd()) {
1425 if (lhs_low == rhs_low) {
1426 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1427 __ Slt(TMP, lhs_low, ZERO);
1428 __ Addu(dst_low, lhs_low, rhs_low);
1429 } else {
1430 __ Addu(dst_low, lhs_low, rhs_low);
1431 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1432 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1433 }
1434 __ Addu(dst_high, lhs_high, rhs_high);
1435 __ Addu(dst_high, dst_high, TMP);
1436 } else {
1437 DCHECK(instruction->IsSub());
1438 __ Sltu(TMP, lhs_low, rhs_low);
1439 __ Subu(dst_low, lhs_low, rhs_low);
1440 __ Subu(dst_high, lhs_high, rhs_high);
1441 __ Subu(dst_high, dst_high, TMP);
1442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001444 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1445 if (instruction->IsOr()) {
1446 uint32_t low = Low32Bits(value);
1447 uint32_t high = High32Bits(value);
1448 if (IsUint<16>(low)) {
1449 if (dst_low != lhs_low || low != 0) {
1450 __ Ori(dst_low, lhs_low, low);
1451 }
1452 } else {
1453 __ LoadConst32(TMP, low);
1454 __ Or(dst_low, lhs_low, TMP);
1455 }
1456 if (IsUint<16>(high)) {
1457 if (dst_high != lhs_high || high != 0) {
1458 __ Ori(dst_high, lhs_high, high);
1459 }
1460 } else {
1461 if (high != low) {
1462 __ LoadConst32(TMP, high);
1463 }
1464 __ Or(dst_high, lhs_high, TMP);
1465 }
1466 } else if (instruction->IsXor()) {
1467 uint32_t low = Low32Bits(value);
1468 uint32_t high = High32Bits(value);
1469 if (IsUint<16>(low)) {
1470 if (dst_low != lhs_low || low != 0) {
1471 __ Xori(dst_low, lhs_low, low);
1472 }
1473 } else {
1474 __ LoadConst32(TMP, low);
1475 __ Xor(dst_low, lhs_low, TMP);
1476 }
1477 if (IsUint<16>(high)) {
1478 if (dst_high != lhs_high || high != 0) {
1479 __ Xori(dst_high, lhs_high, high);
1480 }
1481 } else {
1482 if (high != low) {
1483 __ LoadConst32(TMP, high);
1484 }
1485 __ Xor(dst_high, lhs_high, TMP);
1486 }
1487 } else if (instruction->IsAnd()) {
1488 uint32_t low = Low32Bits(value);
1489 uint32_t high = High32Bits(value);
1490 if (IsUint<16>(low)) {
1491 __ Andi(dst_low, lhs_low, low);
1492 } else if (low != 0xFFFFFFFF) {
1493 __ LoadConst32(TMP, low);
1494 __ And(dst_low, lhs_low, TMP);
1495 } else if (dst_low != lhs_low) {
1496 __ Move(dst_low, lhs_low);
1497 }
1498 if (IsUint<16>(high)) {
1499 __ Andi(dst_high, lhs_high, high);
1500 } else if (high != 0xFFFFFFFF) {
1501 if (high != low) {
1502 __ LoadConst32(TMP, high);
1503 }
1504 __ And(dst_high, lhs_high, TMP);
1505 } else if (dst_high != lhs_high) {
1506 __ Move(dst_high, lhs_high);
1507 }
1508 } else {
1509 if (instruction->IsSub()) {
1510 value = -value;
1511 } else {
1512 DCHECK(instruction->IsAdd());
1513 }
1514 int32_t low = Low32Bits(value);
1515 int32_t high = High32Bits(value);
1516 if (IsInt<16>(low)) {
1517 if (dst_low != lhs_low || low != 0) {
1518 __ Addiu(dst_low, lhs_low, low);
1519 }
1520 if (low != 0) {
1521 __ Sltiu(AT, dst_low, low);
1522 }
1523 } else {
1524 __ LoadConst32(TMP, low);
1525 __ Addu(dst_low, lhs_low, TMP);
1526 __ Sltu(AT, dst_low, TMP);
1527 }
1528 if (IsInt<16>(high)) {
1529 if (dst_high != lhs_high || high != 0) {
1530 __ Addiu(dst_high, lhs_high, high);
1531 }
1532 } else {
1533 if (high != low) {
1534 __ LoadConst32(TMP, high);
1535 }
1536 __ Addu(dst_high, lhs_high, TMP);
1537 }
1538 if (low != 0) {
1539 __ Addu(dst_high, dst_high, AT);
1540 }
1541 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimFloat:
1547 case Primitive::kPrimDouble: {
1548 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1549 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1550 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1551 if (instruction->IsAdd()) {
1552 if (type == Primitive::kPrimFloat) {
1553 __ AddS(dst, lhs, rhs);
1554 } else {
1555 __ AddD(dst, lhs, rhs);
1556 }
1557 } else {
1558 DCHECK(instruction->IsSub());
1559 if (type == Primitive::kPrimFloat) {
1560 __ SubS(dst, lhs, rhs);
1561 } else {
1562 __ SubD(dst, lhs, rhs);
1563 }
1564 }
1565 break;
1566 }
1567
1568 default:
1569 LOG(FATAL) << "Unexpected binary operation type " << type;
1570 }
1571}
1572
1573void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001574 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575
1576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1577 Primitive::Type type = instr->GetResultType();
1578 switch (type) {
1579 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001580 locations->SetInAt(0, Location::RequiresRegister());
1581 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1583 break;
1584 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001585 locations->SetInAt(0, Location::RequiresRegister());
1586 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1587 locations->SetOut(Location::RequiresRegister());
1588 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001589 default:
1590 LOG(FATAL) << "Unexpected shift type " << type;
1591 }
1592}
1593
1594static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1595
1596void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001597 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 LocationSummary* locations = instr->GetLocations();
1599 Primitive::Type type = instr->GetType();
1600
1601 Location rhs_location = locations->InAt(1);
1602 bool use_imm = rhs_location.IsConstant();
1603 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1604 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001605 const uint32_t shift_mask =
1606 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001607 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001608 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1609 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001610
1611 switch (type) {
1612 case Primitive::kPrimInt: {
1613 Register dst = locations->Out().AsRegister<Register>();
1614 Register lhs = locations->InAt(0).AsRegister<Register>();
1615 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001616 if (shift_value == 0) {
1617 if (dst != lhs) {
1618 __ Move(dst, lhs);
1619 }
1620 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001621 __ Sll(dst, lhs, shift_value);
1622 } else if (instr->IsShr()) {
1623 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001624 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001626 } else {
1627 if (has_ins_rotr) {
1628 __ Rotr(dst, lhs, shift_value);
1629 } else {
1630 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1631 __ Srl(dst, lhs, shift_value);
1632 __ Or(dst, dst, TMP);
1633 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001634 }
1635 } else {
1636 if (instr->IsShl()) {
1637 __ Sllv(dst, lhs, rhs_reg);
1638 } else if (instr->IsShr()) {
1639 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001640 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001641 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001642 } else {
1643 if (has_ins_rotr) {
1644 __ Rotrv(dst, lhs, rhs_reg);
1645 } else {
1646 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001647 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1648 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1649 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1650 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1651 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001652 __ Sllv(TMP, lhs, TMP);
1653 __ Srlv(dst, lhs, rhs_reg);
1654 __ Or(dst, dst, TMP);
1655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001656 }
1657 }
1658 break;
1659 }
1660
1661 case Primitive::kPrimLong: {
1662 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1663 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1664 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1665 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1666 if (use_imm) {
1667 if (shift_value == 0) {
1668 codegen_->Move64(locations->Out(), locations->InAt(0));
1669 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001670 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001671 if (instr->IsShl()) {
1672 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1673 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1674 __ Sll(dst_low, lhs_low, shift_value);
1675 } else if (instr->IsShr()) {
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1678 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001679 } else if (instr->IsUShr()) {
1680 __ Srl(dst_low, lhs_low, shift_value);
1681 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1682 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001683 } else {
1684 __ Srl(dst_low, lhs_low, shift_value);
1685 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1686 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001687 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001689 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001690 if (instr->IsShl()) {
1691 __ Sll(dst_low, lhs_low, shift_value);
1692 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1693 __ Sll(dst_high, lhs_high, shift_value);
1694 __ Or(dst_high, dst_high, TMP);
1695 } else if (instr->IsShr()) {
1696 __ Sra(dst_high, lhs_high, shift_value);
1697 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1698 __ Srl(dst_low, lhs_low, shift_value);
1699 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001701 __ Srl(dst_high, lhs_high, shift_value);
1702 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1703 __ Srl(dst_low, lhs_low, shift_value);
1704 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001705 } else {
1706 __ Srl(TMP, lhs_low, shift_value);
1707 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1708 __ Or(dst_low, dst_low, TMP);
1709 __ Srl(TMP, lhs_high, shift_value);
1710 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1711 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001713 }
1714 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001715 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001716 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001717 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 __ Move(dst_low, ZERO);
1719 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001720 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001721 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001722 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001723 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001724 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001725 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001726 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001727 // 64-bit rotation by 32 is just a swap.
1728 __ Move(dst_low, lhs_high);
1729 __ Move(dst_high, lhs_low);
1730 } else {
1731 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001732 __ Srl(dst_low, lhs_high, shift_value_high);
1733 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1734 __ Srl(dst_high, lhs_low, shift_value_high);
1735 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001736 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001737 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1738 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001739 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001740 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1741 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001742 __ Or(dst_high, dst_high, TMP);
1743 }
1744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 }
1746 }
1747 } else {
1748 MipsLabel done;
1749 if (instr->IsShl()) {
1750 __ Sllv(dst_low, lhs_low, rhs_reg);
1751 __ Nor(AT, ZERO, rhs_reg);
1752 __ Srl(TMP, lhs_low, 1);
1753 __ Srlv(TMP, TMP, AT);
1754 __ Sllv(dst_high, lhs_high, rhs_reg);
1755 __ Or(dst_high, dst_high, TMP);
1756 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1757 __ Beqz(TMP, &done);
1758 __ Move(dst_high, dst_low);
1759 __ Move(dst_low, ZERO);
1760 } else if (instr->IsShr()) {
1761 __ Srav(dst_high, lhs_high, rhs_reg);
1762 __ Nor(AT, ZERO, rhs_reg);
1763 __ Sll(TMP, lhs_high, 1);
1764 __ Sllv(TMP, TMP, AT);
1765 __ Srlv(dst_low, lhs_low, rhs_reg);
1766 __ Or(dst_low, dst_low, TMP);
1767 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1768 __ Beqz(TMP, &done);
1769 __ Move(dst_low, dst_high);
1770 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001771 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001772 __ Srlv(dst_high, lhs_high, rhs_reg);
1773 __ Nor(AT, ZERO, rhs_reg);
1774 __ Sll(TMP, lhs_high, 1);
1775 __ Sllv(TMP, TMP, AT);
1776 __ Srlv(dst_low, lhs_low, rhs_reg);
1777 __ Or(dst_low, dst_low, TMP);
1778 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1779 __ Beqz(TMP, &done);
1780 __ Move(dst_low, dst_high);
1781 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001782 } else {
1783 __ Nor(AT, ZERO, rhs_reg);
1784 __ Srlv(TMP, lhs_low, rhs_reg);
1785 __ Sll(dst_low, lhs_high, 1);
1786 __ Sllv(dst_low, dst_low, AT);
1787 __ Or(dst_low, dst_low, TMP);
1788 __ Srlv(TMP, lhs_high, rhs_reg);
1789 __ Sll(dst_high, lhs_low, 1);
1790 __ Sllv(dst_high, dst_high, AT);
1791 __ Or(dst_high, dst_high, TMP);
1792 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1793 __ Beqz(TMP, &done);
1794 __ Move(TMP, dst_high);
1795 __ Move(dst_high, dst_low);
1796 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001797 }
1798 __ Bind(&done);
1799 }
1800 break;
1801 }
1802
1803 default:
1804 LOG(FATAL) << "Unexpected shift operation type " << type;
1805 }
1806}
1807
1808void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1809 HandleBinaryOp(instruction);
1810}
1811
1812void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1813 HandleBinaryOp(instruction);
1814}
1815
1816void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1817 HandleBinaryOp(instruction);
1818}
1819
1820void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1821 HandleBinaryOp(instruction);
1822}
1823
1824void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1825 LocationSummary* locations =
1826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1827 locations->SetInAt(0, Location::RequiresRegister());
1828 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1829 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1830 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1831 } else {
1832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1833 }
1834}
1835
Alexey Frunze2923db72016-08-20 01:55:47 -07001836auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1837 auto null_checker = [this, instruction]() {
1838 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1839 };
1840 return null_checker;
1841}
1842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1844 LocationSummary* locations = instruction->GetLocations();
1845 Register obj = locations->InAt(0).AsRegister<Register>();
1846 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001847 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001848 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001850 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001851 switch (type) {
1852 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 Register out = locations->Out().AsRegister<Register>();
1854 if (index.IsConstant()) {
1855 size_t offset =
1856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001857 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 } else {
1859 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001860 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 }
1862 break;
1863 }
1864
1865 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866 Register out = locations->Out().AsRegister<Register>();
1867 if (index.IsConstant()) {
1868 size_t offset =
1869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001870 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 } else {
1872 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001873 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001874 }
1875 break;
1876 }
1877
1878 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879 Register out = locations->Out().AsRegister<Register>();
1880 if (index.IsConstant()) {
1881 size_t offset =
1882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001883 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 } else {
1885 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1886 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001887 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001888 }
1889 break;
1890 }
1891
1892 case Primitive::kPrimChar: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893 Register out = locations->Out().AsRegister<Register>();
1894 if (index.IsConstant()) {
1895 size_t offset =
1896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001897 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001898 } else {
1899 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1900 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001901 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 }
1903 break;
1904 }
1905
1906 case Primitive::kPrimInt:
1907 case Primitive::kPrimNot: {
1908 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 Register out = locations->Out().AsRegister<Register>();
1910 if (index.IsConstant()) {
1911 size_t offset =
1912 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001913 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914 } else {
1915 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1916 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001917 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001918 }
1919 break;
1920 }
1921
1922 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923 Register out = locations->Out().AsRegisterPairLow<Register>();
1924 if (index.IsConstant()) {
1925 size_t offset =
1926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001927 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 } else {
1929 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1930 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001931 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001932 }
1933 break;
1934 }
1935
1936 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1938 if (index.IsConstant()) {
1939 size_t offset =
1940 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001941 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942 } else {
1943 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1944 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001945 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946 }
1947 break;
1948 }
1949
1950 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1952 if (index.IsConstant()) {
1953 size_t offset =
1954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001955 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001956 } else {
1957 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1958 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001959 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960 }
1961 break;
1962 }
1963
1964 case Primitive::kPrimVoid:
1965 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1966 UNREACHABLE();
1967 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001968}
1969
1970void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1974}
1975
1976void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1977 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001978 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 Register obj = locations->InAt(0).AsRegister<Register>();
1980 Register out = locations->Out().AsRegister<Register>();
1981 __ LoadFromOffset(kLoadWord, out, obj, offset);
1982 codegen_->MaybeRecordImplicitNullCheck(instruction);
1983}
1984
Alexey Frunzef58b2482016-09-02 22:14:06 -07001985Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1986 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1987 ? Location::ConstantLocation(instruction->AsConstant())
1988 : Location::RequiresRegister();
1989}
1990
1991Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1992 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1993 // We can store a non-zero float or double constant without first loading it into the FPU,
1994 // but we should only prefer this if the constant has a single use.
1995 if (instruction->IsConstant() &&
1996 (instruction->AsConstant()->IsZeroBitPattern() ||
1997 instruction->GetUses().HasExactlyOneElement())) {
1998 return Location::ConstantLocation(instruction->AsConstant());
1999 // Otherwise fall through and require an FPU register for the constant.
2000 }
2001 return Location::RequiresFpuRegister();
2002}
2003
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002004void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01002005 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2007 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002008 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01002009 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 InvokeRuntimeCallingConvention calling_convention;
2011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2013 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2014 } else {
2015 locations->SetInAt(0, Location::RequiresRegister());
2016 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2017 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002018 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002020 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 }
2022 }
2023}
2024
2025void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2026 LocationSummary* locations = instruction->GetLocations();
2027 Register obj = locations->InAt(0).AsRegister<Register>();
2028 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002029 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002030 Primitive::Type value_type = instruction->GetComponentType();
2031 bool needs_runtime_call = locations->WillCall();
2032 bool needs_write_barrier =
2033 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002034 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002035 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036
2037 switch (value_type) {
2038 case Primitive::kPrimBoolean:
2039 case Primitive::kPrimByte: {
2040 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002041 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002042 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002044 __ Addu(base_reg, obj, index.AsRegister<Register>());
2045 }
2046 if (value_location.IsConstant()) {
2047 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2048 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2049 } else {
2050 Register value = value_location.AsRegister<Register>();
2051 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 }
2053 break;
2054 }
2055
2056 case Primitive::kPrimShort:
2057 case Primitive::kPrimChar: {
2058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002060 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002062 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2063 __ Addu(base_reg, obj, base_reg);
2064 }
2065 if (value_location.IsConstant()) {
2066 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2067 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2068 } else {
2069 Register value = value_location.AsRegister<Register>();
2070 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071 }
2072 break;
2073 }
2074
2075 case Primitive::kPrimInt:
2076 case Primitive::kPrimNot: {
2077 if (!needs_runtime_call) {
2078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002079 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002080 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002081 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002082 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2083 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002085 if (value_location.IsConstant()) {
2086 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2087 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2088 DCHECK(!needs_write_barrier);
2089 } else {
2090 Register value = value_location.AsRegister<Register>();
2091 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2092 if (needs_write_barrier) {
2093 DCHECK_EQ(value_type, Primitive::kPrimNot);
2094 codegen_->MarkGCCard(obj, value);
2095 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002096 }
2097 } else {
2098 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002099 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002100 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2101 }
2102 break;
2103 }
2104
2105 case Primitive::kPrimLong: {
2106 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002107 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002108 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002109 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002110 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2111 __ Addu(base_reg, obj, base_reg);
2112 }
2113 if (value_location.IsConstant()) {
2114 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2115 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2116 } else {
2117 Register value = value_location.AsRegisterPairLow<Register>();
2118 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002119 }
2120 break;
2121 }
2122
2123 case Primitive::kPrimFloat: {
2124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002125 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002126 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002127 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002128 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2129 __ Addu(base_reg, obj, base_reg);
2130 }
2131 if (value_location.IsConstant()) {
2132 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2133 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2134 } else {
2135 FRegister value = value_location.AsFpuRegister<FRegister>();
2136 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002137 }
2138 break;
2139 }
2140
2141 case Primitive::kPrimDouble: {
2142 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002144 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002145 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002146 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2147 __ Addu(base_reg, obj, base_reg);
2148 }
2149 if (value_location.IsConstant()) {
2150 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2151 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2152 } else {
2153 FRegister value = value_location.AsFpuRegister<FRegister>();
2154 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 }
2156 break;
2157 }
2158
2159 case Primitive::kPrimVoid:
2160 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2161 UNREACHABLE();
2162 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002163}
2164
2165void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002166 RegisterSet caller_saves = RegisterSet::Empty();
2167 InvokeRuntimeCallingConvention calling_convention;
2168 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2170 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002171 locations->SetInAt(0, Location::RequiresRegister());
2172 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002173}
2174
2175void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2176 LocationSummary* locations = instruction->GetLocations();
2177 BoundsCheckSlowPathMIPS* slow_path =
2178 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2179 codegen_->AddSlowPath(slow_path);
2180
2181 Register index = locations->InAt(0).AsRegister<Register>();
2182 Register length = locations->InAt(1).AsRegister<Register>();
2183
2184 // length is limited by the maximum positive signed 32-bit integer.
2185 // Unsigned comparison of length and index checks for index < 0
2186 // and for length <= index simultaneously.
2187 __ Bgeu(index, length, slow_path->GetEntryLabel());
2188}
2189
2190void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2191 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2192 instruction,
2193 LocationSummary::kCallOnSlowPath);
2194 locations->SetInAt(0, Location::RequiresRegister());
2195 locations->SetInAt(1, Location::RequiresRegister());
2196 // Note that TypeCheckSlowPathMIPS uses this register too.
2197 locations->AddTemp(Location::RequiresRegister());
2198}
2199
2200void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2201 LocationSummary* locations = instruction->GetLocations();
2202 Register obj = locations->InAt(0).AsRegister<Register>();
2203 Register cls = locations->InAt(1).AsRegister<Register>();
2204 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2205
2206 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2207 codegen_->AddSlowPath(slow_path);
2208
2209 // TODO: avoid this check if we know obj is not null.
2210 __ Beqz(obj, slow_path->GetExitLabel());
2211 // Compare the class of `obj` with `cls`.
2212 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2213 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2214 __ Bind(slow_path->GetExitLabel());
2215}
2216
2217void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2220 locations->SetInAt(0, Location::RequiresRegister());
2221 if (check->HasUses()) {
2222 locations->SetOut(Location::SameAsFirstInput());
2223 }
2224}
2225
2226void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2227 // We assume the class is not null.
2228 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2229 check->GetLoadClass(),
2230 check,
2231 check->GetDexPc(),
2232 true);
2233 codegen_->AddSlowPath(slow_path);
2234 GenerateClassInitializationCheck(slow_path,
2235 check->GetLocations()->InAt(0).AsRegister<Register>());
2236}
2237
2238void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2239 Primitive::Type in_type = compare->InputAt(0)->GetType();
2240
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002241 LocationSummary* locations =
2242 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243
2244 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002245 case Primitive::kPrimBoolean:
2246 case Primitive::kPrimByte:
2247 case Primitive::kPrimShort:
2248 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002249 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002250 locations->SetInAt(0, Location::RequiresRegister());
2251 locations->SetInAt(1, Location::RequiresRegister());
2252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2253 break;
2254
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 case Primitive::kPrimLong:
2256 locations->SetInAt(0, Location::RequiresRegister());
2257 locations->SetInAt(1, Location::RequiresRegister());
2258 // Output overlaps because it is written before doing the low comparison.
2259 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2260 break;
2261
2262 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002263 case Primitive::kPrimDouble:
2264 locations->SetInAt(0, Location::RequiresFpuRegister());
2265 locations->SetInAt(1, Location::RequiresFpuRegister());
2266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002268
2269 default:
2270 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2271 }
2272}
2273
2274void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2275 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002276 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002277 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002278 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002279
2280 // 0 if: left == right
2281 // 1 if: left > right
2282 // -1 if: left < right
2283 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002284 case Primitive::kPrimBoolean:
2285 case Primitive::kPrimByte:
2286 case Primitive::kPrimShort:
2287 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002288 case Primitive::kPrimInt: {
2289 Register lhs = locations->InAt(0).AsRegister<Register>();
2290 Register rhs = locations->InAt(1).AsRegister<Register>();
2291 __ Slt(TMP, lhs, rhs);
2292 __ Slt(res, rhs, lhs);
2293 __ Subu(res, res, TMP);
2294 break;
2295 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002296 case Primitive::kPrimLong: {
2297 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2299 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2300 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2301 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2302 // TODO: more efficient (direct) comparison with a constant.
2303 __ Slt(TMP, lhs_high, rhs_high);
2304 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2305 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2306 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2307 __ Sltu(TMP, lhs_low, rhs_low);
2308 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2309 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2310 __ Bind(&done);
2311 break;
2312 }
2313
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002314 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002315 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002316 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2317 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2318 MipsLabel done;
2319 if (isR6) {
2320 __ CmpEqS(FTMP, lhs, rhs);
2321 __ LoadConst32(res, 0);
2322 __ Bc1nez(FTMP, &done);
2323 if (gt_bias) {
2324 __ CmpLtS(FTMP, lhs, rhs);
2325 __ LoadConst32(res, -1);
2326 __ Bc1nez(FTMP, &done);
2327 __ LoadConst32(res, 1);
2328 } else {
2329 __ CmpLtS(FTMP, rhs, lhs);
2330 __ LoadConst32(res, 1);
2331 __ Bc1nez(FTMP, &done);
2332 __ LoadConst32(res, -1);
2333 }
2334 } else {
2335 if (gt_bias) {
2336 __ ColtS(0, lhs, rhs);
2337 __ LoadConst32(res, -1);
2338 __ Bc1t(0, &done);
2339 __ CeqS(0, lhs, rhs);
2340 __ LoadConst32(res, 1);
2341 __ Movt(res, ZERO, 0);
2342 } else {
2343 __ ColtS(0, rhs, lhs);
2344 __ LoadConst32(res, 1);
2345 __ Bc1t(0, &done);
2346 __ CeqS(0, lhs, rhs);
2347 __ LoadConst32(res, -1);
2348 __ Movt(res, ZERO, 0);
2349 }
2350 }
2351 __ Bind(&done);
2352 break;
2353 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002355 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002356 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2357 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2358 MipsLabel done;
2359 if (isR6) {
2360 __ CmpEqD(FTMP, lhs, rhs);
2361 __ LoadConst32(res, 0);
2362 __ Bc1nez(FTMP, &done);
2363 if (gt_bias) {
2364 __ CmpLtD(FTMP, lhs, rhs);
2365 __ LoadConst32(res, -1);
2366 __ Bc1nez(FTMP, &done);
2367 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002369 __ CmpLtD(FTMP, rhs, lhs);
2370 __ LoadConst32(res, 1);
2371 __ Bc1nez(FTMP, &done);
2372 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002373 }
2374 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 if (gt_bias) {
2376 __ ColtD(0, lhs, rhs);
2377 __ LoadConst32(res, -1);
2378 __ Bc1t(0, &done);
2379 __ CeqD(0, lhs, rhs);
2380 __ LoadConst32(res, 1);
2381 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002383 __ ColtD(0, rhs, lhs);
2384 __ LoadConst32(res, 1);
2385 __ Bc1t(0, &done);
2386 __ CeqD(0, lhs, rhs);
2387 __ LoadConst32(res, -1);
2388 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 }
2390 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002391 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 break;
2393 }
2394
2395 default:
2396 LOG(FATAL) << "Unimplemented compare type " << in_type;
2397 }
2398}
2399
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002400void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002402 switch (instruction->InputAt(0)->GetType()) {
2403 default:
2404 case Primitive::kPrimLong:
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2407 break;
2408
2409 case Primitive::kPrimFloat:
2410 case Primitive::kPrimDouble:
2411 locations->SetInAt(0, Location::RequiresFpuRegister());
2412 locations->SetInAt(1, Location::RequiresFpuRegister());
2413 break;
2414 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002415 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2417 }
2418}
2419
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002420void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002421 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 return;
2423 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002425 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 LocationSummary* locations = instruction->GetLocations();
2427 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002428 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002430 switch (type) {
2431 default:
2432 // Integer case.
2433 GenerateIntCompare(instruction->GetCondition(), locations);
2434 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002436 case Primitive::kPrimLong:
2437 // TODO: don't use branches.
2438 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 break;
2440
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002441 case Primitive::kPrimFloat:
2442 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002443 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2444 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002446
2447 // Convert the branches into the result.
2448 MipsLabel done;
2449
2450 // False case: result = 0.
2451 __ LoadConst32(dst, 0);
2452 __ B(&done);
2453
2454 // True case: result = 1.
2455 __ Bind(&true_label);
2456 __ LoadConst32(dst, 1);
2457 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458}
2459
Alexey Frunze7e99e052015-11-24 19:28:01 -08002460void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2461 DCHECK(instruction->IsDiv() || instruction->IsRem());
2462 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2463
2464 LocationSummary* locations = instruction->GetLocations();
2465 Location second = locations->InAt(1);
2466 DCHECK(second.IsConstant());
2467
2468 Register out = locations->Out().AsRegister<Register>();
2469 Register dividend = locations->InAt(0).AsRegister<Register>();
2470 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2471 DCHECK(imm == 1 || imm == -1);
2472
2473 if (instruction->IsRem()) {
2474 __ Move(out, ZERO);
2475 } else {
2476 if (imm == -1) {
2477 __ Subu(out, ZERO, dividend);
2478 } else if (out != dividend) {
2479 __ Move(out, dividend);
2480 }
2481 }
2482}
2483
2484void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2485 DCHECK(instruction->IsDiv() || instruction->IsRem());
2486 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2487
2488 LocationSummary* locations = instruction->GetLocations();
2489 Location second = locations->InAt(1);
2490 DCHECK(second.IsConstant());
2491
2492 Register out = locations->Out().AsRegister<Register>();
2493 Register dividend = locations->InAt(0).AsRegister<Register>();
2494 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002495 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002496 int ctz_imm = CTZ(abs_imm);
2497
2498 if (instruction->IsDiv()) {
2499 if (ctz_imm == 1) {
2500 // Fast path for division by +/-2, which is very common.
2501 __ Srl(TMP, dividend, 31);
2502 } else {
2503 __ Sra(TMP, dividend, 31);
2504 __ Srl(TMP, TMP, 32 - ctz_imm);
2505 }
2506 __ Addu(out, dividend, TMP);
2507 __ Sra(out, out, ctz_imm);
2508 if (imm < 0) {
2509 __ Subu(out, ZERO, out);
2510 }
2511 } else {
2512 if (ctz_imm == 1) {
2513 // Fast path for modulo +/-2, which is very common.
2514 __ Sra(TMP, dividend, 31);
2515 __ Subu(out, dividend, TMP);
2516 __ Andi(out, out, 1);
2517 __ Addu(out, out, TMP);
2518 } else {
2519 __ Sra(TMP, dividend, 31);
2520 __ Srl(TMP, TMP, 32 - ctz_imm);
2521 __ Addu(out, dividend, TMP);
2522 if (IsUint<16>(abs_imm - 1)) {
2523 __ Andi(out, out, abs_imm - 1);
2524 } else {
2525 __ Sll(out, out, 32 - ctz_imm);
2526 __ Srl(out, out, 32 - ctz_imm);
2527 }
2528 __ Subu(out, out, TMP);
2529 }
2530 }
2531}
2532
2533void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2534 DCHECK(instruction->IsDiv() || instruction->IsRem());
2535 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2536
2537 LocationSummary* locations = instruction->GetLocations();
2538 Location second = locations->InAt(1);
2539 DCHECK(second.IsConstant());
2540
2541 Register out = locations->Out().AsRegister<Register>();
2542 Register dividend = locations->InAt(0).AsRegister<Register>();
2543 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2544
2545 int64_t magic;
2546 int shift;
2547 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2548
2549 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2550
2551 __ LoadConst32(TMP, magic);
2552 if (isR6) {
2553 __ MuhR6(TMP, dividend, TMP);
2554 } else {
2555 __ MultR2(dividend, TMP);
2556 __ Mfhi(TMP);
2557 }
2558 if (imm > 0 && magic < 0) {
2559 __ Addu(TMP, TMP, dividend);
2560 } else if (imm < 0 && magic > 0) {
2561 __ Subu(TMP, TMP, dividend);
2562 }
2563
2564 if (shift != 0) {
2565 __ Sra(TMP, TMP, shift);
2566 }
2567
2568 if (instruction->IsDiv()) {
2569 __ Sra(out, TMP, 31);
2570 __ Subu(out, TMP, out);
2571 } else {
2572 __ Sra(AT, TMP, 31);
2573 __ Subu(AT, TMP, AT);
2574 __ LoadConst32(TMP, imm);
2575 if (isR6) {
2576 __ MulR6(TMP, AT, TMP);
2577 } else {
2578 __ MulR2(TMP, AT, TMP);
2579 }
2580 __ Subu(out, dividend, TMP);
2581 }
2582}
2583
2584void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2585 DCHECK(instruction->IsDiv() || instruction->IsRem());
2586 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2587
2588 LocationSummary* locations = instruction->GetLocations();
2589 Register out = locations->Out().AsRegister<Register>();
2590 Location second = locations->InAt(1);
2591
2592 if (second.IsConstant()) {
2593 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2594 if (imm == 0) {
2595 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2596 } else if (imm == 1 || imm == -1) {
2597 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002598 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002599 DivRemByPowerOfTwo(instruction);
2600 } else {
2601 DCHECK(imm <= -2 || imm >= 2);
2602 GenerateDivRemWithAnyConstant(instruction);
2603 }
2604 } else {
2605 Register dividend = locations->InAt(0).AsRegister<Register>();
2606 Register divisor = second.AsRegister<Register>();
2607 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2608 if (instruction->IsDiv()) {
2609 if (isR6) {
2610 __ DivR6(out, dividend, divisor);
2611 } else {
2612 __ DivR2(out, dividend, divisor);
2613 }
2614 } else {
2615 if (isR6) {
2616 __ ModR6(out, dividend, divisor);
2617 } else {
2618 __ ModR2(out, dividend, divisor);
2619 }
2620 }
2621 }
2622}
2623
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2625 Primitive::Type type = div->GetResultType();
2626 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002627 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 : LocationSummary::kNoCall;
2629
2630 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2631
2632 switch (type) {
2633 case Primitive::kPrimInt:
2634 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002635 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2637 break;
2638
2639 case Primitive::kPrimLong: {
2640 InvokeRuntimeCallingConvention calling_convention;
2641 locations->SetInAt(0, Location::RegisterPairLocation(
2642 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2643 locations->SetInAt(1, Location::RegisterPairLocation(
2644 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2645 locations->SetOut(calling_convention.GetReturnLocation(type));
2646 break;
2647 }
2648
2649 case Primitive::kPrimFloat:
2650 case Primitive::kPrimDouble:
2651 locations->SetInAt(0, Location::RequiresFpuRegister());
2652 locations->SetInAt(1, Location::RequiresFpuRegister());
2653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2654 break;
2655
2656 default:
2657 LOG(FATAL) << "Unexpected div type " << type;
2658 }
2659}
2660
2661void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2662 Primitive::Type type = instruction->GetType();
2663 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664
2665 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002666 case Primitive::kPrimInt:
2667 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002670 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002671 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2672 break;
2673 }
2674 case Primitive::kPrimFloat:
2675 case Primitive::kPrimDouble: {
2676 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2677 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2678 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2679 if (type == Primitive::kPrimFloat) {
2680 __ DivS(dst, lhs, rhs);
2681 } else {
2682 __ DivD(dst, lhs, rhs);
2683 }
2684 break;
2685 }
2686 default:
2687 LOG(FATAL) << "Unexpected div type " << type;
2688 }
2689}
2690
2691void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002692 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002693 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002694}
2695
2696void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2697 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2698 codegen_->AddSlowPath(slow_path);
2699 Location value = instruction->GetLocations()->InAt(0);
2700 Primitive::Type type = instruction->GetType();
2701
2702 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002703 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 case Primitive::kPrimByte:
2705 case Primitive::kPrimChar:
2706 case Primitive::kPrimShort:
2707 case Primitive::kPrimInt: {
2708 if (value.IsConstant()) {
2709 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2710 __ B(slow_path->GetEntryLabel());
2711 } else {
2712 // A division by a non-null constant is valid. We don't need to perform
2713 // any check, so simply fall through.
2714 }
2715 } else {
2716 DCHECK(value.IsRegister()) << value;
2717 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2718 }
2719 break;
2720 }
2721 case Primitive::kPrimLong: {
2722 if (value.IsConstant()) {
2723 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2724 __ B(slow_path->GetEntryLabel());
2725 } else {
2726 // A division by a non-null constant is valid. We don't need to perform
2727 // any check, so simply fall through.
2728 }
2729 } else {
2730 DCHECK(value.IsRegisterPair()) << value;
2731 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2732 __ Beqz(TMP, slow_path->GetEntryLabel());
2733 }
2734 break;
2735 }
2736 default:
2737 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2738 }
2739}
2740
2741void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2744 locations->SetOut(Location::ConstantLocation(constant));
2745}
2746
2747void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2748 // Will be generated at use site.
2749}
2750
2751void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2752 exit->SetLocations(nullptr);
2753}
2754
2755void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2756}
2757
2758void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2761 locations->SetOut(Location::ConstantLocation(constant));
2762}
2763
2764void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2765 // Will be generated at use site.
2766}
2767
2768void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2769 got->SetLocations(nullptr);
2770}
2771
2772void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2773 DCHECK(!successor->IsExitBlock());
2774 HBasicBlock* block = got->GetBlock();
2775 HInstruction* previous = got->GetPrevious();
2776 HLoopInformation* info = block->GetLoopInformation();
2777
2778 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2779 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2780 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2781 return;
2782 }
2783 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2784 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2785 }
2786 if (!codegen_->GoesToNextBlock(block, successor)) {
2787 __ B(codegen_->GetLabelOf(successor));
2788 }
2789}
2790
2791void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2792 HandleGoto(got, got->GetSuccessor());
2793}
2794
2795void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2796 try_boundary->SetLocations(nullptr);
2797}
2798
2799void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2800 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2801 if (!successor->IsExitBlock()) {
2802 HandleGoto(try_boundary, successor);
2803 }
2804}
2805
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002806void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2807 LocationSummary* locations) {
2808 Register dst = locations->Out().AsRegister<Register>();
2809 Register lhs = locations->InAt(0).AsRegister<Register>();
2810 Location rhs_location = locations->InAt(1);
2811 Register rhs_reg = ZERO;
2812 int64_t rhs_imm = 0;
2813 bool use_imm = rhs_location.IsConstant();
2814 if (use_imm) {
2815 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2816 } else {
2817 rhs_reg = rhs_location.AsRegister<Register>();
2818 }
2819
2820 switch (cond) {
2821 case kCondEQ:
2822 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002823 if (use_imm && IsInt<16>(-rhs_imm)) {
2824 if (rhs_imm == 0) {
2825 if (cond == kCondEQ) {
2826 __ Sltiu(dst, lhs, 1);
2827 } else {
2828 __ Sltu(dst, ZERO, lhs);
2829 }
2830 } else {
2831 __ Addiu(dst, lhs, -rhs_imm);
2832 if (cond == kCondEQ) {
2833 __ Sltiu(dst, dst, 1);
2834 } else {
2835 __ Sltu(dst, ZERO, dst);
2836 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002837 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002838 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002839 if (use_imm && IsUint<16>(rhs_imm)) {
2840 __ Xori(dst, lhs, rhs_imm);
2841 } else {
2842 if (use_imm) {
2843 rhs_reg = TMP;
2844 __ LoadConst32(rhs_reg, rhs_imm);
2845 }
2846 __ Xor(dst, lhs, rhs_reg);
2847 }
2848 if (cond == kCondEQ) {
2849 __ Sltiu(dst, dst, 1);
2850 } else {
2851 __ Sltu(dst, ZERO, dst);
2852 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002853 }
2854 break;
2855
2856 case kCondLT:
2857 case kCondGE:
2858 if (use_imm && IsInt<16>(rhs_imm)) {
2859 __ Slti(dst, lhs, rhs_imm);
2860 } else {
2861 if (use_imm) {
2862 rhs_reg = TMP;
2863 __ LoadConst32(rhs_reg, rhs_imm);
2864 }
2865 __ Slt(dst, lhs, rhs_reg);
2866 }
2867 if (cond == kCondGE) {
2868 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2869 // only the slt instruction but no sge.
2870 __ Xori(dst, dst, 1);
2871 }
2872 break;
2873
2874 case kCondLE:
2875 case kCondGT:
2876 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2877 // Simulate lhs <= rhs via lhs < rhs + 1.
2878 __ Slti(dst, lhs, rhs_imm + 1);
2879 if (cond == kCondGT) {
2880 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2881 // only the slti instruction but no sgti.
2882 __ Xori(dst, dst, 1);
2883 }
2884 } else {
2885 if (use_imm) {
2886 rhs_reg = TMP;
2887 __ LoadConst32(rhs_reg, rhs_imm);
2888 }
2889 __ Slt(dst, rhs_reg, lhs);
2890 if (cond == kCondLE) {
2891 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2892 // only the slt instruction but no sle.
2893 __ Xori(dst, dst, 1);
2894 }
2895 }
2896 break;
2897
2898 case kCondB:
2899 case kCondAE:
2900 if (use_imm && IsInt<16>(rhs_imm)) {
2901 // Sltiu sign-extends its 16-bit immediate operand before
2902 // the comparison and thus lets us compare directly with
2903 // unsigned values in the ranges [0, 0x7fff] and
2904 // [0xffff8000, 0xffffffff].
2905 __ Sltiu(dst, lhs, rhs_imm);
2906 } else {
2907 if (use_imm) {
2908 rhs_reg = TMP;
2909 __ LoadConst32(rhs_reg, rhs_imm);
2910 }
2911 __ Sltu(dst, lhs, rhs_reg);
2912 }
2913 if (cond == kCondAE) {
2914 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2915 // only the sltu instruction but no sgeu.
2916 __ Xori(dst, dst, 1);
2917 }
2918 break;
2919
2920 case kCondBE:
2921 case kCondA:
2922 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2923 // Simulate lhs <= rhs via lhs < rhs + 1.
2924 // Note that this only works if rhs + 1 does not overflow
2925 // to 0, hence the check above.
2926 // Sltiu sign-extends its 16-bit immediate operand before
2927 // the comparison and thus lets us compare directly with
2928 // unsigned values in the ranges [0, 0x7fff] and
2929 // [0xffff8000, 0xffffffff].
2930 __ Sltiu(dst, lhs, rhs_imm + 1);
2931 if (cond == kCondA) {
2932 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2933 // only the sltiu instruction but no sgtiu.
2934 __ Xori(dst, dst, 1);
2935 }
2936 } else {
2937 if (use_imm) {
2938 rhs_reg = TMP;
2939 __ LoadConst32(rhs_reg, rhs_imm);
2940 }
2941 __ Sltu(dst, rhs_reg, lhs);
2942 if (cond == kCondBE) {
2943 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2944 // only the sltu instruction but no sleu.
2945 __ Xori(dst, dst, 1);
2946 }
2947 }
2948 break;
2949 }
2950}
2951
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002952bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2953 LocationSummary* input_locations,
2954 Register dst) {
2955 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2956 Location rhs_location = input_locations->InAt(1);
2957 Register rhs_reg = ZERO;
2958 int64_t rhs_imm = 0;
2959 bool use_imm = rhs_location.IsConstant();
2960 if (use_imm) {
2961 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2962 } else {
2963 rhs_reg = rhs_location.AsRegister<Register>();
2964 }
2965
2966 switch (cond) {
2967 case kCondEQ:
2968 case kCondNE:
2969 if (use_imm && IsInt<16>(-rhs_imm)) {
2970 __ Addiu(dst, lhs, -rhs_imm);
2971 } else if (use_imm && IsUint<16>(rhs_imm)) {
2972 __ Xori(dst, lhs, rhs_imm);
2973 } else {
2974 if (use_imm) {
2975 rhs_reg = TMP;
2976 __ LoadConst32(rhs_reg, rhs_imm);
2977 }
2978 __ Xor(dst, lhs, rhs_reg);
2979 }
2980 return (cond == kCondEQ);
2981
2982 case kCondLT:
2983 case kCondGE:
2984 if (use_imm && IsInt<16>(rhs_imm)) {
2985 __ Slti(dst, lhs, rhs_imm);
2986 } else {
2987 if (use_imm) {
2988 rhs_reg = TMP;
2989 __ LoadConst32(rhs_reg, rhs_imm);
2990 }
2991 __ Slt(dst, lhs, rhs_reg);
2992 }
2993 return (cond == kCondGE);
2994
2995 case kCondLE:
2996 case kCondGT:
2997 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2998 // Simulate lhs <= rhs via lhs < rhs + 1.
2999 __ Slti(dst, lhs, rhs_imm + 1);
3000 return (cond == kCondGT);
3001 } else {
3002 if (use_imm) {
3003 rhs_reg = TMP;
3004 __ LoadConst32(rhs_reg, rhs_imm);
3005 }
3006 __ Slt(dst, rhs_reg, lhs);
3007 return (cond == kCondLE);
3008 }
3009
3010 case kCondB:
3011 case kCondAE:
3012 if (use_imm && IsInt<16>(rhs_imm)) {
3013 // Sltiu sign-extends its 16-bit immediate operand before
3014 // the comparison and thus lets us compare directly with
3015 // unsigned values in the ranges [0, 0x7fff] and
3016 // [0xffff8000, 0xffffffff].
3017 __ Sltiu(dst, lhs, rhs_imm);
3018 } else {
3019 if (use_imm) {
3020 rhs_reg = TMP;
3021 __ LoadConst32(rhs_reg, rhs_imm);
3022 }
3023 __ Sltu(dst, lhs, rhs_reg);
3024 }
3025 return (cond == kCondAE);
3026
3027 case kCondBE:
3028 case kCondA:
3029 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3030 // Simulate lhs <= rhs via lhs < rhs + 1.
3031 // Note that this only works if rhs + 1 does not overflow
3032 // to 0, hence the check above.
3033 // Sltiu sign-extends its 16-bit immediate operand before
3034 // the comparison and thus lets us compare directly with
3035 // unsigned values in the ranges [0, 0x7fff] and
3036 // [0xffff8000, 0xffffffff].
3037 __ Sltiu(dst, lhs, rhs_imm + 1);
3038 return (cond == kCondA);
3039 } else {
3040 if (use_imm) {
3041 rhs_reg = TMP;
3042 __ LoadConst32(rhs_reg, rhs_imm);
3043 }
3044 __ Sltu(dst, rhs_reg, lhs);
3045 return (cond == kCondBE);
3046 }
3047 }
3048}
3049
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003050void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3051 LocationSummary* locations,
3052 MipsLabel* label) {
3053 Register lhs = locations->InAt(0).AsRegister<Register>();
3054 Location rhs_location = locations->InAt(1);
3055 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003056 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003057 bool use_imm = rhs_location.IsConstant();
3058 if (use_imm) {
3059 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3060 } else {
3061 rhs_reg = rhs_location.AsRegister<Register>();
3062 }
3063
3064 if (use_imm && rhs_imm == 0) {
3065 switch (cond) {
3066 case kCondEQ:
3067 case kCondBE: // <= 0 if zero
3068 __ Beqz(lhs, label);
3069 break;
3070 case kCondNE:
3071 case kCondA: // > 0 if non-zero
3072 __ Bnez(lhs, label);
3073 break;
3074 case kCondLT:
3075 __ Bltz(lhs, label);
3076 break;
3077 case kCondGE:
3078 __ Bgez(lhs, label);
3079 break;
3080 case kCondLE:
3081 __ Blez(lhs, label);
3082 break;
3083 case kCondGT:
3084 __ Bgtz(lhs, label);
3085 break;
3086 case kCondB: // always false
3087 break;
3088 case kCondAE: // always true
3089 __ B(label);
3090 break;
3091 }
3092 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003093 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3094 if (isR6 || !use_imm) {
3095 if (use_imm) {
3096 rhs_reg = TMP;
3097 __ LoadConst32(rhs_reg, rhs_imm);
3098 }
3099 switch (cond) {
3100 case kCondEQ:
3101 __ Beq(lhs, rhs_reg, label);
3102 break;
3103 case kCondNE:
3104 __ Bne(lhs, rhs_reg, label);
3105 break;
3106 case kCondLT:
3107 __ Blt(lhs, rhs_reg, label);
3108 break;
3109 case kCondGE:
3110 __ Bge(lhs, rhs_reg, label);
3111 break;
3112 case kCondLE:
3113 __ Bge(rhs_reg, lhs, label);
3114 break;
3115 case kCondGT:
3116 __ Blt(rhs_reg, lhs, label);
3117 break;
3118 case kCondB:
3119 __ Bltu(lhs, rhs_reg, label);
3120 break;
3121 case kCondAE:
3122 __ Bgeu(lhs, rhs_reg, label);
3123 break;
3124 case kCondBE:
3125 __ Bgeu(rhs_reg, lhs, label);
3126 break;
3127 case kCondA:
3128 __ Bltu(rhs_reg, lhs, label);
3129 break;
3130 }
3131 } else {
3132 // Special cases for more efficient comparison with constants on R2.
3133 switch (cond) {
3134 case kCondEQ:
3135 __ LoadConst32(TMP, rhs_imm);
3136 __ Beq(lhs, TMP, label);
3137 break;
3138 case kCondNE:
3139 __ LoadConst32(TMP, rhs_imm);
3140 __ Bne(lhs, TMP, label);
3141 break;
3142 case kCondLT:
3143 if (IsInt<16>(rhs_imm)) {
3144 __ Slti(TMP, lhs, rhs_imm);
3145 __ Bnez(TMP, label);
3146 } else {
3147 __ LoadConst32(TMP, rhs_imm);
3148 __ Blt(lhs, TMP, label);
3149 }
3150 break;
3151 case kCondGE:
3152 if (IsInt<16>(rhs_imm)) {
3153 __ Slti(TMP, lhs, rhs_imm);
3154 __ Beqz(TMP, label);
3155 } else {
3156 __ LoadConst32(TMP, rhs_imm);
3157 __ Bge(lhs, TMP, label);
3158 }
3159 break;
3160 case kCondLE:
3161 if (IsInt<16>(rhs_imm + 1)) {
3162 // Simulate lhs <= rhs via lhs < rhs + 1.
3163 __ Slti(TMP, lhs, rhs_imm + 1);
3164 __ Bnez(TMP, label);
3165 } else {
3166 __ LoadConst32(TMP, rhs_imm);
3167 __ Bge(TMP, lhs, label);
3168 }
3169 break;
3170 case kCondGT:
3171 if (IsInt<16>(rhs_imm + 1)) {
3172 // Simulate lhs > rhs via !(lhs < rhs + 1).
3173 __ Slti(TMP, lhs, rhs_imm + 1);
3174 __ Beqz(TMP, label);
3175 } else {
3176 __ LoadConst32(TMP, rhs_imm);
3177 __ Blt(TMP, lhs, label);
3178 }
3179 break;
3180 case kCondB:
3181 if (IsInt<16>(rhs_imm)) {
3182 __ Sltiu(TMP, lhs, rhs_imm);
3183 __ Bnez(TMP, label);
3184 } else {
3185 __ LoadConst32(TMP, rhs_imm);
3186 __ Bltu(lhs, TMP, label);
3187 }
3188 break;
3189 case kCondAE:
3190 if (IsInt<16>(rhs_imm)) {
3191 __ Sltiu(TMP, lhs, rhs_imm);
3192 __ Beqz(TMP, label);
3193 } else {
3194 __ LoadConst32(TMP, rhs_imm);
3195 __ Bgeu(lhs, TMP, label);
3196 }
3197 break;
3198 case kCondBE:
3199 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3200 // Simulate lhs <= rhs via lhs < rhs + 1.
3201 // Note that this only works if rhs + 1 does not overflow
3202 // to 0, hence the check above.
3203 __ Sltiu(TMP, lhs, rhs_imm + 1);
3204 __ Bnez(TMP, label);
3205 } else {
3206 __ LoadConst32(TMP, rhs_imm);
3207 __ Bgeu(TMP, lhs, label);
3208 }
3209 break;
3210 case kCondA:
3211 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3212 // Simulate lhs > rhs via !(lhs < rhs + 1).
3213 // Note that this only works if rhs + 1 does not overflow
3214 // to 0, hence the check above.
3215 __ Sltiu(TMP, lhs, rhs_imm + 1);
3216 __ Beqz(TMP, label);
3217 } else {
3218 __ LoadConst32(TMP, rhs_imm);
3219 __ Bltu(TMP, lhs, label);
3220 }
3221 break;
3222 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003223 }
3224 }
3225}
3226
3227void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3228 LocationSummary* locations,
3229 MipsLabel* label) {
3230 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3231 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3232 Location rhs_location = locations->InAt(1);
3233 Register rhs_high = ZERO;
3234 Register rhs_low = ZERO;
3235 int64_t imm = 0;
3236 uint32_t imm_high = 0;
3237 uint32_t imm_low = 0;
3238 bool use_imm = rhs_location.IsConstant();
3239 if (use_imm) {
3240 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3241 imm_high = High32Bits(imm);
3242 imm_low = Low32Bits(imm);
3243 } else {
3244 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3245 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3246 }
3247
3248 if (use_imm && imm == 0) {
3249 switch (cond) {
3250 case kCondEQ:
3251 case kCondBE: // <= 0 if zero
3252 __ Or(TMP, lhs_high, lhs_low);
3253 __ Beqz(TMP, label);
3254 break;
3255 case kCondNE:
3256 case kCondA: // > 0 if non-zero
3257 __ Or(TMP, lhs_high, lhs_low);
3258 __ Bnez(TMP, label);
3259 break;
3260 case kCondLT:
3261 __ Bltz(lhs_high, label);
3262 break;
3263 case kCondGE:
3264 __ Bgez(lhs_high, label);
3265 break;
3266 case kCondLE:
3267 __ Or(TMP, lhs_high, lhs_low);
3268 __ Sra(AT, lhs_high, 31);
3269 __ Bgeu(AT, TMP, label);
3270 break;
3271 case kCondGT:
3272 __ Or(TMP, lhs_high, lhs_low);
3273 __ Sra(AT, lhs_high, 31);
3274 __ Bltu(AT, TMP, label);
3275 break;
3276 case kCondB: // always false
3277 break;
3278 case kCondAE: // always true
3279 __ B(label);
3280 break;
3281 }
3282 } else if (use_imm) {
3283 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3284 switch (cond) {
3285 case kCondEQ:
3286 __ LoadConst32(TMP, imm_high);
3287 __ Xor(TMP, TMP, lhs_high);
3288 __ LoadConst32(AT, imm_low);
3289 __ Xor(AT, AT, lhs_low);
3290 __ Or(TMP, TMP, AT);
3291 __ Beqz(TMP, label);
3292 break;
3293 case kCondNE:
3294 __ LoadConst32(TMP, imm_high);
3295 __ Xor(TMP, TMP, lhs_high);
3296 __ LoadConst32(AT, imm_low);
3297 __ Xor(AT, AT, lhs_low);
3298 __ Or(TMP, TMP, AT);
3299 __ Bnez(TMP, label);
3300 break;
3301 case kCondLT:
3302 __ LoadConst32(TMP, imm_high);
3303 __ Blt(lhs_high, TMP, label);
3304 __ Slt(TMP, TMP, lhs_high);
3305 __ LoadConst32(AT, imm_low);
3306 __ Sltu(AT, lhs_low, AT);
3307 __ Blt(TMP, AT, label);
3308 break;
3309 case kCondGE:
3310 __ LoadConst32(TMP, imm_high);
3311 __ Blt(TMP, lhs_high, label);
3312 __ Slt(TMP, lhs_high, TMP);
3313 __ LoadConst32(AT, imm_low);
3314 __ Sltu(AT, lhs_low, AT);
3315 __ Or(TMP, TMP, AT);
3316 __ Beqz(TMP, label);
3317 break;
3318 case kCondLE:
3319 __ LoadConst32(TMP, imm_high);
3320 __ Blt(lhs_high, TMP, label);
3321 __ Slt(TMP, TMP, lhs_high);
3322 __ LoadConst32(AT, imm_low);
3323 __ Sltu(AT, AT, lhs_low);
3324 __ Or(TMP, TMP, AT);
3325 __ Beqz(TMP, label);
3326 break;
3327 case kCondGT:
3328 __ LoadConst32(TMP, imm_high);
3329 __ Blt(TMP, lhs_high, label);
3330 __ Slt(TMP, lhs_high, TMP);
3331 __ LoadConst32(AT, imm_low);
3332 __ Sltu(AT, AT, lhs_low);
3333 __ Blt(TMP, AT, label);
3334 break;
3335 case kCondB:
3336 __ LoadConst32(TMP, imm_high);
3337 __ Bltu(lhs_high, TMP, label);
3338 __ Sltu(TMP, TMP, lhs_high);
3339 __ LoadConst32(AT, imm_low);
3340 __ Sltu(AT, lhs_low, AT);
3341 __ Blt(TMP, AT, label);
3342 break;
3343 case kCondAE:
3344 __ LoadConst32(TMP, imm_high);
3345 __ Bltu(TMP, lhs_high, label);
3346 __ Sltu(TMP, lhs_high, TMP);
3347 __ LoadConst32(AT, imm_low);
3348 __ Sltu(AT, lhs_low, AT);
3349 __ Or(TMP, TMP, AT);
3350 __ Beqz(TMP, label);
3351 break;
3352 case kCondBE:
3353 __ LoadConst32(TMP, imm_high);
3354 __ Bltu(lhs_high, TMP, label);
3355 __ Sltu(TMP, TMP, lhs_high);
3356 __ LoadConst32(AT, imm_low);
3357 __ Sltu(AT, AT, lhs_low);
3358 __ Or(TMP, TMP, AT);
3359 __ Beqz(TMP, label);
3360 break;
3361 case kCondA:
3362 __ LoadConst32(TMP, imm_high);
3363 __ Bltu(TMP, lhs_high, label);
3364 __ Sltu(TMP, lhs_high, TMP);
3365 __ LoadConst32(AT, imm_low);
3366 __ Sltu(AT, AT, lhs_low);
3367 __ Blt(TMP, AT, label);
3368 break;
3369 }
3370 } else {
3371 switch (cond) {
3372 case kCondEQ:
3373 __ Xor(TMP, lhs_high, rhs_high);
3374 __ Xor(AT, lhs_low, rhs_low);
3375 __ Or(TMP, TMP, AT);
3376 __ Beqz(TMP, label);
3377 break;
3378 case kCondNE:
3379 __ Xor(TMP, lhs_high, rhs_high);
3380 __ Xor(AT, lhs_low, rhs_low);
3381 __ Or(TMP, TMP, AT);
3382 __ Bnez(TMP, label);
3383 break;
3384 case kCondLT:
3385 __ Blt(lhs_high, rhs_high, label);
3386 __ Slt(TMP, rhs_high, lhs_high);
3387 __ Sltu(AT, lhs_low, rhs_low);
3388 __ Blt(TMP, AT, label);
3389 break;
3390 case kCondGE:
3391 __ Blt(rhs_high, lhs_high, label);
3392 __ Slt(TMP, lhs_high, rhs_high);
3393 __ Sltu(AT, lhs_low, rhs_low);
3394 __ Or(TMP, TMP, AT);
3395 __ Beqz(TMP, label);
3396 break;
3397 case kCondLE:
3398 __ Blt(lhs_high, rhs_high, label);
3399 __ Slt(TMP, rhs_high, lhs_high);
3400 __ Sltu(AT, rhs_low, lhs_low);
3401 __ Or(TMP, TMP, AT);
3402 __ Beqz(TMP, label);
3403 break;
3404 case kCondGT:
3405 __ Blt(rhs_high, lhs_high, label);
3406 __ Slt(TMP, lhs_high, rhs_high);
3407 __ Sltu(AT, rhs_low, lhs_low);
3408 __ Blt(TMP, AT, label);
3409 break;
3410 case kCondB:
3411 __ Bltu(lhs_high, rhs_high, label);
3412 __ Sltu(TMP, rhs_high, lhs_high);
3413 __ Sltu(AT, lhs_low, rhs_low);
3414 __ Blt(TMP, AT, label);
3415 break;
3416 case kCondAE:
3417 __ Bltu(rhs_high, lhs_high, label);
3418 __ Sltu(TMP, lhs_high, rhs_high);
3419 __ Sltu(AT, lhs_low, rhs_low);
3420 __ Or(TMP, TMP, AT);
3421 __ Beqz(TMP, label);
3422 break;
3423 case kCondBE:
3424 __ Bltu(lhs_high, rhs_high, label);
3425 __ Sltu(TMP, rhs_high, lhs_high);
3426 __ Sltu(AT, rhs_low, lhs_low);
3427 __ Or(TMP, TMP, AT);
3428 __ Beqz(TMP, label);
3429 break;
3430 case kCondA:
3431 __ Bltu(rhs_high, lhs_high, label);
3432 __ Sltu(TMP, lhs_high, rhs_high);
3433 __ Sltu(AT, rhs_low, lhs_low);
3434 __ Blt(TMP, AT, label);
3435 break;
3436 }
3437 }
3438}
3439
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003440void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3441 bool gt_bias,
3442 Primitive::Type type,
3443 LocationSummary* locations) {
3444 Register dst = locations->Out().AsRegister<Register>();
3445 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3446 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3447 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3448 if (type == Primitive::kPrimFloat) {
3449 if (isR6) {
3450 switch (cond) {
3451 case kCondEQ:
3452 __ CmpEqS(FTMP, lhs, rhs);
3453 __ Mfc1(dst, FTMP);
3454 __ Andi(dst, dst, 1);
3455 break;
3456 case kCondNE:
3457 __ CmpEqS(FTMP, lhs, rhs);
3458 __ Mfc1(dst, FTMP);
3459 __ Addiu(dst, dst, 1);
3460 break;
3461 case kCondLT:
3462 if (gt_bias) {
3463 __ CmpLtS(FTMP, lhs, rhs);
3464 } else {
3465 __ CmpUltS(FTMP, lhs, rhs);
3466 }
3467 __ Mfc1(dst, FTMP);
3468 __ Andi(dst, dst, 1);
3469 break;
3470 case kCondLE:
3471 if (gt_bias) {
3472 __ CmpLeS(FTMP, lhs, rhs);
3473 } else {
3474 __ CmpUleS(FTMP, lhs, rhs);
3475 }
3476 __ Mfc1(dst, FTMP);
3477 __ Andi(dst, dst, 1);
3478 break;
3479 case kCondGT:
3480 if (gt_bias) {
3481 __ CmpUltS(FTMP, rhs, lhs);
3482 } else {
3483 __ CmpLtS(FTMP, rhs, lhs);
3484 }
3485 __ Mfc1(dst, FTMP);
3486 __ Andi(dst, dst, 1);
3487 break;
3488 case kCondGE:
3489 if (gt_bias) {
3490 __ CmpUleS(FTMP, rhs, lhs);
3491 } else {
3492 __ CmpLeS(FTMP, rhs, lhs);
3493 }
3494 __ Mfc1(dst, FTMP);
3495 __ Andi(dst, dst, 1);
3496 break;
3497 default:
3498 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3499 UNREACHABLE();
3500 }
3501 } else {
3502 switch (cond) {
3503 case kCondEQ:
3504 __ CeqS(0, lhs, rhs);
3505 __ LoadConst32(dst, 1);
3506 __ Movf(dst, ZERO, 0);
3507 break;
3508 case kCondNE:
3509 __ CeqS(0, lhs, rhs);
3510 __ LoadConst32(dst, 1);
3511 __ Movt(dst, ZERO, 0);
3512 break;
3513 case kCondLT:
3514 if (gt_bias) {
3515 __ ColtS(0, lhs, rhs);
3516 } else {
3517 __ CultS(0, lhs, rhs);
3518 }
3519 __ LoadConst32(dst, 1);
3520 __ Movf(dst, ZERO, 0);
3521 break;
3522 case kCondLE:
3523 if (gt_bias) {
3524 __ ColeS(0, lhs, rhs);
3525 } else {
3526 __ CuleS(0, lhs, rhs);
3527 }
3528 __ LoadConst32(dst, 1);
3529 __ Movf(dst, ZERO, 0);
3530 break;
3531 case kCondGT:
3532 if (gt_bias) {
3533 __ CultS(0, rhs, lhs);
3534 } else {
3535 __ ColtS(0, rhs, lhs);
3536 }
3537 __ LoadConst32(dst, 1);
3538 __ Movf(dst, ZERO, 0);
3539 break;
3540 case kCondGE:
3541 if (gt_bias) {
3542 __ CuleS(0, rhs, lhs);
3543 } else {
3544 __ ColeS(0, rhs, lhs);
3545 }
3546 __ LoadConst32(dst, 1);
3547 __ Movf(dst, ZERO, 0);
3548 break;
3549 default:
3550 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3551 UNREACHABLE();
3552 }
3553 }
3554 } else {
3555 DCHECK_EQ(type, Primitive::kPrimDouble);
3556 if (isR6) {
3557 switch (cond) {
3558 case kCondEQ:
3559 __ CmpEqD(FTMP, lhs, rhs);
3560 __ Mfc1(dst, FTMP);
3561 __ Andi(dst, dst, 1);
3562 break;
3563 case kCondNE:
3564 __ CmpEqD(FTMP, lhs, rhs);
3565 __ Mfc1(dst, FTMP);
3566 __ Addiu(dst, dst, 1);
3567 break;
3568 case kCondLT:
3569 if (gt_bias) {
3570 __ CmpLtD(FTMP, lhs, rhs);
3571 } else {
3572 __ CmpUltD(FTMP, lhs, rhs);
3573 }
3574 __ Mfc1(dst, FTMP);
3575 __ Andi(dst, dst, 1);
3576 break;
3577 case kCondLE:
3578 if (gt_bias) {
3579 __ CmpLeD(FTMP, lhs, rhs);
3580 } else {
3581 __ CmpUleD(FTMP, lhs, rhs);
3582 }
3583 __ Mfc1(dst, FTMP);
3584 __ Andi(dst, dst, 1);
3585 break;
3586 case kCondGT:
3587 if (gt_bias) {
3588 __ CmpUltD(FTMP, rhs, lhs);
3589 } else {
3590 __ CmpLtD(FTMP, rhs, lhs);
3591 }
3592 __ Mfc1(dst, FTMP);
3593 __ Andi(dst, dst, 1);
3594 break;
3595 case kCondGE:
3596 if (gt_bias) {
3597 __ CmpUleD(FTMP, rhs, lhs);
3598 } else {
3599 __ CmpLeD(FTMP, rhs, lhs);
3600 }
3601 __ Mfc1(dst, FTMP);
3602 __ Andi(dst, dst, 1);
3603 break;
3604 default:
3605 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3606 UNREACHABLE();
3607 }
3608 } else {
3609 switch (cond) {
3610 case kCondEQ:
3611 __ CeqD(0, lhs, rhs);
3612 __ LoadConst32(dst, 1);
3613 __ Movf(dst, ZERO, 0);
3614 break;
3615 case kCondNE:
3616 __ CeqD(0, lhs, rhs);
3617 __ LoadConst32(dst, 1);
3618 __ Movt(dst, ZERO, 0);
3619 break;
3620 case kCondLT:
3621 if (gt_bias) {
3622 __ ColtD(0, lhs, rhs);
3623 } else {
3624 __ CultD(0, lhs, rhs);
3625 }
3626 __ LoadConst32(dst, 1);
3627 __ Movf(dst, ZERO, 0);
3628 break;
3629 case kCondLE:
3630 if (gt_bias) {
3631 __ ColeD(0, lhs, rhs);
3632 } else {
3633 __ CuleD(0, lhs, rhs);
3634 }
3635 __ LoadConst32(dst, 1);
3636 __ Movf(dst, ZERO, 0);
3637 break;
3638 case kCondGT:
3639 if (gt_bias) {
3640 __ CultD(0, rhs, lhs);
3641 } else {
3642 __ ColtD(0, rhs, lhs);
3643 }
3644 __ LoadConst32(dst, 1);
3645 __ Movf(dst, ZERO, 0);
3646 break;
3647 case kCondGE:
3648 if (gt_bias) {
3649 __ CuleD(0, rhs, lhs);
3650 } else {
3651 __ ColeD(0, rhs, lhs);
3652 }
3653 __ LoadConst32(dst, 1);
3654 __ Movf(dst, ZERO, 0);
3655 break;
3656 default:
3657 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3658 UNREACHABLE();
3659 }
3660 }
3661 }
3662}
3663
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003664bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3665 bool gt_bias,
3666 Primitive::Type type,
3667 LocationSummary* input_locations,
3668 int cc) {
3669 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3670 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3671 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3672 if (type == Primitive::kPrimFloat) {
3673 switch (cond) {
3674 case kCondEQ:
3675 __ CeqS(cc, lhs, rhs);
3676 return false;
3677 case kCondNE:
3678 __ CeqS(cc, lhs, rhs);
3679 return true;
3680 case kCondLT:
3681 if (gt_bias) {
3682 __ ColtS(cc, lhs, rhs);
3683 } else {
3684 __ CultS(cc, lhs, rhs);
3685 }
3686 return false;
3687 case kCondLE:
3688 if (gt_bias) {
3689 __ ColeS(cc, lhs, rhs);
3690 } else {
3691 __ CuleS(cc, lhs, rhs);
3692 }
3693 return false;
3694 case kCondGT:
3695 if (gt_bias) {
3696 __ CultS(cc, rhs, lhs);
3697 } else {
3698 __ ColtS(cc, rhs, lhs);
3699 }
3700 return false;
3701 case kCondGE:
3702 if (gt_bias) {
3703 __ CuleS(cc, rhs, lhs);
3704 } else {
3705 __ ColeS(cc, rhs, lhs);
3706 }
3707 return false;
3708 default:
3709 LOG(FATAL) << "Unexpected non-floating-point condition";
3710 UNREACHABLE();
3711 }
3712 } else {
3713 DCHECK_EQ(type, Primitive::kPrimDouble);
3714 switch (cond) {
3715 case kCondEQ:
3716 __ CeqD(cc, lhs, rhs);
3717 return false;
3718 case kCondNE:
3719 __ CeqD(cc, lhs, rhs);
3720 return true;
3721 case kCondLT:
3722 if (gt_bias) {
3723 __ ColtD(cc, lhs, rhs);
3724 } else {
3725 __ CultD(cc, lhs, rhs);
3726 }
3727 return false;
3728 case kCondLE:
3729 if (gt_bias) {
3730 __ ColeD(cc, lhs, rhs);
3731 } else {
3732 __ CuleD(cc, lhs, rhs);
3733 }
3734 return false;
3735 case kCondGT:
3736 if (gt_bias) {
3737 __ CultD(cc, rhs, lhs);
3738 } else {
3739 __ ColtD(cc, rhs, lhs);
3740 }
3741 return false;
3742 case kCondGE:
3743 if (gt_bias) {
3744 __ CuleD(cc, rhs, lhs);
3745 } else {
3746 __ ColeD(cc, rhs, lhs);
3747 }
3748 return false;
3749 default:
3750 LOG(FATAL) << "Unexpected non-floating-point condition";
3751 UNREACHABLE();
3752 }
3753 }
3754}
3755
3756bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3757 bool gt_bias,
3758 Primitive::Type type,
3759 LocationSummary* input_locations,
3760 FRegister dst) {
3761 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3762 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3763 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3764 if (type == Primitive::kPrimFloat) {
3765 switch (cond) {
3766 case kCondEQ:
3767 __ CmpEqS(dst, lhs, rhs);
3768 return false;
3769 case kCondNE:
3770 __ CmpEqS(dst, lhs, rhs);
3771 return true;
3772 case kCondLT:
3773 if (gt_bias) {
3774 __ CmpLtS(dst, lhs, rhs);
3775 } else {
3776 __ CmpUltS(dst, lhs, rhs);
3777 }
3778 return false;
3779 case kCondLE:
3780 if (gt_bias) {
3781 __ CmpLeS(dst, lhs, rhs);
3782 } else {
3783 __ CmpUleS(dst, lhs, rhs);
3784 }
3785 return false;
3786 case kCondGT:
3787 if (gt_bias) {
3788 __ CmpUltS(dst, rhs, lhs);
3789 } else {
3790 __ CmpLtS(dst, rhs, lhs);
3791 }
3792 return false;
3793 case kCondGE:
3794 if (gt_bias) {
3795 __ CmpUleS(dst, rhs, lhs);
3796 } else {
3797 __ CmpLeS(dst, rhs, lhs);
3798 }
3799 return false;
3800 default:
3801 LOG(FATAL) << "Unexpected non-floating-point condition";
3802 UNREACHABLE();
3803 }
3804 } else {
3805 DCHECK_EQ(type, Primitive::kPrimDouble);
3806 switch (cond) {
3807 case kCondEQ:
3808 __ CmpEqD(dst, lhs, rhs);
3809 return false;
3810 case kCondNE:
3811 __ CmpEqD(dst, lhs, rhs);
3812 return true;
3813 case kCondLT:
3814 if (gt_bias) {
3815 __ CmpLtD(dst, lhs, rhs);
3816 } else {
3817 __ CmpUltD(dst, lhs, rhs);
3818 }
3819 return false;
3820 case kCondLE:
3821 if (gt_bias) {
3822 __ CmpLeD(dst, lhs, rhs);
3823 } else {
3824 __ CmpUleD(dst, lhs, rhs);
3825 }
3826 return false;
3827 case kCondGT:
3828 if (gt_bias) {
3829 __ CmpUltD(dst, rhs, lhs);
3830 } else {
3831 __ CmpLtD(dst, rhs, lhs);
3832 }
3833 return false;
3834 case kCondGE:
3835 if (gt_bias) {
3836 __ CmpUleD(dst, rhs, lhs);
3837 } else {
3838 __ CmpLeD(dst, rhs, lhs);
3839 }
3840 return false;
3841 default:
3842 LOG(FATAL) << "Unexpected non-floating-point condition";
3843 UNREACHABLE();
3844 }
3845 }
3846}
3847
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003848void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3849 bool gt_bias,
3850 Primitive::Type type,
3851 LocationSummary* locations,
3852 MipsLabel* label) {
3853 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3854 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3855 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3856 if (type == Primitive::kPrimFloat) {
3857 if (isR6) {
3858 switch (cond) {
3859 case kCondEQ:
3860 __ CmpEqS(FTMP, lhs, rhs);
3861 __ Bc1nez(FTMP, label);
3862 break;
3863 case kCondNE:
3864 __ CmpEqS(FTMP, lhs, rhs);
3865 __ Bc1eqz(FTMP, label);
3866 break;
3867 case kCondLT:
3868 if (gt_bias) {
3869 __ CmpLtS(FTMP, lhs, rhs);
3870 } else {
3871 __ CmpUltS(FTMP, lhs, rhs);
3872 }
3873 __ Bc1nez(FTMP, label);
3874 break;
3875 case kCondLE:
3876 if (gt_bias) {
3877 __ CmpLeS(FTMP, lhs, rhs);
3878 } else {
3879 __ CmpUleS(FTMP, lhs, rhs);
3880 }
3881 __ Bc1nez(FTMP, label);
3882 break;
3883 case kCondGT:
3884 if (gt_bias) {
3885 __ CmpUltS(FTMP, rhs, lhs);
3886 } else {
3887 __ CmpLtS(FTMP, rhs, lhs);
3888 }
3889 __ Bc1nez(FTMP, label);
3890 break;
3891 case kCondGE:
3892 if (gt_bias) {
3893 __ CmpUleS(FTMP, rhs, lhs);
3894 } else {
3895 __ CmpLeS(FTMP, rhs, lhs);
3896 }
3897 __ Bc1nez(FTMP, label);
3898 break;
3899 default:
3900 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003901 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003902 }
3903 } else {
3904 switch (cond) {
3905 case kCondEQ:
3906 __ CeqS(0, lhs, rhs);
3907 __ Bc1t(0, label);
3908 break;
3909 case kCondNE:
3910 __ CeqS(0, lhs, rhs);
3911 __ Bc1f(0, label);
3912 break;
3913 case kCondLT:
3914 if (gt_bias) {
3915 __ ColtS(0, lhs, rhs);
3916 } else {
3917 __ CultS(0, lhs, rhs);
3918 }
3919 __ Bc1t(0, label);
3920 break;
3921 case kCondLE:
3922 if (gt_bias) {
3923 __ ColeS(0, lhs, rhs);
3924 } else {
3925 __ CuleS(0, lhs, rhs);
3926 }
3927 __ Bc1t(0, label);
3928 break;
3929 case kCondGT:
3930 if (gt_bias) {
3931 __ CultS(0, rhs, lhs);
3932 } else {
3933 __ ColtS(0, rhs, lhs);
3934 }
3935 __ Bc1t(0, label);
3936 break;
3937 case kCondGE:
3938 if (gt_bias) {
3939 __ CuleS(0, rhs, lhs);
3940 } else {
3941 __ ColeS(0, rhs, lhs);
3942 }
3943 __ Bc1t(0, label);
3944 break;
3945 default:
3946 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003947 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003948 }
3949 }
3950 } else {
3951 DCHECK_EQ(type, Primitive::kPrimDouble);
3952 if (isR6) {
3953 switch (cond) {
3954 case kCondEQ:
3955 __ CmpEqD(FTMP, lhs, rhs);
3956 __ Bc1nez(FTMP, label);
3957 break;
3958 case kCondNE:
3959 __ CmpEqD(FTMP, lhs, rhs);
3960 __ Bc1eqz(FTMP, label);
3961 break;
3962 case kCondLT:
3963 if (gt_bias) {
3964 __ CmpLtD(FTMP, lhs, rhs);
3965 } else {
3966 __ CmpUltD(FTMP, lhs, rhs);
3967 }
3968 __ Bc1nez(FTMP, label);
3969 break;
3970 case kCondLE:
3971 if (gt_bias) {
3972 __ CmpLeD(FTMP, lhs, rhs);
3973 } else {
3974 __ CmpUleD(FTMP, lhs, rhs);
3975 }
3976 __ Bc1nez(FTMP, label);
3977 break;
3978 case kCondGT:
3979 if (gt_bias) {
3980 __ CmpUltD(FTMP, rhs, lhs);
3981 } else {
3982 __ CmpLtD(FTMP, rhs, lhs);
3983 }
3984 __ Bc1nez(FTMP, label);
3985 break;
3986 case kCondGE:
3987 if (gt_bias) {
3988 __ CmpUleD(FTMP, rhs, lhs);
3989 } else {
3990 __ CmpLeD(FTMP, rhs, lhs);
3991 }
3992 __ Bc1nez(FTMP, label);
3993 break;
3994 default:
3995 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003996 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003997 }
3998 } else {
3999 switch (cond) {
4000 case kCondEQ:
4001 __ CeqD(0, lhs, rhs);
4002 __ Bc1t(0, label);
4003 break;
4004 case kCondNE:
4005 __ CeqD(0, lhs, rhs);
4006 __ Bc1f(0, label);
4007 break;
4008 case kCondLT:
4009 if (gt_bias) {
4010 __ ColtD(0, lhs, rhs);
4011 } else {
4012 __ CultD(0, lhs, rhs);
4013 }
4014 __ Bc1t(0, label);
4015 break;
4016 case kCondLE:
4017 if (gt_bias) {
4018 __ ColeD(0, lhs, rhs);
4019 } else {
4020 __ CuleD(0, lhs, rhs);
4021 }
4022 __ Bc1t(0, label);
4023 break;
4024 case kCondGT:
4025 if (gt_bias) {
4026 __ CultD(0, rhs, lhs);
4027 } else {
4028 __ ColtD(0, rhs, lhs);
4029 }
4030 __ Bc1t(0, label);
4031 break;
4032 case kCondGE:
4033 if (gt_bias) {
4034 __ CuleD(0, rhs, lhs);
4035 } else {
4036 __ ColeD(0, rhs, lhs);
4037 }
4038 __ Bc1t(0, label);
4039 break;
4040 default:
4041 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004042 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004043 }
4044 }
4045 }
4046}
4047
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004048void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004049 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004050 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004051 MipsLabel* false_target) {
4052 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004053
David Brazdil0debae72015-11-12 18:37:00 +00004054 if (true_target == nullptr && false_target == nullptr) {
4055 // Nothing to do. The code always falls through.
4056 return;
4057 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004058 // Constant condition, statically compared against "true" (integer value 1).
4059 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004060 if (true_target != nullptr) {
4061 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004063 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004064 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004065 if (false_target != nullptr) {
4066 __ B(false_target);
4067 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004068 }
David Brazdil0debae72015-11-12 18:37:00 +00004069 return;
4070 }
4071
4072 // The following code generates these patterns:
4073 // (1) true_target == nullptr && false_target != nullptr
4074 // - opposite condition true => branch to false_target
4075 // (2) true_target != nullptr && false_target == nullptr
4076 // - condition true => branch to true_target
4077 // (3) true_target != nullptr && false_target != nullptr
4078 // - condition true => branch to true_target
4079 // - branch to false_target
4080 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004081 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004082 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004083 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004084 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004085 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4086 } else {
4087 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4088 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004089 } else {
4090 // The condition instruction has not been materialized, use its inputs as
4091 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004092 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004093 Primitive::Type type = condition->InputAt(0)->GetType();
4094 LocationSummary* locations = cond->GetLocations();
4095 IfCondition if_cond = condition->GetCondition();
4096 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004097
David Brazdil0debae72015-11-12 18:37:00 +00004098 if (true_target == nullptr) {
4099 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004100 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004101 }
4102
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004103 switch (type) {
4104 default:
4105 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4106 break;
4107 case Primitive::kPrimLong:
4108 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4109 break;
4110 case Primitive::kPrimFloat:
4111 case Primitive::kPrimDouble:
4112 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4113 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004114 }
4115 }
David Brazdil0debae72015-11-12 18:37:00 +00004116
4117 // If neither branch falls through (case 3), the conditional branch to `true_target`
4118 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4119 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120 __ B(false_target);
4121 }
4122}
4123
4124void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4125 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004126 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004127 locations->SetInAt(0, Location::RequiresRegister());
4128 }
4129}
4130
4131void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004132 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4133 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4134 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4135 nullptr : codegen_->GetLabelOf(true_successor);
4136 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4137 nullptr : codegen_->GetLabelOf(false_successor);
4138 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139}
4140
4141void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4142 LocationSummary* locations = new (GetGraph()->GetArena())
4143 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004144 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004145 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004146 locations->SetInAt(0, Location::RequiresRegister());
4147 }
4148}
4149
4150void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004151 SlowPathCodeMIPS* slow_path =
4152 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004153 GenerateTestAndBranch(deoptimize,
4154 /* condition_input_index */ 0,
4155 slow_path->GetEntryLabel(),
4156 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004157}
4158
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004159// This function returns true if a conditional move can be generated for HSelect.
4160// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4161// branches and regular moves.
4162//
4163// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4164//
4165// While determining feasibility of a conditional move and setting inputs/outputs
4166// are two distinct tasks, this function does both because they share quite a bit
4167// of common logic.
4168static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4169 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4170 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4171 HCondition* condition = cond->AsCondition();
4172
4173 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4174 Primitive::Type dst_type = select->GetType();
4175
4176 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4177 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4178 bool is_true_value_zero_constant =
4179 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4180 bool is_false_value_zero_constant =
4181 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4182
4183 bool can_move_conditionally = false;
4184 bool use_const_for_false_in = false;
4185 bool use_const_for_true_in = false;
4186
4187 if (!cond->IsConstant()) {
4188 switch (cond_type) {
4189 default:
4190 switch (dst_type) {
4191 default:
4192 // Moving int on int condition.
4193 if (is_r6) {
4194 if (is_true_value_zero_constant) {
4195 // seleqz out_reg, false_reg, cond_reg
4196 can_move_conditionally = true;
4197 use_const_for_true_in = true;
4198 } else if (is_false_value_zero_constant) {
4199 // selnez out_reg, true_reg, cond_reg
4200 can_move_conditionally = true;
4201 use_const_for_false_in = true;
4202 } else if (materialized) {
4203 // Not materializing unmaterialized int conditions
4204 // to keep the instruction count low.
4205 // selnez AT, true_reg, cond_reg
4206 // seleqz TMP, false_reg, cond_reg
4207 // or out_reg, AT, TMP
4208 can_move_conditionally = true;
4209 }
4210 } else {
4211 // movn out_reg, true_reg/ZERO, cond_reg
4212 can_move_conditionally = true;
4213 use_const_for_true_in = is_true_value_zero_constant;
4214 }
4215 break;
4216 case Primitive::kPrimLong:
4217 // Moving long on int condition.
4218 if (is_r6) {
4219 if (is_true_value_zero_constant) {
4220 // seleqz out_reg_lo, false_reg_lo, cond_reg
4221 // seleqz out_reg_hi, false_reg_hi, cond_reg
4222 can_move_conditionally = true;
4223 use_const_for_true_in = true;
4224 } else if (is_false_value_zero_constant) {
4225 // selnez out_reg_lo, true_reg_lo, cond_reg
4226 // selnez out_reg_hi, true_reg_hi, cond_reg
4227 can_move_conditionally = true;
4228 use_const_for_false_in = true;
4229 }
4230 // Other long conditional moves would generate 6+ instructions,
4231 // which is too many.
4232 } else {
4233 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4234 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4235 can_move_conditionally = true;
4236 use_const_for_true_in = is_true_value_zero_constant;
4237 }
4238 break;
4239 case Primitive::kPrimFloat:
4240 case Primitive::kPrimDouble:
4241 // Moving float/double on int condition.
4242 if (is_r6) {
4243 if (materialized) {
4244 // Not materializing unmaterialized int conditions
4245 // to keep the instruction count low.
4246 can_move_conditionally = true;
4247 if (is_true_value_zero_constant) {
4248 // sltu TMP, ZERO, cond_reg
4249 // mtc1 TMP, temp_cond_reg
4250 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4251 use_const_for_true_in = true;
4252 } else if (is_false_value_zero_constant) {
4253 // sltu TMP, ZERO, cond_reg
4254 // mtc1 TMP, temp_cond_reg
4255 // selnez.fmt out_reg, true_reg, temp_cond_reg
4256 use_const_for_false_in = true;
4257 } else {
4258 // sltu TMP, ZERO, cond_reg
4259 // mtc1 TMP, temp_cond_reg
4260 // sel.fmt temp_cond_reg, false_reg, true_reg
4261 // mov.fmt out_reg, temp_cond_reg
4262 }
4263 }
4264 } else {
4265 // movn.fmt out_reg, true_reg, cond_reg
4266 can_move_conditionally = true;
4267 }
4268 break;
4269 }
4270 break;
4271 case Primitive::kPrimLong:
4272 // We don't materialize long comparison now
4273 // and use conditional branches instead.
4274 break;
4275 case Primitive::kPrimFloat:
4276 case Primitive::kPrimDouble:
4277 switch (dst_type) {
4278 default:
4279 // Moving int on float/double condition.
4280 if (is_r6) {
4281 if (is_true_value_zero_constant) {
4282 // mfc1 TMP, temp_cond_reg
4283 // seleqz out_reg, false_reg, TMP
4284 can_move_conditionally = true;
4285 use_const_for_true_in = true;
4286 } else if (is_false_value_zero_constant) {
4287 // mfc1 TMP, temp_cond_reg
4288 // selnez out_reg, true_reg, TMP
4289 can_move_conditionally = true;
4290 use_const_for_false_in = true;
4291 } else {
4292 // mfc1 TMP, temp_cond_reg
4293 // selnez AT, true_reg, TMP
4294 // seleqz TMP, false_reg, TMP
4295 // or out_reg, AT, TMP
4296 can_move_conditionally = true;
4297 }
4298 } else {
4299 // movt out_reg, true_reg/ZERO, cc
4300 can_move_conditionally = true;
4301 use_const_for_true_in = is_true_value_zero_constant;
4302 }
4303 break;
4304 case Primitive::kPrimLong:
4305 // Moving long on float/double condition.
4306 if (is_r6) {
4307 if (is_true_value_zero_constant) {
4308 // mfc1 TMP, temp_cond_reg
4309 // seleqz out_reg_lo, false_reg_lo, TMP
4310 // seleqz out_reg_hi, false_reg_hi, TMP
4311 can_move_conditionally = true;
4312 use_const_for_true_in = true;
4313 } else if (is_false_value_zero_constant) {
4314 // mfc1 TMP, temp_cond_reg
4315 // selnez out_reg_lo, true_reg_lo, TMP
4316 // selnez out_reg_hi, true_reg_hi, TMP
4317 can_move_conditionally = true;
4318 use_const_for_false_in = true;
4319 }
4320 // Other long conditional moves would generate 6+ instructions,
4321 // which is too many.
4322 } else {
4323 // movt out_reg_lo, true_reg_lo/ZERO, cc
4324 // movt out_reg_hi, true_reg_hi/ZERO, cc
4325 can_move_conditionally = true;
4326 use_const_for_true_in = is_true_value_zero_constant;
4327 }
4328 break;
4329 case Primitive::kPrimFloat:
4330 case Primitive::kPrimDouble:
4331 // Moving float/double on float/double condition.
4332 if (is_r6) {
4333 can_move_conditionally = true;
4334 if (is_true_value_zero_constant) {
4335 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4336 use_const_for_true_in = true;
4337 } else if (is_false_value_zero_constant) {
4338 // selnez.fmt out_reg, true_reg, temp_cond_reg
4339 use_const_for_false_in = true;
4340 } else {
4341 // sel.fmt temp_cond_reg, false_reg, true_reg
4342 // mov.fmt out_reg, temp_cond_reg
4343 }
4344 } else {
4345 // movt.fmt out_reg, true_reg, cc
4346 can_move_conditionally = true;
4347 }
4348 break;
4349 }
4350 break;
4351 }
4352 }
4353
4354 if (can_move_conditionally) {
4355 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4356 } else {
4357 DCHECK(!use_const_for_false_in);
4358 DCHECK(!use_const_for_true_in);
4359 }
4360
4361 if (locations_to_set != nullptr) {
4362 if (use_const_for_false_in) {
4363 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4364 } else {
4365 locations_to_set->SetInAt(0,
4366 Primitive::IsFloatingPointType(dst_type)
4367 ? Location::RequiresFpuRegister()
4368 : Location::RequiresRegister());
4369 }
4370 if (use_const_for_true_in) {
4371 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4372 } else {
4373 locations_to_set->SetInAt(1,
4374 Primitive::IsFloatingPointType(dst_type)
4375 ? Location::RequiresFpuRegister()
4376 : Location::RequiresRegister());
4377 }
4378 if (materialized) {
4379 locations_to_set->SetInAt(2, Location::RequiresRegister());
4380 }
4381 // On R6 we don't require the output to be the same as the
4382 // first input for conditional moves unlike on R2.
4383 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4384 if (is_out_same_as_first_in) {
4385 locations_to_set->SetOut(Location::SameAsFirstInput());
4386 } else {
4387 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4388 ? Location::RequiresFpuRegister()
4389 : Location::RequiresRegister());
4390 }
4391 }
4392
4393 return can_move_conditionally;
4394}
4395
4396void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4397 LocationSummary* locations = select->GetLocations();
4398 Location dst = locations->Out();
4399 Location src = locations->InAt(1);
4400 Register src_reg = ZERO;
4401 Register src_reg_high = ZERO;
4402 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4403 Register cond_reg = TMP;
4404 int cond_cc = 0;
4405 Primitive::Type cond_type = Primitive::kPrimInt;
4406 bool cond_inverted = false;
4407 Primitive::Type dst_type = select->GetType();
4408
4409 if (IsBooleanValueOrMaterializedCondition(cond)) {
4410 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4411 } else {
4412 HCondition* condition = cond->AsCondition();
4413 LocationSummary* cond_locations = cond->GetLocations();
4414 IfCondition if_cond = condition->GetCondition();
4415 cond_type = condition->InputAt(0)->GetType();
4416 switch (cond_type) {
4417 default:
4418 DCHECK_NE(cond_type, Primitive::kPrimLong);
4419 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4420 break;
4421 case Primitive::kPrimFloat:
4422 case Primitive::kPrimDouble:
4423 cond_inverted = MaterializeFpCompareR2(if_cond,
4424 condition->IsGtBias(),
4425 cond_type,
4426 cond_locations,
4427 cond_cc);
4428 break;
4429 }
4430 }
4431
4432 DCHECK(dst.Equals(locations->InAt(0)));
4433 if (src.IsRegister()) {
4434 src_reg = src.AsRegister<Register>();
4435 } else if (src.IsRegisterPair()) {
4436 src_reg = src.AsRegisterPairLow<Register>();
4437 src_reg_high = src.AsRegisterPairHigh<Register>();
4438 } else if (src.IsConstant()) {
4439 DCHECK(src.GetConstant()->IsZeroBitPattern());
4440 }
4441
4442 switch (cond_type) {
4443 default:
4444 switch (dst_type) {
4445 default:
4446 if (cond_inverted) {
4447 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4448 } else {
4449 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4450 }
4451 break;
4452 case Primitive::kPrimLong:
4453 if (cond_inverted) {
4454 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4455 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4456 } else {
4457 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4458 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4459 }
4460 break;
4461 case Primitive::kPrimFloat:
4462 if (cond_inverted) {
4463 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4464 } else {
4465 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4466 }
4467 break;
4468 case Primitive::kPrimDouble:
4469 if (cond_inverted) {
4470 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4471 } else {
4472 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4473 }
4474 break;
4475 }
4476 break;
4477 case Primitive::kPrimLong:
4478 LOG(FATAL) << "Unreachable";
4479 UNREACHABLE();
4480 case Primitive::kPrimFloat:
4481 case Primitive::kPrimDouble:
4482 switch (dst_type) {
4483 default:
4484 if (cond_inverted) {
4485 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4486 } else {
4487 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4488 }
4489 break;
4490 case Primitive::kPrimLong:
4491 if (cond_inverted) {
4492 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4493 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4494 } else {
4495 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4496 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4497 }
4498 break;
4499 case Primitive::kPrimFloat:
4500 if (cond_inverted) {
4501 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4502 } else {
4503 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4504 }
4505 break;
4506 case Primitive::kPrimDouble:
4507 if (cond_inverted) {
4508 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4509 } else {
4510 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4511 }
4512 break;
4513 }
4514 break;
4515 }
4516}
4517
4518void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4519 LocationSummary* locations = select->GetLocations();
4520 Location dst = locations->Out();
4521 Location false_src = locations->InAt(0);
4522 Location true_src = locations->InAt(1);
4523 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4524 Register cond_reg = TMP;
4525 FRegister fcond_reg = FTMP;
4526 Primitive::Type cond_type = Primitive::kPrimInt;
4527 bool cond_inverted = false;
4528 Primitive::Type dst_type = select->GetType();
4529
4530 if (IsBooleanValueOrMaterializedCondition(cond)) {
4531 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4532 } else {
4533 HCondition* condition = cond->AsCondition();
4534 LocationSummary* cond_locations = cond->GetLocations();
4535 IfCondition if_cond = condition->GetCondition();
4536 cond_type = condition->InputAt(0)->GetType();
4537 switch (cond_type) {
4538 default:
4539 DCHECK_NE(cond_type, Primitive::kPrimLong);
4540 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4541 break;
4542 case Primitive::kPrimFloat:
4543 case Primitive::kPrimDouble:
4544 cond_inverted = MaterializeFpCompareR6(if_cond,
4545 condition->IsGtBias(),
4546 cond_type,
4547 cond_locations,
4548 fcond_reg);
4549 break;
4550 }
4551 }
4552
4553 if (true_src.IsConstant()) {
4554 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4555 }
4556 if (false_src.IsConstant()) {
4557 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4558 }
4559
4560 switch (dst_type) {
4561 default:
4562 if (Primitive::IsFloatingPointType(cond_type)) {
4563 __ Mfc1(cond_reg, fcond_reg);
4564 }
4565 if (true_src.IsConstant()) {
4566 if (cond_inverted) {
4567 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4568 } else {
4569 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4570 }
4571 } else if (false_src.IsConstant()) {
4572 if (cond_inverted) {
4573 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4574 } else {
4575 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4576 }
4577 } else {
4578 DCHECK_NE(cond_reg, AT);
4579 if (cond_inverted) {
4580 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4581 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4582 } else {
4583 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4584 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4585 }
4586 __ Or(dst.AsRegister<Register>(), AT, TMP);
4587 }
4588 break;
4589 case Primitive::kPrimLong: {
4590 if (Primitive::IsFloatingPointType(cond_type)) {
4591 __ Mfc1(cond_reg, fcond_reg);
4592 }
4593 Register dst_lo = dst.AsRegisterPairLow<Register>();
4594 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4595 if (true_src.IsConstant()) {
4596 Register src_lo = false_src.AsRegisterPairLow<Register>();
4597 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4598 if (cond_inverted) {
4599 __ Selnez(dst_lo, src_lo, cond_reg);
4600 __ Selnez(dst_hi, src_hi, cond_reg);
4601 } else {
4602 __ Seleqz(dst_lo, src_lo, cond_reg);
4603 __ Seleqz(dst_hi, src_hi, cond_reg);
4604 }
4605 } else {
4606 DCHECK(false_src.IsConstant());
4607 Register src_lo = true_src.AsRegisterPairLow<Register>();
4608 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4609 if (cond_inverted) {
4610 __ Seleqz(dst_lo, src_lo, cond_reg);
4611 __ Seleqz(dst_hi, src_hi, cond_reg);
4612 } else {
4613 __ Selnez(dst_lo, src_lo, cond_reg);
4614 __ Selnez(dst_hi, src_hi, cond_reg);
4615 }
4616 }
4617 break;
4618 }
4619 case Primitive::kPrimFloat: {
4620 if (!Primitive::IsFloatingPointType(cond_type)) {
4621 // sel*.fmt tests bit 0 of the condition register, account for that.
4622 __ Sltu(TMP, ZERO, cond_reg);
4623 __ Mtc1(TMP, fcond_reg);
4624 }
4625 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4626 if (true_src.IsConstant()) {
4627 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4628 if (cond_inverted) {
4629 __ SelnezS(dst_reg, src_reg, fcond_reg);
4630 } else {
4631 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4632 }
4633 } else if (false_src.IsConstant()) {
4634 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4635 if (cond_inverted) {
4636 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4637 } else {
4638 __ SelnezS(dst_reg, src_reg, fcond_reg);
4639 }
4640 } else {
4641 if (cond_inverted) {
4642 __ SelS(fcond_reg,
4643 true_src.AsFpuRegister<FRegister>(),
4644 false_src.AsFpuRegister<FRegister>());
4645 } else {
4646 __ SelS(fcond_reg,
4647 false_src.AsFpuRegister<FRegister>(),
4648 true_src.AsFpuRegister<FRegister>());
4649 }
4650 __ MovS(dst_reg, fcond_reg);
4651 }
4652 break;
4653 }
4654 case Primitive::kPrimDouble: {
4655 if (!Primitive::IsFloatingPointType(cond_type)) {
4656 // sel*.fmt tests bit 0 of the condition register, account for that.
4657 __ Sltu(TMP, ZERO, cond_reg);
4658 __ Mtc1(TMP, fcond_reg);
4659 }
4660 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4661 if (true_src.IsConstant()) {
4662 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4663 if (cond_inverted) {
4664 __ SelnezD(dst_reg, src_reg, fcond_reg);
4665 } else {
4666 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4667 }
4668 } else if (false_src.IsConstant()) {
4669 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4670 if (cond_inverted) {
4671 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4672 } else {
4673 __ SelnezD(dst_reg, src_reg, fcond_reg);
4674 }
4675 } else {
4676 if (cond_inverted) {
4677 __ SelD(fcond_reg,
4678 true_src.AsFpuRegister<FRegister>(),
4679 false_src.AsFpuRegister<FRegister>());
4680 } else {
4681 __ SelD(fcond_reg,
4682 false_src.AsFpuRegister<FRegister>(),
4683 true_src.AsFpuRegister<FRegister>());
4684 }
4685 __ MovD(dst_reg, fcond_reg);
4686 }
4687 break;
4688 }
4689 }
4690}
4691
David Brazdil74eb1b22015-12-14 11:44:01 +00004692void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4693 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004694 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004695}
4696
4697void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004698 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4699 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4700 if (is_r6) {
4701 GenConditionalMoveR6(select);
4702 } else {
4703 GenConditionalMoveR2(select);
4704 }
4705 } else {
4706 LocationSummary* locations = select->GetLocations();
4707 MipsLabel false_target;
4708 GenerateTestAndBranch(select,
4709 /* condition_input_index */ 2,
4710 /* true_target */ nullptr,
4711 &false_target);
4712 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4713 __ Bind(&false_target);
4714 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004715}
4716
David Srbecky0cf44932015-12-09 14:09:59 +00004717void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4718 new (GetGraph()->GetArena()) LocationSummary(info);
4719}
4720
David Srbeckyd28f4a02016-03-14 17:14:24 +00004721void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4722 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004723}
4724
4725void CodeGeneratorMIPS::GenerateNop() {
4726 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004727}
4728
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004729void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4730 Primitive::Type field_type = field_info.GetFieldType();
4731 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4732 bool generate_volatile = field_info.IsVolatile() && is_wide;
4733 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004734 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004735
4736 locations->SetInAt(0, Location::RequiresRegister());
4737 if (generate_volatile) {
4738 InvokeRuntimeCallingConvention calling_convention;
4739 // need A0 to hold base + offset
4740 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4741 if (field_type == Primitive::kPrimLong) {
4742 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4743 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004744 // Use Location::Any() to prevent situations when running out of available fp registers.
4745 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004746 // Need some temp core regs since FP results are returned in core registers
4747 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4748 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4749 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4750 }
4751 } else {
4752 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4753 locations->SetOut(Location::RequiresFpuRegister());
4754 } else {
4755 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4756 }
4757 }
4758}
4759
4760void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4761 const FieldInfo& field_info,
4762 uint32_t dex_pc) {
4763 Primitive::Type type = field_info.GetFieldType();
4764 LocationSummary* locations = instruction->GetLocations();
4765 Register obj = locations->InAt(0).AsRegister<Register>();
4766 LoadOperandType load_type = kLoadUnsignedByte;
4767 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004768 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004769 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004770
4771 switch (type) {
4772 case Primitive::kPrimBoolean:
4773 load_type = kLoadUnsignedByte;
4774 break;
4775 case Primitive::kPrimByte:
4776 load_type = kLoadSignedByte;
4777 break;
4778 case Primitive::kPrimShort:
4779 load_type = kLoadSignedHalfword;
4780 break;
4781 case Primitive::kPrimChar:
4782 load_type = kLoadUnsignedHalfword;
4783 break;
4784 case Primitive::kPrimInt:
4785 case Primitive::kPrimFloat:
4786 case Primitive::kPrimNot:
4787 load_type = kLoadWord;
4788 break;
4789 case Primitive::kPrimLong:
4790 case Primitive::kPrimDouble:
4791 load_type = kLoadDoubleword;
4792 break;
4793 case Primitive::kPrimVoid:
4794 LOG(FATAL) << "Unreachable type " << type;
4795 UNREACHABLE();
4796 }
4797
4798 if (is_volatile && load_type == kLoadDoubleword) {
4799 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004800 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004801 // Do implicit Null check
4802 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4803 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004804 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004805 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4806 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004807 // FP results are returned in core registers. Need to move them.
4808 Location out = locations->Out();
4809 if (out.IsFpuRegister()) {
4810 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4811 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4812 out.AsFpuRegister<FRegister>());
4813 } else {
4814 DCHECK(out.IsDoubleStackSlot());
4815 __ StoreToOffset(kStoreWord,
4816 locations->GetTemp(1).AsRegister<Register>(),
4817 SP,
4818 out.GetStackIndex());
4819 __ StoreToOffset(kStoreWord,
4820 locations->GetTemp(2).AsRegister<Register>(),
4821 SP,
4822 out.GetStackIndex() + 4);
4823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004824 }
4825 } else {
4826 if (!Primitive::IsFloatingPointType(type)) {
4827 Register dst;
4828 if (type == Primitive::kPrimLong) {
4829 DCHECK(locations->Out().IsRegisterPair());
4830 dst = locations->Out().AsRegisterPairLow<Register>();
4831 } else {
4832 DCHECK(locations->Out().IsRegister());
4833 dst = locations->Out().AsRegister<Register>();
4834 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004835 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004836 } else {
4837 DCHECK(locations->Out().IsFpuRegister());
4838 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4839 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004840 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004841 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004842 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004843 }
4844 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004845 }
4846
4847 if (is_volatile) {
4848 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4849 }
4850}
4851
4852void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4853 Primitive::Type field_type = field_info.GetFieldType();
4854 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4855 bool generate_volatile = field_info.IsVolatile() && is_wide;
4856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004857 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004858
4859 locations->SetInAt(0, Location::RequiresRegister());
4860 if (generate_volatile) {
4861 InvokeRuntimeCallingConvention calling_convention;
4862 // need A0 to hold base + offset
4863 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4864 if (field_type == Primitive::kPrimLong) {
4865 locations->SetInAt(1, Location::RegisterPairLocation(
4866 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4867 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004868 // Use Location::Any() to prevent situations when running out of available fp registers.
4869 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004870 // Pass FP parameters in core registers.
4871 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4872 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4873 }
4874 } else {
4875 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004876 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004877 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004878 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004879 }
4880 }
4881}
4882
4883void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4884 const FieldInfo& field_info,
4885 uint32_t dex_pc) {
4886 Primitive::Type type = field_info.GetFieldType();
4887 LocationSummary* locations = instruction->GetLocations();
4888 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004889 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004890 StoreOperandType store_type = kStoreByte;
4891 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004892 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004893 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004894
4895 switch (type) {
4896 case Primitive::kPrimBoolean:
4897 case Primitive::kPrimByte:
4898 store_type = kStoreByte;
4899 break;
4900 case Primitive::kPrimShort:
4901 case Primitive::kPrimChar:
4902 store_type = kStoreHalfword;
4903 break;
4904 case Primitive::kPrimInt:
4905 case Primitive::kPrimFloat:
4906 case Primitive::kPrimNot:
4907 store_type = kStoreWord;
4908 break;
4909 case Primitive::kPrimLong:
4910 case Primitive::kPrimDouble:
4911 store_type = kStoreDoubleword;
4912 break;
4913 case Primitive::kPrimVoid:
4914 LOG(FATAL) << "Unreachable type " << type;
4915 UNREACHABLE();
4916 }
4917
4918 if (is_volatile) {
4919 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4920 }
4921
4922 if (is_volatile && store_type == kStoreDoubleword) {
4923 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004924 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004925 // Do implicit Null check.
4926 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4927 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4928 if (type == Primitive::kPrimDouble) {
4929 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004930 if (value_location.IsFpuRegister()) {
4931 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4932 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004933 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004934 value_location.AsFpuRegister<FRegister>());
4935 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004936 __ LoadFromOffset(kLoadWord,
4937 locations->GetTemp(1).AsRegister<Register>(),
4938 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004939 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004940 __ LoadFromOffset(kLoadWord,
4941 locations->GetTemp(2).AsRegister<Register>(),
4942 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004943 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004944 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004945 DCHECK(value_location.IsConstant());
4946 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4947 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004948 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4949 locations->GetTemp(1).AsRegister<Register>(),
4950 value);
4951 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004952 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004953 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004954 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4955 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004956 if (value_location.IsConstant()) {
4957 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4958 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4959 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004960 Register src;
4961 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004962 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004963 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004964 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004965 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004966 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004967 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004968 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004969 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004970 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004971 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004972 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004973 }
4974 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004975 }
4976
4977 // TODO: memory barriers?
4978 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004979 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004980 codegen_->MarkGCCard(obj, src);
4981 }
4982
4983 if (is_volatile) {
4984 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4985 }
4986}
4987
4988void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4989 HandleFieldGet(instruction, instruction->GetFieldInfo());
4990}
4991
4992void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4993 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4994}
4995
4996void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4997 HandleFieldSet(instruction, instruction->GetFieldInfo());
4998}
4999
5000void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5001 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
5002}
5003
Alexey Frunze06a46c42016-07-19 15:00:40 -07005004void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
5005 HInstruction* instruction ATTRIBUTE_UNUSED,
5006 Location root,
5007 Register obj,
5008 uint32_t offset) {
5009 Register root_reg = root.AsRegister<Register>();
5010 if (kEmitCompilerReadBarrier) {
5011 UNIMPLEMENTED(FATAL) << "for read barrier";
5012 } else {
5013 // Plain GC root load with no read barrier.
5014 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5015 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5016 // Note that GC roots are not affected by heap poisoning, thus we
5017 // do not have to unpoison `root_reg` here.
5018 }
5019}
5020
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005021void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5022 LocationSummary::CallKind call_kind =
5023 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5025 locations->SetInAt(0, Location::RequiresRegister());
5026 locations->SetInAt(1, Location::RequiresRegister());
5027 // The output does overlap inputs.
5028 // Note that TypeCheckSlowPathMIPS uses this register too.
5029 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5030}
5031
5032void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5033 LocationSummary* locations = instruction->GetLocations();
5034 Register obj = locations->InAt(0).AsRegister<Register>();
5035 Register cls = locations->InAt(1).AsRegister<Register>();
5036 Register out = locations->Out().AsRegister<Register>();
5037
5038 MipsLabel done;
5039
5040 // Return 0 if `obj` is null.
5041 // TODO: Avoid this check if we know `obj` is not null.
5042 __ Move(out, ZERO);
5043 __ Beqz(obj, &done);
5044
5045 // Compare the class of `obj` with `cls`.
5046 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5047 if (instruction->IsExactCheck()) {
5048 // Classes must be equal for the instanceof to succeed.
5049 __ Xor(out, out, cls);
5050 __ Sltiu(out, out, 1);
5051 } else {
5052 // If the classes are not equal, we go into a slow path.
5053 DCHECK(locations->OnlyCallsOnSlowPath());
5054 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5055 codegen_->AddSlowPath(slow_path);
5056 __ Bne(out, cls, slow_path->GetEntryLabel());
5057 __ LoadConst32(out, 1);
5058 __ Bind(slow_path->GetExitLabel());
5059 }
5060
5061 __ Bind(&done);
5062}
5063
5064void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5066 locations->SetOut(Location::ConstantLocation(constant));
5067}
5068
5069void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5070 // Will be generated at use site.
5071}
5072
5073void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5074 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5075 locations->SetOut(Location::ConstantLocation(constant));
5076}
5077
5078void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5079 // Will be generated at use site.
5080}
5081
5082void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5083 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5084 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5085}
5086
5087void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5088 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005089 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005090 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005091 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005092}
5093
5094void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5095 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5096 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005097 Location receiver = invoke->GetLocations()->InAt(0);
5098 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005099 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005100
5101 // Set the hidden argument.
5102 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5103 invoke->GetDexMethodIndex());
5104
5105 // temp = object->GetClass();
5106 if (receiver.IsStackSlot()) {
5107 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5108 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5109 } else {
5110 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5111 }
5112 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005113 __ LoadFromOffset(kLoadWord, temp, temp,
5114 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5115 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005116 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005117 // temp = temp->GetImtEntryAt(method_offset);
5118 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5119 // T9 = temp->GetEntryPoint();
5120 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5121 // T9();
5122 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005123 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005124 DCHECK(!codegen_->IsLeafMethod());
5125 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5126}
5127
5128void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005129 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5130 if (intrinsic.TryDispatch(invoke)) {
5131 return;
5132 }
5133
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005134 HandleInvoke(invoke);
5135}
5136
5137void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005138 // Explicit clinit checks triggered by static invokes must have been pruned by
5139 // art::PrepareForRegisterAllocation.
5140 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005141
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005142 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5143 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5144 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5145
5146 // kDirectAddressWithFixup and kCallDirectWithFixup need no extra input on R6 because
5147 // R6 has PC-relative addressing.
5148 bool has_extra_input = !isR6 &&
5149 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5150 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup));
5151
5152 if (invoke->HasPcRelativeDexCache()) {
5153 // kDexCachePcRelative is mutually exclusive with
5154 // kDirectAddressWithFixup/kCallDirectWithFixup.
5155 CHECK(!has_extra_input);
5156 has_extra_input = true;
5157 }
5158
Chris Larsen701566a2015-10-27 15:29:13 -07005159 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5160 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005161 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5162 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5163 }
Chris Larsen701566a2015-10-27 15:29:13 -07005164 return;
5165 }
5166
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005167 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005168
5169 // Add the extra input register if either the dex cache array base register
5170 // or the PC-relative base register for accessing literals is needed.
5171 if (has_extra_input) {
5172 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5173 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005174}
5175
Chris Larsen701566a2015-10-27 15:29:13 -07005176static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005177 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005178 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5179 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005180 return true;
5181 }
5182 return false;
5183}
5184
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005185HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005186 HLoadString::LoadKind desired_string_load_kind) {
5187 if (kEmitCompilerReadBarrier) {
5188 UNIMPLEMENTED(FATAL) << "for read barrier";
5189 }
5190 // We disable PC-relative load when there is an irreducible loop, as the optimization
5191 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005192 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5193 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005194 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5195 bool fallback_load = has_irreducible_loops;
5196 switch (desired_string_load_kind) {
5197 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5198 DCHECK(!GetCompilerOptions().GetCompilePic());
5199 break;
5200 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5201 DCHECK(GetCompilerOptions().GetCompilePic());
5202 break;
5203 case HLoadString::LoadKind::kBootImageAddress:
5204 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005205 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005206 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005207 break;
5208 case HLoadString::LoadKind::kDexCacheViaMethod:
5209 fallback_load = false;
5210 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005211 case HLoadString::LoadKind::kJitTableAddress:
5212 DCHECK(Runtime::Current()->UseJitCompilation());
5213 // TODO: implement.
5214 fallback_load = true;
5215 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005216 }
5217 if (fallback_load) {
5218 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5219 }
5220 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005221}
5222
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005223HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5224 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005225 if (kEmitCompilerReadBarrier) {
5226 UNIMPLEMENTED(FATAL) << "for read barrier";
5227 }
5228 // We disable pc-relative load when there is an irreducible loop, as the optimization
5229 // is incompatible with it.
5230 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5231 bool fallback_load = has_irreducible_loops;
5232 switch (desired_class_load_kind) {
5233 case HLoadClass::LoadKind::kReferrersClass:
5234 fallback_load = false;
5235 break;
5236 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5237 DCHECK(!GetCompilerOptions().GetCompilePic());
5238 break;
5239 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5240 DCHECK(GetCompilerOptions().GetCompilePic());
5241 break;
5242 case HLoadClass::LoadKind::kBootImageAddress:
5243 break;
5244 case HLoadClass::LoadKind::kDexCacheAddress:
5245 DCHECK(Runtime::Current()->UseJitCompilation());
5246 fallback_load = false;
5247 break;
5248 case HLoadClass::LoadKind::kDexCachePcRelative:
5249 DCHECK(!Runtime::Current()->UseJitCompilation());
5250 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5251 // with irreducible loops.
5252 break;
5253 case HLoadClass::LoadKind::kDexCacheViaMethod:
5254 fallback_load = false;
5255 break;
5256 }
5257 if (fallback_load) {
5258 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5259 }
5260 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005261}
5262
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005263Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5264 Register temp) {
5265 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5266 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5267 if (!invoke->GetLocations()->Intrinsified()) {
5268 return location.AsRegister<Register>();
5269 }
5270 // For intrinsics we allow any location, so it may be on the stack.
5271 if (!location.IsRegister()) {
5272 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5273 return temp;
5274 }
5275 // For register locations, check if the register was saved. If so, get it from the stack.
5276 // Note: There is a chance that the register was saved but not overwritten, so we could
5277 // save one load. However, since this is just an intrinsic slow path we prefer this
5278 // simple and more robust approach rather that trying to determine if that's the case.
5279 SlowPathCode* slow_path = GetCurrentSlowPath();
5280 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5281 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5282 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5283 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5284 return temp;
5285 }
5286 return location.AsRegister<Register>();
5287}
5288
Vladimir Markodc151b22015-10-15 18:02:30 +01005289HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5290 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005291 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005292 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5293 // We disable PC-relative load when there is an irreducible loop, as the optimization
5294 // is incompatible with it.
5295 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5296 bool fallback_load = true;
5297 bool fallback_call = true;
5298 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005299 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
5300 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005301 fallback_load = has_irreducible_loops;
5302 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005303 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005304 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005305 break;
5306 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005307 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005308 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 fallback_call = has_irreducible_loops;
5310 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005311 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005312 // TODO: Implement this type.
5313 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005314 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005315 fallback_call = false;
5316 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005317 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005318 if (fallback_load) {
5319 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5320 dispatch_info.method_load_data = 0;
5321 }
5322 if (fallback_call) {
5323 dispatch_info.code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
5324 dispatch_info.direct_code_ptr = 0;
5325 }
5326 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005327}
5328
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005329void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5330 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005331 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005332 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5333 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
5334 bool isR6 = isa_features_.IsR6();
5335 // kDirectAddressWithFixup and kCallDirectWithFixup have no extra input on R6 because
5336 // R6 has PC-relative addressing.
5337 bool has_extra_input = invoke->HasPcRelativeDexCache() ||
5338 (!isR6 &&
5339 ((method_load_kind == HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) ||
5340 (code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup)));
5341 Register base_reg = has_extra_input
5342 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5343 : ZERO;
5344
5345 // For better instruction scheduling we load the direct code pointer before the method pointer.
5346 switch (code_ptr_location) {
5347 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
5348 // T9 = invoke->GetDirectCodePtr();
5349 __ LoadConst32(T9, invoke->GetDirectCodePtr());
5350 break;
5351 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5352 // T9 = code address from literal pool with link-time patch.
5353 __ LoadLiteral(T9, base_reg, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
5354 break;
5355 default:
5356 break;
5357 }
5358
5359 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005360 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005362 uint32_t offset =
5363 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005364 __ LoadFromOffset(kLoadWord,
5365 temp.AsRegister<Register>(),
5366 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005367 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005368 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005371 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005372 break;
5373 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5374 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5375 break;
5376 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005377 __ LoadLiteral(temp.AsRegister<Register>(),
5378 base_reg,
5379 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
5380 break;
5381 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5382 HMipsDexCacheArraysBase* base =
5383 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5384 int32_t offset =
5385 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5386 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5387 break;
5388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005389 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005390 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391 Register reg = temp.AsRegister<Register>();
5392 Register method_reg;
5393 if (current_method.IsRegister()) {
5394 method_reg = current_method.AsRegister<Register>();
5395 } else {
5396 // TODO: use the appropriate DCHECK() here if possible.
5397 // DCHECK(invoke->GetLocations()->Intrinsified());
5398 DCHECK(!current_method.IsValid());
5399 method_reg = reg;
5400 __ Lw(reg, SP, kCurrentMethodStackOffset);
5401 }
5402
5403 // temp = temp->dex_cache_resolved_methods_;
5404 __ LoadFromOffset(kLoadWord,
5405 reg,
5406 method_reg,
5407 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005408 // temp = temp[index_in_cache];
5409 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5410 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005411 __ LoadFromOffset(kLoadWord,
5412 reg,
5413 reg,
5414 CodeGenerator::GetCachePointerOffset(index_in_cache));
5415 break;
5416 }
5417 }
5418
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005419 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005420 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005421 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005422 break;
5423 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005424 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
5425 // T9 prepared above for better instruction scheduling.
5426 // T9()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005427 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005428 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005430 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005431 // TODO: Implement this type.
Vladimir Markodc151b22015-10-15 18:02:30 +01005432 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch().
5433 LOG(FATAL) << "Unsupported";
5434 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005435 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5436 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005437 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005438 T9,
5439 callee_method.AsRegister<Register>(),
5440 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005441 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005442 // T9()
5443 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005444 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005445 break;
5446 }
5447 DCHECK(!IsLeafMethod());
5448}
5449
5450void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005451 // Explicit clinit checks triggered by static invokes must have been pruned by
5452 // art::PrepareForRegisterAllocation.
5453 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454
5455 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5456 return;
5457 }
5458
5459 LocationSummary* locations = invoke->GetLocations();
5460 codegen_->GenerateStaticOrDirectCall(invoke,
5461 locations->HasTemps()
5462 ? locations->GetTemp(0)
5463 : Location::NoLocation());
5464 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5465}
5466
Chris Larsen3acee732015-11-18 13:31:08 -08005467void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005468 // Use the calling convention instead of the location of the receiver, as
5469 // intrinsics may have put the receiver in a different register. In the intrinsics
5470 // slow path, the arguments have been moved to the right place, so here we are
5471 // guaranteed that the receiver is the first register of the calling convention.
5472 InvokeDexCallingConvention calling_convention;
5473 Register receiver = calling_convention.GetRegisterAt(0);
5474
Chris Larsen3acee732015-11-18 13:31:08 -08005475 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005476 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5477 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5478 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005479 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005480
5481 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005482 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005483 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005484 // temp = temp->GetMethodAt(method_offset);
5485 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5486 // T9 = temp->GetEntryPoint();
5487 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5488 // T9();
5489 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005490 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005491}
5492
5493void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5494 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5495 return;
5496 }
5497
5498 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005499 DCHECK(!codegen_->IsLeafMethod());
5500 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5501}
5502
5503void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005504 if (cls->NeedsAccessCheck()) {
5505 InvokeRuntimeCallingConvention calling_convention;
5506 CodeGenerator::CreateLoadClassLocationSummary(
5507 cls,
5508 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5509 Location::RegisterLocation(V0),
5510 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5511 return;
5512 }
5513
5514 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5515 ? LocationSummary::kCallOnSlowPath
5516 : LocationSummary::kNoCall;
5517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5518 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5519 switch (load_kind) {
5520 // We need an extra register for PC-relative literals on R2.
5521 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5522 case HLoadClass::LoadKind::kBootImageAddress:
5523 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5524 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5525 break;
5526 }
5527 FALLTHROUGH_INTENDED;
5528 // We need an extra register for PC-relative dex cache accesses.
5529 case HLoadClass::LoadKind::kDexCachePcRelative:
5530 case HLoadClass::LoadKind::kReferrersClass:
5531 case HLoadClass::LoadKind::kDexCacheViaMethod:
5532 locations->SetInAt(0, Location::RequiresRegister());
5533 break;
5534 default:
5535 break;
5536 }
5537 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005538}
5539
5540void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5541 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005542 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005543 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005544 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005545 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005546 return;
5547 }
5548
Alexey Frunze06a46c42016-07-19 15:00:40 -07005549 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5550 Location out_loc = locations->Out();
5551 Register out = out_loc.AsRegister<Register>();
5552 Register base_or_current_method_reg;
5553 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5554 switch (load_kind) {
5555 // We need an extra register for PC-relative literals on R2.
5556 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5557 case HLoadClass::LoadKind::kBootImageAddress:
5558 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5559 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5560 break;
5561 // We need an extra register for PC-relative dex cache accesses.
5562 case HLoadClass::LoadKind::kDexCachePcRelative:
5563 case HLoadClass::LoadKind::kReferrersClass:
5564 case HLoadClass::LoadKind::kDexCacheViaMethod:
5565 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5566 break;
5567 default:
5568 base_or_current_method_reg = ZERO;
5569 break;
5570 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005571
Alexey Frunze06a46c42016-07-19 15:00:40 -07005572 bool generate_null_check = false;
5573 switch (load_kind) {
5574 case HLoadClass::LoadKind::kReferrersClass: {
5575 DCHECK(!cls->CanCallRuntime());
5576 DCHECK(!cls->MustGenerateClinitCheck());
5577 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5578 GenerateGcRootFieldLoad(cls,
5579 out_loc,
5580 base_or_current_method_reg,
5581 ArtMethod::DeclaringClassOffset().Int32Value());
5582 break;
5583 }
5584 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5585 DCHECK(!kEmitCompilerReadBarrier);
5586 __ LoadLiteral(out,
5587 base_or_current_method_reg,
5588 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5589 cls->GetTypeIndex()));
5590 break;
5591 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5592 DCHECK(!kEmitCompilerReadBarrier);
5593 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5594 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005595 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005596 break;
5597 }
5598 case HLoadClass::LoadKind::kBootImageAddress: {
5599 DCHECK(!kEmitCompilerReadBarrier);
5600 DCHECK_NE(cls->GetAddress(), 0u);
5601 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5602 __ LoadLiteral(out,
5603 base_or_current_method_reg,
5604 codegen_->DeduplicateBootImageAddressLiteral(address));
5605 break;
5606 }
5607 case HLoadClass::LoadKind::kDexCacheAddress: {
5608 DCHECK_NE(cls->GetAddress(), 0u);
5609 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5610 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5611 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5612 int16_t offset = Low16Bits(address);
5613 uint32_t base_address = address - offset; // This accounts for offset sign extension.
5614 __ Lui(out, High16Bits(base_address));
5615 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
5616 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5617 generate_null_check = !cls->IsInDexCache();
5618 break;
5619 }
5620 case HLoadClass::LoadKind::kDexCachePcRelative: {
5621 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5622 int32_t offset =
5623 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5624 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5625 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5626 generate_null_check = !cls->IsInDexCache();
5627 break;
5628 }
5629 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5630 // /* GcRoot<mirror::Class>[] */ out =
5631 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5632 __ LoadFromOffset(kLoadWord,
5633 out,
5634 base_or_current_method_reg,
5635 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5636 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005637 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005638 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5639 generate_null_check = !cls->IsInDexCache();
5640 }
5641 }
5642
5643 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5644 DCHECK(cls->CanCallRuntime());
5645 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5646 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5647 codegen_->AddSlowPath(slow_path);
5648 if (generate_null_check) {
5649 __ Beqz(out, slow_path->GetEntryLabel());
5650 }
5651 if (cls->MustGenerateClinitCheck()) {
5652 GenerateClassInitializationCheck(slow_path, out);
5653 } else {
5654 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005655 }
5656 }
5657}
5658
5659static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005660 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005661}
5662
5663void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5664 LocationSummary* locations =
5665 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5666 locations->SetOut(Location::RequiresRegister());
5667}
5668
5669void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5670 Register out = load->GetLocations()->Out().AsRegister<Register>();
5671 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5672}
5673
5674void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5675 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5676}
5677
5678void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5679 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5680}
5681
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005682void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005683 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Vladimir Markoaad75c62016-10-03 08:46:48 +00005684 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5685 ? LocationSummary::kCallOnMainOnly
5686 : LocationSummary::kCallOnSlowPath)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005687 : LocationSummary::kNoCall;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005689 HLoadString::LoadKind load_kind = load->GetLoadKind();
5690 switch (load_kind) {
5691 // We need an extra register for PC-relative literals on R2.
5692 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5693 case HLoadString::LoadKind::kBootImageAddress:
5694 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005695 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005696 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5697 break;
5698 }
5699 FALLTHROUGH_INTENDED;
5700 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005701 case HLoadString::LoadKind::kDexCacheViaMethod:
5702 locations->SetInAt(0, Location::RequiresRegister());
5703 break;
5704 default:
5705 break;
5706 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005707 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5708 InvokeRuntimeCallingConvention calling_convention;
5709 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5710 } else {
5711 locations->SetOut(Location::RequiresRegister());
5712 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005713}
5714
5715void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005716 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005717 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005718 Location out_loc = locations->Out();
5719 Register out = out_loc.AsRegister<Register>();
5720 Register base_or_current_method_reg;
5721 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5722 switch (load_kind) {
5723 // We need an extra register for PC-relative literals on R2.
5724 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5725 case HLoadString::LoadKind::kBootImageAddress:
5726 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005727 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005728 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5729 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005730 default:
5731 base_or_current_method_reg = ZERO;
5732 break;
5733 }
5734
5735 switch (load_kind) {
5736 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5737 DCHECK(!kEmitCompilerReadBarrier);
5738 __ LoadLiteral(out,
5739 base_or_current_method_reg,
5740 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5741 load->GetStringIndex()));
5742 return; // No dex cache slow path.
5743 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5744 DCHECK(!kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005745 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005746 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5747 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005748 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005749 return; // No dex cache slow path.
5750 }
5751 case HLoadString::LoadKind::kBootImageAddress: {
5752 DCHECK(!kEmitCompilerReadBarrier);
5753 DCHECK_NE(load->GetAddress(), 0u);
5754 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5755 __ LoadLiteral(out,
5756 base_or_current_method_reg,
5757 codegen_->DeduplicateBootImageAddressLiteral(address));
5758 return; // No dex cache slow path.
5759 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005760 case HLoadString::LoadKind::kBssEntry: {
5761 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5762 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5763 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5764 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5765 __ LoadFromOffset(kLoadWord, out, out, 0);
5766 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5767 codegen_->AddSlowPath(slow_path);
5768 __ Beqz(out, slow_path->GetEntryLabel());
5769 __ Bind(slow_path->GetExitLabel());
5770 return;
5771 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005772 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005773 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005774 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005775
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005776 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005777 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5778 InvokeRuntimeCallingConvention calling_convention;
5779 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5780 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5781 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005782}
5783
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005784void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5786 locations->SetOut(Location::ConstantLocation(constant));
5787}
5788
5789void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5790 // Will be generated at use site.
5791}
5792
5793void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5794 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796 InvokeRuntimeCallingConvention calling_convention;
5797 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5798}
5799
5800void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5801 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005802 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005803 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5804 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005805 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005806 }
5807 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5808}
5809
5810void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5811 LocationSummary* locations =
5812 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5813 switch (mul->GetResultType()) {
5814 case Primitive::kPrimInt:
5815 case Primitive::kPrimLong:
5816 locations->SetInAt(0, Location::RequiresRegister());
5817 locations->SetInAt(1, Location::RequiresRegister());
5818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5819 break;
5820
5821 case Primitive::kPrimFloat:
5822 case Primitive::kPrimDouble:
5823 locations->SetInAt(0, Location::RequiresFpuRegister());
5824 locations->SetInAt(1, Location::RequiresFpuRegister());
5825 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5826 break;
5827
5828 default:
5829 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5830 }
5831}
5832
5833void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5834 Primitive::Type type = instruction->GetType();
5835 LocationSummary* locations = instruction->GetLocations();
5836 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5837
5838 switch (type) {
5839 case Primitive::kPrimInt: {
5840 Register dst = locations->Out().AsRegister<Register>();
5841 Register lhs = locations->InAt(0).AsRegister<Register>();
5842 Register rhs = locations->InAt(1).AsRegister<Register>();
5843
5844 if (isR6) {
5845 __ MulR6(dst, lhs, rhs);
5846 } else {
5847 __ MulR2(dst, lhs, rhs);
5848 }
5849 break;
5850 }
5851 case Primitive::kPrimLong: {
5852 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5853 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5854 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5855 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5856 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5857 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5858
5859 // Extra checks to protect caused by the existance of A1_A2.
5860 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5861 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5862 DCHECK_NE(dst_high, lhs_low);
5863 DCHECK_NE(dst_high, rhs_low);
5864
5865 // A_B * C_D
5866 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5867 // dst_lo: [ low(B*D) ]
5868 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5869
5870 if (isR6) {
5871 __ MulR6(TMP, lhs_high, rhs_low);
5872 __ MulR6(dst_high, lhs_low, rhs_high);
5873 __ Addu(dst_high, dst_high, TMP);
5874 __ MuhuR6(TMP, lhs_low, rhs_low);
5875 __ Addu(dst_high, dst_high, TMP);
5876 __ MulR6(dst_low, lhs_low, rhs_low);
5877 } else {
5878 __ MulR2(TMP, lhs_high, rhs_low);
5879 __ MulR2(dst_high, lhs_low, rhs_high);
5880 __ Addu(dst_high, dst_high, TMP);
5881 __ MultuR2(lhs_low, rhs_low);
5882 __ Mfhi(TMP);
5883 __ Addu(dst_high, dst_high, TMP);
5884 __ Mflo(dst_low);
5885 }
5886 break;
5887 }
5888 case Primitive::kPrimFloat:
5889 case Primitive::kPrimDouble: {
5890 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5891 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5892 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5893 if (type == Primitive::kPrimFloat) {
5894 __ MulS(dst, lhs, rhs);
5895 } else {
5896 __ MulD(dst, lhs, rhs);
5897 }
5898 break;
5899 }
5900 default:
5901 LOG(FATAL) << "Unexpected mul type " << type;
5902 }
5903}
5904
5905void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5906 LocationSummary* locations =
5907 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5908 switch (neg->GetResultType()) {
5909 case Primitive::kPrimInt:
5910 case Primitive::kPrimLong:
5911 locations->SetInAt(0, Location::RequiresRegister());
5912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5913 break;
5914
5915 case Primitive::kPrimFloat:
5916 case Primitive::kPrimDouble:
5917 locations->SetInAt(0, Location::RequiresFpuRegister());
5918 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5919 break;
5920
5921 default:
5922 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5923 }
5924}
5925
5926void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5927 Primitive::Type type = instruction->GetType();
5928 LocationSummary* locations = instruction->GetLocations();
5929
5930 switch (type) {
5931 case Primitive::kPrimInt: {
5932 Register dst = locations->Out().AsRegister<Register>();
5933 Register src = locations->InAt(0).AsRegister<Register>();
5934 __ Subu(dst, ZERO, src);
5935 break;
5936 }
5937 case Primitive::kPrimLong: {
5938 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5939 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5940 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5941 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5942 __ Subu(dst_low, ZERO, src_low);
5943 __ Sltu(TMP, ZERO, dst_low);
5944 __ Subu(dst_high, ZERO, src_high);
5945 __ Subu(dst_high, dst_high, TMP);
5946 break;
5947 }
5948 case Primitive::kPrimFloat:
5949 case Primitive::kPrimDouble: {
5950 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5951 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5952 if (type == Primitive::kPrimFloat) {
5953 __ NegS(dst, src);
5954 } else {
5955 __ NegD(dst, src);
5956 }
5957 break;
5958 }
5959 default:
5960 LOG(FATAL) << "Unexpected neg type " << type;
5961 }
5962}
5963
5964void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5965 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005966 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005967 InvokeRuntimeCallingConvention calling_convention;
5968 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5969 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5970 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5971 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5972}
5973
5974void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5975 InvokeRuntimeCallingConvention calling_convention;
5976 Register current_method_register = calling_convention.GetRegisterAt(2);
5977 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5978 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005979 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005980 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005981 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5982 void*, uint32_t, int32_t, ArtMethod*>();
5983}
5984
5985void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5986 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005988 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005989 if (instruction->IsStringAlloc()) {
5990 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5991 } else {
5992 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5993 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5994 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005995 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5996}
5997
5998void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005999 if (instruction->IsStringAlloc()) {
6000 // String is allocated through StringFactory. Call NewEmptyString entry point.
6001 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07006002 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00006003 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
6004 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
6005 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006006 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00006007 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6008 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006009 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00006010 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
6011 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006012}
6013
6014void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
6015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6016 locations->SetInAt(0, Location::RequiresRegister());
6017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6018}
6019
6020void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
6021 Primitive::Type type = instruction->GetType();
6022 LocationSummary* locations = instruction->GetLocations();
6023
6024 switch (type) {
6025 case Primitive::kPrimInt: {
6026 Register dst = locations->Out().AsRegister<Register>();
6027 Register src = locations->InAt(0).AsRegister<Register>();
6028 __ Nor(dst, src, ZERO);
6029 break;
6030 }
6031
6032 case Primitive::kPrimLong: {
6033 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6034 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6035 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6036 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6037 __ Nor(dst_high, src_high, ZERO);
6038 __ Nor(dst_low, src_low, ZERO);
6039 break;
6040 }
6041
6042 default:
6043 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
6044 }
6045}
6046
6047void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6048 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6049 locations->SetInAt(0, Location::RequiresRegister());
6050 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6051}
6052
6053void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
6054 LocationSummary* locations = instruction->GetLocations();
6055 __ Xori(locations->Out().AsRegister<Register>(),
6056 locations->InAt(0).AsRegister<Register>(),
6057 1);
6058}
6059
6060void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006061 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6062 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006063}
6064
Calin Juravle2ae48182016-03-16 14:05:09 +00006065void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
6066 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006067 return;
6068 }
6069 Location obj = instruction->GetLocations()->InAt(0);
6070
6071 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006072 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006073}
6074
Calin Juravle2ae48182016-03-16 14:05:09 +00006075void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006076 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006077 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006078
6079 Location obj = instruction->GetLocations()->InAt(0);
6080
6081 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
6082}
6083
6084void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006085 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006086}
6087
6088void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
6089 HandleBinaryOp(instruction);
6090}
6091
6092void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
6093 HandleBinaryOp(instruction);
6094}
6095
6096void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
6097 LOG(FATAL) << "Unreachable";
6098}
6099
6100void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6101 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6102}
6103
6104void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6106 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6107 if (location.IsStackSlot()) {
6108 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6109 } else if (location.IsDoubleStackSlot()) {
6110 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6111 }
6112 locations->SetOut(location);
6113}
6114
6115void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6116 ATTRIBUTE_UNUSED) {
6117 // Nothing to do, the parameter is already at its location.
6118}
6119
6120void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6121 LocationSummary* locations =
6122 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6123 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6124}
6125
6126void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6127 ATTRIBUTE_UNUSED) {
6128 // Nothing to do, the method is already at its location.
6129}
6130
6131void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006133 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006134 locations->SetInAt(i, Location::Any());
6135 }
6136 locations->SetOut(Location::Any());
6137}
6138
6139void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6140 LOG(FATAL) << "Unreachable";
6141}
6142
6143void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6144 Primitive::Type type = rem->GetResultType();
6145 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006146 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006147 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6148
6149 switch (type) {
6150 case Primitive::kPrimInt:
6151 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006152 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6154 break;
6155
6156 case Primitive::kPrimLong: {
6157 InvokeRuntimeCallingConvention calling_convention;
6158 locations->SetInAt(0, Location::RegisterPairLocation(
6159 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6160 locations->SetInAt(1, Location::RegisterPairLocation(
6161 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6162 locations->SetOut(calling_convention.GetReturnLocation(type));
6163 break;
6164 }
6165
6166 case Primitive::kPrimFloat:
6167 case Primitive::kPrimDouble: {
6168 InvokeRuntimeCallingConvention calling_convention;
6169 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6170 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6171 locations->SetOut(calling_convention.GetReturnLocation(type));
6172 break;
6173 }
6174
6175 default:
6176 LOG(FATAL) << "Unexpected rem type " << type;
6177 }
6178}
6179
6180void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6181 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006182
6183 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006184 case Primitive::kPrimInt:
6185 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006186 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006187 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006188 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006189 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6190 break;
6191 }
6192 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006193 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006194 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006195 break;
6196 }
6197 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006198 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006199 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006200 break;
6201 }
6202 default:
6203 LOG(FATAL) << "Unexpected rem type " << type;
6204 }
6205}
6206
6207void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6208 memory_barrier->SetLocations(nullptr);
6209}
6210
6211void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6212 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6213}
6214
6215void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6217 Primitive::Type return_type = ret->InputAt(0)->GetType();
6218 locations->SetInAt(0, MipsReturnLocation(return_type));
6219}
6220
6221void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6222 codegen_->GenerateFrameExit();
6223}
6224
6225void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6226 ret->SetLocations(nullptr);
6227}
6228
6229void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6230 codegen_->GenerateFrameExit();
6231}
6232
Alexey Frunze92d90602015-12-18 18:16:36 -08006233void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6234 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006235}
6236
Alexey Frunze92d90602015-12-18 18:16:36 -08006237void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6238 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006239}
6240
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006241void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6242 HandleShift(shl);
6243}
6244
6245void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6246 HandleShift(shl);
6247}
6248
6249void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6250 HandleShift(shr);
6251}
6252
6253void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6254 HandleShift(shr);
6255}
6256
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6258 HandleBinaryOp(instruction);
6259}
6260
6261void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6262 HandleBinaryOp(instruction);
6263}
6264
6265void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6266 HandleFieldGet(instruction, instruction->GetFieldInfo());
6267}
6268
6269void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6270 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6271}
6272
6273void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6274 HandleFieldSet(instruction, instruction->GetFieldInfo());
6275}
6276
6277void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6278 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6279}
6280
6281void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6282 HUnresolvedInstanceFieldGet* instruction) {
6283 FieldAccessCallingConventionMIPS calling_convention;
6284 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6285 instruction->GetFieldType(),
6286 calling_convention);
6287}
6288
6289void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6290 HUnresolvedInstanceFieldGet* instruction) {
6291 FieldAccessCallingConventionMIPS calling_convention;
6292 codegen_->GenerateUnresolvedFieldAccess(instruction,
6293 instruction->GetFieldType(),
6294 instruction->GetFieldIndex(),
6295 instruction->GetDexPc(),
6296 calling_convention);
6297}
6298
6299void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6300 HUnresolvedInstanceFieldSet* instruction) {
6301 FieldAccessCallingConventionMIPS calling_convention;
6302 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6303 instruction->GetFieldType(),
6304 calling_convention);
6305}
6306
6307void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6308 HUnresolvedInstanceFieldSet* instruction) {
6309 FieldAccessCallingConventionMIPS calling_convention;
6310 codegen_->GenerateUnresolvedFieldAccess(instruction,
6311 instruction->GetFieldType(),
6312 instruction->GetFieldIndex(),
6313 instruction->GetDexPc(),
6314 calling_convention);
6315}
6316
6317void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6318 HUnresolvedStaticFieldGet* instruction) {
6319 FieldAccessCallingConventionMIPS calling_convention;
6320 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6321 instruction->GetFieldType(),
6322 calling_convention);
6323}
6324
6325void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6326 HUnresolvedStaticFieldGet* instruction) {
6327 FieldAccessCallingConventionMIPS calling_convention;
6328 codegen_->GenerateUnresolvedFieldAccess(instruction,
6329 instruction->GetFieldType(),
6330 instruction->GetFieldIndex(),
6331 instruction->GetDexPc(),
6332 calling_convention);
6333}
6334
6335void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6336 HUnresolvedStaticFieldSet* instruction) {
6337 FieldAccessCallingConventionMIPS calling_convention;
6338 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6339 instruction->GetFieldType(),
6340 calling_convention);
6341}
6342
6343void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6344 HUnresolvedStaticFieldSet* instruction) {
6345 FieldAccessCallingConventionMIPS calling_convention;
6346 codegen_->GenerateUnresolvedFieldAccess(instruction,
6347 instruction->GetFieldType(),
6348 instruction->GetFieldIndex(),
6349 instruction->GetDexPc(),
6350 calling_convention);
6351}
6352
6353void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006354 LocationSummary* locations =
6355 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006356 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006357}
6358
6359void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6360 HBasicBlock* block = instruction->GetBlock();
6361 if (block->GetLoopInformation() != nullptr) {
6362 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6363 // The back edge will generate the suspend check.
6364 return;
6365 }
6366 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6367 // The goto will generate the suspend check.
6368 return;
6369 }
6370 GenerateSuspendCheck(instruction, nullptr);
6371}
6372
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006373void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6374 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 InvokeRuntimeCallingConvention calling_convention;
6377 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6378}
6379
6380void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006381 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006382 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6383}
6384
6385void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6386 Primitive::Type input_type = conversion->GetInputType();
6387 Primitive::Type result_type = conversion->GetResultType();
6388 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006389 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006390
6391 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6392 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6393 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6394 }
6395
6396 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006397 if (!isR6 &&
6398 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6399 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006400 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006401 }
6402
6403 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6404
6405 if (call_kind == LocationSummary::kNoCall) {
6406 if (Primitive::IsFloatingPointType(input_type)) {
6407 locations->SetInAt(0, Location::RequiresFpuRegister());
6408 } else {
6409 locations->SetInAt(0, Location::RequiresRegister());
6410 }
6411
6412 if (Primitive::IsFloatingPointType(result_type)) {
6413 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6414 } else {
6415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6416 }
6417 } else {
6418 InvokeRuntimeCallingConvention calling_convention;
6419
6420 if (Primitive::IsFloatingPointType(input_type)) {
6421 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6422 } else {
6423 DCHECK_EQ(input_type, Primitive::kPrimLong);
6424 locations->SetInAt(0, Location::RegisterPairLocation(
6425 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6426 }
6427
6428 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6429 }
6430}
6431
6432void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6433 LocationSummary* locations = conversion->GetLocations();
6434 Primitive::Type result_type = conversion->GetResultType();
6435 Primitive::Type input_type = conversion->GetInputType();
6436 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006437 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006438
6439 DCHECK_NE(input_type, result_type);
6440
6441 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6442 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6443 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6444 Register src = locations->InAt(0).AsRegister<Register>();
6445
Alexey Frunzea871ef12016-06-27 15:20:11 -07006446 if (dst_low != src) {
6447 __ Move(dst_low, src);
6448 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006449 __ Sra(dst_high, src, 31);
6450 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6451 Register dst = locations->Out().AsRegister<Register>();
6452 Register src = (input_type == Primitive::kPrimLong)
6453 ? locations->InAt(0).AsRegisterPairLow<Register>()
6454 : locations->InAt(0).AsRegister<Register>();
6455
6456 switch (result_type) {
6457 case Primitive::kPrimChar:
6458 __ Andi(dst, src, 0xFFFF);
6459 break;
6460 case Primitive::kPrimByte:
6461 if (has_sign_extension) {
6462 __ Seb(dst, src);
6463 } else {
6464 __ Sll(dst, src, 24);
6465 __ Sra(dst, dst, 24);
6466 }
6467 break;
6468 case Primitive::kPrimShort:
6469 if (has_sign_extension) {
6470 __ Seh(dst, src);
6471 } else {
6472 __ Sll(dst, src, 16);
6473 __ Sra(dst, dst, 16);
6474 }
6475 break;
6476 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006477 if (dst != src) {
6478 __ Move(dst, src);
6479 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 break;
6481
6482 default:
6483 LOG(FATAL) << "Unexpected type conversion from " << input_type
6484 << " to " << result_type;
6485 }
6486 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006487 if (input_type == Primitive::kPrimLong) {
6488 if (isR6) {
6489 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6490 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6491 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6492 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6493 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6494 __ Mtc1(src_low, FTMP);
6495 __ Mthc1(src_high, FTMP);
6496 if (result_type == Primitive::kPrimFloat) {
6497 __ Cvtsl(dst, FTMP);
6498 } else {
6499 __ Cvtdl(dst, FTMP);
6500 }
6501 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006502 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6503 : kQuickL2d;
6504 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006505 if (result_type == Primitive::kPrimFloat) {
6506 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6507 } else {
6508 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6509 }
6510 }
6511 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006512 Register src = locations->InAt(0).AsRegister<Register>();
6513 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6514 __ Mtc1(src, FTMP);
6515 if (result_type == Primitive::kPrimFloat) {
6516 __ Cvtsw(dst, FTMP);
6517 } else {
6518 __ Cvtdw(dst, FTMP);
6519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 }
6521 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6522 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006523 if (result_type == Primitive::kPrimLong) {
6524 if (isR6) {
6525 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6526 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6527 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6528 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6529 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6530 MipsLabel truncate;
6531 MipsLabel done;
6532
6533 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6534 // value when the input is either a NaN or is outside of the range of the output type
6535 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6536 // the same result.
6537 //
6538 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6539 // value of the output type if the input is outside of the range after the truncation or
6540 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6541 // results. This matches the desired float/double-to-int/long conversion exactly.
6542 //
6543 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6544 //
6545 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6546 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6547 // even though it must be NAN2008=1 on R6.
6548 //
6549 // The code takes care of the different behaviors by first comparing the input to the
6550 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6551 // If the input is greater than or equal to the minimum, it procedes to the truncate
6552 // instruction, which will handle such an input the same way irrespective of NAN2008.
6553 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6554 // in order to return either zero or the minimum value.
6555 //
6556 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6557 // truncate instruction for MIPS64R6.
6558 if (input_type == Primitive::kPrimFloat) {
6559 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6560 __ LoadConst32(TMP, min_val);
6561 __ Mtc1(TMP, FTMP);
6562 __ CmpLeS(FTMP, FTMP, src);
6563 } else {
6564 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6565 __ LoadConst32(TMP, High32Bits(min_val));
6566 __ Mtc1(ZERO, FTMP);
6567 __ Mthc1(TMP, FTMP);
6568 __ CmpLeD(FTMP, FTMP, src);
6569 }
6570
6571 __ Bc1nez(FTMP, &truncate);
6572
6573 if (input_type == Primitive::kPrimFloat) {
6574 __ CmpEqS(FTMP, src, src);
6575 } else {
6576 __ CmpEqD(FTMP, src, src);
6577 }
6578 __ Move(dst_low, ZERO);
6579 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6580 __ Mfc1(TMP, FTMP);
6581 __ And(dst_high, dst_high, TMP);
6582
6583 __ B(&done);
6584
6585 __ Bind(&truncate);
6586
6587 if (input_type == Primitive::kPrimFloat) {
6588 __ TruncLS(FTMP, src);
6589 } else {
6590 __ TruncLD(FTMP, src);
6591 }
6592 __ Mfc1(dst_low, FTMP);
6593 __ Mfhc1(dst_high, FTMP);
6594
6595 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006596 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006597 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6598 : kQuickD2l;
6599 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006600 if (input_type == Primitive::kPrimFloat) {
6601 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6602 } else {
6603 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6604 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605 }
6606 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006607 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6608 Register dst = locations->Out().AsRegister<Register>();
6609 MipsLabel truncate;
6610 MipsLabel done;
6611
6612 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6613 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6614 // even though it must be NAN2008=1 on R6.
6615 //
6616 // For details see the large comment above for the truncation of float/double to long on R6.
6617 //
6618 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6619 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006620 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006621 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6622 __ LoadConst32(TMP, min_val);
6623 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006624 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006625 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6626 __ LoadConst32(TMP, High32Bits(min_val));
6627 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006628 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006630
6631 if (isR6) {
6632 if (input_type == Primitive::kPrimFloat) {
6633 __ CmpLeS(FTMP, FTMP, src);
6634 } else {
6635 __ CmpLeD(FTMP, FTMP, src);
6636 }
6637 __ Bc1nez(FTMP, &truncate);
6638
6639 if (input_type == Primitive::kPrimFloat) {
6640 __ CmpEqS(FTMP, src, src);
6641 } else {
6642 __ CmpEqD(FTMP, src, src);
6643 }
6644 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6645 __ Mfc1(TMP, FTMP);
6646 __ And(dst, dst, TMP);
6647 } else {
6648 if (input_type == Primitive::kPrimFloat) {
6649 __ ColeS(0, FTMP, src);
6650 } else {
6651 __ ColeD(0, FTMP, src);
6652 }
6653 __ Bc1t(0, &truncate);
6654
6655 if (input_type == Primitive::kPrimFloat) {
6656 __ CeqS(0, src, src);
6657 } else {
6658 __ CeqD(0, src, src);
6659 }
6660 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6661 __ Movf(dst, ZERO, 0);
6662 }
6663
6664 __ B(&done);
6665
6666 __ Bind(&truncate);
6667
6668 if (input_type == Primitive::kPrimFloat) {
6669 __ TruncWS(FTMP, src);
6670 } else {
6671 __ TruncWD(FTMP, src);
6672 }
6673 __ Mfc1(dst, FTMP);
6674
6675 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006676 }
6677 } else if (Primitive::IsFloatingPointType(result_type) &&
6678 Primitive::IsFloatingPointType(input_type)) {
6679 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6680 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6681 if (result_type == Primitive::kPrimFloat) {
6682 __ Cvtsd(dst, src);
6683 } else {
6684 __ Cvtds(dst, src);
6685 }
6686 } else {
6687 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6688 << " to " << result_type;
6689 }
6690}
6691
6692void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6693 HandleShift(ushr);
6694}
6695
6696void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6697 HandleShift(ushr);
6698}
6699
6700void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6701 HandleBinaryOp(instruction);
6702}
6703
6704void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6705 HandleBinaryOp(instruction);
6706}
6707
6708void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6709 // Nothing to do, this should be removed during prepare for register allocator.
6710 LOG(FATAL) << "Unreachable";
6711}
6712
6713void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6714 // Nothing to do, this should be removed during prepare for register allocator.
6715 LOG(FATAL) << "Unreachable";
6716}
6717
6718void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006719 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720}
6721
6722void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006723 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724}
6725
6726void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006727 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006728}
6729
6730void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006731 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732}
6733
6734void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006735 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006736}
6737
6738void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006739 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006740}
6741
6742void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006743 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744}
6745
6746void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006747 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006748}
6749
6750void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006751 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006752}
6753
6754void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006755 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006756}
6757
6758void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006759 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006760}
6761
6762void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006763 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006764}
6765
6766void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006767 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006768}
6769
6770void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006771 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006772}
6773
6774void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006775 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006776}
6777
6778void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006779 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006780}
6781
6782void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006783 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006784}
6785
6786void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788}
6789
6790void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006792}
6793
6794void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006796}
6797
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006798void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6799 LocationSummary* locations =
6800 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6801 locations->SetInAt(0, Location::RequiresRegister());
6802}
6803
Alexey Frunze96b66822016-09-10 02:32:44 -07006804void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6805 int32_t lower_bound,
6806 uint32_t num_entries,
6807 HBasicBlock* switch_block,
6808 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006809 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006810 Register temp_reg = TMP;
6811 __ Addiu32(temp_reg, value_reg, -lower_bound);
6812 // Jump to default if index is negative
6813 // Note: We don't check the case that index is positive while value < lower_bound, because in
6814 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6815 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6816
Alexey Frunze96b66822016-09-10 02:32:44 -07006817 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006818 // Jump to successors[0] if value == lower_bound.
6819 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6820 int32_t last_index = 0;
6821 for (; num_entries - last_index > 2; last_index += 2) {
6822 __ Addiu(temp_reg, temp_reg, -2);
6823 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6824 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6825 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6826 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6827 }
6828 if (num_entries - last_index == 2) {
6829 // The last missing case_value.
6830 __ Addiu(temp_reg, temp_reg, -1);
6831 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006832 }
6833
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006834 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006835 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006836 __ B(codegen_->GetLabelOf(default_block));
6837 }
6838}
6839
Alexey Frunze96b66822016-09-10 02:32:44 -07006840void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6841 Register constant_area,
6842 int32_t lower_bound,
6843 uint32_t num_entries,
6844 HBasicBlock* switch_block,
6845 HBasicBlock* default_block) {
6846 // Create a jump table.
6847 std::vector<MipsLabel*> labels(num_entries);
6848 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6849 for (uint32_t i = 0; i < num_entries; i++) {
6850 labels[i] = codegen_->GetLabelOf(successors[i]);
6851 }
6852 JumpTable* table = __ CreateJumpTable(std::move(labels));
6853
6854 // Is the value in range?
6855 __ Addiu32(TMP, value_reg, -lower_bound);
6856 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6857 __ Sltiu(AT, TMP, num_entries);
6858 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6859 } else {
6860 __ LoadConst32(AT, num_entries);
6861 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6862 }
6863
6864 // We are in the range of the table.
6865 // Load the target address from the jump table, indexing by the value.
6866 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6867 __ Sll(TMP, TMP, 2);
6868 __ Addu(TMP, TMP, AT);
6869 __ Lw(TMP, TMP, 0);
6870 // Compute the absolute target address by adding the table start address
6871 // (the table contains offsets to targets relative to its start).
6872 __ Addu(TMP, TMP, AT);
6873 // And jump.
6874 __ Jr(TMP);
6875 __ NopIfNoReordering();
6876}
6877
6878void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6879 int32_t lower_bound = switch_instr->GetStartValue();
6880 uint32_t num_entries = switch_instr->GetNumEntries();
6881 LocationSummary* locations = switch_instr->GetLocations();
6882 Register value_reg = locations->InAt(0).AsRegister<Register>();
6883 HBasicBlock* switch_block = switch_instr->GetBlock();
6884 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6885
6886 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6887 num_entries > kPackedSwitchJumpTableThreshold) {
6888 // R6 uses PC-relative addressing to access the jump table.
6889 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6890 // the jump table and it is implemented by changing HPackedSwitch to
6891 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6892 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6893 GenTableBasedPackedSwitch(value_reg,
6894 ZERO,
6895 lower_bound,
6896 num_entries,
6897 switch_block,
6898 default_block);
6899 } else {
6900 GenPackedSwitchWithCompares(value_reg,
6901 lower_bound,
6902 num_entries,
6903 switch_block,
6904 default_block);
6905 }
6906}
6907
6908void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6909 LocationSummary* locations =
6910 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6911 locations->SetInAt(0, Location::RequiresRegister());
6912 // Constant area pointer (HMipsComputeBaseMethodAddress).
6913 locations->SetInAt(1, Location::RequiresRegister());
6914}
6915
6916void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6917 int32_t lower_bound = switch_instr->GetStartValue();
6918 uint32_t num_entries = switch_instr->GetNumEntries();
6919 LocationSummary* locations = switch_instr->GetLocations();
6920 Register value_reg = locations->InAt(0).AsRegister<Register>();
6921 Register constant_area = locations->InAt(1).AsRegister<Register>();
6922 HBasicBlock* switch_block = switch_instr->GetBlock();
6923 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6924
6925 // This is an R2-only path. HPackedSwitch has been changed to
6926 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6927 // required to address the jump table relative to PC.
6928 GenTableBasedPackedSwitch(value_reg,
6929 constant_area,
6930 lower_bound,
6931 num_entries,
6932 switch_block,
6933 default_block);
6934}
6935
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006936void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6937 HMipsComputeBaseMethodAddress* insn) {
6938 LocationSummary* locations =
6939 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6940 locations->SetOut(Location::RequiresRegister());
6941}
6942
6943void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6944 HMipsComputeBaseMethodAddress* insn) {
6945 LocationSummary* locations = insn->GetLocations();
6946 Register reg = locations->Out().AsRegister<Register>();
6947
6948 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6949
6950 // Generate a dummy PC-relative call to obtain PC.
6951 __ Nal();
6952 // Grab the return address off RA.
6953 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006954 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006955
6956 // Remember this offset (the obtained PC value) for later use with constant area.
6957 __ BindPcRelBaseLabel();
6958}
6959
6960void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6962 locations->SetOut(Location::RequiresRegister());
6963}
6964
6965void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6966 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6967 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6968 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006969 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6970 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006971}
6972
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006973void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6974 // The trampoline uses the same calling convention as dex calling conventions,
6975 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6976 // the method_idx.
6977 HandleInvoke(invoke);
6978}
6979
6980void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6981 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6982}
6983
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006984void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6985 LocationSummary* locations =
6986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6987 locations->SetInAt(0, Location::RequiresRegister());
6988 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006989}
6990
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006991void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6992 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006993 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006994 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006995 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006996 __ LoadFromOffset(kLoadWord,
6997 locations->Out().AsRegister<Register>(),
6998 locations->InAt(0).AsRegister<Register>(),
6999 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007000 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007001 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007002 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007003 __ LoadFromOffset(kLoadWord,
7004 locations->Out().AsRegister<Register>(),
7005 locations->InAt(0).AsRegister<Register>(),
7006 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007007 __ LoadFromOffset(kLoadWord,
7008 locations->Out().AsRegister<Register>(),
7009 locations->Out().AsRegister<Register>(),
7010 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00007011 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007012}
7013
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007014#undef __
7015#undef QUICK_ENTRY_POINT
7016
7017} // namespace mips
7018} // namespace art