blob: c5029b39020700aa9557043c0b47230bb89beb48 [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();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800284 const uint32_t string_index = load->GetStringIndex().index_;
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 Frunze06a46c42016-07-19 15:00:40 -0700461 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
462 boot_image_string_patches_(StringReferenceValueComparator(),
463 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
464 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
465 boot_image_type_patches_(TypeReferenceValueComparator(),
466 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
467 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
468 boot_image_address_patches_(std::less<uint32_t>(),
469 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
470 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200471 // Save RA (containing the return address) to mimic Quick.
472 AddAllocatedRegister(Location::RegisterLocation(RA));
473}
474
475#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100476// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
477#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700478#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200479
480void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
481 // Ensure that we fix up branches.
482 __ FinalizeCode();
483
484 // Adjust native pc offsets in stack maps.
485 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
486 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
487 uint32_t new_position = __ GetAdjustedPosition(old_position);
488 DCHECK_GE(new_position, old_position);
489 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
490 }
491
492 // Adjust pc offsets for the disassembly information.
493 if (disasm_info_ != nullptr) {
494 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
495 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
496 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
497 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
498 it.second.start = __ GetAdjustedPosition(it.second.start);
499 it.second.end = __ GetAdjustedPosition(it.second.end);
500 }
501 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
502 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
503 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
504 }
505 }
506
507 CodeGenerator::Finalize(allocator);
508}
509
510MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
511 return codegen_->GetAssembler();
512}
513
514void ParallelMoveResolverMIPS::EmitMove(size_t index) {
515 DCHECK_LT(index, moves_.size());
516 MoveOperands* move = moves_[index];
517 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
518}
519
520void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
521 DCHECK_LT(index, moves_.size());
522 MoveOperands* move = moves_[index];
523 Primitive::Type type = move->GetType();
524 Location loc1 = move->GetDestination();
525 Location loc2 = move->GetSource();
526
527 DCHECK(!loc1.IsConstant());
528 DCHECK(!loc2.IsConstant());
529
530 if (loc1.Equals(loc2)) {
531 return;
532 }
533
534 if (loc1.IsRegister() && loc2.IsRegister()) {
535 // Swap 2 GPRs.
536 Register r1 = loc1.AsRegister<Register>();
537 Register r2 = loc2.AsRegister<Register>();
538 __ Move(TMP, r2);
539 __ Move(r2, r1);
540 __ Move(r1, TMP);
541 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
542 FRegister f1 = loc1.AsFpuRegister<FRegister>();
543 FRegister f2 = loc2.AsFpuRegister<FRegister>();
544 if (type == Primitive::kPrimFloat) {
545 __ MovS(FTMP, f2);
546 __ MovS(f2, f1);
547 __ MovS(f1, FTMP);
548 } else {
549 DCHECK_EQ(type, Primitive::kPrimDouble);
550 __ MovD(FTMP, f2);
551 __ MovD(f2, f1);
552 __ MovD(f1, FTMP);
553 }
554 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
555 (loc1.IsFpuRegister() && loc2.IsRegister())) {
556 // Swap FPR and GPR.
557 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
558 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
559 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200560 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200561 __ Move(TMP, r2);
562 __ Mfc1(r2, f1);
563 __ Mtc1(TMP, f1);
564 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
565 // Swap 2 GPR register pairs.
566 Register r1 = loc1.AsRegisterPairLow<Register>();
567 Register r2 = loc2.AsRegisterPairLow<Register>();
568 __ Move(TMP, r2);
569 __ Move(r2, r1);
570 __ Move(r1, TMP);
571 r1 = loc1.AsRegisterPairHigh<Register>();
572 r2 = loc2.AsRegisterPairHigh<Register>();
573 __ Move(TMP, r2);
574 __ Move(r2, r1);
575 __ Move(r1, TMP);
576 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
577 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
578 // Swap FPR and GPR register pair.
579 DCHECK_EQ(type, Primitive::kPrimDouble);
580 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
581 : loc2.AsFpuRegister<FRegister>();
582 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
583 : loc2.AsRegisterPairLow<Register>();
584 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
585 : loc2.AsRegisterPairHigh<Register>();
586 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
587 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
588 // unpredictable and the following mfch1 will fail.
589 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800590 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200591 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800592 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200593 __ Move(r2_l, TMP);
594 __ Move(r2_h, AT);
595 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
596 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
597 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
598 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +0000599 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
600 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200601 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
602 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000603 __ Move(TMP, reg);
604 __ LoadFromOffset(kLoadWord, reg, SP, offset);
605 __ StoreToOffset(kStoreWord, TMP, SP, offset);
606 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
607 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
608 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
609 : loc2.AsRegisterPairLow<Register>();
610 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
611 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200612 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +0000613 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
614 : loc2.GetHighStackIndex(kMipsWordSize);
615 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000616 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +0000617 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +0000618 __ Move(TMP, reg_h);
619 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
620 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +0200621 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
622 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
623 : loc2.AsFpuRegister<FRegister>();
624 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
625 if (type == Primitive::kPrimFloat) {
626 __ MovS(FTMP, reg);
627 __ LoadSFromOffset(reg, SP, offset);
628 __ StoreSToOffset(FTMP, SP, offset);
629 } else {
630 DCHECK_EQ(type, Primitive::kPrimDouble);
631 __ MovD(FTMP, reg);
632 __ LoadDFromOffset(reg, SP, offset);
633 __ StoreDToOffset(FTMP, SP, offset);
634 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200635 } else {
636 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
637 }
638}
639
640void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
641 __ Pop(static_cast<Register>(reg));
642}
643
644void ParallelMoveResolverMIPS::SpillScratch(int reg) {
645 __ Push(static_cast<Register>(reg));
646}
647
648void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
649 // Allocate a scratch register other than TMP, if available.
650 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
651 // automatically unspilled when the scratch scope object is destroyed).
652 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
653 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
654 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
655 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
656 __ LoadFromOffset(kLoadWord,
657 Register(ensure_scratch.GetRegister()),
658 SP,
659 index1 + stack_offset);
660 __ LoadFromOffset(kLoadWord,
661 TMP,
662 SP,
663 index2 + stack_offset);
664 __ StoreToOffset(kStoreWord,
665 Register(ensure_scratch.GetRegister()),
666 SP,
667 index2 + stack_offset);
668 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
669 }
670}
671
Alexey Frunze73296a72016-06-03 22:51:46 -0700672void CodeGeneratorMIPS::ComputeSpillMask() {
673 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
674 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
675 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
676 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
677 // registers, include the ZERO register to force alignment of FPU callee-saved registers
678 // within the stack frame.
679 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
680 core_spill_mask_ |= (1 << ZERO);
681 }
Alexey Frunze58320ce2016-08-30 21:40:46 -0700682}
683
684bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -0700685 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -0700686 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
687 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
688 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze06a46c42016-07-19 15:00:40 -0700689 // TODO: Can this be improved? It causes creation of a stack frame (while RA might be
690 // saved in an unused temporary register) and saving of RA and the current method pointer
691 // in the frame.
Alexey Frunze58320ce2016-08-30 21:40:46 -0700692 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -0700693}
694
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200695static dwarf::Reg DWARFReg(Register reg) {
696 return dwarf::Reg::MipsCore(static_cast<int>(reg));
697}
698
699// TODO: mapping of floating-point registers to DWARF.
700
701void CodeGeneratorMIPS::GenerateFrameEntry() {
702 __ Bind(&frame_entry_label_);
703
704 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
705
706 if (do_overflow_check) {
707 __ LoadFromOffset(kLoadWord,
708 ZERO,
709 SP,
710 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
711 RecordPcInfo(nullptr, 0);
712 }
713
714 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -0700715 CHECK_EQ(fpu_spill_mask_, 0u);
716 CHECK_EQ(core_spill_mask_, 1u << RA);
717 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200718 return;
719 }
720
721 // Make sure the frame size isn't unreasonably large.
722 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
723 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
724 }
725
726 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200727
Alexey Frunze73296a72016-06-03 22:51:46 -0700728 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200729 __ IncreaseFrameSize(ofs);
730
Alexey Frunze73296a72016-06-03 22:51:46 -0700731 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
732 Register reg = static_cast<Register>(MostSignificantBit(mask));
733 mask ^= 1u << reg;
734 ofs -= kMipsWordSize;
735 // The ZERO register is only included for alignment.
736 if (reg != ZERO) {
737 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200738 __ cfi().RelOffset(DWARFReg(reg), ofs);
739 }
740 }
741
Alexey Frunze73296a72016-06-03 22:51:46 -0700742 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
743 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
744 mask ^= 1u << reg;
745 ofs -= kMipsDoublewordSize;
746 __ StoreDToOffset(reg, SP, ofs);
747 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200748 }
749
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +0100750 // Save the current method if we need it. Note that we do not
751 // do this in HCurrentMethod, as the instruction might have been removed
752 // in the SSA graph.
753 if (RequiresCurrentMethod()) {
754 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
755 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +0100756
757 if (GetGraph()->HasShouldDeoptimizeFlag()) {
758 // Initialize should deoptimize flag to 0.
759 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
760 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200761}
762
763void CodeGeneratorMIPS::GenerateFrameExit() {
764 __ cfi().RememberState();
765
766 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200767 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200768
Alexey Frunze73296a72016-06-03 22:51:46 -0700769 // For better instruction scheduling restore RA before other registers.
770 uint32_t ofs = GetFrameSize();
771 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
772 Register reg = static_cast<Register>(MostSignificantBit(mask));
773 mask ^= 1u << reg;
774 ofs -= kMipsWordSize;
775 // The ZERO register is only included for alignment.
776 if (reg != ZERO) {
777 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200778 __ cfi().Restore(DWARFReg(reg));
779 }
780 }
781
Alexey Frunze73296a72016-06-03 22:51:46 -0700782 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
783 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
784 mask ^= 1u << reg;
785 ofs -= kMipsDoublewordSize;
786 __ LoadDFromOffset(reg, SP, ofs);
787 // TODO: __ cfi().Restore(DWARFReg(reg));
788 }
789
Alexey Frunze57eb0f52016-07-29 22:04:46 -0700790 size_t frame_size = GetFrameSize();
791 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
792 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
793 bool reordering = __ SetReorder(false);
794 if (exchange) {
795 __ Jr(RA);
796 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
797 } else {
798 __ DecreaseFrameSize(frame_size);
799 __ Jr(RA);
800 __ Nop(); // In delay slot.
801 }
802 __ SetReorder(reordering);
803 } else {
804 __ Jr(RA);
805 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200806 }
807
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200808 __ cfi().RestoreState();
809 __ cfi().DefCFAOffset(GetFrameSize());
810}
811
812void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
813 __ Bind(GetLabelOf(block));
814}
815
816void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
817 if (src.Equals(dst)) {
818 return;
819 }
820
821 if (src.IsConstant()) {
822 MoveConstant(dst, src.GetConstant());
823 } else {
824 if (Primitive::Is64BitType(dst_type)) {
825 Move64(dst, src);
826 } else {
827 Move32(dst, src);
828 }
829 }
830}
831
832void CodeGeneratorMIPS::Move32(Location destination, Location source) {
833 if (source.Equals(destination)) {
834 return;
835 }
836
837 if (destination.IsRegister()) {
838 if (source.IsRegister()) {
839 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
840 } else if (source.IsFpuRegister()) {
841 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
842 } else {
843 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
844 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
845 }
846 } else if (destination.IsFpuRegister()) {
847 if (source.IsRegister()) {
848 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
849 } else if (source.IsFpuRegister()) {
850 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
851 } else {
852 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
853 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
854 }
855 } else {
856 DCHECK(destination.IsStackSlot()) << destination;
857 if (source.IsRegister()) {
858 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
859 } else if (source.IsFpuRegister()) {
860 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
861 } else {
862 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
863 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
864 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
865 }
866 }
867}
868
869void CodeGeneratorMIPS::Move64(Location destination, Location source) {
870 if (source.Equals(destination)) {
871 return;
872 }
873
874 if (destination.IsRegisterPair()) {
875 if (source.IsRegisterPair()) {
876 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
877 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
878 } else if (source.IsFpuRegister()) {
879 Register dst_high = destination.AsRegisterPairHigh<Register>();
880 Register dst_low = destination.AsRegisterPairLow<Register>();
881 FRegister src = source.AsFpuRegister<FRegister>();
882 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800883 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200884 } else {
885 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
886 int32_t off = source.GetStackIndex();
887 Register r = destination.AsRegisterPairLow<Register>();
888 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
889 }
890 } else if (destination.IsFpuRegister()) {
891 if (source.IsRegisterPair()) {
892 FRegister dst = destination.AsFpuRegister<FRegister>();
893 Register src_high = source.AsRegisterPairHigh<Register>();
894 Register src_low = source.AsRegisterPairLow<Register>();
895 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -0800896 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200897 } else if (source.IsFpuRegister()) {
898 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
899 } else {
900 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
901 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
902 }
903 } else {
904 DCHECK(destination.IsDoubleStackSlot()) << destination;
905 int32_t off = destination.GetStackIndex();
906 if (source.IsRegisterPair()) {
907 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
908 } else if (source.IsFpuRegister()) {
909 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
910 } else {
911 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
912 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
913 __ StoreToOffset(kStoreWord, TMP, SP, off);
914 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
915 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
916 }
917 }
918}
919
920void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
921 if (c->IsIntConstant() || c->IsNullConstant()) {
922 // Move 32 bit constant.
923 int32_t value = GetInt32ValueOf(c);
924 if (destination.IsRegister()) {
925 Register dst = destination.AsRegister<Register>();
926 __ LoadConst32(dst, value);
927 } else {
928 DCHECK(destination.IsStackSlot())
929 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700930 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200931 }
932 } else if (c->IsLongConstant()) {
933 // Move 64 bit constant.
934 int64_t value = GetInt64ValueOf(c);
935 if (destination.IsRegisterPair()) {
936 Register r_h = destination.AsRegisterPairHigh<Register>();
937 Register r_l = destination.AsRegisterPairLow<Register>();
938 __ LoadConst64(r_h, r_l, value);
939 } else {
940 DCHECK(destination.IsDoubleStackSlot())
941 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700942 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200943 }
944 } else if (c->IsFloatConstant()) {
945 // Move 32 bit float constant.
946 int32_t value = GetInt32ValueOf(c);
947 if (destination.IsFpuRegister()) {
948 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
949 } else {
950 DCHECK(destination.IsStackSlot())
951 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700952 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200953 }
954 } else {
955 // Move 64 bit double constant.
956 DCHECK(c->IsDoubleConstant()) << c->DebugName();
957 int64_t value = GetInt64ValueOf(c);
958 if (destination.IsFpuRegister()) {
959 FRegister fd = destination.AsFpuRegister<FRegister>();
960 __ LoadDConst64(fd, value, TMP);
961 } else {
962 DCHECK(destination.IsDoubleStackSlot())
963 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -0700964 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200965 }
966 }
967}
968
969void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
970 DCHECK(destination.IsRegister());
971 Register dst = destination.AsRegister<Register>();
972 __ LoadConst32(dst, value);
973}
974
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200975void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
976 if (location.IsRegister()) {
977 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -0700978 } else if (location.IsRegisterPair()) {
979 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
980 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200981 } else {
982 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
983 }
984}
985
Vladimir Markoaad75c62016-10-03 08:46:48 +0000986template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
987inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
988 const ArenaDeque<PcRelativePatchInfo>& infos,
989 ArenaVector<LinkerPatch>* linker_patches) {
990 for (const PcRelativePatchInfo& info : infos) {
991 const DexFile& dex_file = info.target_dex_file;
992 size_t offset_or_index = info.offset_or_index;
993 DCHECK(info.high_label.IsBound());
994 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
995 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
996 // the assembler's base label used for PC-relative addressing.
997 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
998 ? __ GetLabelLocation(&info.pc_rel_label)
999 : __ GetPcRelBaseLabelLocation();
1000 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1001 }
1002}
1003
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001004void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1005 DCHECK(linker_patches->empty());
1006 size_t size =
Alexey Frunze06a46c42016-07-19 15:00:40 -07001007 pc_relative_dex_cache_patches_.size() +
1008 pc_relative_string_patches_.size() +
1009 pc_relative_type_patches_.size() +
1010 boot_image_string_patches_.size() +
1011 boot_image_type_patches_.size() +
1012 boot_image_address_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001014 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
1015 linker_patches);
1016 if (!GetCompilerOptions().IsBootImage()) {
1017 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1018 linker_patches);
1019 } else {
1020 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1021 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001022 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00001023 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1024 linker_patches);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001025 for (const auto& entry : boot_image_string_patches_) {
1026 const StringReference& target_string = entry.first;
1027 Literal* literal = entry.second;
1028 DCHECK(literal->GetLabel()->IsBound());
1029 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1030 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
1031 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001032 target_string.string_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001033 }
1034 for (const auto& entry : boot_image_type_patches_) {
1035 const TypeReference& target_type = entry.first;
1036 Literal* literal = entry.second;
1037 DCHECK(literal->GetLabel()->IsBound());
1038 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1039 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
1040 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001041 target_type.type_index.index_));
Alexey Frunze06a46c42016-07-19 15:00:40 -07001042 }
1043 for (const auto& entry : boot_image_address_patches_) {
1044 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
1045 Literal* literal = entry.second;
1046 DCHECK(literal->GetLabel()->IsBound());
1047 uint32_t literal_offset = __ GetLabelLocation(literal->GetLabel());
1048 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
1049 }
1050}
1051
1052CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1053 const DexFile& dex_file, uint32_t string_index) {
1054 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
1055}
1056
1057CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001058 const DexFile& dex_file, dex::TypeIndex type_index) {
1059 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001060}
1061
1062CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeDexCacheArrayPatch(
1063 const DexFile& dex_file, uint32_t element_offset) {
1064 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
1065}
1066
1067CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1068 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1069 patches->emplace_back(dex_file, offset_or_index);
1070 return &patches->back();
1071}
1072
Alexey Frunze06a46c42016-07-19 15:00:40 -07001073Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1074 return map->GetOrCreate(
1075 value,
1076 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1077}
1078
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001079Literal* CodeGeneratorMIPS::DeduplicateMethodLiteral(MethodReference target_method,
1080 MethodToLiteralMap* map) {
1081 return map->GetOrCreate(
1082 target_method,
1083 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1084}
1085
Alexey Frunze06a46c42016-07-19 15:00:40 -07001086Literal* CodeGeneratorMIPS::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001087 dex::StringIndex string_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001088 return boot_image_string_patches_.GetOrCreate(
1089 StringReference(&dex_file, string_index),
1090 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1091}
1092
1093Literal* CodeGeneratorMIPS::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08001094 dex::TypeIndex type_index) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001095 return boot_image_type_patches_.GetOrCreate(
1096 TypeReference(&dex_file, type_index),
1097 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
1098}
1099
1100Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
1101 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
1102 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
1103 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
1104}
1105
Vladimir Markoaad75c62016-10-03 08:46:48 +00001106void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholder(
1107 PcRelativePatchInfo* info, Register out, Register base) {
1108 bool reordering = __ SetReorder(false);
1109 if (GetInstructionSetFeatures().IsR6()) {
1110 DCHECK_EQ(base, ZERO);
1111 __ Bind(&info->high_label);
1112 __ Bind(&info->pc_rel_label);
1113 // Add a 32-bit offset to PC.
1114 __ Auipc(out, /* placeholder */ 0x1234);
1115 __ Addiu(out, out, /* placeholder */ 0x5678);
1116 } else {
1117 // If base is ZERO, emit NAL to obtain the actual base.
1118 if (base == ZERO) {
1119 // Generate a dummy PC-relative call to obtain PC.
1120 __ Nal();
1121 }
1122 __ Bind(&info->high_label);
1123 __ Lui(out, /* placeholder */ 0x1234);
1124 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1125 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1126 if (base == ZERO) {
1127 __ Bind(&info->pc_rel_label);
1128 }
1129 __ Ori(out, out, /* placeholder */ 0x5678);
1130 // Add a 32-bit offset to PC.
1131 __ Addu(out, out, (base == ZERO) ? RA : base);
1132 }
1133 __ SetReorder(reordering);
1134}
1135
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001136void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1137 MipsLabel done;
1138 Register card = AT;
1139 Register temp = TMP;
1140 __ Beqz(value, &done);
1141 __ LoadFromOffset(kLoadWord,
1142 card,
1143 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001144 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001145 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1146 __ Addu(temp, card, temp);
1147 __ Sb(card, temp, 0);
1148 __ Bind(&done);
1149}
1150
David Brazdil58282f42016-01-14 12:45:10 +00001151void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001152 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1153 blocked_core_registers_[ZERO] = true;
1154 blocked_core_registers_[K0] = true;
1155 blocked_core_registers_[K1] = true;
1156 blocked_core_registers_[GP] = true;
1157 blocked_core_registers_[SP] = true;
1158 blocked_core_registers_[RA] = true;
1159
1160 // AT and TMP(T8) are used as temporary/scratch registers
1161 // (similar to how AT is used by MIPS assemblers).
1162 blocked_core_registers_[AT] = true;
1163 blocked_core_registers_[TMP] = true;
1164 blocked_fpu_registers_[FTMP] = true;
1165
1166 // Reserve suspend and thread registers.
1167 blocked_core_registers_[S0] = true;
1168 blocked_core_registers_[TR] = true;
1169
1170 // Reserve T9 for function calls
1171 blocked_core_registers_[T9] = true;
1172
1173 // Reserve odd-numbered FPU registers.
1174 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1175 blocked_fpu_registers_[i] = true;
1176 }
1177
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001178 if (GetGraph()->IsDebuggable()) {
1179 // Stubs do not save callee-save floating point registers. If the graph
1180 // is debuggable, we need to deal with these registers differently. For
1181 // now, just block them.
1182 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1183 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1184 }
1185 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001186}
1187
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001188size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1189 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1190 return kMipsWordSize;
1191}
1192
1193size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1194 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1195 return kMipsWordSize;
1196}
1197
1198size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1199 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1200 return kMipsDoublewordSize;
1201}
1202
1203size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1204 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1205 return kMipsDoublewordSize;
1206}
1207
1208void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001209 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210}
1211
1212void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001213 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001214}
1215
Serban Constantinescufca16662016-07-14 09:21:59 +01001216constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1217
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1219 HInstruction* instruction,
1220 uint32_t dex_pc,
1221 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001222 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001223 bool reordering = __ SetReorder(false);
Serban Constantinescufca16662016-07-14 09:21:59 +01001224 __ LoadFromOffset(kLoadWord, T9, TR, GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001225 __ Jalr(T9);
Serban Constantinescufca16662016-07-14 09:21:59 +01001226 if (IsDirectEntrypoint(entrypoint)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001227 // Reserve argument space on stack (for $a0-$a3) for
1228 // entrypoints that directly reference native implementations.
1229 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001230 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001231 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001232 } else {
1233 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001234 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001235 __ SetReorder(reordering);
Serban Constantinescufca16662016-07-14 09:21:59 +01001236 if (EntrypointRequiresStackMap(entrypoint)) {
1237 RecordPcInfo(instruction, dex_pc, slow_path);
1238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001239}
1240
1241void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1242 Register class_reg) {
1243 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1244 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1245 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1246 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1247 __ Sync(0);
1248 __ Bind(slow_path->GetExitLabel());
1249}
1250
1251void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1252 __ Sync(0); // Only stype 0 is supported.
1253}
1254
1255void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1256 HBasicBlock* successor) {
1257 SuspendCheckSlowPathMIPS* slow_path =
1258 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1259 codegen_->AddSlowPath(slow_path);
1260
1261 __ LoadFromOffset(kLoadUnsignedHalfword,
1262 TMP,
1263 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001264 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001265 if (successor == nullptr) {
1266 __ Bnez(TMP, slow_path->GetEntryLabel());
1267 __ Bind(slow_path->GetReturnLabel());
1268 } else {
1269 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1270 __ B(slow_path->GetEntryLabel());
1271 // slow_path will return to GetLabelOf(successor).
1272 }
1273}
1274
1275InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1276 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001277 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001278 assembler_(codegen->GetAssembler()),
1279 codegen_(codegen) {}
1280
1281void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1282 DCHECK_EQ(instruction->InputCount(), 2U);
1283 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1284 Primitive::Type type = instruction->GetResultType();
1285 switch (type) {
1286 case Primitive::kPrimInt: {
1287 locations->SetInAt(0, Location::RequiresRegister());
1288 HInstruction* right = instruction->InputAt(1);
1289 bool can_use_imm = false;
1290 if (right->IsConstant()) {
1291 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1292 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1293 can_use_imm = IsUint<16>(imm);
1294 } else if (instruction->IsAdd()) {
1295 can_use_imm = IsInt<16>(imm);
1296 } else {
1297 DCHECK(instruction->IsSub());
1298 can_use_imm = IsInt<16>(-imm);
1299 }
1300 }
1301 if (can_use_imm)
1302 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1303 else
1304 locations->SetInAt(1, Location::RequiresRegister());
1305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1306 break;
1307 }
1308
1309 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001311 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001313 break;
1314 }
1315
1316 case Primitive::kPrimFloat:
1317 case Primitive::kPrimDouble:
1318 DCHECK(instruction->IsAdd() || instruction->IsSub());
1319 locations->SetInAt(0, Location::RequiresFpuRegister());
1320 locations->SetInAt(1, Location::RequiresFpuRegister());
1321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1322 break;
1323
1324 default:
1325 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1326 }
1327}
1328
1329void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1330 Primitive::Type type = instruction->GetType();
1331 LocationSummary* locations = instruction->GetLocations();
1332
1333 switch (type) {
1334 case Primitive::kPrimInt: {
1335 Register dst = locations->Out().AsRegister<Register>();
1336 Register lhs = locations->InAt(0).AsRegister<Register>();
1337 Location rhs_location = locations->InAt(1);
1338
1339 Register rhs_reg = ZERO;
1340 int32_t rhs_imm = 0;
1341 bool use_imm = rhs_location.IsConstant();
1342 if (use_imm) {
1343 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1344 } else {
1345 rhs_reg = rhs_location.AsRegister<Register>();
1346 }
1347
1348 if (instruction->IsAnd()) {
1349 if (use_imm)
1350 __ Andi(dst, lhs, rhs_imm);
1351 else
1352 __ And(dst, lhs, rhs_reg);
1353 } else if (instruction->IsOr()) {
1354 if (use_imm)
1355 __ Ori(dst, lhs, rhs_imm);
1356 else
1357 __ Or(dst, lhs, rhs_reg);
1358 } else if (instruction->IsXor()) {
1359 if (use_imm)
1360 __ Xori(dst, lhs, rhs_imm);
1361 else
1362 __ Xor(dst, lhs, rhs_reg);
1363 } else if (instruction->IsAdd()) {
1364 if (use_imm)
1365 __ Addiu(dst, lhs, rhs_imm);
1366 else
1367 __ Addu(dst, lhs, rhs_reg);
1368 } else {
1369 DCHECK(instruction->IsSub());
1370 if (use_imm)
1371 __ Addiu(dst, lhs, -rhs_imm);
1372 else
1373 __ Subu(dst, lhs, rhs_reg);
1374 }
1375 break;
1376 }
1377
1378 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1380 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1381 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1382 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001383 Location rhs_location = locations->InAt(1);
1384 bool use_imm = rhs_location.IsConstant();
1385 if (!use_imm) {
1386 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
1387 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
1388 if (instruction->IsAnd()) {
1389 __ And(dst_low, lhs_low, rhs_low);
1390 __ And(dst_high, lhs_high, rhs_high);
1391 } else if (instruction->IsOr()) {
1392 __ Or(dst_low, lhs_low, rhs_low);
1393 __ Or(dst_high, lhs_high, rhs_high);
1394 } else if (instruction->IsXor()) {
1395 __ Xor(dst_low, lhs_low, rhs_low);
1396 __ Xor(dst_high, lhs_high, rhs_high);
1397 } else if (instruction->IsAdd()) {
1398 if (lhs_low == rhs_low) {
1399 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
1400 __ Slt(TMP, lhs_low, ZERO);
1401 __ Addu(dst_low, lhs_low, rhs_low);
1402 } else {
1403 __ Addu(dst_low, lhs_low, rhs_low);
1404 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
1405 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
1406 }
1407 __ Addu(dst_high, lhs_high, rhs_high);
1408 __ Addu(dst_high, dst_high, TMP);
1409 } else {
1410 DCHECK(instruction->IsSub());
1411 __ Sltu(TMP, lhs_low, rhs_low);
1412 __ Subu(dst_low, lhs_low, rhs_low);
1413 __ Subu(dst_high, lhs_high, rhs_high);
1414 __ Subu(dst_high, dst_high, TMP);
1415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001416 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001417 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
1418 if (instruction->IsOr()) {
1419 uint32_t low = Low32Bits(value);
1420 uint32_t high = High32Bits(value);
1421 if (IsUint<16>(low)) {
1422 if (dst_low != lhs_low || low != 0) {
1423 __ Ori(dst_low, lhs_low, low);
1424 }
1425 } else {
1426 __ LoadConst32(TMP, low);
1427 __ Or(dst_low, lhs_low, TMP);
1428 }
1429 if (IsUint<16>(high)) {
1430 if (dst_high != lhs_high || high != 0) {
1431 __ Ori(dst_high, lhs_high, high);
1432 }
1433 } else {
1434 if (high != low) {
1435 __ LoadConst32(TMP, high);
1436 }
1437 __ Or(dst_high, lhs_high, TMP);
1438 }
1439 } else if (instruction->IsXor()) {
1440 uint32_t low = Low32Bits(value);
1441 uint32_t high = High32Bits(value);
1442 if (IsUint<16>(low)) {
1443 if (dst_low != lhs_low || low != 0) {
1444 __ Xori(dst_low, lhs_low, low);
1445 }
1446 } else {
1447 __ LoadConst32(TMP, low);
1448 __ Xor(dst_low, lhs_low, TMP);
1449 }
1450 if (IsUint<16>(high)) {
1451 if (dst_high != lhs_high || high != 0) {
1452 __ Xori(dst_high, lhs_high, high);
1453 }
1454 } else {
1455 if (high != low) {
1456 __ LoadConst32(TMP, high);
1457 }
1458 __ Xor(dst_high, lhs_high, TMP);
1459 }
1460 } else if (instruction->IsAnd()) {
1461 uint32_t low = Low32Bits(value);
1462 uint32_t high = High32Bits(value);
1463 if (IsUint<16>(low)) {
1464 __ Andi(dst_low, lhs_low, low);
1465 } else if (low != 0xFFFFFFFF) {
1466 __ LoadConst32(TMP, low);
1467 __ And(dst_low, lhs_low, TMP);
1468 } else if (dst_low != lhs_low) {
1469 __ Move(dst_low, lhs_low);
1470 }
1471 if (IsUint<16>(high)) {
1472 __ Andi(dst_high, lhs_high, high);
1473 } else if (high != 0xFFFFFFFF) {
1474 if (high != low) {
1475 __ LoadConst32(TMP, high);
1476 }
1477 __ And(dst_high, lhs_high, TMP);
1478 } else if (dst_high != lhs_high) {
1479 __ Move(dst_high, lhs_high);
1480 }
1481 } else {
1482 if (instruction->IsSub()) {
1483 value = -value;
1484 } else {
1485 DCHECK(instruction->IsAdd());
1486 }
1487 int32_t low = Low32Bits(value);
1488 int32_t high = High32Bits(value);
1489 if (IsInt<16>(low)) {
1490 if (dst_low != lhs_low || low != 0) {
1491 __ Addiu(dst_low, lhs_low, low);
1492 }
1493 if (low != 0) {
1494 __ Sltiu(AT, dst_low, low);
1495 }
1496 } else {
1497 __ LoadConst32(TMP, low);
1498 __ Addu(dst_low, lhs_low, TMP);
1499 __ Sltu(AT, dst_low, TMP);
1500 }
1501 if (IsInt<16>(high)) {
1502 if (dst_high != lhs_high || high != 0) {
1503 __ Addiu(dst_high, lhs_high, high);
1504 }
1505 } else {
1506 if (high != low) {
1507 __ LoadConst32(TMP, high);
1508 }
1509 __ Addu(dst_high, lhs_high, TMP);
1510 }
1511 if (low != 0) {
1512 __ Addu(dst_high, dst_high, AT);
1513 }
1514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001515 }
1516 break;
1517 }
1518
1519 case Primitive::kPrimFloat:
1520 case Primitive::kPrimDouble: {
1521 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1522 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1523 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1524 if (instruction->IsAdd()) {
1525 if (type == Primitive::kPrimFloat) {
1526 __ AddS(dst, lhs, rhs);
1527 } else {
1528 __ AddD(dst, lhs, rhs);
1529 }
1530 } else {
1531 DCHECK(instruction->IsSub());
1532 if (type == Primitive::kPrimFloat) {
1533 __ SubS(dst, lhs, rhs);
1534 } else {
1535 __ SubD(dst, lhs, rhs);
1536 }
1537 }
1538 break;
1539 }
1540
1541 default:
1542 LOG(FATAL) << "Unexpected binary operation type " << type;
1543 }
1544}
1545
1546void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001547 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548
1549 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1550 Primitive::Type type = instr->GetResultType();
1551 switch (type) {
1552 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001553 locations->SetInAt(0, Location::RequiresRegister());
1554 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1556 break;
1557 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1560 locations->SetOut(Location::RequiresRegister());
1561 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001562 default:
1563 LOG(FATAL) << "Unexpected shift type " << type;
1564 }
1565}
1566
1567static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1568
1569void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001570 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001571 LocationSummary* locations = instr->GetLocations();
1572 Primitive::Type type = instr->GetType();
1573
1574 Location rhs_location = locations->InAt(1);
1575 bool use_imm = rhs_location.IsConstant();
1576 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1577 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00001578 const uint32_t shift_mask =
1579 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001580 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08001581 // Are the INS (Insert Bit Field) and ROTR instructions supported?
1582 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001583
1584 switch (type) {
1585 case Primitive::kPrimInt: {
1586 Register dst = locations->Out().AsRegister<Register>();
1587 Register lhs = locations->InAt(0).AsRegister<Register>();
1588 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001589 if (shift_value == 0) {
1590 if (dst != lhs) {
1591 __ Move(dst, lhs);
1592 }
1593 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001594 __ Sll(dst, lhs, shift_value);
1595 } else if (instr->IsShr()) {
1596 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001597 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001599 } else {
1600 if (has_ins_rotr) {
1601 __ Rotr(dst, lhs, shift_value);
1602 } else {
1603 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
1604 __ Srl(dst, lhs, shift_value);
1605 __ Or(dst, dst, TMP);
1606 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001607 }
1608 } else {
1609 if (instr->IsShl()) {
1610 __ Sllv(dst, lhs, rhs_reg);
1611 } else if (instr->IsShr()) {
1612 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001613 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001614 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08001615 } else {
1616 if (has_ins_rotr) {
1617 __ Rotrv(dst, lhs, rhs_reg);
1618 } else {
1619 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001620 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
1621 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
1622 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
1623 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
1624 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08001625 __ Sllv(TMP, lhs, TMP);
1626 __ Srlv(dst, lhs, rhs_reg);
1627 __ Or(dst, dst, TMP);
1628 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001629 }
1630 }
1631 break;
1632 }
1633
1634 case Primitive::kPrimLong: {
1635 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1636 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1637 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1638 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1639 if (use_imm) {
1640 if (shift_value == 0) {
1641 codegen_->Move64(locations->Out(), locations->InAt(0));
1642 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001643 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001644 if (instr->IsShl()) {
1645 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1646 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
1647 __ Sll(dst_low, lhs_low, shift_value);
1648 } else if (instr->IsShr()) {
1649 __ Srl(dst_low, lhs_low, shift_value);
1650 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1651 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001652 } else if (instr->IsUShr()) {
1653 __ Srl(dst_low, lhs_low, shift_value);
1654 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1655 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001656 } else {
1657 __ Srl(dst_low, lhs_low, shift_value);
1658 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
1659 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08001660 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001661 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001662 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001663 if (instr->IsShl()) {
1664 __ Sll(dst_low, lhs_low, shift_value);
1665 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1666 __ Sll(dst_high, lhs_high, shift_value);
1667 __ Or(dst_high, dst_high, TMP);
1668 } else if (instr->IsShr()) {
1669 __ Sra(dst_high, lhs_high, shift_value);
1670 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1671 __ Srl(dst_low, lhs_low, shift_value);
1672 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001673 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001674 __ Srl(dst_high, lhs_high, shift_value);
1675 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1676 __ Srl(dst_low, lhs_low, shift_value);
1677 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08001678 } else {
1679 __ Srl(TMP, lhs_low, shift_value);
1680 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
1681 __ Or(dst_low, dst_low, TMP);
1682 __ Srl(TMP, lhs_high, shift_value);
1683 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
1684 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001685 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001686 }
1687 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001688 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001689 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001690 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001691 __ Move(dst_low, ZERO);
1692 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001693 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001694 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08001695 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001696 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001697 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001698 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001699 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08001700 // 64-bit rotation by 32 is just a swap.
1701 __ Move(dst_low, lhs_high);
1702 __ Move(dst_high, lhs_low);
1703 } else {
1704 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001705 __ Srl(dst_low, lhs_high, shift_value_high);
1706 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
1707 __ Srl(dst_high, lhs_low, shift_value_high);
1708 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001709 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001710 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
1711 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001712 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08001713 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
1714 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08001715 __ Or(dst_high, dst_high, TMP);
1716 }
1717 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001718 }
1719 }
1720 } else {
1721 MipsLabel done;
1722 if (instr->IsShl()) {
1723 __ Sllv(dst_low, lhs_low, rhs_reg);
1724 __ Nor(AT, ZERO, rhs_reg);
1725 __ Srl(TMP, lhs_low, 1);
1726 __ Srlv(TMP, TMP, AT);
1727 __ Sllv(dst_high, lhs_high, rhs_reg);
1728 __ Or(dst_high, dst_high, TMP);
1729 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1730 __ Beqz(TMP, &done);
1731 __ Move(dst_high, dst_low);
1732 __ Move(dst_low, ZERO);
1733 } else if (instr->IsShr()) {
1734 __ Srav(dst_high, lhs_high, rhs_reg);
1735 __ Nor(AT, ZERO, rhs_reg);
1736 __ Sll(TMP, lhs_high, 1);
1737 __ Sllv(TMP, TMP, AT);
1738 __ Srlv(dst_low, lhs_low, rhs_reg);
1739 __ Or(dst_low, dst_low, TMP);
1740 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1741 __ Beqz(TMP, &done);
1742 __ Move(dst_low, dst_high);
1743 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08001744 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001745 __ Srlv(dst_high, lhs_high, rhs_reg);
1746 __ Nor(AT, ZERO, rhs_reg);
1747 __ Sll(TMP, lhs_high, 1);
1748 __ Sllv(TMP, TMP, AT);
1749 __ Srlv(dst_low, lhs_low, rhs_reg);
1750 __ Or(dst_low, dst_low, TMP);
1751 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1752 __ Beqz(TMP, &done);
1753 __ Move(dst_low, dst_high);
1754 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08001755 } else {
1756 __ Nor(AT, ZERO, rhs_reg);
1757 __ Srlv(TMP, lhs_low, rhs_reg);
1758 __ Sll(dst_low, lhs_high, 1);
1759 __ Sllv(dst_low, dst_low, AT);
1760 __ Or(dst_low, dst_low, TMP);
1761 __ Srlv(TMP, lhs_high, rhs_reg);
1762 __ Sll(dst_high, lhs_low, 1);
1763 __ Sllv(dst_high, dst_high, AT);
1764 __ Or(dst_high, dst_high, TMP);
1765 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1766 __ Beqz(TMP, &done);
1767 __ Move(TMP, dst_high);
1768 __ Move(dst_high, dst_low);
1769 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001770 }
1771 __ Bind(&done);
1772 }
1773 break;
1774 }
1775
1776 default:
1777 LOG(FATAL) << "Unexpected shift operation type " << type;
1778 }
1779}
1780
1781void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1782 HandleBinaryOp(instruction);
1783}
1784
1785void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1786 HandleBinaryOp(instruction);
1787}
1788
1789void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1790 HandleBinaryOp(instruction);
1791}
1792
1793void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1794 HandleBinaryOp(instruction);
1795}
1796
1797void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1802 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1803 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1804 } else {
1805 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1806 }
1807}
1808
Alexey Frunze2923db72016-08-20 01:55:47 -07001809auto InstructionCodeGeneratorMIPS::GetImplicitNullChecker(HInstruction* instruction) {
1810 auto null_checker = [this, instruction]() {
1811 this->codegen_->MaybeRecordImplicitNullCheck(instruction);
1812 };
1813 return null_checker;
1814}
1815
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001816void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1817 LocationSummary* locations = instruction->GetLocations();
1818 Register obj = locations->InAt(0).AsRegister<Register>();
1819 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001820 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07001821 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001823 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824 switch (type) {
1825 case Primitive::kPrimBoolean: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001826 Register out = locations->Out().AsRegister<Register>();
1827 if (index.IsConstant()) {
1828 size_t offset =
1829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001830 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001831 } else {
1832 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001833 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimByte: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001839 Register out = locations->Out().AsRegister<Register>();
1840 if (index.IsConstant()) {
1841 size_t offset =
1842 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001843 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844 } else {
1845 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07001846 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847 }
1848 break;
1849 }
1850
1851 case Primitive::kPrimShort: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 Register out = locations->Out().AsRegister<Register>();
1853 if (index.IsConstant()) {
1854 size_t offset =
1855 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001856 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 } else {
1858 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1859 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001860 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861 }
1862 break;
1863 }
1864
1865 case Primitive::kPrimChar: {
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_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001870 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 } else {
1872 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1873 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001874 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875 }
1876 break;
1877 }
1878
1879 case Primitive::kPrimInt:
1880 case Primitive::kPrimNot: {
1881 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882 Register out = locations->Out().AsRegister<Register>();
1883 if (index.IsConstant()) {
1884 size_t offset =
1885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001886 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887 } else {
1888 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1889 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001890 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891 }
1892 break;
1893 }
1894
1895 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001896 Register out = locations->Out().AsRegisterPairLow<Register>();
1897 if (index.IsConstant()) {
1898 size_t offset =
1899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001900 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901 } else {
1902 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1903 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001904 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimFloat: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001910 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1911 if (index.IsConstant()) {
1912 size_t offset =
1913 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001914 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 } else {
1916 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1917 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001918 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 }
1920 break;
1921 }
1922
1923 case Primitive::kPrimDouble: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1925 if (index.IsConstant()) {
1926 size_t offset =
1927 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07001928 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929 } else {
1930 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1931 __ Addu(TMP, obj, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07001932 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 }
1934 break;
1935 }
1936
1937 case Primitive::kPrimVoid:
1938 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1939 UNREACHABLE();
1940 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941}
1942
1943void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1945 locations->SetInAt(0, Location::RequiresRegister());
1946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1947}
1948
1949void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1950 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01001951 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 Register obj = locations->InAt(0).AsRegister<Register>();
1953 Register out = locations->Out().AsRegister<Register>();
1954 __ LoadFromOffset(kLoadWord, out, obj, offset);
1955 codegen_->MaybeRecordImplicitNullCheck(instruction);
1956}
1957
Alexey Frunzef58b2482016-09-02 22:14:06 -07001958Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
1959 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
1960 ? Location::ConstantLocation(instruction->AsConstant())
1961 : Location::RequiresRegister();
1962}
1963
1964Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
1965 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
1966 // We can store a non-zero float or double constant without first loading it into the FPU,
1967 // but we should only prefer this if the constant has a single use.
1968 if (instruction->IsConstant() &&
1969 (instruction->AsConstant()->IsZeroBitPattern() ||
1970 instruction->GetUses().HasExactlyOneElement())) {
1971 return Location::ConstantLocation(instruction->AsConstant());
1972 // Otherwise fall through and require an FPU register for the constant.
1973 }
1974 return Location::RequiresFpuRegister();
1975}
1976
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Pavle Batuta934808f2015-11-03 13:23:54 +01001978 bool needs_runtime_call = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1980 instruction,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001981 needs_runtime_call ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Pavle Batuta934808f2015-11-03 13:23:54 +01001982 if (needs_runtime_call) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001983 InvokeRuntimeCallingConvention calling_convention;
1984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1985 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1986 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1987 } else {
1988 locations->SetInAt(0, Location::RequiresRegister());
1989 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1990 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001991 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001992 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07001993 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001994 }
1995 }
1996}
1997
1998void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1999 LocationSummary* locations = instruction->GetLocations();
2000 Register obj = locations->InAt(0).AsRegister<Register>();
2001 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002002 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 Primitive::Type value_type = instruction->GetComponentType();
2004 bool needs_runtime_call = locations->WillCall();
2005 bool needs_write_barrier =
2006 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexey Frunze2923db72016-08-20 01:55:47 -07002007 auto null_checker = GetImplicitNullChecker(instruction);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002008 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002009
2010 switch (value_type) {
2011 case Primitive::kPrimBoolean:
2012 case Primitive::kPrimByte: {
2013 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002015 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002017 __ Addu(base_reg, obj, index.AsRegister<Register>());
2018 }
2019 if (value_location.IsConstant()) {
2020 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2021 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2022 } else {
2023 Register value = value_location.AsRegister<Register>();
2024 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002025 }
2026 break;
2027 }
2028
2029 case Primitive::kPrimShort:
2030 case Primitive::kPrimChar: {
2031 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002033 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002035 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_2);
2036 __ Addu(base_reg, obj, base_reg);
2037 }
2038 if (value_location.IsConstant()) {
2039 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2040 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2041 } else {
2042 Register value = value_location.AsRegister<Register>();
2043 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044 }
2045 break;
2046 }
2047
2048 case Primitive::kPrimInt:
2049 case Primitive::kPrimNot: {
2050 if (!needs_runtime_call) {
2051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002053 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002055 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2056 __ Addu(base_reg, obj, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002057 }
Alexey Frunzef58b2482016-09-02 22:14:06 -07002058 if (value_location.IsConstant()) {
2059 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2060 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2061 DCHECK(!needs_write_barrier);
2062 } else {
2063 Register value = value_location.AsRegister<Register>();
2064 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2065 if (needs_write_barrier) {
2066 DCHECK_EQ(value_type, Primitive::kPrimNot);
2067 codegen_->MarkGCCard(obj, value);
2068 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002069 }
2070 } else {
2071 DCHECK_EQ(value_type, Primitive::kPrimNot);
Serban Constantinescufca16662016-07-14 09:21:59 +01002072 codegen_->InvokeRuntime(kQuickAputObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002073 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
2074 }
2075 break;
2076 }
2077
2078 case Primitive::kPrimLong: {
2079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002080 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002081 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002082 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002083 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2084 __ Addu(base_reg, obj, base_reg);
2085 }
2086 if (value_location.IsConstant()) {
2087 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2088 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2089 } else {
2090 Register value = value_location.AsRegisterPairLow<Register>();
2091 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002092 }
2093 break;
2094 }
2095
2096 case Primitive::kPrimFloat: {
2097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002098 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002099 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002100 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002101 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_4);
2102 __ Addu(base_reg, obj, base_reg);
2103 }
2104 if (value_location.IsConstant()) {
2105 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2106 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2107 } else {
2108 FRegister value = value_location.AsFpuRegister<FRegister>();
2109 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002110 }
2111 break;
2112 }
2113
2114 case Primitive::kPrimDouble: {
2115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002116 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002117 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002119 __ Sll(base_reg, index.AsRegister<Register>(), TIMES_8);
2120 __ Addu(base_reg, obj, base_reg);
2121 }
2122 if (value_location.IsConstant()) {
2123 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2124 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2125 } else {
2126 FRegister value = value_location.AsFpuRegister<FRegister>();
2127 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002128 }
2129 break;
2130 }
2131
2132 case Primitive::kPrimVoid:
2133 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2134 UNREACHABLE();
2135 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136}
2137
2138void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002139 RegisterSet caller_saves = RegisterSet::Empty();
2140 InvokeRuntimeCallingConvention calling_convention;
2141 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2142 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2143 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144 locations->SetInAt(0, Location::RequiresRegister());
2145 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002146}
2147
2148void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
2149 LocationSummary* locations = instruction->GetLocations();
2150 BoundsCheckSlowPathMIPS* slow_path =
2151 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
2152 codegen_->AddSlowPath(slow_path);
2153
2154 Register index = locations->InAt(0).AsRegister<Register>();
2155 Register length = locations->InAt(1).AsRegister<Register>();
2156
2157 // length is limited by the maximum positive signed 32-bit integer.
2158 // Unsigned comparison of length and index checks for index < 0
2159 // and for length <= index simultaneously.
2160 __ Bgeu(index, length, slow_path->GetEntryLabel());
2161}
2162
2163void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
2164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2165 instruction,
2166 LocationSummary::kCallOnSlowPath);
2167 locations->SetInAt(0, Location::RequiresRegister());
2168 locations->SetInAt(1, Location::RequiresRegister());
2169 // Note that TypeCheckSlowPathMIPS uses this register too.
2170 locations->AddTemp(Location::RequiresRegister());
2171}
2172
2173void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
2174 LocationSummary* locations = instruction->GetLocations();
2175 Register obj = locations->InAt(0).AsRegister<Register>();
2176 Register cls = locations->InAt(1).AsRegister<Register>();
2177 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
2178
2179 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2180 codegen_->AddSlowPath(slow_path);
2181
2182 // TODO: avoid this check if we know obj is not null.
2183 __ Beqz(obj, slow_path->GetExitLabel());
2184 // Compare the class of `obj` with `cls`.
2185 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
2186 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
2187 __ Bind(slow_path->GetExitLabel());
2188}
2189
2190void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
2191 LocationSummary* locations =
2192 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2193 locations->SetInAt(0, Location::RequiresRegister());
2194 if (check->HasUses()) {
2195 locations->SetOut(Location::SameAsFirstInput());
2196 }
2197}
2198
2199void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
2200 // We assume the class is not null.
2201 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
2202 check->GetLoadClass(),
2203 check,
2204 check->GetDexPc(),
2205 true);
2206 codegen_->AddSlowPath(slow_path);
2207 GenerateClassInitializationCheck(slow_path,
2208 check->GetLocations()->InAt(0).AsRegister<Register>());
2209}
2210
2211void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
2212 Primitive::Type in_type = compare->InputAt(0)->GetType();
2213
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002216
2217 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002218 case Primitive::kPrimBoolean:
2219 case Primitive::kPrimByte:
2220 case Primitive::kPrimShort:
2221 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002222 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07002223 locations->SetInAt(0, Location::RequiresRegister());
2224 locations->SetInAt(1, Location::RequiresRegister());
2225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2226 break;
2227
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002228 case Primitive::kPrimLong:
2229 locations->SetInAt(0, Location::RequiresRegister());
2230 locations->SetInAt(1, Location::RequiresRegister());
2231 // Output overlaps because it is written before doing the low comparison.
2232 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2233 break;
2234
2235 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002236 case Primitive::kPrimDouble:
2237 locations->SetInAt(0, Location::RequiresFpuRegister());
2238 locations->SetInAt(1, Location::RequiresFpuRegister());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241
2242 default:
2243 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2244 }
2245}
2246
2247void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
2248 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002249 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002250 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002251 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002252
2253 // 0 if: left == right
2254 // 1 if: left > right
2255 // -1 if: left < right
2256 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002257 case Primitive::kPrimBoolean:
2258 case Primitive::kPrimByte:
2259 case Primitive::kPrimShort:
2260 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002261 case Primitive::kPrimInt: {
2262 Register lhs = locations->InAt(0).AsRegister<Register>();
2263 Register rhs = locations->InAt(1).AsRegister<Register>();
2264 __ Slt(TMP, lhs, rhs);
2265 __ Slt(res, rhs, lhs);
2266 __ Subu(res, res, TMP);
2267 break;
2268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 case Primitive::kPrimLong: {
2270 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002271 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2272 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2273 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2274 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2275 // TODO: more efficient (direct) comparison with a constant.
2276 __ Slt(TMP, lhs_high, rhs_high);
2277 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2278 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2279 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2280 __ Sltu(TMP, lhs_low, rhs_low);
2281 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2282 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2283 __ Bind(&done);
2284 break;
2285 }
2286
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002287 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002288 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002289 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2290 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2291 MipsLabel done;
2292 if (isR6) {
2293 __ CmpEqS(FTMP, lhs, rhs);
2294 __ LoadConst32(res, 0);
2295 __ Bc1nez(FTMP, &done);
2296 if (gt_bias) {
2297 __ CmpLtS(FTMP, lhs, rhs);
2298 __ LoadConst32(res, -1);
2299 __ Bc1nez(FTMP, &done);
2300 __ LoadConst32(res, 1);
2301 } else {
2302 __ CmpLtS(FTMP, rhs, lhs);
2303 __ LoadConst32(res, 1);
2304 __ Bc1nez(FTMP, &done);
2305 __ LoadConst32(res, -1);
2306 }
2307 } else {
2308 if (gt_bias) {
2309 __ ColtS(0, lhs, rhs);
2310 __ LoadConst32(res, -1);
2311 __ Bc1t(0, &done);
2312 __ CeqS(0, lhs, rhs);
2313 __ LoadConst32(res, 1);
2314 __ Movt(res, ZERO, 0);
2315 } else {
2316 __ ColtS(0, rhs, lhs);
2317 __ LoadConst32(res, 1);
2318 __ Bc1t(0, &done);
2319 __ CeqS(0, lhs, rhs);
2320 __ LoadConst32(res, -1);
2321 __ Movt(res, ZERO, 0);
2322 }
2323 }
2324 __ Bind(&done);
2325 break;
2326 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00002328 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002329 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2330 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2331 MipsLabel done;
2332 if (isR6) {
2333 __ CmpEqD(FTMP, lhs, rhs);
2334 __ LoadConst32(res, 0);
2335 __ Bc1nez(FTMP, &done);
2336 if (gt_bias) {
2337 __ CmpLtD(FTMP, lhs, rhs);
2338 __ LoadConst32(res, -1);
2339 __ Bc1nez(FTMP, &done);
2340 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002342 __ CmpLtD(FTMP, rhs, lhs);
2343 __ LoadConst32(res, 1);
2344 __ Bc1nez(FTMP, &done);
2345 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 }
2347 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002348 if (gt_bias) {
2349 __ ColtD(0, lhs, rhs);
2350 __ LoadConst32(res, -1);
2351 __ Bc1t(0, &done);
2352 __ CeqD(0, lhs, rhs);
2353 __ LoadConst32(res, 1);
2354 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002356 __ ColtD(0, rhs, lhs);
2357 __ LoadConst32(res, 1);
2358 __ Bc1t(0, &done);
2359 __ CeqD(0, lhs, rhs);
2360 __ LoadConst32(res, -1);
2361 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 }
2363 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002364 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 break;
2366 }
2367
2368 default:
2369 LOG(FATAL) << "Unimplemented compare type " << in_type;
2370 }
2371}
2372
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002373void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002375 switch (instruction->InputAt(0)->GetType()) {
2376 default:
2377 case Primitive::kPrimLong:
2378 locations->SetInAt(0, Location::RequiresRegister());
2379 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2380 break;
2381
2382 case Primitive::kPrimFloat:
2383 case Primitive::kPrimDouble:
2384 locations->SetInAt(0, Location::RequiresFpuRegister());
2385 locations->SetInAt(1, Location::RequiresFpuRegister());
2386 break;
2387 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002388 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2390 }
2391}
2392
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002393void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002394 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 return;
2396 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002398 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002399 LocationSummary* locations = instruction->GetLocations();
2400 Register dst = locations->Out().AsRegister<Register>();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002401 MipsLabel true_label;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002402
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002403 switch (type) {
2404 default:
2405 // Integer case.
2406 GenerateIntCompare(instruction->GetCondition(), locations);
2407 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002409 case Primitive::kPrimLong:
2410 // TODO: don't use branches.
2411 GenerateLongCompareAndBranch(instruction->GetCondition(), locations, &true_label);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002412 break;
2413
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002414 case Primitive::kPrimFloat:
2415 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07002416 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
2417 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002419
2420 // Convert the branches into the result.
2421 MipsLabel done;
2422
2423 // False case: result = 0.
2424 __ LoadConst32(dst, 0);
2425 __ B(&done);
2426
2427 // True case: result = 1.
2428 __ Bind(&true_label);
2429 __ LoadConst32(dst, 1);
2430 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431}
2432
Alexey Frunze7e99e052015-11-24 19:28:01 -08002433void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2434 DCHECK(instruction->IsDiv() || instruction->IsRem());
2435 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2436
2437 LocationSummary* locations = instruction->GetLocations();
2438 Location second = locations->InAt(1);
2439 DCHECK(second.IsConstant());
2440
2441 Register out = locations->Out().AsRegister<Register>();
2442 Register dividend = locations->InAt(0).AsRegister<Register>();
2443 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2444 DCHECK(imm == 1 || imm == -1);
2445
2446 if (instruction->IsRem()) {
2447 __ Move(out, ZERO);
2448 } else {
2449 if (imm == -1) {
2450 __ Subu(out, ZERO, dividend);
2451 } else if (out != dividend) {
2452 __ Move(out, dividend);
2453 }
2454 }
2455}
2456
2457void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2458 DCHECK(instruction->IsDiv() || instruction->IsRem());
2459 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2460
2461 LocationSummary* locations = instruction->GetLocations();
2462 Location second = locations->InAt(1);
2463 DCHECK(second.IsConstant());
2464
2465 Register out = locations->Out().AsRegister<Register>();
2466 Register dividend = locations->InAt(0).AsRegister<Register>();
2467 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002468 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08002469 int ctz_imm = CTZ(abs_imm);
2470
2471 if (instruction->IsDiv()) {
2472 if (ctz_imm == 1) {
2473 // Fast path for division by +/-2, which is very common.
2474 __ Srl(TMP, dividend, 31);
2475 } else {
2476 __ Sra(TMP, dividend, 31);
2477 __ Srl(TMP, TMP, 32 - ctz_imm);
2478 }
2479 __ Addu(out, dividend, TMP);
2480 __ Sra(out, out, ctz_imm);
2481 if (imm < 0) {
2482 __ Subu(out, ZERO, out);
2483 }
2484 } else {
2485 if (ctz_imm == 1) {
2486 // Fast path for modulo +/-2, which is very common.
2487 __ Sra(TMP, dividend, 31);
2488 __ Subu(out, dividend, TMP);
2489 __ Andi(out, out, 1);
2490 __ Addu(out, out, TMP);
2491 } else {
2492 __ Sra(TMP, dividend, 31);
2493 __ Srl(TMP, TMP, 32 - ctz_imm);
2494 __ Addu(out, dividend, TMP);
2495 if (IsUint<16>(abs_imm - 1)) {
2496 __ Andi(out, out, abs_imm - 1);
2497 } else {
2498 __ Sll(out, out, 32 - ctz_imm);
2499 __ Srl(out, out, 32 - ctz_imm);
2500 }
2501 __ Subu(out, out, TMP);
2502 }
2503 }
2504}
2505
2506void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2507 DCHECK(instruction->IsDiv() || instruction->IsRem());
2508 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2509
2510 LocationSummary* locations = instruction->GetLocations();
2511 Location second = locations->InAt(1);
2512 DCHECK(second.IsConstant());
2513
2514 Register out = locations->Out().AsRegister<Register>();
2515 Register dividend = locations->InAt(0).AsRegister<Register>();
2516 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2517
2518 int64_t magic;
2519 int shift;
2520 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2521
2522 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2523
2524 __ LoadConst32(TMP, magic);
2525 if (isR6) {
2526 __ MuhR6(TMP, dividend, TMP);
2527 } else {
2528 __ MultR2(dividend, TMP);
2529 __ Mfhi(TMP);
2530 }
2531 if (imm > 0 && magic < 0) {
2532 __ Addu(TMP, TMP, dividend);
2533 } else if (imm < 0 && magic > 0) {
2534 __ Subu(TMP, TMP, dividend);
2535 }
2536
2537 if (shift != 0) {
2538 __ Sra(TMP, TMP, shift);
2539 }
2540
2541 if (instruction->IsDiv()) {
2542 __ Sra(out, TMP, 31);
2543 __ Subu(out, TMP, out);
2544 } else {
2545 __ Sra(AT, TMP, 31);
2546 __ Subu(AT, TMP, AT);
2547 __ LoadConst32(TMP, imm);
2548 if (isR6) {
2549 __ MulR6(TMP, AT, TMP);
2550 } else {
2551 __ MulR2(TMP, AT, TMP);
2552 }
2553 __ Subu(out, dividend, TMP);
2554 }
2555}
2556
2557void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2558 DCHECK(instruction->IsDiv() || instruction->IsRem());
2559 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
2560
2561 LocationSummary* locations = instruction->GetLocations();
2562 Register out = locations->Out().AsRegister<Register>();
2563 Location second = locations->InAt(1);
2564
2565 if (second.IsConstant()) {
2566 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2567 if (imm == 0) {
2568 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2569 } else if (imm == 1 || imm == -1) {
2570 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002571 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002572 DivRemByPowerOfTwo(instruction);
2573 } else {
2574 DCHECK(imm <= -2 || imm >= 2);
2575 GenerateDivRemWithAnyConstant(instruction);
2576 }
2577 } else {
2578 Register dividend = locations->InAt(0).AsRegister<Register>();
2579 Register divisor = second.AsRegister<Register>();
2580 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2581 if (instruction->IsDiv()) {
2582 if (isR6) {
2583 __ DivR6(out, dividend, divisor);
2584 } else {
2585 __ DivR2(out, dividend, divisor);
2586 }
2587 } else {
2588 if (isR6) {
2589 __ ModR6(out, dividend, divisor);
2590 } else {
2591 __ ModR2(out, dividend, divisor);
2592 }
2593 }
2594 }
2595}
2596
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2598 Primitive::Type type = div->GetResultType();
2599 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002600 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 : LocationSummary::kNoCall;
2602
2603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2604
2605 switch (type) {
2606 case Primitive::kPrimInt:
2607 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08002608 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2610 break;
2611
2612 case Primitive::kPrimLong: {
2613 InvokeRuntimeCallingConvention calling_convention;
2614 locations->SetInAt(0, Location::RegisterPairLocation(
2615 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2616 locations->SetInAt(1, Location::RegisterPairLocation(
2617 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2618 locations->SetOut(calling_convention.GetReturnLocation(type));
2619 break;
2620 }
2621
2622 case Primitive::kPrimFloat:
2623 case Primitive::kPrimDouble:
2624 locations->SetInAt(0, Location::RequiresFpuRegister());
2625 locations->SetInAt(1, Location::RequiresFpuRegister());
2626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2627 break;
2628
2629 default:
2630 LOG(FATAL) << "Unexpected div type " << type;
2631 }
2632}
2633
2634void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2635 Primitive::Type type = instruction->GetType();
2636 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002637
2638 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08002639 case Primitive::kPrimInt:
2640 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002642 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01002643 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002644 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2645 break;
2646 }
2647 case Primitive::kPrimFloat:
2648 case Primitive::kPrimDouble: {
2649 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2650 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2651 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2652 if (type == Primitive::kPrimFloat) {
2653 __ DivS(dst, lhs, rhs);
2654 } else {
2655 __ DivD(dst, lhs, rhs);
2656 }
2657 break;
2658 }
2659 default:
2660 LOG(FATAL) << "Unexpected div type " << type;
2661 }
2662}
2663
2664void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002665 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002666 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667}
2668
2669void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2670 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2671 codegen_->AddSlowPath(slow_path);
2672 Location value = instruction->GetLocations()->InAt(0);
2673 Primitive::Type type = instruction->GetType();
2674
2675 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002676 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 case Primitive::kPrimByte:
2678 case Primitive::kPrimChar:
2679 case Primitive::kPrimShort:
2680 case Primitive::kPrimInt: {
2681 if (value.IsConstant()) {
2682 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2683 __ B(slow_path->GetEntryLabel());
2684 } else {
2685 // A division by a non-null constant is valid. We don't need to perform
2686 // any check, so simply fall through.
2687 }
2688 } else {
2689 DCHECK(value.IsRegister()) << value;
2690 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2691 }
2692 break;
2693 }
2694 case Primitive::kPrimLong: {
2695 if (value.IsConstant()) {
2696 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2697 __ B(slow_path->GetEntryLabel());
2698 } else {
2699 // A division by a non-null constant is valid. We don't need to perform
2700 // any check, so simply fall through.
2701 }
2702 } else {
2703 DCHECK(value.IsRegisterPair()) << value;
2704 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2705 __ Beqz(TMP, slow_path->GetEntryLabel());
2706 }
2707 break;
2708 }
2709 default:
2710 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2711 }
2712}
2713
2714void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2715 LocationSummary* locations =
2716 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2717 locations->SetOut(Location::ConstantLocation(constant));
2718}
2719
2720void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2721 // Will be generated at use site.
2722}
2723
2724void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2725 exit->SetLocations(nullptr);
2726}
2727
2728void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2729}
2730
2731void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2732 LocationSummary* locations =
2733 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2734 locations->SetOut(Location::ConstantLocation(constant));
2735}
2736
2737void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2738 // Will be generated at use site.
2739}
2740
2741void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2742 got->SetLocations(nullptr);
2743}
2744
2745void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2746 DCHECK(!successor->IsExitBlock());
2747 HBasicBlock* block = got->GetBlock();
2748 HInstruction* previous = got->GetPrevious();
2749 HLoopInformation* info = block->GetLoopInformation();
2750
2751 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2752 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2753 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2754 return;
2755 }
2756 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2757 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2758 }
2759 if (!codegen_->GoesToNextBlock(block, successor)) {
2760 __ B(codegen_->GetLabelOf(successor));
2761 }
2762}
2763
2764void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2765 HandleGoto(got, got->GetSuccessor());
2766}
2767
2768void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2769 try_boundary->SetLocations(nullptr);
2770}
2771
2772void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2773 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2774 if (!successor->IsExitBlock()) {
2775 HandleGoto(try_boundary, successor);
2776 }
2777}
2778
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002779void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
2780 LocationSummary* locations) {
2781 Register dst = locations->Out().AsRegister<Register>();
2782 Register lhs = locations->InAt(0).AsRegister<Register>();
2783 Location rhs_location = locations->InAt(1);
2784 Register rhs_reg = ZERO;
2785 int64_t rhs_imm = 0;
2786 bool use_imm = rhs_location.IsConstant();
2787 if (use_imm) {
2788 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2789 } else {
2790 rhs_reg = rhs_location.AsRegister<Register>();
2791 }
2792
2793 switch (cond) {
2794 case kCondEQ:
2795 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07002796 if (use_imm && IsInt<16>(-rhs_imm)) {
2797 if (rhs_imm == 0) {
2798 if (cond == kCondEQ) {
2799 __ Sltiu(dst, lhs, 1);
2800 } else {
2801 __ Sltu(dst, ZERO, lhs);
2802 }
2803 } else {
2804 __ Addiu(dst, lhs, -rhs_imm);
2805 if (cond == kCondEQ) {
2806 __ Sltiu(dst, dst, 1);
2807 } else {
2808 __ Sltu(dst, ZERO, dst);
2809 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002810 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002811 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07002812 if (use_imm && IsUint<16>(rhs_imm)) {
2813 __ Xori(dst, lhs, rhs_imm);
2814 } else {
2815 if (use_imm) {
2816 rhs_reg = TMP;
2817 __ LoadConst32(rhs_reg, rhs_imm);
2818 }
2819 __ Xor(dst, lhs, rhs_reg);
2820 }
2821 if (cond == kCondEQ) {
2822 __ Sltiu(dst, dst, 1);
2823 } else {
2824 __ Sltu(dst, ZERO, dst);
2825 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08002826 }
2827 break;
2828
2829 case kCondLT:
2830 case kCondGE:
2831 if (use_imm && IsInt<16>(rhs_imm)) {
2832 __ Slti(dst, lhs, rhs_imm);
2833 } else {
2834 if (use_imm) {
2835 rhs_reg = TMP;
2836 __ LoadConst32(rhs_reg, rhs_imm);
2837 }
2838 __ Slt(dst, lhs, rhs_reg);
2839 }
2840 if (cond == kCondGE) {
2841 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2842 // only the slt instruction but no sge.
2843 __ Xori(dst, dst, 1);
2844 }
2845 break;
2846
2847 case kCondLE:
2848 case kCondGT:
2849 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2850 // Simulate lhs <= rhs via lhs < rhs + 1.
2851 __ Slti(dst, lhs, rhs_imm + 1);
2852 if (cond == kCondGT) {
2853 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2854 // only the slti instruction but no sgti.
2855 __ Xori(dst, dst, 1);
2856 }
2857 } else {
2858 if (use_imm) {
2859 rhs_reg = TMP;
2860 __ LoadConst32(rhs_reg, rhs_imm);
2861 }
2862 __ Slt(dst, rhs_reg, lhs);
2863 if (cond == kCondLE) {
2864 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2865 // only the slt instruction but no sle.
2866 __ Xori(dst, dst, 1);
2867 }
2868 }
2869 break;
2870
2871 case kCondB:
2872 case kCondAE:
2873 if (use_imm && IsInt<16>(rhs_imm)) {
2874 // Sltiu sign-extends its 16-bit immediate operand before
2875 // the comparison and thus lets us compare directly with
2876 // unsigned values in the ranges [0, 0x7fff] and
2877 // [0xffff8000, 0xffffffff].
2878 __ Sltiu(dst, lhs, rhs_imm);
2879 } else {
2880 if (use_imm) {
2881 rhs_reg = TMP;
2882 __ LoadConst32(rhs_reg, rhs_imm);
2883 }
2884 __ Sltu(dst, lhs, rhs_reg);
2885 }
2886 if (cond == kCondAE) {
2887 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2888 // only the sltu instruction but no sgeu.
2889 __ Xori(dst, dst, 1);
2890 }
2891 break;
2892
2893 case kCondBE:
2894 case kCondA:
2895 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
2896 // Simulate lhs <= rhs via lhs < rhs + 1.
2897 // Note that this only works if rhs + 1 does not overflow
2898 // to 0, hence the check above.
2899 // Sltiu sign-extends its 16-bit immediate operand before
2900 // the comparison and thus lets us compare directly with
2901 // unsigned values in the ranges [0, 0x7fff] and
2902 // [0xffff8000, 0xffffffff].
2903 __ Sltiu(dst, lhs, rhs_imm + 1);
2904 if (cond == kCondA) {
2905 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2906 // only the sltiu instruction but no sgtiu.
2907 __ Xori(dst, dst, 1);
2908 }
2909 } else {
2910 if (use_imm) {
2911 rhs_reg = TMP;
2912 __ LoadConst32(rhs_reg, rhs_imm);
2913 }
2914 __ Sltu(dst, rhs_reg, lhs);
2915 if (cond == kCondBE) {
2916 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2917 // only the sltu instruction but no sleu.
2918 __ Xori(dst, dst, 1);
2919 }
2920 }
2921 break;
2922 }
2923}
2924
Alexey Frunze674b9ee2016-09-20 14:54:15 -07002925bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
2926 LocationSummary* input_locations,
2927 Register dst) {
2928 Register lhs = input_locations->InAt(0).AsRegister<Register>();
2929 Location rhs_location = input_locations->InAt(1);
2930 Register rhs_reg = ZERO;
2931 int64_t rhs_imm = 0;
2932 bool use_imm = rhs_location.IsConstant();
2933 if (use_imm) {
2934 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2935 } else {
2936 rhs_reg = rhs_location.AsRegister<Register>();
2937 }
2938
2939 switch (cond) {
2940 case kCondEQ:
2941 case kCondNE:
2942 if (use_imm && IsInt<16>(-rhs_imm)) {
2943 __ Addiu(dst, lhs, -rhs_imm);
2944 } else if (use_imm && IsUint<16>(rhs_imm)) {
2945 __ Xori(dst, lhs, rhs_imm);
2946 } else {
2947 if (use_imm) {
2948 rhs_reg = TMP;
2949 __ LoadConst32(rhs_reg, rhs_imm);
2950 }
2951 __ Xor(dst, lhs, rhs_reg);
2952 }
2953 return (cond == kCondEQ);
2954
2955 case kCondLT:
2956 case kCondGE:
2957 if (use_imm && IsInt<16>(rhs_imm)) {
2958 __ Slti(dst, lhs, rhs_imm);
2959 } else {
2960 if (use_imm) {
2961 rhs_reg = TMP;
2962 __ LoadConst32(rhs_reg, rhs_imm);
2963 }
2964 __ Slt(dst, lhs, rhs_reg);
2965 }
2966 return (cond == kCondGE);
2967
2968 case kCondLE:
2969 case kCondGT:
2970 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2971 // Simulate lhs <= rhs via lhs < rhs + 1.
2972 __ Slti(dst, lhs, rhs_imm + 1);
2973 return (cond == kCondGT);
2974 } else {
2975 if (use_imm) {
2976 rhs_reg = TMP;
2977 __ LoadConst32(rhs_reg, rhs_imm);
2978 }
2979 __ Slt(dst, rhs_reg, lhs);
2980 return (cond == kCondLE);
2981 }
2982
2983 case kCondB:
2984 case kCondAE:
2985 if (use_imm && IsInt<16>(rhs_imm)) {
2986 // Sltiu sign-extends its 16-bit immediate operand before
2987 // the comparison and thus lets us compare directly with
2988 // unsigned values in the ranges [0, 0x7fff] and
2989 // [0xffff8000, 0xffffffff].
2990 __ Sltiu(dst, lhs, rhs_imm);
2991 } else {
2992 if (use_imm) {
2993 rhs_reg = TMP;
2994 __ LoadConst32(rhs_reg, rhs_imm);
2995 }
2996 __ Sltu(dst, lhs, rhs_reg);
2997 }
2998 return (cond == kCondAE);
2999
3000 case kCondBE:
3001 case kCondA:
3002 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3003 // Simulate lhs <= rhs via lhs < rhs + 1.
3004 // Note that this only works if rhs + 1 does not overflow
3005 // to 0, hence the check above.
3006 // Sltiu sign-extends its 16-bit immediate operand before
3007 // the comparison and thus lets us compare directly with
3008 // unsigned values in the ranges [0, 0x7fff] and
3009 // [0xffff8000, 0xffffffff].
3010 __ Sltiu(dst, lhs, rhs_imm + 1);
3011 return (cond == kCondA);
3012 } else {
3013 if (use_imm) {
3014 rhs_reg = TMP;
3015 __ LoadConst32(rhs_reg, rhs_imm);
3016 }
3017 __ Sltu(dst, rhs_reg, lhs);
3018 return (cond == kCondBE);
3019 }
3020 }
3021}
3022
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003023void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
3024 LocationSummary* locations,
3025 MipsLabel* label) {
3026 Register lhs = locations->InAt(0).AsRegister<Register>();
3027 Location rhs_location = locations->InAt(1);
3028 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07003029 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003030 bool use_imm = rhs_location.IsConstant();
3031 if (use_imm) {
3032 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3033 } else {
3034 rhs_reg = rhs_location.AsRegister<Register>();
3035 }
3036
3037 if (use_imm && rhs_imm == 0) {
3038 switch (cond) {
3039 case kCondEQ:
3040 case kCondBE: // <= 0 if zero
3041 __ Beqz(lhs, label);
3042 break;
3043 case kCondNE:
3044 case kCondA: // > 0 if non-zero
3045 __ Bnez(lhs, label);
3046 break;
3047 case kCondLT:
3048 __ Bltz(lhs, label);
3049 break;
3050 case kCondGE:
3051 __ Bgez(lhs, label);
3052 break;
3053 case kCondLE:
3054 __ Blez(lhs, label);
3055 break;
3056 case kCondGT:
3057 __ Bgtz(lhs, label);
3058 break;
3059 case kCondB: // always false
3060 break;
3061 case kCondAE: // always true
3062 __ B(label);
3063 break;
3064 }
3065 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003066 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3067 if (isR6 || !use_imm) {
3068 if (use_imm) {
3069 rhs_reg = TMP;
3070 __ LoadConst32(rhs_reg, rhs_imm);
3071 }
3072 switch (cond) {
3073 case kCondEQ:
3074 __ Beq(lhs, rhs_reg, label);
3075 break;
3076 case kCondNE:
3077 __ Bne(lhs, rhs_reg, label);
3078 break;
3079 case kCondLT:
3080 __ Blt(lhs, rhs_reg, label);
3081 break;
3082 case kCondGE:
3083 __ Bge(lhs, rhs_reg, label);
3084 break;
3085 case kCondLE:
3086 __ Bge(rhs_reg, lhs, label);
3087 break;
3088 case kCondGT:
3089 __ Blt(rhs_reg, lhs, label);
3090 break;
3091 case kCondB:
3092 __ Bltu(lhs, rhs_reg, label);
3093 break;
3094 case kCondAE:
3095 __ Bgeu(lhs, rhs_reg, label);
3096 break;
3097 case kCondBE:
3098 __ Bgeu(rhs_reg, lhs, label);
3099 break;
3100 case kCondA:
3101 __ Bltu(rhs_reg, lhs, label);
3102 break;
3103 }
3104 } else {
3105 // Special cases for more efficient comparison with constants on R2.
3106 switch (cond) {
3107 case kCondEQ:
3108 __ LoadConst32(TMP, rhs_imm);
3109 __ Beq(lhs, TMP, label);
3110 break;
3111 case kCondNE:
3112 __ LoadConst32(TMP, rhs_imm);
3113 __ Bne(lhs, TMP, label);
3114 break;
3115 case kCondLT:
3116 if (IsInt<16>(rhs_imm)) {
3117 __ Slti(TMP, lhs, rhs_imm);
3118 __ Bnez(TMP, label);
3119 } else {
3120 __ LoadConst32(TMP, rhs_imm);
3121 __ Blt(lhs, TMP, label);
3122 }
3123 break;
3124 case kCondGE:
3125 if (IsInt<16>(rhs_imm)) {
3126 __ Slti(TMP, lhs, rhs_imm);
3127 __ Beqz(TMP, label);
3128 } else {
3129 __ LoadConst32(TMP, rhs_imm);
3130 __ Bge(lhs, TMP, label);
3131 }
3132 break;
3133 case kCondLE:
3134 if (IsInt<16>(rhs_imm + 1)) {
3135 // Simulate lhs <= rhs via lhs < rhs + 1.
3136 __ Slti(TMP, lhs, rhs_imm + 1);
3137 __ Bnez(TMP, label);
3138 } else {
3139 __ LoadConst32(TMP, rhs_imm);
3140 __ Bge(TMP, lhs, label);
3141 }
3142 break;
3143 case kCondGT:
3144 if (IsInt<16>(rhs_imm + 1)) {
3145 // Simulate lhs > rhs via !(lhs < rhs + 1).
3146 __ Slti(TMP, lhs, rhs_imm + 1);
3147 __ Beqz(TMP, label);
3148 } else {
3149 __ LoadConst32(TMP, rhs_imm);
3150 __ Blt(TMP, lhs, label);
3151 }
3152 break;
3153 case kCondB:
3154 if (IsInt<16>(rhs_imm)) {
3155 __ Sltiu(TMP, lhs, rhs_imm);
3156 __ Bnez(TMP, label);
3157 } else {
3158 __ LoadConst32(TMP, rhs_imm);
3159 __ Bltu(lhs, TMP, label);
3160 }
3161 break;
3162 case kCondAE:
3163 if (IsInt<16>(rhs_imm)) {
3164 __ Sltiu(TMP, lhs, rhs_imm);
3165 __ Beqz(TMP, label);
3166 } else {
3167 __ LoadConst32(TMP, rhs_imm);
3168 __ Bgeu(lhs, TMP, label);
3169 }
3170 break;
3171 case kCondBE:
3172 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3173 // Simulate lhs <= rhs via lhs < rhs + 1.
3174 // Note that this only works if rhs + 1 does not overflow
3175 // to 0, hence the check above.
3176 __ Sltiu(TMP, lhs, rhs_imm + 1);
3177 __ Bnez(TMP, label);
3178 } else {
3179 __ LoadConst32(TMP, rhs_imm);
3180 __ Bgeu(TMP, lhs, label);
3181 }
3182 break;
3183 case kCondA:
3184 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3185 // Simulate lhs > rhs via !(lhs < rhs + 1).
3186 // Note that this only works if rhs + 1 does not overflow
3187 // to 0, hence the check above.
3188 __ Sltiu(TMP, lhs, rhs_imm + 1);
3189 __ Beqz(TMP, label);
3190 } else {
3191 __ LoadConst32(TMP, rhs_imm);
3192 __ Bltu(TMP, lhs, label);
3193 }
3194 break;
3195 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003196 }
3197 }
3198}
3199
3200void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
3201 LocationSummary* locations,
3202 MipsLabel* label) {
3203 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3204 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3205 Location rhs_location = locations->InAt(1);
3206 Register rhs_high = ZERO;
3207 Register rhs_low = ZERO;
3208 int64_t imm = 0;
3209 uint32_t imm_high = 0;
3210 uint32_t imm_low = 0;
3211 bool use_imm = rhs_location.IsConstant();
3212 if (use_imm) {
3213 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
3214 imm_high = High32Bits(imm);
3215 imm_low = Low32Bits(imm);
3216 } else {
3217 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
3218 rhs_low = rhs_location.AsRegisterPairLow<Register>();
3219 }
3220
3221 if (use_imm && imm == 0) {
3222 switch (cond) {
3223 case kCondEQ:
3224 case kCondBE: // <= 0 if zero
3225 __ Or(TMP, lhs_high, lhs_low);
3226 __ Beqz(TMP, label);
3227 break;
3228 case kCondNE:
3229 case kCondA: // > 0 if non-zero
3230 __ Or(TMP, lhs_high, lhs_low);
3231 __ Bnez(TMP, label);
3232 break;
3233 case kCondLT:
3234 __ Bltz(lhs_high, label);
3235 break;
3236 case kCondGE:
3237 __ Bgez(lhs_high, label);
3238 break;
3239 case kCondLE:
3240 __ Or(TMP, lhs_high, lhs_low);
3241 __ Sra(AT, lhs_high, 31);
3242 __ Bgeu(AT, TMP, label);
3243 break;
3244 case kCondGT:
3245 __ Or(TMP, lhs_high, lhs_low);
3246 __ Sra(AT, lhs_high, 31);
3247 __ Bltu(AT, TMP, label);
3248 break;
3249 case kCondB: // always false
3250 break;
3251 case kCondAE: // always true
3252 __ B(label);
3253 break;
3254 }
3255 } else if (use_imm) {
3256 // TODO: more efficient comparison with constants without loading them into TMP/AT.
3257 switch (cond) {
3258 case kCondEQ:
3259 __ LoadConst32(TMP, imm_high);
3260 __ Xor(TMP, TMP, lhs_high);
3261 __ LoadConst32(AT, imm_low);
3262 __ Xor(AT, AT, lhs_low);
3263 __ Or(TMP, TMP, AT);
3264 __ Beqz(TMP, label);
3265 break;
3266 case kCondNE:
3267 __ LoadConst32(TMP, imm_high);
3268 __ Xor(TMP, TMP, lhs_high);
3269 __ LoadConst32(AT, imm_low);
3270 __ Xor(AT, AT, lhs_low);
3271 __ Or(TMP, TMP, AT);
3272 __ Bnez(TMP, label);
3273 break;
3274 case kCondLT:
3275 __ LoadConst32(TMP, imm_high);
3276 __ Blt(lhs_high, TMP, label);
3277 __ Slt(TMP, TMP, lhs_high);
3278 __ LoadConst32(AT, imm_low);
3279 __ Sltu(AT, lhs_low, AT);
3280 __ Blt(TMP, AT, label);
3281 break;
3282 case kCondGE:
3283 __ LoadConst32(TMP, imm_high);
3284 __ Blt(TMP, lhs_high, label);
3285 __ Slt(TMP, lhs_high, TMP);
3286 __ LoadConst32(AT, imm_low);
3287 __ Sltu(AT, lhs_low, AT);
3288 __ Or(TMP, TMP, AT);
3289 __ Beqz(TMP, label);
3290 break;
3291 case kCondLE:
3292 __ LoadConst32(TMP, imm_high);
3293 __ Blt(lhs_high, TMP, label);
3294 __ Slt(TMP, TMP, lhs_high);
3295 __ LoadConst32(AT, imm_low);
3296 __ Sltu(AT, AT, lhs_low);
3297 __ Or(TMP, TMP, AT);
3298 __ Beqz(TMP, label);
3299 break;
3300 case kCondGT:
3301 __ LoadConst32(TMP, imm_high);
3302 __ Blt(TMP, lhs_high, label);
3303 __ Slt(TMP, lhs_high, TMP);
3304 __ LoadConst32(AT, imm_low);
3305 __ Sltu(AT, AT, lhs_low);
3306 __ Blt(TMP, AT, label);
3307 break;
3308 case kCondB:
3309 __ LoadConst32(TMP, imm_high);
3310 __ Bltu(lhs_high, TMP, label);
3311 __ Sltu(TMP, TMP, lhs_high);
3312 __ LoadConst32(AT, imm_low);
3313 __ Sltu(AT, lhs_low, AT);
3314 __ Blt(TMP, AT, label);
3315 break;
3316 case kCondAE:
3317 __ LoadConst32(TMP, imm_high);
3318 __ Bltu(TMP, lhs_high, label);
3319 __ Sltu(TMP, lhs_high, TMP);
3320 __ LoadConst32(AT, imm_low);
3321 __ Sltu(AT, lhs_low, AT);
3322 __ Or(TMP, TMP, AT);
3323 __ Beqz(TMP, label);
3324 break;
3325 case kCondBE:
3326 __ LoadConst32(TMP, imm_high);
3327 __ Bltu(lhs_high, TMP, label);
3328 __ Sltu(TMP, TMP, lhs_high);
3329 __ LoadConst32(AT, imm_low);
3330 __ Sltu(AT, AT, lhs_low);
3331 __ Or(TMP, TMP, AT);
3332 __ Beqz(TMP, label);
3333 break;
3334 case kCondA:
3335 __ LoadConst32(TMP, imm_high);
3336 __ Bltu(TMP, lhs_high, label);
3337 __ Sltu(TMP, lhs_high, TMP);
3338 __ LoadConst32(AT, imm_low);
3339 __ Sltu(AT, AT, lhs_low);
3340 __ Blt(TMP, AT, label);
3341 break;
3342 }
3343 } else {
3344 switch (cond) {
3345 case kCondEQ:
3346 __ Xor(TMP, lhs_high, rhs_high);
3347 __ Xor(AT, lhs_low, rhs_low);
3348 __ Or(TMP, TMP, AT);
3349 __ Beqz(TMP, label);
3350 break;
3351 case kCondNE:
3352 __ Xor(TMP, lhs_high, rhs_high);
3353 __ Xor(AT, lhs_low, rhs_low);
3354 __ Or(TMP, TMP, AT);
3355 __ Bnez(TMP, label);
3356 break;
3357 case kCondLT:
3358 __ Blt(lhs_high, rhs_high, label);
3359 __ Slt(TMP, rhs_high, lhs_high);
3360 __ Sltu(AT, lhs_low, rhs_low);
3361 __ Blt(TMP, AT, label);
3362 break;
3363 case kCondGE:
3364 __ Blt(rhs_high, lhs_high, label);
3365 __ Slt(TMP, lhs_high, rhs_high);
3366 __ Sltu(AT, lhs_low, rhs_low);
3367 __ Or(TMP, TMP, AT);
3368 __ Beqz(TMP, label);
3369 break;
3370 case kCondLE:
3371 __ Blt(lhs_high, rhs_high, label);
3372 __ Slt(TMP, rhs_high, lhs_high);
3373 __ Sltu(AT, rhs_low, lhs_low);
3374 __ Or(TMP, TMP, AT);
3375 __ Beqz(TMP, label);
3376 break;
3377 case kCondGT:
3378 __ Blt(rhs_high, lhs_high, label);
3379 __ Slt(TMP, lhs_high, rhs_high);
3380 __ Sltu(AT, rhs_low, lhs_low);
3381 __ Blt(TMP, AT, label);
3382 break;
3383 case kCondB:
3384 __ Bltu(lhs_high, rhs_high, label);
3385 __ Sltu(TMP, rhs_high, lhs_high);
3386 __ Sltu(AT, lhs_low, rhs_low);
3387 __ Blt(TMP, AT, label);
3388 break;
3389 case kCondAE:
3390 __ Bltu(rhs_high, lhs_high, label);
3391 __ Sltu(TMP, lhs_high, rhs_high);
3392 __ Sltu(AT, lhs_low, rhs_low);
3393 __ Or(TMP, TMP, AT);
3394 __ Beqz(TMP, label);
3395 break;
3396 case kCondBE:
3397 __ Bltu(lhs_high, rhs_high, label);
3398 __ Sltu(TMP, rhs_high, lhs_high);
3399 __ Sltu(AT, rhs_low, lhs_low);
3400 __ Or(TMP, TMP, AT);
3401 __ Beqz(TMP, label);
3402 break;
3403 case kCondA:
3404 __ Bltu(rhs_high, lhs_high, label);
3405 __ Sltu(TMP, lhs_high, rhs_high);
3406 __ Sltu(AT, rhs_low, lhs_low);
3407 __ Blt(TMP, AT, label);
3408 break;
3409 }
3410 }
3411}
3412
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003413void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
3414 bool gt_bias,
3415 Primitive::Type type,
3416 LocationSummary* locations) {
3417 Register dst = locations->Out().AsRegister<Register>();
3418 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3419 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3420 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3421 if (type == Primitive::kPrimFloat) {
3422 if (isR6) {
3423 switch (cond) {
3424 case kCondEQ:
3425 __ CmpEqS(FTMP, lhs, rhs);
3426 __ Mfc1(dst, FTMP);
3427 __ Andi(dst, dst, 1);
3428 break;
3429 case kCondNE:
3430 __ CmpEqS(FTMP, lhs, rhs);
3431 __ Mfc1(dst, FTMP);
3432 __ Addiu(dst, dst, 1);
3433 break;
3434 case kCondLT:
3435 if (gt_bias) {
3436 __ CmpLtS(FTMP, lhs, rhs);
3437 } else {
3438 __ CmpUltS(FTMP, lhs, rhs);
3439 }
3440 __ Mfc1(dst, FTMP);
3441 __ Andi(dst, dst, 1);
3442 break;
3443 case kCondLE:
3444 if (gt_bias) {
3445 __ CmpLeS(FTMP, lhs, rhs);
3446 } else {
3447 __ CmpUleS(FTMP, lhs, rhs);
3448 }
3449 __ Mfc1(dst, FTMP);
3450 __ Andi(dst, dst, 1);
3451 break;
3452 case kCondGT:
3453 if (gt_bias) {
3454 __ CmpUltS(FTMP, rhs, lhs);
3455 } else {
3456 __ CmpLtS(FTMP, rhs, lhs);
3457 }
3458 __ Mfc1(dst, FTMP);
3459 __ Andi(dst, dst, 1);
3460 break;
3461 case kCondGE:
3462 if (gt_bias) {
3463 __ CmpUleS(FTMP, rhs, lhs);
3464 } else {
3465 __ CmpLeS(FTMP, rhs, lhs);
3466 }
3467 __ Mfc1(dst, FTMP);
3468 __ Andi(dst, dst, 1);
3469 break;
3470 default:
3471 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3472 UNREACHABLE();
3473 }
3474 } else {
3475 switch (cond) {
3476 case kCondEQ:
3477 __ CeqS(0, lhs, rhs);
3478 __ LoadConst32(dst, 1);
3479 __ Movf(dst, ZERO, 0);
3480 break;
3481 case kCondNE:
3482 __ CeqS(0, lhs, rhs);
3483 __ LoadConst32(dst, 1);
3484 __ Movt(dst, ZERO, 0);
3485 break;
3486 case kCondLT:
3487 if (gt_bias) {
3488 __ ColtS(0, lhs, rhs);
3489 } else {
3490 __ CultS(0, lhs, rhs);
3491 }
3492 __ LoadConst32(dst, 1);
3493 __ Movf(dst, ZERO, 0);
3494 break;
3495 case kCondLE:
3496 if (gt_bias) {
3497 __ ColeS(0, lhs, rhs);
3498 } else {
3499 __ CuleS(0, lhs, rhs);
3500 }
3501 __ LoadConst32(dst, 1);
3502 __ Movf(dst, ZERO, 0);
3503 break;
3504 case kCondGT:
3505 if (gt_bias) {
3506 __ CultS(0, rhs, lhs);
3507 } else {
3508 __ ColtS(0, rhs, lhs);
3509 }
3510 __ LoadConst32(dst, 1);
3511 __ Movf(dst, ZERO, 0);
3512 break;
3513 case kCondGE:
3514 if (gt_bias) {
3515 __ CuleS(0, rhs, lhs);
3516 } else {
3517 __ ColeS(0, rhs, lhs);
3518 }
3519 __ LoadConst32(dst, 1);
3520 __ Movf(dst, ZERO, 0);
3521 break;
3522 default:
3523 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3524 UNREACHABLE();
3525 }
3526 }
3527 } else {
3528 DCHECK_EQ(type, Primitive::kPrimDouble);
3529 if (isR6) {
3530 switch (cond) {
3531 case kCondEQ:
3532 __ CmpEqD(FTMP, lhs, rhs);
3533 __ Mfc1(dst, FTMP);
3534 __ Andi(dst, dst, 1);
3535 break;
3536 case kCondNE:
3537 __ CmpEqD(FTMP, lhs, rhs);
3538 __ Mfc1(dst, FTMP);
3539 __ Addiu(dst, dst, 1);
3540 break;
3541 case kCondLT:
3542 if (gt_bias) {
3543 __ CmpLtD(FTMP, lhs, rhs);
3544 } else {
3545 __ CmpUltD(FTMP, lhs, rhs);
3546 }
3547 __ Mfc1(dst, FTMP);
3548 __ Andi(dst, dst, 1);
3549 break;
3550 case kCondLE:
3551 if (gt_bias) {
3552 __ CmpLeD(FTMP, lhs, rhs);
3553 } else {
3554 __ CmpUleD(FTMP, lhs, rhs);
3555 }
3556 __ Mfc1(dst, FTMP);
3557 __ Andi(dst, dst, 1);
3558 break;
3559 case kCondGT:
3560 if (gt_bias) {
3561 __ CmpUltD(FTMP, rhs, lhs);
3562 } else {
3563 __ CmpLtD(FTMP, rhs, lhs);
3564 }
3565 __ Mfc1(dst, FTMP);
3566 __ Andi(dst, dst, 1);
3567 break;
3568 case kCondGE:
3569 if (gt_bias) {
3570 __ CmpUleD(FTMP, rhs, lhs);
3571 } else {
3572 __ CmpLeD(FTMP, rhs, lhs);
3573 }
3574 __ Mfc1(dst, FTMP);
3575 __ Andi(dst, dst, 1);
3576 break;
3577 default:
3578 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3579 UNREACHABLE();
3580 }
3581 } else {
3582 switch (cond) {
3583 case kCondEQ:
3584 __ CeqD(0, lhs, rhs);
3585 __ LoadConst32(dst, 1);
3586 __ Movf(dst, ZERO, 0);
3587 break;
3588 case kCondNE:
3589 __ CeqD(0, lhs, rhs);
3590 __ LoadConst32(dst, 1);
3591 __ Movt(dst, ZERO, 0);
3592 break;
3593 case kCondLT:
3594 if (gt_bias) {
3595 __ ColtD(0, lhs, rhs);
3596 } else {
3597 __ CultD(0, lhs, rhs);
3598 }
3599 __ LoadConst32(dst, 1);
3600 __ Movf(dst, ZERO, 0);
3601 break;
3602 case kCondLE:
3603 if (gt_bias) {
3604 __ ColeD(0, lhs, rhs);
3605 } else {
3606 __ CuleD(0, lhs, rhs);
3607 }
3608 __ LoadConst32(dst, 1);
3609 __ Movf(dst, ZERO, 0);
3610 break;
3611 case kCondGT:
3612 if (gt_bias) {
3613 __ CultD(0, rhs, lhs);
3614 } else {
3615 __ ColtD(0, rhs, lhs);
3616 }
3617 __ LoadConst32(dst, 1);
3618 __ Movf(dst, ZERO, 0);
3619 break;
3620 case kCondGE:
3621 if (gt_bias) {
3622 __ CuleD(0, rhs, lhs);
3623 } else {
3624 __ ColeD(0, rhs, lhs);
3625 }
3626 __ LoadConst32(dst, 1);
3627 __ Movf(dst, ZERO, 0);
3628 break;
3629 default:
3630 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
3631 UNREACHABLE();
3632 }
3633 }
3634 }
3635}
3636
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003637bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
3638 bool gt_bias,
3639 Primitive::Type type,
3640 LocationSummary* input_locations,
3641 int cc) {
3642 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3643 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3644 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
3645 if (type == Primitive::kPrimFloat) {
3646 switch (cond) {
3647 case kCondEQ:
3648 __ CeqS(cc, lhs, rhs);
3649 return false;
3650 case kCondNE:
3651 __ CeqS(cc, lhs, rhs);
3652 return true;
3653 case kCondLT:
3654 if (gt_bias) {
3655 __ ColtS(cc, lhs, rhs);
3656 } else {
3657 __ CultS(cc, lhs, rhs);
3658 }
3659 return false;
3660 case kCondLE:
3661 if (gt_bias) {
3662 __ ColeS(cc, lhs, rhs);
3663 } else {
3664 __ CuleS(cc, lhs, rhs);
3665 }
3666 return false;
3667 case kCondGT:
3668 if (gt_bias) {
3669 __ CultS(cc, rhs, lhs);
3670 } else {
3671 __ ColtS(cc, rhs, lhs);
3672 }
3673 return false;
3674 case kCondGE:
3675 if (gt_bias) {
3676 __ CuleS(cc, rhs, lhs);
3677 } else {
3678 __ ColeS(cc, rhs, lhs);
3679 }
3680 return false;
3681 default:
3682 LOG(FATAL) << "Unexpected non-floating-point condition";
3683 UNREACHABLE();
3684 }
3685 } else {
3686 DCHECK_EQ(type, Primitive::kPrimDouble);
3687 switch (cond) {
3688 case kCondEQ:
3689 __ CeqD(cc, lhs, rhs);
3690 return false;
3691 case kCondNE:
3692 __ CeqD(cc, lhs, rhs);
3693 return true;
3694 case kCondLT:
3695 if (gt_bias) {
3696 __ ColtD(cc, lhs, rhs);
3697 } else {
3698 __ CultD(cc, lhs, rhs);
3699 }
3700 return false;
3701 case kCondLE:
3702 if (gt_bias) {
3703 __ ColeD(cc, lhs, rhs);
3704 } else {
3705 __ CuleD(cc, lhs, rhs);
3706 }
3707 return false;
3708 case kCondGT:
3709 if (gt_bias) {
3710 __ CultD(cc, rhs, lhs);
3711 } else {
3712 __ ColtD(cc, rhs, lhs);
3713 }
3714 return false;
3715 case kCondGE:
3716 if (gt_bias) {
3717 __ CuleD(cc, rhs, lhs);
3718 } else {
3719 __ ColeD(cc, rhs, lhs);
3720 }
3721 return false;
3722 default:
3723 LOG(FATAL) << "Unexpected non-floating-point condition";
3724 UNREACHABLE();
3725 }
3726 }
3727}
3728
3729bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
3730 bool gt_bias,
3731 Primitive::Type type,
3732 LocationSummary* input_locations,
3733 FRegister dst) {
3734 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
3735 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
3736 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
3737 if (type == Primitive::kPrimFloat) {
3738 switch (cond) {
3739 case kCondEQ:
3740 __ CmpEqS(dst, lhs, rhs);
3741 return false;
3742 case kCondNE:
3743 __ CmpEqS(dst, lhs, rhs);
3744 return true;
3745 case kCondLT:
3746 if (gt_bias) {
3747 __ CmpLtS(dst, lhs, rhs);
3748 } else {
3749 __ CmpUltS(dst, lhs, rhs);
3750 }
3751 return false;
3752 case kCondLE:
3753 if (gt_bias) {
3754 __ CmpLeS(dst, lhs, rhs);
3755 } else {
3756 __ CmpUleS(dst, lhs, rhs);
3757 }
3758 return false;
3759 case kCondGT:
3760 if (gt_bias) {
3761 __ CmpUltS(dst, rhs, lhs);
3762 } else {
3763 __ CmpLtS(dst, rhs, lhs);
3764 }
3765 return false;
3766 case kCondGE:
3767 if (gt_bias) {
3768 __ CmpUleS(dst, rhs, lhs);
3769 } else {
3770 __ CmpLeS(dst, rhs, lhs);
3771 }
3772 return false;
3773 default:
3774 LOG(FATAL) << "Unexpected non-floating-point condition";
3775 UNREACHABLE();
3776 }
3777 } else {
3778 DCHECK_EQ(type, Primitive::kPrimDouble);
3779 switch (cond) {
3780 case kCondEQ:
3781 __ CmpEqD(dst, lhs, rhs);
3782 return false;
3783 case kCondNE:
3784 __ CmpEqD(dst, lhs, rhs);
3785 return true;
3786 case kCondLT:
3787 if (gt_bias) {
3788 __ CmpLtD(dst, lhs, rhs);
3789 } else {
3790 __ CmpUltD(dst, lhs, rhs);
3791 }
3792 return false;
3793 case kCondLE:
3794 if (gt_bias) {
3795 __ CmpLeD(dst, lhs, rhs);
3796 } else {
3797 __ CmpUleD(dst, lhs, rhs);
3798 }
3799 return false;
3800 case kCondGT:
3801 if (gt_bias) {
3802 __ CmpUltD(dst, rhs, lhs);
3803 } else {
3804 __ CmpLtD(dst, rhs, lhs);
3805 }
3806 return false;
3807 case kCondGE:
3808 if (gt_bias) {
3809 __ CmpUleD(dst, rhs, lhs);
3810 } else {
3811 __ CmpLeD(dst, rhs, lhs);
3812 }
3813 return false;
3814 default:
3815 LOG(FATAL) << "Unexpected non-floating-point condition";
3816 UNREACHABLE();
3817 }
3818 }
3819}
3820
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003821void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
3822 bool gt_bias,
3823 Primitive::Type type,
3824 LocationSummary* locations,
3825 MipsLabel* label) {
3826 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3827 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3828 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3829 if (type == Primitive::kPrimFloat) {
3830 if (isR6) {
3831 switch (cond) {
3832 case kCondEQ:
3833 __ CmpEqS(FTMP, lhs, rhs);
3834 __ Bc1nez(FTMP, label);
3835 break;
3836 case kCondNE:
3837 __ CmpEqS(FTMP, lhs, rhs);
3838 __ Bc1eqz(FTMP, label);
3839 break;
3840 case kCondLT:
3841 if (gt_bias) {
3842 __ CmpLtS(FTMP, lhs, rhs);
3843 } else {
3844 __ CmpUltS(FTMP, lhs, rhs);
3845 }
3846 __ Bc1nez(FTMP, label);
3847 break;
3848 case kCondLE:
3849 if (gt_bias) {
3850 __ CmpLeS(FTMP, lhs, rhs);
3851 } else {
3852 __ CmpUleS(FTMP, lhs, rhs);
3853 }
3854 __ Bc1nez(FTMP, label);
3855 break;
3856 case kCondGT:
3857 if (gt_bias) {
3858 __ CmpUltS(FTMP, rhs, lhs);
3859 } else {
3860 __ CmpLtS(FTMP, rhs, lhs);
3861 }
3862 __ Bc1nez(FTMP, label);
3863 break;
3864 case kCondGE:
3865 if (gt_bias) {
3866 __ CmpUleS(FTMP, rhs, lhs);
3867 } else {
3868 __ CmpLeS(FTMP, rhs, lhs);
3869 }
3870 __ Bc1nez(FTMP, label);
3871 break;
3872 default:
3873 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003874 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003875 }
3876 } else {
3877 switch (cond) {
3878 case kCondEQ:
3879 __ CeqS(0, lhs, rhs);
3880 __ Bc1t(0, label);
3881 break;
3882 case kCondNE:
3883 __ CeqS(0, lhs, rhs);
3884 __ Bc1f(0, label);
3885 break;
3886 case kCondLT:
3887 if (gt_bias) {
3888 __ ColtS(0, lhs, rhs);
3889 } else {
3890 __ CultS(0, lhs, rhs);
3891 }
3892 __ Bc1t(0, label);
3893 break;
3894 case kCondLE:
3895 if (gt_bias) {
3896 __ ColeS(0, lhs, rhs);
3897 } else {
3898 __ CuleS(0, lhs, rhs);
3899 }
3900 __ Bc1t(0, label);
3901 break;
3902 case kCondGT:
3903 if (gt_bias) {
3904 __ CultS(0, rhs, lhs);
3905 } else {
3906 __ ColtS(0, rhs, lhs);
3907 }
3908 __ Bc1t(0, label);
3909 break;
3910 case kCondGE:
3911 if (gt_bias) {
3912 __ CuleS(0, rhs, lhs);
3913 } else {
3914 __ ColeS(0, rhs, lhs);
3915 }
3916 __ Bc1t(0, label);
3917 break;
3918 default:
3919 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003920 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003921 }
3922 }
3923 } else {
3924 DCHECK_EQ(type, Primitive::kPrimDouble);
3925 if (isR6) {
3926 switch (cond) {
3927 case kCondEQ:
3928 __ CmpEqD(FTMP, lhs, rhs);
3929 __ Bc1nez(FTMP, label);
3930 break;
3931 case kCondNE:
3932 __ CmpEqD(FTMP, lhs, rhs);
3933 __ Bc1eqz(FTMP, label);
3934 break;
3935 case kCondLT:
3936 if (gt_bias) {
3937 __ CmpLtD(FTMP, lhs, rhs);
3938 } else {
3939 __ CmpUltD(FTMP, lhs, rhs);
3940 }
3941 __ Bc1nez(FTMP, label);
3942 break;
3943 case kCondLE:
3944 if (gt_bias) {
3945 __ CmpLeD(FTMP, lhs, rhs);
3946 } else {
3947 __ CmpUleD(FTMP, lhs, rhs);
3948 }
3949 __ Bc1nez(FTMP, label);
3950 break;
3951 case kCondGT:
3952 if (gt_bias) {
3953 __ CmpUltD(FTMP, rhs, lhs);
3954 } else {
3955 __ CmpLtD(FTMP, rhs, lhs);
3956 }
3957 __ Bc1nez(FTMP, label);
3958 break;
3959 case kCondGE:
3960 if (gt_bias) {
3961 __ CmpUleD(FTMP, rhs, lhs);
3962 } else {
3963 __ CmpLeD(FTMP, rhs, lhs);
3964 }
3965 __ Bc1nez(FTMP, label);
3966 break;
3967 default:
3968 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003969 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003970 }
3971 } else {
3972 switch (cond) {
3973 case kCondEQ:
3974 __ CeqD(0, lhs, rhs);
3975 __ Bc1t(0, label);
3976 break;
3977 case kCondNE:
3978 __ CeqD(0, lhs, rhs);
3979 __ Bc1f(0, label);
3980 break;
3981 case kCondLT:
3982 if (gt_bias) {
3983 __ ColtD(0, lhs, rhs);
3984 } else {
3985 __ CultD(0, lhs, rhs);
3986 }
3987 __ Bc1t(0, label);
3988 break;
3989 case kCondLE:
3990 if (gt_bias) {
3991 __ ColeD(0, lhs, rhs);
3992 } else {
3993 __ CuleD(0, lhs, rhs);
3994 }
3995 __ Bc1t(0, label);
3996 break;
3997 case kCondGT:
3998 if (gt_bias) {
3999 __ CultD(0, rhs, lhs);
4000 } else {
4001 __ ColtD(0, rhs, lhs);
4002 }
4003 __ Bc1t(0, label);
4004 break;
4005 case kCondGE:
4006 if (gt_bias) {
4007 __ CuleD(0, rhs, lhs);
4008 } else {
4009 __ ColeD(0, rhs, lhs);
4010 }
4011 __ Bc1t(0, label);
4012 break;
4013 default:
4014 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004015 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004016 }
4017 }
4018 }
4019}
4020
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004021void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00004022 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004023 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00004024 MipsLabel* false_target) {
4025 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004026
David Brazdil0debae72015-11-12 18:37:00 +00004027 if (true_target == nullptr && false_target == nullptr) {
4028 // Nothing to do. The code always falls through.
4029 return;
4030 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00004031 // Constant condition, statically compared against "true" (integer value 1).
4032 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00004033 if (true_target != nullptr) {
4034 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004035 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004036 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00004037 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00004038 if (false_target != nullptr) {
4039 __ B(false_target);
4040 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004041 }
David Brazdil0debae72015-11-12 18:37:00 +00004042 return;
4043 }
4044
4045 // The following code generates these patterns:
4046 // (1) true_target == nullptr && false_target != nullptr
4047 // - opposite condition true => branch to false_target
4048 // (2) true_target != nullptr && false_target == nullptr
4049 // - condition true => branch to true_target
4050 // (3) true_target != nullptr && false_target != nullptr
4051 // - condition true => branch to true_target
4052 // - branch to false_target
4053 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004054 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00004055 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004056 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004057 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00004058 __ Beqz(cond_val.AsRegister<Register>(), false_target);
4059 } else {
4060 __ Bnez(cond_val.AsRegister<Register>(), true_target);
4061 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004062 } else {
4063 // The condition instruction has not been materialized, use its inputs as
4064 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00004065 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004066 Primitive::Type type = condition->InputAt(0)->GetType();
4067 LocationSummary* locations = cond->GetLocations();
4068 IfCondition if_cond = condition->GetCondition();
4069 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00004070
David Brazdil0debae72015-11-12 18:37:00 +00004071 if (true_target == nullptr) {
4072 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004073 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00004074 }
4075
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004076 switch (type) {
4077 default:
4078 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
4079 break;
4080 case Primitive::kPrimLong:
4081 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
4082 break;
4083 case Primitive::kPrimFloat:
4084 case Primitive::kPrimDouble:
4085 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
4086 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004087 }
4088 }
David Brazdil0debae72015-11-12 18:37:00 +00004089
4090 // If neither branch falls through (case 3), the conditional branch to `true_target`
4091 // was already emitted (case 2) and we need to emit a jump to `false_target`.
4092 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004093 __ B(false_target);
4094 }
4095}
4096
4097void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
4098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00004099 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004100 locations->SetInAt(0, Location::RequiresRegister());
4101 }
4102}
4103
4104void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00004105 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
4106 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
4107 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
4108 nullptr : codegen_->GetLabelOf(true_successor);
4109 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
4110 nullptr : codegen_->GetLabelOf(false_successor);
4111 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112}
4113
4114void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
4115 LocationSummary* locations = new (GetGraph()->GetArena())
4116 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01004117 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00004118 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004119 locations->SetInAt(0, Location::RequiresRegister());
4120 }
4121}
4122
4123void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08004124 SlowPathCodeMIPS* slow_path =
4125 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00004126 GenerateTestAndBranch(deoptimize,
4127 /* condition_input_index */ 0,
4128 slow_path->GetEntryLabel(),
4129 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004130}
4131
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004132// This function returns true if a conditional move can be generated for HSelect.
4133// Otherwise it returns false and HSelect must be implemented in terms of conditonal
4134// branches and regular moves.
4135//
4136// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
4137//
4138// While determining feasibility of a conditional move and setting inputs/outputs
4139// are two distinct tasks, this function does both because they share quite a bit
4140// of common logic.
4141static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
4142 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
4143 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4144 HCondition* condition = cond->AsCondition();
4145
4146 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
4147 Primitive::Type dst_type = select->GetType();
4148
4149 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
4150 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
4151 bool is_true_value_zero_constant =
4152 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
4153 bool is_false_value_zero_constant =
4154 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
4155
4156 bool can_move_conditionally = false;
4157 bool use_const_for_false_in = false;
4158 bool use_const_for_true_in = false;
4159
4160 if (!cond->IsConstant()) {
4161 switch (cond_type) {
4162 default:
4163 switch (dst_type) {
4164 default:
4165 // Moving int on int condition.
4166 if (is_r6) {
4167 if (is_true_value_zero_constant) {
4168 // seleqz out_reg, false_reg, cond_reg
4169 can_move_conditionally = true;
4170 use_const_for_true_in = true;
4171 } else if (is_false_value_zero_constant) {
4172 // selnez out_reg, true_reg, cond_reg
4173 can_move_conditionally = true;
4174 use_const_for_false_in = true;
4175 } else if (materialized) {
4176 // Not materializing unmaterialized int conditions
4177 // to keep the instruction count low.
4178 // selnez AT, true_reg, cond_reg
4179 // seleqz TMP, false_reg, cond_reg
4180 // or out_reg, AT, TMP
4181 can_move_conditionally = true;
4182 }
4183 } else {
4184 // movn out_reg, true_reg/ZERO, cond_reg
4185 can_move_conditionally = true;
4186 use_const_for_true_in = is_true_value_zero_constant;
4187 }
4188 break;
4189 case Primitive::kPrimLong:
4190 // Moving long on int condition.
4191 if (is_r6) {
4192 if (is_true_value_zero_constant) {
4193 // seleqz out_reg_lo, false_reg_lo, cond_reg
4194 // seleqz out_reg_hi, false_reg_hi, cond_reg
4195 can_move_conditionally = true;
4196 use_const_for_true_in = true;
4197 } else if (is_false_value_zero_constant) {
4198 // selnez out_reg_lo, true_reg_lo, cond_reg
4199 // selnez out_reg_hi, true_reg_hi, cond_reg
4200 can_move_conditionally = true;
4201 use_const_for_false_in = true;
4202 }
4203 // Other long conditional moves would generate 6+ instructions,
4204 // which is too many.
4205 } else {
4206 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
4207 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
4208 can_move_conditionally = true;
4209 use_const_for_true_in = is_true_value_zero_constant;
4210 }
4211 break;
4212 case Primitive::kPrimFloat:
4213 case Primitive::kPrimDouble:
4214 // Moving float/double on int condition.
4215 if (is_r6) {
4216 if (materialized) {
4217 // Not materializing unmaterialized int conditions
4218 // to keep the instruction count low.
4219 can_move_conditionally = true;
4220 if (is_true_value_zero_constant) {
4221 // sltu TMP, ZERO, cond_reg
4222 // mtc1 TMP, temp_cond_reg
4223 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4224 use_const_for_true_in = true;
4225 } else if (is_false_value_zero_constant) {
4226 // sltu TMP, ZERO, cond_reg
4227 // mtc1 TMP, temp_cond_reg
4228 // selnez.fmt out_reg, true_reg, temp_cond_reg
4229 use_const_for_false_in = true;
4230 } else {
4231 // sltu TMP, ZERO, cond_reg
4232 // mtc1 TMP, temp_cond_reg
4233 // sel.fmt temp_cond_reg, false_reg, true_reg
4234 // mov.fmt out_reg, temp_cond_reg
4235 }
4236 }
4237 } else {
4238 // movn.fmt out_reg, true_reg, cond_reg
4239 can_move_conditionally = true;
4240 }
4241 break;
4242 }
4243 break;
4244 case Primitive::kPrimLong:
4245 // We don't materialize long comparison now
4246 // and use conditional branches instead.
4247 break;
4248 case Primitive::kPrimFloat:
4249 case Primitive::kPrimDouble:
4250 switch (dst_type) {
4251 default:
4252 // Moving int on float/double condition.
4253 if (is_r6) {
4254 if (is_true_value_zero_constant) {
4255 // mfc1 TMP, temp_cond_reg
4256 // seleqz out_reg, false_reg, TMP
4257 can_move_conditionally = true;
4258 use_const_for_true_in = true;
4259 } else if (is_false_value_zero_constant) {
4260 // mfc1 TMP, temp_cond_reg
4261 // selnez out_reg, true_reg, TMP
4262 can_move_conditionally = true;
4263 use_const_for_false_in = true;
4264 } else {
4265 // mfc1 TMP, temp_cond_reg
4266 // selnez AT, true_reg, TMP
4267 // seleqz TMP, false_reg, TMP
4268 // or out_reg, AT, TMP
4269 can_move_conditionally = true;
4270 }
4271 } else {
4272 // movt out_reg, true_reg/ZERO, cc
4273 can_move_conditionally = true;
4274 use_const_for_true_in = is_true_value_zero_constant;
4275 }
4276 break;
4277 case Primitive::kPrimLong:
4278 // Moving long on float/double condition.
4279 if (is_r6) {
4280 if (is_true_value_zero_constant) {
4281 // mfc1 TMP, temp_cond_reg
4282 // seleqz out_reg_lo, false_reg_lo, TMP
4283 // seleqz out_reg_hi, false_reg_hi, 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_lo, true_reg_lo, TMP
4289 // selnez out_reg_hi, true_reg_hi, TMP
4290 can_move_conditionally = true;
4291 use_const_for_false_in = true;
4292 }
4293 // Other long conditional moves would generate 6+ instructions,
4294 // which is too many.
4295 } else {
4296 // movt out_reg_lo, true_reg_lo/ZERO, cc
4297 // movt out_reg_hi, true_reg_hi/ZERO, cc
4298 can_move_conditionally = true;
4299 use_const_for_true_in = is_true_value_zero_constant;
4300 }
4301 break;
4302 case Primitive::kPrimFloat:
4303 case Primitive::kPrimDouble:
4304 // Moving float/double on float/double condition.
4305 if (is_r6) {
4306 can_move_conditionally = true;
4307 if (is_true_value_zero_constant) {
4308 // seleqz.fmt out_reg, false_reg, temp_cond_reg
4309 use_const_for_true_in = true;
4310 } else if (is_false_value_zero_constant) {
4311 // selnez.fmt out_reg, true_reg, temp_cond_reg
4312 use_const_for_false_in = true;
4313 } else {
4314 // sel.fmt temp_cond_reg, false_reg, true_reg
4315 // mov.fmt out_reg, temp_cond_reg
4316 }
4317 } else {
4318 // movt.fmt out_reg, true_reg, cc
4319 can_move_conditionally = true;
4320 }
4321 break;
4322 }
4323 break;
4324 }
4325 }
4326
4327 if (can_move_conditionally) {
4328 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
4329 } else {
4330 DCHECK(!use_const_for_false_in);
4331 DCHECK(!use_const_for_true_in);
4332 }
4333
4334 if (locations_to_set != nullptr) {
4335 if (use_const_for_false_in) {
4336 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
4337 } else {
4338 locations_to_set->SetInAt(0,
4339 Primitive::IsFloatingPointType(dst_type)
4340 ? Location::RequiresFpuRegister()
4341 : Location::RequiresRegister());
4342 }
4343 if (use_const_for_true_in) {
4344 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
4345 } else {
4346 locations_to_set->SetInAt(1,
4347 Primitive::IsFloatingPointType(dst_type)
4348 ? Location::RequiresFpuRegister()
4349 : Location::RequiresRegister());
4350 }
4351 if (materialized) {
4352 locations_to_set->SetInAt(2, Location::RequiresRegister());
4353 }
4354 // On R6 we don't require the output to be the same as the
4355 // first input for conditional moves unlike on R2.
4356 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
4357 if (is_out_same_as_first_in) {
4358 locations_to_set->SetOut(Location::SameAsFirstInput());
4359 } else {
4360 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
4361 ? Location::RequiresFpuRegister()
4362 : Location::RequiresRegister());
4363 }
4364 }
4365
4366 return can_move_conditionally;
4367}
4368
4369void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
4370 LocationSummary* locations = select->GetLocations();
4371 Location dst = locations->Out();
4372 Location src = locations->InAt(1);
4373 Register src_reg = ZERO;
4374 Register src_reg_high = ZERO;
4375 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4376 Register cond_reg = TMP;
4377 int cond_cc = 0;
4378 Primitive::Type cond_type = Primitive::kPrimInt;
4379 bool cond_inverted = false;
4380 Primitive::Type dst_type = select->GetType();
4381
4382 if (IsBooleanValueOrMaterializedCondition(cond)) {
4383 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4384 } else {
4385 HCondition* condition = cond->AsCondition();
4386 LocationSummary* cond_locations = cond->GetLocations();
4387 IfCondition if_cond = condition->GetCondition();
4388 cond_type = condition->InputAt(0)->GetType();
4389 switch (cond_type) {
4390 default:
4391 DCHECK_NE(cond_type, Primitive::kPrimLong);
4392 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4393 break;
4394 case Primitive::kPrimFloat:
4395 case Primitive::kPrimDouble:
4396 cond_inverted = MaterializeFpCompareR2(if_cond,
4397 condition->IsGtBias(),
4398 cond_type,
4399 cond_locations,
4400 cond_cc);
4401 break;
4402 }
4403 }
4404
4405 DCHECK(dst.Equals(locations->InAt(0)));
4406 if (src.IsRegister()) {
4407 src_reg = src.AsRegister<Register>();
4408 } else if (src.IsRegisterPair()) {
4409 src_reg = src.AsRegisterPairLow<Register>();
4410 src_reg_high = src.AsRegisterPairHigh<Register>();
4411 } else if (src.IsConstant()) {
4412 DCHECK(src.GetConstant()->IsZeroBitPattern());
4413 }
4414
4415 switch (cond_type) {
4416 default:
4417 switch (dst_type) {
4418 default:
4419 if (cond_inverted) {
4420 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
4421 } else {
4422 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
4423 }
4424 break;
4425 case Primitive::kPrimLong:
4426 if (cond_inverted) {
4427 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4428 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4429 } else {
4430 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
4431 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
4432 }
4433 break;
4434 case Primitive::kPrimFloat:
4435 if (cond_inverted) {
4436 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4437 } else {
4438 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4439 }
4440 break;
4441 case Primitive::kPrimDouble:
4442 if (cond_inverted) {
4443 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4444 } else {
4445 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
4446 }
4447 break;
4448 }
4449 break;
4450 case Primitive::kPrimLong:
4451 LOG(FATAL) << "Unreachable";
4452 UNREACHABLE();
4453 case Primitive::kPrimFloat:
4454 case Primitive::kPrimDouble:
4455 switch (dst_type) {
4456 default:
4457 if (cond_inverted) {
4458 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
4459 } else {
4460 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
4461 }
4462 break;
4463 case Primitive::kPrimLong:
4464 if (cond_inverted) {
4465 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4466 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4467 } else {
4468 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
4469 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
4470 }
4471 break;
4472 case Primitive::kPrimFloat:
4473 if (cond_inverted) {
4474 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4475 } else {
4476 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4477 }
4478 break;
4479 case Primitive::kPrimDouble:
4480 if (cond_inverted) {
4481 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4482 } else {
4483 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
4484 }
4485 break;
4486 }
4487 break;
4488 }
4489}
4490
4491void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
4492 LocationSummary* locations = select->GetLocations();
4493 Location dst = locations->Out();
4494 Location false_src = locations->InAt(0);
4495 Location true_src = locations->InAt(1);
4496 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
4497 Register cond_reg = TMP;
4498 FRegister fcond_reg = FTMP;
4499 Primitive::Type cond_type = Primitive::kPrimInt;
4500 bool cond_inverted = false;
4501 Primitive::Type dst_type = select->GetType();
4502
4503 if (IsBooleanValueOrMaterializedCondition(cond)) {
4504 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
4505 } else {
4506 HCondition* condition = cond->AsCondition();
4507 LocationSummary* cond_locations = cond->GetLocations();
4508 IfCondition if_cond = condition->GetCondition();
4509 cond_type = condition->InputAt(0)->GetType();
4510 switch (cond_type) {
4511 default:
4512 DCHECK_NE(cond_type, Primitive::kPrimLong);
4513 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
4514 break;
4515 case Primitive::kPrimFloat:
4516 case Primitive::kPrimDouble:
4517 cond_inverted = MaterializeFpCompareR6(if_cond,
4518 condition->IsGtBias(),
4519 cond_type,
4520 cond_locations,
4521 fcond_reg);
4522 break;
4523 }
4524 }
4525
4526 if (true_src.IsConstant()) {
4527 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
4528 }
4529 if (false_src.IsConstant()) {
4530 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
4531 }
4532
4533 switch (dst_type) {
4534 default:
4535 if (Primitive::IsFloatingPointType(cond_type)) {
4536 __ Mfc1(cond_reg, fcond_reg);
4537 }
4538 if (true_src.IsConstant()) {
4539 if (cond_inverted) {
4540 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4541 } else {
4542 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
4543 }
4544 } else if (false_src.IsConstant()) {
4545 if (cond_inverted) {
4546 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4547 } else {
4548 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
4549 }
4550 } else {
4551 DCHECK_NE(cond_reg, AT);
4552 if (cond_inverted) {
4553 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
4554 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
4555 } else {
4556 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
4557 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
4558 }
4559 __ Or(dst.AsRegister<Register>(), AT, TMP);
4560 }
4561 break;
4562 case Primitive::kPrimLong: {
4563 if (Primitive::IsFloatingPointType(cond_type)) {
4564 __ Mfc1(cond_reg, fcond_reg);
4565 }
4566 Register dst_lo = dst.AsRegisterPairLow<Register>();
4567 Register dst_hi = dst.AsRegisterPairHigh<Register>();
4568 if (true_src.IsConstant()) {
4569 Register src_lo = false_src.AsRegisterPairLow<Register>();
4570 Register src_hi = false_src.AsRegisterPairHigh<Register>();
4571 if (cond_inverted) {
4572 __ Selnez(dst_lo, src_lo, cond_reg);
4573 __ Selnez(dst_hi, src_hi, cond_reg);
4574 } else {
4575 __ Seleqz(dst_lo, src_lo, cond_reg);
4576 __ Seleqz(dst_hi, src_hi, cond_reg);
4577 }
4578 } else {
4579 DCHECK(false_src.IsConstant());
4580 Register src_lo = true_src.AsRegisterPairLow<Register>();
4581 Register src_hi = true_src.AsRegisterPairHigh<Register>();
4582 if (cond_inverted) {
4583 __ Seleqz(dst_lo, src_lo, cond_reg);
4584 __ Seleqz(dst_hi, src_hi, cond_reg);
4585 } else {
4586 __ Selnez(dst_lo, src_lo, cond_reg);
4587 __ Selnez(dst_hi, src_hi, cond_reg);
4588 }
4589 }
4590 break;
4591 }
4592 case Primitive::kPrimFloat: {
4593 if (!Primitive::IsFloatingPointType(cond_type)) {
4594 // sel*.fmt tests bit 0 of the condition register, account for that.
4595 __ Sltu(TMP, ZERO, cond_reg);
4596 __ Mtc1(TMP, fcond_reg);
4597 }
4598 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4599 if (true_src.IsConstant()) {
4600 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4601 if (cond_inverted) {
4602 __ SelnezS(dst_reg, src_reg, fcond_reg);
4603 } else {
4604 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4605 }
4606 } else if (false_src.IsConstant()) {
4607 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4608 if (cond_inverted) {
4609 __ SeleqzS(dst_reg, src_reg, fcond_reg);
4610 } else {
4611 __ SelnezS(dst_reg, src_reg, fcond_reg);
4612 }
4613 } else {
4614 if (cond_inverted) {
4615 __ SelS(fcond_reg,
4616 true_src.AsFpuRegister<FRegister>(),
4617 false_src.AsFpuRegister<FRegister>());
4618 } else {
4619 __ SelS(fcond_reg,
4620 false_src.AsFpuRegister<FRegister>(),
4621 true_src.AsFpuRegister<FRegister>());
4622 }
4623 __ MovS(dst_reg, fcond_reg);
4624 }
4625 break;
4626 }
4627 case Primitive::kPrimDouble: {
4628 if (!Primitive::IsFloatingPointType(cond_type)) {
4629 // sel*.fmt tests bit 0 of the condition register, account for that.
4630 __ Sltu(TMP, ZERO, cond_reg);
4631 __ Mtc1(TMP, fcond_reg);
4632 }
4633 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
4634 if (true_src.IsConstant()) {
4635 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
4636 if (cond_inverted) {
4637 __ SelnezD(dst_reg, src_reg, fcond_reg);
4638 } else {
4639 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4640 }
4641 } else if (false_src.IsConstant()) {
4642 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
4643 if (cond_inverted) {
4644 __ SeleqzD(dst_reg, src_reg, fcond_reg);
4645 } else {
4646 __ SelnezD(dst_reg, src_reg, fcond_reg);
4647 }
4648 } else {
4649 if (cond_inverted) {
4650 __ SelD(fcond_reg,
4651 true_src.AsFpuRegister<FRegister>(),
4652 false_src.AsFpuRegister<FRegister>());
4653 } else {
4654 __ SelD(fcond_reg,
4655 false_src.AsFpuRegister<FRegister>(),
4656 true_src.AsFpuRegister<FRegister>());
4657 }
4658 __ MovD(dst_reg, fcond_reg);
4659 }
4660 break;
4661 }
4662 }
4663}
4664
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004665void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4666 LocationSummary* locations = new (GetGraph()->GetArena())
4667 LocationSummary(flag, LocationSummary::kNoCall);
4668 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07004669}
4670
Goran Jakovljevicc6418422016-12-05 16:31:55 +01004671void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
4672 __ LoadFromOffset(kLoadWord,
4673 flag->GetLocations()->Out().AsRegister<Register>(),
4674 SP,
4675 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07004676}
4677
David Brazdil74eb1b22015-12-14 11:44:01 +00004678void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
4679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004680 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00004681}
4682
4683void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004684 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
4685 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
4686 if (is_r6) {
4687 GenConditionalMoveR6(select);
4688 } else {
4689 GenConditionalMoveR2(select);
4690 }
4691 } else {
4692 LocationSummary* locations = select->GetLocations();
4693 MipsLabel false_target;
4694 GenerateTestAndBranch(select,
4695 /* condition_input_index */ 2,
4696 /* true_target */ nullptr,
4697 &false_target);
4698 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
4699 __ Bind(&false_target);
4700 }
David Brazdil74eb1b22015-12-14 11:44:01 +00004701}
4702
David Srbecky0cf44932015-12-09 14:09:59 +00004703void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
4704 new (GetGraph()->GetArena()) LocationSummary(info);
4705}
4706
David Srbeckyd28f4a02016-03-14 17:14:24 +00004707void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
4708 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00004709}
4710
4711void CodeGeneratorMIPS::GenerateNop() {
4712 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00004713}
4714
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004715void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4716 Primitive::Type field_type = field_info.GetFieldType();
4717 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4718 bool generate_volatile = field_info.IsVolatile() && is_wide;
4719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004720 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004721
4722 locations->SetInAt(0, Location::RequiresRegister());
4723 if (generate_volatile) {
4724 InvokeRuntimeCallingConvention calling_convention;
4725 // need A0 to hold base + offset
4726 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4727 if (field_type == Primitive::kPrimLong) {
4728 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
4729 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004730 // Use Location::Any() to prevent situations when running out of available fp registers.
4731 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004732 // Need some temp core regs since FP results are returned in core registers
4733 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
4734 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
4735 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
4736 }
4737 } else {
4738 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4739 locations->SetOut(Location::RequiresFpuRegister());
4740 } else {
4741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4742 }
4743 }
4744}
4745
4746void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
4747 const FieldInfo& field_info,
4748 uint32_t dex_pc) {
4749 Primitive::Type type = field_info.GetFieldType();
4750 LocationSummary* locations = instruction->GetLocations();
4751 Register obj = locations->InAt(0).AsRegister<Register>();
4752 LoadOperandType load_type = kLoadUnsignedByte;
4753 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004754 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004755 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004756
4757 switch (type) {
4758 case Primitive::kPrimBoolean:
4759 load_type = kLoadUnsignedByte;
4760 break;
4761 case Primitive::kPrimByte:
4762 load_type = kLoadSignedByte;
4763 break;
4764 case Primitive::kPrimShort:
4765 load_type = kLoadSignedHalfword;
4766 break;
4767 case Primitive::kPrimChar:
4768 load_type = kLoadUnsignedHalfword;
4769 break;
4770 case Primitive::kPrimInt:
4771 case Primitive::kPrimFloat:
4772 case Primitive::kPrimNot:
4773 load_type = kLoadWord;
4774 break;
4775 case Primitive::kPrimLong:
4776 case Primitive::kPrimDouble:
4777 load_type = kLoadDoubleword;
4778 break;
4779 case Primitive::kPrimVoid:
4780 LOG(FATAL) << "Unreachable type " << type;
4781 UNREACHABLE();
4782 }
4783
4784 if (is_volatile && load_type == kLoadDoubleword) {
4785 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004786 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004787 // Do implicit Null check
4788 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4789 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01004790 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004791 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
4792 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004793 // FP results are returned in core registers. Need to move them.
4794 Location out = locations->Out();
4795 if (out.IsFpuRegister()) {
4796 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), out.AsFpuRegister<FRegister>());
4797 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
4798 out.AsFpuRegister<FRegister>());
4799 } else {
4800 DCHECK(out.IsDoubleStackSlot());
4801 __ StoreToOffset(kStoreWord,
4802 locations->GetTemp(1).AsRegister<Register>(),
4803 SP,
4804 out.GetStackIndex());
4805 __ StoreToOffset(kStoreWord,
4806 locations->GetTemp(2).AsRegister<Register>(),
4807 SP,
4808 out.GetStackIndex() + 4);
4809 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004810 }
4811 } else {
4812 if (!Primitive::IsFloatingPointType(type)) {
4813 Register dst;
4814 if (type == Primitive::kPrimLong) {
4815 DCHECK(locations->Out().IsRegisterPair());
4816 dst = locations->Out().AsRegisterPairLow<Register>();
4817 } else {
4818 DCHECK(locations->Out().IsRegister());
4819 dst = locations->Out().AsRegister<Register>();
4820 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004821 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004822 } else {
4823 DCHECK(locations->Out().IsFpuRegister());
4824 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4825 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004826 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004827 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004828 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004829 }
4830 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004831 }
4832
4833 if (is_volatile) {
4834 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4835 }
4836}
4837
4838void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4839 Primitive::Type field_type = field_info.GetFieldType();
4840 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
4841 bool generate_volatile = field_info.IsVolatile() && is_wide;
4842 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004843 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004844
4845 locations->SetInAt(0, Location::RequiresRegister());
4846 if (generate_volatile) {
4847 InvokeRuntimeCallingConvention calling_convention;
4848 // need A0 to hold base + offset
4849 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4850 if (field_type == Primitive::kPrimLong) {
4851 locations->SetInAt(1, Location::RegisterPairLocation(
4852 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4853 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004854 // Use Location::Any() to prevent situations when running out of available fp registers.
4855 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004856 // Pass FP parameters in core registers.
4857 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
4858 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
4859 }
4860 } else {
4861 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004862 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004863 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004864 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004865 }
4866 }
4867}
4868
4869void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
4870 const FieldInfo& field_info,
4871 uint32_t dex_pc) {
4872 Primitive::Type type = field_info.GetFieldType();
4873 LocationSummary* locations = instruction->GetLocations();
4874 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07004875 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004876 StoreOperandType store_type = kStoreByte;
4877 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004878 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunze2923db72016-08-20 01:55:47 -07004879 auto null_checker = GetImplicitNullChecker(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004880
4881 switch (type) {
4882 case Primitive::kPrimBoolean:
4883 case Primitive::kPrimByte:
4884 store_type = kStoreByte;
4885 break;
4886 case Primitive::kPrimShort:
4887 case Primitive::kPrimChar:
4888 store_type = kStoreHalfword;
4889 break;
4890 case Primitive::kPrimInt:
4891 case Primitive::kPrimFloat:
4892 case Primitive::kPrimNot:
4893 store_type = kStoreWord;
4894 break;
4895 case Primitive::kPrimLong:
4896 case Primitive::kPrimDouble:
4897 store_type = kStoreDoubleword;
4898 break;
4899 case Primitive::kPrimVoid:
4900 LOG(FATAL) << "Unreachable type " << type;
4901 UNREACHABLE();
4902 }
4903
4904 if (is_volatile) {
4905 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4906 }
4907
4908 if (is_volatile && store_type == kStoreDoubleword) {
4909 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01004910 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004911 // Do implicit Null check.
4912 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
4913 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4914 if (type == Primitive::kPrimDouble) {
4915 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07004916 if (value_location.IsFpuRegister()) {
4917 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
4918 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004919 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07004920 value_location.AsFpuRegister<FRegister>());
4921 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004922 __ LoadFromOffset(kLoadWord,
4923 locations->GetTemp(1).AsRegister<Register>(),
4924 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004925 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004926 __ LoadFromOffset(kLoadWord,
4927 locations->GetTemp(2).AsRegister<Register>(),
4928 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07004929 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004930 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004931 DCHECK(value_location.IsConstant());
4932 DCHECK(value_location.GetConstant()->IsDoubleConstant());
4933 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02004934 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
4935 locations->GetTemp(1).AsRegister<Register>(),
4936 value);
4937 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004938 }
Serban Constantinescufca16662016-07-14 09:21:59 +01004939 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004940 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
4941 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004942 if (value_location.IsConstant()) {
4943 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
4944 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
4945 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004946 Register src;
4947 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004948 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004949 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004950 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004951 }
Alexey Frunze2923db72016-08-20 01:55:47 -07004952 __ StoreToOffset(store_type, src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004953 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004954 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004955 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07004956 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004957 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07004958 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004959 }
4960 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004961 }
4962
4963 // TODO: memory barriers?
4964 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07004965 Register src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004966 codegen_->MarkGCCard(obj, src);
4967 }
4968
4969 if (is_volatile) {
4970 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4971 }
4972}
4973
4974void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4975 HandleFieldGet(instruction, instruction->GetFieldInfo());
4976}
4977
4978void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4979 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4980}
4981
4982void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4983 HandleFieldSet(instruction, instruction->GetFieldInfo());
4984}
4985
4986void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4987 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
4988}
4989
Alexey Frunze06a46c42016-07-19 15:00:40 -07004990void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(
4991 HInstruction* instruction ATTRIBUTE_UNUSED,
4992 Location root,
4993 Register obj,
4994 uint32_t offset) {
4995 Register root_reg = root.AsRegister<Register>();
4996 if (kEmitCompilerReadBarrier) {
4997 UNIMPLEMENTED(FATAL) << "for read barrier";
4998 } else {
4999 // Plain GC root load with no read barrier.
5000 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5001 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
5002 // Note that GC roots are not affected by heap poisoning, thus we
5003 // do not have to unpoison `root_reg` here.
5004 }
5005}
5006
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005007void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5008 LocationSummary::CallKind call_kind =
5009 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
5010 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5011 locations->SetInAt(0, Location::RequiresRegister());
5012 locations->SetInAt(1, Location::RequiresRegister());
5013 // The output does overlap inputs.
5014 // Note that TypeCheckSlowPathMIPS uses this register too.
5015 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5016}
5017
5018void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
5019 LocationSummary* locations = instruction->GetLocations();
5020 Register obj = locations->InAt(0).AsRegister<Register>();
5021 Register cls = locations->InAt(1).AsRegister<Register>();
5022 Register out = locations->Out().AsRegister<Register>();
5023
5024 MipsLabel done;
5025
5026 // Return 0 if `obj` is null.
5027 // TODO: Avoid this check if we know `obj` is not null.
5028 __ Move(out, ZERO);
5029 __ Beqz(obj, &done);
5030
5031 // Compare the class of `obj` with `cls`.
5032 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
5033 if (instruction->IsExactCheck()) {
5034 // Classes must be equal for the instanceof to succeed.
5035 __ Xor(out, out, cls);
5036 __ Sltiu(out, out, 1);
5037 } else {
5038 // If the classes are not equal, we go into a slow path.
5039 DCHECK(locations->OnlyCallsOnSlowPath());
5040 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
5041 codegen_->AddSlowPath(slow_path);
5042 __ Bne(out, cls, slow_path->GetEntryLabel());
5043 __ LoadConst32(out, 1);
5044 __ Bind(slow_path->GetExitLabel());
5045 }
5046
5047 __ Bind(&done);
5048}
5049
5050void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
5051 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5052 locations->SetOut(Location::ConstantLocation(constant));
5053}
5054
5055void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
5056 // Will be generated at use site.
5057}
5058
5059void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
5060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5061 locations->SetOut(Location::ConstantLocation(constant));
5062}
5063
5064void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
5065 // Will be generated at use site.
5066}
5067
5068void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
5069 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
5070 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
5071}
5072
5073void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5074 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005075 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005076 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08005077 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005078}
5079
5080void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
5081 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
5082 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005083 Location receiver = invoke->GetLocations()->InAt(0);
5084 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005085 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005086
5087 // Set the hidden argument.
5088 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
5089 invoke->GetDexMethodIndex());
5090
5091 // temp = object->GetClass();
5092 if (receiver.IsStackSlot()) {
5093 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
5094 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
5095 } else {
5096 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
5097 }
5098 codegen_->MaybeRecordImplicitNullCheck(invoke);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005099 __ LoadFromOffset(kLoadWord, temp, temp,
5100 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
5101 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005102 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005103 // temp = temp->GetImtEntryAt(method_offset);
5104 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5105 // T9 = temp->GetEntryPoint();
5106 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5107 // T9();
5108 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005109 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005110 DCHECK(!codegen_->IsLeafMethod());
5111 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5112}
5113
5114void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07005115 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5116 if (intrinsic.TryDispatch(invoke)) {
5117 return;
5118 }
5119
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005120 HandleInvoke(invoke);
5121}
5122
5123void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005124 // Explicit clinit checks triggered by static invokes must have been pruned by
5125 // art::PrepareForRegisterAllocation.
5126 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005127
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005128 bool has_extra_input = invoke->HasPcRelativeDexCache();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005129
Chris Larsen701566a2015-10-27 15:29:13 -07005130 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
5131 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005132 if (invoke->GetLocations()->CanCall() && has_extra_input) {
5133 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
5134 }
Chris Larsen701566a2015-10-27 15:29:13 -07005135 return;
5136 }
5137
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005138 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005139
5140 // Add the extra input register if either the dex cache array base register
5141 // or the PC-relative base register for accessing literals is needed.
5142 if (has_extra_input) {
5143 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
5144 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005145}
5146
Chris Larsen701566a2015-10-27 15:29:13 -07005147static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005148 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07005149 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
5150 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005151 return true;
5152 }
5153 return false;
5154}
5155
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005156HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07005157 HLoadString::LoadKind desired_string_load_kind) {
5158 if (kEmitCompilerReadBarrier) {
5159 UNIMPLEMENTED(FATAL) << "for read barrier";
5160 }
5161 // We disable PC-relative load when there is an irreducible loop, as the optimization
5162 // is incompatible with it.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005163 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5164 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005165 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5166 bool fallback_load = has_irreducible_loops;
5167 switch (desired_string_load_kind) {
5168 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5169 DCHECK(!GetCompilerOptions().GetCompilePic());
5170 break;
5171 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5172 DCHECK(GetCompilerOptions().GetCompilePic());
5173 break;
5174 case HLoadString::LoadKind::kBootImageAddress:
5175 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005176 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005177 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005178 break;
5179 case HLoadString::LoadKind::kDexCacheViaMethod:
5180 fallback_load = false;
5181 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005182 case HLoadString::LoadKind::kJitTableAddress:
5183 DCHECK(Runtime::Current()->UseJitCompilation());
5184 // TODO: implement.
5185 fallback_load = true;
5186 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005187 }
5188 if (fallback_load) {
5189 desired_string_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
5190 }
5191 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005192}
5193
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005194HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
5195 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005196 if (kEmitCompilerReadBarrier) {
5197 UNIMPLEMENTED(FATAL) << "for read barrier";
5198 }
5199 // We disable pc-relative load when there is an irreducible loop, as the optimization
5200 // is incompatible with it.
5201 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5202 bool fallback_load = has_irreducible_loops;
5203 switch (desired_class_load_kind) {
5204 case HLoadClass::LoadKind::kReferrersClass:
5205 fallback_load = false;
5206 break;
5207 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5208 DCHECK(!GetCompilerOptions().GetCompilePic());
5209 break;
5210 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5211 DCHECK(GetCompilerOptions().GetCompilePic());
5212 break;
5213 case HLoadClass::LoadKind::kBootImageAddress:
5214 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005215 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005216 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005217 fallback_load = true;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005218 break;
5219 case HLoadClass::LoadKind::kDexCachePcRelative:
5220 DCHECK(!Runtime::Current()->UseJitCompilation());
5221 // TODO: Create as many MipsDexCacheArraysBase instructions as needed for methods
5222 // with irreducible loops.
5223 break;
5224 case HLoadClass::LoadKind::kDexCacheViaMethod:
5225 fallback_load = false;
5226 break;
5227 }
5228 if (fallback_load) {
5229 desired_class_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
5230 }
5231 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005232}
5233
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005234Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5235 Register temp) {
5236 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5237 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
5238 if (!invoke->GetLocations()->Intrinsified()) {
5239 return location.AsRegister<Register>();
5240 }
5241 // For intrinsics we allow any location, so it may be on the stack.
5242 if (!location.IsRegister()) {
5243 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
5244 return temp;
5245 }
5246 // For register locations, check if the register was saved. If so, get it from the stack.
5247 // Note: There is a chance that the register was saved but not overwritten, so we could
5248 // save one load. However, since this is just an intrinsic slow path we prefer this
5249 // simple and more robust approach rather that trying to determine if that's the case.
5250 SlowPathCode* slow_path = GetCurrentSlowPath();
5251 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5252 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5253 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5254 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
5255 return temp;
5256 }
5257 return location.AsRegister<Register>();
5258}
5259
Vladimir Markodc151b22015-10-15 18:02:30 +01005260HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
5261 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005262 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005263 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
5264 // We disable PC-relative load when there is an irreducible loop, as the optimization
5265 // is incompatible with it.
5266 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
5267 bool fallback_load = true;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005268 switch (dispatch_info.method_load_kind) {
Vladimir Markodc151b22015-10-15 18:02:30 +01005269 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005270 fallback_load = has_irreducible_loops;
5271 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01005272 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005273 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01005274 break;
5275 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005276 if (fallback_load) {
5277 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
5278 dispatch_info.method_load_data = 0;
5279 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005280 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005281}
5282
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005283void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
5284 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005285 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005286 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
5287 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00005288 Register base_reg = invoke->HasPcRelativeDexCache()
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005289 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
5290 : ZERO;
5291
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005292 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005293 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005294 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005295 uint32_t offset =
5296 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005297 __ LoadFromOffset(kLoadWord,
5298 temp.AsRegister<Register>(),
5299 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005300 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005301 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005303 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00005304 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005305 break;
5306 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
5307 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
5308 break;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005309 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
5310 HMipsDexCacheArraysBase* base =
5311 invoke->InputAt(invoke->GetSpecialInputIndex())->AsMipsDexCacheArraysBase();
5312 int32_t offset =
5313 invoke->GetDexCacheArrayOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5314 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
5315 break;
5316 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005317 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005318 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005319 Register reg = temp.AsRegister<Register>();
5320 Register method_reg;
5321 if (current_method.IsRegister()) {
5322 method_reg = current_method.AsRegister<Register>();
5323 } else {
5324 // TODO: use the appropriate DCHECK() here if possible.
5325 // DCHECK(invoke->GetLocations()->Intrinsified());
5326 DCHECK(!current_method.IsValid());
5327 method_reg = reg;
5328 __ Lw(reg, SP, kCurrentMethodStackOffset);
5329 }
5330
5331 // temp = temp->dex_cache_resolved_methods_;
5332 __ LoadFromOffset(kLoadWord,
5333 reg,
5334 method_reg,
5335 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01005336 // temp = temp[index_in_cache];
5337 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
5338 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005339 __ LoadFromOffset(kLoadWord,
5340 reg,
5341 reg,
5342 CodeGenerator::GetCachePointerOffset(index_in_cache));
5343 break;
5344 }
5345 }
5346
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005347 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005348 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07005349 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005350 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005351 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5352 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01005353 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005354 T9,
5355 callee_method.AsRegister<Register>(),
5356 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005357 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005358 // T9()
5359 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005360 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005361 break;
5362 }
5363 DCHECK(!IsLeafMethod());
5364}
5365
5366void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00005367 // Explicit clinit checks triggered by static invokes must have been pruned by
5368 // art::PrepareForRegisterAllocation.
5369 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370
5371 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5372 return;
5373 }
5374
5375 LocationSummary* locations = invoke->GetLocations();
5376 codegen_->GenerateStaticOrDirectCall(invoke,
5377 locations->HasTemps()
5378 ? locations->GetTemp(0)
5379 : Location::NoLocation());
5380 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5381}
5382
Chris Larsen3acee732015-11-18 13:31:08 -08005383void CodeGeneratorMIPS::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02005384 // Use the calling convention instead of the location of the receiver, as
5385 // intrinsics may have put the receiver in a different register. In the intrinsics
5386 // slow path, the arguments have been moved to the right place, so here we are
5387 // guaranteed that the receiver is the first register of the calling convention.
5388 InvokeDexCallingConvention calling_convention;
5389 Register receiver = calling_convention.GetRegisterAt(0);
5390
Chris Larsen3acee732015-11-18 13:31:08 -08005391 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5393 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
5394 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07005395 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005396
5397 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02005398 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08005399 MaybeRecordImplicitNullCheck(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005400 // temp = temp->GetMethodAt(method_offset);
5401 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
5402 // T9 = temp->GetEntryPoint();
5403 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
5404 // T9();
5405 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005406 __ NopIfNoReordering();
Chris Larsen3acee732015-11-18 13:31:08 -08005407}
5408
5409void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
5410 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
5411 return;
5412 }
5413
5414 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005415 DCHECK(!codegen_->IsLeafMethod());
5416 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
5417}
5418
5419void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005420 if (cls->NeedsAccessCheck()) {
5421 InvokeRuntimeCallingConvention calling_convention;
5422 CodeGenerator::CreateLoadClassLocationSummary(
5423 cls,
5424 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5425 Location::RegisterLocation(V0),
5426 /* code_generator_supports_read_barrier */ false); // TODO: revisit this bool.
5427 return;
5428 }
5429
5430 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5431 ? LocationSummary::kCallOnSlowPath
5432 : LocationSummary::kNoCall;
5433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5434 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5435 switch (load_kind) {
5436 // We need an extra register for PC-relative literals on R2.
5437 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5438 case HLoadClass::LoadKind::kBootImageAddress:
5439 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5440 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5441 break;
5442 }
5443 FALLTHROUGH_INTENDED;
5444 // We need an extra register for PC-relative dex cache accesses.
5445 case HLoadClass::LoadKind::kDexCachePcRelative:
5446 case HLoadClass::LoadKind::kReferrersClass:
5447 case HLoadClass::LoadKind::kDexCacheViaMethod:
5448 locations->SetInAt(0, Location::RequiresRegister());
5449 break;
5450 default:
5451 break;
5452 }
5453 locations->SetOut(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005454}
5455
5456void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
5457 LocationSummary* locations = cls->GetLocations();
Pavle Batutae87a7182015-10-28 13:10:42 +01005458 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08005459 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005460 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005461 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Pavle Batutae87a7182015-10-28 13:10:42 +01005462 return;
5463 }
5464
Alexey Frunze06a46c42016-07-19 15:00:40 -07005465 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5466 Location out_loc = locations->Out();
5467 Register out = out_loc.AsRegister<Register>();
5468 Register base_or_current_method_reg;
5469 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5470 switch (load_kind) {
5471 // We need an extra register for PC-relative literals on R2.
5472 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5473 case HLoadClass::LoadKind::kBootImageAddress:
5474 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5475 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5476 break;
5477 // We need an extra register for PC-relative dex cache accesses.
5478 case HLoadClass::LoadKind::kDexCachePcRelative:
5479 case HLoadClass::LoadKind::kReferrersClass:
5480 case HLoadClass::LoadKind::kDexCacheViaMethod:
5481 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
5482 break;
5483 default:
5484 base_or_current_method_reg = ZERO;
5485 break;
5486 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005487
Alexey Frunze06a46c42016-07-19 15:00:40 -07005488 bool generate_null_check = false;
5489 switch (load_kind) {
5490 case HLoadClass::LoadKind::kReferrersClass: {
5491 DCHECK(!cls->CanCallRuntime());
5492 DCHECK(!cls->MustGenerateClinitCheck());
5493 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5494 GenerateGcRootFieldLoad(cls,
5495 out_loc,
5496 base_or_current_method_reg,
5497 ArtMethod::DeclaringClassOffset().Int32Value());
5498 break;
5499 }
5500 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5501 DCHECK(!kEmitCompilerReadBarrier);
5502 __ LoadLiteral(out,
5503 base_or_current_method_reg,
5504 codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5505 cls->GetTypeIndex()));
5506 break;
5507 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5508 DCHECK(!kEmitCompilerReadBarrier);
5509 CodeGeneratorMIPS::PcRelativePatchInfo* info =
5510 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005511 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005512 break;
5513 }
5514 case HLoadClass::LoadKind::kBootImageAddress: {
5515 DCHECK(!kEmitCompilerReadBarrier);
5516 DCHECK_NE(cls->GetAddress(), 0u);
5517 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5518 __ LoadLiteral(out,
5519 base_or_current_method_reg,
5520 codegen_->DeduplicateBootImageAddressLiteral(address));
5521 break;
5522 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005523 case HLoadClass::LoadKind::kJitTableAddress: {
5524 LOG(FATAL) << "Unimplemented";
Alexey Frunze06a46c42016-07-19 15:00:40 -07005525 break;
5526 }
5527 case HLoadClass::LoadKind::kDexCachePcRelative: {
5528 HMipsDexCacheArraysBase* base = cls->InputAt(0)->AsMipsDexCacheArraysBase();
5529 int32_t offset =
5530 cls->GetDexCacheElementOffset() - base->GetElementOffset() - kDexCacheArrayLwOffset;
5531 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
5532 GenerateGcRootFieldLoad(cls, out_loc, base_or_current_method_reg, offset);
5533 generate_null_check = !cls->IsInDexCache();
5534 break;
5535 }
5536 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5537 // /* GcRoot<mirror::Class>[] */ out =
5538 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5539 __ LoadFromOffset(kLoadWord,
5540 out,
5541 base_or_current_method_reg,
5542 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5543 // /* GcRoot<mirror::Class> */ out = out[type_index]
Andreas Gampea5b09a62016-11-17 15:21:22 -08005544 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005545 GenerateGcRootFieldLoad(cls, out_loc, out, offset);
5546 generate_null_check = !cls->IsInDexCache();
5547 }
5548 }
5549
5550 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5551 DCHECK(cls->CanCallRuntime());
5552 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
5553 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5554 codegen_->AddSlowPath(slow_path);
5555 if (generate_null_check) {
5556 __ Beqz(out, slow_path->GetEntryLabel());
5557 }
5558 if (cls->MustGenerateClinitCheck()) {
5559 GenerateClassInitializationCheck(slow_path, out);
5560 } else {
5561 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005562 }
5563 }
5564}
5565
5566static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005567 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005568}
5569
5570void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
5571 LocationSummary* locations =
5572 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5573 locations->SetOut(Location::RequiresRegister());
5574}
5575
5576void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
5577 Register out = load->GetLocations()->Out().AsRegister<Register>();
5578 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5579}
5580
5581void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
5582 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5583}
5584
5585void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5586 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
5587}
5588
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005589void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08005590 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005592 HLoadString::LoadKind load_kind = load->GetLoadKind();
5593 switch (load_kind) {
5594 // We need an extra register for PC-relative literals on R2.
5595 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5596 case HLoadString::LoadKind::kBootImageAddress:
5597 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005598 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005599 if (codegen_->GetInstructionSetFeatures().IsR6()) {
5600 break;
5601 }
5602 FALLTHROUGH_INTENDED;
5603 // We need an extra register for PC-relative dex cache accesses.
Alexey Frunze06a46c42016-07-19 15:00:40 -07005604 case HLoadString::LoadKind::kDexCacheViaMethod:
5605 locations->SetInAt(0, Location::RequiresRegister());
5606 break;
5607 default:
5608 break;
5609 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07005610 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
5611 InvokeRuntimeCallingConvention calling_convention;
5612 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5613 } else {
5614 locations->SetOut(Location::RequiresRegister());
5615 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005616}
5617
5618void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005619 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005620 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07005621 Location out_loc = locations->Out();
5622 Register out = out_loc.AsRegister<Register>();
5623 Register base_or_current_method_reg;
5624 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5625 switch (load_kind) {
5626 // We need an extra register for PC-relative literals on R2.
5627 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5628 case HLoadString::LoadKind::kBootImageAddress:
5629 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005630 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005631 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
5632 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005633 default:
5634 base_or_current_method_reg = ZERO;
5635 break;
5636 }
5637
5638 switch (load_kind) {
5639 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07005640 __ LoadLiteral(out,
5641 base_or_current_method_reg,
5642 codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5643 load->GetStringIndex()));
5644 return; // No dex cache slow path.
5645 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005646 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07005647 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005648 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005649 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
Alexey Frunze06a46c42016-07-19 15:00:40 -07005650 return; // No dex cache slow path.
5651 }
5652 case HLoadString::LoadKind::kBootImageAddress: {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005653 DCHECK_NE(load->GetAddress(), 0u);
5654 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5655 __ LoadLiteral(out,
5656 base_or_current_method_reg,
5657 codegen_->DeduplicateBootImageAddressLiteral(address));
5658 return; // No dex cache slow path.
5659 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005660 case HLoadString::LoadKind::kBssEntry: {
5661 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5662 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005663 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005664 codegen_->EmitPcRelativeAddressPlaceholder(info, out, base_or_current_method_reg);
5665 __ LoadFromOffset(kLoadWord, out, out, 0);
5666 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
5667 codegen_->AddSlowPath(slow_path);
5668 __ Beqz(out, slow_path->GetEntryLabel());
5669 __ Bind(slow_path->GetExitLabel());
5670 return;
5671 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07005672 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005673 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07005674 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005675
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005676 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Markoaad75c62016-10-03 08:46:48 +00005677 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5678 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005679 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005680 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5681 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005682}
5683
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005684void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
5685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5686 locations->SetOut(Location::ConstantLocation(constant));
5687}
5688
5689void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
5690 // Will be generated at use site.
5691}
5692
5693void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5694 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005696 InvokeRuntimeCallingConvention calling_convention;
5697 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5698}
5699
5700void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
5701 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01005702 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005703 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5704 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005705 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005706 }
5707 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5708}
5709
5710void LocationsBuilderMIPS::VisitMul(HMul* mul) {
5711 LocationSummary* locations =
5712 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5713 switch (mul->GetResultType()) {
5714 case Primitive::kPrimInt:
5715 case Primitive::kPrimLong:
5716 locations->SetInAt(0, Location::RequiresRegister());
5717 locations->SetInAt(1, Location::RequiresRegister());
5718 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5719 break;
5720
5721 case Primitive::kPrimFloat:
5722 case Primitive::kPrimDouble:
5723 locations->SetInAt(0, Location::RequiresFpuRegister());
5724 locations->SetInAt(1, Location::RequiresFpuRegister());
5725 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5726 break;
5727
5728 default:
5729 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5730 }
5731}
5732
5733void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
5734 Primitive::Type type = instruction->GetType();
5735 LocationSummary* locations = instruction->GetLocations();
5736 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5737
5738 switch (type) {
5739 case Primitive::kPrimInt: {
5740 Register dst = locations->Out().AsRegister<Register>();
5741 Register lhs = locations->InAt(0).AsRegister<Register>();
5742 Register rhs = locations->InAt(1).AsRegister<Register>();
5743
5744 if (isR6) {
5745 __ MulR6(dst, lhs, rhs);
5746 } else {
5747 __ MulR2(dst, lhs, rhs);
5748 }
5749 break;
5750 }
5751 case Primitive::kPrimLong: {
5752 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5753 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5754 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5755 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5756 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
5757 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
5758
5759 // Extra checks to protect caused by the existance of A1_A2.
5760 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
5761 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
5762 DCHECK_NE(dst_high, lhs_low);
5763 DCHECK_NE(dst_high, rhs_low);
5764
5765 // A_B * C_D
5766 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
5767 // dst_lo: [ low(B*D) ]
5768 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
5769
5770 if (isR6) {
5771 __ MulR6(TMP, lhs_high, rhs_low);
5772 __ MulR6(dst_high, lhs_low, rhs_high);
5773 __ Addu(dst_high, dst_high, TMP);
5774 __ MuhuR6(TMP, lhs_low, rhs_low);
5775 __ Addu(dst_high, dst_high, TMP);
5776 __ MulR6(dst_low, lhs_low, rhs_low);
5777 } else {
5778 __ MulR2(TMP, lhs_high, rhs_low);
5779 __ MulR2(dst_high, lhs_low, rhs_high);
5780 __ Addu(dst_high, dst_high, TMP);
5781 __ MultuR2(lhs_low, rhs_low);
5782 __ Mfhi(TMP);
5783 __ Addu(dst_high, dst_high, TMP);
5784 __ Mflo(dst_low);
5785 }
5786 break;
5787 }
5788 case Primitive::kPrimFloat:
5789 case Primitive::kPrimDouble: {
5790 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5791 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5792 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5793 if (type == Primitive::kPrimFloat) {
5794 __ MulS(dst, lhs, rhs);
5795 } else {
5796 __ MulD(dst, lhs, rhs);
5797 }
5798 break;
5799 }
5800 default:
5801 LOG(FATAL) << "Unexpected mul type " << type;
5802 }
5803}
5804
5805void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
5806 LocationSummary* locations =
5807 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5808 switch (neg->GetResultType()) {
5809 case Primitive::kPrimInt:
5810 case Primitive::kPrimLong:
5811 locations->SetInAt(0, Location::RequiresRegister());
5812 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5813 break;
5814
5815 case Primitive::kPrimFloat:
5816 case Primitive::kPrimDouble:
5817 locations->SetInAt(0, Location::RequiresFpuRegister());
5818 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5819 break;
5820
5821 default:
5822 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5823 }
5824}
5825
5826void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
5827 Primitive::Type type = instruction->GetType();
5828 LocationSummary* locations = instruction->GetLocations();
5829
5830 switch (type) {
5831 case Primitive::kPrimInt: {
5832 Register dst = locations->Out().AsRegister<Register>();
5833 Register src = locations->InAt(0).AsRegister<Register>();
5834 __ Subu(dst, ZERO, src);
5835 break;
5836 }
5837 case Primitive::kPrimLong: {
5838 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5839 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5840 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5841 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5842 __ Subu(dst_low, ZERO, src_low);
5843 __ Sltu(TMP, ZERO, dst_low);
5844 __ Subu(dst_high, ZERO, src_high);
5845 __ Subu(dst_high, dst_high, TMP);
5846 break;
5847 }
5848 case Primitive::kPrimFloat:
5849 case Primitive::kPrimDouble: {
5850 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
5851 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
5852 if (type == Primitive::kPrimFloat) {
5853 __ NegS(dst, src);
5854 } else {
5855 __ NegD(dst, src);
5856 }
5857 break;
5858 }
5859 default:
5860 LOG(FATAL) << "Unexpected neg type " << type;
5861 }
5862}
5863
5864void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
5865 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005867 InvokeRuntimeCallingConvention calling_convention;
5868 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5869 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
5870 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5871 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5872}
5873
5874void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
5875 InvokeRuntimeCallingConvention calling_convention;
5876 Register current_method_register = calling_convention.GetRegisterAt(2);
5877 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
5878 // Move an uint16_t value to a register.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005879 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Serban Constantinescufca16662016-07-14 09:21:59 +01005880 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005881 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
5882 void*, uint32_t, int32_t, ArtMethod*>();
5883}
5884
5885void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
5886 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005887 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005888 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005889 if (instruction->IsStringAlloc()) {
5890 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5891 } else {
5892 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5893 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5894 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005895 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5896}
5897
5898void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
David Brazdil6de19382016-01-08 17:37:10 +00005899 if (instruction->IsStringAlloc()) {
5900 // String is allocated through StringFactory. Call NewEmptyString entry point.
5901 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005902 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005903 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5904 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
5905 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07005906 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00005907 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5908 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01005909 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00005910 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
5911 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005912}
5913
5914void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
5915 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5916 locations->SetInAt(0, Location::RequiresRegister());
5917 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5918}
5919
5920void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
5921 Primitive::Type type = instruction->GetType();
5922 LocationSummary* locations = instruction->GetLocations();
5923
5924 switch (type) {
5925 case Primitive::kPrimInt: {
5926 Register dst = locations->Out().AsRegister<Register>();
5927 Register src = locations->InAt(0).AsRegister<Register>();
5928 __ Nor(dst, src, ZERO);
5929 break;
5930 }
5931
5932 case Primitive::kPrimLong: {
5933 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
5934 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
5935 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5936 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
5937 __ Nor(dst_high, src_high, ZERO);
5938 __ Nor(dst_low, src_low, ZERO);
5939 break;
5940 }
5941
5942 default:
5943 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5944 }
5945}
5946
5947void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5949 locations->SetInAt(0, Location::RequiresRegister());
5950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5951}
5952
5953void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
5954 LocationSummary* locations = instruction->GetLocations();
5955 __ Xori(locations->Out().AsRegister<Register>(),
5956 locations->InAt(0).AsRegister<Register>(),
5957 1);
5958}
5959
5960void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005961 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5962 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005963}
5964
Calin Juravle2ae48182016-03-16 14:05:09 +00005965void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
5966 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005967 return;
5968 }
5969 Location obj = instruction->GetLocations()->InAt(0);
5970
5971 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005972 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005973}
5974
Calin Juravle2ae48182016-03-16 14:05:09 +00005975void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005977 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005978
5979 Location obj = instruction->GetLocations()->InAt(0);
5980
5981 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
5982}
5983
5984void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005985 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005986}
5987
5988void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
5989 HandleBinaryOp(instruction);
5990}
5991
5992void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
5993 HandleBinaryOp(instruction);
5994}
5995
5996void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5997 LOG(FATAL) << "Unreachable";
5998}
5999
6000void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
6001 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6002}
6003
6004void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
6005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
6006 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
6007 if (location.IsStackSlot()) {
6008 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6009 } else if (location.IsDoubleStackSlot()) {
6010 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
6011 }
6012 locations->SetOut(location);
6013}
6014
6015void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
6016 ATTRIBUTE_UNUSED) {
6017 // Nothing to do, the parameter is already at its location.
6018}
6019
6020void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
6021 LocationSummary* locations =
6022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6023 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
6024}
6025
6026void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
6027 ATTRIBUTE_UNUSED) {
6028 // Nothing to do, the method is already at its location.
6029}
6030
6031void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
6032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01006033 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006034 locations->SetInAt(i, Location::Any());
6035 }
6036 locations->SetOut(Location::Any());
6037}
6038
6039void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
6040 LOG(FATAL) << "Unreachable";
6041}
6042
6043void LocationsBuilderMIPS::VisitRem(HRem* rem) {
6044 Primitive::Type type = rem->GetResultType();
6045 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006046 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
6048
6049 switch (type) {
6050 case Primitive::kPrimInt:
6051 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08006052 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6054 break;
6055
6056 case Primitive::kPrimLong: {
6057 InvokeRuntimeCallingConvention calling_convention;
6058 locations->SetInAt(0, Location::RegisterPairLocation(
6059 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6060 locations->SetInAt(1, Location::RegisterPairLocation(
6061 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6062 locations->SetOut(calling_convention.GetReturnLocation(type));
6063 break;
6064 }
6065
6066 case Primitive::kPrimFloat:
6067 case Primitive::kPrimDouble: {
6068 InvokeRuntimeCallingConvention calling_convention;
6069 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6070 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
6071 locations->SetOut(calling_convention.GetReturnLocation(type));
6072 break;
6073 }
6074
6075 default:
6076 LOG(FATAL) << "Unexpected rem type " << type;
6077 }
6078}
6079
6080void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
6081 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006082
6083 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08006084 case Primitive::kPrimInt:
6085 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006086 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006087 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006088 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006089 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
6090 break;
6091 }
6092 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006093 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006094 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006095 break;
6096 }
6097 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01006098 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006099 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100 break;
6101 }
6102 default:
6103 LOG(FATAL) << "Unexpected rem type " << type;
6104 }
6105}
6106
6107void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6108 memory_barrier->SetLocations(nullptr);
6109}
6110
6111void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
6112 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
6113}
6114
6115void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
6116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
6117 Primitive::Type return_type = ret->InputAt(0)->GetType();
6118 locations->SetInAt(0, MipsReturnLocation(return_type));
6119}
6120
6121void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
6122 codegen_->GenerateFrameExit();
6123}
6124
6125void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
6126 ret->SetLocations(nullptr);
6127}
6128
6129void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
6130 codegen_->GenerateFrameExit();
6131}
6132
Alexey Frunze92d90602015-12-18 18:16:36 -08006133void LocationsBuilderMIPS::VisitRor(HRor* ror) {
6134 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006135}
6136
Alexey Frunze92d90602015-12-18 18:16:36 -08006137void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
6138 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006139}
6140
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006141void LocationsBuilderMIPS::VisitShl(HShl* shl) {
6142 HandleShift(shl);
6143}
6144
6145void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
6146 HandleShift(shl);
6147}
6148
6149void LocationsBuilderMIPS::VisitShr(HShr* shr) {
6150 HandleShift(shr);
6151}
6152
6153void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
6154 HandleShift(shr);
6155}
6156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006157void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
6158 HandleBinaryOp(instruction);
6159}
6160
6161void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
6162 HandleBinaryOp(instruction);
6163}
6164
6165void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6166 HandleFieldGet(instruction, instruction->GetFieldInfo());
6167}
6168
6169void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6170 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6171}
6172
6173void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6174 HandleFieldSet(instruction, instruction->GetFieldInfo());
6175}
6176
6177void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6178 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6179}
6180
6181void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
6182 HUnresolvedInstanceFieldGet* instruction) {
6183 FieldAccessCallingConventionMIPS calling_convention;
6184 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6185 instruction->GetFieldType(),
6186 calling_convention);
6187}
6188
6189void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
6190 HUnresolvedInstanceFieldGet* instruction) {
6191 FieldAccessCallingConventionMIPS calling_convention;
6192 codegen_->GenerateUnresolvedFieldAccess(instruction,
6193 instruction->GetFieldType(),
6194 instruction->GetFieldIndex(),
6195 instruction->GetDexPc(),
6196 calling_convention);
6197}
6198
6199void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
6200 HUnresolvedInstanceFieldSet* instruction) {
6201 FieldAccessCallingConventionMIPS calling_convention;
6202 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6203 instruction->GetFieldType(),
6204 calling_convention);
6205}
6206
6207void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
6208 HUnresolvedInstanceFieldSet* instruction) {
6209 FieldAccessCallingConventionMIPS calling_convention;
6210 codegen_->GenerateUnresolvedFieldAccess(instruction,
6211 instruction->GetFieldType(),
6212 instruction->GetFieldIndex(),
6213 instruction->GetDexPc(),
6214 calling_convention);
6215}
6216
6217void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
6218 HUnresolvedStaticFieldGet* instruction) {
6219 FieldAccessCallingConventionMIPS calling_convention;
6220 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6221 instruction->GetFieldType(),
6222 calling_convention);
6223}
6224
6225void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
6226 HUnresolvedStaticFieldGet* instruction) {
6227 FieldAccessCallingConventionMIPS calling_convention;
6228 codegen_->GenerateUnresolvedFieldAccess(instruction,
6229 instruction->GetFieldType(),
6230 instruction->GetFieldIndex(),
6231 instruction->GetDexPc(),
6232 calling_convention);
6233}
6234
6235void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
6236 HUnresolvedStaticFieldSet* instruction) {
6237 FieldAccessCallingConventionMIPS calling_convention;
6238 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
6239 instruction->GetFieldType(),
6240 calling_convention);
6241}
6242
6243void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
6244 HUnresolvedStaticFieldSet* instruction) {
6245 FieldAccessCallingConventionMIPS calling_convention;
6246 codegen_->GenerateUnresolvedFieldAccess(instruction,
6247 instruction->GetFieldType(),
6248 instruction->GetFieldIndex(),
6249 instruction->GetDexPc(),
6250 calling_convention);
6251}
6252
6253void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006254 LocationSummary* locations =
6255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006256 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257}
6258
6259void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
6260 HBasicBlock* block = instruction->GetBlock();
6261 if (block->GetLoopInformation() != nullptr) {
6262 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6263 // The back edge will generate the suspend check.
6264 return;
6265 }
6266 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6267 // The goto will generate the suspend check.
6268 return;
6269 }
6270 GenerateSuspendCheck(instruction, nullptr);
6271}
6272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006273void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
6274 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006275 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 InvokeRuntimeCallingConvention calling_convention;
6277 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6278}
6279
6280void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01006281 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
6283}
6284
6285void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6286 Primitive::Type input_type = conversion->GetInputType();
6287 Primitive::Type result_type = conversion->GetResultType();
6288 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006289 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006290
6291 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
6292 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
6293 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
6294 }
6295
6296 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006297 if (!isR6 &&
6298 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
6299 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006300 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006301 }
6302
6303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
6304
6305 if (call_kind == LocationSummary::kNoCall) {
6306 if (Primitive::IsFloatingPointType(input_type)) {
6307 locations->SetInAt(0, Location::RequiresFpuRegister());
6308 } else {
6309 locations->SetInAt(0, Location::RequiresRegister());
6310 }
6311
6312 if (Primitive::IsFloatingPointType(result_type)) {
6313 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6314 } else {
6315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6316 }
6317 } else {
6318 InvokeRuntimeCallingConvention calling_convention;
6319
6320 if (Primitive::IsFloatingPointType(input_type)) {
6321 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
6322 } else {
6323 DCHECK_EQ(input_type, Primitive::kPrimLong);
6324 locations->SetInAt(0, Location::RegisterPairLocation(
6325 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
6326 }
6327
6328 locations->SetOut(calling_convention.GetReturnLocation(result_type));
6329 }
6330}
6331
6332void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
6333 LocationSummary* locations = conversion->GetLocations();
6334 Primitive::Type result_type = conversion->GetResultType();
6335 Primitive::Type input_type = conversion->GetInputType();
6336 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006337 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338
6339 DCHECK_NE(input_type, result_type);
6340
6341 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
6342 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6343 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6344 Register src = locations->InAt(0).AsRegister<Register>();
6345
Alexey Frunzea871ef12016-06-27 15:20:11 -07006346 if (dst_low != src) {
6347 __ Move(dst_low, src);
6348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006349 __ Sra(dst_high, src, 31);
6350 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
6351 Register dst = locations->Out().AsRegister<Register>();
6352 Register src = (input_type == Primitive::kPrimLong)
6353 ? locations->InAt(0).AsRegisterPairLow<Register>()
6354 : locations->InAt(0).AsRegister<Register>();
6355
6356 switch (result_type) {
6357 case Primitive::kPrimChar:
6358 __ Andi(dst, src, 0xFFFF);
6359 break;
6360 case Primitive::kPrimByte:
6361 if (has_sign_extension) {
6362 __ Seb(dst, src);
6363 } else {
6364 __ Sll(dst, src, 24);
6365 __ Sra(dst, dst, 24);
6366 }
6367 break;
6368 case Primitive::kPrimShort:
6369 if (has_sign_extension) {
6370 __ Seh(dst, src);
6371 } else {
6372 __ Sll(dst, src, 16);
6373 __ Sra(dst, dst, 16);
6374 }
6375 break;
6376 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07006377 if (dst != src) {
6378 __ Move(dst, src);
6379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006380 break;
6381
6382 default:
6383 LOG(FATAL) << "Unexpected type conversion from " << input_type
6384 << " to " << result_type;
6385 }
6386 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006387 if (input_type == Primitive::kPrimLong) {
6388 if (isR6) {
6389 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6390 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6391 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
6392 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
6393 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6394 __ Mtc1(src_low, FTMP);
6395 __ Mthc1(src_high, FTMP);
6396 if (result_type == Primitive::kPrimFloat) {
6397 __ Cvtsl(dst, FTMP);
6398 } else {
6399 __ Cvtdl(dst, FTMP);
6400 }
6401 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006402 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
6403 : kQuickL2d;
6404 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006405 if (result_type == Primitive::kPrimFloat) {
6406 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
6407 } else {
6408 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
6409 }
6410 }
6411 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006412 Register src = locations->InAt(0).AsRegister<Register>();
6413 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6414 __ Mtc1(src, FTMP);
6415 if (result_type == Primitive::kPrimFloat) {
6416 __ Cvtsw(dst, FTMP);
6417 } else {
6418 __ Cvtdw(dst, FTMP);
6419 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 }
6421 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
6422 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006423 if (result_type == Primitive::kPrimLong) {
6424 if (isR6) {
6425 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
6426 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
6427 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6428 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
6429 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
6430 MipsLabel truncate;
6431 MipsLabel done;
6432
6433 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
6434 // value when the input is either a NaN or is outside of the range of the output type
6435 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
6436 // the same result.
6437 //
6438 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
6439 // value of the output type if the input is outside of the range after the truncation or
6440 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
6441 // results. This matches the desired float/double-to-int/long conversion exactly.
6442 //
6443 // So, NAN2008 affects handling of negative values and NaNs by the truncate instruction.
6444 //
6445 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6446 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6447 // even though it must be NAN2008=1 on R6.
6448 //
6449 // The code takes care of the different behaviors by first comparing the input to the
6450 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
6451 // If the input is greater than or equal to the minimum, it procedes to the truncate
6452 // instruction, which will handle such an input the same way irrespective of NAN2008.
6453 // Otherwise the input is compared to itself to determine whether it is a NaN or not
6454 // in order to return either zero or the minimum value.
6455 //
6456 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6457 // truncate instruction for MIPS64R6.
6458 if (input_type == Primitive::kPrimFloat) {
6459 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int64_t>::min());
6460 __ LoadConst32(TMP, min_val);
6461 __ Mtc1(TMP, FTMP);
6462 __ CmpLeS(FTMP, FTMP, src);
6463 } else {
6464 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int64_t>::min());
6465 __ LoadConst32(TMP, High32Bits(min_val));
6466 __ Mtc1(ZERO, FTMP);
6467 __ Mthc1(TMP, FTMP);
6468 __ CmpLeD(FTMP, FTMP, src);
6469 }
6470
6471 __ Bc1nez(FTMP, &truncate);
6472
6473 if (input_type == Primitive::kPrimFloat) {
6474 __ CmpEqS(FTMP, src, src);
6475 } else {
6476 __ CmpEqD(FTMP, src, src);
6477 }
6478 __ Move(dst_low, ZERO);
6479 __ LoadConst32(dst_high, std::numeric_limits<int32_t>::min());
6480 __ Mfc1(TMP, FTMP);
6481 __ And(dst_high, dst_high, TMP);
6482
6483 __ B(&done);
6484
6485 __ Bind(&truncate);
6486
6487 if (input_type == Primitive::kPrimFloat) {
6488 __ TruncLS(FTMP, src);
6489 } else {
6490 __ TruncLD(FTMP, src);
6491 }
6492 __ Mfc1(dst_low, FTMP);
6493 __ Mfhc1(dst_high, FTMP);
6494
6495 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01006497 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
6498 : kQuickD2l;
6499 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006500 if (input_type == Primitive::kPrimFloat) {
6501 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
6502 } else {
6503 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
6504 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006505 }
6506 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006507 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6508 Register dst = locations->Out().AsRegister<Register>();
6509 MipsLabel truncate;
6510 MipsLabel done;
6511
6512 // The following code supports both NAN2008=0 and NAN2008=1 behaviors of the truncate
6513 // instruction, the reason being that the emulator implements NAN2008=0 on MIPS64R6,
6514 // even though it must be NAN2008=1 on R6.
6515 //
6516 // For details see the large comment above for the truncation of float/double to long on R6.
6517 //
6518 // TODO: simplify this when the emulator correctly implements NAN2008=1 behavior of the
6519 // truncate instruction for MIPS64R6.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006521 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
6522 __ LoadConst32(TMP, min_val);
6523 __ Mtc1(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006524 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006525 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
6526 __ LoadConst32(TMP, High32Bits(min_val));
6527 __ Mtc1(ZERO, FTMP);
Alexey Frunzea871ef12016-06-27 15:20:11 -07006528 __ MoveToFpuHigh(TMP, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08006530
6531 if (isR6) {
6532 if (input_type == Primitive::kPrimFloat) {
6533 __ CmpLeS(FTMP, FTMP, src);
6534 } else {
6535 __ CmpLeD(FTMP, FTMP, src);
6536 }
6537 __ Bc1nez(FTMP, &truncate);
6538
6539 if (input_type == Primitive::kPrimFloat) {
6540 __ CmpEqS(FTMP, src, src);
6541 } else {
6542 __ CmpEqD(FTMP, src, src);
6543 }
6544 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6545 __ Mfc1(TMP, FTMP);
6546 __ And(dst, dst, TMP);
6547 } else {
6548 if (input_type == Primitive::kPrimFloat) {
6549 __ ColeS(0, FTMP, src);
6550 } else {
6551 __ ColeD(0, FTMP, src);
6552 }
6553 __ Bc1t(0, &truncate);
6554
6555 if (input_type == Primitive::kPrimFloat) {
6556 __ CeqS(0, src, src);
6557 } else {
6558 __ CeqD(0, src, src);
6559 }
6560 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
6561 __ Movf(dst, ZERO, 0);
6562 }
6563
6564 __ B(&done);
6565
6566 __ Bind(&truncate);
6567
6568 if (input_type == Primitive::kPrimFloat) {
6569 __ TruncWS(FTMP, src);
6570 } else {
6571 __ TruncWD(FTMP, src);
6572 }
6573 __ Mfc1(dst, FTMP);
6574
6575 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006576 }
6577 } else if (Primitive::IsFloatingPointType(result_type) &&
6578 Primitive::IsFloatingPointType(input_type)) {
6579 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
6580 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
6581 if (result_type == Primitive::kPrimFloat) {
6582 __ Cvtsd(dst, src);
6583 } else {
6584 __ Cvtds(dst, src);
6585 }
6586 } else {
6587 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
6588 << " to " << result_type;
6589 }
6590}
6591
6592void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
6593 HandleShift(ushr);
6594}
6595
6596void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
6597 HandleShift(ushr);
6598}
6599
6600void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
6601 HandleBinaryOp(instruction);
6602}
6603
6604void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
6605 HandleBinaryOp(instruction);
6606}
6607
6608void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6609 // Nothing to do, this should be removed during prepare for register allocator.
6610 LOG(FATAL) << "Unreachable";
6611}
6612
6613void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
6614 // Nothing to do, this should be removed during prepare for register allocator.
6615 LOG(FATAL) << "Unreachable";
6616}
6617
6618void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006619 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006620}
6621
6622void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006623 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006624}
6625
6626void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006627 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006628}
6629
6630void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006631 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006632}
6633
6634void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006635 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006636}
6637
6638void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006639 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006640}
6641
6642void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006643 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006644}
6645
6646void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006647 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648}
6649
6650void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006651 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652}
6653
6654void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006655 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006656}
6657
6658void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006659 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660}
6661
6662void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006663 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006664}
6665
6666void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006667 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006668}
6669
6670void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006671 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006672}
6673
6674void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006675 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006676}
6677
6678void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006679 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680}
6681
6682void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006683 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684}
6685
6686void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006687 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688}
6689
6690void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006691 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006692}
6693
6694void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006695 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696}
6697
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6699 LocationSummary* locations =
6700 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6701 locations->SetInAt(0, Location::RequiresRegister());
6702}
6703
Alexey Frunze96b66822016-09-10 02:32:44 -07006704void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
6705 int32_t lower_bound,
6706 uint32_t num_entries,
6707 HBasicBlock* switch_block,
6708 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006709 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006710 Register temp_reg = TMP;
6711 __ Addiu32(temp_reg, value_reg, -lower_bound);
6712 // Jump to default if index is negative
6713 // Note: We don't check the case that index is positive while value < lower_bound, because in
6714 // this case, index >= num_entries must be true. So that we can save one branch instruction.
6715 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
6716
Alexey Frunze96b66822016-09-10 02:32:44 -07006717 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006718 // Jump to successors[0] if value == lower_bound.
6719 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
6720 int32_t last_index = 0;
6721 for (; num_entries - last_index > 2; last_index += 2) {
6722 __ Addiu(temp_reg, temp_reg, -2);
6723 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
6724 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
6725 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
6726 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
6727 }
6728 if (num_entries - last_index == 2) {
6729 // The last missing case_value.
6730 __ Addiu(temp_reg, temp_reg, -1);
6731 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732 }
6733
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006734 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07006735 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006736 __ B(codegen_->GetLabelOf(default_block));
6737 }
6738}
6739
Alexey Frunze96b66822016-09-10 02:32:44 -07006740void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
6741 Register constant_area,
6742 int32_t lower_bound,
6743 uint32_t num_entries,
6744 HBasicBlock* switch_block,
6745 HBasicBlock* default_block) {
6746 // Create a jump table.
6747 std::vector<MipsLabel*> labels(num_entries);
6748 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6749 for (uint32_t i = 0; i < num_entries; i++) {
6750 labels[i] = codegen_->GetLabelOf(successors[i]);
6751 }
6752 JumpTable* table = __ CreateJumpTable(std::move(labels));
6753
6754 // Is the value in range?
6755 __ Addiu32(TMP, value_reg, -lower_bound);
6756 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
6757 __ Sltiu(AT, TMP, num_entries);
6758 __ Beqz(AT, codegen_->GetLabelOf(default_block));
6759 } else {
6760 __ LoadConst32(AT, num_entries);
6761 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
6762 }
6763
6764 // We are in the range of the table.
6765 // Load the target address from the jump table, indexing by the value.
6766 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
6767 __ Sll(TMP, TMP, 2);
6768 __ Addu(TMP, TMP, AT);
6769 __ Lw(TMP, TMP, 0);
6770 // Compute the absolute target address by adding the table start address
6771 // (the table contains offsets to targets relative to its start).
6772 __ Addu(TMP, TMP, AT);
6773 // And jump.
6774 __ Jr(TMP);
6775 __ NopIfNoReordering();
6776}
6777
6778void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6779 int32_t lower_bound = switch_instr->GetStartValue();
6780 uint32_t num_entries = switch_instr->GetNumEntries();
6781 LocationSummary* locations = switch_instr->GetLocations();
6782 Register value_reg = locations->InAt(0).AsRegister<Register>();
6783 HBasicBlock* switch_block = switch_instr->GetBlock();
6784 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6785
6786 if (codegen_->GetInstructionSetFeatures().IsR6() &&
6787 num_entries > kPackedSwitchJumpTableThreshold) {
6788 // R6 uses PC-relative addressing to access the jump table.
6789 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
6790 // the jump table and it is implemented by changing HPackedSwitch to
6791 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
6792 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
6793 GenTableBasedPackedSwitch(value_reg,
6794 ZERO,
6795 lower_bound,
6796 num_entries,
6797 switch_block,
6798 default_block);
6799 } else {
6800 GenPackedSwitchWithCompares(value_reg,
6801 lower_bound,
6802 num_entries,
6803 switch_block,
6804 default_block);
6805 }
6806}
6807
6808void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6809 LocationSummary* locations =
6810 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6811 locations->SetInAt(0, Location::RequiresRegister());
6812 // Constant area pointer (HMipsComputeBaseMethodAddress).
6813 locations->SetInAt(1, Location::RequiresRegister());
6814}
6815
6816void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
6817 int32_t lower_bound = switch_instr->GetStartValue();
6818 uint32_t num_entries = switch_instr->GetNumEntries();
6819 LocationSummary* locations = switch_instr->GetLocations();
6820 Register value_reg = locations->InAt(0).AsRegister<Register>();
6821 Register constant_area = locations->InAt(1).AsRegister<Register>();
6822 HBasicBlock* switch_block = switch_instr->GetBlock();
6823 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6824
6825 // This is an R2-only path. HPackedSwitch has been changed to
6826 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
6827 // required to address the jump table relative to PC.
6828 GenTableBasedPackedSwitch(value_reg,
6829 constant_area,
6830 lower_bound,
6831 num_entries,
6832 switch_block,
6833 default_block);
6834}
6835
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006836void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
6837 HMipsComputeBaseMethodAddress* insn) {
6838 LocationSummary* locations =
6839 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6840 locations->SetOut(Location::RequiresRegister());
6841}
6842
6843void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
6844 HMipsComputeBaseMethodAddress* insn) {
6845 LocationSummary* locations = insn->GetLocations();
6846 Register reg = locations->Out().AsRegister<Register>();
6847
6848 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
6849
6850 // Generate a dummy PC-relative call to obtain PC.
6851 __ Nal();
6852 // Grab the return address off RA.
6853 __ Move(reg, RA);
Alexey Frunze06a46c42016-07-19 15:00:40 -07006854 // TODO: Can we share this code with that of VisitMipsDexCacheArraysBase()?
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006855
6856 // Remember this offset (the obtained PC value) for later use with constant area.
6857 __ BindPcRelBaseLabel();
6858}
6859
6860void LocationsBuilderMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6861 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
6862 locations->SetOut(Location::RequiresRegister());
6863}
6864
6865void InstructionCodeGeneratorMIPS::VisitMipsDexCacheArraysBase(HMipsDexCacheArraysBase* base) {
6866 Register reg = base->GetLocations()->Out().AsRegister<Register>();
6867 CodeGeneratorMIPS::PcRelativePatchInfo* info =
6868 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006869 // TODO: Reuse MipsComputeBaseMethodAddress on R2 instead of passing ZERO to force emitting NAL.
6870 codegen_->EmitPcRelativeAddressPlaceholder(info, reg, ZERO);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006871}
6872
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006873void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6874 // The trampoline uses the same calling convention as dex calling conventions,
6875 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
6876 // the method_idx.
6877 HandleInvoke(invoke);
6878}
6879
6880void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
6881 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
6882}
6883
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006884void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6885 LocationSummary* locations =
6886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6887 locations->SetInAt(0, Location::RequiresRegister());
6888 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006889}
6890
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006891void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
6892 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006893 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006894 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006895 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006896 __ LoadFromOffset(kLoadWord,
6897 locations->Out().AsRegister<Register>(),
6898 locations->InAt(0).AsRegister<Register>(),
6899 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006900 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006901 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006902 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006903 __ LoadFromOffset(kLoadWord,
6904 locations->Out().AsRegister<Register>(),
6905 locations->InAt(0).AsRegister<Register>(),
6906 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006907 __ LoadFromOffset(kLoadWord,
6908 locations->Out().AsRegister<Register>(),
6909 locations->Out().AsRegister<Register>(),
6910 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00006911 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006912}
6913
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006914#undef __
6915#undef QUICK_ENTRY_POINT
6916
6917} // namespace mips
6918} // namespace art